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.setWlxml({tag: params.tag, klass: params.klass});
133 _.keys(params.meta).forEach(function(key) {
134 element.setWlxmlMetaAttr(key, params.meta[key]);
137 element.data('other-attrs', params.others);
139 if(params.rawChildren) {
140 container.append(params.rawChildren);
145 create: function(params, canvas) {
146 return this.fromHTMLElement(this.createDOM(params, canvas)[0], canvas);
149 fromHTMLElement: function(htmlElement, canvas) {
150 return new this(htmlElement, canvas);
154 var manipulate = function(e, params, action) {
156 if(params instanceof DocumentElement) {
159 element = DocumentElement.create(params);
161 var target = (action === 'append' || action === 'prepend') ? e._container() : e.dom();
162 target[action](element.dom());
166 DocumentNodeElement.prototype = new DocumentElement();
169 $.extend(DocumentNodeElement.prototype, {
170 _container: function() {
171 return this.dom().children('[document-element-content]');
174 var parent = this.parent();
178 var parentChildren = parent.children(),
179 myIdx = parent.childIndex(this);
181 if(myIdx > 0 && myIdx < parentChildren.length) {
182 if((parentChildren[myIdx-1] instanceof DocumentTextElement) && (parentChildren[myIdx+1] instanceof DocumentTextElement)) {
183 parentChildren[myIdx-1].appendText(parentChildren[myIdx+1].getText());
184 parentChildren[myIdx+1].detach();
192 var dom = this.dom(),
193 args = Array.prototype.slice.call(arguments, 0);
194 if(args.length === 2 && args[1] === undefined)
195 return dom.removeData(args[0]);
196 return dom.data.apply(dom, arguments);
198 toXML: function(level) {
199 var node = $('<' + this.getWlxmlTag() + '>');
201 if(this.getWlxmlClass())
202 node.attr('class', this.getWlxmlClass());
203 var meta = this.getWlxmlMetaAttrs();
204 meta.forEach(function(attr) {
206 node.attr('meta-' + attr.name, attr.value);
208 _.keys(this.data('other-attrs') || {}).forEach(function(key) {
209 node.attr(key, this.data('other-attrs')[key]);
212 var addFormatting = function() {
213 var toret = $('<div>');
214 var formattings = {};
216 if(this.data('orig-before') !== undefined) {
217 if(this.data('orig-before')) {
218 toret.prepend(document.createTextNode(this.data('orig-before')));
220 } else if(level && this.getWlxmlTag() !== 'span') {
221 toret.append('\n' + (new Array(level * 2 + 1)).join(' '));
226 if(this.data('orig-after')) {
227 toret.append(document.createTextNode(this.data('orig-after')));
231 if(this.data('orig-begin')) {
232 node.prepend(this.data('orig-begin'));
233 formattings.begin = true;
236 if(this.data('orig-end') !== undefined) {
237 if(this.data('orig-end')) {
238 node.append(this.data('orig-end'));
240 } else if(this.getWlxmlTag() !== 'span' && children.length){
241 node.append('\n' + (new Array(level * 2 + 1)).join(' '));
244 return {parts: toret.contents(), formattings: formattings};
249 var children = this.children(),
252 var formatting = addFormatting(node);
254 for(var i = children.length - 1; i >= 0; i--) {
255 childParts = children[i].toXML(level + 1);
256 if(typeof childParts === 'string')
257 childParts = [document.createTextNode(childParts)];
259 if(formatting.formattings.begin) {
260 $(node.contents()[0]).after(childParts);
262 node.prepend(childParts);
264 return formatting.parts;
266 append: function(params) {
267 if(params.tag !== 'span')
268 this.data('orig-end', undefined);
269 return manipulate(this, params, 'append');
271 prepend: function(params) {
272 return manipulate(this, params, 'prepend');
274 before: function(params) {
275 return manipulate(this, params, 'before');
278 after: function(params) {
279 return manipulate(this, params, 'after');
281 children: function() {
283 if(this instanceof DocumentTextElement)
287 var elementContent = this._container().contents();
289 elementContent.each(function(idx) {
290 var childElement = DocumentElement.fromHTMLElement(this, element.canvas);
291 if(childElement === undefined)
293 if(idx === 0 && elementContent.length > 1 && elementContent[1].nodeType === Node.ELEMENT_NODE && (childElement instanceof DocumentTextElement) && $.trim($(this).text()) === '')
295 if(idx > 0 && childElement instanceof DocumentTextElement) {
296 if(toret[toret.length-1] instanceof DocumentNodeElement && $.trim($(this).text()) === '')
299 toret.push(childElement);
303 childIndex: function(child) {
304 var children = this.children(),
306 children.forEach(function(c, idx) {
307 if(c.sameNode(child)) {
314 getWlxmlTag: function() {
315 return this._container().attr('wlxml-tag');
317 setWlxmlTag: function(tag) {
318 if(tag === this.getWlxmlTag())
321 this._container().attr('wlxml-tag', tag);
322 if(!this.__updatingWlxml)
323 this._updateWlxmlManager();
325 getWlxmlClass: function() {
326 var klass = this._container().attr('wlxml-class');
328 return klass.replace(/-/g, '.');
331 setWlxmlClass: function(klass) {
332 if(klass === this.getWlxmlClass())
335 this.getWlxmlMetaAttrs().forEach(function(attr) {
336 if(!classAttributes.hasMetaAttr(klass, attr.name))
337 this.dom().removeAttr('wlxml-meta-' + attr.name);
341 this._container().attr('wlxml-class', klass.replace(/\./g, '-'));
343 this._container().removeAttr('wlxml-class');
344 if(!this.__updatingWlxml)
345 this._updateWlxmlManager();
347 setWlxml: function(params) {
348 this.__updatingWlxml = true;
349 if(params.tag !== undefined)
350 this.setWlxmlTag(params.tag);
351 if(params.klass !== undefined)
352 this.setWlxmlClass(params.klass);
353 this._updateWlxmlManager();
354 this.__updatingWlxml = false;
356 _updateWlxmlManager: function() {
357 var manager = wlxmlManagers.getFor(this);
358 this.data('_wlxmlManager', manager);
362 if(what === 'list' && _.contains(['list.items', 'list.items.enum'], this.getWlxmlClass()))
367 getWlxmlMetaAttr: function(attr) {
368 return this.dom().attr('wlxml-meta-'+attr);
371 getWlxmlMetaAttrs: function() {
373 var attrList = classAttributes.getMetaAttrsList(this.getWlxmlClass());
374 attrList.all.forEach(function(attr) {
375 toret.push({name: attr.name, value: this.getWlxmlMetaAttr(attr.name) || ''});
380 setWlxmlMetaAttr: function(attr, value) {
381 this.dom().attr('wlxml-meta-'+attr, value);
384 toggleLabel: function(toggle) {
385 var displayCss = toggle ? 'inline-block' : 'none';
386 var label = this.dom().children('.canvas-widgets').find('.canvas-widget-label');
387 label.css('display', displayCss);
388 this.toggleHighlight(toggle);
391 toggleHighlight: function(toggle) {
392 this._container().toggleClass('highlighted-element', toggle);
395 toggle: function(toggle) {
396 var mng = this.data('_wlxmlManager');
404 // DocumentNodeElement represents a text node from WLXML document rendered inside Canvas
405 var DocumentTextElement = function(htmlElement, canvas) {
406 DocumentElement.call(this, htmlElement, canvas);
409 $.extend(DocumentTextElement, {
410 createDOM: function(params) {
412 .attr('document-text-element', '')
413 .text(params.text || utils.unicode.ZWS);
416 create: function(params, canvas) {
417 return this.fromHTMLElement(this.createDOM(params)[0]);
420 fromHTMLElement: function(htmlElement, canvas) {
421 return new this(htmlElement, canvas);
423 isContentContainer: function(htmlElement) {
424 return htmlElement.nodeType === Node.TEXT_NODE && $(htmlElement).parent().is('[document-text-element]');
428 DocumentTextElement.prototype = new DocumentElement();
430 $.extend(DocumentTextElement.prototype, {
431 toXML: function(parent) {
432 return this.getText();
434 _setupDOMHandler: function(htmlElement) {
435 var $element = $(htmlElement);
436 if(htmlElement.nodeType === Node.TEXT_NODE)
437 this.$element = $element.parent();
439 this.$element = $element;
446 setText: function(text) {
447 this.dom().contents()[0].data = text;
449 appendText: function(text) {
450 this.dom().contents()[0].data += text;
452 getText: function() {
453 return this.dom().text().replace(utils.unicode.ZWS, '');
455 isEmpty: function() {
456 // Having at least Zero Width Space is guaranteed be Content Observer
457 return this.dom().contents()[0].data === utils.unicode.ZWS;
459 after: function(params) {
460 if(params instanceof DocumentTextElement || params.text)
463 if(params instanceof DocumentNodeElement) {
466 element = DocumentNodeElement.create(params, this.canvas);
468 this.dom().wrap('<div>');
469 this.dom().parent().after(element.dom());
473 before: 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().before(element.dom());
487 wrapWithNodeElement: function(wlxmlNode) {
488 if(typeof wlxmlNode.start === 'number' && typeof wlxmlNode.end === 'number') {
489 return this.canvas.wrapText({
490 inside: this.parent(),
491 textNodeIdx: this.parent().childIndex(this),
492 offsetStart: Math.min(wlxmlNode.start, wlxmlNode.end),
493 offsetEnd: Math.max(wlxmlNode.start, wlxmlNode.end),
494 _with: {tag: wlxmlNode.tag, klass: wlxmlNode.klass}
497 return DocumentElement.prototype.wrapWithNodeElement.call(this, wlxmlNode);
501 var parent = this.parent(),
503 if(parent.children().length === 1) {
504 toret = parent.parent();
505 var grandParent = parent.parent();
507 var grandParentChildren = grandParent.children(),
508 idx = grandParent.childIndex(parent),
509 prev = idx - 1 > -1 ? grandParentChildren[idx-1] : null,
510 next = idx + 1 < grandParentChildren.length ? grandParentChildren[idx+1] : null;
512 prev = (prev instanceof DocumentTextElement) ? prev : null;
513 next = (next instanceof DocumentTextElement) ? next : null;
516 prev.setText(prev.getText() + this.getText() + next.getText());
518 } else if (prev || next) {
519 var target = prev ? prev : next;
520 target.setText(target.getText() + this.getText());
531 split: function(params) {
532 var parentElement = this.parent(),
533 myIdx = parentElement.childIndex(this),
534 myCanvas = this.canvas,
536 succeedingChildren = [],
538 prefix = this.getText().substr(0, params.offset),
539 suffix = this.getText().substr(params.offset);
541 parentElement.children().forEach(function(child) {
543 succeedingChildren.push(child);
544 if(child.sameNode(thisElement))
548 if(prefix.length > 0)
549 this.setText(prefix);
553 var newElement = DocumentNodeElement.create({tag: parentElement.getWlxmlTag(), klass: parentElement.getWlxmlClass()}, myCanvas);
554 parentElement.after(newElement);
556 if(suffix.length > 0)
557 newElement.append({text: suffix});
558 succeedingChildren.forEach(function(child) {
559 newElement.append(child);
562 return {first: parentElement, second: newElement};
564 divide: function(params) {
565 var myText = this.getText();
567 if(params.offset === myText.length)
568 return this.after(params);
569 if(params.offset === 0)
570 return this.before(params);
572 var lhsText = myText.substr(0, params.offset),
573 rhsText = myText.substr(params.offset),
574 newElement = DocumentNodeElement.create({tag: params.tag, klass: params.klass}, this.canvas),
575 rhsTextElement = DocumentTextElement.create({text: rhsText});
577 this.setText(lhsText);
578 this.after(newElement);
579 newElement.after(rhsTextElement);
583 toggleHighlight: function() {
584 // do nothing for now
589 DocumentElement: DocumentElement,
590 DocumentNodeElement: DocumentNodeElement,
591 DocumentTextElement: DocumentTextElement