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 dom = DocumentElement.createDOM(params);
65 return documentElementFromHTMLElement(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')))
135 DocumentElement.createDOM = function(params) {
136 var ElementType = params.text !== undefined ? DocumentTextElement : DocumentNodeElement;
137 return ElementType.createDOM(params);
140 DocumentNodeElement.createDOM = function(params) {
141 var dom = $('<div>').attr('wlxml-tag', params.tag);
143 dom.attr('wlxml-class', params.klass);
147 DocumentTextElement.createDOM = function(params) {
148 return $(document.createTextNode(params.text));
151 DocumentNodeElement.create = function(params, canvas) {
152 return documentElementFromHTMLElement(DocumentNodeElement.createDOM(params)[0]);
156 $.extend(DocumentTextElement.prototype, {
157 setText: function(text) {
158 this.dom()[0].data = text;
160 getText: function() {
161 return this.dom().text();
163 after: function(params) {
164 if(params instanceof DocumentTextElement || params.text)
167 if(params instanceof DocumentNodeElement) {
170 dom = DocumentNodeElement.createDOM(params);
172 this.dom().wrap('<div>');
173 this.dom().parent().after(dom[0]);
175 return documentElementFromHTMLElement(dom[0]);
177 before: function(params) {
178 if(params instanceof DocumentTextElement || params.text)
181 if(params instanceof DocumentNodeElement) {
184 dom = DocumentNodeElement.createDOM(params);
186 this.dom().wrap('<div>');
187 this.dom().parent().before(dom[0]);
189 return documentElementFromHTMLElement(dom[0]);
191 wrapWithNodeElement: function(wlxmlNode) {
192 if(typeof wlxmlNode.start === 'number' && typeof wlxmlNode.end === 'number') {
193 return this.canvas.wrapText({
194 inside: this.parent(),
195 textNodeIdx: this.parent().childIndex(this),
196 offsetStart: Math.min(wlxmlNode.start, wlxmlNode.end),
197 offsetEnd: Math.max(wlxmlNode.start, wlxmlNode.end),
198 _with: {tag: wlxmlNode.tag, klass: wlxmlNode.klass}
201 return DocumentElement.prototype.wrapWithNodeElement.call(this, wlxmlNode);
205 var parent = this.parent();
206 if(parent.children().length === 1) {
207 var grandParent = parent.parent();
209 var grandParentChildren = grandParent.children(),
210 idx = grandParent.childIndex(parent),
211 prev = idx - 1 > -1 ? grandParentChildren[idx-1] : null,
212 next = idx + 1 < grandParentChildren.length ? grandParentChildren[idx+1] : null;
214 prev.setText(prev.getText() + this.getText() + next.getText());
216 } else if (prev || next) {
217 var target = prev ? prev : next;
218 target.setText(target.getText() + this.getText());
228 split: function(params) {
229 var parentElement = this.parent(),
230 myIdx = parentElement.childIndex(this),
231 myCanvas = this.canvas,
233 succeedingChildren = [],
235 prefix = this.getText().substr(0, params.offset),
236 suffix = this.getText().substr(params.offset);
238 parentElement.children().forEach(function(child) {
240 succeedingChildren.push(child);
241 if(child.sameNode(thisElement))
245 if(prefix.length > 0)
246 this.setText(prefix);
250 var newElement = DocumentNodeElement.create({tag: parentElement.getWlxmlTag(), klass: parentElement.getWlxmlClass()}, myCanvas);
251 parentElement.after(newElement);
253 if(suffix.length > 0)
254 newElement.append({text: suffix});
255 succeedingChildren.forEach(function(child) {
256 newElement.append(child);
261 var documentElementFromHTMLElement = function(htmlElement, canvas) {
263 // throw 'no canvas specified';
264 if(htmlElement.nodeType === Node.ELEMENT_NODE)
265 return new DocumentNodeElement(htmlElement, canvas);
266 if(htmlElement.nodeType === Node.TEXT_NODE)
267 return new DocumentTextElement(htmlElement, canvas);
271 wrap: function(htmlElement, canvas) {
272 return documentElementFromHTMLElement(htmlElement, canvas);
274 DocumentElement: DocumentElement,
275 DocumentNodeElement: DocumentNodeElement,
276 DocumentTextElement: DocumentTextElement