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
68 var container = $(this.content.find('#' + options.inside.getId()).get(0));
\r
70 var containerContent = container.contents();
\r
71 var idx1 = Math.min.apply(Math, options.textNodeIdx);
\r
72 var idx2 = Math.max.apply(Math, options.textNodeIdx);
\r
73 var textNode1 = $(containerContent.get(idx1));
\r
74 var textNode2 = $(containerContent.get(idx2));
\r
75 var sameNode = textNode1.get(0) === textNode2.get(0);
\r
76 textNode1.after(options._with.dom);
\r
81 var prefixOutside = textNode1.text().substr(0, options.offsetStart);
\r
82 var prefixInside = textNode1.text().substr(options.offsetStart)
\r
83 var suffixInside = textNode2.text().substr(0, options.offsetEnd)
\r
84 var suffixOutside = textNode2.text().substr(options.offsetEnd);
\r
87 core = textNode1.text().substr(options.offsetStart, options.offsetEnd - options.offsetStart);
\r
89 options._with.dom.before(prefixOutside);
\r
91 options._with.setContent(core);
\r
93 options._with.dom.append(prefixInside);
\r
94 for(var i = idx1 + 1; i < idx2; i++) {
\r
95 options._with.dom.append(containerContent[i]);
\r
97 options._with.dom.append(suffixInside);
\r
99 options._with.dom.after(suffixOutside);
\r
102 Canvas.prototype.nodeSplit = function(options) {
\r
103 options = _.extend({textNodeIdx: 0}, options);
\r
105 var nodeToSplit = $(this.content.find('#' + options.node.getId()).get(0));
\r
107 var nodeContents = nodeToSplit.contents();
\r
108 if(nodeContents.length === 0 ||
\r
109 nodeContents.length - 1 < options.textNodeIdx ||
\r
110 nodeContents.get(options.textNodeIdx).nodeType != 3)
\r
113 var textNode = $(nodeContents.get(options.textNodeIdx));
\r
115 var succeedingNodes = [];
\r
116 var passed = false;
\r
117 nodeContents.each(function() {
\r
120 succeedingNodes.push(node);
\r
121 if(node === textNode.get(0))
\r
125 var prefix = $.trim(textNode.text().substr(0, options.offset));
\r
126 var suffix = $.trim(textNode.text().substr(options.offset));
\r
128 textNode.before(prefix);
\r
131 var newNode = canvasNode.create({tag: nodeToSplit.attr('wlxml-tag'), klass: nodeToSplit.attr('wlxml-class')});
\r
132 newNode.dom.append(suffix);
\r
133 succeedingNodes.forEach(function(node) {
\r
134 newNode.dom.append(node)
\r
136 nodeToSplit.after(newNode.dom);
\r
140 Canvas.prototype.nodeRemove = function(options) {
\r
141 var toRemove = $(this.content.find('#' + options.node.getId()).get(0));
\r
145 Canvas.prototype.listCreate = function(options) {
\r
146 var element1 = $(this.content.find('#' + options.start.getId()).get(0));
\r
147 var element2 = $(this.content.find('#' + options.end.getId()).get(0));
\r
148 if(!element1.parent().get(0) === element2.parent().get(0))
\r
151 var parent = element1.parent();
\r
153 if(parent.contents().index(element1) > parent.contents().index(element2)) {
\r
154 var tmp = element1;
\r
155 element1 = element2;
\r
159 var nodesToWrap = [];
\r
161 var place = 'before';
\r
163 parent.contents().each(function() {
\r
165 if(node === element1.get(0))
\r
167 if(place === 'inside') {
\r
169 if(node.nodeType === 3) {
\r
170 $node = canvasNode.create({tag: 'div', content: $.trim(node.data)}).dom; //canvas._createNode('div').text(node.data);
\r
176 $node.attr('wlxml-class', 'item');
\r
177 nodesToWrap.push($node);
\r
179 if(node === element2.get(0))
\r
183 var list = canvasNode.create({tag: 'div', klass: 'list-items' + (options.type === 'enum' ? '-enum' : '')}).dom; //this._createNode('div', 'list-items');
\r
185 var parentNode = options.start.parent();
\r
188 if(parentNode && parentNode.isOfClass('list-items')) {
\r
189 list.wrap('<div wlxml-tag="div" wlxml-class="item" class="canvas-silent-item">');
\r
190 toret = list.parent();
\r
196 element1.before(toret);
\r
198 nodesToWrap.forEach(function(node) {
\r
204 Canvas.prototype.listRemove = function(options) {
\r
205 var pointerElement = $(this.content.find('#' + options.pointer.getId()));
\r
206 var listElement = options.pointer.getClass() === 'list-items' ? pointerElement :
\r
207 pointerElement.parents('[wlxml-class|="list-items"][wlxml-tag]');
\r
209 var nested = false;
\r
210 if(listElement.length > 1) {
\r
211 listElement = $(listElement[0]);
\r
216 listElement.unwrap();
\r
218 listElement.find('[wlxml-class=item]').each(function() {
\r
219 $(this).removeAttr('wlxml-class');
\r
222 listElement.children().unwrap();
\r
225 Canvas.prototype.getPrecedingNode = function(options) {
\r
226 var element = $(this.content.find('#' + options.node.getId()).get(0));
\r
227 var prev = element.prev()
\r
228 if(prev.length === 0)
\r
229 prev = element.parent();
\r
230 return canvasNode.create(prev);
\r
233 Canvas.prototype.nodeInsideList = function(options) {
\r
235 if(options.node.isOfClass('list-items') || options.node.isOfClass('item'))
\r
237 var pointerElement = $(this.content.find('#' + options.node.getId()));
\r
238 return pointerElement.parents('[wlxml-class=list-items], [wlxml-class=item]').length > 0;
\r
245 create: function(desc) { return new Canvas(desc); }
\r