ae1cee1f3679cd911426bfb8fb479a058dfbb8de
[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 var handlers = {
33     nodeAttrChange: function(event) {
34         if(event.meta.attr === 'class') {
35             var canvasNode = utils.findCanvasElement(event.meta.node);
36             canvasNode.setWlxmlClass(event.meta.newVal);
37         }
38     },
39     nodeTagChange: function(event) {
40         var canvasNode = utils.findCanvasElement(event.meta.node);
41         canvasNode.setWlxmlTag(event.meta.newTagName);
42         canvasNode.data('wlxmlNode', event.meta.node);
43     },
44     nodeAdded: function(event, checkForExistence) {
45         if(event.meta.node.isRoot()) {
46             this.canvas.reloadRoot();
47             return;
48         }
49         var parentElement = utils.findCanvasElement(event.meta.node.parent()),
50             nodeIndex = event.meta.node.getIndex(),
51             referenceElement, referenceAction, actionArg;
52
53         if(nodeIndex === 0) {
54             referenceElement = parentElement;
55             referenceAction = 'prepend';
56         } else {
57             referenceElement = parentElement.children()[nodeIndex-1];
58             referenceAction = 'after';
59         }
60
61         actionArg = (checkForExistence && utils.findCanvasElement(event.meta.node)) || event.meta.node;
62         referenceElement[referenceAction](actionArg);
63     },
64     nodeMoved: function(event) {
65         return handlers.nodeAdded(event, true);
66     },
67     nodeDetached: function(event) {
68         var canvasNode = utils.findCanvasElementInParent(event.meta.node, event.meta.parent);
69         canvasNode.detach();
70     },
71     nodeTextChange: function(event) {
72         //console.log('wlxmlListener: ' + event.meta.node.getText());
73         var canvasElement = utils.findCanvasElement(event.meta.node),
74             toSet = event.meta.node.getText();
75         if(toSet === '') {
76             toSet = utils.unicode.ZWS;
77         }
78         if(toSet !== canvasElement.getText()) {
79             canvasElement.setText(toSet);
80         }
81     }
82 };
83
84 return {
85     create: function(canvas) {
86         return new Listener(canvas);
87     }
88 };
89
90 });