2 'libs/jquery-1.9.1.min',
\r
4 ], function($, wlxmlNode) {
\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', new wlxmlNode.Node($(e.target)));
\r
39 canvas.dom.on('mouseout', '[wlxml-tag]', function(e) {
\r
40 e.stopPropagation();
\r
41 manager.sandbox.publish('nodeBlured', new wlxmlNode.Node($(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(new wlxmlNode.Node($(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(new wlxmlNode.Node(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(wlxmlNode, options) {
\r
83 options = options || {};
\r
84 var nodeElement = this.getNodeElement(wlxmlNode)
\r
86 this.dimNode(wlxmlNode);
\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 = wlxmlNode;
\r
96 this.sandbox.publish('nodeSelected', wlxmlNode);
\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 node = new wlxmlNode.Node($(selection.anchorNode).parent());
\r
113 var newNode = this.canvas.insertNode({place: 'wrapText', context: node, tag: wlxmlTag, klass: wlxmlClass, offsetStart: offsetStart, offsetEnd: offsetEnd});
\r
114 this.selectNode(new wlxmlNode.Node(newNode), {movecaret: 'end'});
\r
120 Manager.prototype.getNodeElement = function(wlxmlNode) {
\r
121 return this.canvas.dom.find('#'+wlxmlNode.id);
\r
124 Manager.prototype.highlightNode = function(wlxmlNode) {
\r
125 var nodeElement = this.getNodeElement(wlxmlNode);
\r
126 if(!this.gridToggled) {
\r
127 nodeElement.addClass('rng-common-hoveredNode');
\r
128 var label = nodeElement.attr('wlxml-tag');
\r
129 if(nodeElement.attr('wlxml-class'))
\r
130 label += ' / ' + nodeElement.attr('wlxml-class');
\r
131 var tag = $('<div>').addClass('rng-module-documentCanvas-hoveredNodeTag').text(label);
\r
132 nodeElement.append(tag);
\r
136 Manager.prototype.dimNode = function(wlxmlNode) {
\r
137 var nodeElement = this.getNodeElement(wlxmlNode);
\r
138 if(!this.gridToggled) {
\r
139 nodeElement.removeClass('rng-common-hoveredNode');
\r
140 nodeElement.find('.rng-module-documentCanvas-hoveredNodeTag').remove();
\r
144 Manager.prototype.selectFirstNode = function() {
\r
145 var firstNodeWithText = this.canvas.dom.find('[wlxml-tag]').filter(function() {
\r
146 return $(this).clone().children().remove().end().text().trim() !== '';
\r
149 if(firstNodeWithText.length)
\r
150 node = $(firstNodeWithText[0])
\r
152 node = this.canvas.dom.find('[wlxml-class|="p"]')
\r
154 this.selectNode(new wlxmlNode.Node(node), {movecaret: true});
\r
157 Manager.prototype.movecaretToNode = function(nodeElement, where) {
\r
158 var range = document.createRange();
\r
159 range.selectNodeContents(nodeElement[0]);
\r
161 var collapseArg = true;
\r
162 if(where === 'end')
\r
163 collapseArg = false;
\r
164 range.collapse(collapseArg);
\r
165 var selection = document.getSelection();
\r
166 selection.removeAllRanges()
\r
167 selection.addRange(range);
\r
170 Manager.prototype.toggleGrid = function(toggle) {
\r
171 this.canvas.dom.find('[wlxml-tag]').toggleClass('rng-common-hoveredNode', toggle);
\r
172 this.gridToggled = toggle;
\r
175 Manager.prototype.onEnterKey = function(e) {
\r
176 e.preventDefault();
\r
177 var pos = getCursorPosition();
\r
180 insertedNode = this.canvas.insertNode({place: 'after', context: {id: pos.parentNode.attr('id')}, tag: pos.parentNode.attr('wlxml-tag'), klass: pos.parentNode.attr('wlxml-class')});
\r
182 insertedNode = this.canvas.splitNode({node: {id: pos.parentNode.attr('id')}, textNodeIdx: pos.textNodeIndex, offset: pos.textNodeOffset});
\r
184 if(insertedNode.length)
\r
185 this.selectNode(new wlxmlNode.Node(insertedNode), {movecaret: true});
\r
186 this.sandbox.publish('contentChanged');
\r
189 Manager.prototype.onBackspaceKey = function(e) {
\r
190 var pos = getCursorPosition();
\r
191 var len = pos.textNode.text().length;
\r
193 // Prevent deleting node by browser after last character removed;
\r
194 e.preventDefault();
\r
195 pos.parentNode.text('');
\r
198 e.preventDefault();
\r
199 var toRemove = new wlxmlNode.Node(pos.textNode);
\r
200 var prevNode = this.canvas.getPreviousNode({node:toRemove});
\r
201 this.canvas.removeNode({node: toRemove}); // jesli nie ma tekstu, to anchor nie jest tex nodem
\r
202 this.selectNode(prevNode, {movecaret: 'end'});
\r
206 Manager.prototype.command = function(command, meta) {
\r
207 var pos = getCursorPosition();
\r
209 if(command === 'createList') {
\r
210 var node = new wlxmlNode.Node(pos.parentNode);
\r
211 if(window.getSelection().getRangeAt().collapsed && this.canvas.insideList({pointer: node})) {
\r
212 this.canvas.removeList({pointer: node});
\r
213 this.selectNode(node, {movecaret: 'end'});
\r
214 this.sandbox.publish('contentChanged');
\r
217 if(!this.canvas.insideList({pointer: node})) {
\r
218 this.canvas.createList({start: new wlxmlNode.Node(pos.parentNode), end: new wlxmlNode.Node(pos.focusNode)});
\r
219 this.selectNode(new wlxmlNode.Node(pos.parentNode), {movecaret: 'end'});
\r
220 this.sandbox.publish('contentChanged');
\r