Fix
[fnpeditor.git] / modules / documentCanvas / canvas / documentElement.js
index f03719e..0cf2e2c 100644 (file)
@@ -2,8 +2,10 @@ define([
 'libs/jquery-1.9.1.min',
 'libs/underscore-min',
 'modules/documentCanvas/classAttributes',
-'modules/documentCanvas/canvas/utils'
-], function($, _, classAttributes, utils) {
+'modules/documentCanvas/canvas/utils',
+'modules/documentCanvas/canvas/widgets',
+'modules/documentCanvas/canvas/wlxmlManagers'
+], function($, _, classAttributes, utils, widgets, wlxmlManagers) {
     
 'use strict';
 
@@ -114,27 +116,29 @@ var DocumentNodeElement = function(htmlElement, canvas) {
 
 $.extend(DocumentNodeElement, {
     createDOM: function(params) {
-        var dom = $('<div document-node-element>'),
-            container = $('<div document-element-content>');
+        var dom = $('<div>')
+                .attr('document-node-element', ''),
+            widgetsContainer = $('<div>')
+                .addClass('canvas-widgets')
+                .attr('contenteditable', false),
+            container = $('<div>')
+                .attr('document-element-content', '');
         
-        container.attr('wlxml-tag', params.tag);
+        dom.append(widgetsContainer, container);
+        // Make sure widgets aren't navigable with arrow keys
+        widgetsContainer.find('*').add(widgetsContainer).attr('tabindex', -1);
+
+        var element = this.fromHTMLElement(dom[0]);
+
+        element.setWlxmlTag(params.tag);
         if(params.klass)
-            container.attr('wlxml-class', params.klass.replace(/\./g, '-'));
+            element.setWlxmlClass(params.klass);
         if(params.meta) {
             _.keys(params.meta).forEach(function(key) {
-                dom.attr('wlxml-meta-'+key, params.meta[key]);
+                element.setWlxmlMetaAttr(key, params.meta[key]);
             });
         }
-        dom.data('other-attrs', params.others);
-
-        var widgets = $('<div class="canvas-widgets" contenteditable="false">');
-        widgets.append($('<span class="canvas-widget canvas-widget-label">').text(params.tag + (params.klass ? ' / ' + params.klass : '')));
-        dom.append(widgets);
-
-        // Make sure widgets aren't navigable with arrow keys
-        widgets.find('*').add(widgets).attr('tabindex', -1);
-        
-        dom.append(container);
+        element.data('other-attrs', params.others);
 
         if(params.rawChildren) {
             container.append(params.rawChildren);
@@ -294,7 +298,11 @@ $.extend(DocumentNodeElement.prototype, {
         return this._container().attr('wlxml-tag');
     },
     setWlxmlTag: function(tag) {
+        if(tag === this.getWlxmlTag())
+            return;
+
         this._container().attr('wlxml-tag', tag);
+        this._updateWlxmlManager();
     },
     getWlxmlClass: function() {
         var klass = this._container().attr('wlxml-class');
@@ -303,6 +311,9 @@ $.extend(DocumentNodeElement.prototype, {
         return undefined;
     },
     setWlxmlClass: function(klass) {
+        if(klass === this.getWlxmlClass())
+            return;
+
         this.getWlxmlMetaAttrs().forEach(function(attr) {
             if(!classAttributes.hasMetaAttr(klass, attr.name))
                 this.dom().removeAttr('wlxml-meta-' + attr.name);
@@ -312,6 +323,12 @@ $.extend(DocumentNodeElement.prototype, {
             this._container().attr('wlxml-class', klass.replace(/\./g, '-'));
         else
             this._container().removeAttr('wlxml-class');
+        this._updateWlxmlManager();
+    },
+    _updateWlxmlManager: function() {
+        var manager = wlxmlManagers.getFor(this);
+        this.data('_wlxmlManager', manager);
+        manager.setup();
     },
     is: function(what) {
         if(what === 'list' && _.contains(['list.items', 'list.items.enum'], this.getWlxmlClass()))
@@ -340,7 +357,11 @@ $.extend(DocumentNodeElement.prototype, {
         var displayCss = toggle ? 'inline-block' : 'none';
         var label = this.dom().children('.canvas-widgets').find('.canvas-widget-label');
         label.css('display', displayCss);
-        this.dom().toggleClass('highlighted-element');
+        this.toggleHighlight(toggle);
+    },
+
+    toggleHighlight: function(toogle) {
+        this._container().toggleClass('highlighted-element');
     }
 });
 
@@ -510,6 +531,10 @@ $.extend(DocumentTextElement.prototype, {
         this.after(newElement);
         newElement.after(rhsTextElement);
         return newElement;
+    },
+
+    toggleHighlight: function() {
+        // do nothing for now
     }
 });