linting
[fnpeditor.git] / src / editor / modules / documentCanvas / canvas / documentElement.js
1 define([
2 'libs/jquery',
3 'libs/underscore',
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) {
9     
10 'use strict';
11
12
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)
16         return;
17     this.canvas = canvas;
18     this._setupDOMHandler(htmlElement);
19 }
20
21
22 var elementTypeFromWlxmlNode = function(wlxmlNode) {
23     return wlxmlNode.nodeType === Node.TEXT_NODE ? DocumentTextElement : DocumentNodeElement;
24 }
25
26 $.extend(DocumentElement, {
27     create: function(node, canvas) {
28         return elementTypeFromWlxmlNode(node).create(node, canvas);
29     },
30
31     createDOM: function(wlxmlNode) {
32         return elementTypeFromParams(wlxmlNode).createDOM(params);
33     },
34
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);
41         return undefined;
42     }
43 });
44
45 $.extend(DocumentElement.prototype, {
46     _setupDOMHandler: function(htmlElement) {
47         this.$element = $(htmlElement);
48     },
49     bound: function() {
50         return $.contains(document.documentElement, this.dom()[0]);
51     },
52     dom: function() {
53         return this.$element;
54     },
55     data: function() {
56         var dom = this.dom(),
57             args = Array.prototype.slice.call(arguments, 0);
58         if(args.length === 2 && args[1] === undefined)
59             return dom.removeData(args[0]);
60         return dom.data.apply(dom, arguments);
61     },
62     parent: function() {
63         var parents = this.$element.parents('[document-node-element]');
64         if(parents.length)
65             return DocumentElement.fromHTMLElement(parents[0], this.canvas);
66         return null;
67     },
68
69     parents: function() {
70         var parents = [],
71             parent = this.parent();
72         while(parent) {
73             parents.push(parent);
74             parent = parent.parent();
75         }
76         return parents;
77     },
78
79     sameNode: function(other) {
80         return other && (typeof other === typeof this) && other.dom()[0] === this.dom()[0];
81     },
82
83     wrapWithNodeElement: function(wlxmlNode) {
84         var wrapper = DocumentNodeElement.create({tag: wlxmlNode.tag, klass: wlxmlNode.klass}, this);
85         this.dom().replaceWith(wrapper.dom());
86         wrapper.append(this);
87         return wrapper;
88     },
89
90     markAsCurrent: function() {
91         this.canvas.markAsCurrent(this);
92     },
93
94     getVerticallyFirstTextElement: function() {
95         var toret;
96         this.children().some(function(child) {
97             if(!child.isVisible())
98                 return false; // continue
99             if(child instanceof DocumentTextElement) {
100                 toret = child;
101                 return true; // break
102             } else {
103                 toret = child.getVerticallyFirstTextElement();
104                 if(toret)
105                     return true; // break
106             }
107         });
108         return toret;
109     },
110
111     getPreviousTextElement: function(includeInvisible) {
112         return this.getNearestTextElement('above', includeInvisible);
113     },
114
115     getNextTextElement: function(includeInvisible) {
116         return this.getNearestTextElement('below', includeInvisible);
117     },
118
119     getNearestTextElement: function(direction, includeInvisible) {
120         includeInvisible = includeInvisible !== undefined ? includeInvisible : false;
121         var selector = '[document-text-element]' + (includeInvisible ? '' : ':visible');
122         return this.canvas.getDocumentElement(utils.nearestInDocumentOrder(selector, direction, this.dom()[0]));
123     },
124
125     isVisible: function() {
126         return this instanceof DocumentTextElement || this.getWlxmlTag() !== 'metadata';
127     },
128
129     isInsideList: function() {
130         return this.parents().some(function(parent) {
131             return parent.is('list');
132         });
133     },
134
135     exec: function(method) {
136         var manager = this.data('_wlxmlManager');
137         if(manager[method])
138             return manager[method].apply(manager, Array.prototype.slice.call(arguments, 1));
139     }
140 });
141
142
143 // DocumentNodeElement represents an element node from WLXML document rendered inside Canvas
144 var DocumentNodeElement = function(htmlElement, canvas) {
145     DocumentElement.call(this, htmlElement, canvas);
146 };
147
148 $.extend(DocumentNodeElement, {
149     create: function(wlxmlNode, canvas) {
150         return this.fromHTMLElement(this.createDOM(wlxmlNode, canvas)[0], canvas);
151     },
152
153     fromHTMLElement: function(htmlElement, canvas) {
154         return new this(htmlElement, canvas);
155     },
156
157     createDOM: function(wlxmlNode, canvas) {
158         var dom = $('<div>')
159                 .attr('document-node-element', ''),
160             widgetsContainer = $('<div>')
161                 .addClass('canvas-widgets')
162                 .attr('contenteditable', false),
163             container = $('<div>')
164                 .attr('document-element-content', '');
165         
166         dom.append(widgetsContainer, container);
167         // Make sure widgets aren't navigable with arrow keys
168         widgetsContainer.find('*').add(widgetsContainer).attr('tabindex', -1);
169
170         var element = this.fromHTMLElement(dom[0], canvas);
171
172         element.data('wlxmlNode', wlxmlNode);
173         wlxmlNode.setData('canvasElement', element);
174
175         element.setWlxml({tag: wlxmlNode.getTagName(), klass: wlxmlNode.getClass()});
176
177         _.keys(wlxmlNode.getMetaAttributes()).forEach(function(key) {
178             element.setWlxmlMetaAttr(key, params.meta[key]);
179         });
180
181         wlxmlNode.contents().forEach((function(node) {
182             container.append(DocumentElement.create(node).dom());
183         }).bind(this));
184
185         return dom;
186     }
187
188 });
189
190 var manipulate = function(e, params, action) {
191     var element;
192     if(params instanceof DocumentElement) {
193         element = params;
194     } else {
195         element = DocumentElement.create(params);
196     }
197     var target = (action === 'append' || action === 'prepend') ? e._container() : e.dom();
198     target[action](element.dom());
199     return element;
200 };
201
202 DocumentNodeElement.prototype = new DocumentElement();
203
204
205 $.extend(DocumentNodeElement.prototype, {
206     _container: function() {
207         return this.dom().children('[document-element-content]');
208     },
209     detach: function() {
210         var parent = this.parent();
211         if(!parent)
212             return;
213
214         var parentChildren = parent.children(),
215             myIdx = parent.childIndex(this);
216
217         if(myIdx > 0 && myIdx < parentChildren.length) {
218             if((parentChildren[myIdx-1] instanceof DocumentTextElement) && (parentChildren[myIdx+1] instanceof DocumentTextElement)) {
219                 parentChildren[myIdx-1].appendText(parentChildren[myIdx+1].getText());
220                 parentChildren[myIdx+1].detach();
221             }
222         }
223         this.dom().detach();
224         this.canvas = null;
225         return this;
226     },
227     unwrapContents: function() {
228         var parent = this.parent();
229         if(!parent)
230             return;
231
232         var parentChildren = parent.children(),
233             myChildren = this.children(),
234             myIdx = parent.childIndex(this);
235
236         if(myChildren.length === 0)
237             return this.detach();
238
239         var moveLeftRange, moveRightRange, leftMerged;
240
241         if(myIdx > 0 && (parentChildren[myIdx-1] instanceof DocumentTextElement) && (myChildren[0] instanceof DocumentTextElement)) {
242             parentChildren[myIdx-1].appendText(myChildren[0].getText());
243             myChildren[0].detach();
244             moveLeftRange = true;
245             leftMerged = true;
246         } else {
247             leftMerged = false;
248         }
249
250         if(!(leftMerged && myChildren.length === 1)) {
251             if(myIdx < parentChildren.length - 1 && (parentChildren[myIdx+1] instanceof DocumentTextElement) && (myChildren[myChildren.length-1] instanceof DocumentTextElement)) {
252                 parentChildren[myIdx+1].prependText(myChildren[myChildren.length-1].getText());
253                 myChildren[myChildren.length-1].detach();
254                 moveRightRange = true;
255             }
256         }
257
258         var childrenLength = this.children().length;
259         this.children().forEach(function(child) {
260             this.before(child);
261         }.bind(this));
262
263         this.detach();
264
265         return {
266             element1: parent.children()[myIdx + (moveLeftRange ? -1 : 0)],
267             element2: parent.children()[myIdx + childrenLength-1 + (moveRightRange ? 1 : 0)]
268         };
269     },
270     toXML: function(level) {
271         var node = $('<' + this.getWlxmlTag() + '>');
272
273         if(this.getWlxmlClass())
274             node.attr('class', this.getWlxmlClass());
275         var meta = this.getWlxmlMetaAttrs();
276         meta.forEach(function(attr) {
277             if(attr.value)
278                 node.attr('meta-' + attr.name, attr.value);
279         });
280         _.keys(this.data('other-attrs') || {}).forEach(function(key) {
281             node.attr(key, this.data('other-attrs')[key]);
282         }, this);
283
284         var addFormatting = function() {
285             var toret = $('<div>');
286             var formattings = {};
287
288             if(this.data('orig-before') !== undefined) {
289                 if(this.data('orig-before')) {
290                     toret.prepend(document.createTextNode(this.data('orig-before')));
291                 }
292             } else if(level && this.getWlxmlTag() !== 'span') {
293                 toret.append('\n' + (new Array(level * 2 + 1)).join(' '));
294             }
295
296             toret.append(node);
297
298             if(this.data('orig-after')) {
299                 toret.append(document.createTextNode(this.data('orig-after')));
300             }
301
302             /* Inside node */
303             if(this.data('orig-begin')) {
304                 node.prepend(this.data('orig-begin'));
305                 formattings.begin = true;
306             }
307
308             if(this.data('orig-end') !== undefined) {
309                 if(this.data('orig-end')) {
310                     node.append(this.data('orig-end'));
311                 }
312             } else if(this.getWlxmlTag() !== 'span' && children.length){
313                 node.append('\n' + (new Array(level * 2 + 1)).join(' '));
314             }
315            
316             return {parts: toret.contents(), formattings: formattings};
317         }.bind(this);
318
319         
320         
321         var children = this.children(),
322             childParts;
323
324         var formatting = addFormatting(node);
325
326         for(var i = children.length - 1; i >= 0; i--) {
327             childParts = children[i].toXML(level + 1);
328             if(typeof childParts === 'string')
329                 childParts = [document.createTextNode(childParts)];
330
331             if(formatting.formattings.begin) {
332                 $(node.contents()[0]).after(childParts);
333             } else
334                 node.prepend(childParts);
335         }
336         return formatting.parts;
337     },
338     append: function(params) {
339         if(params.tag !== 'span')
340             this.data('orig-end', undefined);
341         return manipulate(this, params, 'append');
342     },
343     prepend: function(params) {
344         return manipulate(this, params, 'prepend');
345     },
346     before: function(params) {
347         return manipulate(this, params, 'before');
348
349     },
350     after: function(params) {
351         return manipulate(this, params, 'after');
352     },
353     children: function() {
354         var toret = [];
355         if(this instanceof DocumentTextElement)
356             return toret;
357
358
359         var elementContent = this._container().contents();
360         var element = this;
361         elementContent.each(function(idx) {
362             var childElement = DocumentElement.fromHTMLElement(this, element.canvas);
363             if(childElement === undefined)
364                 return true;
365             if(idx === 0 && elementContent.length > 1 && elementContent[1].nodeType === Node.ELEMENT_NODE && (childElement instanceof DocumentTextElement) && $.trim($(this).text()) === '')
366                 return true;
367             if(idx > 0 && childElement instanceof DocumentTextElement) {
368                 if(toret[toret.length-1] instanceof DocumentNodeElement && $.trim($(this).text()) === '')
369                     return true;
370             }
371             toret.push(childElement);
372         });
373         return toret;
374     },
375     childIndex: function(child) {
376         var children = this.children(),
377             toret = null;
378         children.forEach(function(c, idx) {
379             if(c.sameNode(child)) {
380                 toret = idx;
381                 return false;
382             }
383         });
384         return toret;
385     },
386     getWlxmlTag: function() {
387         return this._container().attr('wlxml-tag');
388     },
389     setWlxmlTag: function(tag) {
390         if(tag === this.getWlxmlTag())
391             return;
392
393         this._container().attr('wlxml-tag', tag);
394         if(!this.__updatingWlxml)
395             this._updateWlxmlManager();
396     },
397     getWlxmlClass: function() {
398         var klass = this._container().attr('wlxml-class');
399         if(klass)
400             return klass.replace(/-/g, '.');
401         return undefined;
402     },
403     setWlxmlClass: function(klass) {
404         if(klass === this.getWlxmlClass())
405             return;
406
407         this.getWlxmlMetaAttrs().forEach(function(attr) {
408             if(!classAttributes.hasMetaAttr(klass, attr.name))
409                 this.dom().removeAttr('wlxml-meta-' + attr.name);
410         }, this);
411
412         if(klass)
413             this._container().attr('wlxml-class', klass.replace(/\./g, '-'));
414         else
415             this._container().removeAttr('wlxml-class');
416         if(!this.__updatingWlxml)
417             this._updateWlxmlManager();
418     },
419     setWlxml: function(params) {
420         this.__updatingWlxml = true;
421         if(params.tag !== undefined)
422             this.setWlxmlTag(params.tag);
423         if(params.klass !== undefined)
424             this.setWlxmlClass(params.klass);
425         this._updateWlxmlManager();
426         this.__updatingWlxml = false;
427     },
428     _updateWlxmlManager: function() {
429         var manager = wlxmlManagers.getFor(this);
430         this.data('_wlxmlManager', manager);
431         manager.setup();
432     },
433     is: function(what) {
434         if(what === 'list' && _.contains(['list.items', 'list.items.enum'], this.getWlxmlClass()))
435             return true;
436         return false;
437     },
438
439     getWlxmlMetaAttr: function(attr) {
440         return this.dom().attr('wlxml-meta-'+attr);
441     },
442
443     getWlxmlMetaAttrs: function() {
444         var toret = [];
445         var attrList = classAttributes.getMetaAttrsList(this.getWlxmlClass());
446         attrList.all.forEach(function(attr) {
447             toret.push({name: attr.name, value: this.getWlxmlMetaAttr(attr.name) || ''});
448         }, this);
449         return toret;
450     },
451
452     setWlxmlMetaAttr: function(attr, value) {
453         this.dom().attr('wlxml-meta-'+attr, value);
454     },
455     
456     toggleLabel: function(toggle) {
457         var displayCss = toggle ? 'inline-block' : 'none';
458         var label = this.dom().children('.canvas-widgets').find('.canvas-widget-label');
459         label.css('display', displayCss);
460         this.toggleHighlight(toggle);
461     },
462
463     toggleHighlight: function(toggle) {
464         this._container().toggleClass('highlighted-element', toggle);
465     },
466
467     toggle: function(toggle) {
468         var mng = this.data('_wlxmlManager');
469         if(mng) {
470             mng.toggle(toggle);
471         }
472     }
473 });
474
475
476 // DocumentNodeElement represents a text node from WLXML document rendered inside Canvas
477 var DocumentTextElement = function(htmlElement, canvas) {
478     DocumentElement.call(this, htmlElement, canvas);
479 };
480
481 $.extend(DocumentTextElement, {
482     createDOM: function(wlxmlTextNode) {
483         var dom = $('<div>')
484             .attr('document-text-element', '')
485             .text(wlxmlTextNode.getText() || utils.unicode.ZWS),
486         element = this.fromHTMLElement(dom[0], this);
487         element.data('wlxmlNode', wlxmlTextNode);
488         return dom;
489     },
490
491     create: function(wlxmlTextNode, canvas) {
492         return this.fromHTMLElement(this.createDOM(wlxmlTextNode)[0]);
493     },
494
495     fromHTMLElement: function(htmlElement, canvas) {
496         return new this(htmlElement, canvas);
497     },
498     isContentContainer: function(htmlElement) {
499         return htmlElement.nodeType === Node.TEXT_NODE && $(htmlElement).parent().is('[document-text-element]');
500     }
501 });
502
503 DocumentTextElement.prototype = new DocumentElement();
504
505 $.extend(DocumentTextElement.prototype, {
506     toXML: function(parent) {
507         return this.getText();
508     },
509     _setupDOMHandler: function(htmlElement) {
510         var $element = $(htmlElement);
511         if(htmlElement.nodeType === Node.TEXT_NODE)
512             this.$element = $element.parent();
513         else
514             this.$element = $element;
515     },
516     detach: function() {
517         this.dom().detach();
518         this.canvas = null;
519         return this;
520     },
521     setText: function(text) {
522         this.dom().contents()[0].data = text;
523     },
524     appendText: function(text) {
525         this.dom().contents()[0].data += text;
526     },
527     prependText: function(text) {
528         this.dom().contents()[0].data = text + this.dom().contents()[0].data;
529     },
530     getText: function(options) {
531         options = _.extend({raw: false}, options || {});
532         var toret = this.dom().text();
533         if(!options.raw) {
534             toret = toret.replace(utils.unicode.ZWS, '');
535         }
536         return toret;
537     },
538     isEmpty: function() {
539         // Having at least Zero Width Space is guaranteed be Content Observer
540         return this.dom().contents()[0].data === utils.unicode.ZWS;
541     },
542     after: function(params) {
543         if(params instanceof DocumentTextElement || params.text)
544             return false;
545         var element;
546         if(params instanceof DocumentNodeElement) {
547             element = params;
548         } else {
549             element = DocumentNodeElement.create(params, this.canvas);
550         }
551         this.dom().wrap('<div>');
552         this.dom().parent().after(element.dom());
553         this.dom().unwrap();
554         return element;
555     },
556     before: function(params) {
557         if(params instanceof DocumentTextElement || params.text)
558             return false;
559         var element;
560         if(params instanceof DocumentNodeElement) {
561             element = params;
562         } else {
563             element = DocumentNodeElement.create(params, this.canvas);
564         }
565         this.dom().wrap('<div>');
566         this.dom().parent().before(element.dom());
567         this.dom().unwrap();
568         return element;
569     },
570     wrapWithNodeElement: function(wlxmlNode) {
571         if(typeof wlxmlNode.start === 'number' && typeof wlxmlNode.end === 'number') {
572             return this.canvas.wrapText({
573                 inside: this.parent(),
574                 textNodeIdx: this.parent().childIndex(this),
575                 offsetStart: Math.min(wlxmlNode.start, wlxmlNode.end),
576                 offsetEnd: Math.max(wlxmlNode.start, wlxmlNode.end),
577                 _with: {tag: wlxmlNode.tag, klass: wlxmlNode.klass}
578             });
579         } else {
580             return DocumentElement.prototype.wrapWithNodeElement.call(this, wlxmlNode);
581         }
582     },
583     unwrap: function() {
584         var parent = this.parent(),
585             toret;
586         if(parent.children().length === 1) {
587             toret = parent.parent();
588             var grandParent = parent.parent();
589             if(grandParent) {
590                 var grandParentChildren = grandParent.children(),
591                     idx = grandParent.childIndex(parent),
592                     prev = idx - 1 > -1 ? grandParentChildren[idx-1] : null,
593                     next = idx + 1 < grandParentChildren.length ? grandParentChildren[idx+1] : null;
594                 
595                 prev = (prev instanceof DocumentTextElement) ? prev : null;
596                 next = (next instanceof DocumentTextElement) ? next : null;
597
598                 if(prev && next) {
599                     prev.setText(prev.getText() + this.getText() + next.getText());
600                     next.detach();
601                 } else if (prev || next) {
602                     var target = prev ? prev : next,
603                         newText = prev ? target.getText() + this.getText() : this.getText() + target.getText();
604                     target.setText(newText);
605                 } else {
606                     parent.after(this);
607                 }
608             } else {
609                 parent.after(this);
610             }
611             parent.detach();
612             return toret;
613         }
614     },
615     split: function(params) {
616         var parentElement = this.parent(),
617             myIdx = parentElement.childIndex(this),
618             myCanvas = this.canvas,
619             passed = false,
620             succeedingChildren = [],
621             thisElement = this,
622             prefix = this.getText().substr(0, params.offset),
623             suffix = this.getText().substr(params.offset);
624
625         parentElement.children().forEach(function(child) {
626             if(passed)
627                 succeedingChildren.push(child);
628             if(child.sameNode(thisElement))
629                 passed = true;
630         });
631
632         if(prefix.length > 0)
633             this.setText(prefix);
634         else
635             this.detach();
636         
637         var newElement = DocumentNodeElement.create({tag: parentElement.getWlxmlTag(), klass: parentElement.getWlxmlClass()}, myCanvas);
638         parentElement.after(newElement);
639
640         if(suffix.length > 0)
641             newElement.append({text: suffix});
642         succeedingChildren.forEach(function(child) {
643             newElement.append(child);
644         });
645
646         return {first: parentElement, second: newElement};
647     },
648     divide: function(params) {
649         var myText = this.getText();
650
651         if(params.offset === myText.length)
652             return this.after(params);
653         if(params.offset === 0)
654             return this.before(params);
655
656         var lhsText = myText.substr(0, params.offset),
657             rhsText = myText.substr(params.offset),
658             newElement = DocumentNodeElement.create({tag: params.tag, klass: params.klass}, this.canvas),
659             rhsTextElement = DocumentTextElement.create({text: rhsText});
660
661         this.setText(lhsText);
662         this.after(newElement);
663         newElement.after(rhsTextElement);
664         return newElement;
665     },
666
667     toggleHighlight: function() {
668         // do nothing for now
669     }
670 });
671
672 return {
673     DocumentElement: DocumentElement,
674     DocumentNodeElement: DocumentNodeElement,
675     DocumentTextElement: DocumentTextElement
676 };
677
678 });