server/apps/files/js/keyboardshortcuts.js

220 lines
5.6 KiB
JavaScript
Raw Normal View History

2012-11-29 23:52:41 +00:00
/**
* Copyright (c) 2012 Erik Sargent <esthepiking at gmail dot com>
* This file is licensed under the Affero General Public License version 3 or
* later.
*/
2012-11-29 17:18:34 +00:00
/*****************************
* Keyboard shortcuts for Files app
* ctrl/cmd+n: new folder
* ctrl/cmd+shift+n: new file
* esc (while new file context menu is open): close menu
* up/down: select file/folder
* enter: open file/folder
* delete/backspace: delete file/folder
* ctrl/cmd+shift+r: rename file/folder
*****************************/
2012-11-28 16:50:54 +00:00
var Files = Files || {};
2012-11-29 23:52:41 +00:00
(function(Files){
2012-11-29 17:18:34 +00:00
var keys = [];
var keyCodes = {
shift: 16,
n: 78,
r: 82,
cmdFirefox: 224,
cmdOpera: 17,
leftCmdWebKit: 91,
rightCmdWebKit: 93,
2012-11-30 15:43:24 +00:00
ctrl: 17,
2012-11-29 17:18:34 +00:00
esc: 27,
downArrow: 40,
upArrow: 38,
enter: 13,
del: 46
};
2012-11-28 17:36:54 +00:00
function removeA(arr) {
var what, a = arguments, L = a.length, ax;
while (L > 1 && arr.length) {
what = a[--L];
2012-11-29 00:32:05 +00:00
while ((ax = arr.indexOf(what)) !== -1) {
2012-11-28 17:36:54 +00:00
arr.splice(ax, 1);
}
}
return arr;
}
2012-11-28 16:50:54 +00:00
2012-11-29 17:18:34 +00:00
function newFile(){
$("#new").addClass("active");
$(".popup.popupTop").toggle(true);
$('#new li[data-type="file"]').trigger('click');
removeA(keys, keyCodes.n);
}
function newFolder(){
$("#new").addClass("active");
$(".popup.popupTop").toggle(true);
$('#new li[data-type="folder"]').trigger('click');
removeA(keys, keyCodes.n);
}
function esc(){
$("#controls").trigger('click');
}
function down(){
var select = -1;
$("#fileList tr").each(function(index){
if($(this).hasClass("mouseOver")){
2012-11-29 23:52:41 +00:00
select = index + 1;
2012-11-29 17:18:34 +00:00
$(this).removeClass("mouseOver");
}
});
if(select === -1){
$("#fileList tr:first").addClass("mouseOver");
}
else{
$("#fileList tr").each(function(index){
if(index === select){
$(this).addClass("mouseOver");
}
});
}
}
function up(){
var select = -1;
$("#fileList tr").each(function(index){
if($(this).hasClass("mouseOver")){
2012-11-29 23:52:41 +00:00
select = index - 1;
2012-11-29 17:18:34 +00:00
$(this).removeClass("mouseOver");
}
});
if(select === -1){
$("#fileList tr:last").addClass("mouseOver");
}
else{
$("#fileList tr").each(function(index){
if(index === select){
$(this).addClass("mouseOver");
}
});
}
}
function enter(){
$("#fileList tr").each(function(index){
if($(this).hasClass("mouseOver")){
$(this).removeClass("mouseOver");
$(this).find("span.nametext").trigger('click');
}
});
}
function del(){
$("#fileList tr").each(function(index){
if($(this).hasClass("mouseOver")){
$(this).removeClass("mouseOver");
$(this).find("a.action.delete").trigger('click');
}
});
}
function rename(){
$("#fileList tr").each(function(index){
if($(this).hasClass("mouseOver")){
$(this).removeClass("mouseOver");
$(this).find("a[data-action='Rename']").trigger('click');
}
});
}
2012-11-28 16:50:54 +00:00
2012-11-29 17:18:34 +00:00
Files.bindKeyboardShortcuts = function (document, $) {
2012-11-28 16:50:54 +00:00
$(document).keydown(function(event){//check for modifier keys
2012-11-29 04:00:11 +00:00
var preventDefault = false;
2012-11-29 00:32:05 +00:00
if($.inArray(event.keyCode, keys) === -1)
2012-11-28 16:50:54 +00:00
keys.push(event.keyCode);
2012-11-29 23:52:41 +00:00
if(
$.inArray(keyCodes.n, keys) !== -1
&& ($.inArray(keyCodes.cmdFirefox, keys) !== -1
|| $.inArray(keyCodes.cmdOpera, keys) !== -1
|| $.inArray(keyCodes.leftCmdWebKit, keys) !== -1
|| $.inArray(keyCodes.rightCmdWebKit, keys) !== -1
|| $.inArray(keyCodes.ctrl, keys) !== -1)
){
preventDefault = true;//new file/folder prevent browser from responding
2012-11-29 04:00:11 +00:00
}
2012-11-29 23:52:41 +00:00
if(
!$("#new").hasClass("active")
&& $.inArray(keyCodes.r, keys) !== -1
&& ($.inArray(keyCodes.cmdFirefox, keys) !== -1
|| $.inArray(keyCodes.cmdOpera, keys) !== -1
|| $.inArray(keyCodes.leftCmdWebKit, keys) !== -1
|| $.inArray(keyCodes.rightCmdWebKit, keys) !== -1
2012-11-30 17:10:00 +00:00
|| $.inArray(keyCodes.ctrl, keys) !== -1
|| event.ctrlKey)
2012-11-29 23:52:41 +00:00
&& $.inArray(keyCodes.shift, keys) !== -1
){
$("#fileList tr").each(function(index){//prevent default when renaming file/folder
2012-11-29 04:00:11 +00:00
if($(this).hasClass("mouseOver")){
preventDefault = true;
}
});
}
if(preventDefault){
2012-11-28 16:50:54 +00:00
event.preventDefault(); //Prevent web browser from responding
2012-11-29 00:32:05 +00:00
event.stopPropagation();
return false;
2012-11-28 16:50:54 +00:00
}
});
$(document).keyup(function(event){
// do your event.keyCode checks in here
2012-11-29 23:52:41 +00:00
if(
$.inArray(keyCodes.n, keys) !== -1
&& ($.inArray(keyCodes.cmdFirefox, keys) !== -1
|| $.inArray(keyCodes.cmdOpera, keys) !== -1
|| $.inArray(keyCodes.leftCmdWebKit, keys) !== -1
|| $.inArray(keyCodes.rightCmdWebKit, keys) !== -1
2012-11-30 17:10:00 +00:00
|| $.inArray(keyCodes.ctrl, keys) !== -1
|| event.ctrlKey
)){
2012-11-29 23:52:41 +00:00
if($.inArray(keyCodes.shift, keys) !== -1
){ //16=shift, New File
2012-11-29 17:18:34 +00:00
newFile();
2012-11-28 16:50:54 +00:00
}
else{ //New Folder
2012-11-29 17:18:34 +00:00
newFolder();
2012-11-28 16:50:54 +00:00
}
}
2012-11-29 00:32:05 +00:00
2012-11-29 17:21:20 +00:00
else if($("#new").hasClass("active") && $.inArray(keyCodes.esc, keys) !== -1){ //close new window
2012-11-29 17:18:34 +00:00
esc();
2012-11-28 16:50:54 +00:00
}
2012-11-29 17:21:20 +00:00
else if($.inArray(keyCodes.downArrow, keys) !== -1){ //select file
2012-11-29 17:18:34 +00:00
down();
2012-11-29 00:32:05 +00:00
}
2012-11-29 17:21:20 +00:00
else if($.inArray(keyCodes.upArrow, keys) !== -1){ //select file
2012-11-29 17:18:34 +00:00
up();
2012-11-29 00:32:05 +00:00
}
2012-11-29 17:21:20 +00:00
else if(!$("#new").hasClass("active") && $.inArray(keyCodes.enter, keys) !== -1){//open file
2012-11-29 17:18:34 +00:00
enter();
2012-11-29 04:00:11 +00:00
}
2012-11-29 23:52:41 +00:00
else if(!$("#new").hasClass("active") && $.inArray(keyCodes.del, keys) !== -1) {//delete file
2012-11-29 17:18:34 +00:00
del();
2012-11-29 04:00:11 +00:00
}
2012-11-29 23:52:41 +00:00
else if(
!$("#new").hasClass("active")
&& $.inArray(keyCodes.r, keys) !== -1
&& ($.inArray(keyCodes.cmdFirefox, keys) !== -1
|| $.inArray(keyCodes.cmdOpera, keys) !== -1
|| $.inArray(keyCodes.leftCmdWebKit, keys) !== -1
|| $.inArray(keyCodes.rightCmdWebKit, keys) !== -1
2012-11-30 17:10:00 +00:00
|| $.inArray(keyCodes.ctrl, keys) !== -1
|| event.ctrlKey)
2012-11-29 23:52:41 +00:00
&& $.inArray(keyCodes.shift, keys) !== -1
){//rename file
2012-11-29 17:18:34 +00:00
rename();
2012-11-29 02:58:42 +00:00
}
2012-11-28 17:36:54 +00:00
removeA(keys, event.keyCode);
2012-11-28 16:50:54 +00:00
});
2012-11-29 23:52:41 +00:00
};
})(Files);