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, {
151 append: function(params) {
152 return manipulate(this, params, 'append');
154 before: function(params) {
155 return manipulate(this, params, 'before');
158 after: function(params) {
159 return manipulate(this, params, 'after');
161 children: function() {
163 if(this instanceof DocumentTextElement)
167 var elementContent = this.dom().contents();
169 elementContent.each(function(idx) {
170 var childElement = DocumentElement.fromHTMLElement(this, element.canvas);
171 if(idx === 0 && elementContent.length > 1 && elementContent[1].nodeType === Node.ELEMENT_NODE && (childElement instanceof DocumentTextElement) && $.trim($(this).text()) === '')
173 if(idx > 0 && childElement instanceof DocumentTextElement) {
174 if(toret[toret.length-1] instanceof DocumentNodeElement && $.trim($(this).text()) === '')
177 toret.push(childElement);
181 childIndex: function(child) {
182 var children = this.children(),
184 children.forEach(function(c, idx) {
185 if(c.sameNode(child)) {
192 getWlxmlTag: function() {
193 return this.dom().attr('wlxml-tag');
195 setWlxmlTag: function(tag) {
196 this.dom().attr('wlxml-tag', tag);
198 getWlxmlClass: function() {
199 var klass = this.dom().attr('wlxml-class');
201 return klass.replace(/-/g, '.');
204 setWlxmlClass: function(klass) {
205 this.getWlxmlMetaAttrs().forEach(function(attr) {
206 if(!classAttributes.hasMetaAttr(klass, attr.name))
207 this.dom().removeAttr('wlxml-meta-' + attr.name);
211 this.dom().attr('wlxml-class', klass.replace(/\./g, '-'));
213 this.dom().removeAttr('wlxml-class');
216 if(what === 'list' && _.contains(['list-items', 'list-items-enum'], this.dom().attr('wlxml-class')))
222 getWlxmlMetaAttr: function(attr) {
223 return this.dom().attr('wlxml-meta-'+attr);
225 getWlxmlMetaAttrs: function() {
227 var attrList = classAttributes.getMetaAttrsList(this.getWlxmlClass());
228 attrList.all.forEach(function(attr) {
229 toret.push({name: attr.name, value: this.getWlxmlMetaAttr(attr.name) || ''});
233 setWlxmlMetaAttr: function(attr, value) {
234 this.dom().attr('wlxml-meta-'+attr, value);
239 // DocumentNodeElement represents a text node from WLXML document rendered inside Canvas
240 var DocumentTextElement = function(htmlElement, canvas) {
241 DocumentElement.call(this, htmlElement, canvas);
244 $.extend(DocumentTextElement, {
245 createDOM: function(params) {
247 .attr('wlxml-text', '')
251 create: function(params, canvas) {
252 return this.fromHTMLElement(this.createDOM(params)[0]);
255 fromHTMLElement: function(htmlElement, canvas) {
256 return new this(htmlElement, canvas);
260 DocumentTextElement.prototype = new DocumentElement();
262 $.extend(DocumentTextElement.prototype, {
263 _setupDOMHandler: function(htmlElement) {
264 var $element = $(htmlElement);
265 if(htmlElement.nodeType === Node.TEXT_NODE)
266 this.$element = $element.parent();
268 this.$element = $element;
270 setText: function(text) {
271 this.dom().contents()[0].data = text;
273 getText: function() {
274 return this.dom().text();
276 after: function(params) {
277 if(params instanceof DocumentTextElement || params.text)
280 if(params instanceof DocumentNodeElement) {
283 element = DocumentNodeElement.create(params);
285 this.dom().wrap('<div>');
286 this.dom().parent().after(element.dom());
290 before: function(params) {
291 if(params instanceof DocumentTextElement || params.text)
294 if(params instanceof DocumentNodeElement) {
297 element = DocumentNodeElement.create(params);
299 this.dom().wrap('<div>');
300 this.dom().parent().before(element.dom());
304 wrapWithNodeElement: function(wlxmlNode) {
305 if(typeof wlxmlNode.start === 'number' && typeof wlxmlNode.end === 'number') {
306 return this.canvas.wrapText({
307 inside: this.parent(),
308 textNodeIdx: this.parent().childIndex(this),
309 offsetStart: Math.min(wlxmlNode.start, wlxmlNode.end),
310 offsetEnd: Math.max(wlxmlNode.start, wlxmlNode.end),
311 _with: {tag: wlxmlNode.tag, klass: wlxmlNode.klass}
314 return DocumentElement.prototype.wrapWithNodeElement.call(this, wlxmlNode);
318 var parent = this.parent();
319 if(parent.children().length === 1) {
320 var grandParent = parent.parent();
322 var grandParentChildren = grandParent.children(),
323 idx = grandParent.childIndex(parent),
324 prev = idx - 1 > -1 ? grandParentChildren[idx-1] : null,
325 next = idx + 1 < grandParentChildren.length ? grandParentChildren[idx+1] : null;
327 prev.setText(prev.getText() + this.getText() + next.getText());
329 } else if (prev || next) {
330 var target = prev ? prev : next;
331 target.setText(target.getText() + this.getText());
341 split: function(params) {
342 var parentElement = this.parent(),
343 myIdx = parentElement.childIndex(this),
344 myCanvas = this.canvas,
346 succeedingChildren = [],
348 prefix = this.getText().substr(0, params.offset),
349 suffix = this.getText().substr(params.offset);
351 parentElement.children().forEach(function(child) {
353 succeedingChildren.push(child);
354 if(child.sameNode(thisElement))
358 if(prefix.length > 0)
359 this.setText(prefix);
363 var newElement = DocumentNodeElement.create({tag: parentElement.getWlxmlTag(), klass: parentElement.getWlxmlClass()}, myCanvas);
364 parentElement.after(newElement);
366 if(suffix.length > 0)
367 newElement.append({text: suffix});
368 succeedingChildren.forEach(function(child) {
369 newElement.append(child);
372 return {first: parentElement, second: newElement};
377 DocumentElement: DocumentElement,
378 DocumentNodeElement: DocumentNodeElement,
379 DocumentTextElement: DocumentTextElement