2 'libs/jquery-1.9.1.min',
4 ], function($, canvasNode) {
8 var getCursorPosition = function() {
9 var selection = window.getSelection();
10 var anchorNode = $(selection.anchorNode);
11 var parent = anchorNode.parent();
14 textNodeOffset: selection.anchorOffset,
15 textNodeIndex: parent.contents().index(anchorNode),
17 focusNode: $(selection.focusNode).parent(),
18 isAtEnd: selection.anchorOffset === anchorNode.text().length
22 var Manager = function(canvas, sandbox) {
24 this.sandbox = sandbox;
25 this.shownAlready = false;
26 this.gridToggled = false;
27 this.scrollbarPosition = 0;
28 this.currentNode = null;
31 canvas.doc().dom().find('#rng-module-documentCanvas-content').on('keyup', function() {
32 manager.sandbox.publish('contentChanged');
35 canvas.doc().dom().on('mouseover', '[wlxml-tag]', function(e) {
37 manager.sandbox.publish('nodeHovered', canvasNode.create($(e.target)));
39 canvas.doc().dom().on('mouseout', '[wlxml-tag]', function(e) {
41 manager.sandbox.publish('nodeBlured', canvasNode.create($(e.target)));
43 canvas.doc().dom().on('click', '[wlxml-tag]', function(e) {
45 console.log('clicked node type: '+e.target.nodeType);
46 manager.selectNode(canvasNode.create($(e.target)));
49 canvas.doc().dom().on('keyup', '#rng-module-documentCanvas-contentWrapper', function(e) {
50 var anchor = $(window.getSelection().anchorNode);
52 if(anchor[0].nodeType === Node.TEXT_NODE)
53 anchor = anchor.parent();
54 if(!anchor.is('[wlxml-tag]'))
56 manager.selectNode(canvasNode.create(anchor));
59 canvas.doc().dom().on('keydown', '#rng-module-documentCanvas-contentWrapper', function(e) {
61 manager.onEnterKey(e);
64 manager.onBackspaceKey(e);
68 canvas.doc().dom().onShow = function() {
69 if(!manager.shownAlready) {
70 manager.shownAlready = true;
71 manager.selectFirstNode();
72 } else if(manager.currentNode) {
73 manager.movecaretToNode(manager.getNodeElement(manager.currentNode));
74 canvas.doc().dom().find('#rng-module-documentCanvas-contentWrapper').scrollTop(manager.scrollbarPosition);
77 canvas.doc().dom().onHide = function() {
78 manager.scrollbarPosition = canvas.doc().dom().find('#rng-module-documentCanvas-contentWrapper').scrollTop();
82 Manager.prototype.selectNode = function(cnode, options) {
83 options = options || {};
84 var nodeElement = this.getNodeElement(cnode);
88 this.canvas.doc().dom().find('.rng-module-documentCanvas-currentNode').removeClass('rng-module-documentCanvas-currentNode');
89 nodeElement.addClass('rng-module-documentCanvas-currentNode');
91 if(options.movecaret) {
92 this.movecaretToNode(nodeElement, options.movecaret);
95 this.currentNode = cnode;
96 this.sandbox.publish('nodeSelected', cnode);
99 Manager.prototype.getNodeElement = function(cnode) {
100 return this.canvas.doc().dom().find('#'+cnode.getId());
103 Manager.prototype.highlightNode = function(cnode) {
104 var nodeElement = this.getNodeElement(cnode);
105 if(!this.gridToggled) {
106 nodeElement.addClass('rng-common-hoveredNode');
107 var label = nodeElement.attr('wlxml-tag');
108 if(nodeElement.attr('wlxml-class'))
109 label += ' / ' + nodeElement.attr('wlxml-class');
110 var tag = $('<div>').addClass('rng-module-documentCanvas-hoveredNodeTag').text(label);
111 nodeElement.append(tag);
115 Manager.prototype.dimNode = function(cnode) {
116 var nodeElement = this.getNodeElement(cnode);
117 if(!this.gridToggled) {
118 nodeElement.removeClass('rng-common-hoveredNode');
119 nodeElement.find('.rng-module-documentCanvas-hoveredNodeTag').remove();
123 Manager.prototype.selectFirstNode = function() {
124 var firstNodeWithText = this.canvas.doc().dom().find('[wlxml-tag]').filter(function() {
125 return $(this).clone().children().remove().end().text().trim() !== '';
128 if(firstNodeWithText.length)
129 node = $(firstNodeWithText[0]);
131 node = this.canvas.doc().dom().find('[wlxml-class|="p"]');
133 this.selectNode(canvasNode.create(node), {movecaret: true});
136 Manager.prototype.movecaretToNode = function(nodeElement, where) {
137 if(!nodeElement.length)
139 var range = document.createRange();
140 range.selectNodeContents(nodeElement[0]);
142 var collapseArg = true;
145 range.collapse(collapseArg);
146 var selection = document.getSelection();
147 selection.removeAllRanges();
148 selection.addRange(range);
151 Manager.prototype.onEnterKey = function(e) {
153 var pos = getCursorPosition();
154 var contextNode = this.canvas.getNodeById(pos.parentNode.attr('id'));
158 newNode = canvasNode.create({tag: pos.parentNode.attr('wlxml-tag'), klass: pos.parentNode.attr('wlxml-class')});
159 this.canvas.nodeInsertAfter({node: newNode, after: this.canvas.getNodeById(pos.parentNode.attr('id'))});
161 newNode = this.canvas.nodeSplit({node: contextNode, textNodeIdx: pos.textNodeIndex, offset: pos.textNodeOffset});
164 this.selectNode(newNode, {movecaret: true});
165 this.sandbox.publish('contentChanged');
168 Manager.prototype.onBackspaceKey = function(e) {
169 var pos = getCursorPosition();
170 var len = pos.textNode.text().length;
172 // Prevent deleting node by browser after last character removed;
174 pos.parentNode.text('');
178 var toRemove = canvasNode.create(pos.textNode);
179 var prevNode = this.canvas.getPrecedingNode({node:toRemove});
180 this.canvas.nodeRemove({node: toRemove}); // jesli nie ma tekstu, to anchor nie jest tex nodem
181 this.selectNode(prevNode, {movecaret: 'end'});
185 Manager.prototype.toggleList = function(toggle) {
186 var selection = window.getSelection(),
187 node1 = $(selection.anchorNode).parent()[0],
188 node2 = $(selection.focusNode).parent()[0],
189 element1 = this.canvas.getDocumentElement(node1),
190 element2 = this.canvas.getDocumentElement(node2);
194 Manager.prototype.command = function(command, params) {
195 var selection = window.getSelection(),
196 element1 = this.canvas.getDocumentElement(selection.anchorNode),
197 element2 = this.canvas.getDocumentElement(selection.focusNode);
199 if(command === 'unwrap-node') {
200 // this.canvas.nodeUnwrap({node: canvasNode.create(pos.parentNode)});
201 // this.sandbox.publish('contentChanged');
202 element1 = element1.parent();
203 element2 = element2.parent();
204 if(this.canvas.list.areItemsOfTheSameList({element1: element1, element2: element2})) {
205 this.canvas.list.extractItems({element1: element1, element2: element2});
207 } else if(command === 'wrap-node') {
208 element1 = element1.parent();
209 element2 = element2.parent();
210 if(this.canvas.list.areItemsOfTheSameList({element1: element1, element2: element2})) {
211 this.canvas.list.create({element1: element1, element2: element2});
213 } else if(command === 'toggle-list') {
214 element1 = element1.parent();
215 element2 = element2.parent();
217 this.canvas.list.create({element1: element1, element2: element2});
219 if(this.canvas.list.areItemsOfTheSameList({element1: element1, element2: element2})) {
220 this.canvas.list.extractItems({element1: element1, element2: element2, merge: false});
223 } else if(command == 'toggle-grid') {
224 this.canvas.doc().dom().find('[wlxml-tag]').toggleClass('rng-common-hoveredNode', params.toggle);
225 this.gridToggled = params.toggle;
226 } else if(command == 'newNodeRequested') {
227 if(!selection.isCollapsed && element1.parent().sameNode(element2.parent())) {
228 var parent = element1.parent(),
229 offsetStart = selection.anchorOffset,
230 offsetEnd = selection.focusOffset;
232 if(element1.sameNode(element2)) {
233 element1.wrapWithNodeElement({tag: params.wlxmlTag, klass: params.wlxmlClass, start: offsetStart, end: offsetEnd});
236 if(parent.childIndex(element1) > parent.childIndex(element2)) {
237 var tmp = offsetStart;
238 offsetStart = offsetEnd;
241 this.canvas.wrapText({
243 _with: {tag: params.wlxmlTag, klass: params.wlxmlClass},
244 offsetStart: offsetStart,
245 offsetEnd: offsetEnd,
246 textNodeIdx: [parent.childIndex(element1), parent.childIndex(element2)]