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') !== undefined) {
174 toret.append(document.createTextNode(this.data('orig-before')));
175 } else if(level && this.getWlxmlTag() !== 'span') {
176 toret.append('\n' + (new Array(level * 2 + 1)).join(' '));
178 if(this.data('orig-append') !== undefined) {
179 node.append(this.data('orig-append'));
180 //toret = toret.prepend(document.createTextNode(this.data('orig-prepend')));
181 } else if(this.getWlxmlTag() !== 'span'){
182 node.append('\n' + (new Array(level * 2 + 1)).join(' '));
186 return toret.contents();
188 append: function(params) {
189 if(params.tag !== 'span')
190 this.data('orig-append', undefined);
191 return manipulate(this, params, 'append');
193 before: function(params) {
194 return manipulate(this, params, 'before');
197 after: function(params) {
198 return manipulate(this, params, 'after');
200 children: function() {
202 if(this instanceof DocumentTextElement)
206 var elementContent = this.dom().contents();
208 elementContent.each(function(idx) {
209 var childElement = DocumentElement.fromHTMLElement(this, element.canvas);
210 if(idx === 0 && elementContent.length > 1 && elementContent[1].nodeType === Node.ELEMENT_NODE && (childElement instanceof DocumentTextElement) && $.trim($(this).text()) === '')
212 if(idx > 0 && childElement instanceof DocumentTextElement) {
213 if(toret[toret.length-1] instanceof DocumentNodeElement && $.trim($(this).text()) === '')
216 toret.push(childElement);
220 childIndex: function(child) {
221 var children = this.children(),
223 children.forEach(function(c, idx) {
224 if(c.sameNode(child)) {
231 getWlxmlTag: function() {
232 return this.dom().attr('wlxml-tag');
234 setWlxmlTag: function(tag) {
235 this.dom().attr('wlxml-tag', tag);
237 getWlxmlClass: function() {
238 var klass = this.dom().attr('wlxml-class');
240 return klass.replace(/-/g, '.');
243 setWlxmlClass: function(klass) {
244 this.getWlxmlMetaAttrs().forEach(function(attr) {
245 if(!classAttributes.hasMetaAttr(klass, attr.name))
246 this.dom().removeAttr('wlxml-meta-' + attr.name);
250 this.dom().attr('wlxml-class', klass.replace(/\./g, '-'));
252 this.dom().removeAttr('wlxml-class');
255 if(what === 'list' && _.contains(['list-items', 'list-items-enum'], this.dom().attr('wlxml-class')))
261 getWlxmlMetaAttr: function(attr) {
262 return this.dom().attr('wlxml-meta-'+attr);
264 getWlxmlMetaAttrs: function() {
266 var attrList = classAttributes.getMetaAttrsList(this.getWlxmlClass());
267 attrList.all.forEach(function(attr) {
268 toret.push({name: attr.name, value: this.getWlxmlMetaAttr(attr.name) || ''});
272 setWlxmlMetaAttr: function(attr, value) {
273 this.dom().attr('wlxml-meta-'+attr, value);
278 // DocumentNodeElement represents a text node from WLXML document rendered inside Canvas
279 var DocumentTextElement = function(htmlElement, canvas) {
280 DocumentElement.call(this, htmlElement, canvas);
283 $.extend(DocumentTextElement, {
284 createDOM: function(params) {
286 .attr('wlxml-text', '')
290 create: function(params, canvas) {
291 return this.fromHTMLElement(this.createDOM(params)[0]);
294 fromHTMLElement: function(htmlElement, canvas) {
295 return new this(htmlElement, canvas);
299 DocumentTextElement.prototype = new DocumentElement();
301 $.extend(DocumentTextElement.prototype, {
303 return this.getText();
305 _setupDOMHandler: function(htmlElement) {
306 var $element = $(htmlElement);
307 if(htmlElement.nodeType === Node.TEXT_NODE)
308 this.$element = $element.parent();
310 this.$element = $element;
312 setText: function(text) {
313 this.dom().contents()[0].data = text;
315 getText: function() {
316 return this.dom().text();
318 after: function(params) {
319 if(params instanceof DocumentTextElement || params.text)
322 if(params instanceof DocumentNodeElement) {
325 element = DocumentNodeElement.create(params);
327 this.dom().wrap('<div>');
328 this.dom().parent().after(element.dom());
332 before: function(params) {
333 if(params instanceof DocumentTextElement || params.text)
336 if(params instanceof DocumentNodeElement) {
339 element = DocumentNodeElement.create(params);
341 this.dom().wrap('<div>');
342 this.dom().parent().before(element.dom());
346 wrapWithNodeElement: function(wlxmlNode) {
347 if(typeof wlxmlNode.start === 'number' && typeof wlxmlNode.end === 'number') {
348 return this.canvas.wrapText({
349 inside: this.parent(),
350 textNodeIdx: this.parent().childIndex(this),
351 offsetStart: Math.min(wlxmlNode.start, wlxmlNode.end),
352 offsetEnd: Math.max(wlxmlNode.start, wlxmlNode.end),
353 _with: {tag: wlxmlNode.tag, klass: wlxmlNode.klass}
356 return DocumentElement.prototype.wrapWithNodeElement.call(this, wlxmlNode);
360 var parent = this.parent(),
362 if(parent.children().length === 1) {
363 toret = parent.parent();
364 var grandParent = parent.parent();
366 var grandParentChildren = grandParent.children(),
367 idx = grandParent.childIndex(parent),
368 prev = idx - 1 > -1 ? grandParentChildren[idx-1] : null,
369 next = idx + 1 < grandParentChildren.length ? grandParentChildren[idx+1] : null;
371 prev.setText(prev.getText() + this.getText() + next.getText());
373 } else if (prev || next) {
374 var target = prev ? prev : next;
375 target.setText(target.getText() + this.getText());
386 split: function(params) {
387 var parentElement = this.parent(),
388 myIdx = parentElement.childIndex(this),
389 myCanvas = this.canvas,
391 succeedingChildren = [],
393 prefix = this.getText().substr(0, params.offset),
394 suffix = this.getText().substr(params.offset);
396 parentElement.children().forEach(function(child) {
398 succeedingChildren.push(child);
399 if(child.sameNode(thisElement))
403 if(prefix.length > 0)
404 this.setText(prefix);
408 var newElement = DocumentNodeElement.create({tag: parentElement.getWlxmlTag(), klass: parentElement.getWlxmlClass()}, myCanvas);
409 parentElement.after(newElement);
411 if(suffix.length > 0)
412 newElement.append({text: suffix});
413 succeedingChildren.forEach(function(child) {
414 newElement.append(child);
417 return {first: parentElement, second: newElement};
422 DocumentElement: DocumentElement,
423 DocumentNodeElement: DocumentNodeElement,
424 DocumentTextElement: DocumentTextElement