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, {
21 children: function() {
23 if(this instanceof DocumentTextElement)
27 var elementContent = this.$element.contents();
29 elementContent.each(function(idx) {
30 var childElement = documentElementFromHTMLElement(this, element.canvas);
31 if(idx === 0 && elementContent.length > 1 && elementContent[1].nodeType === Node.ELEMENT_NODE && (childElement instanceof DocumentTextElement) && $.trim($(this).text()) === '')
33 if(idx > 0 && childElement instanceof DocumentTextElement) {
34 if(toret[toret.length-1] instanceof DocumentNodeElement && $.trim($(this).text()) === '')
37 toret.push(childElement);
42 return documentElementFromHTMLElement(this.$element.parent()[0], this.canvas);
45 sameNode: function(other) {
46 return other && (typeof other === typeof this) && other.$element[0] === this.$element[0];
49 wrapWithNodeElement: function(wlxmlNode) {
50 var wrapper = DocumentNodeElement.create({tag: wlxmlNode.tag, klass: wlxmlNode.klass});
51 this.$element.replaceWith(wrapper.dom());
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 var klass = this.$element.attr('wlxml-class');
117 return klass.replace('-', '.');
120 setWlxmlClass: function(klass) {
122 this.$element.attr('wlxml-class', klass);
124 this.$element.removeAttr('wlxml-class');
127 if(what === 'list' && _.contains(['list-items', 'list-items-enum'], this.$element.attr('wlxml-class')))
133 DocumentNodeElement.createDOM = function(params) {
136 dom = $(document.createTextNode(params.text));
138 dom = $('<div>').attr('wlxml-tag', params.tag);
140 dom.attr('wlxml-class', params.klass);
146 DocumentNodeElement.create = function(params, canvas) {
147 return documentElementFromHTMLElement(DocumentNodeElement.createDOM(params)[0]);
151 $.extend(DocumentTextElement.prototype, {
152 setText: function(text) {
153 this.$element[0].data = text;
155 getText: function() {
156 return this.$element.text();
158 after: function(params) {
159 if(params instanceof DocumentTextElement || params.text)
162 if(params instanceof DocumentNodeElement) {
165 dom = DocumentNodeElement.createDOM(params);
167 this.$element.wrap('<div>');
168 this.$element.parent().after(dom[0]);
169 this.$element.unwrap();
170 return documentElementFromHTMLElement(dom[0]);
172 before: function(params) {
173 if(params instanceof DocumentTextElement || params.text)
176 if(params instanceof DocumentNodeElement) {
179 dom = DocumentNodeElement.createDOM(params);
181 this.$element.wrap('<div>');
182 this.$element.parent().before(dom[0]);
183 this.$element.unwrap();
184 return documentElementFromHTMLElement(dom[0]);
186 wrapWithNodeElement: function(wlxmlNode) {
187 if(typeof wlxmlNode.start === 'number' && typeof wlxmlNode.end === 'number') {
188 return this.canvas.wrapText({
189 inside: this.parent(),
190 textNodeIdx: this.parent().childIndex(this),
191 offsetStart: Math.min(wlxmlNode.start, wlxmlNode.end),
192 offsetEnd: Math.max(wlxmlNode.start, wlxmlNode.end),
193 _with: {tag: wlxmlNode.tag, klass: wlxmlNode.klass}
196 return DocumentElement.prototype.wrapWithNodeElement.call(this, wlxmlNode);
200 if(this.parent().children().length === 1) {
201 var parent = this.parent();
206 split: function(params) {
207 var parentElement = this.parent(),
208 myIdx = parentElement.childIndex(this),
209 myCanvas = this.canvas,
211 succeedingChildren = [],
213 prefix = this.getText().substr(0, params.offset),
214 suffix = this.getText().substr(params.offset);
216 parentElement.children().forEach(function(child) {
218 succeedingChildren.push(child);
219 if(child.sameNode(thisElement))
223 if(prefix.length > 0)
224 this.setText(prefix);
228 var newElement = DocumentNodeElement.create({tag: parentElement.getWlxmlTag(), klass: parentElement.getWlxmlClass()}, myCanvas);
229 parentElement.after(newElement);
231 if(suffix.length > 0)
232 newElement.append({text: suffix});
233 succeedingChildren.forEach(function(child) {
234 newElement.append(child);
239 var documentElementFromHTMLElement = function(htmlElement, canvas) {
241 // throw 'no canvas specified';
242 if(htmlElement.nodeType === Node.ELEMENT_NODE)
243 return new DocumentNodeElement(htmlElement, canvas);
244 if(htmlElement.nodeType === Node.TEXT_NODE)
245 return new DocumentTextElement(htmlElement, canvas);
249 wrap: function(htmlElement, canvas) {
250 return documentElementFromHTMLElement(htmlElement, canvas);
252 DocumentElement: DocumentElement,
253 DocumentNodeElement: DocumentNodeElement,
254 DocumentTextElement: DocumentTextElement