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 var wrapper = $('<div>');
21 wrapper.find('*').replaceWith(function() {
22 var currentTag = $(this);
23 if(currentTag.attr('wlxml-tag'))
25 var toret = $('<div>').attr('wlxml-tag', currentTag.prop('tagName').toLowerCase());
26 //toret.attr('id', 'xxxxxxxx-xxxx-xxxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {var r = Math.random()*16|0,v=c=='x'?r:r&0x3|0x8;return v.toString(16);}));
27 for(var i = 0; i < this.attributes.length; i++) {
28 var attr = this.attributes.item(i);
29 var value = attr.name === 'class' ? attr.value.replace(/\./g, '-') : attr.value;
30 toret.attr('wlxml-' + attr.name, value);
32 toret.append(currentTag.contents());
36 wrapper.find(':not(iframe)').addBack().contents()
37 .filter(function() {return this.nodeType === Node.TEXT_NODE})
42 // TODO: use DocumentElement API
43 var spanParent = el.parent().attr('wlxml-tag') === 'span',
44 spanBefore = el.prev().length > 0 && $(el.prev()[0]).attr('wlxml-tag') === 'span',
45 spanAfter = el.next().length > 0 && $(el.next()[0]).attr('wlxml-tag') === 'span';
47 if(spanParent || spanBefore || spanAfter) {
48 var startSpace = /\s/g.test(this.data.substr(0,1));
49 var endSpace = /\s/g.test(this.data.substr(-1)) && this.data.length > 1;
50 var trimmed = $.trim(this.data);
51 this.data = (startSpace && (spanParent || spanBefore) ? ' ' : '')
53 + (endSpace && (spanParent || spanAfter) ? ' ' : '');
56 var oldLength = this.data.length;
57 this.data = $.trim(this.data);
58 if(this.data.length === 0 && oldLength > 0 && el.parent().contents().length === 1)
60 if(this.data.length === 0)
65 this.d = wrapper.children(0);
75 return documentElement.wrap(this.d.get(0), this); //{wlxmlTag: this.d.prop('tagName')};
78 wrapText: function(params) {
79 params = _.extend({textNodeIdx: 0}, params);
80 if(typeof params.textNodeIdx === 'number')
81 params.textNodeIdx = [params.textNodeIdx];
83 var childrenInside = params.inside.children(),
84 idx1 = Math.min.apply(Math, params.textNodeIdx),
85 idx2 = Math.max.apply(Math, params.textNodeIdx),
86 textNode1 = childrenInside[idx1],
87 textNode2 = childrenInside[idx2],
88 sameNode = textNode1.sameNode(textNode2),
89 prefixOutside = textNode1.getText().substr(0, params.offsetStart),
90 prefixInside = textNode1.getText().substr(params.offsetStart),
91 suffixInside = textNode2.getText().substr(0, params.offsetEnd),
92 suffixOutside = textNode2.getText().substr(params.offsetEnd)
95 var wrapperElement = documentElement.DocumentNodeElement.create({tag: params._with.tag, klass: params._with.klass});
96 textNode1.after(wrapperElement);
99 if(prefixOutside.length > 0)
100 wrapperElement.before({text:prefixOutside});
102 var core = textNode1.getText().substr(params.offsetStart, params.offsetEnd - params.offsetStart);
103 wrapperElement.append({text: core});
106 if(prefixInside.length > 0)
107 wrapperElement.append({text: prefixInside});
108 for(var i = idx1 + 1; i < idx2; i++) {
109 wrapperElement.append(childrenInside[i]);
111 if(suffixInside.length > 0)
112 wrapperElement.append({text: suffixInside});
114 if(suffixOutside.length > 0)
115 wrapperElement.after({text: suffixOutside});
116 return wrapperElement;
118 getDocumentElement: function(from) {
119 if(from instanceof HTMLElement) {
120 return documentElement.wrap(from, this);
126 $.extend(Canvas.prototype.list, {
127 create: function(params) {
128 if(!(params.element1.parent().sameNode(params.element2.parent())))
131 var parent = params.element1.parent();
133 if(parent.childIndex(params.element1) > parent.childIndex(params.element2)) {
134 var tmp = params.element1;
135 params.element1 = params.element2;
136 params.element2 = tmp;
139 var elementsToWrap = [];
141 var place = 'before';
143 parent.children().some(function(element) {
145 if(element.sameNode(params.element1))
147 if(place === 'inside') {
148 if(element instanceof documentElement.DocumentTextElement) {
149 element = element.wrapWithNodeElement({tag: 'div', klass: 'list.item'});
150 if(element.children()[0].sameNode(params.element1))
151 params.element1 = element;
153 element.setWlxmlClass('item');
154 elementsToWrap.push(element);
156 if(_e.sameNode(params.element2))
160 var listElement = documentElement.DocumentNodeElement.create({tag: 'div', klass: 'list-items' + (params.type === 'enum' ? '-enum' : '')});
163 if(parent.is('list')) {
164 listElement.wrapWithNodeElement({tag: 'div', klass: 'item'});
165 toret = listElement.parent();
170 params.element1.before(toret);
172 elementsToWrap.forEach(function(element) {
174 listElement.append(element);
177 extractItems: function(params) {
178 params = _.extend({merge: true}, params);
179 var list = params.element1.parent();
180 if(!list.is('list') || !(list.sameNode(params.element2.parent())))
183 var idx1 = list.childIndex(params.element1),
184 idx2 = list.childIndex(params.element2),
187 succeedingItems = [],
188 items = list.children(),
189 listIsNested = list.parent().getWlxmlClass() === 'item',
193 var tmp = idx1; idx1 = idx2; idx2 = tmp;
196 items.forEach(function(item, idx) {
198 precedingItems.push(item);
199 else if(idx >= idx1 && idx <= idx2) {
200 extractedItems.push(item);
203 succeedingItems.push(item);
207 var reference = listIsNested ? list.parent() : list;
208 if(succeedingItems.length === 0) {
209 var reference_orig = reference;
210 extractedItems.forEach(function(item) {
211 reference.after(item);
214 item.setWlxmlClass(null);
216 if(precedingItems.length === 0)
217 reference_orig.detach();
218 } else if(precedingItems.length === 0) {
219 extractedItems.forEach(function(item) {
220 reference.before(item);
222 item.setWlxmlClass(null);
225 extractedItems.forEach(function(item) {
226 reference.after(item);
228 item.setWlxmlClass(null);
231 var secondList = documentElement.DocumentNodeElement.create({tag: 'div', klass:'list-items'}, this),
235 toAdd = secondList.wrapWithNodeElement({tag: 'div', klass:'item'});
237 succeedingItems.forEach(function(item) {
238 secondList.append(item);
241 reference.after(toAdd);
243 if(!params.merge && listIsNested) {
244 return this.extractItems({element1: extractedItems[0], element2: extractedItems[extractedItems.length-1]});
248 areItemsOfTheSameList: function(params) {
249 var e1 = params.element1,
250 e2 = params.element2;
251 return e1.parent().sameNode(e2.parent())
252 && e1.parent().is('list');
257 fromXML: function(xml) {
258 return new Canvas(xml);