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);
57 parent = this.parent();
60 parent = parent.parent();
65 sameNode: function(other) {
66 return other && (typeof other === typeof this) && other.dom()[0] === this.dom()[0];
69 wrapWithNodeElement: function(wlxmlNode) {
70 var wrapper = DocumentNodeElement.create({tag: wlxmlNode.tag, klass: wlxmlNode.klass});
71 this.dom().replaceWith(wrapper.dom());
81 markAsCurrent: function() {
82 this.canvas.markAsCurrent(this);
87 // DocumentNodeElement represents an element node from WLXML document rendered inside Canvas
88 var DocumentNodeElement = function(htmlElement, canvas) {
89 DocumentElement.call(this, htmlElement, canvas);
92 $.extend(DocumentNodeElement, {
93 createDOM: function(params) {
95 .attr('wlxml-tag', params.tag);
97 dom.attr('wlxml-class', params.klass.replace(/\./g, '-'));
101 create: function(params, canvas) {
102 return this.fromHTMLElement(this.createDOM(params)[0]);
105 fromHTMLElement: function(htmlElement, canvas) {
106 return new this(htmlElement, canvas);
110 var manipulate = function(e, params, action) {
112 if(params instanceof DocumentElement) {
115 element = DocumentElement.create(params);
117 e.dom()[action](element.dom());
121 DocumentNodeElement.prototype = new DocumentElement();
123 $.extend(DocumentNodeElement.prototype, {
124 append: function(params) {
125 manipulate(this, params, 'append');
127 before: function(params) {
128 manipulate(this, params, 'before');
131 after: function(params) {
132 manipulate(this, params, 'after');
134 children: function() {
136 if(this instanceof DocumentTextElement)
140 var elementContent = this.dom().contents();
142 elementContent.each(function(idx) {
143 var childElement = DocumentElement.fromHTMLElement(this, element.canvas);
144 if(idx === 0 && elementContent.length > 1 && elementContent[1].nodeType === Node.ELEMENT_NODE && (childElement instanceof DocumentTextElement) && $.trim($(this).text()) === '')
146 if(idx > 0 && childElement instanceof DocumentTextElement) {
147 if(toret[toret.length-1] instanceof DocumentNodeElement && $.trim($(this).text()) === '')
150 toret.push(childElement);
154 childIndex: function(child) {
155 var children = this.children(),
157 children.forEach(function(c, idx) {
158 if(c.sameNode(child)) {
165 getWlxmlTag: function() {
166 return this.dom().attr('wlxml-tag');
168 setWlxmlTag: function(tag) {
169 this.dom().attr('wlxml-tag', tag);
171 getWlxmlClass: function() {
172 var klass = this.dom().attr('wlxml-class');
174 return klass.replace('-', '.');
177 setWlxmlClass: function(klass) {
179 this.dom().attr('wlxml-class', klass);
181 this.dom().removeAttr('wlxml-class');
184 if(what === 'list' && _.contains(['list-items', 'list-items-enum'], this.dom().attr('wlxml-class')))
191 // DocumentNodeElement represents a text node from WLXML document rendered inside Canvas
192 var DocumentTextElement = function(htmlElement, canvas) {
193 DocumentElement.call(this, htmlElement, canvas);
196 $.extend(DocumentTextElement, {
197 createDOM: function(params) {
199 .attr('wlxml-text', '')
203 create: function(params, canvas) {
204 return this.fromHTMLElement(this.createDOM(params)[0]);
207 fromHTMLElement: function(htmlElement, canvas) {
208 return new this(htmlElement, canvas);
212 DocumentTextElement.prototype = new DocumentElement();
214 $.extend(DocumentTextElement.prototype, {
215 _setupDOMHandler: function(htmlElement) {
216 var $element = $(htmlElement);
217 if(htmlElement.nodeType === Node.TEXT_NODE)
218 this.$element = $element.parent();
220 this.$element = $element;
222 setText: function(text) {
223 this.dom().contents()[0].data = text;
225 getText: function() {
226 return this.dom().text();
228 after: function(params) {
229 if(params instanceof DocumentTextElement || params.text)
232 if(params instanceof DocumentNodeElement) {
235 element = DocumentNodeElement.create(params);
237 this.dom().wrap('<div>');
238 this.dom().parent().after(element.dom());
242 before: function(params) {
243 if(params instanceof DocumentTextElement || params.text)
246 if(params instanceof DocumentNodeElement) {
249 element = DocumentNodeElement.create(params);
251 this.dom().wrap('<div>');
252 this.dom().parent().before(element.dom());
256 wrapWithNodeElement: function(wlxmlNode) {
257 if(typeof wlxmlNode.start === 'number' && typeof wlxmlNode.end === 'number') {
258 return this.canvas.wrapText({
259 inside: this.parent(),
260 textNodeIdx: this.parent().childIndex(this),
261 offsetStart: Math.min(wlxmlNode.start, wlxmlNode.end),
262 offsetEnd: Math.max(wlxmlNode.start, wlxmlNode.end),
263 _with: {tag: wlxmlNode.tag, klass: wlxmlNode.klass}
266 return DocumentElement.prototype.wrapWithNodeElement.call(this, wlxmlNode);
270 var parent = this.parent();
271 if(parent.children().length === 1) {
272 var grandParent = parent.parent();
274 var grandParentChildren = grandParent.children(),
275 idx = grandParent.childIndex(parent),
276 prev = idx - 1 > -1 ? grandParentChildren[idx-1] : null,
277 next = idx + 1 < grandParentChildren.length ? grandParentChildren[idx+1] : null;
279 prev.setText(prev.getText() + this.getText() + next.getText());
281 } else if (prev || next) {
282 var target = prev ? prev : next;
283 target.setText(target.getText() + this.getText());
293 split: function(params) {
294 var parentElement = this.parent(),
295 myIdx = parentElement.childIndex(this),
296 myCanvas = this.canvas,
298 succeedingChildren = [],
300 prefix = this.getText().substr(0, params.offset),
301 suffix = this.getText().substr(params.offset);
303 parentElement.children().forEach(function(child) {
305 succeedingChildren.push(child);
306 if(child.sameNode(thisElement))
310 if(prefix.length > 0)
311 this.setText(prefix);
315 var newElement = DocumentNodeElement.create({tag: parentElement.getWlxmlTag(), klass: parentElement.getWlxmlClass()}, myCanvas);
316 parentElement.after(newElement);
318 if(suffix.length > 0)
319 newElement.append({text: suffix});
320 succeedingChildren.forEach(function(child) {
321 newElement.append(child);
327 DocumentElement: DocumentElement,
328 DocumentNodeElement: DocumentNodeElement,
329 DocumentTextElement: DocumentTextElement