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) {
80 var dom = $('<div>').attr('wlxml-tag', params.tag);
82 dom.attr('wlxml-class', params.klass);
86 create: function(params, canvas) {
87 return this.fromHTMLElement(this.createDOM(params)[0]);
90 fromHTMLElement: function(htmlElement, canvas) {
91 return new this(htmlElement, canvas);
95 var manipulate = function(e, params, action) {
97 if(params instanceof DocumentElement) {
100 element = DocumentElement.create(params);
102 e.dom()[action](element.dom());
106 DocumentNodeElement.prototype = new DocumentElement();
108 $.extend(DocumentNodeElement.prototype, {
109 append: function(params) {
110 manipulate(this, params, 'append');
112 before: function(params) {
113 manipulate(this, params, 'before');
116 after: function(params) {
117 manipulate(this, params, 'after');
119 children: function() {
121 if(this instanceof DocumentTextElement)
125 var elementContent = this.dom().contents();
127 elementContent.each(function(idx) {
128 var childElement = DocumentElement.fromHTMLElement(this, element.canvas);
129 if(idx === 0 && elementContent.length > 1 && elementContent[1].nodeType === Node.ELEMENT_NODE && (childElement instanceof DocumentTextElement) && $.trim($(this).text()) === '')
131 if(idx > 0 && childElement instanceof DocumentTextElement) {
132 if(toret[toret.length-1] instanceof DocumentNodeElement && $.trim($(this).text()) === '')
135 toret.push(childElement);
139 childIndex: function(child) {
140 var children = this.children(),
142 children.forEach(function(c, idx) {
143 if(c.sameNode(child)) {
150 getWlxmlTag: function() {
151 return this.dom().attr('wlxml-tag');
153 setWlxmlTag: function(tag) {
154 this.dom().attr('wlxml-tag', tag);
156 getWlxmlClass: function() {
157 var klass = this.dom().attr('wlxml-class');
159 return klass.replace('-', '.');
162 setWlxmlClass: function(klass) {
164 this.dom().attr('wlxml-class', klass);
166 this.dom().removeAttr('wlxml-class');
169 if(what === 'list' && _.contains(['list-items', 'list-items-enum'], this.dom().attr('wlxml-class')))
176 // DocumentNodeElement represents a text node from WLXML document rendered inside Canvas
177 var DocumentTextElement = function(htmlElement, canvas) {
178 DocumentElement.call(this, htmlElement, canvas);
181 $.extend(DocumentTextElement, {
182 createDOM: function(params) {
184 .attr('wlxml-text', '')
188 create: function(params, canvas) {
189 return this.fromHTMLElement(this.createDOM(params)[0]);
192 fromHTMLElement: function(htmlElement, canvas) {
193 return new this(htmlElement, canvas);
197 DocumentTextElement.prototype = new DocumentElement();
199 $.extend(DocumentTextElement.prototype, {
200 _setupDOMHandler: function(htmlElement) {
201 var $element = $(htmlElement);
202 if(htmlElement.nodeType === Node.TEXT_NODE)
203 this.$element = $element.parent();
205 this.$element = $element;
207 setText: function(text) {
208 this.dom().contents()[0].data = text;
210 getText: function() {
211 return this.dom().text();
213 after: function(params) {
214 if(params instanceof DocumentTextElement || params.text)
217 if(params instanceof DocumentNodeElement) {
220 element = DocumentNodeElement.create(params);
222 this.dom().wrap('<div>');
223 this.dom().parent().after(element.dom());
227 before: function(params) {
228 if(params instanceof DocumentTextElement || params.text)
231 if(params instanceof DocumentNodeElement) {
234 element = DocumentNodeElement.create(params);
236 this.dom().wrap('<div>');
237 this.dom().parent().before(element.dom());
241 wrapWithNodeElement: function(wlxmlNode) {
242 if(typeof wlxmlNode.start === 'number' && typeof wlxmlNode.end === 'number') {
243 return this.canvas.wrapText({
244 inside: this.parent(),
245 textNodeIdx: this.parent().childIndex(this),
246 offsetStart: Math.min(wlxmlNode.start, wlxmlNode.end),
247 offsetEnd: Math.max(wlxmlNode.start, wlxmlNode.end),
248 _with: {tag: wlxmlNode.tag, klass: wlxmlNode.klass}
251 return DocumentElement.prototype.wrapWithNodeElement.call(this, wlxmlNode);
255 var parent = this.parent();
256 if(parent.children().length === 1) {
257 var grandParent = parent.parent();
259 var grandParentChildren = grandParent.children(),
260 idx = grandParent.childIndex(parent),
261 prev = idx - 1 > -1 ? grandParentChildren[idx-1] : null,
262 next = idx + 1 < grandParentChildren.length ? grandParentChildren[idx+1] : null;
264 prev.setText(prev.getText() + this.getText() + next.getText());
266 } else if (prev || next) {
267 var target = prev ? prev : next;
268 target.setText(target.getText() + this.getText());
278 split: function(params) {
279 var parentElement = this.parent(),
280 myIdx = parentElement.childIndex(this),
281 myCanvas = this.canvas,
283 succeedingChildren = [],
285 prefix = this.getText().substr(0, params.offset),
286 suffix = this.getText().substr(params.offset);
288 parentElement.children().forEach(function(child) {
290 succeedingChildren.push(child);
291 if(child.sameNode(thisElement))
295 if(prefix.length > 0)
296 this.setText(prefix);
300 var newElement = DocumentNodeElement.create({tag: parentElement.getWlxmlTag(), klass: parentElement.getWlxmlClass()}, myCanvas);
301 parentElement.after(newElement);
303 if(suffix.length > 0)
304 newElement.append({text: suffix});
305 succeedingChildren.forEach(function(child) {
306 newElement.append(child);
312 DocumentElement: DocumentElement,
313 DocumentNodeElement: DocumentNodeElement,
314 DocumentTextElement: DocumentTextElement