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]);
128 create: function(params, canvas) {
129 return this.fromHTMLElement(this.createDOM(params)[0]);
132 fromHTMLElement: function(htmlElement, canvas) {
133 return new this(htmlElement, canvas);
137 var manipulate = function(e, params, action) {
139 if(params instanceof DocumentElement) {
142 element = DocumentElement.create(params);
144 e.dom()[action](element.dom());
148 DocumentNodeElement.prototype = new DocumentElement();
150 $.extend(DocumentNodeElement.prototype, {
152 var dom = this.dom(),
153 args = Array.prototype.slice.call(arguments, 0);
154 if(args.length === 2 && args[1] === undefined)
155 return dom.removeData(args[1]);
156 return dom.data.apply(dom, arguments);
158 toXML: function(level) {
159 var node = $('<' + this.getWlxmlTag() + '>'),
161 if(this.getWlxmlClass())
162 node.attr('class', this.getWlxmlClass());
163 var meta = this.getWlxmlMetaAttrs();
164 meta.forEach(function(attr) {
165 node.attr('meta-' + attr.name, attr.value);
168 if(this.data('orig-before') !== undefined) {
169 toret.append(document.createTextNode(this.data('orig-before')));
170 } else if(level && this.getWlxmlTag() !== 'span') {
171 toret.append('\n' + (new Array(level * 2 + 1)).join(' '));
173 if(this.data('orig-append') !== undefined) {
174 node.append(this.data('orig-append'));
175 //toret = toret.prepend(document.createTextNode(this.data('orig-prepend')));
176 } else if(this.getWlxmlTag() !== 'span'){
177 node.append('\n' + (new Array(level * 2 + 1)).join(' '));
181 return toret.contents();
183 append: function(params) {
184 if(params.tag !== 'span')
185 this.data('orig-append', undefined);
186 return manipulate(this, params, 'append');
188 before: function(params) {
189 return manipulate(this, params, 'before');
192 after: function(params) {
193 return manipulate(this, params, 'after');
195 children: function() {
197 if(this instanceof DocumentTextElement)
201 var elementContent = this.dom().contents();
203 elementContent.each(function(idx) {
204 var childElement = DocumentElement.fromHTMLElement(this, element.canvas);
205 if(idx === 0 && elementContent.length > 1 && elementContent[1].nodeType === Node.ELEMENT_NODE && (childElement instanceof DocumentTextElement) && $.trim($(this).text()) === '')
207 if(idx > 0 && childElement instanceof DocumentTextElement) {
208 if(toret[toret.length-1] instanceof DocumentNodeElement && $.trim($(this).text()) === '')
211 toret.push(childElement);
215 childIndex: function(child) {
216 var children = this.children(),
218 children.forEach(function(c, idx) {
219 if(c.sameNode(child)) {
226 getWlxmlTag: function() {
227 return this.dom().attr('wlxml-tag');
229 setWlxmlTag: function(tag) {
230 this.dom().attr('wlxml-tag', tag);
232 getWlxmlClass: function() {
233 var klass = this.dom().attr('wlxml-class');
235 return klass.replace(/-/g, '.');
238 setWlxmlClass: function(klass) {
239 this.getWlxmlMetaAttrs().forEach(function(attr) {
240 if(!classAttributes.hasMetaAttr(klass, attr.name))
241 this.dom().removeAttr('wlxml-meta-' + attr.name);
245 this.dom().attr('wlxml-class', klass.replace(/\./g, '-'));
247 this.dom().removeAttr('wlxml-class');
250 if(what === 'list' && _.contains(['list-items', 'list-items-enum'], this.dom().attr('wlxml-class')))
256 getWlxmlMetaAttr: function(attr) {
257 return this.dom().attr('wlxml-meta-'+attr);
259 getWlxmlMetaAttrs: function() {
261 var attrList = classAttributes.getMetaAttrsList(this.getWlxmlClass());
262 attrList.all.forEach(function(attr) {
263 toret.push({name: attr.name, value: this.getWlxmlMetaAttr(attr.name) || ''});
267 setWlxmlMetaAttr: function(attr, value) {
268 this.dom().attr('wlxml-meta-'+attr, value);
273 // DocumentNodeElement represents a text node from WLXML document rendered inside Canvas
274 var DocumentTextElement = function(htmlElement, canvas) {
275 DocumentElement.call(this, htmlElement, canvas);
278 $.extend(DocumentTextElement, {
279 createDOM: function(params) {
281 .attr('wlxml-text', '')
285 create: function(params, canvas) {
286 return this.fromHTMLElement(this.createDOM(params)[0]);
289 fromHTMLElement: function(htmlElement, canvas) {
290 return new this(htmlElement, canvas);
294 DocumentTextElement.prototype = new DocumentElement();
296 $.extend(DocumentTextElement.prototype, {
298 return this.getText();
300 _setupDOMHandler: function(htmlElement) {
301 var $element = $(htmlElement);
302 if(htmlElement.nodeType === Node.TEXT_NODE)
303 this.$element = $element.parent();
305 this.$element = $element;
307 setText: function(text) {
308 this.dom().contents()[0].data = text;
310 getText: function() {
311 return this.dom().text();
313 after: function(params) {
314 if(params instanceof DocumentTextElement || params.text)
317 if(params instanceof DocumentNodeElement) {
320 element = DocumentNodeElement.create(params);
322 this.dom().wrap('<div>');
323 this.dom().parent().after(element.dom());
327 before: function(params) {
328 if(params instanceof DocumentTextElement || params.text)
331 if(params instanceof DocumentNodeElement) {
334 element = DocumentNodeElement.create(params);
336 this.dom().wrap('<div>');
337 this.dom().parent().before(element.dom());
341 wrapWithNodeElement: function(wlxmlNode) {
342 if(typeof wlxmlNode.start === 'number' && typeof wlxmlNode.end === 'number') {
343 return this.canvas.wrapText({
344 inside: this.parent(),
345 textNodeIdx: this.parent().childIndex(this),
346 offsetStart: Math.min(wlxmlNode.start, wlxmlNode.end),
347 offsetEnd: Math.max(wlxmlNode.start, wlxmlNode.end),
348 _with: {tag: wlxmlNode.tag, klass: wlxmlNode.klass}
351 return DocumentElement.prototype.wrapWithNodeElement.call(this, wlxmlNode);
355 var parent = this.parent(),
357 if(parent.children().length === 1) {
358 toret = parent.parent();
359 var grandParent = parent.parent();
361 var grandParentChildren = grandParent.children(),
362 idx = grandParent.childIndex(parent),
363 prev = idx - 1 > -1 ? grandParentChildren[idx-1] : null,
364 next = idx + 1 < grandParentChildren.length ? grandParentChildren[idx+1] : null;
366 prev.setText(prev.getText() + this.getText() + next.getText());
368 } else if (prev || next) {
369 var target = prev ? prev : next;
370 target.setText(target.getText() + this.getText());
381 split: function(params) {
382 var parentElement = this.parent(),
383 myIdx = parentElement.childIndex(this),
384 myCanvas = this.canvas,
386 succeedingChildren = [],
388 prefix = this.getText().substr(0, params.offset),
389 suffix = this.getText().substr(params.offset);
391 parentElement.children().forEach(function(child) {
393 succeedingChildren.push(child);
394 if(child.sameNode(thisElement))
398 if(prefix.length > 0)
399 this.setText(prefix);
403 var newElement = DocumentNodeElement.create({tag: parentElement.getWlxmlTag(), klass: parentElement.getWlxmlClass()}, myCanvas);
404 parentElement.after(newElement);
406 if(suffix.length > 0)
407 newElement.append({text: suffix});
408 succeedingChildren.forEach(function(child) {
409 newElement.append(child);
412 return {first: parentElement, second: newElement};
417 DocumentElement: DocumentElement,
418 DocumentNodeElement: DocumentNodeElement,
419 DocumentTextElement: DocumentTextElement