2 'libs/jquery-1.9.1.min',
4 'modules/documentCanvas/classAttributes'
5 ], function($, _, classAttributes) {
10 // DocumentElement represents a text or an element node from WLXML document rendered inside Canvas
11 var DocumentElement = function(htmlElement, canvas) {
12 if(arguments.length === 0)
15 this._setupDOMHandler(htmlElement);
18 var elementTypeFromParams = function(params) {
19 return params.text !== undefined ? DocumentTextElement : DocumentNodeElement;
23 $.extend(DocumentElement, {
24 create: function(params, canvas) {
25 return elementTypeFromParams(params).create(params);
28 createDOM: function(params) {
29 return elementTypeFromParams(params).createDOM(params);
32 fromHTMLElement: function(htmlElement, canvas) {
33 var $element = $(htmlElement);
34 if(htmlElement.nodeType === Node.ELEMENT_NODE && $element.attr('wlxml-tag'))
35 return DocumentNodeElement.fromHTMLElement(htmlElement, canvas);
36 if($element.attr('wlxml-text') !== undefined || (htmlElement.nodeType === Node.TEXT_NODE && $element.parent().attr('wlxml-text') !== undefined))
37 return DocumentTextElement.fromHTMLElement(htmlElement, canvas);
42 $.extend(DocumentElement.prototype, {
43 _setupDOMHandler: function(htmlElement) {
44 this.$element = $(htmlElement);
50 var parents = this.$element.parents('[wlxml-tag]');
52 return DocumentElement.fromHTMLElement(parents[0], this.canvas);
58 parent = this.parent();
61 parent = parent.parent();
66 sameNode: function(other) {
67 return other && (typeof other === typeof this) && other.dom()[0] === this.dom()[0];
70 wrapWithNodeElement: function(wlxmlNode) {
71 var wrapper = DocumentNodeElement.create({tag: wlxmlNode.tag, klass: wlxmlNode.klass});
72 this.dom().replaceWith(wrapper.dom());
82 markAsCurrent: function() {
83 this.canvas.markAsCurrent(this);
86 getVerticallyFirstTextElement: function() {
88 this.children().some(function(child) {
89 if(!child.isVisible())
90 return false; // continue
91 if(child instanceof DocumentTextElement) {
95 toret = child.getVerticallyFirstTextElement();
103 isVisible: function() {
104 return this instanceof DocumentTextElement || this.getWlxmlTag() !== 'metadata';
109 // DocumentNodeElement represents an element node from WLXML document rendered inside Canvas
110 var DocumentNodeElement = function(htmlElement, canvas) {
111 DocumentElement.call(this, htmlElement, canvas);
114 $.extend(DocumentNodeElement, {
115 createDOM: function(params) {
117 .attr('wlxml-tag', params.tag);
119 dom.attr('wlxml-class', params.klass.replace(/\./g, '-'));
121 _.keys(params.meta).forEach(function(key) {
122 dom.attr('wlxml-meta-'+key, params.meta[key]);
125 dom.data('other-attrs', params.others);
129 create: function(params, canvas) {
130 return this.fromHTMLElement(this.createDOM(params)[0]);
133 fromHTMLElement: function(htmlElement, canvas) {
134 return new this(htmlElement, canvas);
138 var manipulate = function(e, params, action) {
140 if(params instanceof DocumentElement) {
143 element = DocumentElement.create(params);
145 e.dom()[action](element.dom());
149 DocumentNodeElement.prototype = new DocumentElement();
152 $.extend(DocumentNodeElement.prototype, {
154 var dom = this.dom(),
155 args = Array.prototype.slice.call(arguments, 0);
156 if(args.length === 2 && args[1] === undefined)
157 return dom.removeData(args[1]);
158 return dom.data.apply(dom, arguments);
160 toXML: function(level) {
161 var node = $('<' + this.getWlxmlTag() + '>');
163 if(this.getWlxmlClass())
164 node.attr('class', this.getWlxmlClass());
165 var meta = this.getWlxmlMetaAttrs();
166 meta.forEach(function(attr) {
168 node.attr('meta-' + attr.name, attr.value);
170 _.keys(this.data('other-attrs') || {}).forEach(function(key) {
171 node.attr(key, this.data('other-attrs')[key]);
174 var addFormatting = function() {
175 var toret = $('<div>');
177 if(this.data('orig-before') && this.data('orig-before').length) {
178 this.data('orig-before').forEach(function(toAdd) {
180 toret.prepend(document.createTextNode(toAdd));
182 } else if(level && this.getWlxmlTag() !== 'span') {
183 toret.append('\n' + (new Array(level * 2 + 1)).join(' '));
185 if(this.data('orig-append') && this.data('orig-append').length) {
186 this.data('orig-append').forEach(function(toAdd) {
190 } else if(this.getWlxmlTag() !== 'span'){
191 node.append('\n' + (new Array(level * 2 + 1)).join(' '));
194 return toret.contents();
197 var parts = addFormatting(node);
199 var children = this.children(),
203 for(var i = children.length - 1; i >= 0; i--) {
204 childParts = children[i].toXML(level + 1);
206 if(i === children.length - 1 && node.contents().length === 2) {
207 $(node.contents()[0]).after(childParts);
208 prevChildParts = childParts;
212 if(prevChildParts && prevChildParts.length > 1 && prevChildParts[0].nodeType === Node.TEXT_NODE && prevChildParts[1].nodeType === Node.TEXT_NODE) {
213 $(node.contents()[0]).after(childParts);
214 prevChildParts = childParts;
218 node.prepend(childParts);
220 prevChildParts = childParts;
224 append: function(params) {
225 if(params.tag !== 'span')
226 this.data('orig-append', []);
227 return manipulate(this, params, 'append');
229 before: function(params) {
230 return manipulate(this, params, 'before');
233 after: function(params) {
234 return manipulate(this, params, 'after');
236 children: function() {
238 if(this instanceof DocumentTextElement)
242 var elementContent = this.dom().contents();
244 elementContent.each(function(idx) {
245 var childElement = DocumentElement.fromHTMLElement(this, element.canvas);
246 if(idx === 0 && elementContent.length > 1 && elementContent[1].nodeType === Node.ELEMENT_NODE && (childElement instanceof DocumentTextElement) && $.trim($(this).text()) === '')
248 if(idx > 0 && childElement instanceof DocumentTextElement) {
249 if(toret[toret.length-1] instanceof DocumentNodeElement && $.trim($(this).text()) === '')
252 toret.push(childElement);
256 childIndex: function(child) {
257 var children = this.children(),
259 children.forEach(function(c, idx) {
260 if(c.sameNode(child)) {
267 getWlxmlTag: function() {
268 return this.dom().attr('wlxml-tag');
270 setWlxmlTag: function(tag) {
271 this.dom().attr('wlxml-tag', tag);
273 getWlxmlClass: function() {
274 var klass = this.dom().attr('wlxml-class');
276 return klass.replace(/-/g, '.');
279 setWlxmlClass: function(klass) {
280 this.getWlxmlMetaAttrs().forEach(function(attr) {
281 if(!classAttributes.hasMetaAttr(klass, attr.name))
282 this.dom().removeAttr('wlxml-meta-' + attr.name);
286 this.dom().attr('wlxml-class', klass.replace(/\./g, '-'));
288 this.dom().removeAttr('wlxml-class');
291 if(what === 'list' && _.contains(['list-items', 'list-items-enum'], this.dom().attr('wlxml-class')))
297 getWlxmlMetaAttr: function(attr) {
298 return this.dom().attr('wlxml-meta-'+attr);
300 getWlxmlMetaAttrs: function() {
302 var attrList = classAttributes.getMetaAttrsList(this.getWlxmlClass());
303 attrList.all.forEach(function(attr) {
304 toret.push({name: attr.name, value: this.getWlxmlMetaAttr(attr.name) || ''});
308 setWlxmlMetaAttr: function(attr, value) {
309 this.dom().attr('wlxml-meta-'+attr, value);
314 // DocumentNodeElement represents a text node from WLXML document rendered inside Canvas
315 var DocumentTextElement = function(htmlElement, canvas) {
316 DocumentElement.call(this, htmlElement, canvas);
319 $.extend(DocumentTextElement, {
320 createDOM: function(params) {
322 .attr('wlxml-text', '')
326 create: function(params, canvas) {
327 return this.fromHTMLElement(this.createDOM(params)[0]);
330 fromHTMLElement: function(htmlElement, canvas) {
331 return new this(htmlElement, canvas);
335 DocumentTextElement.prototype = new DocumentElement();
337 $.extend(DocumentTextElement.prototype, {
338 toXML: function(parent) {
339 return this.getText();
341 _setupDOMHandler: function(htmlElement) {
342 var $element = $(htmlElement);
343 if(htmlElement.nodeType === Node.TEXT_NODE)
344 this.$element = $element.parent();
346 this.$element = $element;
348 setText: function(text) {
349 this.dom().contents()[0].data = text;
351 getText: function() {
352 return this.dom().text();
354 after: function(params) {
355 if(params instanceof DocumentTextElement || params.text)
358 if(params instanceof DocumentNodeElement) {
361 element = DocumentNodeElement.create(params);
363 this.dom().wrap('<div>');
364 this.dom().parent().after(element.dom());
368 before: function(params) {
369 if(params instanceof DocumentTextElement || params.text)
372 if(params instanceof DocumentNodeElement) {
375 element = DocumentNodeElement.create(params);
377 this.dom().wrap('<div>');
378 this.dom().parent().before(element.dom());
382 wrapWithNodeElement: function(wlxmlNode) {
383 if(typeof wlxmlNode.start === 'number' && typeof wlxmlNode.end === 'number') {
384 return this.canvas.wrapText({
385 inside: this.parent(),
386 textNodeIdx: this.parent().childIndex(this),
387 offsetStart: Math.min(wlxmlNode.start, wlxmlNode.end),
388 offsetEnd: Math.max(wlxmlNode.start, wlxmlNode.end),
389 _with: {tag: wlxmlNode.tag, klass: wlxmlNode.klass}
392 return DocumentElement.prototype.wrapWithNodeElement.call(this, wlxmlNode);
396 var parent = this.parent(),
398 if(parent.children().length === 1) {
399 toret = parent.parent();
400 var grandParent = parent.parent();
402 var grandParentChildren = grandParent.children(),
403 idx = grandParent.childIndex(parent),
404 prev = idx - 1 > -1 ? grandParentChildren[idx-1] : null,
405 next = idx + 1 < grandParentChildren.length ? grandParentChildren[idx+1] : null;
407 prev.setText(prev.getText() + this.getText() + next.getText());
409 } else if (prev || next) {
410 var target = prev ? prev : next;
411 target.setText(target.getText() + this.getText());
422 split: function(params) {
423 var parentElement = this.parent(),
424 myIdx = parentElement.childIndex(this),
425 myCanvas = this.canvas,
427 succeedingChildren = [],
429 prefix = this.getText().substr(0, params.offset),
430 suffix = this.getText().substr(params.offset);
432 parentElement.children().forEach(function(child) {
434 succeedingChildren.push(child);
435 if(child.sameNode(thisElement))
439 if(prefix.length > 0)
440 this.setText(prefix);
444 var newElement = DocumentNodeElement.create({tag: parentElement.getWlxmlTag(), klass: parentElement.getWlxmlClass()}, myCanvas);
445 parentElement.after(newElement);
447 if(suffix.length > 0)
448 newElement.append({text: suffix});
449 succeedingChildren.forEach(function(child) {
450 newElement.append(child);
453 return {first: parentElement, second: newElement};
458 DocumentElement: DocumentElement,
459 DocumentNodeElement: DocumentNodeElement,
460 DocumentTextElement: DocumentTextElement