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' || action === 'prepend') ? 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 prepend: function(params) {
261 return manipulate(this, params, 'prepend');
263 before: function(params) {
264 return manipulate(this, params, 'before');
267 after: function(params) {
268 return manipulate(this, params, 'after');
270 children: function() {
272 if(this instanceof DocumentTextElement)
276 var elementContent = this._container().contents();
278 elementContent.each(function(idx) {
279 var childElement = DocumentElement.fromHTMLElement(this, element.canvas);
280 if(childElement === undefined)
282 if(idx === 0 && elementContent.length > 1 && elementContent[1].nodeType === Node.ELEMENT_NODE && (childElement instanceof DocumentTextElement) && $.trim($(this).text()) === '')
284 if(idx > 0 && childElement instanceof DocumentTextElement) {
285 if(toret[toret.length-1] instanceof DocumentNodeElement && $.trim($(this).text()) === '')
288 toret.push(childElement);
292 childIndex: function(child) {
293 var children = this.children(),
295 children.forEach(function(c, idx) {
296 if(c.sameNode(child)) {
303 getWlxmlTag: function() {
304 return this._container().attr('wlxml-tag');
306 setWlxmlTag: function(tag) {
307 if(tag === this.getWlxmlTag())
310 this._container().attr('wlxml-tag', tag);
311 this._updateWlxmlManager();
313 getWlxmlClass: function() {
314 var klass = this._container().attr('wlxml-class');
316 return klass.replace(/-/g, '.');
319 setWlxmlClass: function(klass) {
320 if(klass === this.getWlxmlClass())
323 this.getWlxmlMetaAttrs().forEach(function(attr) {
324 if(!classAttributes.hasMetaAttr(klass, attr.name))
325 this.dom().removeAttr('wlxml-meta-' + attr.name);
329 this._container().attr('wlxml-class', klass.replace(/\./g, '-'));
331 this._container().removeAttr('wlxml-class');
332 this._updateWlxmlManager();
334 _updateWlxmlManager: function() {
335 var manager = wlxmlManagers.getFor(this);
336 this.data('_wlxmlManager', manager);
340 if(what === 'list' && _.contains(['list.items', 'list.items.enum'], this.getWlxmlClass()))
345 getWlxmlMetaAttr: function(attr) {
346 return this.dom().attr('wlxml-meta-'+attr);
349 getWlxmlMetaAttrs: function() {
351 var attrList = classAttributes.getMetaAttrsList(this.getWlxmlClass());
352 attrList.all.forEach(function(attr) {
353 toret.push({name: attr.name, value: this.getWlxmlMetaAttr(attr.name) || ''});
358 setWlxmlMetaAttr: function(attr, value) {
359 this.dom().attr('wlxml-meta-'+attr, value);
362 toggleLabel: function(toggle) {
363 var displayCss = toggle ? 'inline-block' : 'none';
364 var label = this.dom().children('.canvas-widgets').find('.canvas-widget-label');
365 label.css('display', displayCss);
366 this.toggleHighlight(toggle);
369 toggleHighlight: function(toogle) {
370 this._container().toggleClass('highlighted-element');
373 toggle: function(toggle) {
374 var mng = this.data('_wlxmlManager');
382 // DocumentNodeElement represents a text node from WLXML document rendered inside Canvas
383 var DocumentTextElement = function(htmlElement, canvas) {
384 DocumentElement.call(this, htmlElement, canvas);
387 $.extend(DocumentTextElement, {
388 createDOM: function(params) {
390 .attr('document-text-element', '')
391 .text(params.text || utils.unicode.ZWS);
394 create: function(params, canvas) {
395 return this.fromHTMLElement(this.createDOM(params)[0]);
398 fromHTMLElement: function(htmlElement, canvas) {
399 return new this(htmlElement, canvas);
401 isContentContainer: function(htmlElement) {
402 return htmlElement.nodeType === Node.TEXT_NODE && $(htmlElement).parent().is('[document-text-element]');
406 DocumentTextElement.prototype = new DocumentElement();
408 $.extend(DocumentTextElement.prototype, {
409 toXML: function(parent) {
410 return this.getText();
412 _setupDOMHandler: function(htmlElement) {
413 var $element = $(htmlElement);
414 if(htmlElement.nodeType === Node.TEXT_NODE)
415 this.$element = $element.parent();
417 this.$element = $element;
419 setText: function(text) {
420 this.dom().contents()[0].data = text;
422 getText: function() {
423 return this.dom().text().replace(utils.unicode.ZWS, '');
425 isEmpty: function() {
426 // Having at least Zero Width Space is guaranteed be Content Observer
427 return this.dom().contents()[0].data === utils.unicode.ZWS;
429 after: function(params) {
430 if(params instanceof DocumentTextElement || params.text)
433 if(params instanceof DocumentNodeElement) {
436 element = DocumentNodeElement.create(params, this.canvas);
438 this.dom().wrap('<div>');
439 this.dom().parent().after(element.dom());
443 before: function(params) {
444 if(params instanceof DocumentTextElement || params.text)
447 if(params instanceof DocumentNodeElement) {
450 element = DocumentNodeElement.create(params, this.canvas);
452 this.dom().wrap('<div>');
453 this.dom().parent().before(element.dom());
457 wrapWithNodeElement: function(wlxmlNode) {
458 if(typeof wlxmlNode.start === 'number' && typeof wlxmlNode.end === 'number') {
459 return this.canvas.wrapText({
460 inside: this.parent(),
461 textNodeIdx: this.parent().childIndex(this),
462 offsetStart: Math.min(wlxmlNode.start, wlxmlNode.end),
463 offsetEnd: Math.max(wlxmlNode.start, wlxmlNode.end),
464 _with: {tag: wlxmlNode.tag, klass: wlxmlNode.klass}
467 return DocumentElement.prototype.wrapWithNodeElement.call(this, wlxmlNode);
471 var parent = this.parent(),
473 if(parent.children().length === 1) {
474 toret = parent.parent();
475 var grandParent = parent.parent();
477 var grandParentChildren = grandParent.children(),
478 idx = grandParent.childIndex(parent),
479 prev = idx - 1 > -1 ? grandParentChildren[idx-1] : null,
480 next = idx + 1 < grandParentChildren.length ? grandParentChildren[idx+1] : null;
482 prev.setText(prev.getText() + this.getText() + next.getText());
484 } else if (prev || next) {
485 var target = prev ? prev : next;
486 target.setText(target.getText() + this.getText());
497 split: function(params) {
498 var parentElement = this.parent(),
499 myIdx = parentElement.childIndex(this),
500 myCanvas = this.canvas,
502 succeedingChildren = [],
504 prefix = this.getText().substr(0, params.offset),
505 suffix = this.getText().substr(params.offset);
507 parentElement.children().forEach(function(child) {
509 succeedingChildren.push(child);
510 if(child.sameNode(thisElement))
514 if(prefix.length > 0)
515 this.setText(prefix);
519 var newElement = DocumentNodeElement.create({tag: parentElement.getWlxmlTag(), klass: parentElement.getWlxmlClass()}, myCanvas);
520 parentElement.after(newElement);
522 if(suffix.length > 0)
523 newElement.append({text: suffix});
524 succeedingChildren.forEach(function(child) {
525 newElement.append(child);
528 return {first: parentElement, second: newElement};
530 divide: function(params) {
531 var myText = this.getText();
533 if(params.offset === myText.length)
534 return this.after(params);
535 if(params.offset === 0)
536 return this.before(params);
538 var lhsText = myText.substr(0, params.offset),
539 rhsText = myText.substr(params.offset),
540 newElement = DocumentNodeElement.create({tag: params.tag, klass: params.klass}, this.canvas),
541 rhsTextElement = DocumentTextElement.create({text: rhsText});
543 this.setText(lhsText);
544 this.after(newElement);
545 newElement.after(rhsTextElement);
549 toggleHighlight: function() {
550 // do nothing for now
555 DocumentElement: DocumentElement,
556 DocumentNodeElement: DocumentNodeElement,
557 DocumentTextElement: DocumentTextElement