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');
116 // DocumentNodeElement represents an element node from WLXML document rendered inside Canvas
117 var DocumentNodeElement = function(htmlElement, canvas) {
118 DocumentElement.call(this, htmlElement, canvas);
121 $.extend(DocumentNodeElement, {
122 createDOM: function(params, canvas) {
124 .attr('document-node-element', ''),
125 widgetsContainer = $('<div>')
126 .addClass('canvas-widgets')
127 .attr('contenteditable', false),
128 container = $('<div>')
129 .attr('document-element-content', '');
131 dom.append(widgetsContainer, container);
132 // Make sure widgets aren't navigable with arrow keys
133 widgetsContainer.find('*').add(widgetsContainer).attr('tabindex', -1);
135 var element = this.fromHTMLElement(dom[0], canvas);
137 element.setWlxml({tag: params.tag, klass: params.klass});
139 _.keys(params.meta).forEach(function(key) {
140 element.setWlxmlMetaAttr(key, params.meta[key]);
143 element.data('other-attrs', params.others);
145 if(params.rawChildren) {
146 container.append(params.rawChildren);
151 create: function(params, canvas) {
152 return this.fromHTMLElement(this.createDOM(params, canvas)[0], canvas);
155 fromHTMLElement: function(htmlElement, canvas) {
156 return new this(htmlElement, canvas);
160 var manipulate = function(e, params, action) {
162 if(params instanceof DocumentElement) {
165 element = DocumentElement.create(params);
167 var target = (action === 'append' || action === 'prepend') ? e._container() : e.dom();
168 target[action](element.dom());
172 DocumentNodeElement.prototype = new DocumentElement();
175 $.extend(DocumentNodeElement.prototype, {
176 _container: function() {
177 return this.dom().children('[document-element-content]');
180 var parent = this.parent();
184 var parentChildren = parent.children(),
185 myIdx = parent.childIndex(this);
187 if(myIdx > 0 && myIdx < parentChildren.length) {
188 if((parentChildren[myIdx-1] instanceof DocumentTextElement) && (parentChildren[myIdx+1] instanceof DocumentTextElement)) {
189 parentChildren[myIdx-1].appendText(parentChildren[myIdx+1].getText());
190 parentChildren[myIdx+1].detach();
198 var dom = this.dom(),
199 args = Array.prototype.slice.call(arguments, 0);
200 if(args.length === 2 && args[1] === undefined)
201 return dom.removeData(args[0]);
202 return dom.data.apply(dom, arguments);
204 toXML: function(level) {
205 var node = $('<' + this.getWlxmlTag() + '>');
207 if(this.getWlxmlClass())
208 node.attr('class', this.getWlxmlClass());
209 var meta = this.getWlxmlMetaAttrs();
210 meta.forEach(function(attr) {
212 node.attr('meta-' + attr.name, attr.value);
214 _.keys(this.data('other-attrs') || {}).forEach(function(key) {
215 node.attr(key, this.data('other-attrs')[key]);
218 var addFormatting = function() {
219 var toret = $('<div>');
220 var formattings = {};
222 if(this.data('orig-before') !== undefined) {
223 if(this.data('orig-before')) {
224 toret.prepend(document.createTextNode(this.data('orig-before')));
226 } else if(level && this.getWlxmlTag() !== 'span') {
227 toret.append('\n' + (new Array(level * 2 + 1)).join(' '));
232 if(this.data('orig-after')) {
233 toret.append(document.createTextNode(this.data('orig-after')));
237 if(this.data('orig-begin')) {
238 node.prepend(this.data('orig-begin'));
239 formattings.begin = true;
242 if(this.data('orig-end') !== undefined) {
243 if(this.data('orig-end')) {
244 node.append(this.data('orig-end'));
246 } else if(this.getWlxmlTag() !== 'span' && children.length){
247 node.append('\n' + (new Array(level * 2 + 1)).join(' '));
250 return {parts: toret.contents(), formattings: formattings};
255 var children = this.children(),
258 var formatting = addFormatting(node);
260 for(var i = children.length - 1; i >= 0; i--) {
261 childParts = children[i].toXML(level + 1);
262 if(typeof childParts === 'string')
263 childParts = [document.createTextNode(childParts)];
265 if(formatting.formattings.begin) {
266 $(node.contents()[0]).after(childParts);
268 node.prepend(childParts);
270 return formatting.parts;
272 append: function(params) {
273 if(params.tag !== 'span')
274 this.data('orig-end', undefined);
275 return manipulate(this, params, 'append');
277 prepend: function(params) {
278 return manipulate(this, params, 'prepend');
280 before: function(params) {
281 return manipulate(this, params, 'before');
284 after: function(params) {
285 return manipulate(this, params, 'after');
287 children: function() {
289 if(this instanceof DocumentTextElement)
293 var elementContent = this._container().contents();
295 elementContent.each(function(idx) {
296 var childElement = DocumentElement.fromHTMLElement(this, element.canvas);
297 if(childElement === undefined)
299 if(idx === 0 && elementContent.length > 1 && elementContent[1].nodeType === Node.ELEMENT_NODE && (childElement instanceof DocumentTextElement) && $.trim($(this).text()) === '')
301 if(idx > 0 && childElement instanceof DocumentTextElement) {
302 if(toret[toret.length-1] instanceof DocumentNodeElement && $.trim($(this).text()) === '')
305 toret.push(childElement);
309 childIndex: function(child) {
310 var children = this.children(),
312 children.forEach(function(c, idx) {
313 if(c.sameNode(child)) {
320 getWlxmlTag: function() {
321 return this._container().attr('wlxml-tag');
323 setWlxmlTag: function(tag) {
324 if(tag === this.getWlxmlTag())
327 this._container().attr('wlxml-tag', tag);
328 if(!this.__updatingWlxml)
329 this._updateWlxmlManager();
331 getWlxmlClass: function() {
332 var klass = this._container().attr('wlxml-class');
334 return klass.replace(/-/g, '.');
337 setWlxmlClass: function(klass) {
338 if(klass === this.getWlxmlClass())
341 this.getWlxmlMetaAttrs().forEach(function(attr) {
342 if(!classAttributes.hasMetaAttr(klass, attr.name))
343 this.dom().removeAttr('wlxml-meta-' + attr.name);
347 this._container().attr('wlxml-class', klass.replace(/\./g, '-'));
349 this._container().removeAttr('wlxml-class');
350 if(!this.__updatingWlxml)
351 this._updateWlxmlManager();
353 setWlxml: function(params) {
354 this.__updatingWlxml = true;
355 if(params.tag !== undefined)
356 this.setWlxmlTag(params.tag);
357 if(params.klass !== undefined)
358 this.setWlxmlClass(params.klass);
359 this._updateWlxmlManager();
360 this.__updatingWlxml = false;
362 _updateWlxmlManager: function() {
363 var manager = wlxmlManagers.getFor(this);
364 this.data('_wlxmlManager', manager);
368 if(what === 'list' && _.contains(['list.items', 'list.items.enum'], this.getWlxmlClass()))
373 getWlxmlMetaAttr: function(attr) {
374 return this.dom().attr('wlxml-meta-'+attr);
377 getWlxmlMetaAttrs: function() {
379 var attrList = classAttributes.getMetaAttrsList(this.getWlxmlClass());
380 attrList.all.forEach(function(attr) {
381 toret.push({name: attr.name, value: this.getWlxmlMetaAttr(attr.name) || ''});
386 setWlxmlMetaAttr: function(attr, value) {
387 this.dom().attr('wlxml-meta-'+attr, value);
390 toggleLabel: function(toggle) {
391 var displayCss = toggle ? 'inline-block' : 'none';
392 var label = this.dom().children('.canvas-widgets').find('.canvas-widget-label');
393 label.css('display', displayCss);
394 this.toggleHighlight(toggle);
397 toggleHighlight: function(toggle) {
398 this._container().toggleClass('highlighted-element', toggle);
401 toggle: function(toggle) {
402 var mng = this.data('_wlxmlManager');
410 // DocumentNodeElement represents a text node from WLXML document rendered inside Canvas
411 var DocumentTextElement = function(htmlElement, canvas) {
412 DocumentElement.call(this, htmlElement, canvas);
415 $.extend(DocumentTextElement, {
416 createDOM: function(params) {
418 .attr('document-text-element', '')
419 .text(params.text || utils.unicode.ZWS);
422 create: function(params, canvas) {
423 return this.fromHTMLElement(this.createDOM(params)[0]);
426 fromHTMLElement: function(htmlElement, canvas) {
427 return new this(htmlElement, canvas);
429 isContentContainer: function(htmlElement) {
430 return htmlElement.nodeType === Node.TEXT_NODE && $(htmlElement).parent().is('[document-text-element]');
434 DocumentTextElement.prototype = new DocumentElement();
436 $.extend(DocumentTextElement.prototype, {
437 toXML: function(parent) {
438 return this.getText();
440 _setupDOMHandler: function(htmlElement) {
441 var $element = $(htmlElement);
442 if(htmlElement.nodeType === Node.TEXT_NODE)
443 this.$element = $element.parent();
445 this.$element = $element;
452 setText: function(text) {
453 this.dom().contents()[0].data = text;
455 appendText: function(text) {
456 this.dom().contents()[0].data += text;
458 getText: function() {
459 return this.dom().text().replace(utils.unicode.ZWS, '');
461 isEmpty: function() {
462 // Having at least Zero Width Space is guaranteed be Content Observer
463 return this.dom().contents()[0].data === utils.unicode.ZWS;
465 after: function(params) {
466 if(params instanceof DocumentTextElement || params.text)
469 if(params instanceof DocumentNodeElement) {
472 element = DocumentNodeElement.create(params, this.canvas);
474 this.dom().wrap('<div>');
475 this.dom().parent().after(element.dom());
479 before: function(params) {
480 if(params instanceof DocumentTextElement || params.text)
483 if(params instanceof DocumentNodeElement) {
486 element = DocumentNodeElement.create(params, this.canvas);
488 this.dom().wrap('<div>');
489 this.dom().parent().before(element.dom());
493 wrapWithNodeElement: function(wlxmlNode) {
494 if(typeof wlxmlNode.start === 'number' && typeof wlxmlNode.end === 'number') {
495 return this.canvas.wrapText({
496 inside: this.parent(),
497 textNodeIdx: this.parent().childIndex(this),
498 offsetStart: Math.min(wlxmlNode.start, wlxmlNode.end),
499 offsetEnd: Math.max(wlxmlNode.start, wlxmlNode.end),
500 _with: {tag: wlxmlNode.tag, klass: wlxmlNode.klass}
503 return DocumentElement.prototype.wrapWithNodeElement.call(this, wlxmlNode);
507 var parent = this.parent(),
509 if(parent.children().length === 1) {
510 toret = parent.parent();
511 var grandParent = parent.parent();
513 var grandParentChildren = grandParent.children(),
514 idx = grandParent.childIndex(parent),
515 prev = idx - 1 > -1 ? grandParentChildren[idx-1] : null,
516 next = idx + 1 < grandParentChildren.length ? grandParentChildren[idx+1] : null;
518 prev = (prev instanceof DocumentTextElement) ? prev : null;
519 next = (next instanceof DocumentTextElement) ? next : null;
522 prev.setText(prev.getText() + this.getText() + next.getText());
524 } else if (prev || next) {
525 var target = prev ? prev : next;
526 target.setText(target.getText() + this.getText());
537 split: function(params) {
538 var parentElement = this.parent(),
539 myIdx = parentElement.childIndex(this),
540 myCanvas = this.canvas,
542 succeedingChildren = [],
544 prefix = this.getText().substr(0, params.offset),
545 suffix = this.getText().substr(params.offset);
547 parentElement.children().forEach(function(child) {
549 succeedingChildren.push(child);
550 if(child.sameNode(thisElement))
554 if(prefix.length > 0)
555 this.setText(prefix);
559 var newElement = DocumentNodeElement.create({tag: parentElement.getWlxmlTag(), klass: parentElement.getWlxmlClass()}, myCanvas);
560 parentElement.after(newElement);
562 if(suffix.length > 0)
563 newElement.append({text: suffix});
564 succeedingChildren.forEach(function(child) {
565 newElement.append(child);
568 return {first: parentElement, second: newElement};
570 divide: function(params) {
571 var myText = this.getText();
573 if(params.offset === myText.length)
574 return this.after(params);
575 if(params.offset === 0)
576 return this.before(params);
578 var lhsText = myText.substr(0, params.offset),
579 rhsText = myText.substr(params.offset),
580 newElement = DocumentNodeElement.create({tag: params.tag, klass: params.klass}, this.canvas),
581 rhsTextElement = DocumentTextElement.create({text: rhsText});
583 this.setText(lhsText);
584 this.after(newElement);
585 newElement.after(rhsTextElement);
589 toggleHighlight: function() {
590 // do nothing for now
595 DocumentElement: DocumentElement,
596 DocumentNodeElement: DocumentNodeElement,
597 DocumentTextElement: DocumentTextElement