X-Git-Url: https://git.mdrn.pl/fnpeditor.git/blobdiff_plain/22e9258db6c11e6e0b042e142836cdb21399cc7b..c031ffaa79a223a05a4a58de2854c9855e94cc18:/src/editor/modules/documentCanvas/canvas/documentElement.js diff --git a/src/editor/modules/documentCanvas/canvas/documentElement.js b/src/editor/modules/documentCanvas/canvas/documentElement.js index afa510f..2265f92 100644 --- a/src/editor/modules/documentCanvas/canvas/documentElement.js +++ b/src/editor/modules/documentCanvas/canvas/documentElement.js @@ -11,9 +11,6 @@ define([ // DocumentElement represents a text or an element node from WLXML document rendered inside Canvas var DocumentElement = function(wlxmlNode, canvas) { - if(arguments.length === 0) { - return; - } this.wlxmlNode = wlxmlNode; this.canvas = canvas; @@ -22,6 +19,15 @@ var DocumentElement = function(wlxmlNode, canvas) { }; $.extend(DocumentElement.prototype, { + refreshPath: function() { + this.parents().forEach(function(parent) { + parent.refresh(); + }); + this.refresh(); + }, + refresh: function() { + // noop + }, bound: function() { return $.contains(document.documentElement, this.dom()[0]); }, @@ -50,16 +56,9 @@ $.extend(DocumentElement.prototype, { return other && (typeof other === typeof this) && other.dom()[0] === this.dom()[0]; }, - markAsCurrent: function() { - this.canvas.markAsCurrent(this); - }, - getVerticallyFirstTextElement: function() { var toret; this.children().some(function(child) { - if(!child.isVisible()) { - return false; // continue - } if(child instanceof DocumentTextElement) { toret = child; return true; // break @@ -87,16 +86,6 @@ $.extend(DocumentElement.prototype, { return this.canvas.getDocumentElement(utils.nearestInDocumentOrder(selector, direction, this.dom()[0])); }, - isVisible: function() { - return this instanceof DocumentTextElement || this.getWlxmlTag() !== 'metadata'; - }, - - isInsideList: function() { - return this.parents().some(function(parent) { - return parent.is('list'); - }); - }, - exec: function(method) { if(this.manager && this.manager[method]) { return this.manager[method].apply(this.manager, Array.prototype.slice.call(arguments, 1)); @@ -121,13 +110,17 @@ var manipulate = function(e, params, action) { } var target = (action === 'append' || action === 'prepend') ? e._container() : e.dom(); target[action](element.dom()); + e.refreshPath(); return element; }; -DocumentNodeElement.prototype = new DocumentElement(); +DocumentNodeElement.prototype = Object.create(DocumentElement.prototype); $.extend(DocumentNodeElement.prototype, { + init: function() { + // noo[] + }, createDOM: function() { var dom = $('
') .attr('document-node-element', ''), @@ -141,20 +134,29 @@ $.extend(DocumentNodeElement.prototype, { // Make sure widgets aren't navigable with arrow keys widgetsContainer.find('*').add(widgetsContainer).attr('tabindex', -1); this.$element = dom; //@!!! - this.setWlxml({tag: this.wlxmlNode.getTagName(), klass: this.wlxmlNode.getClass()}); + + this.setWlxmlTag(this.wlxmlNode.getTagName()); + this.setWlxmlClass(this.wlxmlNode.getClass()); this.wlxmlNode.contents().forEach(function(node) { container.append(this.canvas.createElement(node).dom()); }.bind(this)); + + this.init(); + return dom; }, _container: function() { return this.dom().children('[document-element-content]'); }, detach: function() { + var parents = this.parents(); this.dom().detach(); this.canvas = null; - return this; + if(parents[0]) { + parents[0].refreshPath(); + } + return this; }, append: function(params) { return manipulate(this, params, 'append'); @@ -202,14 +204,7 @@ $.extend(DocumentNodeElement.prototype, { return this._container().attr('wlxml-tag'); }, setWlxmlTag: function(tag) { - if(tag === this.getWlxmlTag()) { - return; - } - this._container().attr('wlxml-tag', tag); - if(!this.__updatingWlxml) { - this._updateWlxmlManager(); - } }, getWlxmlClass: function() { var klass = this._container().attr('wlxml-class'); @@ -228,30 +223,10 @@ $.extend(DocumentNodeElement.prototype, { else { this._container().removeAttr('wlxml-class'); } - if(!this.__updatingWlxml) { - this._updateWlxmlManager(); - } - }, - setWlxml: function(params) { - this.__updatingWlxml = true; - if(params.tag !== undefined) { - this.setWlxmlTag(params.tag); - } - if(params.klass !== undefined) { - this.setWlxmlClass(params.klass); - } - this._updateWlxmlManager(); - this.__updatingWlxml = false; - }, - _updateWlxmlManager: function() { this.manager = wlxmlManagers.getFor(this); this.manager.setup(); - }, - is: function(what) { - if(what === 'list' && _.contains(['list.items', 'list.items.enum'], this.getWlxmlClass())) { - return true; - } - return false; + + this.refreshPath(); }, toggleLabel: function(toggle) { var displayCss = toggle ? 'inline-block' : 'none'; @@ -268,6 +243,33 @@ $.extend(DocumentNodeElement.prototype, { if(this.manager) { this.manager.toggle(toggle); } + }, + + isBlock: function() { + return this.dom().css('display') === 'block'; + }, + + containsBlock: function() { + return this.children() + .filter(function(child) { + return child instanceof DocumentNodeElement; + }) + .some(function(child) { + if(child.isBlock()) { + return true; + } else { + return child.containsBlock(); + } + }); + }, + + displayAsBlock: function() { + this.dom().css('display', 'block'); + this._container().css('display', 'block'); + }, + displayInline: function() { + this.dom().css('display', 'inline'); + this._container().css('display', 'inline'); } }); @@ -283,7 +285,7 @@ $.extend(DocumentTextElement, { } }); -DocumentTextElement.prototype = new DocumentElement(); +DocumentTextElement.prototype = Object.create(DocumentElement.prototype); $.extend(DocumentTextElement.prototype, { createDOM: function() { @@ -324,6 +326,7 @@ $.extend(DocumentTextElement.prototype, { this.dom().wrap('
'); this.dom().parent().after(element.dom()); this.dom().unwrap(); + this.refreshPath(); return element; }, before: function(params) { @@ -339,18 +342,44 @@ $.extend(DocumentTextElement.prototype, { this.dom().wrap('
'); this.dom().parent().before(element.dom()); this.dom().unwrap(); + this.refreshPath(); return element; }, toggleHighlight: function() { // do nothing for now } + +}); + +var SpanElement = function() { + DocumentNodeElement.apply(this, Array.prototype.slice.call(arguments, 0)); +}; +SpanElement.prototype = $.extend(Object.create(DocumentNodeElement.prototype), { + init: function() { + if(this.containsBlock()) { + this.displayAsBlock(); + } else { + this.displayInline(); + } + }, + refresh: function() { + this.init(); + } }); +var elements = { + span: SpanElement +}; + + return { DocumentElement: DocumentElement, DocumentNodeElement: DocumentNodeElement, - DocumentTextElement: DocumentTextElement + DocumentTextElement: DocumentTextElement, //, + factoryForTag: function(tagName) { + return elements[tagName] || DocumentNodeElement; + } }; }); \ No newline at end of file