2 'libs/jquery-1.9.1.min',
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 $.extend(DocumentElement, {
27 create: function(params, canvas) {
28 return elementTypeFromParams(params).create(params);
31 createDOM: function(params) {
32 return elementTypeFromParams(params).createDOM(params);
35 fromHTMLElement: function(htmlElement, canvas) {
36 var $element = $(htmlElement);
37 if(htmlElement.nodeType === Node.ELEMENT_NODE && $element.attr('document-node-element') !== undefined)
38 return DocumentNodeElement.fromHTMLElement(htmlElement, canvas);
39 if($element.attr('document-text-element') !== undefined || (htmlElement.nodeType === Node.TEXT_NODE && $element.parent().attr('document-text-element') !== undefined))
40 return DocumentTextElement.fromHTMLElement(htmlElement, canvas);
45 $.extend(DocumentElement.prototype, {
46 _setupDOMHandler: function(htmlElement) {
47 this.$element = $(htmlElement);
50 return this.canvas !== undefined;
56 var parents = this.$element.parents('[document-node-element]');
58 return DocumentElement.fromHTMLElement(parents[0], this.canvas);
64 parent = this.parent();
67 parent = parent.parent();
72 sameNode: function(other) {
73 return other && (typeof other === typeof this) && other.dom()[0] === this.dom()[0];
76 wrapWithNodeElement: function(wlxmlNode) {
77 var wrapper = DocumentNodeElement.create({tag: wlxmlNode.tag, klass: wlxmlNode.klass}, this);
78 this.dom().replaceWith(wrapper.dom());
83 markAsCurrent: function() {
84 this.canvas.markAsCurrent(this);
87 getVerticallyFirstTextElement: function() {
89 this.children().some(function(child) {
90 if(!child.isVisible())
91 return false; // continue
92 if(child instanceof DocumentTextElement) {
96 toret = child.getVerticallyFirstTextElement();
104 isVisible: function() {
105 return this instanceof DocumentTextElement || this.getWlxmlTag() !== 'metadata';
108 isInsideList: function() {
109 return this.parents().some(function(parent) {
110 return parent.is('list');
114 exec: function(method) {
115 var manager = this.data('_wlxmlManager');
117 return manager[method].apply(manager, Array.prototype.slice.call(arguments, 1));
122 // DocumentNodeElement represents an element node from WLXML document rendered inside Canvas
123 var DocumentNodeElement = function(htmlElement, canvas) {
124 DocumentElement.call(this, htmlElement, canvas);
127 $.extend(DocumentNodeElement, {
128 createDOM: function(params, canvas) {
130 .attr('document-node-element', ''),
131 widgetsContainer = $('<div>')
132 .addClass('canvas-widgets')
133 .attr('contenteditable', false),
134 container = $('<div>')
135 .attr('document-element-content', '');
137 dom.append(widgetsContainer, container);
138 // Make sure widgets aren't navigable with arrow keys
139 widgetsContainer.find('*').add(widgetsContainer).attr('tabindex', -1);
141 var element = this.fromHTMLElement(dom[0], canvas);
143 element.setWlxml({tag: params.tag, klass: params.klass});
145 _.keys(params.meta).forEach(function(key) {
146 element.setWlxmlMetaAttr(key, params.meta[key]);
149 element.data('other-attrs', params.others);
151 if(params.rawChildren && params.rawChildren.length) {
152 container.append(params.rawChildren);
153 } else if(params.prepopulateOnEmpty) {
154 element.append(DocumentTextElement.create({text: ''}));
159 create: function(params, canvas) {
160 return this.fromHTMLElement(this.createDOM(params, canvas)[0], canvas);
163 fromHTMLElement: function(htmlElement, canvas) {
164 return new this(htmlElement, canvas);
168 var manipulate = function(e, params, action) {
170 if(params instanceof DocumentElement) {
173 element = DocumentElement.create(params);
175 var target = (action === 'append' || action === 'prepend') ? e._container() : e.dom();
176 target[action](element.dom());
180 DocumentNodeElement.prototype = new DocumentElement();
183 $.extend(DocumentNodeElement.prototype, {
184 _container: function() {
185 return this.dom().children('[document-element-content]');
188 var parent = this.parent();
192 var parentChildren = parent.children(),
193 myIdx = parent.childIndex(this);
195 if(myIdx > 0 && myIdx < parentChildren.length) {
196 if((parentChildren[myIdx-1] instanceof DocumentTextElement) && (parentChildren[myIdx+1] instanceof DocumentTextElement)) {
197 parentChildren[myIdx-1].appendText(parentChildren[myIdx+1].getText());
198 parentChildren[myIdx+1].detach();
206 var dom = this.dom(),
207 args = Array.prototype.slice.call(arguments, 0);
208 if(args.length === 2 && args[1] === undefined)
209 return dom.removeData(args[0]);
210 return dom.data.apply(dom, arguments);
212 toXML: function(level) {
213 var node = $('<' + this.getWlxmlTag() + '>');
215 if(this.getWlxmlClass())
216 node.attr('class', this.getWlxmlClass());
217 var meta = this.getWlxmlMetaAttrs();
218 meta.forEach(function(attr) {
220 node.attr('meta-' + attr.name, attr.value);
222 _.keys(this.data('other-attrs') || {}).forEach(function(key) {
223 node.attr(key, this.data('other-attrs')[key]);
226 var addFormatting = function() {
227 var toret = $('<div>');
228 var formattings = {};
230 if(this.data('orig-before') !== undefined) {
231 if(this.data('orig-before')) {
232 toret.prepend(document.createTextNode(this.data('orig-before')));
234 } else if(level && this.getWlxmlTag() !== 'span') {
235 toret.append('\n' + (new Array(level * 2 + 1)).join(' '));
240 if(this.data('orig-after')) {
241 toret.append(document.createTextNode(this.data('orig-after')));
245 if(this.data('orig-begin')) {
246 node.prepend(this.data('orig-begin'));
247 formattings.begin = true;
250 if(this.data('orig-end') !== undefined) {
251 if(this.data('orig-end')) {
252 node.append(this.data('orig-end'));
254 } else if(this.getWlxmlTag() !== 'span' && children.length){
255 node.append('\n' + (new Array(level * 2 + 1)).join(' '));
258 return {parts: toret.contents(), formattings: formattings};
263 var children = this.children(),
266 var formatting = addFormatting(node);
268 for(var i = children.length - 1; i >= 0; i--) {
269 childParts = children[i].toXML(level + 1);
270 if(typeof childParts === 'string')
271 childParts = [document.createTextNode(childParts)];
273 if(formatting.formattings.begin) {
274 $(node.contents()[0]).after(childParts);
276 node.prepend(childParts);
278 return formatting.parts;
280 append: function(params) {
281 if(params.tag !== 'span')
282 this.data('orig-end', undefined);
283 return manipulate(this, params, 'append');
285 prepend: function(params) {
286 return manipulate(this, params, 'prepend');
288 before: function(params) {
289 return manipulate(this, params, 'before');
292 after: function(params) {
293 return manipulate(this, params, 'after');
295 children: function() {
297 if(this instanceof DocumentTextElement)
301 var elementContent = this._container().contents();
303 elementContent.each(function(idx) {
304 var childElement = DocumentElement.fromHTMLElement(this, element.canvas);
305 if(childElement === undefined)
307 if(idx === 0 && elementContent.length > 1 && elementContent[1].nodeType === Node.ELEMENT_NODE && (childElement instanceof DocumentTextElement) && $.trim($(this).text()) === '')
309 if(idx > 0 && childElement instanceof DocumentTextElement) {
310 if(toret[toret.length-1] instanceof DocumentNodeElement && $.trim($(this).text()) === '')
313 toret.push(childElement);
317 childIndex: function(child) {
318 var children = this.children(),
320 children.forEach(function(c, idx) {
321 if(c.sameNode(child)) {
328 getWlxmlTag: function() {
329 return this._container().attr('wlxml-tag');
331 setWlxmlTag: function(tag) {
332 if(tag === this.getWlxmlTag())
335 this._container().attr('wlxml-tag', tag);
336 if(!this.__updatingWlxml)
337 this._updateWlxmlManager();
339 getWlxmlClass: function() {
340 var klass = this._container().attr('wlxml-class');
342 return klass.replace(/-/g, '.');
345 setWlxmlClass: function(klass) {
346 if(klass === this.getWlxmlClass())
349 this.getWlxmlMetaAttrs().forEach(function(attr) {
350 if(!classAttributes.hasMetaAttr(klass, attr.name))
351 this.dom().removeAttr('wlxml-meta-' + attr.name);
355 this._container().attr('wlxml-class', klass.replace(/\./g, '-'));
357 this._container().removeAttr('wlxml-class');
358 if(!this.__updatingWlxml)
359 this._updateWlxmlManager();
361 setWlxml: function(params) {
362 this.__updatingWlxml = true;
363 if(params.tag !== undefined)
364 this.setWlxmlTag(params.tag);
365 if(params.klass !== undefined)
366 this.setWlxmlClass(params.klass);
367 this._updateWlxmlManager();
368 this.__updatingWlxml = false;
370 _updateWlxmlManager: function() {
371 var manager = wlxmlManagers.getFor(this);
372 this.data('_wlxmlManager', manager);
376 if(what === 'list' && _.contains(['list.items', 'list.items.enum'], this.getWlxmlClass()))
381 getWlxmlMetaAttr: function(attr) {
382 return this.dom().attr('wlxml-meta-'+attr);
385 getWlxmlMetaAttrs: function() {
387 var attrList = classAttributes.getMetaAttrsList(this.getWlxmlClass());
388 attrList.all.forEach(function(attr) {
389 toret.push({name: attr.name, value: this.getWlxmlMetaAttr(attr.name) || ''});
394 setWlxmlMetaAttr: function(attr, value) {
395 this.dom().attr('wlxml-meta-'+attr, value);
398 toggleLabel: function(toggle) {
399 var displayCss = toggle ? 'inline-block' : 'none';
400 var label = this.dom().children('.canvas-widgets').find('.canvas-widget-label');
401 label.css('display', displayCss);
402 this.toggleHighlight(toggle);
405 toggleHighlight: function(toggle) {
406 this._container().toggleClass('highlighted-element', toggle);
409 toggle: function(toggle) {
410 var mng = this.data('_wlxmlManager');
418 // DocumentNodeElement represents a text node from WLXML document rendered inside Canvas
419 var DocumentTextElement = function(htmlElement, canvas) {
420 DocumentElement.call(this, htmlElement, canvas);
423 $.extend(DocumentTextElement, {
424 createDOM: function(params) {
426 .attr('document-text-element', '')
427 .text(params.text || utils.unicode.ZWS);
430 create: function(params, canvas) {
431 return this.fromHTMLElement(this.createDOM(params)[0]);
434 fromHTMLElement: function(htmlElement, canvas) {
435 return new this(htmlElement, canvas);
437 isContentContainer: function(htmlElement) {
438 return htmlElement.nodeType === Node.TEXT_NODE && $(htmlElement).parent().is('[document-text-element]');
442 DocumentTextElement.prototype = new DocumentElement();
444 $.extend(DocumentTextElement.prototype, {
445 toXML: function(parent) {
446 return this.getText();
448 _setupDOMHandler: function(htmlElement) {
449 var $element = $(htmlElement);
450 if(htmlElement.nodeType === Node.TEXT_NODE)
451 this.$element = $element.parent();
453 this.$element = $element;
460 setText: function(text) {
461 this.dom().contents()[0].data = text;
463 appendText: function(text) {
464 this.dom().contents()[0].data += text;
466 getText: function() {
467 return this.dom().text().replace(utils.unicode.ZWS, '');
469 isEmpty: function() {
470 // Having at least Zero Width Space is guaranteed be Content Observer
471 return this.dom().contents()[0].data === utils.unicode.ZWS;
473 after: function(params) {
474 if(params instanceof DocumentTextElement || params.text)
477 if(params instanceof DocumentNodeElement) {
480 element = DocumentNodeElement.create(params, this.canvas);
482 this.dom().wrap('<div>');
483 this.dom().parent().after(element.dom());
487 before: function(params) {
488 if(params instanceof DocumentTextElement || params.text)
491 if(params instanceof DocumentNodeElement) {
494 element = DocumentNodeElement.create(params, this.canvas);
496 this.dom().wrap('<div>');
497 this.dom().parent().before(element.dom());
501 wrapWithNodeElement: function(wlxmlNode) {
502 if(typeof wlxmlNode.start === 'number' && typeof wlxmlNode.end === 'number') {
503 return this.canvas.wrapText({
504 inside: this.parent(),
505 textNodeIdx: this.parent().childIndex(this),
506 offsetStart: Math.min(wlxmlNode.start, wlxmlNode.end),
507 offsetEnd: Math.max(wlxmlNode.start, wlxmlNode.end),
508 _with: {tag: wlxmlNode.tag, klass: wlxmlNode.klass}
511 return DocumentElement.prototype.wrapWithNodeElement.call(this, wlxmlNode);
515 var parent = this.parent(),
517 if(parent.children().length === 1) {
518 toret = parent.parent();
519 var grandParent = parent.parent();
521 var grandParentChildren = grandParent.children(),
522 idx = grandParent.childIndex(parent),
523 prev = idx - 1 > -1 ? grandParentChildren[idx-1] : null,
524 next = idx + 1 < grandParentChildren.length ? grandParentChildren[idx+1] : null;
526 prev = (prev instanceof DocumentTextElement) ? prev : null;
527 next = (next instanceof DocumentTextElement) ? next : null;
530 prev.setText(prev.getText() + this.getText() + next.getText());
532 } else if (prev || next) {
533 var target = prev ? prev : next,
534 newText = prev ? target.getText() + this.getText() : this.getText() + target.getText();
535 target.setText(newText);
546 split: function(params) {
547 var parentElement = this.parent(),
548 myIdx = parentElement.childIndex(this),
549 myCanvas = this.canvas,
551 succeedingChildren = [],
553 prefix = this.getText().substr(0, params.offset),
554 suffix = this.getText().substr(params.offset);
556 parentElement.children().forEach(function(child) {
558 succeedingChildren.push(child);
559 if(child.sameNode(thisElement))
563 if(prefix.length > 0)
564 this.setText(prefix);
568 var newElement = DocumentNodeElement.create({tag: parentElement.getWlxmlTag(), klass: parentElement.getWlxmlClass()}, myCanvas);
569 parentElement.after(newElement);
571 if(suffix.length > 0)
572 newElement.append({text: suffix});
573 succeedingChildren.forEach(function(child) {
574 newElement.append(child);
577 return {first: parentElement, second: newElement};
579 divide: function(params) {
580 var myText = this.getText();
582 if(params.offset === myText.length)
583 return this.after(params);
584 if(params.offset === 0)
585 return this.before(params);
587 var lhsText = myText.substr(0, params.offset),
588 rhsText = myText.substr(params.offset),
589 newElement = DocumentNodeElement.create({tag: params.tag, klass: params.klass}, this.canvas),
590 rhsTextElement = DocumentTextElement.create({text: rhsText});
592 this.setText(lhsText);
593 this.after(newElement);
594 newElement.after(rhsTextElement);
598 toggleHighlight: function() {
599 // do nothing for now
604 DocumentElement: DocumentElement,
605 DocumentNodeElement: DocumentNodeElement,
606 DocumentTextElement: DocumentTextElement