2 'libs/jquery-1.9.1.min',
9 // DocumentElement represents a text or an element node from WLXML document rendered inside Canvas
10 var DocumentElement = function(htmlElement, canvas) {
11 if(arguments.length === 0)
14 this._setupDOMHandler(htmlElement);
17 var elementTypeFromParams = function(params) {
18 return params.text !== undefined ? DocumentTextElement : DocumentNodeElement;
22 $.extend(DocumentElement, {
23 create: function(params, canvas) {
24 return elementTypeFromParams(params).create(params);
27 createDOM: function(params) {
28 return elementTypeFromParams(params).createDOM(params);
31 fromHTMLElement: function(htmlElement, canvas) {
32 var $element = $(htmlElement);
33 if(htmlElement.nodeType === Node.ELEMENT_NODE && $element.attr('wlxml-tag'))
34 return DocumentNodeElement.fromHTMLElement(htmlElement, canvas);
35 if($element.attr('wlxml-text') !== undefined || (htmlElement.nodeType === Node.TEXT_NODE && $element.parent().attr('wlxml-text') !== undefined))
36 return DocumentTextElement.fromHTMLElement(htmlElement, canvas);
41 $.extend(DocumentElement.prototype, {
42 _setupDOMHandler: function(htmlElement) {
43 this.$element = $(htmlElement);
49 var parents = this.$element.parents('[wlxml-tag]');
51 return DocumentElement.fromHTMLElement(parents[0], this.canvas);
55 sameNode: function(other) {
56 return other && (typeof other === typeof this) && other.dom()[0] === this.dom()[0];
59 wrapWithNodeElement: function(wlxmlNode) {
60 var wrapper = DocumentNodeElement.create({tag: wlxmlNode.tag, klass: wlxmlNode.klass});
61 this.dom().replaceWith(wrapper.dom());
73 // DocumentNodeElement represents an element node from WLXML document rendered inside Canvas
74 var DocumentNodeElement = function(htmlElement, canvas) {
75 DocumentElement.call(this, htmlElement, canvas);
78 $.extend(DocumentNodeElement, {
79 createDOM: function(params) {
81 .attr('wlxml-tag', params.tag);
83 dom.attr('wlxml-class', params.klass.replace(/\./g, '-'));
87 create: function(params, canvas) {
88 return this.fromHTMLElement(this.createDOM(params)[0]);
91 fromHTMLElement: function(htmlElement, canvas) {
92 return new this(htmlElement, canvas);
96 var manipulate = function(e, params, action) {
98 if(params instanceof DocumentElement) {
101 element = DocumentElement.create(params);
103 e.dom()[action](element.dom());
107 DocumentNodeElement.prototype = new DocumentElement();
109 $.extend(DocumentNodeElement.prototype, {
110 append: function(params) {
111 manipulate(this, params, 'append');
113 before: function(params) {
114 manipulate(this, params, 'before');
117 after: function(params) {
118 manipulate(this, params, 'after');
120 children: function() {
122 if(this instanceof DocumentTextElement)
126 var elementContent = this.dom().contents();
128 elementContent.each(function(idx) {
129 var childElement = DocumentElement.fromHTMLElement(this, element.canvas);
130 if(idx === 0 && elementContent.length > 1 && elementContent[1].nodeType === Node.ELEMENT_NODE && (childElement instanceof DocumentTextElement) && $.trim($(this).text()) === '')
132 if(idx > 0 && childElement instanceof DocumentTextElement) {
133 if(toret[toret.length-1] instanceof DocumentNodeElement && $.trim($(this).text()) === '')
136 toret.push(childElement);
140 childIndex: function(child) {
141 var children = this.children(),
143 children.forEach(function(c, idx) {
144 if(c.sameNode(child)) {
151 getWlxmlTag: function() {
152 return this.dom().attr('wlxml-tag');
154 setWlxmlTag: function(tag) {
155 this.dom().attr('wlxml-tag', tag);
157 getWlxmlClass: function() {
158 var klass = this.dom().attr('wlxml-class');
160 return klass.replace('-', '.');
163 setWlxmlClass: function(klass) {
165 this.dom().attr('wlxml-class', klass);
167 this.dom().removeAttr('wlxml-class');
170 if(what === 'list' && _.contains(['list-items', 'list-items-enum'], this.dom().attr('wlxml-class')))
177 // DocumentNodeElement represents a text node from WLXML document rendered inside Canvas
178 var DocumentTextElement = function(htmlElement, canvas) {
179 DocumentElement.call(this, htmlElement, canvas);
182 $.extend(DocumentTextElement, {
183 createDOM: function(params) {
185 .attr('wlxml-text', '')
189 create: function(params, canvas) {
190 return this.fromHTMLElement(this.createDOM(params)[0]);
193 fromHTMLElement: function(htmlElement, canvas) {
194 return new this(htmlElement, canvas);
198 DocumentTextElement.prototype = new DocumentElement();
200 $.extend(DocumentTextElement.prototype, {
201 _setupDOMHandler: function(htmlElement) {
202 var $element = $(htmlElement);
203 if(htmlElement.nodeType === Node.TEXT_NODE)
204 this.$element = $element.parent();
206 this.$element = $element;
208 setText: function(text) {
209 this.dom().contents()[0].data = text;
211 getText: function() {
212 return this.dom().text();
214 after: function(params) {
215 if(params instanceof DocumentTextElement || params.text)
218 if(params instanceof DocumentNodeElement) {
221 element = DocumentNodeElement.create(params);
223 this.dom().wrap('<div>');
224 this.dom().parent().after(element.dom());
228 before: function(params) {
229 if(params instanceof DocumentTextElement || params.text)
232 if(params instanceof DocumentNodeElement) {
235 element = DocumentNodeElement.create(params);
237 this.dom().wrap('<div>');
238 this.dom().parent().before(element.dom());
242 wrapWithNodeElement: function(wlxmlNode) {
243 if(typeof wlxmlNode.start === 'number' && typeof wlxmlNode.end === 'number') {
244 return this.canvas.wrapText({
245 inside: this.parent(),
246 textNodeIdx: this.parent().childIndex(this),
247 offsetStart: Math.min(wlxmlNode.start, wlxmlNode.end),
248 offsetEnd: Math.max(wlxmlNode.start, wlxmlNode.end),
249 _with: {tag: wlxmlNode.tag, klass: wlxmlNode.klass}
252 return DocumentElement.prototype.wrapWithNodeElement.call(this, wlxmlNode);
256 var parent = this.parent();
257 if(parent.children().length === 1) {
258 var grandParent = parent.parent();
260 var grandParentChildren = grandParent.children(),
261 idx = grandParent.childIndex(parent),
262 prev = idx - 1 > -1 ? grandParentChildren[idx-1] : null,
263 next = idx + 1 < grandParentChildren.length ? grandParentChildren[idx+1] : null;
265 prev.setText(prev.getText() + this.getText() + next.getText());
267 } else if (prev || next) {
268 var target = prev ? prev : next;
269 target.setText(target.getText() + this.getText());
279 split: function(params) {
280 var parentElement = this.parent(),
281 myIdx = parentElement.childIndex(this),
282 myCanvas = this.canvas,
284 succeedingChildren = [],
286 prefix = this.getText().substr(0, params.offset),
287 suffix = this.getText().substr(params.offset);
289 parentElement.children().forEach(function(child) {
291 succeedingChildren.push(child);
292 if(child.sameNode(thisElement))
296 if(prefix.length > 0)
297 this.setText(prefix);
301 var newElement = DocumentNodeElement.create({tag: parentElement.getWlxmlTag(), klass: parentElement.getWlxmlClass()}, myCanvas);
302 parentElement.after(newElement);
304 if(suffix.length > 0)
305 newElement.append({text: suffix});
306 succeedingChildren.forEach(function(child) {
307 newElement.append(child);
313 DocumentElement: DocumentElement,
314 DocumentNodeElement: DocumentNodeElement,
315 DocumentTextElement: DocumentTextElement