4 'modules/documentCanvas/canvas/utils'
5 ], function($, _, utils) {
8 /* global Node:false */
10 // DocumentElement represents a text or an element node from WLXML document rendered inside Canvas
11 var DocumentElement = function(wlxmlNode, canvas) {
12 this.wlxmlNode = wlxmlNode;
19 this.dom = this.createDOM();
20 this.dom.data('canvas-element', this);
23 $.extend(DocumentElement.prototype, {
24 refreshPath: function() {
25 this.parents().forEach(function(parent) {
33 updateState: function(toUpdate) {
36 .filter(function(key) {
37 return this.state.hasOwnProperty(key);
39 .forEach(function(key) {
40 if(this.state !== toUpdate[key]) {
41 this.state[key] = changes[key] = toUpdate[key];
44 if(_.isFunction(this.onStateChange)) {
45 this.onStateChange(changes);
49 var parents = this.dom.parents('[document-node-element]');
51 return this.canvas.getDocumentElement(parents[0]);
58 parent = this.parent();
61 parent = parent.parent();
66 sameNode: function(other) {
67 return other && (typeof other === typeof this) && other.dom[0] === this.dom[0];
71 this.canvas.eventBus.trigger.apply(this.canvas.eventBus, Array.prototype.slice.call(arguments, 0));
78 // DocumentNodeElement represents an element node from WLXML document rendered inside Canvas
79 var DocumentNodeElement = function(wlxmlNode, canvas) {
80 DocumentElement.call(this, wlxmlNode, canvas);
81 wlxmlNode.setData('canvasElement', this);
86 var manipulate = function(e, params, action) {
88 if(params instanceof DocumentElement) {
91 element = e.canvas.createElement(params);
94 e.dom[action](element.dom);
100 DocumentNodeElement.prototype = Object.create(DocumentElement.prototype);
103 $.extend(DocumentNodeElement.prototype, {
104 defaultDisplayStyle: 'block',
106 addWidget: function(widget) {
107 this.dom.children('.canvas-widgets').append(widget.DOM ? widget.DOM : widget);
109 clearWidgets: function() {
110 this.dom.children('.canvas-widgets').empty();
112 handle: function(event) {
113 var method = 'on' + event.type[0].toUpperCase() + event.type.substr(1);
118 createDOM: function() {
119 var wrapper = $('<div>').attr('document-node-element', ''),
120 widgetsContainer = $('<div>')
121 .addClass('canvas-widgets')
122 .attr('contenteditable', false),
123 contentContainer = $('<div>')
124 .attr('document-element-content', '');
126 wrapper.append(contentContainer, widgetsContainer);
127 widgetsContainer.find('*').add(widgetsContainer).attr('tabindex', -1);
130 _container: function() {
131 return this.dom.children('[document-element-content]');
134 var parents = this.parents();
137 parents[0].refreshPath();
141 before: function(params) {
142 return manipulate(this, params, 'before');
145 after: function(params) {
146 return manipulate(this, params, 'after');
149 isBlock: function() {
150 return this.dom.css('display') === 'block';
153 displayAsBlock: function() {
154 this.dom.css('display', 'block');
155 this._container().css('display', 'block');
157 displayInline: function() {
158 this.dom.css('display', 'inline');
159 this._container().css('display', 'inline');
161 displayAs: function(what) {
162 // [this.dom(), this._container()].forEach(e) {
163 // var isBlock = window.getComputedStyle(e).display === 'block';
164 // if(!isBlock && what === 'block') {
165 // e.css('display', what);
166 // } else if(isBlock && what === 'inline') {
170 this.dom.css('display', what);
171 this._container().css('display', what);
176 // DocumentNodeElement represents a text node from WLXML document rendered inside Canvas
177 var DocumentTextElement = function(wlxmlTextNode, canvas) {
178 DocumentElement.call(this, wlxmlTextNode, canvas);
181 $.extend(DocumentTextElement, {
182 isContentContainer: function(htmlElement) {
183 return htmlElement.nodeType === Node.TEXT_NODE && $(htmlElement).parent().is('[document-text-element]');
187 DocumentTextElement.prototype = Object.create(DocumentElement.prototype);
189 $.extend(DocumentTextElement.prototype, {
190 createDOM: function() {
192 .attr('document-text-element', '')
193 .text(this.wlxmlNode.getText() || utils.unicode.ZWS);
200 setText: function(text) {
202 text = utils.unicode.ZWS;
204 if(text !== this.getText()) {
205 this.dom.contents()[0].data = text;
208 getText: function(options) {
209 options = _.extend({raw: false}, options || {});
210 var toret = this.dom.text();
212 toret = toret.replace(utils.unicode.ZWS, '');
216 isEmpty: function() {
217 // Having at least Zero Width Space is guaranteed be Content Observer
218 return this.dom.contents()[0].data === utils.unicode.ZWS;
220 after: function(params) {
221 if(params instanceof DocumentTextElement || params.text) {
225 if(params instanceof DocumentNodeElement) {
228 element = this.canvas.createElement(params);
231 this.dom.wrap('<div>');
232 this.dom.parent().after(element.dom);
238 before: function(params) {
239 if(params instanceof DocumentTextElement || params.text) {
243 if(params instanceof DocumentNodeElement) {
246 element = this.canvas.createElement(params);
249 this.dom.wrap('<div>');
250 this.dom.parent().before(element.dom);
257 children: function() {
265 DocumentElement: DocumentElement,
266 DocumentNodeElement: DocumentNodeElement,
267 DocumentTextElement: DocumentTextElement