2 'libs/jquery-1.9.1.min',
4 'modules/documentCanvas/classAttributes',
5 'modules/documentCanvas/canvas/utils'
6 ], function($, _, classAttributes, utils) {
11 // DocumentElement represents a text or an element node from WLXML document rendered inside Canvas
12 var DocumentElement = function(htmlElement, canvas) {
13 if(arguments.length === 0)
16 this._setupDOMHandler(htmlElement);
19 var elementTypeFromParams = function(params) {
20 return params.text !== undefined ? DocumentTextElement : DocumentNodeElement;
24 $.extend(DocumentElement, {
25 create: function(params, canvas) {
26 return elementTypeFromParams(params).create(params);
29 createDOM: function(params) {
30 return elementTypeFromParams(params).createDOM(params);
33 fromHTMLElement: function(htmlElement, canvas) {
34 var $element = $(htmlElement);
35 if(htmlElement.nodeType === Node.ELEMENT_NODE && $element.attr('document-node-element') !== undefined)
36 return DocumentNodeElement.fromHTMLElement(htmlElement, canvas);
37 if($element.attr('document-text-element') !== undefined || (htmlElement.nodeType === Node.TEXT_NODE && $element.parent().attr('document-text-element') !== undefined))
38 return DocumentTextElement.fromHTMLElement(htmlElement, canvas);
43 $.extend(DocumentElement.prototype, {
44 _setupDOMHandler: function(htmlElement) {
45 this.$element = $(htmlElement);
51 var parents = this.$element.parents('[document-node-element]');
53 return DocumentElement.fromHTMLElement(parents[0], this.canvas);
59 parent = this.parent();
62 parent = parent.parent();
67 sameNode: function(other) {
68 return other && (typeof other === typeof this) && other.dom()[0] === this.dom()[0];
71 wrapWithNodeElement: function(wlxmlNode) {
72 var wrapper = DocumentNodeElement.create({tag: wlxmlNode.tag, klass: wlxmlNode.klass});
73 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) {
117 var dom = $('<div document-node-element>'),
118 container = $('<div document-element-content>');
120 container.attr('wlxml-tag', params.tag);
122 container.attr('wlxml-class', params.klass.replace(/\./g, '-'));
124 _.keys(params.meta).forEach(function(key) {
125 dom.attr('wlxml-meta-'+key, params.meta[key]);
128 dom.data('other-attrs', params.others);
130 var widgets = $('<div class="canvas-widgets" contenteditable="false">');
131 widgets.append($('<span class="canvas-widget canvas-widget-label">').text(params.tag + (params.klass ? ' / ' + params.klass : '')));
134 // Make sure widgets aren't navigable with arrow keys
135 widgets.find('*').add(widgets).attr('tabindex', -1);
137 dom.append(container);
139 if(params.rawChildren) {
140 container.append(params.rawChildren);
145 create: function(params, canvas) {
146 return this.fromHTMLElement(this.createDOM(params)[0]);
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' ? 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 dom = this.dom(),
175 args = Array.prototype.slice.call(arguments, 0);
176 if(args.length === 2 && args[1] === undefined)
177 return dom.removeData(args[0]);
178 return dom.data.apply(dom, arguments);
180 toXML: function(level) {
181 var node = $('<' + this.getWlxmlTag() + '>');
183 if(this.getWlxmlClass())
184 node.attr('class', this.getWlxmlClass());
185 var meta = this.getWlxmlMetaAttrs();
186 meta.forEach(function(attr) {
188 node.attr('meta-' + attr.name, attr.value);
190 _.keys(this.data('other-attrs') || {}).forEach(function(key) {
191 node.attr(key, this.data('other-attrs')[key]);
194 var addFormatting = function() {
195 var toret = $('<div>');
196 var formattings = {};
198 if(this.data('orig-before') !== undefined) {
199 if(this.data('orig-before')) {
200 toret.prepend(document.createTextNode(this.data('orig-before')));
202 } else if(level && this.getWlxmlTag() !== 'span') {
203 toret.append('\n' + (new Array(level * 2 + 1)).join(' '));
208 if(this.data('orig-after')) {
209 toret.append(document.createTextNode(this.data('orig-after')));
213 if(this.data('orig-begin')) {
214 node.prepend(this.data('orig-begin'));
215 formattings.begin = true;
218 if(this.data('orig-end') !== undefined) {
219 if(this.data('orig-end')) {
220 node.append(this.data('orig-end'));
222 } else if(this.getWlxmlTag() !== 'span' && children.length){
223 node.append('\n' + (new Array(level * 2 + 1)).join(' '));
226 return {parts: toret.contents(), formattings: formattings};
231 var children = this.children(),
234 var formatting = addFormatting(node);
236 for(var i = children.length - 1; i >= 0; i--) {
237 childParts = children[i].toXML(level + 1);
238 if(typeof childParts === 'string')
239 childParts = [document.createTextNode(childParts)];
241 if(formatting.formattings.begin) {
242 $(node.contents()[0]).after(childParts);
244 node.prepend(childParts);
246 return formatting.parts;
248 append: function(params) {
249 if(params.tag !== 'span')
250 this.data('orig-end', undefined);
251 return manipulate(this, params, 'append');
253 before: function(params) {
254 return manipulate(this, params, 'before');
257 after: function(params) {
258 return manipulate(this, params, 'after');
260 children: function() {
262 if(this instanceof DocumentTextElement)
266 var elementContent = this._container().contents();
268 elementContent.each(function(idx) {
269 var childElement = DocumentElement.fromHTMLElement(this, element.canvas);
270 if(childElement === undefined)
272 if(idx === 0 && elementContent.length > 1 && elementContent[1].nodeType === Node.ELEMENT_NODE && (childElement instanceof DocumentTextElement) && $.trim($(this).text()) === '')
274 if(idx > 0 && childElement instanceof DocumentTextElement) {
275 if(toret[toret.length-1] instanceof DocumentNodeElement && $.trim($(this).text()) === '')
278 toret.push(childElement);
282 childIndex: function(child) {
283 var children = this.children(),
285 children.forEach(function(c, idx) {
286 if(c.sameNode(child)) {
293 getWlxmlTag: function() {
294 return this._container().attr('wlxml-tag');
296 setWlxmlTag: function(tag) {
297 this._container().attr('wlxml-tag', tag);
299 getWlxmlClass: function() {
300 var klass = this._container().attr('wlxml-class');
302 return klass.replace(/-/g, '.');
305 setWlxmlClass: function(klass) {
306 this.getWlxmlMetaAttrs().forEach(function(attr) {
307 if(!classAttributes.hasMetaAttr(klass, attr.name))
308 this.dom().removeAttr('wlxml-meta-' + attr.name);
312 this._container().attr('wlxml-class', klass.replace(/\./g, '-'));
314 this._container().removeAttr('wlxml-class');
317 if(what === 'list' && _.contains(['list.items', 'list.items.enum'], this.getWlxmlClass()))
322 getWlxmlMetaAttr: function(attr) {
323 return this.dom().attr('wlxml-meta-'+attr);
326 getWlxmlMetaAttrs: function() {
328 var attrList = classAttributes.getMetaAttrsList(this.getWlxmlClass());
329 attrList.all.forEach(function(attr) {
330 toret.push({name: attr.name, value: this.getWlxmlMetaAttr(attr.name) || ''});
335 setWlxmlMetaAttr: function(attr, value) {
336 this.dom().attr('wlxml-meta-'+attr, value);
339 toggleLabel: function(toggle) {
340 var displayCss = toggle ? 'inline-block' : 'none';
341 var label = this.dom().children('.canvas-widgets').find('.canvas-widget-label');
342 label.css('display', displayCss);
343 this.dom().toggleClass('highlighted-element');
348 // DocumentNodeElement represents a text node from WLXML document rendered inside Canvas
349 var DocumentTextElement = function(htmlElement, canvas) {
350 DocumentElement.call(this, htmlElement, canvas);
353 $.extend(DocumentTextElement, {
354 createDOM: function(params) {
356 .attr('document-text-element', '')
357 .text(params.text || utils.unicode.ZWS);
360 create: function(params, canvas) {
361 return this.fromHTMLElement(this.createDOM(params)[0]);
364 fromHTMLElement: function(htmlElement, canvas) {
365 return new this(htmlElement, canvas);
367 isContentContainer: function(htmlElement) {
368 return htmlElement.nodeType === Node.TEXT_NODE && $(htmlElement).parent().is('[document-text-element]');
372 DocumentTextElement.prototype = new DocumentElement();
374 $.extend(DocumentTextElement.prototype, {
375 toXML: function(parent) {
376 return this.getText();
378 _setupDOMHandler: function(htmlElement) {
379 var $element = $(htmlElement);
380 if(htmlElement.nodeType === Node.TEXT_NODE)
381 this.$element = $element.parent();
383 this.$element = $element;
385 setText: function(text) {
386 this.dom().contents()[0].data = text;
388 getText: function() {
389 return this.dom().text().replace(utils.unicode.ZWS, '');
391 isEmpty: function() {
392 // Having at least Zero Width Space is guaranteed be Content Observer
393 return this.dom().contents()[0].data === utils.unicode.ZWS;
395 after: function(params) {
396 if(params instanceof DocumentTextElement || params.text)
399 if(params instanceof DocumentNodeElement) {
402 element = DocumentNodeElement.create(params);
404 this.dom().wrap('<div>');
405 this.dom().parent().after(element.dom());
409 before: function(params) {
410 if(params instanceof DocumentTextElement || params.text)
413 if(params instanceof DocumentNodeElement) {
416 element = DocumentNodeElement.create(params);
418 this.dom().wrap('<div>');
419 this.dom().parent().before(element.dom());
423 wrapWithNodeElement: function(wlxmlNode) {
424 if(typeof wlxmlNode.start === 'number' && typeof wlxmlNode.end === 'number') {
425 return this.canvas.wrapText({
426 inside: this.parent(),
427 textNodeIdx: this.parent().childIndex(this),
428 offsetStart: Math.min(wlxmlNode.start, wlxmlNode.end),
429 offsetEnd: Math.max(wlxmlNode.start, wlxmlNode.end),
430 _with: {tag: wlxmlNode.tag, klass: wlxmlNode.klass}
433 return DocumentElement.prototype.wrapWithNodeElement.call(this, wlxmlNode);
437 var parent = this.parent(),
439 if(parent.children().length === 1) {
440 toret = parent.parent();
441 var grandParent = parent.parent();
443 var grandParentChildren = grandParent.children(),
444 idx = grandParent.childIndex(parent),
445 prev = idx - 1 > -1 ? grandParentChildren[idx-1] : null,
446 next = idx + 1 < grandParentChildren.length ? grandParentChildren[idx+1] : null;
448 prev.setText(prev.getText() + this.getText() + next.getText());
450 } else if (prev || next) {
451 var target = prev ? prev : next;
452 target.setText(target.getText() + this.getText());
463 split: function(params) {
464 var parentElement = this.parent(),
465 myIdx = parentElement.childIndex(this),
466 myCanvas = this.canvas,
468 succeedingChildren = [],
470 prefix = this.getText().substr(0, params.offset),
471 suffix = this.getText().substr(params.offset);
473 parentElement.children().forEach(function(child) {
475 succeedingChildren.push(child);
476 if(child.sameNode(thisElement))
480 if(prefix.length > 0)
481 this.setText(prefix);
485 var newElement = DocumentNodeElement.create({tag: parentElement.getWlxmlTag(), klass: parentElement.getWlxmlClass()}, myCanvas);
486 parentElement.after(newElement);
488 if(suffix.length > 0)
489 newElement.append({text: suffix});
490 succeedingChildren.forEach(function(child) {
491 newElement.append(child);
494 return {first: parentElement, second: newElement};
496 divide: function(params) {
497 var myText = this.getText();
499 if(params.offset === myText.length)
500 return this.after(params);
501 if(params.offset === 0)
502 return this.before(params);
504 var lhsText = myText.substr(0, params.offset),
505 rhsText = myText.substr(params.offset),
506 newElement = DocumentNodeElement.create({tag: params.tag, klass: params.klass}, this.canvas),
507 rhsTextElement = DocumentTextElement.create({text: rhsText});
509 this.setText(lhsText);
510 this.after(newElement);
511 newElement.after(rhsTextElement);
517 DocumentElement: DocumentElement,
518 DocumentNodeElement: DocumentNodeElement,
519 DocumentTextElement: DocumentTextElement