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());
88 markAsCurrent: function() {
89 this.canvas.markAsCurrent(this);
92 getVerticallyFirstTextElement: function() {
94 this.children().some(function(child) {
95 if(!child.isVisible())
96 return false; // continue
97 if(child instanceof DocumentTextElement) {
101 toret = child.getVerticallyFirstTextElement();
103 return true; // break
109 isVisible: function() {
110 return this instanceof DocumentTextElement || this.getWlxmlTag() !== 'metadata';
115 // DocumentNodeElement represents an element node from WLXML document rendered inside Canvas
116 var DocumentNodeElement = function(htmlElement, canvas) {
117 DocumentElement.call(this, htmlElement, canvas);
120 $.extend(DocumentNodeElement, {
121 createDOM: function(params, canvas) {
123 .attr('document-node-element', ''),
124 widgetsContainer = $('<div>')
125 .addClass('canvas-widgets')
126 .attr('contenteditable', false),
127 container = $('<div>')
128 .attr('document-element-content', '');
130 dom.append(widgetsContainer, container);
131 // Make sure widgets aren't navigable with arrow keys
132 widgetsContainer.find('*').add(widgetsContainer).attr('tabindex', -1);
134 var element = this.fromHTMLElement(dom[0], canvas);
136 element.setWlxmlTag(params.tag);
138 element.setWlxmlClass(params.klass);
140 _.keys(params.meta).forEach(function(key) {
141 element.setWlxmlMetaAttr(key, params.meta[key]);
144 element.data('other-attrs', params.others);
146 if(params.rawChildren) {
147 container.append(params.rawChildren);
152 create: function(params, canvas) {
153 return this.fromHTMLElement(this.createDOM(params, canvas)[0], canvas);
156 fromHTMLElement: function(htmlElement, canvas) {
157 return new this(htmlElement, canvas);
161 var manipulate = function(e, params, action) {
163 if(params instanceof DocumentElement) {
166 element = DocumentElement.create(params);
168 var target = action === 'append' ? e._container() : e.dom();
169 target[action](element.dom());
173 DocumentNodeElement.prototype = new DocumentElement();
176 $.extend(DocumentNodeElement.prototype, {
177 _container: function() {
178 return this.dom().children('[document-element-content]');
181 var dom = this.dom(),
182 args = Array.prototype.slice.call(arguments, 0);
183 if(args.length === 2 && args[1] === undefined)
184 return dom.removeData(args[0]);
185 return dom.data.apply(dom, arguments);
187 toXML: function(level) {
188 var node = $('<' + this.getWlxmlTag() + '>');
190 if(this.getWlxmlClass())
191 node.attr('class', this.getWlxmlClass());
192 var meta = this.getWlxmlMetaAttrs();
193 meta.forEach(function(attr) {
195 node.attr('meta-' + attr.name, attr.value);
197 _.keys(this.data('other-attrs') || {}).forEach(function(key) {
198 node.attr(key, this.data('other-attrs')[key]);
201 var addFormatting = function() {
202 var toret = $('<div>');
203 var formattings = {};
205 if(this.data('orig-before') !== undefined) {
206 if(this.data('orig-before')) {
207 toret.prepend(document.createTextNode(this.data('orig-before')));
209 } else if(level && this.getWlxmlTag() !== 'span') {
210 toret.append('\n' + (new Array(level * 2 + 1)).join(' '));
215 if(this.data('orig-after')) {
216 toret.append(document.createTextNode(this.data('orig-after')));
220 if(this.data('orig-begin')) {
221 node.prepend(this.data('orig-begin'));
222 formattings.begin = true;
225 if(this.data('orig-end') !== undefined) {
226 if(this.data('orig-end')) {
227 node.append(this.data('orig-end'));
229 } else if(this.getWlxmlTag() !== 'span' && children.length){
230 node.append('\n' + (new Array(level * 2 + 1)).join(' '));
233 return {parts: toret.contents(), formattings: formattings};
238 var children = this.children(),
241 var formatting = addFormatting(node);
243 for(var i = children.length - 1; i >= 0; i--) {
244 childParts = children[i].toXML(level + 1);
245 if(typeof childParts === 'string')
246 childParts = [document.createTextNode(childParts)];
248 if(formatting.formattings.begin) {
249 $(node.contents()[0]).after(childParts);
251 node.prepend(childParts);
253 return formatting.parts;
255 append: function(params) {
256 if(params.tag !== 'span')
257 this.data('orig-end', undefined);
258 return manipulate(this, params, 'append');
260 before: function(params) {
261 return manipulate(this, params, 'before');
264 after: function(params) {
265 return manipulate(this, params, 'after');
267 children: function() {
269 if(this instanceof DocumentTextElement)
273 var elementContent = this._container().contents();
275 elementContent.each(function(idx) {
276 var childElement = DocumentElement.fromHTMLElement(this, element.canvas);
277 if(childElement === undefined)
279 if(idx === 0 && elementContent.length > 1 && elementContent[1].nodeType === Node.ELEMENT_NODE && (childElement instanceof DocumentTextElement) && $.trim($(this).text()) === '')
281 if(idx > 0 && childElement instanceof DocumentTextElement) {
282 if(toret[toret.length-1] instanceof DocumentNodeElement && $.trim($(this).text()) === '')
285 toret.push(childElement);
289 childIndex: function(child) {
290 var children = this.children(),
292 children.forEach(function(c, idx) {
293 if(c.sameNode(child)) {
300 getWlxmlTag: function() {
301 return this._container().attr('wlxml-tag');
303 setWlxmlTag: function(tag) {
304 if(tag === this.getWlxmlTag())
307 this._container().attr('wlxml-tag', tag);
308 this._updateWlxmlManager();
310 getWlxmlClass: function() {
311 var klass = this._container().attr('wlxml-class');
313 return klass.replace(/-/g, '.');
316 setWlxmlClass: function(klass) {
317 if(klass === this.getWlxmlClass())
320 this.getWlxmlMetaAttrs().forEach(function(attr) {
321 if(!classAttributes.hasMetaAttr(klass, attr.name))
322 this.dom().removeAttr('wlxml-meta-' + attr.name);
326 this._container().attr('wlxml-class', klass.replace(/\./g, '-'));
328 this._container().removeAttr('wlxml-class');
329 this._updateWlxmlManager();
331 _updateWlxmlManager: function() {
332 var manager = wlxmlManagers.getFor(this);
333 this.data('_wlxmlManager', manager);
337 if(what === 'list' && _.contains(['list.items', 'list.items.enum'], this.getWlxmlClass()))
342 getWlxmlMetaAttr: function(attr) {
343 return this.dom().attr('wlxml-meta-'+attr);
346 getWlxmlMetaAttrs: function() {
348 var attrList = classAttributes.getMetaAttrsList(this.getWlxmlClass());
349 attrList.all.forEach(function(attr) {
350 toret.push({name: attr.name, value: this.getWlxmlMetaAttr(attr.name) || ''});
355 setWlxmlMetaAttr: function(attr, value) {
356 this.dom().attr('wlxml-meta-'+attr, value);
359 toggleLabel: function(toggle) {
360 var displayCss = toggle ? 'inline-block' : 'none';
361 var label = this.dom().children('.canvas-widgets').find('.canvas-widget-label');
362 label.css('display', displayCss);
363 this.toggleHighlight(toggle);
366 toggleHighlight: function(toogle) {
367 this._container().toggleClass('highlighted-element');
370 toggle: function(toggle) {
371 var mng = this.data('_wlxmlManager');
379 // DocumentNodeElement represents a text node from WLXML document rendered inside Canvas
380 var DocumentTextElement = function(htmlElement, canvas) {
381 DocumentElement.call(this, htmlElement, canvas);
384 $.extend(DocumentTextElement, {
385 createDOM: function(params) {
387 .attr('document-text-element', '')
388 .text(params.text || utils.unicode.ZWS);
391 create: function(params, canvas) {
392 return this.fromHTMLElement(this.createDOM(params)[0]);
395 fromHTMLElement: function(htmlElement, canvas) {
396 return new this(htmlElement, canvas);
398 isContentContainer: function(htmlElement) {
399 return htmlElement.nodeType === Node.TEXT_NODE && $(htmlElement).parent().is('[document-text-element]');
403 DocumentTextElement.prototype = new DocumentElement();
405 $.extend(DocumentTextElement.prototype, {
406 toXML: function(parent) {
407 return this.getText();
409 _setupDOMHandler: function(htmlElement) {
410 var $element = $(htmlElement);
411 if(htmlElement.nodeType === Node.TEXT_NODE)
412 this.$element = $element.parent();
414 this.$element = $element;
416 setText: function(text) {
417 this.dom().contents()[0].data = text;
419 getText: function() {
420 return this.dom().text().replace(utils.unicode.ZWS, '');
422 isEmpty: function() {
423 // Having at least Zero Width Space is guaranteed be Content Observer
424 return this.dom().contents()[0].data === utils.unicode.ZWS;
426 after: function(params) {
427 if(params instanceof DocumentTextElement || params.text)
430 if(params instanceof DocumentNodeElement) {
433 element = DocumentNodeElement.create(params, this.canvas);
435 this.dom().wrap('<div>');
436 this.dom().parent().after(element.dom());
440 before: function(params) {
441 if(params instanceof DocumentTextElement || params.text)
444 if(params instanceof DocumentNodeElement) {
447 element = DocumentNodeElement.create(params, this.canvas);
449 this.dom().wrap('<div>');
450 this.dom().parent().before(element.dom());
454 wrapWithNodeElement: function(wlxmlNode) {
455 if(typeof wlxmlNode.start === 'number' && typeof wlxmlNode.end === 'number') {
456 return this.canvas.wrapText({
457 inside: this.parent(),
458 textNodeIdx: this.parent().childIndex(this),
459 offsetStart: Math.min(wlxmlNode.start, wlxmlNode.end),
460 offsetEnd: Math.max(wlxmlNode.start, wlxmlNode.end),
461 _with: {tag: wlxmlNode.tag, klass: wlxmlNode.klass}
464 return DocumentElement.prototype.wrapWithNodeElement.call(this, wlxmlNode);
468 var parent = this.parent(),
470 if(parent.children().length === 1) {
471 toret = parent.parent();
472 var grandParent = parent.parent();
474 var grandParentChildren = grandParent.children(),
475 idx = grandParent.childIndex(parent),
476 prev = idx - 1 > -1 ? grandParentChildren[idx-1] : null,
477 next = idx + 1 < grandParentChildren.length ? grandParentChildren[idx+1] : null;
479 prev.setText(prev.getText() + this.getText() + next.getText());
481 } else if (prev || next) {
482 var target = prev ? prev : next;
483 target.setText(target.getText() + this.getText());
494 split: function(params) {
495 var parentElement = this.parent(),
496 myIdx = parentElement.childIndex(this),
497 myCanvas = this.canvas,
499 succeedingChildren = [],
501 prefix = this.getText().substr(0, params.offset),
502 suffix = this.getText().substr(params.offset);
504 parentElement.children().forEach(function(child) {
506 succeedingChildren.push(child);
507 if(child.sameNode(thisElement))
511 if(prefix.length > 0)
512 this.setText(prefix);
516 var newElement = DocumentNodeElement.create({tag: parentElement.getWlxmlTag(), klass: parentElement.getWlxmlClass()}, myCanvas);
517 parentElement.after(newElement);
519 if(suffix.length > 0)
520 newElement.append({text: suffix});
521 succeedingChildren.forEach(function(child) {
522 newElement.append(child);
525 return {first: parentElement, second: newElement};
527 divide: function(params) {
528 var myText = this.getText();
530 if(params.offset === myText.length)
531 return this.after(params);
532 if(params.offset === 0)
533 return this.before(params);
535 var lhsText = myText.substr(0, params.offset),
536 rhsText = myText.substr(params.offset),
537 newElement = DocumentNodeElement.create({tag: params.tag, klass: params.klass}, this.canvas),
538 rhsTextElement = DocumentTextElement.create({text: rhsText});
540 this.setText(lhsText);
541 this.after(newElement);
542 newElement.after(rhsTextElement);
546 toggleHighlight: function() {
547 // do nothing for now
552 DocumentElement: DocumentElement,
553 DocumentNodeElement: DocumentNodeElement,
554 DocumentTextElement: DocumentTextElement