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();
151 var addParts = function(parts, parent) {
152 var parentChildren = parent.contents();
153 if(parentChildren.length > 2 && parentChildren[0].nodeType === Node.TEXT_NODE && parentChildren[1].nodeType == Node.TEXT_NODE) {
154 $(parentChildren[0]).after(parts);
156 parent.prepend(parts);
160 $.extend(DocumentNodeElement.prototype, {
162 var dom = this.dom(),
163 args = Array.prototype.slice.call(arguments, 0);
164 if(args.length === 2 && args[1] === undefined)
165 return dom.removeData(args[1]);
166 return dom.data.apply(dom, arguments);
168 toXML: function(parent, level) {
169 var node = $('<' + this.getWlxmlTag() + '>');
171 if(this.getWlxmlClass())
172 node.attr('class', this.getWlxmlClass());
173 var meta = this.getWlxmlMetaAttrs();
174 meta.forEach(function(attr) {
176 node.attr('meta-' + attr.name, attr.value);
178 _.keys(this.data('other-attrs') || {}).forEach(function(key) {
179 node.attr(key, this.data('other-attrs')[key]);
182 var addFormatting = function() {
183 var toret = $('<div>');
185 if(this.data('orig-before') && this.data('orig-before').length) {
186 this.data('orig-before').forEach(function(toAdd) {
188 toret.prepend(document.createTextNode(toAdd));
190 } else if(level && this.getWlxmlTag() !== 'span') {
191 toret.append('\n' + (new Array(level * 2 + 1)).join(' '));
193 if(this.data('orig-append') && this.data('orig-append').length) {
194 this.data('orig-append').forEach(function(toAdd) {
198 } else if(this.getWlxmlTag() !== 'span'){
199 node.append('\n' + (new Array(level * 2 + 1)).join(' '));
202 return toret.contents();
205 var parts = addFormatting(node);
207 var children = this.children();
208 for(var i = children.length - 1; i >= 0; i--) {
209 children[i].toXML(node, level + 1);
212 addParts(parts, parent);
214 append: function(params) {
215 if(params.tag !== 'span')
216 this.data('orig-append', []);
217 return manipulate(this, params, 'append');
219 before: function(params) {
220 return manipulate(this, params, 'before');
223 after: function(params) {
224 return manipulate(this, params, 'after');
226 children: function() {
228 if(this instanceof DocumentTextElement)
232 var elementContent = this.dom().contents();
234 elementContent.each(function(idx) {
235 var childElement = DocumentElement.fromHTMLElement(this, element.canvas);
236 if(idx === 0 && elementContent.length > 1 && elementContent[1].nodeType === Node.ELEMENT_NODE && (childElement instanceof DocumentTextElement) && $.trim($(this).text()) === '')
238 if(idx > 0 && childElement instanceof DocumentTextElement) {
239 if(toret[toret.length-1] instanceof DocumentNodeElement && $.trim($(this).text()) === '')
242 toret.push(childElement);
246 childIndex: function(child) {
247 var children = this.children(),
249 children.forEach(function(c, idx) {
250 if(c.sameNode(child)) {
257 getWlxmlTag: function() {
258 return this.dom().attr('wlxml-tag');
260 setWlxmlTag: function(tag) {
261 this.dom().attr('wlxml-tag', tag);
263 getWlxmlClass: function() {
264 var klass = this.dom().attr('wlxml-class');
266 return klass.replace(/-/g, '.');
269 setWlxmlClass: function(klass) {
270 this.getWlxmlMetaAttrs().forEach(function(attr) {
271 if(!classAttributes.hasMetaAttr(klass, attr.name))
272 this.dom().removeAttr('wlxml-meta-' + attr.name);
276 this.dom().attr('wlxml-class', klass.replace(/\./g, '-'));
278 this.dom().removeAttr('wlxml-class');
281 if(what === 'list' && _.contains(['list-items', 'list-items-enum'], this.dom().attr('wlxml-class')))
287 getWlxmlMetaAttr: function(attr) {
288 return this.dom().attr('wlxml-meta-'+attr);
290 getWlxmlMetaAttrs: function() {
292 var attrList = classAttributes.getMetaAttrsList(this.getWlxmlClass());
293 attrList.all.forEach(function(attr) {
294 toret.push({name: attr.name, value: this.getWlxmlMetaAttr(attr.name) || ''});
298 setWlxmlMetaAttr: function(attr, value) {
299 this.dom().attr('wlxml-meta-'+attr, value);
304 // DocumentNodeElement represents a text node from WLXML document rendered inside Canvas
305 var DocumentTextElement = function(htmlElement, canvas) {
306 DocumentElement.call(this, htmlElement, canvas);
309 $.extend(DocumentTextElement, {
310 createDOM: function(params) {
312 .attr('wlxml-text', '')
316 create: function(params, canvas) {
317 return this.fromHTMLElement(this.createDOM(params)[0]);
320 fromHTMLElement: function(htmlElement, canvas) {
321 return new this(htmlElement, canvas);
325 DocumentTextElement.prototype = new DocumentElement();
327 $.extend(DocumentTextElement.prototype, {
328 toXML: function(parent) {
329 addParts(this.getText(), parent);
331 _setupDOMHandler: function(htmlElement) {
332 var $element = $(htmlElement);
333 if(htmlElement.nodeType === Node.TEXT_NODE)
334 this.$element = $element.parent();
336 this.$element = $element;
338 setText: function(text) {
339 this.dom().contents()[0].data = text;
341 getText: function() {
342 return this.dom().text();
344 after: function(params) {
345 if(params instanceof DocumentTextElement || params.text)
348 if(params instanceof DocumentNodeElement) {
351 element = DocumentNodeElement.create(params);
353 this.dom().wrap('<div>');
354 this.dom().parent().after(element.dom());
358 before: function(params) {
359 if(params instanceof DocumentTextElement || params.text)
362 if(params instanceof DocumentNodeElement) {
365 element = DocumentNodeElement.create(params);
367 this.dom().wrap('<div>');
368 this.dom().parent().before(element.dom());
372 wrapWithNodeElement: function(wlxmlNode) {
373 if(typeof wlxmlNode.start === 'number' && typeof wlxmlNode.end === 'number') {
374 return this.canvas.wrapText({
375 inside: this.parent(),
376 textNodeIdx: this.parent().childIndex(this),
377 offsetStart: Math.min(wlxmlNode.start, wlxmlNode.end),
378 offsetEnd: Math.max(wlxmlNode.start, wlxmlNode.end),
379 _with: {tag: wlxmlNode.tag, klass: wlxmlNode.klass}
382 return DocumentElement.prototype.wrapWithNodeElement.call(this, wlxmlNode);
386 var parent = this.parent(),
388 if(parent.children().length === 1) {
389 toret = parent.parent();
390 var grandParent = parent.parent();
392 var grandParentChildren = grandParent.children(),
393 idx = grandParent.childIndex(parent),
394 prev = idx - 1 > -1 ? grandParentChildren[idx-1] : null,
395 next = idx + 1 < grandParentChildren.length ? grandParentChildren[idx+1] : null;
397 prev.setText(prev.getText() + this.getText() + next.getText());
399 } else if (prev || next) {
400 var target = prev ? prev : next;
401 target.setText(target.getText() + this.getText());
412 split: function(params) {
413 var parentElement = this.parent(),
414 myIdx = parentElement.childIndex(this),
415 myCanvas = this.canvas,
417 succeedingChildren = [],
419 prefix = this.getText().substr(0, params.offset),
420 suffix = this.getText().substr(params.offset);
422 parentElement.children().forEach(function(child) {
424 succeedingChildren.push(child);
425 if(child.sameNode(thisElement))
429 if(prefix.length > 0)
430 this.setText(prefix);
434 var newElement = DocumentNodeElement.create({tag: parentElement.getWlxmlTag(), klass: parentElement.getWlxmlClass()}, myCanvas);
435 parentElement.after(newElement);
437 if(suffix.length > 0)
438 newElement.append({text: suffix});
439 succeedingChildren.forEach(function(child) {
440 newElement.append(child);
443 return {first: parentElement, second: newElement};
448 DocumentElement: DocumentElement,
449 DocumentNodeElement: DocumentNodeElement,
450 DocumentTextElement: DocumentTextElement