cbb582dc3468466740f58d7b2969badaa33f165f
[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
18         wlxmlDocument.on('change', function(event) {
19             var handler = handlers[event.type];
20             if(handler) {
21                 handler.bind(this)(event);
22             }
23         }, this);
24     }
25 });
26
27 var handlers = {
28     nodeAttrChange: function(event) {
29         if(event.meta.attr === 'class') {
30             var canvasNode = utils.findCanvasElement(event.meta.node);
31             canvasNode.setWlxmlClass(event.meta.newVal);
32         }
33     },
34     nodeTagChange: function(event) {
35         var canvasNode = utils.findCanvasElement(event.meta.node);
36         canvasNode.setWlxmlTag(event.meta.newTagName);
37     }
38 };
39
40 return {
41     create: function(canvas) {
42         return new Listener(canvas);
43     }
44 };
45
46 });