2 'libs/jquery-1.9.1.min',
\r
4 ], function($, canvasNode) {
\r
8 var getCursorPosition = function() {
\r
9 var selection = window.getSelection();
\r
10 var anchorNode = $(selection.anchorNode);
\r
11 var parent = anchorNode.parent();
\r
13 textNode: anchorNode,
\r
14 textNodeOffset: selection.anchorOffset,
\r
15 textNodeIndex: parent.contents().index(anchorNode),
\r
17 focusNode: $(selection.focusNode).parent(),
\r
18 isAtEnd: selection.anchorOffset === anchorNode.text().length
\r
22 var Manager = function(canvas, sandbox) {
\r
23 this.canvas = canvas;
\r
24 this.sandbox = sandbox;
\r
25 this.shownAlready = false;
\r
26 this.gridToggled = false;
\r
27 this.scrollbarPosition = 0;
\r
28 this.currentNode = null;
\r
31 canvas.dom.find('#rng-module-documentCanvas-content').on('keyup', function() {
\r
32 manager.sandbox.publish('contentChanged');
\r
35 canvas.dom.on('mouseover', '[wlxml-tag]', function(e) {
\r
36 e.stopPropagation();
\r
37 manager.sandbox.publish('nodeHovered', canvasNode.create($(e.target)));
\r
39 canvas.dom.on('mouseout', '[wlxml-tag]', function(e) {
\r
40 e.stopPropagation();
\r
41 manager.sandbox.publish('nodeBlured', canvasNode.create($(e.target)));
\r
43 canvas.dom.on('click', '[wlxml-tag]', function(e) {
\r
44 e.stopPropagation();
\r
45 console.log('clicked node type: '+e.target.nodeType);
\r
46 manager.selectNode(canvasNode.create($(e.target)));
\r
49 canvas.dom.on('keyup', '#rng-module-documentCanvas-contentWrapper', function(e) {
\r
50 var anchor = $(window.getSelection().anchorNode);
\r
52 if(anchor[0].nodeType === Node.TEXT_NODE)
\r
53 anchor = anchor.parent();
\r
54 if(!anchor.is('[wlxml-tag]'))
\r
56 manager.selectNode(canvasNode.create(anchor));
\r
59 canvas.dom.on('keydown', '#rng-module-documentCanvas-contentWrapper', function(e) {
\r
60 if(e.which === 13) {
\r
61 manager.onEnterKey(e);
\r
64 manager.onBackspaceKey(e);
\r
68 canvas.dom.onShow = function() {
\r
69 if(!manager.shownAlready) {
\r
70 manager.shownAlready = true;
\r
71 manager.selectFirstNode();
\r
72 } else if(manager.currentNode) {
\r
73 manager.movecaretToNode(manager.getNodeElement(manager.currentNode));
\r
74 canvas.dom.find('#rng-module-documentCanvas-contentWrapper').scrollTop(manager.scrollbarPosition);
\r
77 canvas.dom.onHide = function() {
\r
78 manager.scrollbarPosition = canvas.dom.find('#rng-module-documentCanvas-contentWrapper').scrollTop();
\r
82 Manager.prototype.selectNode = function(cnode, options) {
\r
83 options = options || {};
\r
84 var nodeElement = this.getNodeElement(cnode)
\r
86 this.dimNode(cnode);
\r
88 this.canvas.dom.find('.rng-module-documentCanvas-currentNode').removeClass('rng-module-documentCanvas-currentNode');
\r
89 nodeElement.addClass('rng-module-documentCanvas-currentNode');
\r
91 if(options.movecaret) {
\r
92 this.movecaretToNode(nodeElement, options.movecaret);
\r
95 this.currentNode = cnode;
\r
96 this.sandbox.publish('nodeSelected', cnode);
\r
99 Manager.prototype.insertNewNode = function(wlxmlTag, wlxmlClass) {
\r
100 var selection = window.getSelection();
\r
102 if(selection.getRangeAt(0).collapsed) {
\r
105 var offsetStart = selection.anchorOffset;
\r
106 var offsetEnd = selection.focusOffset;
\r
107 if(offsetStart > offsetEnd) {
\r
108 var tmp = offsetStart;
\r
109 offsetStart = offsetEnd;
\r
112 var wrapper = canvasNode.create({tag: wlxmlTag, klass: wlxmlClass});
\r
113 var parent = $(selection.anchorNode).parent();
\r
114 this.canvas.nodeWrap({inside: canvasNode.create(parent),
\r
116 offsetStart: offsetStart,
\r
117 offsetEnd: offsetEnd,
\r
118 textNodeIdx: parent.contents().index($(selection.anchorNode))
\r
120 this.selectNode(wrapper, {movecaret: 'end'});
\r
126 Manager.prototype.getNodeElement = function(cnode) {
\r
127 return this.canvas.dom.find('#'+cnode.getId());
\r
130 Manager.prototype.highlightNode = function(cnode) {
\r
131 var nodeElement = this.getNodeElement(cnode);
\r
132 if(!this.gridToggled) {
\r
133 nodeElement.addClass('rng-common-hoveredNode');
\r
134 var label = nodeElement.attr('wlxml-tag');
\r
135 if(nodeElement.attr('wlxml-class'))
\r
136 label += ' / ' + nodeElement.attr('wlxml-class');
\r
137 var tag = $('<div>').addClass('rng-module-documentCanvas-hoveredNodeTag').text(label);
\r
138 nodeElement.append(tag);
\r
142 Manager.prototype.dimNode = function(cnode) {
\r
143 var nodeElement = this.getNodeElement(cnode);
\r
144 if(!this.gridToggled) {
\r
145 nodeElement.removeClass('rng-common-hoveredNode');
\r
146 nodeElement.find('.rng-module-documentCanvas-hoveredNodeTag').remove();
\r
150 Manager.prototype.selectFirstNode = function() {
\r
151 var firstNodeWithText = this.canvas.dom.find('[wlxml-tag]').filter(function() {
\r
152 return $(this).clone().children().remove().end().text().trim() !== '';
\r
155 if(firstNodeWithText.length)
\r
156 node = $(firstNodeWithText[0])
\r
158 node = this.canvas.dom.find('[wlxml-class|="p"]')
\r
160 this.selectNode(canvasNode.create(node), {movecaret: true});
\r
163 Manager.prototype.movecaretToNode = function(nodeElement, where) {
\r
164 if(!nodeElement.length)
\r
166 var range = document.createRange();
\r
167 range.selectNodeContents(nodeElement[0]);
\r
169 var collapseArg = true;
\r
170 if(where === 'end')
\r
171 collapseArg = false;
\r
172 range.collapse(collapseArg);
\r
173 var selection = document.getSelection();
\r
174 selection.removeAllRanges()
\r
175 selection.addRange(range);
\r
178 Manager.prototype.toggleGrid = function(toggle) {
\r
179 this.canvas.dom.find('[wlxml-tag]').toggleClass('rng-common-hoveredNode', toggle);
\r
180 this.gridToggled = toggle;
\r
183 Manager.prototype.onEnterKey = function(e) {
\r
184 e.preventDefault();
\r
185 var pos = getCursorPosition();
\r
186 var contextNode = this.canvas.getNodeById(pos.parentNode.attr('id'));
\r
190 newNode = canvasNode.create({tag: pos.parentNode.attr('wlxml-tag'), klass: pos.parentNode.attr('wlxml-class')});
\r
191 this.canvas.nodeInsertAfter({node: newNode, after: this.canvas.getNodeById(pos.parentNode.attr('id'))});
\r
193 newNode = this.canvas.nodeSplit({node: contextNode, textNodeIdx: pos.textNodeIndex, offset: pos.textNodeOffset});
\r
196 this.selectNode(newNode, {movecaret: true});
\r
197 this.sandbox.publish('contentChanged');
\r
200 Manager.prototype.onBackspaceKey = function(e) {
\r
201 var pos = getCursorPosition();
\r
202 var len = pos.textNode.text().length;
\r
204 // Prevent deleting node by browser after last character removed;
\r
205 e.preventDefault();
\r
206 pos.parentNode.text('');
\r
209 e.preventDefault();
\r
210 var toRemove = canvasNode.create(pos.textNode);
\r
211 var prevNode = this.canvas.getPrecedingNode({node:toRemove});
\r
212 this.canvas.nodeRemove({node: toRemove}); // jesli nie ma tekstu, to anchor nie jest tex nodem
\r
213 this.selectNode(prevNode, {movecaret: 'end'});
\r
217 Manager.prototype.command = function(command, meta) {
\r
218 var pos = getCursorPosition();
\r
220 if(command === 'createList') {
\r
221 var node = canvasNode.create(pos.parentNode);
\r
222 if(window.getSelection().getRangeAt(0).collapsed && this.canvas.nodeInsideList({node: node})) {
\r
223 this.canvas.listRemove({pointer: node});
\r
224 this.selectNode(node, {movecaret: 'end'});
\r
225 this.sandbox.publish('contentChanged');
\r
228 //if(!this.canvas.nodeInsideList({node: node})) {
\r
229 this.canvas.listCreate({start: node, end: canvasNode.create(pos.focusNode), type: meta});
\r
230 this.selectNode(node, {movecaret: 'end'});
\r
231 this.sandbox.publish('contentChanged');
\r