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[0]);
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>');
176 var formattings = {};
178 if(this.data('orig-before') !== undefined) {
179 if(this.data('orig-before')) {
180 toret.prepend(document.createTextNode(this.data('orig-before')));
182 } else if(level && this.getWlxmlTag() !== 'span') {
183 toret.append('\n' + (new Array(level * 2 + 1)).join(' '));
188 if(this.data('orig-after')) {
189 toret.append(document.createTextNode(this.data('orig-after')));
193 if(this.data('orig-begin')) {
194 node.prepend(this.data('orig-begin'));
195 formattings.begin = true;
198 if(this.data('orig-end') !== undefined) {
199 if(this.data('orig-end')) {
200 node.append(this.data('orig-end'));
202 } else if(this.getWlxmlTag() !== 'span' && children.length){
203 node.append('\n' + (new Array(level * 2 + 1)).join(' '));
206 return {parts: toret.contents(), formattings: formattings};
211 var children = this.children(),
214 var formatting = addFormatting(node);
216 for(var i = children.length - 1; i >= 0; i--) {
217 childParts = children[i].toXML(level + 1);
218 if(typeof childParts === 'string')
219 childParts = [document.createTextNode(childParts)];
221 if(formatting.formattings.begin) {
222 $(node.contents()[0]).after(childParts);
224 node.prepend(childParts);
226 return formatting.parts;
228 append: function(params) {
229 if(params.tag !== 'span')
230 this.data('orig-end', undefined);
231 return manipulate(this, params, 'append');
233 before: function(params) {
234 return manipulate(this, params, 'before');
237 after: function(params) {
238 return manipulate(this, params, 'after');
240 children: function() {
242 if(this instanceof DocumentTextElement)
246 var elementContent = this.dom().contents();
248 elementContent.each(function(idx) {
249 var childElement = DocumentElement.fromHTMLElement(this, element.canvas);
250 if(idx === 0 && elementContent.length > 1 && elementContent[1].nodeType === Node.ELEMENT_NODE && (childElement instanceof DocumentTextElement) && $.trim($(this).text()) === '')
252 if(idx > 0 && childElement instanceof DocumentTextElement) {
253 if(toret[toret.length-1] instanceof DocumentNodeElement && $.trim($(this).text()) === '')
256 toret.push(childElement);
260 childIndex: function(child) {
261 var children = this.children(),
263 children.forEach(function(c, idx) {
264 if(c.sameNode(child)) {
271 getWlxmlTag: function() {
272 return this.dom().attr('wlxml-tag');
274 setWlxmlTag: function(tag) {
275 this.dom().attr('wlxml-tag', tag);
277 getWlxmlClass: function() {
278 var klass = this.dom().attr('wlxml-class');
280 return klass.replace(/-/g, '.');
283 setWlxmlClass: function(klass) {
284 this.getWlxmlMetaAttrs().forEach(function(attr) {
285 if(!classAttributes.hasMetaAttr(klass, attr.name))
286 this.dom().removeAttr('wlxml-meta-' + attr.name);
290 this.dom().attr('wlxml-class', klass.replace(/\./g, '-'));
292 this.dom().removeAttr('wlxml-class');
295 if(what === 'list' && _.contains(['list.items', 'list.items.enum'], this.getWlxmlClass()))
301 getWlxmlMetaAttr: function(attr) {
302 return this.dom().attr('wlxml-meta-'+attr);
304 getWlxmlMetaAttrs: function() {
306 var attrList = classAttributes.getMetaAttrsList(this.getWlxmlClass());
307 attrList.all.forEach(function(attr) {
308 toret.push({name: attr.name, value: this.getWlxmlMetaAttr(attr.name) || ''});
312 setWlxmlMetaAttr: function(attr, value) {
313 this.dom().attr('wlxml-meta-'+attr, value);
318 // DocumentNodeElement represents a text node from WLXML document rendered inside Canvas
319 var DocumentTextElement = function(htmlElement, canvas) {
320 DocumentElement.call(this, htmlElement, canvas);
323 $.extend(DocumentTextElement, {
324 createDOM: function(params) {
326 .attr('wlxml-text', '')
330 create: function(params, canvas) {
331 return this.fromHTMLElement(this.createDOM(params)[0]);
334 fromHTMLElement: function(htmlElement, canvas) {
335 return new this(htmlElement, canvas);
339 DocumentTextElement.prototype = new DocumentElement();
341 $.extend(DocumentTextElement.prototype, {
342 toXML: function(parent) {
343 return this.getText();
345 _setupDOMHandler: function(htmlElement) {
346 var $element = $(htmlElement);
347 if(htmlElement.nodeType === Node.TEXT_NODE)
348 this.$element = $element.parent();
350 this.$element = $element;
352 setText: function(text) {
353 this.dom().contents()[0].data = text;
355 getText: function() {
356 return this.dom().text();
358 after: 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().after(element.dom());
372 before: function(params) {
373 if(params instanceof DocumentTextElement || params.text)
376 if(params instanceof DocumentNodeElement) {
379 element = DocumentNodeElement.create(params);
381 this.dom().wrap('<div>');
382 this.dom().parent().before(element.dom());
386 wrapWithNodeElement: function(wlxmlNode) {
387 if(typeof wlxmlNode.start === 'number' && typeof wlxmlNode.end === 'number') {
388 return this.canvas.wrapText({
389 inside: this.parent(),
390 textNodeIdx: this.parent().childIndex(this),
391 offsetStart: Math.min(wlxmlNode.start, wlxmlNode.end),
392 offsetEnd: Math.max(wlxmlNode.start, wlxmlNode.end),
393 _with: {tag: wlxmlNode.tag, klass: wlxmlNode.klass}
396 return DocumentElement.prototype.wrapWithNodeElement.call(this, wlxmlNode);
400 var parent = this.parent(),
402 if(parent.children().length === 1) {
403 toret = parent.parent();
404 var grandParent = parent.parent();
406 var grandParentChildren = grandParent.children(),
407 idx = grandParent.childIndex(parent),
408 prev = idx - 1 > -1 ? grandParentChildren[idx-1] : null,
409 next = idx + 1 < grandParentChildren.length ? grandParentChildren[idx+1] : null;
411 prev.setText(prev.getText() + this.getText() + next.getText());
413 } else if (prev || next) {
414 var target = prev ? prev : next;
415 target.setText(target.getText() + this.getText());
426 split: function(params) {
427 var parentElement = this.parent(),
428 myIdx = parentElement.childIndex(this),
429 myCanvas = this.canvas,
431 succeedingChildren = [],
433 prefix = this.getText().substr(0, params.offset),
434 suffix = this.getText().substr(params.offset);
436 parentElement.children().forEach(function(child) {
438 succeedingChildren.push(child);
439 if(child.sameNode(thisElement))
443 if(prefix.length > 0)
444 this.setText(prefix);
448 var newElement = DocumentNodeElement.create({tag: parentElement.getWlxmlTag(), klass: parentElement.getWlxmlClass()}, myCanvas);
449 parentElement.after(newElement);
451 if(suffix.length > 0)
452 newElement.append({text: suffix});
453 succeedingChildren.forEach(function(child) {
454 newElement.append(child);
457 return {first: parentElement, second: newElement};
459 divide: function(params) {
460 var myText = this.getText();
462 if(params.offset <= 0 || params.offset >= myText.length)
465 var lhsText = myText.substr(0, params.offset),
466 rhsText = myText.substr(params.offset),
467 newElement = DocumentNodeElement.create({tag: params.tag, klass: params.klass}, this.canvas),
468 rhsTextElement = DocumentTextElement.create({text: rhsText});
470 this.setText(lhsText);
471 this.after(newElement);
472 newElement.after(rhsTextElement);
478 DocumentElement: DocumentElement,
479 DocumentNodeElement: DocumentNodeElement,
480 DocumentTextElement: DocumentTextElement