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.insertNewNode = function(wlxmlTag, wlxmlClass) {
100 var selection = window.getSelection(),
101 $anchorNode = $(selection.anchorNode),
102 $focusNode = $(selection.focusNode);
105 if(!selection.isCollapsed && $anchorNode.parent()[0] === $focusNode.parent()[0]) {
107 parent = $anchorNode.parent(),
108 parentContents = parent.contents(),
109 offsetStart = selection.anchorOffset,
110 offsetEnd = selection.focusOffset;
112 if(selection.anchorNode === selection.focusNode) {
113 if(offsetStart > offsetEnd) {
114 var tmp = offsetStart;
115 offsetStart = offsetEnd;
118 textNodeIdx = parentContents.index($anchorNode);
120 if(parentContents.index($anchorNode) > parentContents.index($focusNode)) {
121 offsetStart = selection.focusOffset;
122 offsetEnd = selection.anchorOffset;
124 textNodeIdx = [parentContents.index($anchorNode), parentContents.index($focusNode)];
127 var wrapper = canvasNode.create({tag: wlxmlTag, klass: wlxmlClass});
128 this.canvas.nodeWrap({inside: canvasNode.create(parent),
130 offsetStart: offsetStart,
131 offsetEnd: offsetEnd,
132 textNodeIdx: textNodeIdx
134 this.selectNode(wrapper, {movecaret: 'end'});
140 Manager.prototype.getNodeElement = function(cnode) {
141 return this.canvas.doc().dom().find('#'+cnode.getId());
144 Manager.prototype.highlightNode = function(cnode) {
145 var nodeElement = this.getNodeElement(cnode);
146 if(!this.gridToggled) {
147 nodeElement.addClass('rng-common-hoveredNode');
148 var label = nodeElement.attr('wlxml-tag');
149 if(nodeElement.attr('wlxml-class'))
150 label += ' / ' + nodeElement.attr('wlxml-class');
151 var tag = $('<div>').addClass('rng-module-documentCanvas-hoveredNodeTag').text(label);
152 nodeElement.append(tag);
156 Manager.prototype.dimNode = function(cnode) {
157 var nodeElement = this.getNodeElement(cnode);
158 if(!this.gridToggled) {
159 nodeElement.removeClass('rng-common-hoveredNode');
160 nodeElement.find('.rng-module-documentCanvas-hoveredNodeTag').remove();
164 Manager.prototype.selectFirstNode = function() {
165 var firstNodeWithText = this.canvas.doc().dom().find('[wlxml-tag]').filter(function() {
166 return $(this).clone().children().remove().end().text().trim() !== '';
169 if(firstNodeWithText.length)
170 node = $(firstNodeWithText[0]);
172 node = this.canvas.doc().dom().find('[wlxml-class|="p"]');
174 this.selectNode(canvasNode.create(node), {movecaret: true});
177 Manager.prototype.movecaretToNode = function(nodeElement, where) {
178 if(!nodeElement.length)
180 var range = document.createRange();
181 range.selectNodeContents(nodeElement[0]);
183 var collapseArg = true;
186 range.collapse(collapseArg);
187 var selection = document.getSelection();
188 selection.removeAllRanges();
189 selection.addRange(range);
192 Manager.prototype.toggleGrid = function(toggle) {
193 this.canvas.doc().dom().find('[wlxml-tag]').toggleClass('rng-common-hoveredNode', toggle);
194 this.gridToggled = toggle;
197 Manager.prototype.onEnterKey = function(e) {
199 var pos = getCursorPosition();
200 var contextNode = this.canvas.getNodeById(pos.parentNode.attr('id'));
204 newNode = canvasNode.create({tag: pos.parentNode.attr('wlxml-tag'), klass: pos.parentNode.attr('wlxml-class')});
205 this.canvas.nodeInsertAfter({node: newNode, after: this.canvas.getNodeById(pos.parentNode.attr('id'))});
207 newNode = this.canvas.nodeSplit({node: contextNode, textNodeIdx: pos.textNodeIndex, offset: pos.textNodeOffset});
210 this.selectNode(newNode, {movecaret: true});
211 this.sandbox.publish('contentChanged');
214 Manager.prototype.onBackspaceKey = function(e) {
215 var pos = getCursorPosition();
216 var len = pos.textNode.text().length;
218 // Prevent deleting node by browser after last character removed;
220 pos.parentNode.text('');
224 var toRemove = canvasNode.create(pos.textNode);
225 var prevNode = this.canvas.getPrecedingNode({node:toRemove});
226 this.canvas.nodeRemove({node: toRemove}); // jesli nie ma tekstu, to anchor nie jest tex nodem
227 this.selectNode(prevNode, {movecaret: 'end'});
231 Manager.prototype.command = function(command, meta) {
232 var selection = window.getSelection(),
233 node1 = $(selection.anchorNode).parent()[0],
234 node2 = $(selection.focusNode).parent()[0],
235 element1 = this.canvas.getDocumentElement(node1),
236 element2 = this.canvas.getDocumentElement(node2);
237 if(command === 'createList') {
238 this.canvas.list.create({element1: element1, element2: element2});
239 } else if(command === 'unwrap-node') {
240 // this.canvas.nodeUnwrap({node: canvasNode.create(pos.parentNode)});
241 // this.sandbox.publish('contentChanged');
242 if(this.canvas.list.areItemsOfTheSameList({element1: element1, element2: element2})) {
243 this.canvas.list.extractItems({element1: element1, element2: element2});