Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Avatar for KuCalc : devops.
Download
50659 views
1
//----------------------------------------------------------------------------
2
// Copyright (C) 2011 The IPython Development Team
3
//
4
// Distributed under the terms of the BSD License. The full license is in
5
// the file COPYING, distributed as part of this software.
6
//----------------------------------------------------------------------------
7
8
//============================================================================
9
// Keyboard management
10
//============================================================================
11
12
IPython.namespace('IPython.keyboard');
13
14
IPython.keyboard = (function (IPython) {
15
"use strict";
16
17
// Setup global keycodes and inverse keycodes.
18
19
// See http://unixpapa.com/js/key.html for a complete description. The short of
20
// it is that there are different keycode sets. Firefox uses the "Mozilla keycodes"
21
// and Webkit/IE use the "IE keycodes". These keycode sets are mostly the same
22
// but have minor differences.
23
24
// These apply to Firefox, (Webkit and IE)
25
var _keycodes = {
26
'a': 65, 'b': 66, 'c': 67, 'd': 68, 'e': 69, 'f': 70, 'g': 71, 'h': 72, 'i': 73,
27
'j': 74, 'k': 75, 'l': 76, 'm': 77, 'n': 78, 'o': 79, 'p': 80, 'q': 81, 'r': 82,
28
's': 83, 't': 84, 'u': 85, 'v': 86, 'w': 87, 'x': 88, 'y': 89, 'z': 90,
29
'1 !': 49, '2 @': 50, '3 #': 51, '4 $': 52, '5 %': 53, '6 ^': 54,
30
'7 &': 55, '8 *': 56, '9 (': 57, '0 )': 48,
31
'[ {': 219, '] }': 221, '` ~': 192, ', <': 188, '. >': 190, '/ ?': 191,
32
'\\ |': 220, '\' "': 222,
33
'numpad0': 96, 'numpad1': 97, 'numpad2': 98, 'numpad3': 99, 'numpad4': 100,
34
'numpad5': 101, 'numpad6': 102, 'numpad7': 103, 'numpad8': 104, 'numpad9': 105,
35
'multiply': 106, 'add': 107, 'subtract': 109, 'decimal': 110, 'divide': 111,
36
'f1': 112, 'f2': 113, 'f3': 114, 'f4': 115, 'f5': 116, 'f6': 117, 'f7': 118,
37
'f8': 119, 'f9': 120, 'f11': 122, 'f12': 123, 'f13': 124, 'f14': 125, 'f15': 126,
38
'backspace': 8, 'tab': 9, 'enter': 13, 'shift': 16, 'ctrl': 17, 'alt': 18,
39
'meta': 91, 'capslock': 20, 'esc': 27, 'space': 32, 'pageup': 33, 'pagedown': 34,
40
'end': 35, 'home': 36, 'left': 37, 'up': 38, 'right': 39, 'down': 40,
41
'insert': 45, 'delete': 46, 'numlock': 144,
42
};
43
44
// These apply to Firefox and Opera
45
var _mozilla_keycodes = {
46
'; :': 59, '= +': 61, '- _': 173, 'meta': 224
47
};
48
49
// This apply to Webkit and IE
50
var _ie_keycodes = {
51
'; :': 186, '= +': 187, '- _': 189
52
};
53
54
var browser = IPython.utils.browser[0];
55
var platform = IPython.utils.platform;
56
57
if (browser === 'Firefox' || browser === 'Opera' || browser === 'Netscape') {
58
$.extend(_keycodes, _mozilla_keycodes);
59
} else if (browser === 'Safari' || browser === 'Chrome' || browser === 'MSIE') {
60
$.extend(_keycodes, _ie_keycodes);
61
}
62
63
var keycodes = {};
64
var inv_keycodes = {};
65
for (var name in _keycodes) {
66
var names = name.split(' ');
67
if (names.length === 1) {
68
var n = names[0];
69
keycodes[n] = _keycodes[n];
70
inv_keycodes[_keycodes[n]] = n;
71
} else {
72
var primary = names[0];
73
var secondary = names[1];
74
keycodes[primary] = _keycodes[name];
75
keycodes[secondary] = _keycodes[name];
76
inv_keycodes[_keycodes[name]] = primary;
77
}
78
}
79
80
var normalize_key = function (key) {
81
return inv_keycodes[keycodes[key]];
82
};
83
84
var normalize_shortcut = function (shortcut) {
85
// Put a shortcut into normalized form:
86
// 1. Make lowercase
87
// 2. Replace cmd by meta
88
// 3. Sort '-' separated modifiers into the order alt-ctrl-meta-shift
89
// 4. Normalize keys
90
shortcut = shortcut.toLowerCase().replace('cmd', 'meta');
91
shortcut = shortcut.replace(/-$/, '_'); // catch shortcuts using '-' key
92
var values = shortcut.split("-");
93
if (values.length === 1) {
94
return normalize_key(values[0]);
95
} else {
96
var modifiers = values.slice(0,-1);
97
var key = normalize_key(values[values.length-1]);
98
modifiers.sort();
99
return modifiers.join('-') + '-' + key;
100
}
101
};
102
103
var shortcut_to_event = function (shortcut, type) {
104
// Convert a shortcut (shift-r) to a jQuery Event object
105
type = type || 'keydown';
106
shortcut = normalize_shortcut(shortcut);
107
shortcut = shortcut.replace(/-$/, '_'); // catch shortcuts using '-' key
108
var values = shortcut.split("-");
109
var modifiers = values.slice(0,-1);
110
var key = values[values.length-1];
111
var opts = {which: keycodes[key]};
112
if (modifiers.indexOf('alt') !== -1) {opts.altKey = true;}
113
if (modifiers.indexOf('ctrl') !== -1) {opts.ctrlKey = true;}
114
if (modifiers.indexOf('meta') !== -1) {opts.metaKey = true;}
115
if (modifiers.indexOf('shift') !== -1) {opts.shiftKey = true;}
116
return $.Event(type, opts);
117
};
118
119
var event_to_shortcut = function (event) {
120
// Convert a jQuery Event object to a shortcut (shift-r)
121
var shortcut = '';
122
var key = inv_keycodes[event.which];
123
if (event.altKey && key !== 'alt') {shortcut += 'alt-';}
124
if (event.ctrlKey && key !== 'ctrl') {shortcut += 'ctrl-';}
125
if (event.metaKey && key !== 'meta') {shortcut += 'meta-';}
126
if (event.shiftKey && key !== 'shift') {shortcut += 'shift-';}
127
shortcut += key;
128
return shortcut;
129
};
130
131
// Shortcut manager class
132
133
var ShortcutManager = function (delay) {
134
this._shortcuts = {};
135
this._counts = {};
136
this._timers = {};
137
this.delay = delay || 800; // delay in milliseconds
138
};
139
140
ShortcutManager.prototype.help = function () {
141
var help = [];
142
for (var shortcut in this._shortcuts) {
143
var help_string = this._shortcuts[shortcut]['help'];
144
var help_index = this._shortcuts[shortcut]['help_index'];
145
if (help_string) {
146
if (platform === 'MacOS') {
147
shortcut = shortcut.replace('meta', 'cmd');
148
}
149
help.push({
150
shortcut: shortcut,
151
help: help_string,
152
help_index: help_index}
153
);
154
}
155
}
156
help.sort(function (a, b) {
157
if (a.help_index > b.help_index)
158
return 1;
159
if (a.help_index < b.help_index)
160
return -1;
161
return 0;
162
});
163
return help;
164
};
165
166
ShortcutManager.prototype.clear_shortcuts = function () {
167
this._shortcuts = {};
168
};
169
170
ShortcutManager.prototype.add_shortcut = function (shortcut, data, suppress_help_update) {
171
if (typeof(data) === 'function') {
172
data = {help: '', help_index: '', handler: data};
173
}
174
data.help_index = data.help_index || '';
175
data.help = data.help || '';
176
data.count = data.count || 1;
177
if (data.help_index === '') {
178
data.help_index = 'zz';
179
}
180
shortcut = normalize_shortcut(shortcut);
181
this._counts[shortcut] = 0;
182
this._shortcuts[shortcut] = data;
183
if (!suppress_help_update) {
184
// update the keyboard shortcuts notebook help
185
$([IPython.events]).trigger('rebuild.QuickHelp');
186
}
187
};
188
189
ShortcutManager.prototype.add_shortcuts = function (data) {
190
for (var shortcut in data) {
191
this.add_shortcut(shortcut, data[shortcut], true);
192
}
193
// update the keyboard shortcuts notebook help
194
$([IPython.events]).trigger('rebuild.QuickHelp');
195
};
196
197
ShortcutManager.prototype.remove_shortcut = function (shortcut, suppress_help_update) {
198
shortcut = normalize_shortcut(shortcut);
199
delete this._counts[shortcut];
200
delete this._shortcuts[shortcut];
201
if (!suppress_help_update) {
202
// update the keyboard shortcuts notebook help
203
$([IPython.events]).trigger('rebuild.QuickHelp');
204
}
205
};
206
207
ShortcutManager.prototype.count_handler = function (shortcut, event, data) {
208
var that = this;
209
var c = this._counts;
210
var t = this._timers;
211
var timer = null;
212
if (c[shortcut] === data.count-1) {
213
c[shortcut] = 0;
214
var timer = t[shortcut];
215
if (timer) {clearTimeout(timer); delete t[shortcut];}
216
return data.handler(event);
217
} else {
218
c[shortcut] = c[shortcut] + 1;
219
timer = setTimeout(function () {
220
c[shortcut] = 0;
221
}, that.delay);
222
t[shortcut] = timer;
223
}
224
return false;
225
};
226
227
ShortcutManager.prototype.call_handler = function (event) {
228
var shortcut = event_to_shortcut(event);
229
var data = this._shortcuts[shortcut];
230
if (data) {
231
var handler = data['handler'];
232
if (handler) {
233
if (data.count === 1) {
234
return handler(event);
235
} else if (data.count > 1) {
236
return this.count_handler(shortcut, event, data);
237
}
238
}
239
}
240
return true;
241
};
242
243
ShortcutManager.prototype.handles = function (event) {
244
var shortcut = event_to_shortcut(event);
245
var data = this._shortcuts[shortcut];
246
return !( data === undefined || data.handler === undefined )
247
}
248
249
return {
250
keycodes : keycodes,
251
inv_keycodes : inv_keycodes,
252
ShortcutManager : ShortcutManager,
253
normalize_key : normalize_key,
254
normalize_shortcut : normalize_shortcut,
255
shortcut_to_event : shortcut_to_event,
256
event_to_shortcut : event_to_shortcut
257
};
258
259
}(IPython));
260
261