2 'libs/jquery-1.9.1.min',
\r
3 'libs/underscore-min',
\r
4 'modules/documentCanvas/transformations',
\r
5 'modules/documentCanvas/canvasNode',
\r
6 'libs/text!./template.html'
\r
7 ], function($, _, transformations, canvasNode, template) {
\r
11 var Canvas = function(html) {
\r
12 this.dom = $(template);
\r
13 this.content = this.dom.find('#rng-module-documentCanvas-content');
\r
17 Canvas.prototype.setHTML = function(html) {
\r
19 this.content.html(html);
\r
23 Canvas.prototype.getContent = function() {
\r
24 return this.content.contents();
\r
27 Canvas.prototype.findNodes = function(desc) {
\r
29 if(typeof desc === 'string') {
\r
34 selector += '[wlxml-class=' + desc.klass + ']';
\r
36 selector += '[wlxml-tag=' + desc.tag + ']';
\r
39 this.content.find(selector).each(function() {
\r
40 toret.push(canvasNode.create($(this)));
\r
45 Canvas.prototype.getNodeById = function(id) {
\r
46 return canvasNode.create($(this.content.find('#' +id)));
\r
49 Canvas.prototype.nodeAppend = function(options) {
\r
50 var element; // = $(this.content.find('#' + options.context.id).get(0));
\r
51 if(options.to === 'root') {
\r
52 element = this.content;
\r
54 element = $(this.content.find('#' + options.to.getId()).get(0));
\r
56 element.append(options.node.dom);
\r
59 Canvas.prototype.nodeInsertAfter = function(options) {
\r
60 var element = $(this.content.find('#' + options.after.getId()).get(0));
\r
61 element.after(options.node.dom);
\r
64 Canvas.prototype.nodeWrap = function(options) {
\r
65 options = _.extend({textNodeIdx: 0}, options);
\r
66 if(typeof options.textNodeIdx === 'number')
\r
67 options.textNodeIdx = [options.textNodeIdx];
\r
69 var container = $(this.content.find('#' + options.inside.getId()).get(0)),
\r
70 containerContent = container.contents(),
\r
71 idx1 = Math.min.apply(Math, options.textNodeIdx),
\r
72 idx2 = Math.max.apply(Math, options.textNodeIdx),
\r
73 textNode1 = $(containerContent.get(idx1)),
\r
74 textNode2 = $(containerContent.get(idx2)),
\r
75 sameNode = textNode1.get(0) === textNode2.get(0),
\r
76 prefixOutside = textNode1.text().substr(0, options.offsetStart),
\r
77 prefixInside = textNode1.text().substr(options.offsetStart),
\r
78 suffixInside = textNode2.text().substr(0, options.offsetEnd),
\r
79 suffixOutside = textNode2.text().substr(options.offsetEnd)
\r
82 textNode1.after(options._with.dom);
\r
85 options._with.dom.before(prefixOutside);
\r
87 var core = textNode1.text().substr(options.offsetStart, options.offsetEnd - options.offsetStart);
\r
88 options._with.setContent(core);
\r
91 options._with.dom.append(prefixInside);
\r
92 for(var i = idx1 + 1; i < idx2; i++) {
\r
93 options._with.dom.append(containerContent[i]);
\r
95 options._with.dom.append(suffixInside);
\r
97 options._with.dom.after(suffixOutside);
\r
100 Canvas.prototype.nodeSplit = function(options) {
\r
101 options = _.extend({textNodeIdx: 0}, options);
\r
103 var nodeToSplit = $(this.content.find('#' + options.node.getId()).get(0));
\r
105 var nodeContents = nodeToSplit.contents();
\r
106 if(nodeContents.length === 0 ||
\r
107 nodeContents.length - 1 < options.textNodeIdx ||
\r
108 nodeContents.get(options.textNodeIdx).nodeType != 3)
\r
111 var textNode = $(nodeContents.get(options.textNodeIdx));
\r
113 var succeedingNodes = [];
\r
114 var passed = false;
\r
115 nodeContents.each(function() {
\r
118 succeedingNodes.push(node);
\r
119 if(node === textNode.get(0))
\r
123 var prefix = $.trim(textNode.text().substr(0, options.offset));
\r
124 var suffix = $.trim(textNode.text().substr(options.offset));
\r
126 textNode.before(prefix);
\r
129 var newNode = canvasNode.create({tag: nodeToSplit.attr('wlxml-tag'), klass: nodeToSplit.attr('wlxml-class')});
\r
130 newNode.dom.append(suffix);
\r
131 succeedingNodes.forEach(function(node) {
\r
132 newNode.dom.append(node)
\r
134 nodeToSplit.after(newNode.dom);
\r
138 Canvas.prototype.nodeRemove = function(options) {
\r
139 var toRemove = $(this.content.find('#' + options.node.getId()).get(0));
\r
143 Canvas.prototype.listCreate = function(options) {
\r
144 var element1 = $(this.content.find('#' + options.start.getId()).get(0));
\r
145 var element2 = $(this.content.find('#' + options.end.getId()).get(0));
\r
146 if(!element1.parent().get(0) === element2.parent().get(0))
\r
149 var parent = element1.parent();
\r
151 if(parent.contents().index(element1) > parent.contents().index(element2)) {
\r
152 var tmp = element1;
\r
153 element1 = element2;
\r
157 var nodesToWrap = [];
\r
159 var place = 'before';
\r
161 parent.contents().each(function() {
\r
163 if(node === element1.get(0))
\r
165 if(place === 'inside') {
\r
167 if(node.nodeType === 3) {
\r
168 $node = canvasNode.create({tag: 'div', content: $.trim(node.data)}).dom; //canvas._createNode('div').text(node.data);
\r
174 $node.attr('wlxml-class', 'item');
\r
175 nodesToWrap.push($node);
\r
177 if(node === element2.get(0))
\r
181 var list = canvasNode.create({tag: 'div', klass: 'list-items' + (options.type === 'enum' ? '-enum' : '')}).dom; //this._createNode('div', 'list-items');
\r
183 var parentNode = options.start.parent();
\r
186 if(parentNode && parentNode.isOfClass('list-items')) {
\r
187 list.wrap('<div wlxml-tag="div" wlxml-class="item" class="canvas-silent-item">');
\r
188 toret = list.parent();
\r
194 element1.before(toret);
\r
196 nodesToWrap.forEach(function(node) {
\r
202 Canvas.prototype.listRemove = function(options) {
\r
203 var pointerElement = $(this.content.find('#' + options.pointer.getId()));
\r
204 var listElement = options.pointer.getClass() === 'list-items' ? pointerElement :
\r
205 pointerElement.parents('[wlxml-class|="list-items"][wlxml-tag]');
\r
207 var nested = false;
\r
208 if(listElement.length > 1) {
\r
209 listElement = $(listElement[0]);
\r
214 listElement.unwrap();
\r
216 listElement.find('[wlxml-class=item]').each(function() {
\r
217 $(this).removeAttr('wlxml-class');
\r
220 listElement.children().unwrap();
\r
223 Canvas.prototype.getPrecedingNode = function(options) {
\r
224 var element = $(this.content.find('#' + options.node.getId()).get(0));
\r
225 var prev = element.prev()
\r
226 if(prev.length === 0)
\r
227 prev = element.parent();
\r
228 return canvasNode.create(prev);
\r
231 Canvas.prototype.nodeInsideList = function(options) {
\r
233 if(options.node.isOfClass('list-items') || options.node.isOfClass('item'))
\r
235 var pointerElement = $(this.content.find('#' + options.node.getId()));
\r
236 return pointerElement.parents('[wlxml-class=list-items], [wlxml-class=item]').length > 0;
\r
243 create: function(desc) { return new Canvas(desc); }
\r