3 'modules/documentCanvas/canvas/utils',
4 ], function($, utils) {
8 var Listener = function(canvas) {
12 $.extend(Listener.prototype, {
13 listenTo: function(wlxmlDocument) {
14 if(wlxmlDocument === this.wlxmlDocument) {
17 this.wlxmlDocument = wlxmlDocument;
19 wlxmlDocument.on('change', function(event) {
20 var handler = handlers[event.type];
22 handler.bind(this)(event);
26 wlxmlDocument.on('contentSet', function() {
27 this.canvas.loadWlxmlDocument(wlxmlDocument);
33 var _metadataEventHandler = function(event) {
34 var canvasNode = utils.findCanvasElement(event.meta.node);
35 canvasNode.exec('updateMetadata');
39 nodeAttrChange: function(event) {
40 if(event.meta.attr === 'class') {
41 var canvasNode = utils.findCanvasElement(event.meta.node);
42 canvasNode.setWlxmlClass(event.meta.newVal);
45 nodeTagChange: function(event) {
46 var canvasNode = utils.findCanvasElement(event.meta.node);
47 canvasNode.setWlxmlTag(event.meta.newTagName);
48 canvasNode.wlxmlNode = event.meta.node;
50 nodeAdded: function(event, checkForExistence) {
51 if(event.meta.node.isRoot()) {
52 this.canvas.reloadRoot();
55 var parentElement = utils.findCanvasElement(event.meta.node.parent()),
56 nodeIndex = event.meta.node.getIndex(),
57 referenceElement, referenceAction, actionArg;
60 referenceElement = parentElement;
61 referenceAction = 'prepend';
63 referenceElement = parentElement.children()[nodeIndex-1];
64 referenceAction = 'after';
67 actionArg = (checkForExistence && utils.findCanvasElement(event.meta.node, event.meta.parent)) || event.meta.node;
68 referenceElement[referenceAction](actionArg);
70 nodeMoved: function(event) {
71 return handlers.nodeAdded.call(this, event, true);
73 nodeDetached: function(event) {
74 var canvasNode = utils.findCanvasElementInParent(event.meta.node, event.meta.parent);
77 nodeTextChange: function(event) {
78 //console.log('wlxmlListener: ' + event.meta.node.getText());
79 var canvasElement = utils.findCanvasElement(event.meta.node),
80 toSet = event.meta.node.getText();
82 toSet = utils.unicode.ZWS;
84 if(toSet !== canvasElement.getText()) {
85 canvasElement.setText(toSet);
89 metadataChanged: _metadataEventHandler,
90 metadataAdded: _metadataEventHandler,
91 metadataRemoved: _metadataEventHandler
95 create: function(canvas) {
96 return new Listener(canvas);