integration wip: handling nodeTextChange event
[fnpeditor.git] / src / editor / modules / documentCanvas / canvas / canvas.js
index a589a20..3a662e9 100644 (file)
@@ -4,14 +4,16 @@ define([
 'libs/backbone',
 'modules/documentCanvas/canvas/documentElement',
 'modules/documentCanvas/canvas/keyboard',
 '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';
 
 var Canvas = function(wlxmlDocument, publisher) {
     this.eventBus = _.extend({}, Backbone.Events);
     this.wrapper = $('<div>').addClass('canvas-wrapper').attr('contenteditable', true);
     
 'use strict';
 
 var Canvas = function(wlxmlDocument, publisher) {
     this.eventBus = _.extend({}, Backbone.Events);
     this.wrapper = $('<div>').addClass('canvas-wrapper').attr('contenteditable', true);
+    this.wlxmlListener = wlxmlListener.create(this);
     this.loadWlxmlDocument(wlxmlDocument);
     this.publisher = publisher ? publisher : function() {};
 };
     this.loadWlxmlDocument(wlxmlDocument);
     this.publisher = publisher ? publisher : function() {};
 };
@@ -19,10 +21,10 @@ var Canvas = function(wlxmlDocument, publisher) {
 $.extend(Canvas.prototype, {
 
     loadWlxmlDocument: function(wlxmlDocument) {
 $.extend(Canvas.prototype, {
 
     loadWlxmlDocument: function(wlxmlDocument) {
-        wlxmlDocument = wlxmlDocument || this.wlxmlDocument;
         if(!wlxmlDocument) {
             return false;
         }
         if(!wlxmlDocument) {
             return false;
         }
+
         var canvasDOM = this.generateCanvasDOM(wlxmlDocument.root);
 
         this.wrapper.empty();
         var canvasDOM = this.generateCanvasDOM(wlxmlDocument.root);
 
         this.wrapper.empty();
@@ -30,34 +32,7 @@ $.extend(Canvas.prototype, {
         this.d = this.wrapper.children(0);
         this.setupEventHandling();
 
         this.d = this.wrapper.children(0);
         this.setupEventHandling();
 
-        var canvas = this;
-
-        var findCanvasElement = function(node) {
-            if(node.nodeType === Node.ELEMENT_NODE) {
-                return node.getData('canvasElement');
-            }
-            if(node.nodeType === Node.TEXT_NODE) {
-                var parent = node.parent(),
-                    toret;
-
-                parent.children().forEach(function(child) {
-                    if(child.data('wlxmlNode').sameNode(node))
-                        toret = child;
-                });
-                if(toret)
-                    return toret;
-            }
-        }
-
-        if(this.wlxmlDocument !== wlxmlDocument) {
-            wlxmlDocument.on('change', function(event) {
-                var canvasNode = findCanvasElement(event.meta.node);
-                if(event.type === 'nodeAttrChange' && event.meta.attr === 'class') {
-                    canvasNode.setWlxmlClass(event.meta.newVal);
-                }
-
-            });
-        }
+        this.wlxmlListener.listenTo(wlxmlDocument);
         this.wlxmlDocument = wlxmlDocument;
     },
 
         this.wlxmlDocument = wlxmlDocument;
     },
 
@@ -89,6 +64,9 @@ $.extend(Canvas.prototype, {
                     }
                     observer.observe(canvas.d[0], config);
                     canvas.publisher('contentChanged');
                     }
                     observer.observe(canvas.d[0], config);
                     canvas.publisher('contentChanged');
+
+                    var textElement = canvas.getDocumentElement(mutation.target);
+                    textElement.data('wlxmlNode').setText(mutation.target.data);
                 }
             });
         });
                 }
             });
         });
@@ -290,7 +268,7 @@ $.extend(Canvas.prototype, {
             if(params.caretTo || !textElementToLand.sameNode(this.getCursor().getPosition().element))
                 this._moveCaretToTextElement(textElementToLand, params.caretTo); // as method on element?
             if(!(textElementToLand.sameNode(currentTextElement)))
             if(params.caretTo || !textElementToLand.sameNode(this.getCursor().getPosition().element))
                 this._moveCaretToTextElement(textElementToLand, params.caretTo); // as method on element?
             if(!(textElementToLand.sameNode(currentTextElement)))
-                this.publisher('currentTextElementSet', textElementToLand);
+                this.publisher('currentTextElementSet', textElementToLand.data('wlxmlNode'));
         } else {
             document.getSelection().removeAllRanges();
         }
         } else {
             document.getSelection().removeAllRanges();
         }
@@ -298,7 +276,7 @@ $.extend(Canvas.prototype, {
         if(!(currentNodeElement && currentNodeElement.sameNode(nodeElementToLand))) {
             _markAsCurrent(nodeElementToLand);
 
         if(!(currentNodeElement && currentNodeElement.sameNode(nodeElementToLand))) {
             _markAsCurrent(nodeElementToLand);
 
-            this.publisher('currentNodeElementSet', nodeElementToLand);
+            this.publisher('currentNodeElementSet', nodeElementToLand.data('wlxmlNode'));
         }
     },
 
         }
     },