Refactoring: handle wlxmlDocument events in a separate module
authorAleksander Łukasz <aleksander.lukasz@nowoczesnapolska.org.pl>
Thu, 10 Oct 2013 14:58:10 +0000 (16:58 +0200)
committerAleksander Łukasz <aleksander.lukasz@nowoczesnapolska.org.pl>
Mon, 2 Dec 2013 13:50:52 +0000 (14:50 +0100)
src/editor/modules/documentCanvas/canvas/canvas.js
src/editor/modules/documentCanvas/canvas/wlxmlListener.js [new file with mode: 0644]

index 8b94782..97ad592 100644 (file)
@@ -4,8 +4,9 @@ define([
 'libs/backbone',
 'modules/documentCanvas/canvas/documentElement',
 'modules/documentCanvas/canvas/keyboard',
-'modules/documentCanvas/canvas/utils'
-], function($, _, Backbone, documentElement, keyboard, utils) {
+'modules/documentCanvas/canvas/utils',
+'modules/documentCanvas/canvas/wlxmlListener'
+], function($, _, Backbone, documentElement, keyboard, utils, wlxmlListener) {
     
 'use strict';
 
@@ -14,15 +15,16 @@ var Canvas = function(wlxmlDocument, publisher) {
     this.wrapper = $('<div>').addClass('canvas-wrapper').attr('contenteditable', true);
     this.loadWlxmlDocument(wlxmlDocument);
     this.publisher = publisher ? publisher : function() {};
+    this.wlxmlListener = wlxmlListener.create(this);
 };
 
 $.extend(Canvas.prototype, {
 
     loadWlxmlDocument: function(wlxmlDocument) {
-        wlxmlDocument = wlxmlDocument || this.wlxmlDocument;
         if(!wlxmlDocument) {
             return false;
         }
+
         var canvasDOM = this.generateCanvasDOM(wlxmlDocument.root);
 
         this.wrapper.empty();
@@ -30,18 +32,7 @@ $.extend(Canvas.prototype, {
         this.d = this.wrapper.children(0);
         this.setupEventHandling();
 
-        var canvas = this;
-
-        if(this.wlxmlDocument !== wlxmlDocument) {
-            wlxmlDocument.on('change', function(event) {
-                var canvasNode = utils.findCanvasElement(event.meta.node);
-                if(event.type === 'nodeAttrChange' && event.meta.attr === 'class') {
-                    canvasNode.setWlxmlClass(event.meta.newVal);
-                }
-
-            });
-        }
-        this.wlxmlDocument = wlxmlDocument;
+        this.wlxmlListener.listenTo(wlxmlDocument);
     },
 
     generateCanvasDOM: function(wlxmlNode) {
diff --git a/src/editor/modules/documentCanvas/canvas/wlxmlListener.js b/src/editor/modules/documentCanvas/canvas/wlxmlListener.js
new file mode 100644 (file)
index 0000000..fb6a2de
--- /dev/null
@@ -0,0 +1,42 @@
+define([
+'libs/jquery',
+'modules/documentCanvas/canvas/utils',
+], function($, utils) {
+    
+'use strict';
+
+var Listener = function(canvas) {
+    this.canvas = canvas;
+};
+
+$.extend(Listener.prototype, {
+    listenTo: function(wlxmlDocument) {
+        if(wlxmlDocument === this.wlxmlDocument) {
+            return;
+        }
+
+        wlxmlDocument.on('change', function(event) {
+            var handler = handlers[event.type];
+            if(handler) {
+                handler.bind(this)(event);
+            }
+        }, this);
+    }
+});
+
+var handlers = {
+    nodeAttrChange: function(event) {
+        if(event.meta.attr === 'class') {
+            var canvasNode = utils.findCanvasElement(event.meta.node);
+            canvasNode.setWlxmlClass(event.meta.newVal);
+        }
+    }
+};
+
+return {
+    create: function(canvas) {
+        return new Listener(canvas);
+    }
+};
+
+});
\ No newline at end of file