2 'libs/jquery-1.9.1.min',
9 // DocumentElement represents a node from WLXML document rendered inside Canvas
10 var DocumentElement = function(htmlElement, canvas) {
11 if(arguments.length === 0)
14 this.$element = $(htmlElement);
17 $.extend(DocumentElement.prototype, {
22 var parents = this.$element.parents('[wlxml-tag]');
24 return documentElementFromHTMLElement(parents[0], this.canvas);
28 sameNode: function(other) {
29 return other && (typeof other === typeof this) && other.dom()[0] === this.dom()[0];
32 wrapWithNodeElement: function(wlxmlNode) {
33 var wrapper = DocumentNodeElement.create({tag: wlxmlNode.tag, klass: wlxmlNode.klass});
34 this.dom().replaceWith(wrapper.dom());
46 var DocumentNodeElement = function(htmlElement, canvas) {
47 DocumentElement.call(this, htmlElement, canvas);
50 var DocumentTextElement = function(htmlElement, canvas) {
51 DocumentElement.call(this, htmlElement, canvas);
54 DocumentNodeElement.prototype = new DocumentElement();
55 DocumentTextElement.prototype = new DocumentElement();
57 var manipulate = function(e, params, action) {
59 if(params instanceof DocumentElement) {
62 element = DocumentElement.create(params);
64 e.dom()[action](element.dom());
68 $.extend(DocumentNodeElement.prototype, {
69 append: function(params) {
70 manipulate(this, params, 'append');
72 before: function(params) {
73 manipulate(this, params, 'before');
76 after: function(params) {
77 manipulate(this, params, 'after');
79 children: function() {
81 if(this instanceof DocumentTextElement)
85 var elementContent = this.dom().contents();
87 elementContent.each(function(idx) {
88 var childElement = documentElementFromHTMLElement(this, element.canvas);
89 if(idx === 0 && elementContent.length > 1 && elementContent[1].nodeType === Node.ELEMENT_NODE && (childElement instanceof DocumentTextElement) && $.trim($(this).text()) === '')
91 if(idx > 0 && childElement instanceof DocumentTextElement) {
92 if(toret[toret.length-1] instanceof DocumentNodeElement && $.trim($(this).text()) === '')
95 toret.push(childElement);
99 childIndex: function(child) {
100 var children = this.children(),
102 children.forEach(function(c, idx) {
103 if(c.sameNode(child)) {
110 getWlxmlTag: function() {
111 return this.dom().attr('wlxml-tag');
113 setWlxmlTag: function(tag) {
114 this.dom().attr('wlxml-tag', tag);
116 getWlxmlClass: function() {
117 var klass = this.dom().attr('wlxml-class');
119 return klass.replace('-', '.');
122 setWlxmlClass: function(klass) {
124 this.dom().attr('wlxml-class', klass);
126 this.dom().removeAttr('wlxml-class');
129 if(what === 'list' && _.contains(['list-items', 'list-items-enum'], this.dom().attr('wlxml-class')))
136 DocumentElement.create = function(params, canvas) {
137 var ElementType = params.text !== undefined ? DocumentTextElement : DocumentNodeElement;
138 return ElementType.create(params);
141 DocumentElement.createDOM = function(params) {
142 var ElementType = params.text !== undefined ? DocumentTextElement : DocumentNodeElement;
143 return ElementType.createDOM(params);
146 DocumentNodeElement.createDOM = function(params) {
147 var dom = $('<div>').attr('wlxml-tag', params.tag);
149 dom.attr('wlxml-class', params.klass);
153 DocumentTextElement.createDOM = function(params) {
154 return $(document.createTextNode(params.text));
157 DocumentNodeElement.create = function(params, canvas) {
158 return documentElementFromHTMLElement(DocumentNodeElement.createDOM(params)[0]);
161 DocumentTextElement.create = function(params, canvas) {
162 return documentElementFromHTMLElement(DocumentTextElement.createDOM(params)[0]);
166 $.extend(DocumentTextElement.prototype, {
167 setText: function(text) {
168 this.dom()[0].data = text;
170 getText: function() {
171 return this.dom().text();
173 after: function(params) {
174 if(params instanceof DocumentTextElement || params.text)
177 if(params instanceof DocumentNodeElement) {
180 element = DocumentNodeElement.create(params);
182 this.dom().wrap('<div>');
183 this.dom().parent().after(element.dom());
187 before: function(params) {
188 if(params instanceof DocumentTextElement || params.text)
191 if(params instanceof DocumentNodeElement) {
194 element = DocumentNodeElement.create(params);
196 this.dom().wrap('<div>');
197 this.dom().parent().before(element.dom());
201 wrapWithNodeElement: function(wlxmlNode) {
202 if(typeof wlxmlNode.start === 'number' && typeof wlxmlNode.end === 'number') {
203 return this.canvas.wrapText({
204 inside: this.parent(),
205 textNodeIdx: this.parent().childIndex(this),
206 offsetStart: Math.min(wlxmlNode.start, wlxmlNode.end),
207 offsetEnd: Math.max(wlxmlNode.start, wlxmlNode.end),
208 _with: {tag: wlxmlNode.tag, klass: wlxmlNode.klass}
211 return DocumentElement.prototype.wrapWithNodeElement.call(this, wlxmlNode);
215 var parent = this.parent();
216 if(parent.children().length === 1) {
217 var grandParent = parent.parent();
219 var grandParentChildren = grandParent.children(),
220 idx = grandParent.childIndex(parent),
221 prev = idx - 1 > -1 ? grandParentChildren[idx-1] : null,
222 next = idx + 1 < grandParentChildren.length ? grandParentChildren[idx+1] : null;
224 prev.setText(prev.getText() + this.getText() + next.getText());
226 } else if (prev || next) {
227 var target = prev ? prev : next;
228 target.setText(target.getText() + this.getText());
238 split: function(params) {
239 var parentElement = this.parent(),
240 myIdx = parentElement.childIndex(this),
241 myCanvas = this.canvas,
243 succeedingChildren = [],
245 prefix = this.getText().substr(0, params.offset),
246 suffix = this.getText().substr(params.offset);
248 parentElement.children().forEach(function(child) {
250 succeedingChildren.push(child);
251 if(child.sameNode(thisElement))
255 if(prefix.length > 0)
256 this.setText(prefix);
260 var newElement = DocumentNodeElement.create({tag: parentElement.getWlxmlTag(), klass: parentElement.getWlxmlClass()}, myCanvas);
261 parentElement.after(newElement);
263 if(suffix.length > 0)
264 newElement.append({text: suffix});
265 succeedingChildren.forEach(function(child) {
266 newElement.append(child);
271 var documentElementFromHTMLElement = function(htmlElement, canvas) {
273 // throw 'no canvas specified';
274 if(htmlElement.nodeType === Node.ELEMENT_NODE)
275 return new DocumentNodeElement(htmlElement, canvas);
276 if(htmlElement.nodeType === Node.TEXT_NODE)
277 return new DocumentTextElement(htmlElement, canvas);
281 wrap: function(htmlElement, canvas) {
282 return documentElementFromHTMLElement(htmlElement, canvas);
284 DocumentElement: DocumentElement,
285 DocumentNodeElement: DocumentNodeElement,
286 DocumentTextElement: DocumentTextElement