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);
16 this.wlxmlTag = this.$element.attr('wlxml-tag');
19 $.extend(DocumentElement.prototype, {
23 children: function() {
25 if(this instanceof DocumentTextElement)
29 var elementContent = this.$element.contents();
31 elementContent.each(function(idx) {
32 var childElement = documentElementFromHTMLElement(this, element.canvas);
33 if(idx === 0 && elementContent.length > 1 && elementContent[1].nodeType === Node.ELEMENT_NODE && (childElement instanceof DocumentTextElement) && $.trim($(this).text()) === '')
35 if(idx > 0 && childElement instanceof DocumentTextElement) {
36 if(toret[toret.length-1] instanceof DocumentNodeElement && $.trim($(this).text()) === '')
39 toret.push(childElement);
44 return documentElementFromHTMLElement(this.$element.parent()[0], this.canvas);
47 sameNode: function(other) {
48 return other && (typeof other === typeof this) && other.$element[0] === this.$element[0];
51 wrapWithNodeElement: function(wlxmlNode) {
52 this.$element.wrap($('<' + wlxmlNode.tag + ' class="' + wlxmlNode.klass.replace('.', '-') + '">')[0]);
53 return documentElementFromHTMLElement(this.$element.parent().get(0), this.canvas);
56 childIndex: function(child) {
57 var children = this.children(),
59 children.forEach(function(c, idx) {
60 if(c.sameNode(child)) {
69 this.$element.detach();
75 var DocumentNodeElement = function(htmlElement, canvas) {
76 DocumentElement.call(this, htmlElement, canvas);
79 var DocumentTextElement = function(htmlElement, canvas) {
80 DocumentElement.call(this, htmlElement, canvas);
83 DocumentNodeElement.prototype = new DocumentElement();
84 DocumentTextElement.prototype = new DocumentElement();
86 var manipulate = function(e, params, action) {
88 if(params instanceof DocumentElement) {
91 dom = DocumentNodeElement.createDOM(params);
93 e.$element[action](dom);
94 return documentElementFromHTMLElement(dom);
97 $.extend(DocumentNodeElement.prototype, {
98 append: function(params) {
99 manipulate(this, params, 'append');
101 before: function(params) {
102 manipulate(this, params, 'before');
105 after: function(params) {
106 manipulate(this, params, 'after');
108 getWlxmlTag: function() {
109 return this.$element.attr('wlxml-tag');
111 setWlxmlTag: function(tag) {
112 this.$element.attr('wlxml-tag', tag);
114 getWlxmlClass: function() {
115 return this.$element.attr('wlxml-class').replace('-', '.');
117 setWlxmlClass: function(klass) {
118 this.$element.attr('wlxml-class', klass);
121 if(what === 'list' && _.contains(['list-items', 'list-items-enum'], this.$element.attr('wlxml-class')))
127 DocumentNodeElement.createDOM = function(params) {
130 dom = $(document.createTextNode(params.text));
132 dom = $('<div>').attr('wlxml-tag', params.tag);
134 dom.attr('wlxml-class', params.klass);
140 DocumentNodeElement.create = function(params, canvas) {
141 return documentElementFromHTMLElement(DocumentNodeElement.createDOM(params)[0]);
145 $.extend(DocumentTextElement.prototype, {
146 setText: function(text) {
147 this.$element[0].data = text;
149 getText: function() {
150 return this.$element.text();
152 after: function(params) {
153 if(params instanceof DocumentTextElement || params.text)
156 if(params instanceof DocumentNodeElement) {
159 dom = DocumentNodeElement.createDOM(params);
161 this.$element.wrap('<div>');
162 this.$element.parent().after(dom[0]);
163 this.$element.unwrap();
164 return documentElementFromHTMLElement(dom[0]);
166 before: function(params) {
167 if(params instanceof DocumentTextElement || params.text)
170 if(params instanceof DocumentNodeElement) {
173 dom = DocumentNodeElement.createDOM(params);
175 this.$element.wrap('<div>');
176 this.$element.parent().before(dom[0]);
177 this.$element.unwrap();
178 return documentElementFromHTMLElement(dom[0]);
180 wrapWithNodeElement: function(wlxmlNode) {
181 if(wlxmlNode.start && wlxmlNode.end) {
182 return this.canvas.wrapText({
183 inside: this.parent(),
184 textNodeIdx: this.parent().childIndex(this),
185 offsetStart: wlxmlNode.start,
186 offsetEnd: wlxmlNode.end,
187 _with: {tag: wlxmlNode.tag, klass: wlxmlNode.klass}
190 return DocumentElement.prototype.wrapWithNodeElement.call(this, wlxmlNode);
193 split: function(params) {
194 var parentElement = this.parent(),
195 myIdx = parentElement.childIndex(this),
196 myCanvas = this.canvas,
198 succeedingChildren = [],
200 prefix = this.getText().substr(0, params.offset),
201 suffix = this.getText().substr(params.offset);
203 parentElement.children().forEach(function(child) {
205 succeedingChildren.push(child);
206 if(child.sameNode(thisElement))
210 if(prefix.length > 0)
211 this.setText(prefix);
215 var newElement = DocumentNodeElement.create({tag: parentElement.wlxmlTag, klass: parentElement.wlxmlClass}, myCanvas);
216 parentElement.after(newElement);
218 if(suffix.length > 0)
219 newElement.append({text: suffix});
220 succeedingChildren.forEach(function(child) {
221 newElement.append(child);
226 var documentElementFromHTMLElement = function(htmlElement, canvas) {
228 // throw 'no canvas specified';
229 if(htmlElement.nodeType === Node.ELEMENT_NODE)
230 return new DocumentNodeElement(htmlElement, canvas);
231 if(htmlElement.nodeType === Node.TEXT_NODE)
232 return new DocumentTextElement(htmlElement, canvas);
236 wrap: function(htmlElement, canvas) {
237 return documentElementFromHTMLElement(htmlElement, canvas);
239 DocumentElement: DocumentElement,
240 DocumentNodeElement: DocumentNodeElement,
241 DocumentTextElement: DocumentTextElement