From b65f1bbd3863db4164c3b7283e187460ccdf2504 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Aleksander=20=C5=81ukasz?= Date: Thu, 1 Aug 2013 15:25:29 +0200 Subject: [PATCH 1/1] Refactoring - wlxmlManagers --- .../documentCanvas/canvas/documentElement.js | 33 +++++----- .../documentCanvas/canvas/wlxmlManagers.js | 62 +++++++++++++++++++ 2 files changed, 78 insertions(+), 17 deletions(-) create mode 100644 modules/documentCanvas/canvas/wlxmlManagers.js diff --git a/modules/documentCanvas/canvas/documentElement.js b/modules/documentCanvas/canvas/documentElement.js index 2b42a22..0cf2e2c 100644 --- a/modules/documentCanvas/canvas/documentElement.js +++ b/modules/documentCanvas/canvas/documentElement.js @@ -3,8 +3,9 @@ define([ 'libs/underscore-min', 'modules/documentCanvas/classAttributes', 'modules/documentCanvas/canvas/utils', -'modules/documentCanvas/canvas/widgets' -], function($, _, classAttributes, utils, widgets) { +'modules/documentCanvas/canvas/widgets', +'modules/documentCanvas/canvas/wlxmlManagers' +], function($, _, classAttributes, utils, widgets, wlxmlManagers) { 'use strict'; @@ -113,14 +114,6 @@ var DocumentNodeElement = function(htmlElement, canvas) { DocumentElement.call(this, htmlElement, canvas); }; -var getDisplayStyle = function(tag, klass) { - if(tag === 'metadata') - return 'none'; - if(tag === 'span') - return 'inline'; - return 'block'; -} - $.extend(DocumentNodeElement, { createDOM: function(params) { var dom = $('
') @@ -146,7 +139,7 @@ $.extend(DocumentNodeElement, { }); } element.data('other-attrs', params.others); - widgetsContainer.append(widgets.labelWidget(params.tag, params.klass)); + if(params.rawChildren) { container.append(params.rawChildren); } @@ -305,8 +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._updateDisplayStyle(); + this._updateWlxmlManager(); }, getWlxmlClass: function() { var klass = this._container().attr('wlxml-class'); @@ -315,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); @@ -324,12 +323,12 @@ $.extend(DocumentNodeElement.prototype, { this._container().attr('wlxml-class', klass.replace(/\./g, '-')); else this._container().removeAttr('wlxml-class'); - this._updateDisplayStyle(); + this._updateWlxmlManager(); }, - _updateDisplayStyle: function() { - var displayStyle = getDisplayStyle(this.getWlxmlTag(), this.getWlxmlClass()); - this.dom().css('display', displayStyle); - this._container().css('display', displayStyle); + _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())) diff --git a/modules/documentCanvas/canvas/wlxmlManagers.js b/modules/documentCanvas/canvas/wlxmlManagers.js new file mode 100644 index 0000000..2a9bf15 --- /dev/null +++ b/modules/documentCanvas/canvas/wlxmlManagers.js @@ -0,0 +1,62 @@ +define([ +'libs/jquery-1.9.1.min', +'modules/documentCanvas/canvas/widgets' +], function($, widgets) { + +'use strict'; + + +var DocumentElementAPI = function(documentElement) { + + this.addWidget = function(widget) { + documentElement.dom().find('.canvas-widgets').append(widget); + }; + + this.clearWidgets = function() { + documentElement.dom().find('.canvas-widgets').empty(); + } + + this.setDisplayStyle = function(displayStyle) { + documentElement.dom().css('display', displayStyle); + documentElement._container().css('display', displayStyle); + }; + + this.tag = function() { + return documentElement.getWlxmlTag(); + }; + + this.klass = function() { + return documentElement.getWlxmlClass(); + }; +} + +var getDisplayStyle = function(tag, klass) { + if(tag === 'metadata') + return 'none'; + if(tag === 'span') + return 'inline'; + return 'block'; +} + +var GenericManager = function(wlxmlElement) { + this.el = wlxmlElement; +}; + +$.extend(GenericManager.prototype, { + setup: function() { + this.el.setDisplayStyle(getDisplayStyle(this.el.tag(), this.el.klass())); + + this.el.clearWidgets(); + this.el.addWidget(widgets.labelWidget(this.el.tag(), this.el.klass())); + + } +}) + +return { + getFor: function(documentElement) { + var wlxmlElement = new DocumentElementAPI(documentElement); + return new GenericManager(wlxmlElement); + } +}; + +}); \ No newline at end of file -- 2.20.1