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);
17 $.extend(DocumentElement.prototype, {
22 return documentElementFromHTMLElement(this.$element.parent()[0], this.canvas);
25 sameNode: function(other) {
26 return other && (typeof other === typeof this) && other.$element[0] === this.$element[0];
29 wrapWithNodeElement: function(wlxmlNode) {
30 var wrapper = DocumentNodeElement.create({tag: wlxmlNode.tag, klass: wlxmlNode.klass});
31 this.$element.replaceWith(wrapper.dom());
37 this.$element.detach();
43 var DocumentNodeElement = function(htmlElement, canvas) {
44 DocumentElement.call(this, htmlElement, canvas);
47 var DocumentTextElement = function(htmlElement, canvas) {
48 DocumentElement.call(this, htmlElement, canvas);
51 DocumentNodeElement.prototype = new DocumentElement();
52 DocumentTextElement.prototype = new DocumentElement();
54 var manipulate = function(e, params, action) {
56 if(params instanceof DocumentElement) {
59 dom = DocumentNodeElement.createDOM(params);
61 e.$element[action](dom);
62 return documentElementFromHTMLElement(dom);
65 $.extend(DocumentNodeElement.prototype, {
66 append: function(params) {
67 manipulate(this, params, 'append');
69 before: function(params) {
70 manipulate(this, params, 'before');
73 after: function(params) {
74 manipulate(this, params, 'after');
76 children: function() {
78 if(this instanceof DocumentTextElement)
82 var elementContent = this.$element.contents();
84 elementContent.each(function(idx) {
85 var childElement = documentElementFromHTMLElement(this, element.canvas);
86 if(idx === 0 && elementContent.length > 1 && elementContent[1].nodeType === Node.ELEMENT_NODE && (childElement instanceof DocumentTextElement) && $.trim($(this).text()) === '')
88 if(idx > 0 && childElement instanceof DocumentTextElement) {
89 if(toret[toret.length-1] instanceof DocumentNodeElement && $.trim($(this).text()) === '')
92 toret.push(childElement);
96 childIndex: function(child) {
97 var children = this.children(),
99 children.forEach(function(c, idx) {
100 if(c.sameNode(child)) {
107 getWlxmlTag: function() {
108 return this.$element.attr('wlxml-tag');
110 setWlxmlTag: function(tag) {
111 this.$element.attr('wlxml-tag', tag);
113 getWlxmlClass: function() {
114 var klass = this.$element.attr('wlxml-class');
116 return klass.replace('-', '.');
119 setWlxmlClass: function(klass) {
121 this.$element.attr('wlxml-class', klass);
123 this.$element.removeAttr('wlxml-class');
126 if(what === 'list' && _.contains(['list-items', 'list-items-enum'], this.$element.attr('wlxml-class')))
132 DocumentNodeElement.createDOM = function(params) {
135 dom = $(document.createTextNode(params.text));
137 dom = $('<div>').attr('wlxml-tag', params.tag);
139 dom.attr('wlxml-class', params.klass);
145 DocumentNodeElement.create = function(params, canvas) {
146 return documentElementFromHTMLElement(DocumentNodeElement.createDOM(params)[0]);
150 $.extend(DocumentTextElement.prototype, {
151 setText: function(text) {
152 this.$element[0].data = text;
154 getText: function() {
155 return this.$element.text();
157 after: function(params) {
158 if(params instanceof DocumentTextElement || params.text)
161 if(params instanceof DocumentNodeElement) {
164 dom = DocumentNodeElement.createDOM(params);
166 this.$element.wrap('<div>');
167 this.$element.parent().after(dom[0]);
168 this.$element.unwrap();
169 return documentElementFromHTMLElement(dom[0]);
171 before: function(params) {
172 if(params instanceof DocumentTextElement || params.text)
175 if(params instanceof DocumentNodeElement) {
178 dom = DocumentNodeElement.createDOM(params);
180 this.$element.wrap('<div>');
181 this.$element.parent().before(dom[0]);
182 this.$element.unwrap();
183 return documentElementFromHTMLElement(dom[0]);
185 wrapWithNodeElement: function(wlxmlNode) {
186 if(typeof wlxmlNode.start === 'number' && typeof wlxmlNode.end === 'number') {
187 return this.canvas.wrapText({
188 inside: this.parent(),
189 textNodeIdx: this.parent().childIndex(this),
190 offsetStart: Math.min(wlxmlNode.start, wlxmlNode.end),
191 offsetEnd: Math.max(wlxmlNode.start, wlxmlNode.end),
192 _with: {tag: wlxmlNode.tag, klass: wlxmlNode.klass}
195 return DocumentElement.prototype.wrapWithNodeElement.call(this, wlxmlNode);
199 if(this.parent().children().length === 1) {
200 var parent = this.parent();
205 split: function(params) {
206 var parentElement = this.parent(),
207 myIdx = parentElement.childIndex(this),
208 myCanvas = this.canvas,
210 succeedingChildren = [],
212 prefix = this.getText().substr(0, params.offset),
213 suffix = this.getText().substr(params.offset);
215 parentElement.children().forEach(function(child) {
217 succeedingChildren.push(child);
218 if(child.sameNode(thisElement))
222 if(prefix.length > 0)
223 this.setText(prefix);
227 var newElement = DocumentNodeElement.create({tag: parentElement.getWlxmlTag(), klass: parentElement.getWlxmlClass()}, myCanvas);
228 parentElement.after(newElement);
230 if(suffix.length > 0)
231 newElement.append({text: suffix});
232 succeedingChildren.forEach(function(child) {
233 newElement.append(child);
238 var documentElementFromHTMLElement = function(htmlElement, canvas) {
240 // throw 'no canvas specified';
241 if(htmlElement.nodeType === Node.ELEMENT_NODE)
242 return new DocumentNodeElement(htmlElement, canvas);
243 if(htmlElement.nodeType === Node.TEXT_NODE)
244 return new DocumentTextElement(htmlElement, canvas);
248 wrap: function(htmlElement, canvas) {
249 return documentElementFromHTMLElement(htmlElement, canvas);
251 DocumentElement: DocumentElement,
252 DocumentNodeElement: DocumentNodeElement,
253 DocumentTextElement: DocumentTextElement