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());
71 markAsCurrent: function() {
72 this.canvas.markAsCurrent(this);
77 // DocumentNodeElement represents an element node from WLXML document rendered inside Canvas
78 var DocumentNodeElement = function(htmlElement, canvas) {
79 DocumentElement.call(this, htmlElement, canvas);
82 $.extend(DocumentNodeElement, {
83 createDOM: function(params) {
85 .attr('wlxml-tag', params.tag);
87 dom.attr('wlxml-class', params.klass.replace(/\./g, '-'));
91 create: function(params, canvas) {
92 return this.fromHTMLElement(this.createDOM(params)[0]);
95 fromHTMLElement: function(htmlElement, canvas) {
96 return new this(htmlElement, canvas);
100 var manipulate = function(e, params, action) {
102 if(params instanceof DocumentElement) {
105 element = DocumentElement.create(params);
107 e.dom()[action](element.dom());
111 DocumentNodeElement.prototype = new DocumentElement();
113 $.extend(DocumentNodeElement.prototype, {
114 append: function(params) {
115 manipulate(this, params, 'append');
117 before: function(params) {
118 manipulate(this, params, 'before');
121 after: function(params) {
122 manipulate(this, params, 'after');
124 children: function() {
126 if(this instanceof DocumentTextElement)
130 var elementContent = this.dom().contents();
132 elementContent.each(function(idx) {
133 var childElement = DocumentElement.fromHTMLElement(this, element.canvas);
134 if(idx === 0 && elementContent.length > 1 && elementContent[1].nodeType === Node.ELEMENT_NODE && (childElement instanceof DocumentTextElement) && $.trim($(this).text()) === '')
136 if(idx > 0 && childElement instanceof DocumentTextElement) {
137 if(toret[toret.length-1] instanceof DocumentNodeElement && $.trim($(this).text()) === '')
140 toret.push(childElement);
144 childIndex: function(child) {
145 var children = this.children(),
147 children.forEach(function(c, idx) {
148 if(c.sameNode(child)) {
155 getWlxmlTag: function() {
156 return this.dom().attr('wlxml-tag');
158 setWlxmlTag: function(tag) {
159 this.dom().attr('wlxml-tag', tag);
161 getWlxmlClass: function() {
162 var klass = this.dom().attr('wlxml-class');
164 return klass.replace('-', '.');
167 setWlxmlClass: function(klass) {
169 this.dom().attr('wlxml-class', klass);
171 this.dom().removeAttr('wlxml-class');
174 if(what === 'list' && _.contains(['list-items', 'list-items-enum'], this.dom().attr('wlxml-class')))
181 // DocumentNodeElement represents a text node from WLXML document rendered inside Canvas
182 var DocumentTextElement = function(htmlElement, canvas) {
183 DocumentElement.call(this, htmlElement, canvas);
186 $.extend(DocumentTextElement, {
187 createDOM: function(params) {
189 .attr('wlxml-text', '')
193 create: function(params, canvas) {
194 return this.fromHTMLElement(this.createDOM(params)[0]);
197 fromHTMLElement: function(htmlElement, canvas) {
198 return new this(htmlElement, canvas);
202 DocumentTextElement.prototype = new DocumentElement();
204 $.extend(DocumentTextElement.prototype, {
205 _setupDOMHandler: function(htmlElement) {
206 var $element = $(htmlElement);
207 if(htmlElement.nodeType === Node.TEXT_NODE)
208 this.$element = $element.parent();
210 this.$element = $element;
212 setText: function(text) {
213 this.dom().contents()[0].data = text;
215 getText: function() {
216 return this.dom().text();
218 after: function(params) {
219 if(params instanceof DocumentTextElement || params.text)
222 if(params instanceof DocumentNodeElement) {
225 element = DocumentNodeElement.create(params);
227 this.dom().wrap('<div>');
228 this.dom().parent().after(element.dom());
232 before: function(params) {
233 if(params instanceof DocumentTextElement || params.text)
236 if(params instanceof DocumentNodeElement) {
239 element = DocumentNodeElement.create(params);
241 this.dom().wrap('<div>');
242 this.dom().parent().before(element.dom());
246 wrapWithNodeElement: function(wlxmlNode) {
247 if(typeof wlxmlNode.start === 'number' && typeof wlxmlNode.end === 'number') {
248 return this.canvas.wrapText({
249 inside: this.parent(),
250 textNodeIdx: this.parent().childIndex(this),
251 offsetStart: Math.min(wlxmlNode.start, wlxmlNode.end),
252 offsetEnd: Math.max(wlxmlNode.start, wlxmlNode.end),
253 _with: {tag: wlxmlNode.tag, klass: wlxmlNode.klass}
256 return DocumentElement.prototype.wrapWithNodeElement.call(this, wlxmlNode);
260 var parent = this.parent();
261 if(parent.children().length === 1) {
262 var grandParent = parent.parent();
264 var grandParentChildren = grandParent.children(),
265 idx = grandParent.childIndex(parent),
266 prev = idx - 1 > -1 ? grandParentChildren[idx-1] : null,
267 next = idx + 1 < grandParentChildren.length ? grandParentChildren[idx+1] : null;
269 prev.setText(prev.getText() + this.getText() + next.getText());
271 } else if (prev || next) {
272 var target = prev ? prev : next;
273 target.setText(target.getText() + this.getText());
283 split: function(params) {
284 var parentElement = this.parent(),
285 myIdx = parentElement.childIndex(this),
286 myCanvas = this.canvas,
288 succeedingChildren = [],
290 prefix = this.getText().substr(0, params.offset),
291 suffix = this.getText().substr(params.offset);
293 parentElement.children().forEach(function(child) {
295 succeedingChildren.push(child);
296 if(child.sameNode(thisElement))
300 if(prefix.length > 0)
301 this.setText(prefix);
305 var newElement = DocumentNodeElement.create({tag: parentElement.getWlxmlTag(), klass: parentElement.getWlxmlClass()}, myCanvas);
306 parentElement.after(newElement);
308 if(suffix.length > 0)
309 newElement.append({text: suffix});
310 succeedingChildren.forEach(function(child) {
311 newElement.append(child);
317 DocumentElement: DocumentElement,
318 DocumentNodeElement: DocumentNodeElement,
319 DocumentTextElement: DocumentTextElement