7d19e94e820ebdff271c1c5605c841805eddb6bf
[fnpeditor.git] / src / editor / modules / documentCanvas / canvas / wlxmlListener.js
1 define([
2 'libs/jquery',
3 'modules/documentCanvas/canvas/utils',
4 ], function($, utils) {
5     
6 'use strict';
7
8 var Listener = function(canvas) {
9     this.canvas = canvas;
10 };
11
12 $.extend(Listener.prototype, {
13     listenTo: function(wlxmlDocument) {
14         if(wlxmlDocument === this.wlxmlDocument) {
15             return;
16         }
17         this.wlxmlDocument = wlxmlDocument;
18
19         wlxmlDocument.on('change', function(event) {
20             var handler = handlers[event.type];
21             if(handler) {
22                 handler.bind(this)(event);
23             }
24         }, this);
25
26         wlxmlDocument.on('operationEnd', function() {
27             this.canvas.triggerSelectionChanged();
28         }, this);
29
30         wlxmlDocument.on('contentSet', function() {
31             this.canvas.loadWlxmlDocument(wlxmlDocument);
32         }, this);
33     }
34 });
35
36
37 var _metadataEventHandler = function(event) {
38     var canvasNode = utils.findCanvasElement(event.meta.node);
39     canvasNode.exec('updateMetadata');
40 };
41
42 var handlers = {
43     nodeAttrChange: function(event) {
44         if(event.meta.attr === 'class') {
45             var canvasNode = utils.findCanvasElement(event.meta.node);
46             canvasNode.setWlxmlClass(event.meta.newVal);
47         }
48     },
49     nodeAdded: function(event, checkForExistence) {
50         if(event.meta.node.isRoot()) {
51             this.canvas.reloadRoot();
52             return;
53         }
54         var parentElement = utils.findCanvasElement(event.meta.node.parent()),
55             nodeIndex = event.meta.node.getIndex(),
56             referenceElement, referenceAction, actionArg;
57
58         if(nodeIndex === 0) {
59             referenceElement = parentElement;
60             referenceAction = 'prepend';
61         } else {
62             referenceElement = parentElement.children()[nodeIndex-1];
63             referenceAction = 'after';
64         }
65
66         actionArg = (checkForExistence && utils.findCanvasElement(event.meta.node, event.meta.parent)) || event.meta.node;
67         referenceElement[referenceAction](actionArg);
68     },
69     nodeMoved: function(event) {
70         return handlers.nodeAdded.call(this, event, true);
71     },
72     nodeDetached: function(event) {
73         var canvasNode = utils.findCanvasElementInParent(event.meta.node, event.meta.parent);
74         canvasNode.detach();
75     },
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();
80         if(toSet === '') {
81             toSet = utils.unicode.ZWS;
82         }
83         if(toSet !== canvasElement.getText()) {
84             canvasElement.setText(toSet);
85         }
86     },
87
88     metadataChanged: _metadataEventHandler,
89     metadataAdded: _metadataEventHandler,
90     metadataRemoved: _metadataEventHandler
91 };
92
93 return {
94     create: function(canvas) {
95         return new Listener(canvas);
96     }
97 };
98
99 });