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('operationEnd', function() {
27 this.canvas.triggerSelectionChanged();
30 wlxmlDocument.on('contentSet', function() {
31 this.canvas.loadWlxmlDocument(wlxmlDocument);
37 var _metadataEventHandler = function(event) {
38 var canvasNode = utils.findCanvasElement(event.meta.node);
39 canvasNode.exec('updateMetadata');
43 nodeAttrChange: function(event) {
44 if(event.meta.attr === 'class') {
45 var canvasNode = utils.findCanvasElement(event.meta.node);
46 canvasNode.setWlxmlClass(event.meta.newVal);
49 nodeAdded: function(event, checkForExistence) {
50 if(event.meta.node.isRoot()) {
51 this.canvas.reloadRoot();
54 var parentElement = utils.findCanvasElement(event.meta.node.parent()),
55 nodeIndex = event.meta.node.getIndex(),
56 referenceElement, referenceAction, actionArg;
59 referenceElement = parentElement;
60 referenceAction = 'prepend';
62 referenceElement = parentElement.children()[nodeIndex-1];
63 referenceAction = 'after';
66 actionArg = (checkForExistence && utils.findCanvasElement(event.meta.node, event.meta.parent)) || event.meta.node;
67 referenceElement[referenceAction](actionArg);
69 nodeMoved: function(event) {
70 return handlers.nodeAdded.call(this, event, true);
72 nodeDetached: function(event) {
73 var canvasNode = utils.findCanvasElementInParent(event.meta.node, event.meta.parent);
76 nodeTextChange: function(event) {
77 //console.log('wlxmlListener: ' + event.meta.node.getText());
78 var canvasElement = utils.findCanvasElement(event.meta.node),
79 toSet = event.meta.node.getText();
81 toSet = utils.unicode.ZWS;
83 if(toSet !== canvasElement.getText()) {
84 canvasElement.setText(toSet);
88 metadataChanged: _metadataEventHandler,
89 metadataAdded: _metadataEventHandler,
90 metadataRemoved: _metadataEventHandler
94 create: function(canvas) {
95 return new Listener(canvas);