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});
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';
110 // DocumentNodeElement represents an element node from WLXML document rendered inside Canvas
111 var DocumentNodeElement = function(htmlElement, canvas) {
112 DocumentElement.call(this, htmlElement, canvas);
115 $.extend(DocumentNodeElement, {
116 createDOM: function(params, canvas) {
118 .attr('document-node-element', ''),
119 widgetsContainer = $('<div>')
120 .addClass('canvas-widgets')
121 .attr('contenteditable', false),
122 container = $('<div>')
123 .attr('document-element-content', '');
125 dom.append(widgetsContainer, container);
126 // Make sure widgets aren't navigable with arrow keys
127 widgetsContainer.find('*').add(widgetsContainer).attr('tabindex', -1);
129 var element = this.fromHTMLElement(dom[0], canvas);
131 element.setWlxmlTag(params.tag);
133 element.setWlxmlClass(params.klass);
135 _.keys(params.meta).forEach(function(key) {
136 element.setWlxmlMetaAttr(key, params.meta[key]);
139 element.data('other-attrs', params.others);
141 if(params.rawChildren) {
142 container.append(params.rawChildren);
147 create: function(params, canvas) {
148 return this.fromHTMLElement(this.createDOM(params, canvas)[0], canvas);
151 fromHTMLElement: function(htmlElement, canvas) {
152 return new this(htmlElement, canvas);
156 var manipulate = function(e, params, action) {
158 if(params instanceof DocumentElement) {
161 element = DocumentElement.create(params);
163 var target = (action === 'append' || action === 'prepend') ? e._container() : e.dom();
164 target[action](element.dom());
168 DocumentNodeElement.prototype = new DocumentElement();
171 $.extend(DocumentNodeElement.prototype, {
172 _container: function() {
173 return this.dom().children('[document-element-content]');
176 var parent = this.parent();
180 var parentChildren = parent.children(),
181 myIdx = parent.childIndex(this);
183 if(myIdx > 0 && myIdx < parentChildren.length) {
184 if((parentChildren[myIdx-1] instanceof DocumentTextElement) && (parentChildren[myIdx+1] instanceof DocumentTextElement)) {
185 parentChildren[myIdx-1].appendText(parentChildren[myIdx+1].getText());
186 parentChildren[myIdx+1].detach();
194 var dom = this.dom(),
195 args = Array.prototype.slice.call(arguments, 0);
196 if(args.length === 2 && args[1] === undefined)
197 return dom.removeData(args[0]);
198 return dom.data.apply(dom, arguments);
200 toXML: function(level) {
201 var node = $('<' + this.getWlxmlTag() + '>');
203 if(this.getWlxmlClass())
204 node.attr('class', this.getWlxmlClass());
205 var meta = this.getWlxmlMetaAttrs();
206 meta.forEach(function(attr) {
208 node.attr('meta-' + attr.name, attr.value);
210 _.keys(this.data('other-attrs') || {}).forEach(function(key) {
211 node.attr(key, this.data('other-attrs')[key]);
214 var addFormatting = function() {
215 var toret = $('<div>');
216 var formattings = {};
218 if(this.data('orig-before') !== undefined) {
219 if(this.data('orig-before')) {
220 toret.prepend(document.createTextNode(this.data('orig-before')));
222 } else if(level && this.getWlxmlTag() !== 'span') {
223 toret.append('\n' + (new Array(level * 2 + 1)).join(' '));
228 if(this.data('orig-after')) {
229 toret.append(document.createTextNode(this.data('orig-after')));
233 if(this.data('orig-begin')) {
234 node.prepend(this.data('orig-begin'));
235 formattings.begin = true;
238 if(this.data('orig-end') !== undefined) {
239 if(this.data('orig-end')) {
240 node.append(this.data('orig-end'));
242 } else if(this.getWlxmlTag() !== 'span' && children.length){
243 node.append('\n' + (new Array(level * 2 + 1)).join(' '));
246 return {parts: toret.contents(), formattings: formattings};
251 var children = this.children(),
254 var formatting = addFormatting(node);
256 for(var i = children.length - 1; i >= 0; i--) {
257 childParts = children[i].toXML(level + 1);
258 if(typeof childParts === 'string')
259 childParts = [document.createTextNode(childParts)];
261 if(formatting.formattings.begin) {
262 $(node.contents()[0]).after(childParts);
264 node.prepend(childParts);
266 return formatting.parts;
268 append: function(params) {
269 if(params.tag !== 'span')
270 this.data('orig-end', undefined);
271 return manipulate(this, params, 'append');
273 prepend: function(params) {
274 return manipulate(this, params, 'prepend');
276 before: function(params) {
277 return manipulate(this, params, 'before');
280 after: function(params) {
281 return manipulate(this, params, 'after');
283 children: function() {
285 if(this instanceof DocumentTextElement)
289 var elementContent = this._container().contents();
291 elementContent.each(function(idx) {
292 var childElement = DocumentElement.fromHTMLElement(this, element.canvas);
293 if(childElement === undefined)
295 if(idx === 0 && elementContent.length > 1 && elementContent[1].nodeType === Node.ELEMENT_NODE && (childElement instanceof DocumentTextElement) && $.trim($(this).text()) === '')
297 if(idx > 0 && childElement instanceof DocumentTextElement) {
298 if(toret[toret.length-1] instanceof DocumentNodeElement && $.trim($(this).text()) === '')
301 toret.push(childElement);
305 childIndex: function(child) {
306 var children = this.children(),
308 children.forEach(function(c, idx) {
309 if(c.sameNode(child)) {
316 getWlxmlTag: function() {
317 return this._container().attr('wlxml-tag');
319 setWlxmlTag: function(tag) {
320 if(tag === this.getWlxmlTag())
323 this._container().attr('wlxml-tag', tag);
324 this._updateWlxmlManager();
326 getWlxmlClass: function() {
327 var klass = this._container().attr('wlxml-class');
329 return klass.replace(/-/g, '.');
332 setWlxmlClass: function(klass) {
333 if(klass === this.getWlxmlClass())
336 this.getWlxmlMetaAttrs().forEach(function(attr) {
337 if(!classAttributes.hasMetaAttr(klass, attr.name))
338 this.dom().removeAttr('wlxml-meta-' + attr.name);
342 this._container().attr('wlxml-class', klass.replace(/\./g, '-'));
344 this._container().removeAttr('wlxml-class');
345 this._updateWlxmlManager();
347 _updateWlxmlManager: function() {
348 var manager = wlxmlManagers.getFor(this);
349 this.data('_wlxmlManager', manager);
353 if(what === 'list' && _.contains(['list.items', 'list.items.enum'], this.getWlxmlClass()))
358 getWlxmlMetaAttr: function(attr) {
359 return this.dom().attr('wlxml-meta-'+attr);
362 getWlxmlMetaAttrs: function() {
364 var attrList = classAttributes.getMetaAttrsList(this.getWlxmlClass());
365 attrList.all.forEach(function(attr) {
366 toret.push({name: attr.name, value: this.getWlxmlMetaAttr(attr.name) || ''});
371 setWlxmlMetaAttr: function(attr, value) {
372 this.dom().attr('wlxml-meta-'+attr, value);
375 toggleLabel: function(toggle) {
376 var displayCss = toggle ? 'inline-block' : 'none';
377 var label = this.dom().children('.canvas-widgets').find('.canvas-widget-label');
378 label.css('display', displayCss);
379 this.toggleHighlight(toggle);
382 toggleHighlight: function(toggle) {
383 this._container().toggleClass('highlighted-element', toggle);
386 toggle: function(toggle) {
387 var mng = this.data('_wlxmlManager');
395 // DocumentNodeElement represents a text node from WLXML document rendered inside Canvas
396 var DocumentTextElement = function(htmlElement, canvas) {
397 DocumentElement.call(this, htmlElement, canvas);
400 $.extend(DocumentTextElement, {
401 createDOM: function(params) {
403 .attr('document-text-element', '')
404 .text(params.text || utils.unicode.ZWS);
407 create: function(params, canvas) {
408 return this.fromHTMLElement(this.createDOM(params)[0]);
411 fromHTMLElement: function(htmlElement, canvas) {
412 return new this(htmlElement, canvas);
414 isContentContainer: function(htmlElement) {
415 return htmlElement.nodeType === Node.TEXT_NODE && $(htmlElement).parent().is('[document-text-element]');
419 DocumentTextElement.prototype = new DocumentElement();
421 $.extend(DocumentTextElement.prototype, {
422 toXML: function(parent) {
423 return this.getText();
425 _setupDOMHandler: function(htmlElement) {
426 var $element = $(htmlElement);
427 if(htmlElement.nodeType === Node.TEXT_NODE)
428 this.$element = $element.parent();
430 this.$element = $element;
437 setText: function(text) {
438 this.dom().contents()[0].data = text;
440 appendText: function(text) {
441 this.dom().contents()[0].data += text;
443 getText: function() {
444 return this.dom().text().replace(utils.unicode.ZWS, '');
446 isEmpty: function() {
447 // Having at least Zero Width Space is guaranteed be Content Observer
448 return this.dom().contents()[0].data === utils.unicode.ZWS;
450 after: function(params) {
451 if(params instanceof DocumentTextElement || params.text)
454 if(params instanceof DocumentNodeElement) {
457 element = DocumentNodeElement.create(params, this.canvas);
459 this.dom().wrap('<div>');
460 this.dom().parent().after(element.dom());
464 before: function(params) {
465 if(params instanceof DocumentTextElement || params.text)
468 if(params instanceof DocumentNodeElement) {
471 element = DocumentNodeElement.create(params, this.canvas);
473 this.dom().wrap('<div>');
474 this.dom().parent().before(element.dom());
478 wrapWithNodeElement: function(wlxmlNode) {
479 if(typeof wlxmlNode.start === 'number' && typeof wlxmlNode.end === 'number') {
480 return this.canvas.wrapText({
481 inside: this.parent(),
482 textNodeIdx: this.parent().childIndex(this),
483 offsetStart: Math.min(wlxmlNode.start, wlxmlNode.end),
484 offsetEnd: Math.max(wlxmlNode.start, wlxmlNode.end),
485 _with: {tag: wlxmlNode.tag, klass: wlxmlNode.klass}
488 return DocumentElement.prototype.wrapWithNodeElement.call(this, wlxmlNode);
492 var parent = this.parent(),
494 if(parent.children().length === 1) {
495 toret = parent.parent();
496 var grandParent = parent.parent();
498 var grandParentChildren = grandParent.children(),
499 idx = grandParent.childIndex(parent),
500 prev = idx - 1 > -1 ? grandParentChildren[idx-1] : null,
501 next = idx + 1 < grandParentChildren.length ? grandParentChildren[idx+1] : null;
503 prev = (prev instanceof DocumentTextElement) ? prev : null;
504 next = (next instanceof DocumentTextElement) ? next : null;
507 prev.setText(prev.getText() + this.getText() + next.getText());
509 } else if (prev || next) {
510 var target = prev ? prev : next;
511 target.setText(target.getText() + this.getText());
522 split: function(params) {
523 var parentElement = this.parent(),
524 myIdx = parentElement.childIndex(this),
525 myCanvas = this.canvas,
527 succeedingChildren = [],
529 prefix = this.getText().substr(0, params.offset),
530 suffix = this.getText().substr(params.offset);
532 parentElement.children().forEach(function(child) {
534 succeedingChildren.push(child);
535 if(child.sameNode(thisElement))
539 if(prefix.length > 0)
540 this.setText(prefix);
544 var newElement = DocumentNodeElement.create({tag: parentElement.getWlxmlTag(), klass: parentElement.getWlxmlClass()}, myCanvas);
545 parentElement.after(newElement);
547 if(suffix.length > 0)
548 newElement.append({text: suffix});
549 succeedingChildren.forEach(function(child) {
550 newElement.append(child);
553 return {first: parentElement, second: newElement};
555 divide: function(params) {
556 var myText = this.getText();
558 if(params.offset === myText.length)
559 return this.after(params);
560 if(params.offset === 0)
561 return this.before(params);
563 var lhsText = myText.substr(0, params.offset),
564 rhsText = myText.substr(params.offset),
565 newElement = DocumentNodeElement.create({tag: params.tag, klass: params.klass}, this.canvas),
566 rhsTextElement = DocumentTextElement.create({text: rhsText});
568 this.setText(lhsText);
569 this.after(newElement);
570 newElement.after(rhsTextElement);
574 toggleHighlight: function() {
575 // do nothing for now
580 DocumentElement: DocumentElement,
581 DocumentNodeElement: DocumentNodeElement,
582 DocumentTextElement: DocumentTextElement