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 setWlxmlClass: function(klass) {
109 this.$element.attr('class', klass);
112 if(what === 'list' && _.contains(['list-items', 'list-items-enum'], this.$element.attr('wlxml-class')))
118 DocumentNodeElement.createDOM = function(params) {
121 dom = $(document.createTextNode(params.text));
123 dom = $('<div>').attr('wlxml-tag', params.tag);
125 dom.attr('wlxml-class', params.klass);
131 DocumentNodeElement.create = function(params, canvas) {
132 return documentElementFromHTMLElement(DocumentNodeElement.createDOM(params)[0]);
136 $.extend(DocumentTextElement.prototype, {
137 setText: function(text) {
138 this.$element[0].data = text;
140 getText: function() {
141 return this.$element.text();
143 after: function(params) {
144 if(params instanceof DocumentTextElement || params.text)
147 if(params instanceof DocumentNodeElement) {
150 dom = DocumentNodeElement.createDOM(params);
152 this.$element.wrap('<div>');
153 this.$element.parent().after(dom[0]);
154 this.$element.unwrap();
155 return documentElementFromHTMLElement(dom[0]);
157 before: 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().before(dom[0]);
168 this.$element.unwrap();
169 return documentElementFromHTMLElement(dom[0]);
171 wrapWithNodeElement: function(wlxmlNode) {
172 if(wlxmlNode.start && wlxmlNode.end) {
173 return this.canvas.wrapText({
174 inside: this.parent(),
175 textNodeIdx: this.parent().childIndex(this),
176 offsetStart: wlxmlNode.start,
177 offsetEnd: wlxmlNode.end,
178 _with: {tag: wlxmlNode.tag, klass: wlxmlNode.klass}
181 return DocumentElement.prototype.wrapWithNodeElement.call(this, wlxmlNode);
184 split: function(params) {
185 var parentElement = this.parent(),
186 myIdx = parentElement.childIndex(this),
187 myCanvas = this.canvas,
189 succeedingChildren = [],
191 prefix = this.getText().substr(0, params.offset),
192 suffix = this.getText().substr(params.offset);
194 parentElement.children().forEach(function(child) {
196 succeedingChildren.push(child);
197 if(child.sameNode(thisElement))
201 if(prefix.length > 0)
202 this.setText(prefix);
206 var newElement = DocumentNodeElement.create({tag: parentElement.wlxmlTag, klass: parentElement.wlxmlClass}, myCanvas);
207 parentElement.after(newElement);
209 if(suffix.length > 0)
210 newElement.append({text: suffix});
211 succeedingChildren.forEach(function(child) {
212 newElement.append(child);
217 var documentElementFromHTMLElement = function(htmlElement, canvas) {
219 // throw 'no canvas specified';
220 if(htmlElement.nodeType === Node.ELEMENT_NODE)
221 return new DocumentNodeElement(htmlElement, canvas);
222 if(htmlElement.nodeType === Node.TEXT_NODE)
223 return new DocumentTextElement(htmlElement, canvas);
227 wrap: function(htmlElement, canvas) {
228 return documentElementFromHTMLElement(htmlElement, canvas);
230 DocumentElement: DocumentElement,
231 DocumentNodeElement: DocumentNodeElement,
232 DocumentTextElement: DocumentTextElement