4 'modules/documentCanvas/canvas/utils',
5 'modules/documentCanvas/canvas/widgets',
6 'modules/documentCanvas/canvas/wlxmlManagers'
7 ], function($, _, utils, widgets, wlxmlManagers) {
10 /* global Node:false, document:false */
13 // DocumentElement represents a text or an element node from WLXML document rendered inside Canvas
14 var DocumentElement = function(htmlElement, canvas) {
15 if(arguments.length === 0) {
19 this._setupDOMHandler(htmlElement);
23 var elementTypeFromWlxmlNode = function(wlxmlNode) {
24 return wlxmlNode.nodeType === Node.TEXT_NODE ? DocumentTextElement : DocumentNodeElement;
27 $.extend(DocumentElement, {
28 create: function(node, canvas) {
29 return elementTypeFromWlxmlNode(node).create(node, canvas);
32 fromHTMLElement: function(htmlElement, canvas) {
33 var $element = $(htmlElement);
34 if(htmlElement.nodeType === Node.ELEMENT_NODE && $element.attr('document-node-element') !== undefined) {
35 return DocumentNodeElement.fromHTMLElement(htmlElement, canvas);
37 if($element.attr('document-text-element') !== undefined || (htmlElement.nodeType === Node.TEXT_NODE && $element.parent().attr('document-text-element') !== undefined)) {
38 return DocumentTextElement.fromHTMLElement(htmlElement, canvas);
44 $.extend(DocumentElement.prototype, {
45 _setupDOMHandler: function(htmlElement) {
46 this.$element = $(htmlElement);
49 return $.contains(document.documentElement, this.dom()[0]);
56 args = Array.prototype.slice.call(arguments, 0);
57 if(args.length === 2 && args[1] === undefined) {
58 return dom.removeData(args[0]);
60 return dom.data.apply(dom, arguments);
63 var parents = this.$element.parents('[document-node-element]');
65 return DocumentElement.fromHTMLElement(parents[0], this.canvas);
72 parent = this.parent();
75 parent = parent.parent();
80 sameNode: function(other) {
81 return other && (typeof other === typeof this) && other.dom()[0] === this.dom()[0];
84 markAsCurrent: function() {
85 this.canvas.markAsCurrent(this);
88 getVerticallyFirstTextElement: function() {
90 this.children().some(function(child) {
91 if(!child.isVisible()) {
92 return false; // continue
94 if(child instanceof DocumentTextElement) {
98 toret = child.getVerticallyFirstTextElement();
100 return true; // break
107 getPreviousTextElement: function(includeInvisible) {
108 return this.getNearestTextElement('above', includeInvisible);
111 getNextTextElement: function(includeInvisible) {
112 return this.getNearestTextElement('below', includeInvisible);
115 getNearestTextElement: function(direction, includeInvisible) {
116 includeInvisible = includeInvisible !== undefined ? includeInvisible : false;
117 var selector = '[document-text-element]' + (includeInvisible ? '' : ':visible');
118 return this.canvas.getDocumentElement(utils.nearestInDocumentOrder(selector, direction, this.dom()[0]));
121 isVisible: function() {
122 return this instanceof DocumentTextElement || this.getWlxmlTag() !== 'metadata';
125 isInsideList: function() {
126 return this.parents().some(function(parent) {
127 return parent.is('list');
131 exec: function(method) {
132 var manager = this.data('_wlxmlManager');
133 if(manager[method]) {
134 return manager[method].apply(manager, Array.prototype.slice.call(arguments, 1));
140 // DocumentNodeElement represents an element node from WLXML document rendered inside Canvas
141 var DocumentNodeElement = function(htmlElement, canvas) {
142 DocumentElement.call(this, htmlElement, canvas);
145 $.extend(DocumentNodeElement, {
146 create: function(wlxmlNode, canvas) {
147 return this.fromHTMLElement(this.createDOM(wlxmlNode, canvas)[0], canvas);
150 fromHTMLElement: function(htmlElement, canvas) {
151 return new this(htmlElement, canvas);
154 createDOM: function(wlxmlNode, canvas) {
156 .attr('document-node-element', ''),
157 widgetsContainer = $('<div>')
158 .addClass('canvas-widgets')
159 .attr('contenteditable', false),
160 container = $('<div>')
161 .attr('document-element-content', '');
163 dom.append(widgetsContainer, container);
164 // Make sure widgets aren't navigable with arrow keys
165 widgetsContainer.find('*').add(widgetsContainer).attr('tabindex', -1);
167 var element = this.fromHTMLElement(dom[0], canvas);
169 element.data('wlxmlNode', wlxmlNode);
170 wlxmlNode.setData('canvasElement', element);
172 element.setWlxml({tag: wlxmlNode.getTagName(), klass: wlxmlNode.getClass()});
174 wlxmlNode.contents().forEach(function(node) {
175 container.append(DocumentElement.create(node).dom());
183 var manipulate = function(e, params, action) {
185 if(params instanceof DocumentElement) {
188 element = DocumentElement.create(params);
190 var target = (action === 'append' || action === 'prepend') ? e._container() : e.dom();
191 target[action](element.dom());
195 DocumentNodeElement.prototype = new DocumentElement();
198 $.extend(DocumentNodeElement.prototype, {
199 _container: function() {
200 return this.dom().children('[document-element-content]');
207 append: function(params) {
208 if(params.tag !== 'span') {
209 this.data('orig-end', undefined);
211 return manipulate(this, params, 'append');
213 prepend: function(params) {
214 return manipulate(this, params, 'prepend');
216 before: function(params) {
217 return manipulate(this, params, 'before');
220 after: function(params) {
221 return manipulate(this, params, 'after');
223 children: function() {
225 if(this instanceof DocumentTextElement) {
230 var elementContent = this._container().contents();
232 elementContent.each(function(idx) {
233 var childElement = DocumentElement.fromHTMLElement(this, element.canvas);
234 if(childElement === undefined) {
237 if(idx === 0 && elementContent.length > 1 && elementContent[1].nodeType === Node.ELEMENT_NODE && (childElement instanceof DocumentTextElement) && $.trim($(this).text()) === '') {
240 if(idx > 0 && childElement instanceof DocumentTextElement) {
241 if(toret[toret.length-1] instanceof DocumentNodeElement && $.trim($(this).text()) === '') {
245 toret.push(childElement);
249 childIndex: function(child) {
250 var children = this.children(),
252 children.forEach(function(c, idx) {
253 if(c.sameNode(child)) {
260 getWlxmlTag: function() {
261 return this._container().attr('wlxml-tag');
263 setWlxmlTag: function(tag) {
264 if(tag === this.getWlxmlTag()) {
268 this._container().attr('wlxml-tag', tag);
269 if(!this.__updatingWlxml) {
270 this._updateWlxmlManager();
273 getWlxmlClass: function() {
274 var klass = this._container().attr('wlxml-class');
276 return klass.replace(/-/g, '.');
280 setWlxmlClass: function(klass) {
281 if(klass === this.getWlxmlClass()) {
285 this._container().attr('wlxml-class', klass.replace(/\./g, '-'));
288 this._container().removeAttr('wlxml-class');
290 if(!this.__updatingWlxml) {
291 this._updateWlxmlManager();
294 setWlxml: function(params) {
295 this.__updatingWlxml = true;
296 if(params.tag !== undefined) {
297 this.setWlxmlTag(params.tag);
299 if(params.klass !== undefined) {
300 this.setWlxmlClass(params.klass);
302 this._updateWlxmlManager();
303 this.__updatingWlxml = false;
305 _updateWlxmlManager: function() {
306 var manager = wlxmlManagers.getFor(this);
307 this.data('_wlxmlManager', manager);
311 if(what === 'list' && _.contains(['list.items', 'list.items.enum'], this.getWlxmlClass())) {
316 toggleLabel: function(toggle) {
317 var displayCss = toggle ? 'inline-block' : 'none';
318 var label = this.dom().children('.canvas-widgets').find('.canvas-widget-label');
319 label.css('display', displayCss);
320 this.toggleHighlight(toggle);
323 toggleHighlight: function(toggle) {
324 this._container().toggleClass('highlighted-element', toggle);
327 toggle: function(toggle) {
328 var mng = this.data('_wlxmlManager');
336 // DocumentNodeElement represents a text node from WLXML document rendered inside Canvas
337 var DocumentTextElement = function(htmlElement, canvas) {
338 DocumentElement.call(this, htmlElement, canvas);
341 $.extend(DocumentTextElement, {
342 createDOM: function(wlxmlTextNode) {
344 .attr('document-text-element', '')
345 .text(wlxmlTextNode.getText() || utils.unicode.ZWS),
346 element = this.fromHTMLElement(dom[0], this);
347 element.data('wlxmlNode', wlxmlTextNode);
351 create: function(wlxmlTextNode, canvas) {
352 return this.fromHTMLElement(this.createDOM(wlxmlTextNode)[0]);
355 fromHTMLElement: function(htmlElement, canvas) {
356 return new this(htmlElement, canvas);
358 isContentContainer: function(htmlElement) {
359 return htmlElement.nodeType === Node.TEXT_NODE && $(htmlElement).parent().is('[document-text-element]');
363 DocumentTextElement.prototype = new DocumentElement();
365 $.extend(DocumentTextElement.prototype, {
366 _setupDOMHandler: function(htmlElement) {
367 var $element = $(htmlElement);
368 if(htmlElement.nodeType === Node.TEXT_NODE) {
369 this.$element = $element.parent();
372 this.$element = $element;
380 setText: function(text) {
381 this.dom().contents()[0].data = text;
383 getText: function(options) {
384 options = _.extend({raw: false}, options || {});
385 var toret = this.dom().text();
387 toret = toret.replace(utils.unicode.ZWS, '');
391 isEmpty: function() {
392 // Having at least Zero Width Space is guaranteed be Content Observer
393 return this.dom().contents()[0].data === utils.unicode.ZWS;
395 after: function(params) {
396 if(params instanceof DocumentTextElement || params.text) {
400 if(params instanceof DocumentNodeElement) {
403 element = DocumentElement.create(params, this.canvas);
405 this.dom().wrap('<div>');
406 this.dom().parent().after(element.dom());
410 before: function(params) {
411 if(params instanceof DocumentTextElement || params.text) {
415 if(params instanceof DocumentNodeElement) {
418 element = DocumentNodeElement.create(params, this.canvas);
420 this.dom().wrap('<div>');
421 this.dom().parent().before(element.dom());
426 divide: function(params) {
427 var myText = this.getText();
429 if(params.offset === myText.length) {
430 return this.after(params);
432 if(params.offset === 0) {
433 return this.before(params);
436 var lhsText = myText.substr(0, params.offset),
437 rhsText = myText.substr(params.offset),
438 newElement = DocumentNodeElement.create({tag: params.tag, klass: params.klass}, this.canvas),
439 rhsTextElement = DocumentTextElement.create({text: rhsText});
441 this.setText(lhsText);
442 this.after(newElement);
443 newElement.after(rhsTextElement);
447 toggleHighlight: function() {
448 // do nothing for now
453 DocumentElement: DocumentElement,
454 DocumentNodeElement: DocumentNodeElement,
455 DocumentTextElement: DocumentTextElement