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('wlxml-tag'))
36 return DocumentNodeElement.fromHTMLElement(htmlElement, canvas);
37 if($element.attr('wlxml-text') !== undefined || (htmlElement.nodeType === Node.TEXT_NODE && $element.parent().attr('wlxml-text') !== 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('[wlxml-tag]');
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) {
118 .attr('wlxml-tag', params.tag);
120 dom.attr('wlxml-class', params.klass.replace(/\./g, '-'));
122 _.keys(params.meta).forEach(function(key) {
123 dom.attr('wlxml-meta-'+key, params.meta[key]);
126 dom.data('other-attrs', params.others);
128 var widgets = $('<div class="canvas-widgets" contenteditable="false">');
129 widgets.append($('<span class="canvas-widget canvas-widget-label">').text(params.tag + (params.klass ? ' / ' + params.klass : '')));
132 // Make sure widgets aren't navigable with arrow keys
133 widgets.find('*').add(widgets).attr('tabindex', -1);
138 create: function(params, canvas) {
139 return this.fromHTMLElement(this.createDOM(params)[0]);
142 fromHTMLElement: function(htmlElement, canvas) {
143 return new this(htmlElement, canvas);
147 var manipulate = function(e, params, action) {
149 if(params instanceof DocumentElement) {
152 element = DocumentElement.create(params);
154 e.dom()[action](element.dom());
158 DocumentNodeElement.prototype = new DocumentElement();
161 $.extend(DocumentNodeElement.prototype, {
163 var dom = this.dom(),
164 args = Array.prototype.slice.call(arguments, 0);
165 if(args.length === 2 && args[1] === undefined)
166 return dom.removeData(args[0]);
167 return dom.data.apply(dom, arguments);
169 toXML: function(level) {
170 var node = $('<' + this.getWlxmlTag() + '>');
172 if(this.getWlxmlClass())
173 node.attr('class', this.getWlxmlClass());
174 var meta = this.getWlxmlMetaAttrs();
175 meta.forEach(function(attr) {
177 node.attr('meta-' + attr.name, attr.value);
179 _.keys(this.data('other-attrs') || {}).forEach(function(key) {
180 node.attr(key, this.data('other-attrs')[key]);
183 var addFormatting = function() {
184 var toret = $('<div>');
185 var formattings = {};
187 if(this.data('orig-before') !== undefined) {
188 if(this.data('orig-before')) {
189 toret.prepend(document.createTextNode(this.data('orig-before')));
191 } else if(level && this.getWlxmlTag() !== 'span') {
192 toret.append('\n' + (new Array(level * 2 + 1)).join(' '));
197 if(this.data('orig-after')) {
198 toret.append(document.createTextNode(this.data('orig-after')));
202 if(this.data('orig-begin')) {
203 node.prepend(this.data('orig-begin'));
204 formattings.begin = true;
207 if(this.data('orig-end') !== undefined) {
208 if(this.data('orig-end')) {
209 node.append(this.data('orig-end'));
211 } else if(this.getWlxmlTag() !== 'span' && children.length){
212 node.append('\n' + (new Array(level * 2 + 1)).join(' '));
215 return {parts: toret.contents(), formattings: formattings};
220 var children = this.children(),
223 var formatting = addFormatting(node);
225 for(var i = children.length - 1; i >= 0; i--) {
226 childParts = children[i].toXML(level + 1);
227 if(typeof childParts === 'string')
228 childParts = [document.createTextNode(childParts)];
230 if(formatting.formattings.begin) {
231 $(node.contents()[0]).after(childParts);
233 node.prepend(childParts);
235 return formatting.parts;
237 append: function(params) {
238 if(params.tag !== 'span')
239 this.data('orig-end', undefined);
240 return manipulate(this, params, 'append');
242 before: function(params) {
243 return manipulate(this, params, 'before');
246 after: function(params) {
247 return manipulate(this, params, 'after');
249 children: function() {
251 if(this instanceof DocumentTextElement)
255 var elementContent = this.dom().contents();
257 elementContent.each(function(idx) {
258 var childElement = DocumentElement.fromHTMLElement(this, element.canvas);
259 if(childElement === undefined)
261 if(idx === 1 && elementContent.length > 2 && elementContent[1].nodeType === Node.ELEMENT_NODE && (childElement instanceof DocumentTextElement) && $.trim($(this).text()) === '')
263 if(idx > 0 && childElement instanceof DocumentTextElement) {
264 if(toret[toret.length-1] instanceof DocumentNodeElement && $.trim($(this).text()) === '')
267 toret.push(childElement);
271 childIndex: function(child) {
272 var children = this.children(),
274 children.forEach(function(c, idx) {
275 if(c.sameNode(child)) {
282 getWlxmlTag: function() {
283 return this.dom().attr('wlxml-tag');
285 setWlxmlTag: function(tag) {
286 this.dom().attr('wlxml-tag', tag);
288 getWlxmlClass: function() {
289 var klass = this.dom().attr('wlxml-class');
291 return klass.replace(/-/g, '.');
294 setWlxmlClass: function(klass) {
295 this.getWlxmlMetaAttrs().forEach(function(attr) {
296 if(!classAttributes.hasMetaAttr(klass, attr.name))
297 this.dom().removeAttr('wlxml-meta-' + attr.name);
301 this.dom().attr('wlxml-class', klass.replace(/\./g, '-'));
303 this.dom().removeAttr('wlxml-class');
306 if(what === 'list' && _.contains(['list.items', 'list.items.enum'], this.getWlxmlClass()))
311 getWlxmlMetaAttr: function(attr) {
312 return this.dom().attr('wlxml-meta-'+attr);
315 getWlxmlMetaAttrs: function() {
317 var attrList = classAttributes.getMetaAttrsList(this.getWlxmlClass());
318 attrList.all.forEach(function(attr) {
319 toret.push({name: attr.name, value: this.getWlxmlMetaAttr(attr.name) || ''});
324 setWlxmlMetaAttr: function(attr, value) {
325 this.dom().attr('wlxml-meta-'+attr, value);
328 toggleLabel: function(toggle) {
329 var displayCss = toggle ? 'inline-block' : 'none';
330 var label = this.dom().children('.canvas-widgets').find('.canvas-widget-label');
331 label.css('display', displayCss);
332 this.dom().toggleClass('highlighted-element');
337 // DocumentNodeElement represents a text node from WLXML document rendered inside Canvas
338 var DocumentTextElement = function(htmlElement, canvas) {
339 DocumentElement.call(this, htmlElement, canvas);
342 $.extend(DocumentTextElement, {
343 createDOM: function(params) {
345 .attr('wlxml-text', '')
346 .text(params.text || utils.unicode.ZWS);
349 create: function(params, canvas) {
350 return this.fromHTMLElement(this.createDOM(params)[0]);
353 fromHTMLElement: function(htmlElement, canvas) {
354 return new this(htmlElement, canvas);
356 isContentContainer: function(htmlElement) {
357 return htmlElement.nodeType === Node.TEXT_NODE && $(htmlElement).parent().is('[wlxml-text]');
361 DocumentTextElement.prototype = new DocumentElement();
363 $.extend(DocumentTextElement.prototype, {
364 toXML: function(parent) {
365 return this.getText();
367 _setupDOMHandler: function(htmlElement) {
368 var $element = $(htmlElement);
369 if(htmlElement.nodeType === Node.TEXT_NODE)
370 this.$element = $element.parent();
372 this.$element = $element;
374 setText: function(text) {
375 this.dom().contents()[0].data = text;
377 getText: function() {
378 return this.dom().text().replace(utils.unicode.ZWS, '');
380 isEmpty: function() {
381 // Having at least Zero Width Space is guaranteed be Content Observer
382 return this.dom().contents()[0].data === utils.unicode.ZWS;
384 after: function(params) {
385 if(params instanceof DocumentTextElement || params.text)
388 if(params instanceof DocumentNodeElement) {
391 element = DocumentNodeElement.create(params);
393 this.dom().wrap('<div>');
394 this.dom().parent().after(element.dom());
398 before: function(params) {
399 if(params instanceof DocumentTextElement || params.text)
402 if(params instanceof DocumentNodeElement) {
405 element = DocumentNodeElement.create(params);
407 this.dom().wrap('<div>');
408 this.dom().parent().before(element.dom());
412 wrapWithNodeElement: function(wlxmlNode) {
413 if(typeof wlxmlNode.start === 'number' && typeof wlxmlNode.end === 'number') {
414 return this.canvas.wrapText({
415 inside: this.parent(),
416 textNodeIdx: this.parent().childIndex(this),
417 offsetStart: Math.min(wlxmlNode.start, wlxmlNode.end),
418 offsetEnd: Math.max(wlxmlNode.start, wlxmlNode.end),
419 _with: {tag: wlxmlNode.tag, klass: wlxmlNode.klass}
422 return DocumentElement.prototype.wrapWithNodeElement.call(this, wlxmlNode);
426 var parent = this.parent(),
428 if(parent.children().length === 1) {
429 toret = parent.parent();
430 var grandParent = parent.parent();
432 var grandParentChildren = grandParent.children(),
433 idx = grandParent.childIndex(parent),
434 prev = idx - 1 > -1 ? grandParentChildren[idx-1] : null,
435 next = idx + 1 < grandParentChildren.length ? grandParentChildren[idx+1] : null;
437 prev.setText(prev.getText() + this.getText() + next.getText());
439 } else if (prev || next) {
440 var target = prev ? prev : next;
441 target.setText(target.getText() + this.getText());
452 split: function(params) {
453 var parentElement = this.parent(),
454 myIdx = parentElement.childIndex(this),
455 myCanvas = this.canvas,
457 succeedingChildren = [],
459 prefix = this.getText().substr(0, params.offset),
460 suffix = this.getText().substr(params.offset);
462 parentElement.children().forEach(function(child) {
464 succeedingChildren.push(child);
465 if(child.sameNode(thisElement))
469 if(prefix.length > 0)
470 this.setText(prefix);
474 var newElement = DocumentNodeElement.create({tag: parentElement.getWlxmlTag(), klass: parentElement.getWlxmlClass()}, myCanvas);
475 parentElement.after(newElement);
477 if(suffix.length > 0)
478 newElement.append({text: suffix});
479 succeedingChildren.forEach(function(child) {
480 newElement.append(child);
483 return {first: parentElement, second: newElement};
485 divide: function(params) {
486 var myText = this.getText();
488 if(params.offset === myText.length)
489 return this.after(params);
490 if(params.offset === 0)
491 return this.before(params);
493 var lhsText = myText.substr(0, params.offset),
494 rhsText = myText.substr(params.offset),
495 newElement = DocumentNodeElement.create({tag: params.tag, klass: params.klass}, this.canvas),
496 rhsTextElement = DocumentTextElement.create({text: rhsText});
498 this.setText(lhsText);
499 this.after(newElement);
500 newElement.after(rhsTextElement);
506 DocumentElement: DocumentElement,
507 DocumentNodeElement: DocumentNodeElement,
508 DocumentTextElement: DocumentTextElement