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.nodeSplit = function(options) {
101 options = _.extend({textNodeIdx: 0}, options);
103 var nodeToSplit = $(this.content.find('#' + options.node.getId()).get(0));
105 var nodeContents = nodeToSplit.contents();
106 if(nodeContents.length === 0 ||
107 nodeContents.length - 1 < options.textNodeIdx ||
108 nodeContents.get(options.textNodeIdx).nodeType != 3)
111 var textNode = $(nodeContents.get(options.textNodeIdx));
113 var succeedingNodes = [];
115 nodeContents.each(function() {
118 succeedingNodes.push(node);
119 if(node === textNode.get(0))
123 var prefix = $.trim(textNode.text().substr(0, options.offset));
124 var suffix = $.trim(textNode.text().substr(options.offset));
126 textNode.before(prefix);
129 var newNode = canvasNode.create({tag: nodeToSplit.attr('wlxml-tag'), klass: nodeToSplit.attr('wlxml-class')});
130 newNode.dom.append(suffix);
131 succeedingNodes.forEach(function(node) {
132 newNode.dom.append(node);
134 nodeToSplit.after(newNode.dom);
138 Canvas.prototype.nodeRemove = function(options) {
139 var toRemove = $(this.content.find('#' + options.node.getId()).get(0));
143 Canvas.prototype.listCreate = function(options) {
144 var element1 = $(this.content.find('#' + options.start.getId()).get(0));
145 var element2 = $(this.content.find('#' + options.end.getId()).get(0));
146 if(element1.parent().get(0) !== element2.parent().get(0))
149 var parent = element1.parent();
151 if(parent.contents().index(element1) > parent.contents().index(element2)) {
157 var nodesToWrap = [];
159 var place = 'before';
161 parent.contents().each(function() {
163 if(node === element1.get(0))
165 if(place === 'inside') {
167 if(node.nodeType === 3) {
168 $node = canvasNode.create({tag: 'div', content: $.trim(node.data)}).dom; //canvas._createNode('div').text(node.data);
174 $node.attr('wlxml-class', 'item');
175 nodesToWrap.push($node);
177 if(node === element2.get(0))
181 var list = canvasNode.create({tag: 'div', klass: 'list-items' + (options.type === 'enum' ? '-enum' : '')}).dom; //this._createNode('div', 'list-items');
183 var parentNode = options.start.parent();
186 if(parentNode && parentNode.isOfClass('list-items')) {
187 list.wrap('<div wlxml-tag="div" wlxml-class="item" class="canvas-silent-item">');
188 toret = list.parent();
194 element1.before(toret);
196 nodesToWrap.forEach(function(node) {
202 Canvas.prototype.listRemove = function(options) {
203 var pointerElement = $(this.content.find('#' + options.pointer.getId()));
204 var listElement = options.pointer.getClass() === 'list-items' ? pointerElement :
205 pointerElement.parents('[wlxml-class|="list-items"][wlxml-tag]');
208 if(listElement.length > 1) {
209 listElement = $(listElement[0]);
214 listElement.unwrap();
216 listElement.find('[wlxml-class=item]').each(function() {
217 $(this).removeAttr('wlxml-class');
220 listElement.children().unwrap();
223 Canvas.prototype.getPrecedingNode = function(options) {
224 var element = $(this.content.find('#' + options.node.getId()).get(0));
225 var prev = element.prev();
226 if(prev.length === 0)
227 prev = element.parent();
228 return canvasNode.create(prev);
231 Canvas.prototype.nodeInsideList = function(options) {
233 if(options.node.isOfClass('list-items') || options.node.isOfClass('item'))
235 var pointerElement = $(this.content.find('#' + options.node.getId()));
236 return pointerElement.parents('[wlxml-class=list-items], [wlxml-class=item]').length > 0;
243 create: function(desc) { return new Canvas(desc); }