wisemapping-open-source/mindplot/src/main/javascript/DesignerKeyboard.js

337 lines
11 KiB
JavaScript
Raw Normal View History

/*
* Copyright [2011] [wisemapping]
*
* Licensed under WiseMapping Public License, Version 1.0 (the "License").
* It is basically the Apache License, Version 2.0 (the "License") plus the
* "powered by wisemapping" text requirement on every single page;
* you may not use this file except in compliance with the License.
* You may obtain a copy of the license at
*
* http://www.wisemapping.org/license
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
mindplot.DesignerKeyboard = new Class({
Extends:Keyboard,
initialize : function(designer) {
$assert(designer, "designer can not be null");
this.parent({defaultEventType: 'keydown'});
this._registerEvents(designer);
},
2011-08-20 15:50:48 +02:00
_registerEvents : function(designer) {
2011-08-20 15:50:48 +02:00
// Try with the keyboard ..
2011-08-26 03:08:39 +02:00
var model = designer.getModel();
2011-08-20 15:50:48 +02:00
var keyboardEvents = {
'backspace':function(event) {
event.preventDefault();
event.stopPropagation();
}.bind(this),
'space' : function() {
2011-08-26 03:08:39 +02:00
var node = model.selectedTopic();
2011-08-20 16:08:18 +02:00
if (node) {
var model = topic.getModel();
var isShrink = !model.areChildrenShrinked();
topic.setChildrenShrinked(isShrink);
}
}.bind(this),
'f2' : function() {
2011-08-26 03:08:39 +02:00
var node = model.selectedTopic();
2011-08-20 16:08:18 +02:00
if (node) {
2011-08-25 05:17:13 +02:00
node.showTextEditor();
2011-08-20 15:50:48 +02:00
}
}.bind(this),
'delete' : function() {
designer.deleteCurrentNode();
}.bind(this),
'enter' :
function() {
designer.createSiblingForSelectedNode();
}.bind(this),
'insert' : function() {
designer.createChildForSelectedNode();
}.bind(this),
'ctrl+z' : function() {
designer.undo();
}.bind(this),
'meta+z' : function() {
designer.undo();
}.bind(this),
'ctrl+z+shift' :function() {
designer.redo();
}.bind(this),
'meta+z+shift' : function() {
designer.redo();
}.bind(this),
'ctrl+a' : function(event) {
designer.selectAll();
event.preventDefault();
2011-08-20 16:08:18 +02:00
},
'ctrl+b' : function() {
designer.changeFontWeight();
},
'meta+b' : function() {
designer.changeFontWeight();
},
2011-08-22 03:53:37 +02:00
'ctrl+s' : function(event) {
event.preventDefault();
designer.save(null, true);
},
'meta+s' : function(event) {
event.preventDefault();
designer.save(null, true);
},
2011-08-20 16:08:18 +02:00
'ctrl+i' : function() {
designer.changeFontStyle();
},
'meta+i' : function() {
designer.changeFontStyle();
},
2011-08-20 15:50:48 +02:00
'meta+shift+a' : function(event) {
designer.deselectAll();
event.preventDefault();
},
'ctrl+shift+a' : function(event) {
designer.deselectAll();
event.preventDefault();
},
'meta+a' : function(event) {
designer.selectAll();
event.preventDefault();
},
'right' : function() {
2011-08-26 03:08:39 +02:00
var node = model.selectedTopic();
2011-08-20 16:08:18 +02:00
if (node) {
if (node.getTopicType() == mindplot.model.NodeModel.CENTRAL_TOPIC_TYPE) {
this._goToSideChild(designer, node, 'RIGHT');
}
else {
if (node.getPosition().x < 0) {
this._goToParent(designer, node);
}
else if (!node.areChildrenShrinked()) {
this._goToChild(designer, node);
}
}
} else {
var centralTopic = designer.getCentralTopic();
this._goToNode(designer, centralTopic);
}
}.bind(this),
'left' : function() {
2011-08-26 03:08:39 +02:00
var node = model.selectedTopic();
2011-08-20 16:08:18 +02:00
if (node) {
if (node.getTopicType() == mindplot.model.NodeModel.CENTRAL_TOPIC_TYPE) {
this._goToSideChild(designer, node, 'LEFT');
}
else {
if (node.getPosition().x > 0) {
this._goToParent(designer, node);
}
else if (!node.areChildrenShrinked()) {
this._goToChild(designer, node);
}
}
} else {
var centralTopic = designer.getCentralTopic();
this._goToNode(designer, centralTopic);
}
}.bind(this),
'up' : function() {
2011-08-26 03:08:39 +02:00
var node = model.selectedTopic();
2011-08-20 16:08:18 +02:00
if (node) {
if (node.getTopicType() != mindplot.model.NodeModel.CENTRAL_TOPIC_TYPE) {
this._goToBrother(designer, node, 'UP');
}
} else {
var centralTopic = designer.getCentralTopic();
this._goToNode(designer, centralTopic);
}
}.bind(this),
'down' : function() {
2011-08-26 03:08:39 +02:00
var node = model.selectedTopic();
2011-08-20 16:08:18 +02:00
if (node) {
if (node.getTopicType() != mindplot.model.NodeModel.CENTRAL_TOPIC_TYPE) {
this._goToBrother(designer, node, 'DOWN');
}
} else {
var centralTopic = designer.getCentralTopic();
this._goToNode(designer, centralTopic);
}
}.bind(this)
2011-08-20 15:50:48 +02:00
};
this.addEvents(keyboardEvents);
var regex = /^(?:shift|control|ctrl|alt|meta)$/;
var modifiers = ['shift', 'control', 'alt', 'meta'];
2011-08-25 05:17:13 +02:00
var excludes = ['esc','capslock','tab','f1','f3','f4','f5','f6','f7','f8','f9','f10','f11','f12'];
2011-08-20 15:50:48 +02:00
$(document).addEvent('keydown', function(event) {
// Convert key to mootool keyboard event format...
var keys = [];
modifiers.each(function(mod) {
if (event[mod]) keys.push(mod);
});
if (!regex.test(event.key))
keys.push(event.key);
var key = keys.join('+');
// Is the pressed key one of the already registered in the keyboard ?
var isRegistered = false;
for (var eKey in keyboardEvents) {
if (eKey == key) {
isRegistered = true;
break;
}
}
2011-08-20 15:50:48 +02:00
// If it's not registered, let's
2011-09-05 02:32:29 +02:00
if (!isRegistered && !excludes.contains(key) && 'meta+[' != key) {
var nodes = designer.getModel().filterSelectedTopics();
2011-08-20 15:50:48 +02:00
if (nodes.length > 0) {
2011-09-05 02:32:29 +02:00
// If a modifier is press, the key selected must be ignored.
var pressKey = event.key;
if (modifiers.contains(key)) {
pressKey = "";
}
nodes[0].showTextEditor(pressKey);
2011-08-20 15:50:48 +02:00
event.stopPropagation();
}
}
});
2011-08-20 15:50:48 +02:00
},
_goToBrother : function(designer, node, direction) {
var brothers = node._parent._getChildren();
var target = node;
var y = node.getPosition().y;
var x = node.getPosition().x;
var dist = null;
for (var i = 0; i < brothers.length; i++) {
var sameSide = (x * brothers[i].getPosition().x) >= 0;
if (brothers[i] != node && sameSide) {
var brother = brothers[i];
var brotherY = brother.getPosition().y;
if (direction == "DOWN" && brotherY > y) {
var distancia = y - brotherY;
if (distancia < 0) {
distancia = distancia * (-1);
}
if (dist == null || dist > distancia) {
dist = distancia;
target = brothers[i];
}
}
else if (direction == "UP" && brotherY < y) {
var distance = y - brotherY;
if (distance < 0) {
distance = distance * (-1);
}
if (dist == null || dist > distance) {
dist = distance;
target = brothers[i];
}
}
}
}
this._goToNode(designer, target);
},
_goToSideChild : function(designer, node, side) {
var children = node._getChildren();
if (children.length > 0) {
var target = children[0];
var top = null;
for (var i = 0; i < children.length; i++) {
var child = children[i];
var childY = child.getPosition().y;
if (side == 'LEFT' && child.getPosition().x < 0) {
if (top == null || childY < top) {
target = child;
top = childY;
}
}
if (side == 'RIGHT' && child.getPosition().x > 0) {
if (top == null || childY < top) {
target = child;
top = childY;
}
}
}
this._goToNode(designer, target);
}
},
_goToParent : function(designer, node) {
var parent = node._parent;
2011-08-20 15:50:48 +02:00
this._goToNode(designer, parent);
},
_goToChild : function(designer, node) {
var children = node._getChildren();
if (children.length > 0) {
var target = children[0];
var top = target.getPosition().y;
for (var i = 0; i < children.length; i++) {
var child = children[i];
if (child.getPosition().y < top) {
top = child.getPosition().y;
target = child;
}
}
2011-08-20 15:50:48 +02:00
this._goToNode(designer, target);
}
},
_goToNode : function(designer, node) {
// First deselect all the nodes ...
designer.deselectAll();
// Give focus to the selected node....
node.setOnFocus(true);
}
2011-09-05 02:32:29 +02:00
});
2011-08-20 15:50:48 +02:00
mindplot.DesignerKeyboard.register = function(designer) {
this._instance = new mindplot.DesignerKeyboard(designer);
this._instance.activate();
2011-08-20 16:08:18 +02:00
};