cbf8d0888973c96f228fa693b642d7b878127a53
[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('contentSet', function() {
27             this.canvas.loadWlxmlDocument(wlxmlDocument);
28         }, this);
29     }
30 });
31
32
33 var _metadataEventHandler = function(event) {
34     var canvasNode = utils.findCanvasElement(event.meta.node);
35     canvasNode.exec('updateMetadata');
36 };
37
38 var handlers = {
39     nodeAttrChange: function(event) {
40         if(event.meta.attr === 'class') {
41             var canvasNode = utils.findCanvasElement(event.meta.node);
42             canvasNode.setWlxmlClass(event.meta.newVal);
43         }
44     },
45     nodeTagChange: function(event) {
46         var canvasNode = utils.findCanvasElement(event.meta.node);
47         canvasNode.setWlxmlTag(event.meta.newTagName);
48         canvasNode.wlxmlNode = event.meta.node;
49     },
50     nodeAdded: function(event, checkForExistence) {
51         if(event.meta.node.isRoot()) {
52             this.canvas.reloadRoot();
53             return;
54         }
55         var parentElement = utils.findCanvasElement(event.meta.node.parent()),
56             nodeIndex = event.meta.node.getIndex(),
57             referenceElement, referenceAction, actionArg;
58
59         if(nodeIndex === 0) {
60             referenceElement = parentElement;
61             referenceAction = 'prepend';
62         } else {
63             referenceElement = parentElement.children()[nodeIndex-1];
64             referenceAction = 'after';
65         }
66
67         actionArg = (checkForExistence && utils.findCanvasElement(event.meta.node, event.meta.parent)) || event.meta.node;
68         referenceElement[referenceAction](actionArg);
69     },
70     nodeMoved: function(event) {
71         return handlers.nodeAdded.call(this, event, true);
72     },
73     nodeDetached: function(event) {
74         var canvasNode = utils.findCanvasElementInParent(event.meta.node, event.meta.parent);
75         canvasNode.detach();
76     },
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();
81         if(toSet === '') {
82             toSet = utils.unicode.ZWS;
83         }
84         if(toSet !== canvasElement.getText()) {
85             canvasElement.setText(toSet);
86         }
87     },
88
89     metadataChanged: _metadataEventHandler,
90     metadataAdded: _metadataEventHandler,
91     metadataRemoved: _metadataEventHandler
92 };
93
94 return {
95     create: function(canvas) {
96         return new Listener(canvas);
97     }
98 };
99
100 });