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 var containsPrefixAndSuffix = function(idx) {
204 if(idx === children.length - 1 && node.contents().length === 2)
206 if(prevChildParts && prevChildParts.length > 1 && prevChildParts[0].nodeType === Node.TEXT_NODE && prevChildParts[1].nodeType === Node.TEXT_NODE)
211 for(var i = children.length - 1; i >= 0; i--) {
212 childParts = children[i].toXML(level + 1);
214 if(containsPrefixAndSuffix(i) && children[i] instanceof DocumentTextElement) {
215 $(node.contents()[0]).after(childParts);
217 node.prepend(childParts);
219 prevChildParts = childParts;
223 append: function(params) {
224 if(params.tag !== 'span')
225 this.data('orig-append', []);
226 return manipulate(this, params, 'append');
228 before: function(params) {
229 return manipulate(this, params, 'before');
232 after: function(params) {
233 return manipulate(this, params, 'after');
235 children: function() {
237 if(this instanceof DocumentTextElement)
241 var elementContent = this.dom().contents();
243 elementContent.each(function(idx) {
244 var childElement = DocumentElement.fromHTMLElement(this, element.canvas);
245 if(idx === 0 && elementContent.length > 1 && elementContent[1].nodeType === Node.ELEMENT_NODE && (childElement instanceof DocumentTextElement) && $.trim($(this).text()) === '')
247 if(idx > 0 && childElement instanceof DocumentTextElement) {
248 if(toret[toret.length-1] instanceof DocumentNodeElement && $.trim($(this).text()) === '')
251 toret.push(childElement);
255 childIndex: function(child) {
256 var children = this.children(),
258 children.forEach(function(c, idx) {
259 if(c.sameNode(child)) {
266 getWlxmlTag: function() {
267 return this.dom().attr('wlxml-tag');
269 setWlxmlTag: function(tag) {
270 this.dom().attr('wlxml-tag', tag);
272 getWlxmlClass: function() {
273 var klass = this.dom().attr('wlxml-class');
275 return klass.replace(/-/g, '.');
278 setWlxmlClass: function(klass) {
279 this.getWlxmlMetaAttrs().forEach(function(attr) {
280 if(!classAttributes.hasMetaAttr(klass, attr.name))
281 this.dom().removeAttr('wlxml-meta-' + attr.name);
285 this.dom().attr('wlxml-class', klass.replace(/\./g, '-'));
287 this.dom().removeAttr('wlxml-class');
290 if(what === 'list' && _.contains(['list-items', 'list-items-enum'], this.dom().attr('wlxml-class')))
296 getWlxmlMetaAttr: function(attr) {
297 return this.dom().attr('wlxml-meta-'+attr);
299 getWlxmlMetaAttrs: function() {
301 var attrList = classAttributes.getMetaAttrsList(this.getWlxmlClass());
302 attrList.all.forEach(function(attr) {
303 toret.push({name: attr.name, value: this.getWlxmlMetaAttr(attr.name) || ''});
307 setWlxmlMetaAttr: function(attr, value) {
308 this.dom().attr('wlxml-meta-'+attr, value);
313 // DocumentNodeElement represents a text node from WLXML document rendered inside Canvas
314 var DocumentTextElement = function(htmlElement, canvas) {
315 DocumentElement.call(this, htmlElement, canvas);
318 $.extend(DocumentTextElement, {
319 createDOM: function(params) {
321 .attr('wlxml-text', '')
325 create: function(params, canvas) {
326 return this.fromHTMLElement(this.createDOM(params)[0]);
329 fromHTMLElement: function(htmlElement, canvas) {
330 return new this(htmlElement, canvas);
334 DocumentTextElement.prototype = new DocumentElement();
336 $.extend(DocumentTextElement.prototype, {
337 toXML: function(parent) {
338 return this.getText();
340 _setupDOMHandler: function(htmlElement) {
341 var $element = $(htmlElement);
342 if(htmlElement.nodeType === Node.TEXT_NODE)
343 this.$element = $element.parent();
345 this.$element = $element;
347 setText: function(text) {
348 this.dom().contents()[0].data = text;
350 getText: function() {
351 return this.dom().text();
353 after: function(params) {
354 if(params instanceof DocumentTextElement || params.text)
357 if(params instanceof DocumentNodeElement) {
360 element = DocumentNodeElement.create(params);
362 this.dom().wrap('<div>');
363 this.dom().parent().after(element.dom());
367 before: function(params) {
368 if(params instanceof DocumentTextElement || params.text)
371 if(params instanceof DocumentNodeElement) {
374 element = DocumentNodeElement.create(params);
376 this.dom().wrap('<div>');
377 this.dom().parent().before(element.dom());
381 wrapWithNodeElement: function(wlxmlNode) {
382 if(typeof wlxmlNode.start === 'number' && typeof wlxmlNode.end === 'number') {
383 return this.canvas.wrapText({
384 inside: this.parent(),
385 textNodeIdx: this.parent().childIndex(this),
386 offsetStart: Math.min(wlxmlNode.start, wlxmlNode.end),
387 offsetEnd: Math.max(wlxmlNode.start, wlxmlNode.end),
388 _with: {tag: wlxmlNode.tag, klass: wlxmlNode.klass}
391 return DocumentElement.prototype.wrapWithNodeElement.call(this, wlxmlNode);
395 var parent = this.parent(),
397 if(parent.children().length === 1) {
398 toret = parent.parent();
399 var grandParent = parent.parent();
401 var grandParentChildren = grandParent.children(),
402 idx = grandParent.childIndex(parent),
403 prev = idx - 1 > -1 ? grandParentChildren[idx-1] : null,
404 next = idx + 1 < grandParentChildren.length ? grandParentChildren[idx+1] : null;
406 prev.setText(prev.getText() + this.getText() + next.getText());
408 } else if (prev || next) {
409 var target = prev ? prev : next;
410 target.setText(target.getText() + this.getText());
421 split: function(params) {
422 var parentElement = this.parent(),
423 myIdx = parentElement.childIndex(this),
424 myCanvas = this.canvas,
426 succeedingChildren = [],
428 prefix = this.getText().substr(0, params.offset),
429 suffix = this.getText().substr(params.offset);
431 parentElement.children().forEach(function(child) {
433 succeedingChildren.push(child);
434 if(child.sameNode(thisElement))
438 if(prefix.length > 0)
439 this.setText(prefix);
443 var newElement = DocumentNodeElement.create({tag: parentElement.getWlxmlTag(), klass: parentElement.getWlxmlClass()}, myCanvas);
444 parentElement.after(newElement);
446 if(suffix.length > 0)
447 newElement.append({text: suffix});
448 succeedingChildren.forEach(function(child) {
449 newElement.append(child);
452 return {first: parentElement, second: newElement};
457 DocumentElement: DocumentElement,
458 DocumentNodeElement: DocumentNodeElement,
459 DocumentTextElement: DocumentTextElement