4 'modules/documentCanvas/canvas/utils',
5 'modules/documentCanvas/canvas/widgets',
6 'modules/documentCanvas/canvas/wlxmlManagers'
7 ], function($, _, utils, widgets, wlxmlManagers) {
12 // DocumentElement represents a text or an element node from WLXML document rendered inside Canvas
13 var DocumentElement = function(htmlElement, canvas) {
14 if(arguments.length === 0)
17 this._setupDOMHandler(htmlElement);
21 var elementTypeFromWlxmlNode = function(wlxmlNode) {
22 return wlxmlNode.nodeType === Node.TEXT_NODE ? DocumentTextElement : DocumentNodeElement;
25 $.extend(DocumentElement, {
26 create: function(node, canvas) {
27 return elementTypeFromWlxmlNode(node).create(node, canvas);
30 createDOM: function(wlxmlNode) {
31 return elementTypeFromParams(wlxmlNode).createDOM(params);
34 fromHTMLElement: function(htmlElement, canvas) {
35 var $element = $(htmlElement);
36 if(htmlElement.nodeType === Node.ELEMENT_NODE && $element.attr('document-node-element') !== undefined)
37 return DocumentNodeElement.fromHTMLElement(htmlElement, canvas);
38 if($element.attr('document-text-element') !== undefined || (htmlElement.nodeType === Node.TEXT_NODE && $element.parent().attr('document-text-element') !== undefined))
39 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]);
59 return dom.data.apply(dom, arguments);
62 var parents = this.$element.parents('[document-node-element]');
64 return DocumentElement.fromHTMLElement(parents[0], this.canvas);
70 parent = this.parent();
73 parent = parent.parent();
78 sameNode: function(other) {
79 return other && (typeof other === typeof this) && other.dom()[0] === this.dom()[0];
82 wrapWithNodeElement: function(wlxmlNode) {
83 var wrapper = DocumentNodeElement.create({tag: wlxmlNode.tag, klass: wlxmlNode.klass}, this);
84 this.dom().replaceWith(wrapper.dom());
89 markAsCurrent: function() {
90 this.canvas.markAsCurrent(this);
93 getVerticallyFirstTextElement: function() {
95 this.children().some(function(child) {
96 if(!child.isVisible())
97 return false; // continue
98 if(child instanceof DocumentTextElement) {
100 return true; // break
102 toret = child.getVerticallyFirstTextElement();
104 return true; // break
110 getPreviousTextElement: function(includeInvisible) {
111 return this.getNearestTextElement('above', includeInvisible);
114 getNextTextElement: function(includeInvisible) {
115 return this.getNearestTextElement('below', includeInvisible);
118 getNearestTextElement: function(direction, includeInvisible) {
119 includeInvisible = includeInvisible !== undefined ? includeInvisible : false;
120 var selector = '[document-text-element]' + (includeInvisible ? '' : ':visible');
121 return this.canvas.getDocumentElement(utils.nearestInDocumentOrder(selector, direction, this.dom()[0]));
124 isVisible: function() {
125 return this instanceof DocumentTextElement || this.getWlxmlTag() !== 'metadata';
128 isInsideList: function() {
129 return this.parents().some(function(parent) {
130 return parent.is('list');
134 exec: function(method) {
135 var manager = this.data('_wlxmlManager');
137 return manager[method].apply(manager, Array.prototype.slice.call(arguments, 1));
142 // DocumentNodeElement represents an element node from WLXML document rendered inside Canvas
143 var DocumentNodeElement = function(htmlElement, canvas) {
144 DocumentElement.call(this, htmlElement, canvas);
147 $.extend(DocumentNodeElement, {
148 create: function(wlxmlNode, canvas) {
149 return this.fromHTMLElement(this.createDOM(wlxmlNode, canvas)[0], canvas);
152 fromHTMLElement: function(htmlElement, canvas) {
153 return new this(htmlElement, canvas);
156 createDOM: function(wlxmlNode, canvas) {
158 .attr('document-node-element', ''),
159 widgetsContainer = $('<div>')
160 .addClass('canvas-widgets')
161 .attr('contenteditable', false),
162 container = $('<div>')
163 .attr('document-element-content', '');
165 dom.append(widgetsContainer, container);
166 // Make sure widgets aren't navigable with arrow keys
167 widgetsContainer.find('*').add(widgetsContainer).attr('tabindex', -1);
169 var element = this.fromHTMLElement(dom[0], canvas);
171 element.data('wlxmlNode', wlxmlNode);
172 wlxmlNode.setData('canvasElement', element);
174 element.setWlxml({tag: wlxmlNode.getTagName(), klass: wlxmlNode.getClass()});
176 wlxmlNode.contents().forEach((function(node) {
177 container.append(DocumentElement.create(node).dom());
185 var manipulate = function(e, params, action) {
187 if(params instanceof DocumentElement) {
190 element = DocumentElement.create(params);
192 var target = (action === 'append' || action === 'prepend') ? e._container() : e.dom();
193 target[action](element.dom());
197 DocumentNodeElement.prototype = new DocumentElement();
200 $.extend(DocumentNodeElement.prototype, {
201 _container: function() {
202 return this.dom().children('[document-element-content]');
205 var parent = this.parent();
209 var parentChildren = parent.children(),
210 myIdx = parent.childIndex(this);
212 if(myIdx > 0 && myIdx < parentChildren.length) {
213 if((parentChildren[myIdx-1] instanceof DocumentTextElement) && (parentChildren[myIdx+1] instanceof DocumentTextElement)) {
214 parentChildren[myIdx-1].appendText(parentChildren[myIdx+1].getText());
215 parentChildren[myIdx+1].detach();
222 unwrapContents: function() {
223 var parent = this.parent();
227 var parentChildren = parent.children(),
228 myChildren = this.children(),
229 myIdx = parent.childIndex(this);
231 if(myChildren.length === 0)
232 return this.detach();
234 var moveLeftRange, moveRightRange, leftMerged;
236 if(myIdx > 0 && (parentChildren[myIdx-1] instanceof DocumentTextElement) && (myChildren[0] instanceof DocumentTextElement)) {
237 parentChildren[myIdx-1].appendText(myChildren[0].getText());
238 myChildren[0].detach();
239 moveLeftRange = true;
245 if(!(leftMerged && myChildren.length === 1)) {
246 if(myIdx < parentChildren.length - 1 && (parentChildren[myIdx+1] instanceof DocumentTextElement) && (myChildren[myChildren.length-1] instanceof DocumentTextElement)) {
247 parentChildren[myIdx+1].prependText(myChildren[myChildren.length-1].getText());
248 myChildren[myChildren.length-1].detach();
249 moveRightRange = true;
253 var childrenLength = this.children().length;
254 this.children().forEach(function(child) {
261 element1: parent.children()[myIdx + (moveLeftRange ? -1 : 0)],
262 element2: parent.children()[myIdx + childrenLength-1 + (moveRightRange ? 1 : 0)]
265 append: function(params) {
266 if(params.tag !== 'span')
267 this.data('orig-end', undefined);
268 return manipulate(this, params, 'append');
270 prepend: function(params) {
271 return manipulate(this, params, 'prepend');
273 before: function(params) {
274 return manipulate(this, params, 'before');
277 after: function(params) {
278 return manipulate(this, params, 'after');
280 children: function() {
282 if(this instanceof DocumentTextElement)
286 var elementContent = this._container().contents();
288 elementContent.each(function(idx) {
289 var childElement = DocumentElement.fromHTMLElement(this, element.canvas);
290 if(childElement === undefined)
292 if(idx === 0 && elementContent.length > 1 && elementContent[1].nodeType === Node.ELEMENT_NODE && (childElement instanceof DocumentTextElement) && $.trim($(this).text()) === '')
294 if(idx > 0 && childElement instanceof DocumentTextElement) {
295 if(toret[toret.length-1] instanceof DocumentNodeElement && $.trim($(this).text()) === '')
298 toret.push(childElement);
302 childIndex: function(child) {
303 var children = this.children(),
305 children.forEach(function(c, idx) {
306 if(c.sameNode(child)) {
313 getWlxmlTag: function() {
314 return this._container().attr('wlxml-tag');
316 setWlxmlTag: function(tag) {
317 if(tag === this.getWlxmlTag())
320 this._container().attr('wlxml-tag', tag);
321 if(!this.__updatingWlxml)
322 this._updateWlxmlManager();
324 getWlxmlClass: function() {
325 var klass = this._container().attr('wlxml-class');
327 return klass.replace(/-/g, '.');
330 setWlxmlClass: function(klass) {
331 if(klass === this.getWlxmlClass())
334 this._container().attr('wlxml-class', klass.replace(/\./g, '-'));
336 this._container().removeAttr('wlxml-class');
337 if(!this.__updatingWlxml)
338 this._updateWlxmlManager();
340 setWlxml: function(params) {
341 this.__updatingWlxml = true;
342 if(params.tag !== undefined)
343 this.setWlxmlTag(params.tag);
344 if(params.klass !== undefined)
345 this.setWlxmlClass(params.klass);
346 this._updateWlxmlManager();
347 this.__updatingWlxml = false;
349 _updateWlxmlManager: function() {
350 var manager = wlxmlManagers.getFor(this);
351 this.data('_wlxmlManager', manager);
355 if(what === 'list' && _.contains(['list.items', 'list.items.enum'], this.getWlxmlClass()))
359 toggleLabel: function(toggle) {
360 var displayCss = toggle ? 'inline-block' : 'none';
361 var label = this.dom().children('.canvas-widgets').find('.canvas-widget-label');
362 label.css('display', displayCss);
363 this.toggleHighlight(toggle);
366 toggleHighlight: function(toggle) {
367 this._container().toggleClass('highlighted-element', toggle);
370 toggle: function(toggle) {
371 var mng = this.data('_wlxmlManager');
379 // DocumentNodeElement represents a text node from WLXML document rendered inside Canvas
380 var DocumentTextElement = function(htmlElement, canvas) {
381 DocumentElement.call(this, htmlElement, canvas);
384 $.extend(DocumentTextElement, {
385 createDOM: function(wlxmlTextNode) {
387 .attr('document-text-element', '')
388 .text(wlxmlTextNode.getText() || utils.unicode.ZWS),
389 element = this.fromHTMLElement(dom[0], this);
390 element.data('wlxmlNode', wlxmlTextNode);
394 create: function(wlxmlTextNode, canvas) {
395 return this.fromHTMLElement(this.createDOM(wlxmlTextNode)[0]);
398 fromHTMLElement: function(htmlElement, canvas) {
399 return new this(htmlElement, canvas);
401 isContentContainer: function(htmlElement) {
402 return htmlElement.nodeType === Node.TEXT_NODE && $(htmlElement).parent().is('[document-text-element]');
406 DocumentTextElement.prototype = new DocumentElement();
408 $.extend(DocumentTextElement.prototype, {
409 _setupDOMHandler: function(htmlElement) {
410 var $element = $(htmlElement);
411 if(htmlElement.nodeType === Node.TEXT_NODE)
412 this.$element = $element.parent();
414 this.$element = $element;
421 setText: function(text) {
422 this.dom().contents()[0].data = text;
424 appendText: function(text) {
425 this.dom().contents()[0].data += text;
427 prependText: function(text) {
428 this.dom().contents()[0].data = text + this.dom().contents()[0].data;
430 getText: function(options) {
431 options = _.extend({raw: false}, options || {});
432 var toret = this.dom().text();
434 toret = toret.replace(utils.unicode.ZWS, '');
438 isEmpty: function() {
439 // Having at least Zero Width Space is guaranteed be Content Observer
440 return this.dom().contents()[0].data === utils.unicode.ZWS;
442 after: function(params) {
443 if(params instanceof DocumentTextElement || params.text)
446 if(params instanceof DocumentNodeElement) {
449 element = DocumentNodeElement.create(params, this.canvas);
451 this.dom().wrap('<div>');
452 this.dom().parent().after(element.dom());
456 before: function(params) {
457 if(params instanceof DocumentTextElement || params.text)
460 if(params instanceof DocumentNodeElement) {
463 element = DocumentNodeElement.create(params, this.canvas);
465 this.dom().wrap('<div>');
466 this.dom().parent().before(element.dom());
470 wrapWithNodeElement: function(wlxmlNode) {
471 if(typeof wlxmlNode.start === 'number' && typeof wlxmlNode.end === 'number') {
472 return this.canvas.wrapText({
473 inside: this.parent(),
474 textNodeIdx: this.parent().childIndex(this),
475 offsetStart: Math.min(wlxmlNode.start, wlxmlNode.end),
476 offsetEnd: Math.max(wlxmlNode.start, wlxmlNode.end),
477 _with: {tag: wlxmlNode.tag, klass: wlxmlNode.klass}
480 return DocumentElement.prototype.wrapWithNodeElement.call(this, wlxmlNode);
484 var parent = this.parent(),
486 if(parent.children().length === 1) {
487 toret = parent.parent();
488 var grandParent = parent.parent();
490 var grandParentChildren = grandParent.children(),
491 idx = grandParent.childIndex(parent),
492 prev = idx - 1 > -1 ? grandParentChildren[idx-1] : null,
493 next = idx + 1 < grandParentChildren.length ? grandParentChildren[idx+1] : null;
495 prev = (prev instanceof DocumentTextElement) ? prev : null;
496 next = (next instanceof DocumentTextElement) ? next : null;
499 prev.setText(prev.getText() + this.getText() + next.getText());
501 } else if (prev || next) {
502 var target = prev ? prev : next,
503 newText = prev ? target.getText() + this.getText() : this.getText() + target.getText();
504 target.setText(newText);
515 split: function(params) {
516 var parentElement = this.parent(),
517 myIdx = parentElement.childIndex(this),
518 myCanvas = this.canvas,
520 succeedingChildren = [],
522 prefix = this.getText().substr(0, params.offset),
523 suffix = this.getText().substr(params.offset);
525 parentElement.children().forEach(function(child) {
527 succeedingChildren.push(child);
528 if(child.sameNode(thisElement))
532 if(prefix.length > 0)
533 this.setText(prefix);
537 var newElement = DocumentNodeElement.create({tag: parentElement.getWlxmlTag(), klass: parentElement.getWlxmlClass()}, myCanvas);
538 parentElement.after(newElement);
540 if(suffix.length > 0)
541 newElement.append({text: suffix});
542 succeedingChildren.forEach(function(child) {
543 newElement.append(child);
546 return {first: parentElement, second: newElement};
548 divide: function(params) {
549 var myText = this.getText();
551 if(params.offset === myText.length)
552 return this.after(params);
553 if(params.offset === 0)
554 return this.before(params);
556 var lhsText = myText.substr(0, params.offset),
557 rhsText = myText.substr(params.offset),
558 newElement = DocumentNodeElement.create({tag: params.tag, klass: params.klass}, this.canvas),
559 rhsTextElement = DocumentTextElement.create({text: rhsText});
561 this.setText(lhsText);
562 this.after(newElement);
563 newElement.after(rhsTextElement);
567 toggleHighlight: function() {
568 // do nothing for now
573 DocumentElement: DocumentElement,
574 DocumentNodeElement: DocumentNodeElement,
575 DocumentTextElement: DocumentTextElement