creating a list
[fnpeditor.git] / modules / documentCanvas / canvas / documentElement.js
1 define([
2 'libs/jquery-1.9.1.min',
3 'libs/underscore-min'
4 ], function($, _) {
5     
6 'use strict';
7
8
9 // DocumentElement represents a node from WLXML document rendered inside Canvas
10 var DocumentElement = function(htmlElement, canvas) {
11     if(arguments.length === 0)
12         return;
13     this.canvas = canvas;
14     this.$element = $(htmlElement);
15
16     var tagNameProp = this.$element.prop('tagName');
17     this.wlxmlTag = tagNameProp ? tagNameProp.toLowerCase() : undefined;
18 };
19
20 $.extend(DocumentElement.prototype, {
21     dom: function() {
22         return this.$element;
23     },
24     children: function() {
25         var toret = [];
26         if(this instanceof DocumentTextElement)
27             return toret;
28
29
30         var elementContent = this.$element.contents();
31         var element = this;
32         elementContent.each(function(idx) {
33             var childElement = documentElementFromHTMLElement(this, element.canvas);
34             if(idx === 0 && elementContent.length > 1 && elementContent[1].nodeType === Node.ELEMENT_NODE && (childElement instanceof DocumentTextElement) && $.trim($(this).text()) === '')
35                 return true;
36             if(idx > 0 && childElement instanceof DocumentTextElement) {
37                 if(toret[toret.length-1] instanceof DocumentNodeElement && $.trim($(this).text()) === '')
38                     return true;
39             }
40             toret.push(childElement);
41         });
42         return toret;
43     },
44     parent: function() {
45         return documentElementFromHTMLElement(this.$element.parent()[0], this.canvas);
46     },
47
48     sameNode: function(other) {
49         return other && (typeof other === typeof this) && other.$element[0] === this.$element[0];
50     },
51
52     wrapWithNodeElement: function(wlxmlNode) {
53         this.$element.wrap($('<' + wlxmlNode.tag + ' class="' + wlxmlNode.klass.replace('.', '-') + '">')[0]);
54         return documentElementFromHTMLElement(this.$element.parent().get(0), this.canvas);
55     },
56
57     childIndex: function(child) {
58         var children = this.children(),
59             toret = null;
60         children.forEach(function(c, idx) {
61             if(c.sameNode(child)) {
62                 toret = idx;
63                 return false;
64             }
65         });
66         return toret;
67     },
68
69     detach: function() {
70         this.$element.detach();
71         this.canvas = null;
72     }
73 });
74
75
76 var DocumentNodeElement = function(htmlElement, canvas) {
77     DocumentElement.call(this, htmlElement, canvas);
78 };
79
80 var DocumentTextElement = function(htmlElement, canvas) {
81     DocumentElement.call(this, htmlElement, canvas);
82 };
83
84 DocumentNodeElement.prototype = new DocumentElement();
85 DocumentTextElement.prototype = new DocumentElement();
86
87 var manipulate = function(e, params, action) {
88     var dom;
89     if(params instanceof DocumentElement) {
90         dom = params.dom()
91     } else {
92         dom = DocumentNodeElement.createDOM(params);
93     }
94     e.$element[action](dom);
95     return documentElementFromHTMLElement(dom);
96 };
97
98 $.extend(DocumentNodeElement.prototype, {
99     append: function(params) {
100         manipulate(this, params, 'append');
101     },
102     before: function(params) {
103         manipulate(this, params, 'before');
104
105     },
106     after: function(params) {
107         manipulate(this, params, 'after');
108     },
109     setWlxmlClass: function(klass) {
110         this.$element.attr('class', klass);
111     },
112     is: function(what) {
113         if(what === 'list' && _.contains(['list-items', 'list-items-enum'], this.$element.attr('class')))
114             return true;
115         return false;
116     }
117 });
118
119 DocumentNodeElement.createDOM = function(params) {
120     var dom;
121     if(params.text) {
122         dom = $(document.createTextNode(params.text));
123     } else {
124         dom = $('<' + params.tag + '>');
125         if(params.klass)
126             dom.attr('class', params.klass);
127     }
128     return dom;
129 };
130
131
132 DocumentNodeElement.create = function(params, canvas) {
133     return documentElementFromHTMLElement(DocumentNodeElement.createDOM(params)[0]);
134 };
135
136
137 $.extend(DocumentTextElement.prototype, {
138     setText: function(text) {
139         this.$element[0].data = text;
140     },
141     getText: function() {
142         return this.$element.text();
143     },
144     after: function(params) {
145         if(params instanceof DocumentTextElement || params.text)
146             return false;
147         var dom;
148         if(params instanceof DocumentNodeElement) {
149             dom = params.dom();
150         } else {
151             dom = DocumentNodeElement.createDOM(params);
152         }
153         this.$element.wrap('<div>');
154         this.$element.parent().after(dom[0]);
155         this.$element.unwrap();
156         return documentElementFromHTMLElement(dom[0]);
157     },
158     before: function(params) {
159         if(params instanceof DocumentTextElement || params.text)
160             return false;
161         var dom;
162         if(params instanceof DocumentNodeElement) {
163             dom = params.dom();
164         } else {
165             dom = DocumentNodeElement.createDOM(params);
166         }
167         this.$element.wrap('<div>');
168         this.$element.parent().before(dom[0]);
169         this.$element.unwrap();
170         return documentElementFromHTMLElement(dom[0]);
171     },
172     wrapWithNodeElement: function(wlxmlNode) {
173         if(wlxmlNode.start && wlxmlNode.end) {
174             return this.canvas.wrapText({
175                 inside: this.parent(),
176                 textNodeIdx: this.parent().childIndex(this),
177                 offsetStart: wlxmlNode.start,
178                 offsetEnd: wlxmlNode.end,
179                 _with: {tag: wlxmlNode.tag, klass: wlxmlNode.klass}
180             });
181         } else {
182             return DocumentElement.prototype.wrapWithNodeElement.call(this, wlxmlNode);
183         }
184     },
185     split: function(params) {
186         var parentElement = this.parent(),
187             myIdx = parentElement.childIndex(this),
188             myCanvas = this.canvas,
189             passed = false,
190             succeedingChildren = [],
191             thisElement = this,
192             prefix = this.getText().substr(0, params.offset),
193             suffix = this.getText().substr(params.offset);
194
195         parentElement.children().forEach(function(child) {
196             if(passed)
197                 succeedingChildren.push(child);
198             if(child.sameNode(thisElement))
199                 passed = true;
200         });
201
202         if(prefix.length > 0)
203             this.setText(prefix);
204         else
205             this.remove();
206         
207         var newElement = DocumentNodeElement.create({tag: parentElement.wlxmlTag, klass: parentElement.wlxmlClass}, myCanvas);
208         parentElement.after(newElement);
209
210         if(suffix.length > 0)
211             newElement.append({text: suffix});
212         succeedingChildren.forEach(function(child) {
213             newElement.append(child);
214         });
215     }
216 });
217
218 var documentElementFromHTMLElement = function(htmlElement, canvas) {
219     // if(!canvas)
220     //    throw 'no canvas specified';
221     if(htmlElement.nodeType === Node.ELEMENT_NODE)
222         return new DocumentNodeElement(htmlElement, canvas);
223     if(htmlElement.nodeType === Node.TEXT_NODE)
224         return new DocumentTextElement(htmlElement, canvas);
225 };
226
227 return {
228     wrap: function(htmlElement, canvas) {
229         return documentElementFromHTMLElement(htmlElement, canvas);
230     },
231     DocumentElement: DocumentElement,
232     DocumentNodeElement: DocumentNodeElement,
233     DocumentTextElement: DocumentTextElement
234 };
235
236 });