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 $.extend(DocumentNodeElement.prototype, {
153 var dom = this.dom(),
154 args = Array.prototype.slice.call(arguments, 0);
155 if(args.length === 2 && args[1] === undefined)
156 return dom.removeData(args[1]);
157 return dom.data.apply(dom, arguments);
159 toXML: function(level) {
160 var node = $('<' + this.getWlxmlTag() + '>'),
162 if(this.getWlxmlClass())
163 node.attr('class', this.getWlxmlClass());
164 var meta = this.getWlxmlMetaAttrs();
165 meta.forEach(function(attr) {
167 node.attr('meta-' + attr.name, attr.value);
169 _.keys(this.data('other-attrs') || {}).forEach(function(key) {
170 node.attr(key, this.data('other-attrs')[key]);
173 if(this.data('orig-before') && this.data('orig-before').length) {
174 this.data('orig-before').forEach(function(toAdd) {
176 toret.prepend(document.createTextNode(toAdd));
178 } else if(level && this.getWlxmlTag() !== 'span') {
179 toret.append('\n' + (new Array(level * 2 + 1)).join(' '));
181 if(this.data('orig-append') && this.data('orig-append').length) {
182 this.data('orig-append').forEach(function(toAdd) {
186 //toret = toret.prepend(document.createTextNode(this.data('orig-prepend')));
187 } else if(this.getWlxmlTag() !== 'span'){
188 node.append('\n' + (new Array(level * 2 + 1)).join(' '));
192 return toret.contents();
194 append: function(params) {
195 if(params.tag !== 'span')
196 this.data('orig-append', []);
197 return manipulate(this, params, 'append');
199 before: function(params) {
200 return manipulate(this, params, 'before');
203 after: function(params) {
204 return manipulate(this, params, 'after');
206 children: function() {
208 if(this instanceof DocumentTextElement)
212 var elementContent = this.dom().contents();
214 elementContent.each(function(idx) {
215 var childElement = DocumentElement.fromHTMLElement(this, element.canvas);
216 if(idx === 0 && elementContent.length > 1 && elementContent[1].nodeType === Node.ELEMENT_NODE && (childElement instanceof DocumentTextElement) && $.trim($(this).text()) === '')
218 if(idx > 0 && childElement instanceof DocumentTextElement) {
219 if(toret[toret.length-1] instanceof DocumentNodeElement && $.trim($(this).text()) === '')
222 toret.push(childElement);
226 childIndex: function(child) {
227 var children = this.children(),
229 children.forEach(function(c, idx) {
230 if(c.sameNode(child)) {
237 getWlxmlTag: function() {
238 return this.dom().attr('wlxml-tag');
240 setWlxmlTag: function(tag) {
241 this.dom().attr('wlxml-tag', tag);
243 getWlxmlClass: function() {
244 var klass = this.dom().attr('wlxml-class');
246 return klass.replace(/-/g, '.');
249 setWlxmlClass: function(klass) {
250 this.getWlxmlMetaAttrs().forEach(function(attr) {
251 if(!classAttributes.hasMetaAttr(klass, attr.name))
252 this.dom().removeAttr('wlxml-meta-' + attr.name);
256 this.dom().attr('wlxml-class', klass.replace(/\./g, '-'));
258 this.dom().removeAttr('wlxml-class');
261 if(what === 'list' && _.contains(['list-items', 'list-items-enum'], this.dom().attr('wlxml-class')))
267 getWlxmlMetaAttr: function(attr) {
268 return this.dom().attr('wlxml-meta-'+attr);
270 getWlxmlMetaAttrs: function() {
272 var attrList = classAttributes.getMetaAttrsList(this.getWlxmlClass());
273 attrList.all.forEach(function(attr) {
274 toret.push({name: attr.name, value: this.getWlxmlMetaAttr(attr.name) || ''});
278 setWlxmlMetaAttr: function(attr, value) {
279 this.dom().attr('wlxml-meta-'+attr, value);
284 // DocumentNodeElement represents a text node from WLXML document rendered inside Canvas
285 var DocumentTextElement = function(htmlElement, canvas) {
286 DocumentElement.call(this, htmlElement, canvas);
289 $.extend(DocumentTextElement, {
290 createDOM: function(params) {
292 .attr('wlxml-text', '')
296 create: function(params, canvas) {
297 return this.fromHTMLElement(this.createDOM(params)[0]);
300 fromHTMLElement: function(htmlElement, canvas) {
301 return new this(htmlElement, canvas);
305 DocumentTextElement.prototype = new DocumentElement();
307 $.extend(DocumentTextElement.prototype, {
309 return this.getText();
311 _setupDOMHandler: function(htmlElement) {
312 var $element = $(htmlElement);
313 if(htmlElement.nodeType === Node.TEXT_NODE)
314 this.$element = $element.parent();
316 this.$element = $element;
318 setText: function(text) {
319 this.dom().contents()[0].data = text;
321 getText: function() {
322 return this.dom().text();
324 after: function(params) {
325 if(params instanceof DocumentTextElement || params.text)
328 if(params instanceof DocumentNodeElement) {
331 element = DocumentNodeElement.create(params);
333 this.dom().wrap('<div>');
334 this.dom().parent().after(element.dom());
338 before: function(params) {
339 if(params instanceof DocumentTextElement || params.text)
342 if(params instanceof DocumentNodeElement) {
345 element = DocumentNodeElement.create(params);
347 this.dom().wrap('<div>');
348 this.dom().parent().before(element.dom());
352 wrapWithNodeElement: function(wlxmlNode) {
353 if(typeof wlxmlNode.start === 'number' && typeof wlxmlNode.end === 'number') {
354 return this.canvas.wrapText({
355 inside: this.parent(),
356 textNodeIdx: this.parent().childIndex(this),
357 offsetStart: Math.min(wlxmlNode.start, wlxmlNode.end),
358 offsetEnd: Math.max(wlxmlNode.start, wlxmlNode.end),
359 _with: {tag: wlxmlNode.tag, klass: wlxmlNode.klass}
362 return DocumentElement.prototype.wrapWithNodeElement.call(this, wlxmlNode);
366 var parent = this.parent(),
368 if(parent.children().length === 1) {
369 toret = parent.parent();
370 var grandParent = parent.parent();
372 var grandParentChildren = grandParent.children(),
373 idx = grandParent.childIndex(parent),
374 prev = idx - 1 > -1 ? grandParentChildren[idx-1] : null,
375 next = idx + 1 < grandParentChildren.length ? grandParentChildren[idx+1] : null;
377 prev.setText(prev.getText() + this.getText() + next.getText());
379 } else if (prev || next) {
380 var target = prev ? prev : next;
381 target.setText(target.getText() + this.getText());
392 split: function(params) {
393 var parentElement = this.parent(),
394 myIdx = parentElement.childIndex(this),
395 myCanvas = this.canvas,
397 succeedingChildren = [],
399 prefix = this.getText().substr(0, params.offset),
400 suffix = this.getText().substr(params.offset);
402 parentElement.children().forEach(function(child) {
404 succeedingChildren.push(child);
405 if(child.sameNode(thisElement))
409 if(prefix.length > 0)
410 this.setText(prefix);
414 var newElement = DocumentNodeElement.create({tag: parentElement.getWlxmlTag(), klass: parentElement.getWlxmlClass()}, myCanvas);
415 parentElement.after(newElement);
417 if(suffix.length > 0)
418 newElement.append({text: suffix});
419 succeedingChildren.forEach(function(child) {
420 newElement.append(child);
423 return {first: parentElement, second: newElement};
428 DocumentElement: DocumentElement,
429 DocumentNodeElement: DocumentNodeElement,
430 DocumentTextElement: DocumentTextElement