summaryrefslogtreecommitdiffstats
path: root/apps/files/js/keyboardshortcuts.js
blob: 69656b9364d74522a9cdf36a6f13ad76bda1683d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
/**
* Copyright (c) 2012 Erik Sargent <esthepiking at gmail dot com>
* This file is licensed under the Affero General Public License version 3 or
* later.
*/

/*****************************
* 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
*****************************/
var Files = Files || {};

(function(Files){
var keys = [];
var keyCodes = {
	shift: 16,
	n: 78,
	r: 82,
	cmdFirefox: 224,
	cmdOpera: 17,
	leftCmdWebKit: 91,
	rightCmdWebKit: 93,
	ctrl: 17,
	esc: 27,
	downArrow: 40,
	upArrow: 38,
	enter: 13,
	del: 46
};

function removeA(arr) {
    var what, a = arguments, L = a.length, ax;
    while (L > 1 && arr.length) {
        what = a[--L];
        while ((ax = arr.indexOf(what)) !== -1) {
            arr.splice(ax, 1);
        }
    }
    return arr;
}

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")){
	        select = index + 1;
	        $(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")){
	        select = index - 1;
	        $(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');
        }
	});
}

Files.bindKeyboardShortcuts = function (document, $) {
	$(document).keydown(function(event){//check for modifier keys
		var preventDefault = false;
		if($.inArray(event.keyCode, keys) === -1)
			keys.push(event.keyCode);
		
		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
			|| event.ctrlKey)
		){
			preventDefault = true;//new file/folder prevent browser from responding
		}
		/*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
			|| $.inArray(keyCodes.ctrl, keys) !== -1
			|| event.ctrlKey)
			&& $.inArray(keyCodes.shift, keys) !== -1
		){
			$("#fileList tr").each(function(index){//prevent default when renaming file/folder
				if($(this).hasClass("mouseOver")){
			        preventDefault = true;
		        }
			});
		}*/
		if(preventDefault){
			event.preventDefault(); //Prevent web browser from responding
			event.stopPropagation();
			return false;
		}
	});
	
	$(document).keyup(function(event){
       // do your event.keyCode checks in here
		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
			|| event.ctrlKey
		)){
			if($.inArray(keyCodes.shift, keys) !== -1
		){ //16=shift, New File
				newFile();
			}
			else{ //New Folder
				newFolder();
			}
		}
		
		else if($("#new").hasClass("active") && $.inArray(keyCodes.esc, keys) !== -1){ //close new window
			esc();
		}
		else if($.inArray(keyCodes.downArrow, keys) !== -1){ //select file
			down();
		}
		else if($.inArray(keyCodes.upArrow, keys) !== -1){ //select file
			up();
		}
		else if(!$("#new").hasClass("active") && $.inArray(keyCodes.enter, keys) !== -1){//open file
			enter();
		}
		else if(!$("#new").hasClass("active") && $.inArray(keyCodes.del, keys) !== -1) {//delete file
			del();
		}
		/*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
			|| $.inArray(keyCodes.ctrl, keys) !== -1
			|| event.ctrlKey)
			&& $.inArray(keyCodes.shift, keys) !== -1
		){//rename file
			rename();
		}*/
		
		removeA(keys, event.keyCode);
	});
};
})(Files);