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) {
152 container.append(params.rawChildren);
157 create: function(params, canvas) {
158 return this.fromHTMLElement(this.createDOM(params, canvas)[0], canvas);
161 fromHTMLElement: function(htmlElement, canvas) {
162 return new this(htmlElement, canvas);
166 var manipulate = function(e, params, action) {
168 if(params instanceof DocumentElement) {
171 element = DocumentElement.create(params);
173 var target = (action === 'append' || action === 'prepend') ? e._container() : e.dom();
174 target[action](element.dom());
178 DocumentNodeElement.prototype = new DocumentElement();
181 $.extend(DocumentNodeElement.prototype, {
182 _container: function() {
183 return this.dom().children('[document-element-content]');
186 var parent = this.parent();
190 var parentChildren = parent.children(),
191 myIdx = parent.childIndex(this);
193 if(myIdx > 0 && myIdx < parentChildren.length) {
194 if((parentChildren[myIdx-1] instanceof DocumentTextElement) && (parentChildren[myIdx+1] instanceof DocumentTextElement)) {
195 parentChildren[myIdx-1].appendText(parentChildren[myIdx+1].getText());
196 parentChildren[myIdx+1].detach();
204 var dom = this.dom(),
205 args = Array.prototype.slice.call(arguments, 0);
206 if(args.length === 2 && args[1] === undefined)
207 return dom.removeData(args[0]);
208 return dom.data.apply(dom, arguments);
210 toXML: function(level) {
211 var node = $('<' + this.getWlxmlTag() + '>');
213 if(this.getWlxmlClass())
214 node.attr('class', this.getWlxmlClass());
215 var meta = this.getWlxmlMetaAttrs();
216 meta.forEach(function(attr) {
218 node.attr('meta-' + attr.name, attr.value);
220 _.keys(this.data('other-attrs') || {}).forEach(function(key) {
221 node.attr(key, this.data('other-attrs')[key]);
224 var addFormatting = function() {
225 var toret = $('<div>');
226 var formattings = {};
228 if(this.data('orig-before') !== undefined) {
229 if(this.data('orig-before')) {
230 toret.prepend(document.createTextNode(this.data('orig-before')));
232 } else if(level && this.getWlxmlTag() !== 'span') {
233 toret.append('\n' + (new Array(level * 2 + 1)).join(' '));
238 if(this.data('orig-after')) {
239 toret.append(document.createTextNode(this.data('orig-after')));
243 if(this.data('orig-begin')) {
244 node.prepend(this.data('orig-begin'));
245 formattings.begin = true;
248 if(this.data('orig-end') !== undefined) {
249 if(this.data('orig-end')) {
250 node.append(this.data('orig-end'));
252 } else if(this.getWlxmlTag() !== 'span' && children.length){
253 node.append('\n' + (new Array(level * 2 + 1)).join(' '));
256 return {parts: toret.contents(), formattings: formattings};
261 var children = this.children(),
264 var formatting = addFormatting(node);
266 for(var i = children.length - 1; i >= 0; i--) {
267 childParts = children[i].toXML(level + 1);
268 if(typeof childParts === 'string')
269 childParts = [document.createTextNode(childParts)];
271 if(formatting.formattings.begin) {
272 $(node.contents()[0]).after(childParts);
274 node.prepend(childParts);
276 return formatting.parts;
278 append: function(params) {
279 if(params.tag !== 'span')
280 this.data('orig-end', undefined);
281 return manipulate(this, params, 'append');
283 prepend: function(params) {
284 return manipulate(this, params, 'prepend');
286 before: function(params) {
287 return manipulate(this, params, 'before');
290 after: function(params) {
291 return manipulate(this, params, 'after');
293 children: function() {
295 if(this instanceof DocumentTextElement)
299 var elementContent = this._container().contents();
301 elementContent.each(function(idx) {
302 var childElement = DocumentElement.fromHTMLElement(this, element.canvas);
303 if(childElement === undefined)
305 if(idx === 0 && elementContent.length > 1 && elementContent[1].nodeType === Node.ELEMENT_NODE && (childElement instanceof DocumentTextElement) && $.trim($(this).text()) === '')
307 if(idx > 0 && childElement instanceof DocumentTextElement) {
308 if(toret[toret.length-1] instanceof DocumentNodeElement && $.trim($(this).text()) === '')
311 toret.push(childElement);
315 childIndex: function(child) {
316 var children = this.children(),
318 children.forEach(function(c, idx) {
319 if(c.sameNode(child)) {
326 getWlxmlTag: function() {
327 return this._container().attr('wlxml-tag');
329 setWlxmlTag: function(tag) {
330 if(tag === this.getWlxmlTag())
333 this._container().attr('wlxml-tag', tag);
334 if(!this.__updatingWlxml)
335 this._updateWlxmlManager();
337 getWlxmlClass: function() {
338 var klass = this._container().attr('wlxml-class');
340 return klass.replace(/-/g, '.');
343 setWlxmlClass: function(klass) {
344 if(klass === this.getWlxmlClass())
347 this.getWlxmlMetaAttrs().forEach(function(attr) {
348 if(!classAttributes.hasMetaAttr(klass, attr.name))
349 this.dom().removeAttr('wlxml-meta-' + attr.name);
353 this._container().attr('wlxml-class', klass.replace(/\./g, '-'));
355 this._container().removeAttr('wlxml-class');
356 if(!this.__updatingWlxml)
357 this._updateWlxmlManager();
359 setWlxml: function(params) {
360 this.__updatingWlxml = true;
361 if(params.tag !== undefined)
362 this.setWlxmlTag(params.tag);
363 if(params.klass !== undefined)
364 this.setWlxmlClass(params.klass);
365 this._updateWlxmlManager();
366 this.__updatingWlxml = false;
368 _updateWlxmlManager: function() {
369 var manager = wlxmlManagers.getFor(this);
370 this.data('_wlxmlManager', manager);
374 if(what === 'list' && _.contains(['list.items', 'list.items.enum'], this.getWlxmlClass()))
379 getWlxmlMetaAttr: function(attr) {
380 return this.dom().attr('wlxml-meta-'+attr);
383 getWlxmlMetaAttrs: function() {
385 var attrList = classAttributes.getMetaAttrsList(this.getWlxmlClass());
386 attrList.all.forEach(function(attr) {
387 toret.push({name: attr.name, value: this.getWlxmlMetaAttr(attr.name) || ''});
392 setWlxmlMetaAttr: function(attr, value) {
393 this.dom().attr('wlxml-meta-'+attr, value);
396 toggleLabel: function(toggle) {
397 var displayCss = toggle ? 'inline-block' : 'none';
398 var label = this.dom().children('.canvas-widgets').find('.canvas-widget-label');
399 label.css('display', displayCss);
400 this.toggleHighlight(toggle);
403 toggleHighlight: function(toggle) {
404 this._container().toggleClass('highlighted-element', toggle);
407 toggle: function(toggle) {
408 var mng = this.data('_wlxmlManager');
416 // DocumentNodeElement represents a text node from WLXML document rendered inside Canvas
417 var DocumentTextElement = function(htmlElement, canvas) {
418 DocumentElement.call(this, htmlElement, canvas);
421 $.extend(DocumentTextElement, {
422 createDOM: function(params) {
424 .attr('document-text-element', '')
425 .text(params.text || utils.unicode.ZWS);
428 create: function(params, canvas) {
429 return this.fromHTMLElement(this.createDOM(params)[0]);
432 fromHTMLElement: function(htmlElement, canvas) {
433 return new this(htmlElement, canvas);
435 isContentContainer: function(htmlElement) {
436 return htmlElement.nodeType === Node.TEXT_NODE && $(htmlElement).parent().is('[document-text-element]');
440 DocumentTextElement.prototype = new DocumentElement();
442 $.extend(DocumentTextElement.prototype, {
443 toXML: function(parent) {
444 return this.getText();
446 _setupDOMHandler: function(htmlElement) {
447 var $element = $(htmlElement);
448 if(htmlElement.nodeType === Node.TEXT_NODE)
449 this.$element = $element.parent();
451 this.$element = $element;
458 setText: function(text) {
459 this.dom().contents()[0].data = text;
461 appendText: function(text) {
462 this.dom().contents()[0].data += text;
464 getText: function() {
465 return this.dom().text().replace(utils.unicode.ZWS, '');
467 isEmpty: function() {
468 // Having at least Zero Width Space is guaranteed be Content Observer
469 return this.dom().contents()[0].data === utils.unicode.ZWS;
471 after: function(params) {
472 if(params instanceof DocumentTextElement || params.text)
475 if(params instanceof DocumentNodeElement) {
478 element = DocumentNodeElement.create(params, this.canvas);
480 this.dom().wrap('<div>');
481 this.dom().parent().after(element.dom());
485 before: function(params) {
486 if(params instanceof DocumentTextElement || params.text)
489 if(params instanceof DocumentNodeElement) {
492 element = DocumentNodeElement.create(params, this.canvas);
494 this.dom().wrap('<div>');
495 this.dom().parent().before(element.dom());
499 wrapWithNodeElement: function(wlxmlNode) {
500 if(typeof wlxmlNode.start === 'number' && typeof wlxmlNode.end === 'number') {
501 return this.canvas.wrapText({
502 inside: this.parent(),
503 textNodeIdx: this.parent().childIndex(this),
504 offsetStart: Math.min(wlxmlNode.start, wlxmlNode.end),
505 offsetEnd: Math.max(wlxmlNode.start, wlxmlNode.end),
506 _with: {tag: wlxmlNode.tag, klass: wlxmlNode.klass}
509 return DocumentElement.prototype.wrapWithNodeElement.call(this, wlxmlNode);
513 var parent = this.parent(),
515 if(parent.children().length === 1) {
516 toret = parent.parent();
517 var grandParent = parent.parent();
519 var grandParentChildren = grandParent.children(),
520 idx = grandParent.childIndex(parent),
521 prev = idx - 1 > -1 ? grandParentChildren[idx-1] : null,
522 next = idx + 1 < grandParentChildren.length ? grandParentChildren[idx+1] : null;
524 prev = (prev instanceof DocumentTextElement) ? prev : null;
525 next = (next instanceof DocumentTextElement) ? next : null;
528 prev.setText(prev.getText() + this.getText() + next.getText());
530 } else if (prev || next) {
531 var target = prev ? prev : next,
532 newText = prev ? target.getText() + this.getText() : this.getText() + target.getText();
533 target.setText(newText);
544 split: function(params) {
545 var parentElement = this.parent(),
546 myIdx = parentElement.childIndex(this),
547 myCanvas = this.canvas,
549 succeedingChildren = [],
551 prefix = this.getText().substr(0, params.offset),
552 suffix = this.getText().substr(params.offset);
554 parentElement.children().forEach(function(child) {
556 succeedingChildren.push(child);
557 if(child.sameNode(thisElement))
561 if(prefix.length > 0)
562 this.setText(prefix);
566 var newElement = DocumentNodeElement.create({tag: parentElement.getWlxmlTag(), klass: parentElement.getWlxmlClass()}, myCanvas);
567 parentElement.after(newElement);
569 if(suffix.length > 0)
570 newElement.append({text: suffix});
571 succeedingChildren.forEach(function(child) {
572 newElement.append(child);
575 return {first: parentElement, second: newElement};
577 divide: function(params) {
578 var myText = this.getText();
580 if(params.offset === myText.length)
581 return this.after(params);
582 if(params.offset === 0)
583 return this.before(params);
585 var lhsText = myText.substr(0, params.offset),
586 rhsText = myText.substr(params.offset),
587 newElement = DocumentNodeElement.create({tag: params.tag, klass: params.klass}, this.canvas),
588 rhsTextElement = DocumentTextElement.create({text: rhsText});
590 this.setText(lhsText);
591 this.after(newElement);
592 newElement.after(rhsTextElement);
596 toggleHighlight: function() {
597 // do nothing for now
602 DocumentElement: DocumentElement,
603 DocumentNodeElement: DocumentNodeElement,
604 DocumentTextElement: DocumentTextElement