faee6c86e613f9337a45f578ccb6ffa1ac6e30b5
[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 element = utils.getElementForNode(event.meta.node);
39     element.handle(event);
40 };
41
42
43 var handlers = {
44     nodeAttrChange: function(event) {
45         var element = utils.getElementForNode(event.meta.node),
46             objectChanged;
47         if(event.meta.attr === 'class') {
48             objectChanged = element.updateObject();
49         }
50
51         if(!objectChanged) {
52             element.handle(event);
53         }
54     },
55     nodeAdded: function(event) {
56         if(event.meta.node.isRoot()) {
57             this.canvas.reloadRoot();
58             return;
59         }
60
61         var containingNode = event.meta.node.parent(),
62             containingElement = utils.getElementForNode(containingNode);
63
64         containingElement.handle(event);
65     },
66     nodeMoved: function(event) {
67         return handlers.nodeAdded.call(this, event, true); //
68         //
69     },
70     nodeDetached: function(event) {
71         var element = utils.getElementForDetachedNode(event.meta.node, event.meta.parent);
72         element.handle(event);
73     },
74     nodeTextChange: function(event) {
75         var element = utils.getElementForNode(event.meta.node.parent());
76         element.handle(event);
77     },
78
79     metadataChanged: _metadataEventHandler,
80     metadataAdded: _metadataEventHandler,
81     metadataRemoved: _metadataEventHandler
82 };
83
84 return {
85     create: function(canvas) {
86         return new Listener(canvas);
87     }
88 };
89
90 });