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;
}
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) {
toggleElementHighlight: function(node, toggle) {
var element = utils.getElementForNode(node);
- element.toggleHighlight(toggle);
+ element.updateState({exposed: toggle});
},
getCursor: function() {
getCurrentNodeElement: function() {
- var htmlElement = this.wrapper.find('.current-node-element').parent()[0];
- if(htmlElement) {
- return this.getDocumentElement(htmlElement);
- }
+ return this.currentNodeElement;
},
getCurrentTextElement: function() {
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;
+ }
}
},
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);
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);
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) {
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';
},
return element;
},
- toggleHighlight: function() {
- // do nothing for now
- },
children: function() {
return [];
}
'use strict';
var $ = require('libs/jquery'),
+ _ = require('libs/underscore'),
documentElement = require('./documentElement'),
utils = require('./utils'),
wlxmlUtils = require('utils/wlxml');
});
},
+ 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()