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 nodeAdded: function(event, checkForExistence) {
46 if(event.meta.node.isRoot()) {
47 this.canvas.reloadRoot();
50 var parentElement = utils.findCanvasElement(event.meta.node.parent()),
51 nodeIndex = event.meta.node.getIndex(),
52 referenceElement, referenceAction, actionArg;
55 referenceElement = parentElement;
56 referenceAction = 'prepend';
58 referenceElement = parentElement.children()[nodeIndex-1];
59 referenceAction = 'after';
62 actionArg = (checkForExistence && utils.findCanvasElement(event.meta.node, event.meta.parent)) || event.meta.node;
63 referenceElement[referenceAction](actionArg);
65 nodeMoved: function(event) {
66 return handlers.nodeAdded.call(this, event, true);
68 nodeDetached: function(event) {
69 var canvasNode = utils.findCanvasElementInParent(event.meta.node, event.meta.parent);
72 nodeTextChange: function(event) {
73 //console.log('wlxmlListener: ' + event.meta.node.getText());
74 var canvasElement = utils.findCanvasElement(event.meta.node),
75 toSet = event.meta.node.getText();
77 toSet = utils.unicode.ZWS;
79 if(toSet !== canvasElement.getText()) {
80 canvasElement.setText(toSet);
84 metadataChanged: _metadataEventHandler,
85 metadataAdded: _metadataEventHandler,
86 metadataRemoved: _metadataEventHandler
90 create: function(canvas) {
91 return new Listener(canvas);