505 lines
16 KiB
JavaScript
Raw Normal View History

/*
2012-10-04 20:48:01 -03:00
* Copyright [2012] [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({
2014-03-15 03:56:10 -03:00
Extends: mindplot.Keyboard,
2014-03-09 01:46:05 -03:00
Static:{
register:function (designer) {
this._instance = new mindplot.DesignerKeyboard(designer);
//this._instance.activate();
},
getInstance:function () {
return this._instance;
}
},
2012-07-02 22:54:59 -03:00
initialize:function (designer) {
2014-03-09 01:46:05 -03:00
$assert(designer, "designer can not be null");
this._registerEvents(designer);
},
2012-07-02 22:54:59 -03:00
_registerEvents:function (designer) {
2011-08-20 10:50:48 -03:00
// Try with the keyboard ..
2011-08-25 22:08:39 -03:00
var model = designer.getModel();
2014-03-09 01:46:05 -03:00
this.addShortcut(
'backspace', function (event) {
event.preventDefault();
event.stopPropagation();
2012-07-07 11:52:23 -03:00
designer.deleteSelectedEntities();
2014-03-09 01:46:05 -03:00
}
);
this.addShortcut(
'space', function() {
2012-07-02 22:54:59 -03:00
designer.shrinkSelectedBranch();
2014-03-09 01:46:05 -03:00
}
);
this.addShortcut(
'f2',function() {
2011-08-25 22:08:39 -03:00
var node = model.selectedTopic();
2011-08-20 11:08:18 -03:00
if (node) {
2011-08-25 00:17:13 -03:00
node.showTextEditor();
2011-08-20 10:50:48 -03:00
}
2014-03-09 01:46:05 -03:00
}
);
this.addShortcut(
2014-03-09 05:42:44 -03:00
'del', function(event) {
designer.deleteSelectedEntities();
event.preventDefault();
event.stopPropagation();
2014-03-09 01:46:05 -03:00
}
);
this.addShortcut(
'enter', function() {
2012-07-02 22:54:59 -03:00
designer.createSiblingForSelectedNode();
2014-03-09 01:46:05 -03:00
}
);
this.addShortcut(
'insert', function(event) {
designer.createChildForSelectedNode();
event.preventDefault();
event.stopPropagation();
2014-03-09 01:46:05 -03:00
}
);
this.addShortcut(
'tab', function(event) {
designer.createChildForSelectedNode();
event.preventDefault();
event.stopPropagation();
2014-03-09 01:46:05 -03:00
}
);
this.addShortcut(
'-', function() { // "-" is a insert on several Browsers. Don't ask why ...
2012-08-30 22:30:12 -03:00
designer.createChildForSelectedNode();
2014-03-09 01:46:05 -03:00
}
);
this.addShortcut(
'meta+enter', function(event) {
event.preventDefault();
event.stopPropagation();
2012-07-07 11:52:23 -03:00
designer.createChildForSelectedNode();
2014-03-09 01:46:05 -03:00
}
);
this.addShortcut(
'ctrl+z', function(event) {
event.preventDefault(event);
event.stopPropagation();
2012-07-07 11:52:23 -03:00
designer.undo();
2014-03-09 01:46:05 -03:00
}
);
this.addShortcut(
'meta+z', function(event) {
event.preventDefault();
event.stopPropagation();
2012-07-07 11:52:23 -03:00
designer.undo();
2014-03-09 01:46:05 -03:00
}
);
this.addShortcut(
'ctrl+c', function (event) {
2012-07-08 18:41:35 -03:00
event.preventDefault(event);
event.stopPropagation();
designer.copyToClipboard();
2014-03-09 01:46:05 -03:00
}
);
this.addShortcut(
'meta+c', function (event) {
2012-07-08 18:41:35 -03:00
event.preventDefault();
event.stopPropagation();
designer.copyToClipboard();
2014-03-09 01:46:05 -03:00
}
);
this.addShortcut(
'ctrl+v', function (event) {
2012-07-08 18:41:35 -03:00
event.preventDefault(event);
event.stopPropagation();
designer.pasteClipboard();
2014-03-09 01:46:05 -03:00
}
);
this.addShortcut(
'meta+v', function (event) {
2012-07-08 18:41:35 -03:00
event.preventDefault();
event.stopPropagation();
designer.pasteClipboard();
2014-03-09 01:46:05 -03:00
}
);
this.addShortcut(
'ctrl+z+shift', function (event) {
event.preventDefault();
event.stopPropagation();
designer.redo();
2014-03-09 01:46:05 -03:00
}
);
this.addShortcut(
'meta+z+shift', function (event) {
event.preventDefault();
event.stopPropagation();
2012-03-19 23:29:52 -03:00
designer.redo();
2014-03-09 01:46:05 -03:00
}
);
this.addShortcut(
'ctrl+y', function (event) {
event.preventDefault();
event.stopPropagation();
2012-07-07 11:52:23 -03:00
designer.redo();
2014-03-09 01:46:05 -03:00
}
);
this.addShortcut(
'meta+y', function (event) {
event.preventDefault();
event.stopPropagation();
2012-07-07 11:52:23 -03:00
designer.redo();
2014-03-09 01:46:05 -03:00
}
);
this.addShortcut(
'ctrl+a', function (event) {
event.preventDefault();
event.stopPropagation();
2012-07-07 11:52:23 -03:00
designer.selectAll();
2014-03-09 01:46:05 -03:00
}
);
this.addShortcut(
'ctrl+b', function (event) {
event.preventDefault();
event.stopPropagation();
2011-08-20 11:08:18 -03:00
designer.changeFontWeight();
2014-03-09 01:46:05 -03:00
}
);
this.addShortcut(
'meta+b', function (event) {
event.preventDefault();
event.stopPropagation();
2012-07-07 11:52:23 -03:00
designer.changeFontWeight();
2014-03-09 01:46:05 -03:00
}
);
this.addShortcut(
'ctrl+s', function (event) {
event.preventDefault();
event.stopPropagation();
document.id('save').fireEvent('click');
2014-03-09 01:46:05 -03:00
}
);
this.addShortcut(
'meta+s', function (event) {
event.preventDefault();
event.stopPropagation();
2012-07-07 11:52:23 -03:00
document.id('save').fireEvent('click');
2014-03-09 01:46:05 -03:00
}
);
this.addShortcut(
'ctrl+i', function (event) {
event.preventDefault();
event.stopPropagation();
2011-08-20 11:08:18 -03:00
designer.changeFontStyle();
2014-03-09 01:46:05 -03:00
}
);
this.addShortcut(
'meta+i', function (event) {
event.preventDefault();
event.stopPropagation();
2012-07-07 11:52:23 -03:00
designer.changeFontStyle();
2014-03-09 01:46:05 -03:00
}
);
this.addShortcut(
'meta+shift+a', function (event) {
2011-08-20 10:50:48 -03:00
event.preventDefault();
event.stopPropagation();
2011-08-20 10:50:48 -03:00
designer.deselectAll();
2014-03-09 01:46:05 -03:00
}
);
this.addShortcut(
'ctrl+shift+a', function (event) {
event.preventDefault();
event.stopPropagation();
2012-07-07 11:52:23 -03:00
designer.deselectAll();
2014-03-09 01:46:05 -03:00
}
);
this.addShortcut(
'meta+a', function (event) {
event.preventDefault();
event.stopPropagation();
2012-07-07 11:52:23 -03:00
designer.selectAll();
2014-03-09 01:46:05 -03:00
}
);
this.addShortcut(
'meta+=', function (event) {
2012-11-12 22:27:42 -03:00
event.preventDefault();
event.stopPropagation();
designer.zoomIn();
2014-03-09 01:46:05 -03:00
}
);
this.addShortcut(
'meta+-', function (event) {
2012-11-12 22:27:42 -03:00
event.preventDefault();
event.stopPropagation();
designer.zoomOut();
2014-03-09 01:46:05 -03:00
}
);
this.addShortcut(
'ctrl+=', function (event) {
2012-11-12 22:27:42 -03:00
event.preventDefault();
event.stopPropagation();
designer.zoomIn();
2014-03-09 01:46:05 -03:00
}
);
this.addShortcut(
'ctrl+-', function (event) {
2012-11-12 22:27:42 -03:00
event.preventDefault();
event.stopPropagation();
designer.zoomOut();
2014-03-09 01:46:05 -03:00
}
);
this.addShortcut(
'right', function (event) {
2011-08-25 22:08:39 -03:00
var node = model.selectedTopic();
2011-08-20 11:08:18 -03:00
if (node) {
2012-09-20 09:23:48 -03:00
if (node.isCentralTopic()) {
this._goToSideChild(designer, node, 'RIGHT');
}
else {
if (node.getPosition().x < 0) {
this._goToParent(designer, node);
}
else if (!node.areChildrenShrunken()) {
this._goToChild(designer, node);
}
}
} else {
2012-07-16 23:07:00 -03:00
var centralTopic = model.getCentralTopic();
this._goToNode(designer, centralTopic);
}
event.preventDefault();
event.stopPropagation();
2014-03-09 01:46:05 -03:00
}
);
this.addShortcut(
'left', function (event) {
2011-08-25 22:08:39 -03:00
var node = model.selectedTopic();
2011-08-20 11:08:18 -03:00
if (node) {
2012-09-20 09:23:48 -03:00
if (node.isCentralTopic()) {
this._goToSideChild(designer, node, 'LEFT');
}
else {
if (node.getPosition().x > 0) {
this._goToParent(designer, node);
}
else if (!node.areChildrenShrunken()) {
this._goToChild(designer, node);
}
}
} else {
2012-07-16 23:07:00 -03:00
var centralTopic = model.getCentralTopic();
this._goToNode(designer, centralTopic);
}
event.preventDefault();
event.stopPropagation();
2014-03-09 01:46:05 -03:00
}
);
this.addShortcut(
'up', function (event) {
2011-08-25 22:08:39 -03:00
var node = model.selectedTopic();
2011-08-20 11:08:18 -03:00
if (node) {
2012-09-20 09:23:48 -03:00
if (!node.isCentralTopic()) {
this._goToBrother(designer, node, 'UP');
}
} else {
2012-07-16 23:07:00 -03:00
var centralTopic = model.getCentralTopic();
this._goToNode(designer, centralTopic);
}
event.preventDefault();
event.stopPropagation();
2014-03-09 01:46:05 -03:00
}
);
this.addShortcut(
'down', function (event) {
2011-08-25 22:08:39 -03:00
var node = model.selectedTopic();
2011-08-20 11:08:18 -03:00
if (node) {
2012-09-20 09:23:48 -03:00
if (!node.isCentralTopic()) {
this._goToBrother(designer, node, 'DOWN');
}
} else {
2012-07-16 23:07:00 -03:00
var centralTopic = model.getCentralTopic();
this._goToNode(designer, centralTopic);
}
event.preventDefault();
event.stopPropagation();
2014-03-09 01:46:05 -03:00
}
);
2011-08-20 10:50:48 -03:00
var regex = /^(?:shift|control|ctrl|alt|meta)$/;
var modifiers = ['shift', 'control', 'alt', 'meta'];
2012-07-02 22:54:59 -03:00
var excludes = ['esc', 'capslock', 'tab', 'f1', 'f3', 'f4', 'f5', 'f6', 'f7', 'f8', 'f9', 'f10', 'f11', 'f12', 'backspace', 'down', 'up', 'left', 'right', 'control'];
if (!Browser.Platform.mac) {
// This is to avoid enter on edition mode in the node when alt+tab is pressed.
excludes.push("alt");
}
2011-08-20 10:50:48 -03:00
2014-03-09 01:46:05 -03:00
/*
document.id(document).addEvent('keydown', function (event) {
2011-08-20 10:50:48 -03:00
2012-04-02 04:15:08 -03:00
// Convert key to mootools keyboard event format...
2011-08-20 10:50:48 -03:00
var keys = [];
2012-07-02 22:54:59 -03:00
modifiers.each(function (mod) {
2011-08-20 10:50:48 -03:00
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 10:50:48 -03:00
// If it's not registered, let's
2012-07-03 00:25:30 -03:00
if (!isRegistered && !excludes.contains(key) && !excludes.contains(event.key) && !event.meta && !event.control) {
var nodes = designer.getModel().filterSelectedTopics();
2011-08-20 10:50:48 -03:00
if (nodes.length > 0) {
2011-09-04 21:32:29 -03:00
// If a modifier is press, the key selected must be ignored.
var pressKey = event.key;
if (modifiers.contains(event.key)) {
2011-09-04 21:32:29 -03:00
pressKey = "";
}
nodes[0].showTextEditor(pressKey);
2011-08-20 10:50:48 -03:00
event.stopPropagation();
}
}
});
2014-03-09 01:46:05 -03:00
*/
2011-08-20 10:50:48 -03:00
},
2012-07-02 22:54:59 -03:00
_goToBrother:function (designer, node, direction) {
var parent = node.getParent();
if (parent) {
var brothers = 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);
}
},
2012-07-02 22:54:59 -03:00
_goToSideChild:function (designer, node, side) {
2012-03-07 01:13:50 -03:00
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);
}
},
2012-07-02 22:54:59 -03:00
_goToParent:function (designer, node) {
2012-07-16 23:07:00 -03:00
var parent = node.getParent();
if (parent) {
this._goToNode(designer, parent);
}
},
2012-07-02 22:54:59 -03:00
_goToChild:function (designer, node) {
2012-03-07 01:13:50 -03:00
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 10:50:48 -03:00
this._goToNode(designer, target);
}
},
2012-07-02 22:54:59 -03:00
_goToNode:function (designer, node) {
// First deselect all the nodes ...
designer.deselectAll();
// Give focus to the selected node....
node.setOnFocus(true);
}
2011-09-04 21:32:29 -03:00
});