linting
[fnpeditor.git] / src / editor / modules / documentCanvas / canvas / canvas.js
index 482afb9..48f03de 100644 (file)
@@ -4,44 +4,40 @@ 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';
 
     
 'use strict';
 
-var Canvas = function(wlxml, publisher) {
+var Canvas = function(wlxmlDocument, publisher) {
     this.eventBus = _.extend({}, Backbone.Events);
     this.eventBus = _.extend({}, Backbone.Events);
-    this.loadWlxmlDocument(wlxml);
+    this.wrapper = $('<div>').addClass('canvas-wrapper').attr('contenteditable', true);
+    this.wlxmlListener = wlxmlListener.create(this);
+    this.loadWlxmlDocument(wlxmlDocument);
     this.publisher = publisher ? publisher : function() {};
 };
 
 $.extend(Canvas.prototype, {
 
     loadWlxmlDocument: function(wlxmlDocument) {
     this.publisher = publisher ? publisher : function() {};
 };
 
 $.extend(Canvas.prototype, {
 
     loadWlxmlDocument: function(wlxmlDocument) {
+        if(!wlxmlDocument) {
+            return false;
+        }
+
         var canvasDOM = this.generateCanvasDOM(wlxmlDocument.root);
 
         var canvasDOM = this.generateCanvasDOM(wlxmlDocument.root);
 
-        this.wrapper = $('<div>').addClass('canvas-wrapper').attr('contenteditable', true);
+        this.wrapper.empty();
         this.wrapper.append(canvasDOM);
         this.d = this.wrapper.children(0);
         this.setupEventHandling();
         this.wrapper.append(canvasDOM);
         this.d = this.wrapper.children(0);
         this.setupEventHandling();
+
+        this.wlxmlListener.listenTo(wlxmlDocument);
+        this.wlxmlDocument = wlxmlDocument;
     },
 
     generateCanvasDOM: function(wlxmlNode) {
     },
 
     generateCanvasDOM: function(wlxmlNode) {
-        // var element = this.createNodeElement2({
-        //     tag: wlxmlNode.getTagName(),
-        //     klass: wlxmlNode.getClass(), //currentTag.attr('class'),
-        //     meta: wlxmlNode.getMetaAttributes(), //meta,
-        //     others: wlxmlNode.getOtherAttributes(), // ~ //others,
-        //     rawChildren: wlxmlNode.contents(),
-        //     prepopulateOnEmpty: true
-        // }); //->create2
-
         var element = documentElement.DocumentNodeElement.create(wlxmlNode, this);
         var element = documentElement.DocumentNodeElement.create(wlxmlNode, this);
-
-
-        ['orig-before', 'orig-after', 'orig-begin', 'orig-end'].forEach(function(attr) {
-            element.data(attr, '');
-        });
         return element.dom();
     },
 
         return element.dom();
     },
 
@@ -68,6 +64,11 @@ $.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),
+                        toSet = mutation.target.data !== utils.unicode.ZWS ? mutation.target.data : '';
+
+                    textElement.data('wlxmlNode').setText(toSet);
                 }
             });
         });
                 }
             });
         });
@@ -111,6 +112,11 @@ $.extend(Canvas.prototype, {
         return documentElement.DocumentNodeElement.fromHTMLElement(this.d.get(0), this); //{wlxmlTag: this.d.prop('tagName')};
     },
 
         return documentElement.DocumentNodeElement.fromHTMLElement(this.d.get(0), this); //{wlxmlTag: this.d.prop('tagName')};
     },
 
+    toggleElementHighlight: function(node, toggle) {
+        var element = utils.findCanvasElement(node);
+        element.toggleHighlight(toggle);
+    },
+
     createNodeElement: function(params) {
         return documentElement.DocumentNodeElement.create(params, this);
     },
     createNodeElement: function(params) {
         return documentElement.DocumentNodeElement.create(params, this);
     },
@@ -231,6 +237,10 @@ $.extend(Canvas.prototype, {
 
 
     setCurrentElement: function(element, params) {
 
 
     setCurrentElement: function(element, params) {
+        if(!(element instanceof documentElement.DocumentElement)) {
+            element = utils.findCanvasElement(element);
+        }
+
         params = _.extend({caretTo: 'end'}, params);
         var findFirstDirectTextChild = function(e, nodeToLand) {
             var byBrowser = this.getCursor().getPosition().element;
         params = _.extend({caretTo: 'end'}, params);
         var findFirstDirectTextChild = function(e, nodeToLand) {
             var byBrowser = this.getCursor().getPosition().element;
@@ -269,7 +279,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();
         }
@@ -277,7 +287,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'));
         }
     },
 
         }
     },
 
@@ -550,8 +560,8 @@ $.extend(Cursor.prototype, {
 })
 
 return {
 })
 
 return {
-    fromXML: function(wlxmlNode, publisher) {
-        return new Canvas(wlxmlNode, publisher);
+    fromXMLDocument: function(wlxmlDocument, publisher) {
+        return new Canvas(wlxmlDocument, publisher);
     }
 };
 
     }
 };