editor: refactoring canvas element state management
authorAleksander Łukasz <aleksander.lukasz@nowoczesnapolska.org.pl>
Thu, 8 May 2014 13:37:38 +0000 (15:37 +0200)
committerAleksander Łukasz <aleksander.lukasz@nowoczesnapolska.org.pl>
Wed, 28 May 2014 12:45:58 +0000 (14:45 +0200)
src/editor/modules/documentCanvas/canvas/canvas.js
src/editor/modules/documentCanvas/canvas/documentElement.js
src/editor/modules/documentCanvas/canvas/genericElement.js
src/editor/plugins/core/links/linkElement.js

index a057e05..34581be 100644 (file)
@@ -208,8 +208,12 @@ $.extend(Canvas.prototype, Backbone.Events, {
         observer.observe(this.wrapper[0], config);
 
 
-        this.wrapper.on('mouseover', '[document-node-element], [document-text-element]', function(e) {
-            var el = canvas.getDocumentElement(e.currentTarget);
+        var hoverHandler = function(e) {
+            var el = canvas.getDocumentElement(e.currentTarget),
+                expose = {
+                    mouseover: true,
+                    mouseout: false
+                };
             if(!el) {
                 return;
             }
@@ -217,19 +221,11 @@ $.extend(Canvas.prototype, Backbone.Events, {
             if(el instanceof documentElement.DocumentTextElement) {
                 el = el.parent();
             }
-            el.toggleLabel(true);
-        });
-        this.wrapper.on('mouseout', '[document-node-element], [document-text-element]', function(e) {
-            var el = canvas.getDocumentElement(e.currentTarget);
-            if(!el) {
-                return;
-            }
-            e.stopPropagation();
-            if(el instanceof documentElement.DocumentTextElement) {
-                el = el.parent();
-            }
-            el.toggleLabel(false);
-        });
+            el.updateState({exposed:expose[e.type]});
+        };
+
+        this.wrapper.on('mouseover', '[document-node-element], [document-text-element]', hoverHandler);
+        this.wrapper.on('mouseout', '[document-node-element], [document-text-element]', hoverHandler);
 
         this.eventBus.on('elementToggled', function(toggle, element) {
             if(!toggle) {
@@ -248,7 +244,7 @@ $.extend(Canvas.prototype, Backbone.Events, {
 
     toggleElementHighlight: function(node, toggle) {
         var element = utils.getElementForNode(node);
-        element.toggleHighlight(toggle);
+        element.updateState({exposed: toggle});
     },
 
     getCursor: function() {
@@ -257,10 +253,7 @@ $.extend(Canvas.prototype, Backbone.Events, {
 
     
     getCurrentNodeElement: function() {
-        var htmlElement = this.wrapper.find('.current-node-element').parent()[0];
-        if(htmlElement) {
-            return this.getDocumentElement(htmlElement);
-        }
+        return this.currentNodeElement;
     },
 
     getCurrentTextElement: function() {
@@ -293,15 +286,10 @@ $.extend(Canvas.prototype, Backbone.Events, {
         var s = this.getSelection(),
             f = s.toDocumentFragment();
         if(f && f instanceof f.RangeFragment) {
-                var $current = this.wrapper.find('.current-node-element');
-                var current = $current && this.getDocumentElement($current.parent()[0]);
-                
-                if($current) {
-                    $current.removeClass('current-node-element');
-                }
-                if(current) {
-                    current.markAsCurrent(false);
-                }
+            if(this.currentNodeElement) {
+                this.currentNodeElement.updateState({active: false});
+                this.currentNodeElement = null;
+            }
         }
     },
 
@@ -350,15 +338,11 @@ $.extend(Canvas.prototype, Backbone.Events, {
                 this.wrapper.find('.current-text-element').removeClass('current-text-element');
                 element.dom.addClass('current-text-element');
             } else {
-                var $current = this.wrapper.find('.current-node-element');
-                var current = this.getDocumentElement($current.parent()[0]);
-                $current.removeClass('current-node-element');
-
-                if(current) {
-                    current.markAsCurrent(false);
+                if(this.currentNodeElement) {
+                    this.currentNodeElement.updateState({active: false});
                 }
-                element._container().addClass('current-node-element');
-                element.markAsCurrent(true);
+                element.updateState({active: true});
+                this.currentNodeElement = element;
             }
         }.bind(this);
 
index 45592ef..956d0a7 100644 (file)
@@ -11,6 +11,10 @@ define([
 var DocumentElement = function(wlxmlNode, canvas) {
     this.wlxmlNode = wlxmlNode;
     this.canvas = canvas;
+    this.state = {
+        exposed: false,
+        active: false
+    };
 
     this.dom = this.createDOM();
     this.dom.data('canvas-element', this);
@@ -26,6 +30,21 @@ $.extend(DocumentElement.prototype, {
     refresh: function() {
         // noop
     },
+    updateState: function(toUpdate) {
+        var changes = {};
+        _.keys(toUpdate)
+            .filter(function(key) {
+                return this.state.hasOwnProperty(key);
+            }.bind(this))
+            .forEach(function(key) {
+                if(this.state !== toUpdate[key]) {
+                    this.state[key] = changes[key] = toUpdate[key];
+                }
+            }.bind(this));
+        if(_.isFunction(this.onStateChange)) {
+            this.onStateChange(changes);
+        }
+    },
     parent: function() {
         var parents = this.dom.parents('[document-node-element]');
         if(parents.length) {
@@ -125,19 +144,6 @@ $.extend(DocumentNodeElement.prototype, {
         return manipulate(this, params, 'after');
     },
 
-    toggleLabel: function(toggle) {
-        var displayCss = toggle ? 'inline-block' : 'none';
-        var label = this.dom.children('.canvas-widgets').find('.canvas-widget-label');
-        label.css('display', displayCss);
-        this.toggleHighlight(toggle);
-    },
-
-    markAsCurrent: function() {},
-
-    toggleHighlight: function(toggle) {
-        this._container().toggleClass('highlighted-element', toggle);
-    },
-
     isBlock: function() {
         return this.dom.css('display') === 'block';
     },
@@ -242,9 +248,6 @@ $.extend(DocumentTextElement.prototype, {
         return element;
     },
 
-    toggleHighlight: function() {
-        // do nothing for now
-    },
     children: function() {
         return [];
     }
index 44615f5..bbb4b07 100644 (file)
@@ -3,6 +3,7 @@ define(function(require) {
 'use strict';
 
 var $ = require('libs/jquery'),
+    _ = require('libs/underscore'),
     documentElement = require('./documentElement'),
     utils = require('./utils'),
     wlxmlUtils = require('utils/wlxml');
@@ -143,8 +144,20 @@ $.extend(generic, {
         });
     },
 
+    onStateChange: function(changes) {
+        if(_.isBoolean(changes.exposed) && !this.isSpan()) {
+            this._container().toggleClass('highlighted-element', changes.exposed);
+        }
+        if(_.isBoolean(changes.active) && !this.isSpan()) {
+            this._container().toggleClass('current-node-element', changes.active);
+        }
+    },
 
     ///
+
+    isSpan: function() {
+        return this.wlxmlNode.getTagName() === 'span';
+    },
     
     containsBlock: function() {
         return this.children()
index ceefee1..61d2abc 100644 (file)
@@ -26,8 +26,11 @@ _.extend(linkElement, {
         this.box.hide();
         this.addWidget(this.box);
     },
-    markAsCurrent: function(toggle) {
-        this.box.toggle(toggle);
+    onStateChange: function(changes) {
+        genericElement.onStateChange.call(this, changes);
+        if(_.isBoolean(changes.active)) {
+            this.box.toggle(changes.active);
+        }
     },
     onNodeAttrChange: function(event) {
         if(event.meta.attr === 'href') {