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);
85 getVerticallyFirstTextElement: function() {
87 this.children().some(function(child) {
88 if(!child.isVisible())
89 return false; // continue
90 if(child instanceof DocumentTextElement) {
94 toret = child.getVerticallyFirstTextElement();
102 isVisible: function() {
103 return this instanceof DocumentTextElement || this.getWlxmlTag() !== 'metadata';
108 // DocumentNodeElement represents an element node from WLXML document rendered inside Canvas
109 var DocumentNodeElement = function(htmlElement, canvas) {
110 DocumentElement.call(this, htmlElement, canvas);
113 $.extend(DocumentNodeElement, {
114 createDOM: function(params) {
116 .attr('wlxml-tag', params.tag);
118 dom.attr('wlxml-class', params.klass.replace(/\./g, '-'));
122 create: function(params, canvas) {
123 return this.fromHTMLElement(this.createDOM(params)[0]);
126 fromHTMLElement: function(htmlElement, canvas) {
127 return new this(htmlElement, canvas);
131 var manipulate = function(e, params, action) {
133 if(params instanceof DocumentElement) {
136 element = DocumentElement.create(params);
138 e.dom()[action](element.dom());
142 DocumentNodeElement.prototype = new DocumentElement();
144 $.extend(DocumentNodeElement.prototype, {
145 append: function(params) {
146 return manipulate(this, params, 'append');
148 before: function(params) {
149 return manipulate(this, params, 'before');
152 after: function(params) {
153 return manipulate(this, params, 'after');
155 children: function() {
157 if(this instanceof DocumentTextElement)
161 var elementContent = this.dom().contents();
163 elementContent.each(function(idx) {
164 var childElement = DocumentElement.fromHTMLElement(this, element.canvas);
165 if(idx === 0 && elementContent.length > 1 && elementContent[1].nodeType === Node.ELEMENT_NODE && (childElement instanceof DocumentTextElement) && $.trim($(this).text()) === '')
167 if(idx > 0 && childElement instanceof DocumentTextElement) {
168 if(toret[toret.length-1] instanceof DocumentNodeElement && $.trim($(this).text()) === '')
171 toret.push(childElement);
175 childIndex: function(child) {
176 var children = this.children(),
178 children.forEach(function(c, idx) {
179 if(c.sameNode(child)) {
186 getWlxmlTag: function() {
187 return this.dom().attr('wlxml-tag');
189 setWlxmlTag: function(tag) {
190 this.dom().attr('wlxml-tag', tag);
192 getWlxmlClass: function() {
193 var klass = this.dom().attr('wlxml-class');
195 return klass.replace(/-/g, '.');
198 setWlxmlClass: function(klass) {
200 this.dom().attr('wlxml-class', klass.replace(/\./g, '-'));
202 this.dom().removeAttr('wlxml-class');
205 if(what === 'list' && _.contains(['list-items', 'list-items-enum'], this.dom().attr('wlxml-class')))
212 // DocumentNodeElement represents a text node from WLXML document rendered inside Canvas
213 var DocumentTextElement = function(htmlElement, canvas) {
214 DocumentElement.call(this, htmlElement, canvas);
217 $.extend(DocumentTextElement, {
218 createDOM: function(params) {
220 .attr('wlxml-text', '')
224 create: function(params, canvas) {
225 return this.fromHTMLElement(this.createDOM(params)[0]);
228 fromHTMLElement: function(htmlElement, canvas) {
229 return new this(htmlElement, canvas);
233 DocumentTextElement.prototype = new DocumentElement();
235 $.extend(DocumentTextElement.prototype, {
236 _setupDOMHandler: function(htmlElement) {
237 var $element = $(htmlElement);
238 if(htmlElement.nodeType === Node.TEXT_NODE)
239 this.$element = $element.parent();
241 this.$element = $element;
243 setText: function(text) {
244 this.dom().contents()[0].data = text;
246 getText: function() {
247 return this.dom().text();
249 after: function(params) {
250 if(params instanceof DocumentTextElement || params.text)
253 if(params instanceof DocumentNodeElement) {
256 element = DocumentNodeElement.create(params);
258 this.dom().wrap('<div>');
259 this.dom().parent().after(element.dom());
263 before: function(params) {
264 if(params instanceof DocumentTextElement || params.text)
267 if(params instanceof DocumentNodeElement) {
270 element = DocumentNodeElement.create(params);
272 this.dom().wrap('<div>');
273 this.dom().parent().before(element.dom());
277 wrapWithNodeElement: function(wlxmlNode) {
278 if(typeof wlxmlNode.start === 'number' && typeof wlxmlNode.end === 'number') {
279 return this.canvas.wrapText({
280 inside: this.parent(),
281 textNodeIdx: this.parent().childIndex(this),
282 offsetStart: Math.min(wlxmlNode.start, wlxmlNode.end),
283 offsetEnd: Math.max(wlxmlNode.start, wlxmlNode.end),
284 _with: {tag: wlxmlNode.tag, klass: wlxmlNode.klass}
287 return DocumentElement.prototype.wrapWithNodeElement.call(this, wlxmlNode);
291 var parent = this.parent();
292 if(parent.children().length === 1) {
293 var grandParent = parent.parent();
295 var grandParentChildren = grandParent.children(),
296 idx = grandParent.childIndex(parent),
297 prev = idx - 1 > -1 ? grandParentChildren[idx-1] : null,
298 next = idx + 1 < grandParentChildren.length ? grandParentChildren[idx+1] : null;
300 prev.setText(prev.getText() + this.getText() + next.getText());
302 } else if (prev || next) {
303 var target = prev ? prev : next;
304 target.setText(target.getText() + this.getText());
314 split: function(params) {
315 var parentElement = this.parent(),
316 myIdx = parentElement.childIndex(this),
317 myCanvas = this.canvas,
319 succeedingChildren = [],
321 prefix = this.getText().substr(0, params.offset),
322 suffix = this.getText().substr(params.offset);
324 parentElement.children().forEach(function(child) {
326 succeedingChildren.push(child);
327 if(child.sameNode(thisElement))
331 if(prefix.length > 0)
332 this.setText(prefix);
336 var newElement = DocumentNodeElement.create({tag: parentElement.getWlxmlTag(), klass: parentElement.getWlxmlClass()}, myCanvas);
337 parentElement.after(newElement);
339 if(suffix.length > 0)
340 newElement.append({text: suffix});
341 succeedingChildren.forEach(function(child) {
342 newElement.append(child);
345 return {first: parentElement, second: newElement};
350 DocumentElement: DocumentElement,
351 DocumentNodeElement: DocumentNodeElement,
352 DocumentTextElement: DocumentTextElement