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 this.data = $.trim(this.data);
60 this.d = wrapper.children(0);
70 return documentElement.wrap(this.d.get(0), this); //{wlxmlTag: this.d.prop('tagName')};
73 wrapText: function(params) {
74 params = _.extend({textNodeIdx: 0}, params);
75 if(typeof params.textNodeIdx === 'number')
76 params.textNodeIdx = [params.textNodeIdx];
78 var childrenInside = params.inside.children(),
79 idx1 = Math.min.apply(Math, params.textNodeIdx),
80 idx2 = Math.max.apply(Math, params.textNodeIdx),
81 textNode1 = childrenInside[idx1],
82 textNode2 = childrenInside[idx2],
83 sameNode = textNode1.sameNode(textNode2),
84 prefixOutside = textNode1.getText().substr(0, params.offsetStart),
85 prefixInside = textNode1.getText().substr(params.offsetStart),
86 suffixInside = textNode2.getText().substr(0, params.offsetEnd),
87 suffixOutside = textNode2.getText().substr(params.offsetEnd)
90 var wrapperElement = documentElement.DocumentNodeElement.create({tag: params._with.tag, klass: params._with.klass});
91 textNode1.after(wrapperElement);
94 if(prefixOutside.length > 0)
95 wrapperElement.before({text:prefixOutside});
97 var core = textNode1.getText().substr(params.offsetStart, params.offsetEnd - params.offsetStart);
98 wrapperElement.append({text: core});
101 if(prefixInside.length > 0)
102 wrapperElement.append({text: prefixInside});
103 for(var i = idx1 + 1; i < idx2; i++) {
104 wrapperElement.append(childrenInside[i]);
106 if(suffixInside.length > 0)
107 wrapperElement.append({text: suffixInside});
109 if(suffixOutside.length > 0)
110 wrapperElement.after({text: suffixOutside});
111 return wrapperElement;
116 $.extend(Canvas.prototype.list, {
117 create: function(params) {
118 if(!(params.element1.parent().sameNode(params.element2.parent())))
121 var parent = params.element1.parent();
123 if(parent.childIndex(params.element1) > parent.childIndex(params.element2)) {
124 var tmp = params.element1;
125 params.element1 = params.element2;
126 params.element2 = tmp;
129 var elementsToWrap = [];
131 var place = 'before';
133 parent.children().forEach(function(element) {
134 if(element.sameNode(params.element1))
136 if(place === 'inside') {
137 if(element instanceof documentElement.DocumentTextElement) {
138 element = element.wrapWithNodeElement({tag: 'div', klass: 'list.item'});
139 if(element.children()[0].sameNode(params.element1))
140 params.element1 = element;
142 element.setWlxmlClass('item');
143 elementsToWrap.push(element);
145 if(element.sameNode(params.element2))
149 var listElement = documentElement.DocumentNodeElement.create({tag: 'div', klass: 'list-items' + (params.type === 'enum' ? '-enum' : '')});
152 if(parent.is('list')) {
153 listElement.wrap({tag: 'div', klass: 'item'});
154 toret = listElement.parent();
159 params.element1.before(toret);
161 elementsToWrap.forEach(function(element) {
163 listElement.append(element);
169 fromXML: function(xml) {
170 return new Canvas(xml);