f932ea0dfcd5f10fd5e3aa9c72072343916ce8b9
[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     nodeAdded: function(event, checkForExistence) {
46         if(event.meta.node.isRoot()) {
47             this.canvas.reloadRoot();
48             return;
49         }
50         var parentElement = utils.findCanvasElement(event.meta.node.parent()),
51             nodeIndex = event.meta.node.getIndex(),
52             referenceElement, referenceAction, actionArg;
53
54         if(nodeIndex === 0) {
55             referenceElement = parentElement;
56             referenceAction = 'prepend';
57         } else {
58             referenceElement = parentElement.children()[nodeIndex-1];
59             referenceAction = 'after';
60         }
61
62         actionArg = (checkForExistence && utils.findCanvasElement(event.meta.node, event.meta.parent)) || event.meta.node;
63         referenceElement[referenceAction](actionArg);
64     },
65     nodeMoved: function(event) {
66         return handlers.nodeAdded.call(this, event, true);
67     },
68     nodeDetached: function(event) {
69         var canvasNode = utils.findCanvasElementInParent(event.meta.node, event.meta.parent);
70         canvasNode.detach();
71     },
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();
76         if(toSet === '') {
77             toSet = utils.unicode.ZWS;
78         }
79         if(toSet !== canvasElement.getText()) {
80             canvasElement.setText(toSet);
81         }
82     },
83
84     metadataChanged: _metadataEventHandler,
85     metadataAdded: _metadataEventHandler,
86     metadataRemoved: _metadataEventHandler
87 };
88
89 return {
90     create: function(canvas) {
91         return new Listener(canvas);
92     }
93 };
94
95 });