2 'libs/jquery-1.9.1.min',
4 'modules/documentCanvas/canvas/documentElement'
5 ], function($, _, documentElement) {
9 var Canvas = function(wlxml) {
10 this.loadWlxml(wlxml);
13 $.extend(Canvas.prototype, {
15 loadWlxml: function(wlxml) {
16 var d = wlxml ? $($.trim(wlxml)) : null;
18 this.wrapper = $('<div>').addClass('canvas-wrapper').attr('contenteditable', true);
19 this.wrapper.append(d);
21 this.wrapper.find('*').replaceWith(function() {
22 var currentTag = $(this);
23 if(currentTag.attr('wlxml-tag'))
25 var element = documentElement.DocumentNodeElement.createDOM({
26 tag: currentTag.prop('tagName').toLowerCase(),
27 klass: currentTag.attr('class')
30 element.append(currentTag.contents());
34 this.wrapper.find(':not(iframe)').addBack().contents()
35 .filter(function() {return this.nodeType === Node.TEXT_NODE})
40 // TODO: use DocumentElement API
41 var spanParent = el.parent().attr('wlxml-tag') === 'span',
42 spanBefore = el.prev().length > 0 && $(el.prev()[0]).attr('wlxml-tag') === 'span',
43 spanAfter = el.next().length > 0 && $(el.next()[0]).attr('wlxml-tag') === 'span';
45 if(spanParent || spanBefore || spanAfter) {
46 var startSpace = /\s/g.test(this.data.substr(0,1));
47 var endSpace = /\s/g.test(this.data.substr(-1)) && this.data.length > 1;
48 var trimmed = $.trim(this.data);
49 this.data = (startSpace && (spanParent || spanBefore) ? ' ' : '')
51 + (endSpace && (spanParent || spanAfter) ? ' ' : '');
54 var oldLength = this.data.length;
55 this.data = $.trim(this.data);
56 if(this.data.length === 0 && oldLength > 0 && el.parent().contents().length === 1)
58 if(this.data.length === 0) {
60 return true; // continue
64 var element = documentElement.DocumentTextElement.create({text: this.data});
65 $(this).replaceWith(element.dom());
68 this.d = this.wrapper.children(0);
81 return documentElement.DocumentNodeElement.fromHTMLElement(this.d.get(0), this); //{wlxmlTag: this.d.prop('tagName')};
84 wrapText: function(params) {
85 params = _.extend({textNodeIdx: 0}, params);
86 if(typeof params.textNodeIdx === 'number')
87 params.textNodeIdx = [params.textNodeIdx];
89 var childrenInside = params.inside.children(),
90 idx1 = Math.min.apply(Math, params.textNodeIdx),
91 idx2 = Math.max.apply(Math, params.textNodeIdx),
92 textNode1 = childrenInside[idx1],
93 textNode2 = childrenInside[idx2],
94 sameNode = textNode1.sameNode(textNode2),
95 prefixOutside = textNode1.getText().substr(0, params.offsetStart),
96 prefixInside = textNode1.getText().substr(params.offsetStart),
97 suffixInside = textNode2.getText().substr(0, params.offsetEnd),
98 suffixOutside = textNode2.getText().substr(params.offsetEnd)
101 var wrapperElement = documentElement.DocumentNodeElement.create({tag: params._with.tag, klass: params._with.klass});
102 textNode1.after(wrapperElement);
105 if(prefixOutside.length > 0)
106 wrapperElement.before({text:prefixOutside});
108 var core = textNode1.getText().substr(params.offsetStart, params.offsetEnd - params.offsetStart);
109 wrapperElement.append({text: core});
112 if(prefixInside.length > 0)
113 wrapperElement.append({text: prefixInside});
114 for(var i = idx1 + 1; i < idx2; i++) {
115 wrapperElement.append(childrenInside[i]);
117 if(suffixInside.length > 0)
118 wrapperElement.append({text: suffixInside});
120 if(suffixOutside.length > 0)
121 wrapperElement.after({text: suffixOutside});
122 return wrapperElement;
124 getDocumentElement: function(from) {
125 if(from instanceof HTMLElement || from instanceof Text) {
126 return documentElement.DocumentElement.fromHTMLElement(from, this);
129 getCursor: function() {
130 return new Cursor(this);
137 $.extend(Canvas.prototype.list, {
138 create: function(params) {
139 if(!(params.element1.parent().sameNode(params.element2.parent())))
142 var parent = params.element1.parent();
144 if(parent.childIndex(params.element1) > parent.childIndex(params.element2)) {
145 var tmp = params.element1;
146 params.element1 = params.element2;
147 params.element2 = tmp;
150 var elementsToWrap = [];
152 var place = 'before';
154 parent.children().some(function(element) {
156 if(element.sameNode(params.element1))
158 if(place === 'inside') {
159 if(element instanceof documentElement.DocumentTextElement) {
160 element = element.wrapWithNodeElement({tag: 'div', klass: 'list.item'});
161 if(element.children()[0].sameNode(params.element1))
162 params.element1 = element;
164 element.setWlxmlClass('item');
165 elementsToWrap.push(element);
167 if(_e.sameNode(params.element2))
171 var listElement = documentElement.DocumentNodeElement.create({tag: 'div', klass: 'list-items' + (params.type === 'enum' ? '-enum' : '')});
174 if(parent.is('list')) {
175 listElement.wrapWithNodeElement({tag: 'div', klass: 'item'});
176 toret = listElement.parent();
181 params.element1.before(toret);
183 elementsToWrap.forEach(function(element) {
185 listElement.append(element);
188 extractItems: function(params) {
189 params = _.extend({merge: true}, params);
190 var list = params.element1.parent();
191 if(!list.is('list') || !(list.sameNode(params.element2.parent())))
194 var idx1 = list.childIndex(params.element1),
195 idx2 = list.childIndex(params.element2),
198 succeedingItems = [],
199 items = list.children(),
200 listIsNested = list.parent().getWlxmlClass() === 'item',
204 var tmp = idx1; idx1 = idx2; idx2 = tmp;
207 items.forEach(function(item, idx) {
209 precedingItems.push(item);
210 else if(idx >= idx1 && idx <= idx2) {
211 extractedItems.push(item);
214 succeedingItems.push(item);
218 var reference = listIsNested ? list.parent() : list;
219 if(succeedingItems.length === 0) {
220 var reference_orig = reference;
221 extractedItems.forEach(function(item) {
222 reference.after(item);
225 item.setWlxmlClass(null);
227 if(precedingItems.length === 0)
228 reference_orig.detach();
229 } else if(precedingItems.length === 0) {
230 extractedItems.forEach(function(item) {
231 reference.before(item);
233 item.setWlxmlClass(null);
236 extractedItems.forEach(function(item) {
237 reference.after(item);
239 item.setWlxmlClass(null);
242 var secondList = documentElement.DocumentNodeElement.create({tag: 'div', klass:'list-items'}, this),
246 toAdd = secondList.wrapWithNodeElement({tag: 'div', klass:'item'});
248 succeedingItems.forEach(function(item) {
249 secondList.append(item);
252 reference.after(toAdd);
254 if(!params.merge && listIsNested) {
255 return this.extractItems({element1: extractedItems[0], element2: extractedItems[extractedItems.length-1]});
259 areItemsOfTheSameList: function(params) {
260 var e1 = params.element1,
261 e2 = params.element2;
262 return e1.parent().sameNode(e2.parent())
263 && e1.parent().is('list');
268 var Cursor = function(canvas) {
269 this.canvas = canvas;
272 $.extend(Cursor.prototype, {
273 isSelecting: function() {
274 var selection = window.getSelection();
275 return !selection.isCollapsed;
277 isSelectingWithinElement: function() {
278 return this.isSelecting() && this.getSelectionStart().element.sameNode(this.getSelectionEnd().element);
280 isSelectingSiblings: function() {
281 return this.isSelecting() && this.getSelectionStart().element.parent().sameNode(this.getSelectionEnd().element.parent());
283 getPosition: function() {
284 return this.getSelectionAnchor();
286 getSelectionStart: function() {
287 return this.getSelectionBoundry('start');
289 getSelectionEnd: function() {
290 return this.getSelectionBoundry('end');
292 getSelectionAnchor: function() {
293 return this.getSelectionBoundry('anchor');
295 getSelectionBoundry: function(which) {
296 var selection = window.getSelection(),
297 anchorElement = this.canvas.getDocumentElement(selection.anchorNode),
298 focusElement = this.canvas.getDocumentElement(selection.focusNode);
300 if(which === 'anchor') {
302 element: anchorElement,
303 offset: selection.anchorOffset
310 if(anchorElement.parent().sameNode(focusElement.parent())) {
311 var parent = anchorElement.parent(),
312 anchorFirst = parent.childIndex(anchorElement) < parent.childIndex(focusElement);
314 if(which === 'start') {
315 element = anchorElement;
316 offset = selection.anchorOffset;
318 else if(which === 'end') {
319 element = focusElement,
320 offset = selection.focusOffset;
323 if(which === 'start') {
324 element = focusElement,
325 offset = selection.focusOffset
327 else if(which === 'end') {
328 element = anchorElement;
329 offset = selection.anchorOffset;
333 // TODO: Handle order
334 if(which === 'start') {
335 element = anchorElement;
336 offset = selection.anchorOffset
338 element = focusElement;
339 offset = selection.focusOffset
351 fromXML: function(xml) {
352 return new Canvas(xml);