4 'modules/documentCanvas/classAttributes',
5 'modules/documentCanvas/canvas/utils',
6 'modules/documentCanvas/canvas/widgets',
7 'modules/documentCanvas/canvas/wlxmlManagers'
8 ], function($, _, classAttributes, utils, widgets, wlxmlManagers) {
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)
18 this._setupDOMHandler(htmlElement);
21 var elementTypeFromParams = function(params) {
22 return params.text !== undefined ? DocumentTextElement : DocumentNodeElement;
26 var elementTypeFromNode = function(wlxmlNode) {
27 return wlxmlNode.nodeType === Node.TEXT_NODE ? DocumentTextElement : DocumentNodeElement;
30 $.extend(DocumentElement, {
31 create: function(params, canvas) {
32 return elementTypeFromParams(params).create(params);
35 create2: function(node, canvas) {
36 return elementTypeFromNode(node).create2(node, canvas);
39 createDOM: function(wlxmlNode) {
40 return elementTypeFromParams(wlxmlNode).createDOM(params);
43 fromHTMLElement: function(htmlElement, canvas) {
44 var $element = $(htmlElement);
45 if(htmlElement.nodeType === Node.ELEMENT_NODE && $element.attr('document-node-element') !== undefined)
46 return DocumentNodeElement.fromHTMLElement(htmlElement, canvas);
47 if($element.attr('document-text-element') !== undefined || (htmlElement.nodeType === Node.TEXT_NODE && $element.parent().attr('document-text-element') !== undefined))
48 return DocumentTextElement.fromHTMLElement(htmlElement, canvas);
53 $.extend(DocumentElement.prototype, {
54 _setupDOMHandler: function(htmlElement) {
55 this.$element = $(htmlElement);
58 return $.contains(document.documentElement, this.dom()[0]);
64 var parents = this.$element.parents('[document-node-element]');
66 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 wrapWithNodeElement: function(wlxmlNode) {
85 var wrapper = DocumentNodeElement.create({tag: wlxmlNode.tag, klass: wlxmlNode.klass}, this);
86 this.dom().replaceWith(wrapper.dom());
91 markAsCurrent: function() {
92 this.canvas.markAsCurrent(this);
95 getVerticallyFirstTextElement: function() {
97 this.children().some(function(child) {
98 if(!child.isVisible())
99 return false; // continue
100 if(child instanceof DocumentTextElement) {
102 return true; // break
104 toret = child.getVerticallyFirstTextElement();
106 return true; // break
112 getPreviousTextElement: function(includeInvisible) {
113 return this.getNearestTextElement('above', includeInvisible);
116 getNextTextElement: function(includeInvisible) {
117 return this.getNearestTextElement('below', includeInvisible);
120 getNearestTextElement: function(direction, includeInvisible) {
121 includeInvisible = includeInvisible !== undefined ? includeInvisible : false;
122 var selector = '[document-text-element]' + (includeInvisible ? '' : ':visible');
123 return this.canvas.getDocumentElement(utils.nearestInDocumentOrder(selector, direction, this.dom()[0]));
126 isVisible: function() {
127 return this instanceof DocumentTextElement || this.getWlxmlTag() !== 'metadata';
130 isInsideList: function() {
131 return this.parents().some(function(parent) {
132 return parent.is('list');
136 exec: function(method) {
137 var manager = this.data('_wlxmlManager');
139 return manager[method].apply(manager, Array.prototype.slice.call(arguments, 1));
144 // DocumentNodeElement represents an element node from WLXML document rendered inside Canvas
145 var DocumentNodeElement = function(htmlElement, canvas) {
146 DocumentElement.call(this, htmlElement, canvas);
149 $.extend(DocumentNodeElement, {
150 createDOM: function(params, canvas) {
152 .attr('document-node-element', ''),
153 widgetsContainer = $('<div>')
154 .addClass('canvas-widgets')
155 .attr('contenteditable', false),
156 container = $('<div>')
157 .attr('document-element-content', '');
159 dom.append(widgetsContainer, container);
160 // Make sure widgets aren't navigable with arrow keys
161 widgetsContainer.find('*').add(widgetsContainer).attr('tabindex', -1);
163 var element = this.fromHTMLElement(dom[0], canvas);
165 element.setWlxml({tag: params.tag, klass: params.klass});
167 _.keys(params.meta).forEach(function(key) {
168 element.setWlxmlMetaAttr(key, params.meta[key]);
171 element.data('other-attrs', params.others);
173 if(params.rawChildren && params.rawChildren.length) {
174 container.append(params.rawChildren);
175 } else if(params.prepopulateOnEmpty) {
176 element.append(DocumentTextElement.create({text: ''}));
181 create: function(params, canvas) {
182 return this.fromHTMLElement(this.createDOM(params, canvas)[0], canvas);
185 fromHTMLElement: function(htmlElement, canvas) {
186 return new this(htmlElement, canvas);
189 createDOM2: function(wlxmlNode, canvas) {
191 // tag: wlxmlNode.getTagName(),
192 // klass: wlxmlNode.getClass(), //currentTag.attr('class'),
193 // meta: wlxmlNode.getMetaAttributes(), //meta,
194 // others: wlxmlNode.getOtherAttributes(), // ~ //others,
195 // rawChildren: wlxmlNode.contents(),
196 // prepopulateOnEmpty: true
199 .attr('document-node-element', ''),
200 widgetsContainer = $('<div>')
201 .addClass('canvas-widgets')
202 .attr('contenteditable', false),
203 container = $('<div>')
204 .attr('document-element-content', '');
206 dom.append(widgetsContainer, container);
207 // Make sure widgets aren't navigable with arrow keys
208 widgetsContainer.find('*').add(widgetsContainer).attr('tabindex', -1);
210 var element = this.fromHTMLElement(dom[0], canvas);
212 element.setWlxml({tag: wlxmlNode.getTagName(), klass: wlxmlNode.getClass()});
214 _.keys(wlxmlNode.getMetaAttributes()).forEach(function(key) {
215 element.setWlxmlMetaAttr(key, params.meta[key]);
218 //element.data('other-attrs', params.others);
222 wlxmlNode.contents().forEach((function(node) {
223 container.append(DocumentElement.create2(node).dom());
226 // if(params.rawChildren && params.rawChildren.length) {
227 // container.append(params.rawChildren);
228 // } else if(params.prepopulateOnEmpty) {
229 // element.append(DocumentTextElement.create({text: ''}));
234 create2: function(params, canvas) {
235 return this.fromHTMLElement2(this.createDOM2(params, canvas)[0], canvas);
238 fromHTMLElement2: function(htmlElement, canvas) {
239 return new this(htmlElement, canvas);
243 var manipulate = function(e, params, action) {
245 if(params instanceof DocumentElement) {
248 element = DocumentElement.create(params);
250 var target = (action === 'append' || action === 'prepend') ? e._container() : e.dom();
251 target[action](element.dom());
255 DocumentNodeElement.prototype = new DocumentElement();
258 $.extend(DocumentNodeElement.prototype, {
259 _container: function() {
260 return this.dom().children('[document-element-content]');
263 var parent = this.parent();
267 var parentChildren = parent.children(),
268 myIdx = parent.childIndex(this);
270 if(myIdx > 0 && myIdx < parentChildren.length) {
271 if((parentChildren[myIdx-1] instanceof DocumentTextElement) && (parentChildren[myIdx+1] instanceof DocumentTextElement)) {
272 parentChildren[myIdx-1].appendText(parentChildren[myIdx+1].getText());
273 parentChildren[myIdx+1].detach();
280 unwrapContents: function() {
281 var parent = this.parent();
285 var parentChildren = parent.children(),
286 myChildren = this.children(),
287 myIdx = parent.childIndex(this);
289 if(myChildren.length === 0)
290 return this.detach();
292 var moveLeftRange, moveRightRange, leftMerged;
294 if(myIdx > 0 && (parentChildren[myIdx-1] instanceof DocumentTextElement) && (myChildren[0] instanceof DocumentTextElement)) {
295 parentChildren[myIdx-1].appendText(myChildren[0].getText());
296 myChildren[0].detach();
297 moveLeftRange = true;
303 if(!(leftMerged && myChildren.length === 1)) {
304 if(myIdx < parentChildren.length - 1 && (parentChildren[myIdx+1] instanceof DocumentTextElement) && (myChildren[myChildren.length-1] instanceof DocumentTextElement)) {
305 parentChildren[myIdx+1].prependText(myChildren[myChildren.length-1].getText());
306 myChildren[myChildren.length-1].detach();
307 moveRightRange = true;
311 var childrenLength = this.children().length;
312 this.children().forEach(function(child) {
319 element1: parent.children()[myIdx + (moveLeftRange ? -1 : 0)],
320 element2: parent.children()[myIdx + childrenLength-1 + (moveRightRange ? 1 : 0)]
324 var dom = this.dom(),
325 args = Array.prototype.slice.call(arguments, 0);
326 if(args.length === 2 && args[1] === undefined)
327 return dom.removeData(args[0]);
328 return dom.data.apply(dom, arguments);
330 toXML: function(level) {
331 var node = $('<' + this.getWlxmlTag() + '>');
333 if(this.getWlxmlClass())
334 node.attr('class', this.getWlxmlClass());
335 var meta = this.getWlxmlMetaAttrs();
336 meta.forEach(function(attr) {
338 node.attr('meta-' + attr.name, attr.value);
340 _.keys(this.data('other-attrs') || {}).forEach(function(key) {
341 node.attr(key, this.data('other-attrs')[key]);
344 var addFormatting = function() {
345 var toret = $('<div>');
346 var formattings = {};
348 if(this.data('orig-before') !== undefined) {
349 if(this.data('orig-before')) {
350 toret.prepend(document.createTextNode(this.data('orig-before')));
352 } else if(level && this.getWlxmlTag() !== 'span') {
353 toret.append('\n' + (new Array(level * 2 + 1)).join(' '));
358 if(this.data('orig-after')) {
359 toret.append(document.createTextNode(this.data('orig-after')));
363 if(this.data('orig-begin')) {
364 node.prepend(this.data('orig-begin'));
365 formattings.begin = true;
368 if(this.data('orig-end') !== undefined) {
369 if(this.data('orig-end')) {
370 node.append(this.data('orig-end'));
372 } else if(this.getWlxmlTag() !== 'span' && children.length){
373 node.append('\n' + (new Array(level * 2 + 1)).join(' '));
376 return {parts: toret.contents(), formattings: formattings};
381 var children = this.children(),
384 var formatting = addFormatting(node);
386 for(var i = children.length - 1; i >= 0; i--) {
387 childParts = children[i].toXML(level + 1);
388 if(typeof childParts === 'string')
389 childParts = [document.createTextNode(childParts)];
391 if(formatting.formattings.begin) {
392 $(node.contents()[0]).after(childParts);
394 node.prepend(childParts);
396 return formatting.parts;
398 append: function(params) {
399 if(params.tag !== 'span')
400 this.data('orig-end', undefined);
401 return manipulate(this, params, 'append');
403 prepend: function(params) {
404 return manipulate(this, params, 'prepend');
406 before: function(params) {
407 return manipulate(this, params, 'before');
410 after: function(params) {
411 return manipulate(this, params, 'after');
413 children: function() {
415 if(this instanceof DocumentTextElement)
419 var elementContent = this._container().contents();
421 elementContent.each(function(idx) {
422 var childElement = DocumentElement.fromHTMLElement(this, element.canvas);
423 if(childElement === undefined)
425 if(idx === 0 && elementContent.length > 1 && elementContent[1].nodeType === Node.ELEMENT_NODE && (childElement instanceof DocumentTextElement) && $.trim($(this).text()) === '')
427 if(idx > 0 && childElement instanceof DocumentTextElement) {
428 if(toret[toret.length-1] instanceof DocumentNodeElement && $.trim($(this).text()) === '')
431 toret.push(childElement);
435 childIndex: function(child) {
436 var children = this.children(),
438 children.forEach(function(c, idx) {
439 if(c.sameNode(child)) {
446 getWlxmlTag: function() {
447 return this._container().attr('wlxml-tag');
449 setWlxmlTag: function(tag) {
450 if(tag === this.getWlxmlTag())
453 this._container().attr('wlxml-tag', tag);
454 if(!this.__updatingWlxml)
455 this._updateWlxmlManager();
457 getWlxmlClass: function() {
458 var klass = this._container().attr('wlxml-class');
460 return klass.replace(/-/g, '.');
463 setWlxmlClass: function(klass) {
464 if(klass === this.getWlxmlClass())
467 this.getWlxmlMetaAttrs().forEach(function(attr) {
468 if(!classAttributes.hasMetaAttr(klass, attr.name))
469 this.dom().removeAttr('wlxml-meta-' + attr.name);
473 this._container().attr('wlxml-class', klass.replace(/\./g, '-'));
475 this._container().removeAttr('wlxml-class');
476 if(!this.__updatingWlxml)
477 this._updateWlxmlManager();
479 setWlxml: function(params) {
480 this.__updatingWlxml = true;
481 if(params.tag !== undefined)
482 this.setWlxmlTag(params.tag);
483 if(params.klass !== undefined)
484 this.setWlxmlClass(params.klass);
485 this._updateWlxmlManager();
486 this.__updatingWlxml = false;
488 _updateWlxmlManager: function() {
489 var manager = wlxmlManagers.getFor(this);
490 this.data('_wlxmlManager', manager);
494 if(what === 'list' && _.contains(['list.items', 'list.items.enum'], this.getWlxmlClass()))
499 getWlxmlMetaAttr: function(attr) {
500 return this.dom().attr('wlxml-meta-'+attr);
503 getWlxmlMetaAttrs: function() {
505 var attrList = classAttributes.getMetaAttrsList(this.getWlxmlClass());
506 attrList.all.forEach(function(attr) {
507 toret.push({name: attr.name, value: this.getWlxmlMetaAttr(attr.name) || ''});
512 setWlxmlMetaAttr: function(attr, value) {
513 this.dom().attr('wlxml-meta-'+attr, value);
516 toggleLabel: function(toggle) {
517 var displayCss = toggle ? 'inline-block' : 'none';
518 var label = this.dom().children('.canvas-widgets').find('.canvas-widget-label');
519 label.css('display', displayCss);
520 this.toggleHighlight(toggle);
523 toggleHighlight: function(toggle) {
524 this._container().toggleClass('highlighted-element', toggle);
527 toggle: function(toggle) {
528 var mng = this.data('_wlxmlManager');
536 // DocumentNodeElement represents a text node from WLXML document rendered inside Canvas
537 var DocumentTextElement = function(htmlElement, canvas) {
538 DocumentElement.call(this, htmlElement, canvas);
541 $.extend(DocumentTextElement, {
542 createDOM: function(params) {
544 .attr('document-text-element', '')
545 .text(params.text || utils.unicode.ZWS);
548 createDOM2: function(wlxmlTextNode) {
550 .attr('document-text-element', '')
551 .text(wlxmlTextNode.getText() || utils.unicode.ZWS);
554 create: function(params, canvas) {
555 return this.fromHTMLElement(this.createDOM(params)[0]);
558 create2: function(wlxmlTextNode, canvas) {
559 return this.fromHTMLElement(this.createDOM2(wlxmlTextNode)[0]);
562 fromHTMLElement: function(htmlElement, canvas) {
563 return new this(htmlElement, canvas);
565 isContentContainer: function(htmlElement) {
566 return htmlElement.nodeType === Node.TEXT_NODE && $(htmlElement).parent().is('[document-text-element]');
570 DocumentTextElement.prototype = new DocumentElement();
572 $.extend(DocumentTextElement.prototype, {
573 toXML: function(parent) {
574 return this.getText();
576 _setupDOMHandler: function(htmlElement) {
577 var $element = $(htmlElement);
578 if(htmlElement.nodeType === Node.TEXT_NODE)
579 this.$element = $element.parent();
581 this.$element = $element;
588 setText: function(text) {
589 this.dom().contents()[0].data = text;
591 appendText: function(text) {
592 this.dom().contents()[0].data += text;
594 prependText: function(text) {
595 this.dom().contents()[0].data = text + this.dom().contents()[0].data;
597 getText: function() {
598 return this.dom().text().replace(utils.unicode.ZWS, '');
600 isEmpty: function() {
601 // Having at least Zero Width Space is guaranteed be Content Observer
602 return this.dom().contents()[0].data === utils.unicode.ZWS;
604 after: function(params) {
605 if(params instanceof DocumentTextElement || params.text)
608 if(params instanceof DocumentNodeElement) {
611 element = DocumentNodeElement.create(params, this.canvas);
613 this.dom().wrap('<div>');
614 this.dom().parent().after(element.dom());
618 before: function(params) {
619 if(params instanceof DocumentTextElement || params.text)
622 if(params instanceof DocumentNodeElement) {
625 element = DocumentNodeElement.create(params, this.canvas);
627 this.dom().wrap('<div>');
628 this.dom().parent().before(element.dom());
632 wrapWithNodeElement: function(wlxmlNode) {
633 if(typeof wlxmlNode.start === 'number' && typeof wlxmlNode.end === 'number') {
634 return this.canvas.wrapText({
635 inside: this.parent(),
636 textNodeIdx: this.parent().childIndex(this),
637 offsetStart: Math.min(wlxmlNode.start, wlxmlNode.end),
638 offsetEnd: Math.max(wlxmlNode.start, wlxmlNode.end),
639 _with: {tag: wlxmlNode.tag, klass: wlxmlNode.klass}
642 return DocumentElement.prototype.wrapWithNodeElement.call(this, wlxmlNode);
646 var parent = this.parent(),
648 if(parent.children().length === 1) {
649 toret = parent.parent();
650 var grandParent = parent.parent();
652 var grandParentChildren = grandParent.children(),
653 idx = grandParent.childIndex(parent),
654 prev = idx - 1 > -1 ? grandParentChildren[idx-1] : null,
655 next = idx + 1 < grandParentChildren.length ? grandParentChildren[idx+1] : null;
657 prev = (prev instanceof DocumentTextElement) ? prev : null;
658 next = (next instanceof DocumentTextElement) ? next : null;
661 prev.setText(prev.getText() + this.getText() + next.getText());
663 } else if (prev || next) {
664 var target = prev ? prev : next,
665 newText = prev ? target.getText() + this.getText() : this.getText() + target.getText();
666 target.setText(newText);
677 split: function(params) {
678 var parentElement = this.parent(),
679 myIdx = parentElement.childIndex(this),
680 myCanvas = this.canvas,
682 succeedingChildren = [],
684 prefix = this.getText().substr(0, params.offset),
685 suffix = this.getText().substr(params.offset);
687 parentElement.children().forEach(function(child) {
689 succeedingChildren.push(child);
690 if(child.sameNode(thisElement))
694 if(prefix.length > 0)
695 this.setText(prefix);
699 var newElement = DocumentNodeElement.create({tag: parentElement.getWlxmlTag(), klass: parentElement.getWlxmlClass()}, myCanvas);
700 parentElement.after(newElement);
702 if(suffix.length > 0)
703 newElement.append({text: suffix});
704 succeedingChildren.forEach(function(child) {
705 newElement.append(child);
708 return {first: parentElement, second: newElement};
710 divide: function(params) {
711 var myText = this.getText();
713 if(params.offset === myText.length)
714 return this.after(params);
715 if(params.offset === 0)
716 return this.before(params);
718 var lhsText = myText.substr(0, params.offset),
719 rhsText = myText.substr(params.offset),
720 newElement = DocumentNodeElement.create({tag: params.tag, klass: params.klass}, this.canvas),
721 rhsTextElement = DocumentTextElement.create({text: rhsText});
723 this.setText(lhsText);
724 this.after(newElement);
725 newElement.after(rhsTextElement);
729 toggleHighlight: function() {
730 // do nothing for now
735 DocumentElement: DocumentElement,
736 DocumentNodeElement: DocumentNodeElement,
737 DocumentTextElement: DocumentTextElement