2 'libs/jquery-1.9.1.min',
4 'modules/documentCanvas/transformations',
5 'modules/documentCanvas/canvasNode',
6 'libs/text!./template.html'
7 ], function($, _, transformations, canvasNode, template) {
11 var Canvas = function(html) {
12 this.dom = $(template);
13 this.content = this.dom.find('#rng-module-documentCanvas-content');
17 Canvas.prototype.setHTML = function(html) {
19 this.content.html(html);
23 Canvas.prototype.getContent = function() {
24 return this.content.contents();
27 Canvas.prototype.findNodes = function(desc) {
29 if(typeof desc === 'string') {
34 selector += '[wlxml-class=' + desc.klass + ']';
36 selector += '[wlxml-tag=' + desc.tag + ']';
39 this.content.find(selector).each(function() {
40 toret.push(canvasNode.create($(this)));
45 Canvas.prototype.getNodeById = function(id) {
46 return canvasNode.create($(this.content.find('#' +id)));
49 Canvas.prototype.nodeAppend = function(options) {
50 var element; // = $(this.content.find('#' + options.context.id).get(0));
51 if(options.to === 'root') {
52 element = this.content;
54 element = $(this.content.find('#' + options.to.getId()).get(0));
56 element.append(options.node.dom);
59 Canvas.prototype.nodeInsertAfter = function(options) {
60 var element = $(this.content.find('#' + options.after.getId()).get(0));
61 element.after(options.node.dom);
64 Canvas.prototype.nodeWrap = function(options) {
65 options = _.extend({textNodeIdx: 0}, options);
66 if(typeof options.textNodeIdx === 'number')
67 options.textNodeIdx = [options.textNodeIdx];
69 var container = $(this.content.find('#' + options.inside.getId()).get(0)),
70 containerContent = container.contents(),
71 idx1 = Math.min.apply(Math, options.textNodeIdx),
72 idx2 = Math.max.apply(Math, options.textNodeIdx),
73 textNode1 = $(containerContent.get(idx1)),
74 textNode2 = $(containerContent.get(idx2)),
75 sameNode = textNode1.get(0) === textNode2.get(0),
76 prefixOutside = textNode1.text().substr(0, options.offsetStart),
77 prefixInside = textNode1.text().substr(options.offsetStart),
78 suffixInside = textNode2.text().substr(0, options.offsetEnd),
79 suffixOutside = textNode2.text().substr(options.offsetEnd)
82 textNode1.after(options._with.dom);
85 options._with.dom.before(prefixOutside);
87 var core = textNode1.text().substr(options.offsetStart, options.offsetEnd - options.offsetStart);
88 options._with.setContent(core);
91 options._with.dom.append(prefixInside);
92 for(var i = idx1 + 1; i < idx2; i++) {
93 options._with.dom.append(containerContent[i]);
95 options._with.dom.append(suffixInside);
97 options._with.dom.after(suffixOutside);
100 Canvas.prototype.nodeUnwrap = function(options) {
102 var removeWithJoin = function(node) {
103 var contents = node.parent().contents(),
104 idx = contents.index(node),
105 prev = idx > 0 ? contents[idx-1] : null,
106 next = idx + 1 < contents.length ? contents[idx+1] : null;
108 if(prev && prev.nodeType === 3 && next && next.nodeType === 3) {
109 prev.data = prev.data + next.data;
115 var toUnwrap = $(this.content.find('#' + options.node.getId()).get(0));
118 var parent = toUnwrap.parent();
119 var parentContents = parent.contents();
121 if(toUnwrap.contents().length !== 1 || toUnwrap.contents()[0].nodeType !== 3)
124 var idx = parentContents.index(toUnwrap);
129 if(idx > 0 && parentContents[idx-1].nodeType === 3) {
130 combineWith = parentContents[idx-1];
132 } else if(idx + 1 < parentContents.length && parentContents[idx+1].nodeType === 3) {
133 combineWith = parentContents[idx+1];
139 (action === 'prepend' ? toUnwrap.text() : '') +
141 (action === 'append' ? toUnwrap.text() : '')
143 combineWith.data = text;
144 removeWithJoin(toUnwrap);
146 if(parentContents.length === 1 || idx === 0) {
147 parent.append(toUnwrap.text());
149 toUnwrap.prev().after(toUnwrap.text());
155 Canvas.prototype.nodeSplit = function(options) {
156 options = _.extend({textNodeIdx: 0}, options);
158 var nodeToSplit = $(this.content.find('#' + options.node.getId()).get(0));
160 var nodeContents = nodeToSplit.contents();
161 if(nodeContents.length === 0 ||
162 nodeContents.length - 1 < options.textNodeIdx ||
163 nodeContents.get(options.textNodeIdx).nodeType != 3)
166 var textNode = $(nodeContents.get(options.textNodeIdx));
168 var succeedingNodes = [];
170 nodeContents.each(function() {
173 succeedingNodes.push(node);
174 if(node === textNode.get(0))
178 var prefix = $.trim(textNode.text().substr(0, options.offset));
179 var suffix = $.trim(textNode.text().substr(options.offset));
181 textNode.before(prefix);
184 var newNode = canvasNode.create({tag: nodeToSplit.attr('wlxml-tag'), klass: nodeToSplit.attr('wlxml-class')});
185 newNode.dom.append(suffix);
186 succeedingNodes.forEach(function(node) {
187 newNode.dom.append(node);
189 nodeToSplit.after(newNode.dom);
193 Canvas.prototype.nodeRemove = function(options) {
194 var toRemove = $(this.content.find('#' + options.node.getId()).get(0));
198 Canvas.prototype.listCreate = function(options) {
199 var element1 = $(this.content.find('#' + options.start.getId()).get(0));
200 var element2 = $(this.content.find('#' + options.end.getId()).get(0));
201 if(element1.parent().get(0) !== element2.parent().get(0))
204 var parent = element1.parent();
206 if(parent.contents().index(element1) > parent.contents().index(element2)) {
212 var nodesToWrap = [];
214 var place = 'before';
216 parent.contents().each(function() {
218 if(node === element1.get(0))
220 if(place === 'inside') {
222 if(node.nodeType === 3) {
223 $node = canvasNode.create({tag: 'div', content: $.trim(node.data)}).dom; //canvas._createNode('div').text(node.data);
229 $node.attr('wlxml-class', 'item');
230 nodesToWrap.push($node);
232 if(node === element2.get(0))
236 var list = canvasNode.create({tag: 'div', klass: 'list-items' + (options.type === 'enum' ? '-enum' : '')}).dom; //this._createNode('div', 'list-items');
238 var parentNode = options.start.parent();
241 if(parentNode && parentNode.isOfClass('list-items')) {
242 list.wrap('<div wlxml-tag="div" wlxml-class="item" class="canvas-silent-item">');
243 toret = list.parent();
249 element1.before(toret);
251 nodesToWrap.forEach(function(node) {
257 Canvas.prototype.listRemove = function(options) {
258 var pointerElement = $(this.content.find('#' + options.pointer.getId()));
259 var listElement = options.pointer.isOfClass('list-items') ? pointerElement :
260 pointerElement.parents('[wlxml-class|="list-items"][wlxml-tag]');
264 if(listElement.length > 1) {
265 listElement = $(listElement[0]);
270 // We are only moving one level up
271 listElement.unwrap();
273 // We are removing the whole list
274 nestedLists = listElement.find('[wlxml-class=item] > [wlxml-class|=list-items]');
275 nestedLists.unwrap();
276 listElement.find('[wlxml-class=item]').each(function() {
277 $(this).removeAttr('wlxml-class');
281 listElement.children().unwrap();
285 nestedLists.each(function() {
286 c.listRemove({pointer: canvasNode.create($(this))});
291 Canvas.prototype.getPrecedingNode = function(options) {
292 var element = $(this.content.find('#' + options.node.getId()).get(0));
293 var prev = element.prev();
294 if(prev.length === 0)
295 prev = element.parent();
296 return canvasNode.create(prev);
299 Canvas.prototype.nodeInsideList = function(options) {
301 if(options.node.isOfClass('list-items') || options.node.isOfClass('item'))
303 var pointerElement = $(this.content.find('#' + options.node.getId()));
304 return pointerElement.parents('[wlxml-class=list-items], [wlxml-class=item]').length > 0;
311 create: function(desc) { return new Canvas(desc); }