ba7daef04042a011f6b9c43b91e23e407de0a831
[fnpeditor.git] / modules / documentCanvas / canvas / canvas.js
1 define([
2 'libs/jquery-1.9.1.min',
3 'libs/underscore-min',
4 'modules/documentCanvas/canvas/documentElement'
5 ], function($, _, documentElement) {
6     
7 'use strict';
8
9 var Canvas = function(wlxml) {
10     this.loadWlxml(wlxml);
11 };
12
13 $.extend(Canvas.prototype, {
14
15     loadWlxml: function(wlxml) {
16         var d = wlxml ? $($.trim(wlxml)) : null;
17         if(d) {
18             var wrapper = $('<div>');
19             wrapper.append(d);
20             
21             wrapper.find('*').replaceWith(function() {
22                 var currentTag = $(this);
23                 if(currentTag.attr('wlxml-tag'))
24                     return;
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);
31                 }
32                 toret.append(currentTag.contents());
33                 return toret;
34             });
35
36             wrapper.find(':not(iframe)').addBack().contents()
37                 .filter(function() {return this.nodeType === Node.TEXT_NODE})
38                 .each(function() {
39
40                     var el = $(this);
41                     
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';
46                         
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) ? ' ' : '')
52                                     + trimmed
53                                     + (endSpace && (spanParent || spanAfter) ? ' ' : '');
54
55                     } else {
56                         this.data = $.trim(this.data);
57                     }
58                 });
59             
60             this.d = wrapper.children(0);
61             this.d.unwrap();
62         } else {
63             this.d = null;
64         }
65     },
66
67     doc: function() {
68         if(this.d === null)
69             return null;
70         return documentElement.wrap(this.d.get(0), this); //{wlxmlTag: this.d.prop('tagName')};
71     },
72
73     wrapText: function(params) {
74         params = _.extend({textNodeIdx: 0}, params);
75         if(typeof params.textNodeIdx === 'number')
76             params.textNodeIdx = [params.textNodeIdx];
77         
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)
88         ;
89         
90         var wrapperElement = documentElement.DocumentNodeElement.create({tag: params._with.tag, klass: params._with.klass});
91         textNode1.after(wrapperElement);
92         textNode1.detach();
93         
94         if(prefixOutside.length > 0)
95             wrapperElement.before({text:prefixOutside});
96         if(sameNode) {
97             var core = textNode1.getText().substr(params.offsetStart, params.offsetEnd - params.offsetStart);
98             wrapperElement.append({text: core});
99         } else {
100             textNode2.detach();
101             if(prefixInside.length > 0)
102                 wrapperElement.append({text: prefixInside});
103             for(var i = idx1 + 1; i < idx2; i++) {
104                 wrapperElement.append(childrenInside[i]);
105             }
106             if(suffixInside.length > 0)
107                 wrapperElement.append({text: suffixInside});
108         }
109         if(suffixOutside.length > 0)
110             wrapperElement.after({text: suffixOutside});
111         return wrapperElement;
112     },
113     list: {}
114 });
115
116 $.extend(Canvas.prototype.list, {
117     create: function(params) {
118         if(!(params.element1.parent().sameNode(params.element2.parent())))
119             return false;
120             
121         var parent = params.element1.parent();
122         
123         if(parent.childIndex(params.element1) > parent.childIndex(params.element2)) {
124             var tmp = params.element1;
125             params.element1 = params.element2;
126             params.element2 = tmp;
127         }
128         
129         var elementsToWrap = [];
130         
131         var place = 'before';
132         var canvas = this;
133         parent.children().forEach(function(element) {
134             if(element.sameNode(params.element1))
135                 place = 'inside';
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;
141                 }
142                 element.setWlxmlClass('item');
143                 elementsToWrap.push(element);
144             }
145             if(element.sameNode(params.element2))
146                 return false;
147         });
148         
149         var listElement = documentElement.DocumentNodeElement.create({tag: 'div', klass: 'list-items' + (params.type === 'enum' ? '-enum' : '')});
150         
151         var toret;
152         if(parent.is('list')) {
153             listElement.wrap({tag: 'div', klass: 'item'});
154             toret = listElement.parent();
155         } else {
156             toret = listElement;
157         }  
158         
159         params.element1.before(toret);
160         
161         elementsToWrap.forEach(function(element) {
162             element.detach();
163             listElement.append(element);
164         });
165     }
166 });
167
168 return {
169     fromXML: function(xml) {
170         return new Canvas(xml);
171     }
172 };
173
174 });