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);
53 var parents = this.$element.parents('[document-node-element]');
55 return DocumentElement.fromHTMLElement(parents[0], this.canvas);
61 parent = this.parent();
64 parent = parent.parent();
69 sameNode: function(other) {
70 return other && (typeof other === typeof this) && other.dom()[0] === this.dom()[0];
73 wrapWithNodeElement: function(wlxmlNode) {
74 var wrapper = DocumentNodeElement.create({tag: wlxmlNode.tag, klass: wlxmlNode.klass});
75 this.dom().replaceWith(wrapper.dom());
85 markAsCurrent: function() {
86 this.canvas.markAsCurrent(this);
89 getVerticallyFirstTextElement: function() {
91 this.children().some(function(child) {
92 if(!child.isVisible())
93 return false; // continue
94 if(child instanceof DocumentTextElement) {
98 toret = child.getVerticallyFirstTextElement();
100 return true; // break
106 isVisible: function() {
107 return this instanceof DocumentTextElement || this.getWlxmlTag() !== 'metadata';
112 // DocumentNodeElement represents an element node from WLXML document rendered inside Canvas
113 var DocumentNodeElement = function(htmlElement, canvas) {
114 DocumentElement.call(this, htmlElement, canvas);
117 $.extend(DocumentNodeElement, {
118 createDOM: function(params) {
120 .attr('document-node-element', ''),
121 widgetsContainer = $('<div>')
122 .addClass('canvas-widgets')
123 .attr('contenteditable', false),
124 container = $('<div>')
125 .attr('document-element-content', '');
127 dom.append(widgetsContainer, container);
128 // Make sure widgets aren't navigable with arrow keys
129 widgetsContainer.find('*').add(widgetsContainer).attr('tabindex', -1);
131 var element = this.fromHTMLElement(dom[0]);
133 element.setWlxmlTag(params.tag);
135 element.setWlxmlClass(params.klass);
137 _.keys(params.meta).forEach(function(key) {
138 element.setWlxmlMetaAttr(key, params.meta[key]);
141 element.data('other-attrs', params.others);
143 if(params.rawChildren) {
144 container.append(params.rawChildren);
149 create: function(params, canvas) {
150 return this.fromHTMLElement(this.createDOM(params)[0]);
153 fromHTMLElement: function(htmlElement, canvas) {
154 return new this(htmlElement, canvas);
158 var manipulate = function(e, params, action) {
160 if(params instanceof DocumentElement) {
163 element = DocumentElement.create(params);
165 var target = action === 'append' ? e._container() : e.dom();
166 target[action](element.dom());
170 DocumentNodeElement.prototype = new DocumentElement();
173 $.extend(DocumentNodeElement.prototype, {
174 _container: function() {
175 return this.dom().children('[document-element-content]');
178 var dom = this.dom(),
179 args = Array.prototype.slice.call(arguments, 0);
180 if(args.length === 2 && args[1] === undefined)
181 return dom.removeData(args[0]);
182 return dom.data.apply(dom, arguments);
184 toXML: function(level) {
185 var node = $('<' + this.getWlxmlTag() + '>');
187 if(this.getWlxmlClass())
188 node.attr('class', this.getWlxmlClass());
189 var meta = this.getWlxmlMetaAttrs();
190 meta.forEach(function(attr) {
192 node.attr('meta-' + attr.name, attr.value);
194 _.keys(this.data('other-attrs') || {}).forEach(function(key) {
195 node.attr(key, this.data('other-attrs')[key]);
198 var addFormatting = function() {
199 var toret = $('<div>');
200 var formattings = {};
202 if(this.data('orig-before') !== undefined) {
203 if(this.data('orig-before')) {
204 toret.prepend(document.createTextNode(this.data('orig-before')));
206 } else if(level && this.getWlxmlTag() !== 'span') {
207 toret.append('\n' + (new Array(level * 2 + 1)).join(' '));
212 if(this.data('orig-after')) {
213 toret.append(document.createTextNode(this.data('orig-after')));
217 if(this.data('orig-begin')) {
218 node.prepend(this.data('orig-begin'));
219 formattings.begin = true;
222 if(this.data('orig-end') !== undefined) {
223 if(this.data('orig-end')) {
224 node.append(this.data('orig-end'));
226 } else if(this.getWlxmlTag() !== 'span' && children.length){
227 node.append('\n' + (new Array(level * 2 + 1)).join(' '));
230 return {parts: toret.contents(), formattings: formattings};
235 var children = this.children(),
238 var formatting = addFormatting(node);
240 for(var i = children.length - 1; i >= 0; i--) {
241 childParts = children[i].toXML(level + 1);
242 if(typeof childParts === 'string')
243 childParts = [document.createTextNode(childParts)];
245 if(formatting.formattings.begin) {
246 $(node.contents()[0]).after(childParts);
248 node.prepend(childParts);
250 return formatting.parts;
252 append: function(params) {
253 if(params.tag !== 'span')
254 this.data('orig-end', undefined);
255 return manipulate(this, params, 'append');
257 before: function(params) {
258 return manipulate(this, params, 'before');
261 after: function(params) {
262 return manipulate(this, params, 'after');
264 children: function() {
266 if(this instanceof DocumentTextElement)
270 var elementContent = this._container().contents();
272 elementContent.each(function(idx) {
273 var childElement = DocumentElement.fromHTMLElement(this, element.canvas);
274 if(childElement === undefined)
276 if(idx === 0 && elementContent.length > 1 && elementContent[1].nodeType === Node.ELEMENT_NODE && (childElement instanceof DocumentTextElement) && $.trim($(this).text()) === '')
278 if(idx > 0 && childElement instanceof DocumentTextElement) {
279 if(toret[toret.length-1] instanceof DocumentNodeElement && $.trim($(this).text()) === '')
282 toret.push(childElement);
286 childIndex: function(child) {
287 var children = this.children(),
289 children.forEach(function(c, idx) {
290 if(c.sameNode(child)) {
297 getWlxmlTag: function() {
298 return this._container().attr('wlxml-tag');
300 setWlxmlTag: function(tag) {
301 if(tag === this.getWlxmlTag())
304 this._container().attr('wlxml-tag', tag);
305 this._updateWlxmlManager();
307 getWlxmlClass: function() {
308 var klass = this._container().attr('wlxml-class');
310 return klass.replace(/-/g, '.');
313 setWlxmlClass: function(klass) {
314 if(klass === this.getWlxmlClass())
317 this.getWlxmlMetaAttrs().forEach(function(attr) {
318 if(!classAttributes.hasMetaAttr(klass, attr.name))
319 this.dom().removeAttr('wlxml-meta-' + attr.name);
323 this._container().attr('wlxml-class', klass.replace(/\./g, '-'));
325 this._container().removeAttr('wlxml-class');
326 this._updateWlxmlManager();
328 _updateWlxmlManager: function() {
329 var manager = wlxmlManagers.getFor(this);
330 this.data('_wlxmlManager', manager);
334 if(what === 'list' && _.contains(['list.items', 'list.items.enum'], this.getWlxmlClass()))
339 getWlxmlMetaAttr: function(attr) {
340 return this.dom().attr('wlxml-meta-'+attr);
343 getWlxmlMetaAttrs: function() {
345 var attrList = classAttributes.getMetaAttrsList(this.getWlxmlClass());
346 attrList.all.forEach(function(attr) {
347 toret.push({name: attr.name, value: this.getWlxmlMetaAttr(attr.name) || ''});
352 setWlxmlMetaAttr: function(attr, value) {
353 this.dom().attr('wlxml-meta-'+attr, value);
356 toggleLabel: function(toggle) {
357 var displayCss = toggle ? 'inline-block' : 'none';
358 var label = this.dom().children('.canvas-widgets').find('.canvas-widget-label');
359 label.css('display', displayCss);
360 this.toggleHighlight(toggle);
363 toggleHighlight: function(toogle) {
364 this._container().toggleClass('highlighted-element');
367 toggle: function(toggle) {
368 var mng = this.data('_wlxmlManager');
376 // DocumentNodeElement represents a text node from WLXML document rendered inside Canvas
377 var DocumentTextElement = function(htmlElement, canvas) {
378 DocumentElement.call(this, htmlElement, canvas);
381 $.extend(DocumentTextElement, {
382 createDOM: function(params) {
384 .attr('document-text-element', '')
385 .text(params.text || utils.unicode.ZWS);
388 create: function(params, canvas) {
389 return this.fromHTMLElement(this.createDOM(params)[0]);
392 fromHTMLElement: function(htmlElement, canvas) {
393 return new this(htmlElement, canvas);
395 isContentContainer: function(htmlElement) {
396 return htmlElement.nodeType === Node.TEXT_NODE && $(htmlElement).parent().is('[document-text-element]');
400 DocumentTextElement.prototype = new DocumentElement();
402 $.extend(DocumentTextElement.prototype, {
403 toXML: function(parent) {
404 return this.getText();
406 _setupDOMHandler: function(htmlElement) {
407 var $element = $(htmlElement);
408 if(htmlElement.nodeType === Node.TEXT_NODE)
409 this.$element = $element.parent();
411 this.$element = $element;
413 setText: function(text) {
414 this.dom().contents()[0].data = text;
416 getText: function() {
417 return this.dom().text().replace(utils.unicode.ZWS, '');
419 isEmpty: function() {
420 // Having at least Zero Width Space is guaranteed be Content Observer
421 return this.dom().contents()[0].data === utils.unicode.ZWS;
423 after: function(params) {
424 if(params instanceof DocumentTextElement || params.text)
427 if(params instanceof DocumentNodeElement) {
430 element = DocumentNodeElement.create(params);
432 this.dom().wrap('<div>');
433 this.dom().parent().after(element.dom());
437 before: function(params) {
438 if(params instanceof DocumentTextElement || params.text)
441 if(params instanceof DocumentNodeElement) {
444 element = DocumentNodeElement.create(params);
446 this.dom().wrap('<div>');
447 this.dom().parent().before(element.dom());
451 wrapWithNodeElement: function(wlxmlNode) {
452 if(typeof wlxmlNode.start === 'number' && typeof wlxmlNode.end === 'number') {
453 return this.canvas.wrapText({
454 inside: this.parent(),
455 textNodeIdx: this.parent().childIndex(this),
456 offsetStart: Math.min(wlxmlNode.start, wlxmlNode.end),
457 offsetEnd: Math.max(wlxmlNode.start, wlxmlNode.end),
458 _with: {tag: wlxmlNode.tag, klass: wlxmlNode.klass}
461 return DocumentElement.prototype.wrapWithNodeElement.call(this, wlxmlNode);
465 var parent = this.parent(),
467 if(parent.children().length === 1) {
468 toret = parent.parent();
469 var grandParent = parent.parent();
471 var grandParentChildren = grandParent.children(),
472 idx = grandParent.childIndex(parent),
473 prev = idx - 1 > -1 ? grandParentChildren[idx-1] : null,
474 next = idx + 1 < grandParentChildren.length ? grandParentChildren[idx+1] : null;
476 prev.setText(prev.getText() + this.getText() + next.getText());
478 } else if (prev || next) {
479 var target = prev ? prev : next;
480 target.setText(target.getText() + this.getText());
491 split: function(params) {
492 var parentElement = this.parent(),
493 myIdx = parentElement.childIndex(this),
494 myCanvas = this.canvas,
496 succeedingChildren = [],
498 prefix = this.getText().substr(0, params.offset),
499 suffix = this.getText().substr(params.offset);
501 parentElement.children().forEach(function(child) {
503 succeedingChildren.push(child);
504 if(child.sameNode(thisElement))
508 if(prefix.length > 0)
509 this.setText(prefix);
513 var newElement = DocumentNodeElement.create({tag: parentElement.getWlxmlTag(), klass: parentElement.getWlxmlClass()}, myCanvas);
514 parentElement.after(newElement);
516 if(suffix.length > 0)
517 newElement.append({text: suffix});
518 succeedingChildren.forEach(function(child) {
519 newElement.append(child);
522 return {first: parentElement, second: newElement};
524 divide: function(params) {
525 var myText = this.getText();
527 if(params.offset === myText.length)
528 return this.after(params);
529 if(params.offset === 0)
530 return this.before(params);
532 var lhsText = myText.substr(0, params.offset),
533 rhsText = myText.substr(params.offset),
534 newElement = DocumentNodeElement.create({tag: params.tag, klass: params.klass}, this.canvas),
535 rhsTextElement = DocumentTextElement.create({text: rhsText});
537 this.setText(lhsText);
538 this.after(newElement);
539 newElement.after(rhsTextElement);
543 toggleHighlight: function() {
544 // do nothing for now
549 DocumentElement: DocumentElement,
550 DocumentNodeElement: DocumentNodeElement,
551 DocumentTextElement: DocumentTextElement