##// END OF EJS Templates
handle keyboard shortcuts ending with '-'
Paul Ivanov -
Show More
@@ -1,261 +1,263 b''
1 //----------------------------------------------------------------------------
1 //----------------------------------------------------------------------------
2 // Copyright (C) 2011 The IPython Development Team
2 // Copyright (C) 2011 The IPython Development Team
3 //
3 //
4 // Distributed under the terms of the BSD License. The full license is in
4 // Distributed under the terms of the BSD License. The full license is in
5 // the file COPYING, distributed as part of this software.
5 // the file COPYING, distributed as part of this software.
6 //----------------------------------------------------------------------------
6 //----------------------------------------------------------------------------
7
7
8 //============================================================================
8 //============================================================================
9 // Keyboard management
9 // Keyboard management
10 //============================================================================
10 //============================================================================
11
11
12 IPython.namespace('IPython.keyboard');
12 IPython.namespace('IPython.keyboard');
13
13
14 IPython.keyboard = (function (IPython) {
14 IPython.keyboard = (function (IPython) {
15 "use strict";
15 "use strict";
16
16
17 // Setup global keycodes and inverse keycodes.
17 // Setup global keycodes and inverse keycodes.
18
18
19 // See http://unixpapa.com/js/key.html for a complete description. The short of
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"
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
21 // and Webkit/IE use the "IE keycodes". These keycode sets are mostly the same
22 // but have minor differences.
22 // but have minor differences.
23
23
24 // These apply to Firefox, (Webkit and IE)
24 // These apply to Firefox, (Webkit and IE)
25 var _keycodes = {
25 var _keycodes = {
26 'a': 65, 'b': 66, 'c': 67, 'd': 68, 'e': 69, 'f': 70, 'g': 71, 'h': 72, 'i': 73,
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,
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,
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,
29 '1 !': 49, '2 @': 50, '3 #': 51, '4 $': 52, '5 %': 53, '6 ^': 54,
30 '7 &': 55, '8 *': 56, '9 (': 57, '0 )': 48,
30 '7 &': 55, '8 *': 56, '9 (': 57, '0 )': 48,
31 '[ {': 219, '] }': 221, '` ~': 192, ', <': 188, '. >': 190, '/ ?': 191,
31 '[ {': 219, '] }': 221, '` ~': 192, ', <': 188, '. >': 190, '/ ?': 191,
32 '\\ |': 220, '\' "': 222,
32 '\\ |': 220, '\' "': 222,
33 'numpad0': 96, 'numpad1': 97, 'numpad2': 98, 'numpad3': 99, 'numpad4': 100,
33 'numpad0': 96, 'numpad1': 97, 'numpad2': 98, 'numpad3': 99, 'numpad4': 100,
34 'numpad5': 101, 'numpad6': 102, 'numpad7': 103, 'numpad8': 104, 'numpad9': 105,
34 'numpad5': 101, 'numpad6': 102, 'numpad7': 103, 'numpad8': 104, 'numpad9': 105,
35 'multiply': 106, 'add': 107, 'subtract': 109, 'decimal': 110, 'divide': 111,
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,
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,
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,
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,
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,
40 'end': 35, 'home': 36, 'left': 37, 'up': 38, 'right': 39, 'down': 40,
41 'insert': 45, 'delete': 46, 'numlock': 144,
41 'insert': 45, 'delete': 46, 'numlock': 144,
42 };
42 };
43
43
44 // These apply to Firefox and Opera
44 // These apply to Firefox and Opera
45 var _mozilla_keycodes = {
45 var _mozilla_keycodes = {
46 '; :': 59, '= +': 61, '- _': 173, 'meta': 224
46 '; :': 59, '= +': 61, '- _': 173, 'meta': 224
47 };
47 };
48
48
49 // This apply to Webkit and IE
49 // This apply to Webkit and IE
50 var _ie_keycodes = {
50 var _ie_keycodes = {
51 '; :': 186, '= +': 187, '- _': 189,
51 '; :': 186, '= +': 187, '- _': 189,
52 };
52 };
53
53
54 var browser = IPython.utils.browser[0];
54 var browser = IPython.utils.browser[0];
55 var platform = IPython.utils.platform;
55 var platform = IPython.utils.platform;
56
56
57 if (browser === 'Firefox' || browser === 'Opera') {
57 if (browser === 'Firefox' || browser === 'Opera') {
58 $.extend(_keycodes, _mozilla_keycodes);
58 $.extend(_keycodes, _mozilla_keycodes);
59 } else if (browser === 'Safari' || browser === 'Chrome' || browser === 'MSIE') {
59 } else if (browser === 'Safari' || browser === 'Chrome' || browser === 'MSIE') {
60 $.extend(_keycodes, _ie_keycodes);
60 $.extend(_keycodes, _ie_keycodes);
61 }
61 }
62
62
63 var keycodes = {};
63 var keycodes = {};
64 var inv_keycodes = {};
64 var inv_keycodes = {};
65 for (var name in _keycodes) {
65 for (var name in _keycodes) {
66 var names = name.split(' ');
66 var names = name.split(' ');
67 if (names.length === 1) {
67 if (names.length === 1) {
68 var n = names[0];
68 var n = names[0];
69 keycodes[n] = _keycodes[n];
69 keycodes[n] = _keycodes[n];
70 inv_keycodes[_keycodes[n]] = n;
70 inv_keycodes[_keycodes[n]] = n;
71 } else {
71 } else {
72 var primary = names[0];
72 var primary = names[0];
73 var secondary = names[1];
73 var secondary = names[1];
74 keycodes[primary] = _keycodes[name];
74 keycodes[primary] = _keycodes[name];
75 keycodes[secondary] = _keycodes[name];
75 keycodes[secondary] = _keycodes[name];
76 inv_keycodes[_keycodes[name]] = primary;
76 inv_keycodes[_keycodes[name]] = primary;
77 }
77 }
78 }
78 }
79
79
80 var normalize_key = function (key) {
80 var normalize_key = function (key) {
81 return inv_keycodes[keycodes[key]];
81 return inv_keycodes[keycodes[key]];
82 };
82 };
83
83
84 var normalize_shortcut = function (shortcut) {
84 var normalize_shortcut = function (shortcut) {
85 // Put a shortcut into normalized form:
85 // Put a shortcut into normalized form:
86 // 1. Make lowercase
86 // 1. Make lowercase
87 // 2. Replace cmd by meta
87 // 2. Replace cmd by meta
88 // 3. Sort '-' separated modifiers into the order alt-ctrl-meta-shift
88 // 3. Sort '-' separated modifiers into the order alt-ctrl-meta-shift
89 // 4. Normalize keys
89 // 4. Normalize keys
90 shortcut = shortcut.toLowerCase().replace('cmd', 'meta');
90 shortcut = shortcut.toLowerCase().replace('cmd', 'meta');
91 shortcut = shortcut.replace('--', '-'); // catch shortcuts using '-' key
91 var values = shortcut.split("-");
92 var values = shortcut.split("-");
92 if (values.length === 1) {
93 if (values.length === 1) {
93 return normalize_key(values[0]);
94 return normalize_key(values[0]);
94 } else {
95 } else {
95 var modifiers = values.slice(0,-1);
96 var modifiers = values.slice(0,-1);
96 var key = normalize_key(values[values.length-1]);
97 var key = normalize_key(values[values.length-1]);
98 key = key || '-'; // '-' will make the key undefined, since we split on it
97 modifiers.sort();
99 modifiers.sort();
98 return modifiers.join('-') + '-' + key;
100 return modifiers.join('-') + '-' + key;
99 }
101 }
100 };
102 };
101
103
102 var shortcut_to_event = function (shortcut, type) {
104 var shortcut_to_event = function (shortcut, type) {
103 // Convert a shortcut (shift-r) to a jQuery Event object
105 // Convert a shortcut (shift-r) to a jQuery Event object
104 type = type || 'keydown';
106 type = type || 'keydown';
105 shortcut = normalize_shortcut(shortcut);
107 shortcut = normalize_shortcut(shortcut);
106 var values = shortcut.split("-");
108 var values = shortcut.split("-");
107 var modifiers = values.slice(0,-1);
109 var modifiers = values.slice(0,-1);
108 var key = values[values.length-1];
110 var key = values[values.length-1];
109 var opts = {which: keycodes[key]};
111 var opts = {which: keycodes[key]};
110 if (modifiers.indexOf('alt') !== -1) {opts.altKey = true;}
112 if (modifiers.indexOf('alt') !== -1) {opts.altKey = true;}
111 if (modifiers.indexOf('ctrl') !== -1) {opts.ctrlKey = true;}
113 if (modifiers.indexOf('ctrl') !== -1) {opts.ctrlKey = true;}
112 if (modifiers.indexOf('meta') !== -1) {opts.metaKey = true;}
114 if (modifiers.indexOf('meta') !== -1) {opts.metaKey = true;}
113 if (modifiers.indexOf('shift') !== -1) {opts.shiftKey = true;}
115 if (modifiers.indexOf('shift') !== -1) {opts.shiftKey = true;}
114 return $.Event(type, opts);
116 return $.Event(type, opts);
115 };
117 };
116
118
117 var event_to_shortcut = function (event) {
119 var event_to_shortcut = function (event) {
118 // Convert a jQuery Event object to a shortcut (shift-r)
120 // Convert a jQuery Event object to a shortcut (shift-r)
119 var shortcut = '';
121 var shortcut = '';
120 var key = inv_keycodes[event.which];
122 var key = inv_keycodes[event.which];
121 if (event.altKey && key !== 'alt') {shortcut += 'alt-';}
123 if (event.altKey && key !== 'alt') {shortcut += 'alt-';}
122 if (event.ctrlKey && key !== 'ctrl') {shortcut += 'ctrl-';}
124 if (event.ctrlKey && key !== 'ctrl') {shortcut += 'ctrl-';}
123 if (event.metaKey && key !== 'meta') {shortcut += 'meta-';}
125 if (event.metaKey && key !== 'meta') {shortcut += 'meta-';}
124 if (event.shiftKey && key !== 'shift') {shortcut += 'shift-';}
126 if (event.shiftKey && key !== 'shift') {shortcut += 'shift-';}
125 shortcut += key;
127 shortcut += key;
126 return shortcut;
128 return shortcut;
127 };
129 };
128
130
129 var trigger_keydown = function (shortcut, element) {
131 var trigger_keydown = function (shortcut, element) {
130 // Trigger shortcut keydown on an element
132 // Trigger shortcut keydown on an element
131 element = element || document;
133 element = element || document;
132 element = $(element);
134 element = $(element);
133 var event = shortcut_to_event(shortcut, 'keydown');
135 var event = shortcut_to_event(shortcut, 'keydown');
134 element.trigger(event);
136 element.trigger(event);
135 };
137 };
136
138
137
139
138 // Shortcut manager class
140 // Shortcut manager class
139
141
140 var ShortcutManager = function (delay) {
142 var ShortcutManager = function (delay) {
141 this._shortcuts = {};
143 this._shortcuts = {};
142 this._counts = {};
144 this._counts = {};
143 this._timers = {};
145 this._timers = {};
144 this.delay = delay || 800; // delay in milliseconds
146 this.delay = delay || 800; // delay in milliseconds
145 };
147 };
146
148
147 ShortcutManager.prototype.help = function () {
149 ShortcutManager.prototype.help = function () {
148 var help = [];
150 var help = [];
149 for (var shortcut in this._shortcuts) {
151 for (var shortcut in this._shortcuts) {
150 var help_string = this._shortcuts[shortcut]['help'];
152 var help_string = this._shortcuts[shortcut]['help'];
151 var help_index = this._shortcuts[shortcut]['help_index'];
153 var help_index = this._shortcuts[shortcut]['help_index'];
152 if (help_string) {
154 if (help_string) {
153 if (platform === 'MacOS') {
155 if (platform === 'MacOS') {
154 shortcut = shortcut.replace('meta', 'cmd');
156 shortcut = shortcut.replace('meta', 'cmd');
155 }
157 }
156 help.push({
158 help.push({
157 shortcut: shortcut,
159 shortcut: shortcut,
158 help: help_string,
160 help: help_string,
159 help_index: help_index}
161 help_index: help_index}
160 );
162 );
161 }
163 }
162 }
164 }
163 help.sort(function (a, b) {
165 help.sort(function (a, b) {
164 if (a.help_index > b.help_index)
166 if (a.help_index > b.help_index)
165 return 1;
167 return 1;
166 if (a.help_index < b.help_index)
168 if (a.help_index < b.help_index)
167 return -1;
169 return -1;
168 return 0;
170 return 0;
169 });
171 });
170 return help;
172 return help;
171 };
173 };
172
174
173 ShortcutManager.prototype.clear_shortcuts = function () {
175 ShortcutManager.prototype.clear_shortcuts = function () {
174 this._shortcuts = {};
176 this._shortcuts = {};
175 };
177 };
176
178
177 ShortcutManager.prototype.add_shortcut = function (shortcut, data, suppress_help_update) {
179 ShortcutManager.prototype.add_shortcut = function (shortcut, data, suppress_help_update) {
178 if (typeof(data) === 'function') {
180 if (typeof(data) === 'function') {
179 data = {help: '', help_index: '', handler: data};
181 data = {help: '', help_index: '', handler: data};
180 }
182 }
181 data.help_index = data.help_index || '';
183 data.help_index = data.help_index || '';
182 data.help = data.help || '';
184 data.help = data.help || '';
183 data.count = data.count || 1;
185 data.count = data.count || 1;
184 if (data.help_index === '') {
186 if (data.help_index === '') {
185 data.help_index = 'zz';
187 data.help_index = 'zz';
186 }
188 }
187 shortcut = normalize_shortcut(shortcut);
189 shortcut = normalize_shortcut(shortcut);
188 this._counts[shortcut] = 0;
190 this._counts[shortcut] = 0;
189 this._shortcuts[shortcut] = data;
191 this._shortcuts[shortcut] = data;
190 if (!suppress_help_update) {
192 if (!suppress_help_update) {
191 // update the keyboard shortcuts notebook help
193 // update the keyboard shortcuts notebook help
192 $([IPython.events]).trigger('rebuild.QuickHelp');
194 $([IPython.events]).trigger('rebuild.QuickHelp');
193 }
195 }
194 };
196 };
195
197
196 ShortcutManager.prototype.add_shortcuts = function (data) {
198 ShortcutManager.prototype.add_shortcuts = function (data) {
197 for (var shortcut in data) {
199 for (var shortcut in data) {
198 this.add_shortcut(shortcut, data[shortcut], true);
200 this.add_shortcut(shortcut, data[shortcut], true);
199 }
201 }
200 // update the keyboard shortcuts notebook help
202 // update the keyboard shortcuts notebook help
201 $([IPython.events]).trigger('rebuild.QuickHelp');
203 $([IPython.events]).trigger('rebuild.QuickHelp');
202 };
204 };
203
205
204 ShortcutManager.prototype.remove_shortcut = function (shortcut, suppress_help_update) {
206 ShortcutManager.prototype.remove_shortcut = function (shortcut, suppress_help_update) {
205 shortcut = normalize_shortcut(shortcut);
207 shortcut = normalize_shortcut(shortcut);
206 delete this._counts[shortcut];
208 delete this._counts[shortcut];
207 delete this._shortcuts[shortcut];
209 delete this._shortcuts[shortcut];
208 if (!suppress_help_update) {
210 if (!suppress_help_update) {
209 // update the keyboard shortcuts notebook help
211 // update the keyboard shortcuts notebook help
210 $([IPython.events]).trigger('rebuild.QuickHelp');
212 $([IPython.events]).trigger('rebuild.QuickHelp');
211 }
213 }
212 };
214 };
213
215
214 ShortcutManager.prototype.count_handler = function (shortcut, event, data) {
216 ShortcutManager.prototype.count_handler = function (shortcut, event, data) {
215 var that = this;
217 var that = this;
216 var c = this._counts;
218 var c = this._counts;
217 var t = this._timers;
219 var t = this._timers;
218 var timer = null;
220 var timer = null;
219 if (c[shortcut] === data.count-1) {
221 if (c[shortcut] === data.count-1) {
220 c[shortcut] = 0;
222 c[shortcut] = 0;
221 var timer = t[shortcut];
223 var timer = t[shortcut];
222 if (timer) {clearTimeout(timer); delete t[shortcut];}
224 if (timer) {clearTimeout(timer); delete t[shortcut];}
223 return data.handler(event);
225 return data.handler(event);
224 } else {
226 } else {
225 c[shortcut] = c[shortcut] + 1;
227 c[shortcut] = c[shortcut] + 1;
226 timer = setTimeout(function () {
228 timer = setTimeout(function () {
227 c[shortcut] = 0;
229 c[shortcut] = 0;
228 }, that.delay);
230 }, that.delay);
229 t[shortcut] = timer;
231 t[shortcut] = timer;
230 }
232 }
231 return false;
233 return false;
232 };
234 };
233
235
234 ShortcutManager.prototype.call_handler = function (event) {
236 ShortcutManager.prototype.call_handler = function (event) {
235 var shortcut = event_to_shortcut(event);
237 var shortcut = event_to_shortcut(event);
236 var data = this._shortcuts[shortcut];
238 var data = this._shortcuts[shortcut];
237 if (data) {
239 if (data) {
238 var handler = data['handler'];
240 var handler = data['handler'];
239 if (handler) {
241 if (handler) {
240 if (data.count === 1) {
242 if (data.count === 1) {
241 return handler(event);
243 return handler(event);
242 } else if (data.count > 1) {
244 } else if (data.count > 1) {
243 return this.count_handler(shortcut, event, data);
245 return this.count_handler(shortcut, event, data);
244 }
246 }
245 }
247 }
246 }
248 }
247 return true;
249 return true;
248 };
250 };
249
251
250 return {
252 return {
251 keycodes : keycodes,
253 keycodes : keycodes,
252 inv_keycodes : inv_keycodes,
254 inv_keycodes : inv_keycodes,
253 ShortcutManager : ShortcutManager,
255 ShortcutManager : ShortcutManager,
254 normalize_key : normalize_key,
256 normalize_key : normalize_key,
255 normalize_shortcut : normalize_shortcut,
257 normalize_shortcut : normalize_shortcut,
256 shortcut_to_event : shortcut_to_event,
258 shortcut_to_event : shortcut_to_event,
257 event_to_shortcut : event_to_shortcut,
259 event_to_shortcut : event_to_shortcut,
258 trigger_keydown : trigger_keydown
260 trigger_keydown : trigger_keydown
259 };
261 };
260
262
261 }(IPython));
263 }(IPython));
@@ -1,127 +1,128 b''
1 //----------------------------------------------------------------------------
1 //----------------------------------------------------------------------------
2 // Copyright (C) 2008-2011 The IPython Development Team
2 // Copyright (C) 2008-2011 The IPython Development Team
3 //
3 //
4 // Distributed under the terms of the BSD License. The full license is in
4 // Distributed under the terms of the BSD License. The full license is in
5 // the file COPYING, distributed as part of this software.
5 // the file COPYING, distributed as part of this software.
6 //----------------------------------------------------------------------------
6 //----------------------------------------------------------------------------
7
7
8 //============================================================================
8 //============================================================================
9 // QuickHelp button
9 // QuickHelp button
10 //============================================================================
10 //============================================================================
11
11
12 var IPython = (function (IPython) {
12 var IPython = (function (IPython) {
13 "use strict";
13 "use strict";
14
14
15 var QuickHelp = function (selector) {
15 var QuickHelp = function (selector) {
16 };
16 };
17
17
18 QuickHelp.prototype.show_keyboard_shortcuts = function () {
18 QuickHelp.prototype.show_keyboard_shortcuts = function () {
19 // toggles display of keyboard shortcut dialog
19 // toggles display of keyboard shortcut dialog
20 var that = this;
20 var that = this;
21 if ( this.force_rebuild ) {
21 if ( this.force_rebuild ) {
22 this.shortcut_dialog.remove();
22 this.shortcut_dialog.remove();
23 delete(this.shortcut_dialog);
23 delete(this.shortcut_dialog);
24 this.force_rebuild = false;
24 this.force_rebuild = false;
25 }
25 }
26 if ( this.shortcut_dialog ){
26 if ( this.shortcut_dialog ){
27 // if dialog is already shown, close it
27 // if dialog is already shown, close it
28 $(this.shortcut_dialog).modal("toggle");
28 $(this.shortcut_dialog).modal("toggle");
29 return;
29 return;
30 }
30 }
31 var command_shortcuts = IPython.keyboard_manager.command_shortcuts.help();
31 var command_shortcuts = IPython.keyboard_manager.command_shortcuts.help();
32 var edit_shortcuts = IPython.keyboard_manager.edit_shortcuts.help();
32 var edit_shortcuts = IPython.keyboard_manager.edit_shortcuts.help();
33 var help, shortcut;
33 var help, shortcut;
34 var i, half, n;
34 var i, half, n;
35 var element = $('<div/>');
35 var element = $('<div/>');
36
36
37 // The documentation
37 // The documentation
38 var doc = $('<div/>').addClass('alert');
38 var doc = $('<div/>').addClass('alert');
39 doc.append(
39 doc.append(
40 $('<button/>').addClass('close').attr('data-dismiss','alert').html('&times;')
40 $('<button/>').addClass('close').attr('data-dismiss','alert').html('&times;')
41 ).append(
41 ).append(
42 'The IPython Notebook has two different keyboard input modes. <b>Edit mode</b> '+
42 'The IPython Notebook has two different keyboard input modes. <b>Edit mode</b> '+
43 'allows you to type code/text into a cell and is indicated by a green cell '+
43 'allows you to type code/text into a cell and is indicated by a green cell '+
44 'border. <b>Command mode</b> binds the keyboard to notebook level actions '+
44 'border. <b>Command mode</b> binds the keyboard to notebook level actions '+
45 'and is indicated by a grey cell border.'
45 'and is indicated by a grey cell border.'
46 );
46 );
47 element.append(doc);
47 element.append(doc);
48
48
49 // Command mode
49 // Command mode
50 var cmd_div = this.build_command_help();
50 var cmd_div = this.build_command_help();
51 element.append(cmd_div);
51 element.append(cmd_div);
52
52
53 // Edit mode
53 // Edit mode
54 var edit_div = this.build_edit_help();
54 var edit_div = this.build_edit_help();
55 element.append(edit_div);
55 element.append(edit_div);
56
56
57 this.shortcut_dialog = IPython.dialog.modal({
57 this.shortcut_dialog = IPython.dialog.modal({
58 title : "Keyboard shortcuts",
58 title : "Keyboard shortcuts",
59 body : element,
59 body : element,
60 destroy : false,
60 destroy : false,
61 buttons : {
61 buttons : {
62 Close : {}
62 Close : {}
63 }
63 }
64 });
64 });
65
65
66 $([IPython.events]).on('rebuild.QuickHelp', function() { that.force_rebuild = true;});
66 $([IPython.events]).on('rebuild.QuickHelp', function() { that.force_rebuild = true;});
67 };
67 };
68
68
69 QuickHelp.prototype.build_command_help = function () {
69 QuickHelp.prototype.build_command_help = function () {
70 var command_shortcuts = IPython.keyboard_manager.command_shortcuts.help();
70 var command_shortcuts = IPython.keyboard_manager.command_shortcuts.help();
71 return build_div('<h4>Command Mode (press <code>Esc</code> to enable)</h4>', command_shortcuts);
71 return build_div('<h4>Command Mode (press <code>Esc</code> to enable)</h4>', command_shortcuts);
72 };
72 };
73
73
74 var special_case = { pageup: "PageUp", pagedown: "Page Down" };
74 var special_case = { pageup: "PageUp", pagedown: "Page Down", '': '-' };
75 var prettify = function (s) {
75 var prettify = function (s) {
76 s = s.replace('--', '-'); // catch shortcuts using '-' key
76 var keys = s.split('-');
77 var keys = s.split('-');
77 var k, i;
78 var k, i;
78 for (i in keys) {
79 for (i in keys) {
79 k = keys[i];
80 k = keys[i];
80 if ( k.length == 1 ) {
81 if ( k.length == 1 ) {
81 keys[i] = "<code><strong>" + k + "</strong></code>";
82 keys[i] = "<code><strong>" + k + "</strong></code>";
82 continue; // leave individual keys lower-cased
83 continue; // leave individual keys lower-cased
83 }
84 }
84 keys[i] = ( special_case[k] ? special_case[k] : k.charAt(0).toUpperCase() + k.slice(1) );
85 keys[i] = ( special_case[k] ? special_case[k] : k.charAt(0).toUpperCase() + k.slice(1) );
85 keys[i] = "<code><strong>" + keys[i] + "</strong></code>";
86 keys[i] = "<code><strong>" + keys[i] + "</strong></code>";
86 }
87 }
87 return keys.join('-');
88 return keys.join('-');
88
89
89
90
90 };
91 };
91
92
92 QuickHelp.prototype.build_edit_help = function () {
93 QuickHelp.prototype.build_edit_help = function () {
93 var edit_shortcuts = IPython.keyboard_manager.edit_shortcuts.help();
94 var edit_shortcuts = IPython.keyboard_manager.edit_shortcuts.help();
94 // Edit mode
95 // Edit mode
95 return build_div('<h4>Edit Mode (press <code>Enter</code> to enable)</h4>', edit_shortcuts);
96 return build_div('<h4>Edit Mode (press <code>Enter</code> to enable)</h4>', edit_shortcuts);
96 };
97 };
97
98
98 var build_one = function (s) {
99 var build_one = function (s) {
99 var help = s.help;
100 var help = s.help;
100 var shortcut = prettify(s.shortcut);
101 var shortcut = prettify(s.shortcut);
101 return $('<div>').addClass('quickhelp').
102 return $('<div>').addClass('quickhelp').
102 append($('<span/>').addClass('shortcut_key').append($(shortcut))).
103 append($('<span/>').addClass('shortcut_key').append($(shortcut))).
103 append($('<span/>').addClass('shortcut_descr').text(' : ' + help));
104 append($('<span/>').addClass('shortcut_descr').text(' : ' + help));
104
105
105 };
106 };
106
107
107 var build_div = function (title, shortcuts) {
108 var build_div = function (title, shortcuts) {
108 var i, half, n;
109 var i, half, n;
109 var div = $('<div/>').append($(title));
110 var div = $('<div/>').append($(title));
110 var sub_div = $('<div/>').addClass('hbox');
111 var sub_div = $('<div/>').addClass('hbox');
111 var col1 = $('<div/>').addClass('box-flex0');
112 var col1 = $('<div/>').addClass('box-flex0');
112 var col2 = $('<div/>').addClass('box-flex0');
113 var col2 = $('<div/>').addClass('box-flex0');
113 n = shortcuts.length;
114 n = shortcuts.length;
114 half = ~~(n/2); // Truncate :)
115 half = ~~(n/2); // Truncate :)
115 for (i=0; i<half; i++) { col1.append( build_one(shortcuts[i]) ); }
116 for (i=0; i<half; i++) { col1.append( build_one(shortcuts[i]) ); }
116 for (i=half; i<n; i++) { col2.append( build_one(shortcuts[i]) ); }
117 for (i=half; i<n; i++) { col2.append( build_one(shortcuts[i]) ); }
117 sub_div.append(col1).append(col2);
118 sub_div.append(col1).append(col2);
118 div.append(sub_div);
119 div.append(sub_div);
119 return div;
120 return div;
120 };
121 };
121
122
122 // Set module variables
123 // Set module variables
123 IPython.QuickHelp = QuickHelp;
124 IPython.QuickHelp = QuickHelp;
124
125
125 return IPython;
126 return IPython;
126
127
127 }(IPython));
128 }(IPython));
General Comments 0
You need to be logged in to leave comments. Login now