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 DocumentElement.fromHTMLElement(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 = DocumentElement.fromHTMLElement(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 DocumentElement.fromHTMLElement = function(htmlElement, canvas) {
147 if(htmlElement.nodeType === Node.ELEMENT_NODE)
148 return DocumentNodeElement.fromHTMLElement(htmlElement, canvas);
149 if(htmlElement.nodeType === Node.TEXT_NODE)
150 return DocumentTextElement.fromHTMLElement(htmlElement, canvas);
153 DocumentNodeElement.createDOM = function(params) {
154 var dom = $('<div>').attr('wlxml-tag', params.tag);
156 dom.attr('wlxml-class', params.klass);
160 DocumentTextElement.createDOM = function(params) {
161 return $(document.createTextNode(params.text));
164 DocumentNodeElement.create = function(params, canvas) {
165 return this.fromHTMLElement(this.createDOM(params)[0]);
168 DocumentTextElement.create = function(params, canvas) {
169 return this.fromHTMLElement(this.createDOM(params)[0]);
172 DocumentNodeElement.fromHTMLElement = function(htmlElement, canvas) {
173 return new this(htmlElement, canvas);
176 DocumentTextElement.fromHTMLElement = function(htmlElement, canvas) {
177 return new this(htmlElement, canvas);
181 $.extend(DocumentTextElement.prototype, {
182 setText: function(text) {
183 this.dom()[0].data = text;
185 getText: function() {
186 return this.dom().text();
188 after: function(params) {
189 if(params instanceof DocumentTextElement || params.text)
192 if(params instanceof DocumentNodeElement) {
195 element = DocumentNodeElement.create(params);
197 this.dom().wrap('<div>');
198 this.dom().parent().after(element.dom());
202 before: function(params) {
203 if(params instanceof DocumentTextElement || params.text)
206 if(params instanceof DocumentNodeElement) {
209 element = DocumentNodeElement.create(params);
211 this.dom().wrap('<div>');
212 this.dom().parent().before(element.dom());
216 wrapWithNodeElement: function(wlxmlNode) {
217 if(typeof wlxmlNode.start === 'number' && typeof wlxmlNode.end === 'number') {
218 return this.canvas.wrapText({
219 inside: this.parent(),
220 textNodeIdx: this.parent().childIndex(this),
221 offsetStart: Math.min(wlxmlNode.start, wlxmlNode.end),
222 offsetEnd: Math.max(wlxmlNode.start, wlxmlNode.end),
223 _with: {tag: wlxmlNode.tag, klass: wlxmlNode.klass}
226 return DocumentElement.prototype.wrapWithNodeElement.call(this, wlxmlNode);
230 var parent = this.parent();
231 if(parent.children().length === 1) {
232 var grandParent = parent.parent();
234 var grandParentChildren = grandParent.children(),
235 idx = grandParent.childIndex(parent),
236 prev = idx - 1 > -1 ? grandParentChildren[idx-1] : null,
237 next = idx + 1 < grandParentChildren.length ? grandParentChildren[idx+1] : null;
239 prev.setText(prev.getText() + this.getText() + next.getText());
241 } else if (prev || next) {
242 var target = prev ? prev : next;
243 target.setText(target.getText() + this.getText());
253 split: function(params) {
254 var parentElement = this.parent(),
255 myIdx = parentElement.childIndex(this),
256 myCanvas = this.canvas,
258 succeedingChildren = [],
260 prefix = this.getText().substr(0, params.offset),
261 suffix = this.getText().substr(params.offset);
263 parentElement.children().forEach(function(child) {
265 succeedingChildren.push(child);
266 if(child.sameNode(thisElement))
270 if(prefix.length > 0)
271 this.setText(prefix);
275 var newElement = DocumentNodeElement.create({tag: parentElement.getWlxmlTag(), klass: parentElement.getWlxmlClass()}, myCanvas);
276 parentElement.after(newElement);
278 if(suffix.length > 0)
279 newElement.append({text: suffix});
280 succeedingChildren.forEach(function(child) {
281 newElement.append(child);
287 DocumentElement: DocumentElement,
288 DocumentNodeElement: DocumentNodeElement,
289 DocumentTextElement: DocumentTextElement