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.$element = $(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 if(htmlElement.nodeType === Node.ELEMENT_NODE)
33 return DocumentNodeElement.fromHTMLElement(htmlElement, canvas);
34 if(htmlElement.nodeType === Node.TEXT_NODE)
35 return DocumentTextElement.fromHTMLElement(htmlElement, canvas);
39 $.extend(DocumentElement.prototype, {
44 var parents = this.$element.parents('[wlxml-tag]');
46 return DocumentElement.fromHTMLElement(parents[0], this.canvas);
50 sameNode: function(other) {
51 return other && (typeof other === typeof this) && other.dom()[0] === this.dom()[0];
54 wrapWithNodeElement: function(wlxmlNode) {
55 var wrapper = DocumentNodeElement.create({tag: wlxmlNode.tag, klass: wlxmlNode.klass});
56 this.dom().replaceWith(wrapper.dom());
68 // DocumentNodeElement represents an element node from WLXML document rendered inside Canvas
69 var DocumentNodeElement = function(htmlElement, canvas) {
70 DocumentElement.call(this, htmlElement, canvas);
73 $.extend(DocumentNodeElement, {
74 createDOM: function(params) {
75 var dom = $('<div>').attr('wlxml-tag', params.tag);
77 dom.attr('wlxml-class', params.klass);
81 create: function(params, canvas) {
82 return this.fromHTMLElement(this.createDOM(params)[0]);
85 fromHTMLElement: function(htmlElement, canvas) {
86 return new this(htmlElement, canvas);
90 var manipulate = function(e, params, action) {
92 if(params instanceof DocumentElement) {
95 element = DocumentElement.create(params);
97 e.dom()[action](element.dom());
101 DocumentNodeElement.prototype = new DocumentElement();
103 $.extend(DocumentNodeElement.prototype, {
104 append: function(params) {
105 manipulate(this, params, 'append');
107 before: function(params) {
108 manipulate(this, params, 'before');
111 after: function(params) {
112 manipulate(this, params, 'after');
114 children: function() {
116 if(this instanceof DocumentTextElement)
120 var elementContent = this.dom().contents();
122 elementContent.each(function(idx) {
123 var childElement = DocumentElement.fromHTMLElement(this, element.canvas);
124 if(idx === 0 && elementContent.length > 1 && elementContent[1].nodeType === Node.ELEMENT_NODE && (childElement instanceof DocumentTextElement) && $.trim($(this).text()) === '')
126 if(idx > 0 && childElement instanceof DocumentTextElement) {
127 if(toret[toret.length-1] instanceof DocumentNodeElement && $.trim($(this).text()) === '')
130 toret.push(childElement);
134 childIndex: function(child) {
135 var children = this.children(),
137 children.forEach(function(c, idx) {
138 if(c.sameNode(child)) {
145 getWlxmlTag: function() {
146 return this.dom().attr('wlxml-tag');
148 setWlxmlTag: function(tag) {
149 this.dom().attr('wlxml-tag', tag);
151 getWlxmlClass: function() {
152 var klass = this.dom().attr('wlxml-class');
154 return klass.replace('-', '.');
157 setWlxmlClass: function(klass) {
159 this.dom().attr('wlxml-class', klass);
161 this.dom().removeAttr('wlxml-class');
164 if(what === 'list' && _.contains(['list-items', 'list-items-enum'], this.dom().attr('wlxml-class')))
171 // DocumentNodeElement represents a text node from WLXML document rendered inside Canvas
172 var DocumentTextElement = function(htmlElement, canvas) {
173 DocumentElement.call(this, htmlElement, canvas);
176 $.extend(DocumentTextElement, {
177 createDOM: function(params) {
178 return $(document.createTextNode(params.text));
181 create: function(params, canvas) {
182 return this.fromHTMLElement(this.createDOM(params)[0]);
185 fromHTMLElement: function(htmlElement, canvas) {
186 return new this(htmlElement, canvas);
190 DocumentTextElement.prototype = new DocumentElement();
192 $.extend(DocumentTextElement.prototype, {
193 setText: function(text) {
194 this.dom()[0].data = text;
196 getText: function() {
197 return this.dom().text();
199 after: function(params) {
200 if(params instanceof DocumentTextElement || params.text)
203 if(params instanceof DocumentNodeElement) {
206 element = DocumentNodeElement.create(params);
208 this.dom().wrap('<div>');
209 this.dom().parent().after(element.dom());
213 before: 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().before(element.dom());
227 wrapWithNodeElement: function(wlxmlNode) {
228 if(typeof wlxmlNode.start === 'number' && typeof wlxmlNode.end === 'number') {
229 return this.canvas.wrapText({
230 inside: this.parent(),
231 textNodeIdx: this.parent().childIndex(this),
232 offsetStart: Math.min(wlxmlNode.start, wlxmlNode.end),
233 offsetEnd: Math.max(wlxmlNode.start, wlxmlNode.end),
234 _with: {tag: wlxmlNode.tag, klass: wlxmlNode.klass}
237 return DocumentElement.prototype.wrapWithNodeElement.call(this, wlxmlNode);
241 var parent = this.parent();
242 if(parent.children().length === 1) {
243 var grandParent = parent.parent();
245 var grandParentChildren = grandParent.children(),
246 idx = grandParent.childIndex(parent),
247 prev = idx - 1 > -1 ? grandParentChildren[idx-1] : null,
248 next = idx + 1 < grandParentChildren.length ? grandParentChildren[idx+1] : null;
250 prev.setText(prev.getText() + this.getText() + next.getText());
252 } else if (prev || next) {
253 var target = prev ? prev : next;
254 target.setText(target.getText() + this.getText());
264 split: function(params) {
265 var parentElement = this.parent(),
266 myIdx = parentElement.childIndex(this),
267 myCanvas = this.canvas,
269 succeedingChildren = [],
271 prefix = this.getText().substr(0, params.offset),
272 suffix = this.getText().substr(params.offset);
274 parentElement.children().forEach(function(child) {
276 succeedingChildren.push(child);
277 if(child.sameNode(thisElement))
281 if(prefix.length > 0)
282 this.setText(prefix);
286 var newElement = DocumentNodeElement.create({tag: parentElement.getWlxmlTag(), klass: parentElement.getWlxmlClass()}, myCanvas);
287 parentElement.after(newElement);
289 if(suffix.length > 0)
290 newElement.append({text: suffix});
291 succeedingChildren.forEach(function(child) {
292 newElement.append(child);
298 DocumentElement: DocumentElement,
299 DocumentNodeElement: DocumentNodeElement,
300 DocumentTextElement: DocumentTextElement