##// END OF EJS Templates
stack vertically keyboard shortcut on narrow viewport.
Matthias Bussonnier -
Show More
@@ -1,269 +1,269 b''
1 // Copyright (c) IPython Development Team.
1 // Copyright (c) IPython Development Team.
2 // Distributed under the terms of the Modified BSD License.
2 // Distributed under the terms of the Modified BSD License.
3
3
4 define([
4 define([
5 'base/js/namespace',
5 'base/js/namespace',
6 'jquery',
6 'jquery',
7 'base/js/utils',
7 'base/js/utils',
8 'base/js/dialog',
8 'base/js/dialog',
9 ], function(IPython, $, utils, dialog) {
9 ], function(IPython, $, utils, dialog) {
10 "use strict";
10 "use strict";
11 var platform = utils.platform;
11 var platform = utils.platform;
12
12
13 var QuickHelp = function (options) {
13 var QuickHelp = function (options) {
14 /**
14 /**
15 * Constructor
15 * Constructor
16 *
16 *
17 * Parameters:
17 * Parameters:
18 * options: dictionary
18 * options: dictionary
19 * Dictionary of keyword arguments.
19 * Dictionary of keyword arguments.
20 * events: $(Events) instance
20 * events: $(Events) instance
21 * keyboard_manager: KeyboardManager instance
21 * keyboard_manager: KeyboardManager instance
22 * notebook: Notebook instance
22 * notebook: Notebook instance
23 */
23 */
24 this.keyboard_manager = options.keyboard_manager;
24 this.keyboard_manager = options.keyboard_manager;
25 this.notebook = options.notebook;
25 this.notebook = options.notebook;
26 this.keyboard_manager.quick_help = this;
26 this.keyboard_manager.quick_help = this;
27 this.events = options.events;
27 this.events = options.events;
28 };
28 };
29
29
30 var cmd_ctrl = 'Ctrl-';
30 var cmd_ctrl = 'Ctrl-';
31 var platform_specific;
31 var platform_specific;
32
32
33 if (platform === 'MacOS') {
33 if (platform === 'MacOS') {
34 // Mac OS X specific
34 // Mac OS X specific
35 cmd_ctrl = 'Cmd-';
35 cmd_ctrl = 'Cmd-';
36 platform_specific = [
36 platform_specific = [
37 { shortcut: "Cmd-Up", help:"go to cell start" },
37 { shortcut: "Cmd-Up", help:"go to cell start" },
38 { shortcut: "Cmd-Down", help:"go to cell end" },
38 { shortcut: "Cmd-Down", help:"go to cell end" },
39 { shortcut: "Alt-Left", help:"go one word left" },
39 { shortcut: "Alt-Left", help:"go one word left" },
40 { shortcut: "Alt-Right", help:"go one word right" },
40 { shortcut: "Alt-Right", help:"go one word right" },
41 { shortcut: "Alt-Backspace", help:"del word before" },
41 { shortcut: "Alt-Backspace", help:"del word before" },
42 { shortcut: "Alt-Delete", help:"del word after" },
42 { shortcut: "Alt-Delete", help:"del word after" },
43 ];
43 ];
44 } else {
44 } else {
45 // PC specific
45 // PC specific
46 platform_specific = [
46 platform_specific = [
47 { shortcut: "Ctrl-Home", help:"go to cell start" },
47 { shortcut: "Ctrl-Home", help:"go to cell start" },
48 { shortcut: "Ctrl-Up", help:"go to cell start" },
48 { shortcut: "Ctrl-Up", help:"go to cell start" },
49 { shortcut: "Ctrl-End", help:"go to cell end" },
49 { shortcut: "Ctrl-End", help:"go to cell end" },
50 { shortcut: "Ctrl-Down", help:"go to cell end" },
50 { shortcut: "Ctrl-Down", help:"go to cell end" },
51 { shortcut: "Ctrl-Left", help:"go one word left" },
51 { shortcut: "Ctrl-Left", help:"go one word left" },
52 { shortcut: "Ctrl-Right", help:"go one word right" },
52 { shortcut: "Ctrl-Right", help:"go one word right" },
53 { shortcut: "Ctrl-Backspace", help:"del word before" },
53 { shortcut: "Ctrl-Backspace", help:"del word before" },
54 { shortcut: "Ctrl-Delete", help:"del word after" },
54 { shortcut: "Ctrl-Delete", help:"del word after" },
55 ];
55 ];
56 }
56 }
57
57
58 var cm_shortcuts = [
58 var cm_shortcuts = [
59 { shortcut:"Tab", help:"code completion or indent" },
59 { shortcut:"Tab", help:"code completion or indent" },
60 { shortcut:"Shift-Tab", help:"tooltip" },
60 { shortcut:"Shift-Tab", help:"tooltip" },
61 { shortcut: cmd_ctrl + "]", help:"indent" },
61 { shortcut: cmd_ctrl + "]", help:"indent" },
62 { shortcut: cmd_ctrl + "[", help:"dedent" },
62 { shortcut: cmd_ctrl + "[", help:"dedent" },
63 { shortcut: cmd_ctrl + "a", help:"select all" },
63 { shortcut: cmd_ctrl + "a", help:"select all" },
64 { shortcut: cmd_ctrl + "z", help:"undo" },
64 { shortcut: cmd_ctrl + "z", help:"undo" },
65 { shortcut: cmd_ctrl + "Shift-z", help:"redo" },
65 { shortcut: cmd_ctrl + "Shift-z", help:"redo" },
66 { shortcut: cmd_ctrl + "y", help:"redo" },
66 { shortcut: cmd_ctrl + "y", help:"redo" },
67 ].concat( platform_specific );
67 ].concat( platform_specific );
68
68
69 var mac_humanize_map = {
69 var mac_humanize_map = {
70 // all these are unicode, will probably display badly on anything except macs.
70 // all these are unicode, will probably display badly on anything except macs.
71 // these are the standard symbol that are used in MacOS native menus
71 // these are the standard symbol that are used in MacOS native menus
72 // cf http://apple.stackexchange.com/questions/55727/
72 // cf http://apple.stackexchange.com/questions/55727/
73 // for htmlentities and/or unicode value
73 // for htmlentities and/or unicode value
74 'cmd':'⌘',
74 'cmd':'⌘',
75 'shift':'⇧',
75 'shift':'⇧',
76 'alt':'βŒ₯',
76 'alt':'βŒ₯',
77 'up':'↑',
77 'up':'↑',
78 'down':'↓',
78 'down':'↓',
79 'left':'←',
79 'left':'←',
80 'right':'β†’',
80 'right':'β†’',
81 'eject':'⏏',
81 'eject':'⏏',
82 'tab':'β‡₯',
82 'tab':'β‡₯',
83 'backtab':'⇀',
83 'backtab':'⇀',
84 'capslock':'β‡ͺ',
84 'capslock':'β‡ͺ',
85 'esc':'βŽ‹',
85 'esc':'βŽ‹',
86 'ctrl':'βŒƒ',
86 'ctrl':'βŒƒ',
87 'enter':'↩',
87 'enter':'↩',
88 'pageup':'β‡ž',
88 'pageup':'β‡ž',
89 'pagedown':'β‡Ÿ',
89 'pagedown':'β‡Ÿ',
90 'home':'β†–',
90 'home':'β†–',
91 'end':'β†˜',
91 'end':'β†˜',
92 'altenter':'⌀',
92 'altenter':'⌀',
93 'space':'␣',
93 'space':'␣',
94 'delete':'⌦',
94 'delete':'⌦',
95 'backspace':'⌫',
95 'backspace':'⌫',
96 'apple':'ο£Ώ',
96 'apple':'ο£Ώ',
97 };
97 };
98
98
99 var default_humanize_map = {
99 var default_humanize_map = {
100 'shift':'Shift',
100 'shift':'Shift',
101 'alt':'Alt',
101 'alt':'Alt',
102 'up':'Up',
102 'up':'Up',
103 'down':'Down',
103 'down':'Down',
104 'left':'Left',
104 'left':'Left',
105 'right':'Right',
105 'right':'Right',
106 'tab':'Tab',
106 'tab':'Tab',
107 'capslock':'Caps Lock',
107 'capslock':'Caps Lock',
108 'esc':'Esc',
108 'esc':'Esc',
109 'ctrl':'Ctrl',
109 'ctrl':'Ctrl',
110 'enter':'Enter',
110 'enter':'Enter',
111 'pageup':'Page Up',
111 'pageup':'Page Up',
112 'pagedown':'Page Down',
112 'pagedown':'Page Down',
113 'home':'Home',
113 'home':'Home',
114 'end':'End',
114 'end':'End',
115 'space':'Space',
115 'space':'Space',
116 'backspace':'Backspace',
116 'backspace':'Backspace',
117 };
117 };
118
118
119 var humanize_map;
119 var humanize_map;
120
120
121 if (platform === 'MacOS'){
121 if (platform === 'MacOS'){
122 humanize_map = mac_humanize_map;
122 humanize_map = mac_humanize_map;
123 } else {
123 } else {
124 humanize_map = default_humanize_map;
124 humanize_map = default_humanize_map;
125 }
125 }
126
126
127 function humanize_key(key){
127 function humanize_key(key){
128 if (key.length === 1){
128 if (key.length === 1){
129 key = key.toUpperCase();
129 key = key.toUpperCase();
130 }
130 }
131 return humanize_map[key.toLowerCase()]||key;
131 return humanize_map[key.toLowerCase()]||key;
132 }
132 }
133
133
134 function humanize_sequence(sequence){
134 function humanize_sequence(sequence){
135 var joinchar = ',';
135 var joinchar = ',';
136 var hum = _.map(sequence.replace(/meta/g, 'cmd').split(','), humanize_shortcut).join(joinchar);
136 var hum = _.map(sequence.replace(/meta/g, 'cmd').split(','), humanize_shortcut).join(joinchar);
137 return hum;
137 return hum;
138 }
138 }
139
139
140 function humanize_shortcut(shortcut){
140 function humanize_shortcut(shortcut){
141 var joinchar = '-';
141 var joinchar = '-';
142 if (platform === 'MacOS'){
142 if (platform === 'MacOS'){
143 joinchar = '';
143 joinchar = '';
144 }
144 }
145 var sh = _.map(shortcut.split('-'), humanize_key ).join(joinchar);
145 var sh = _.map(shortcut.split('-'), humanize_key ).join(joinchar);
146 return sh;
146 return sh;
147 }
147 }
148
148
149
149
150 QuickHelp.prototype.show_keyboard_shortcuts = function () {
150 QuickHelp.prototype.show_keyboard_shortcuts = function () {
151 /**
151 /**
152 * toggles display of keyboard shortcut dialog
152 * toggles display of keyboard shortcut dialog
153 */
153 */
154 var that = this;
154 var that = this;
155 if ( this.force_rebuild ) {
155 if ( this.force_rebuild ) {
156 this.shortcut_dialog.remove();
156 this.shortcut_dialog.remove();
157 delete(this.shortcut_dialog);
157 delete(this.shortcut_dialog);
158 this.force_rebuild = false;
158 this.force_rebuild = false;
159 }
159 }
160 if ( this.shortcut_dialog ){
160 if ( this.shortcut_dialog ){
161 // if dialog is already shown, close it
161 // if dialog is already shown, close it
162 $(this.shortcut_dialog).modal("toggle");
162 $(this.shortcut_dialog).modal("toggle");
163 return;
163 return;
164 }
164 }
165 var command_shortcuts = this.keyboard_manager.command_shortcuts.help();
165 var command_shortcuts = this.keyboard_manager.command_shortcuts.help();
166 var edit_shortcuts = this.keyboard_manager.edit_shortcuts.help();
166 var edit_shortcuts = this.keyboard_manager.edit_shortcuts.help();
167 var help, shortcut;
167 var help, shortcut;
168 var i, half, n;
168 var i, half, n;
169 var element = $('<div/>');
169 var element = $('<div/>');
170
170
171 // The documentation
171 // The documentation
172 var doc = $('<div/>').addClass('alert alert-warning');
172 var doc = $('<div/>').addClass('alert alert-warning');
173 doc.append(
173 doc.append(
174 $('<button/>').addClass('close').attr('data-dismiss','alert').html('&times;')
174 $('<button/>').addClass('close').attr('data-dismiss','alert').html('&times;')
175 ).append(
175 ).append(
176 'The IPython Notebook has two different keyboard input modes. <b>Edit mode</b> '+
176 'The IPython Notebook has two different keyboard input modes. <b>Edit mode</b> '+
177 'allows you to type code/text into a cell and is indicated by a green cell '+
177 'allows you to type code/text into a cell and is indicated by a green cell '+
178 'border. <b>Command mode</b> binds the keyboard to notebook level actions '+
178 'border. <b>Command mode</b> binds the keyboard to notebook level actions '+
179 'and is indicated by a grey cell border.'
179 'and is indicated by a grey cell border.'
180 );
180 );
181 element.append(doc);
181 element.append(doc);
182
182
183 // Command mode
183 // Command mode
184 var cmd_div = this.build_command_help();
184 var cmd_div = this.build_command_help();
185 element.append(cmd_div);
185 element.append(cmd_div);
186
186
187 // Edit mode
187 // Edit mode
188 var edit_div = this.build_edit_help(cm_shortcuts);
188 var edit_div = this.build_edit_help(cm_shortcuts);
189 element.append(edit_div);
189 element.append(edit_div);
190
190
191 this.shortcut_dialog = dialog.modal({
191 this.shortcut_dialog = dialog.modal({
192 title : "Keyboard shortcuts",
192 title : "Keyboard shortcuts",
193 body : element,
193 body : element,
194 destroy : false,
194 destroy : false,
195 buttons : {
195 buttons : {
196 Close : {}
196 Close : {}
197 },
197 },
198 notebook: this.notebook,
198 notebook: this.notebook,
199 keyboard_manager: this.keyboard_manager,
199 keyboard_manager: this.keyboard_manager,
200 });
200 });
201 this.shortcut_dialog.addClass("modal_stretch");
201 this.shortcut_dialog.addClass("modal_stretch");
202
202
203 this.events.on('rebuild.QuickHelp', function() { that.force_rebuild = true;});
203 this.events.on('rebuild.QuickHelp', function() { that.force_rebuild = true;});
204 };
204 };
205
205
206 QuickHelp.prototype.build_command_help = function () {
206 QuickHelp.prototype.build_command_help = function () {
207 var command_shortcuts = this.keyboard_manager.command_shortcuts.help();
207 var command_shortcuts = this.keyboard_manager.command_shortcuts.help();
208 return build_div('<h4>Command Mode (press <code>Esc</code> to enable)</h4>', command_shortcuts);
208 return build_div('<h4>Command Mode (press <code>Esc</code> to enable)</h4>', command_shortcuts);
209 };
209 };
210
210
211 var special_case = { pageup: "PageUp", pagedown: "Page Down", 'minus': '-' };
211 var special_case = { pageup: "PageUp", pagedown: "Page Down", 'minus': '-' };
212 var prettify = function (s) {
212 var prettify = function (s) {
213 s = s.replace(/-$/, 'minus'); // catch shortcuts using '-' key
213 s = s.replace(/-$/, 'minus'); // catch shortcuts using '-' key
214 var keys = s.split('-');
214 var keys = s.split('-');
215 var k, i;
215 var k, i;
216 for (i=0; i < keys.length; i++) {
216 for (i=0; i < keys.length; i++) {
217 k = keys[i];
217 k = keys[i];
218 if ( k.length == 1 ) {
218 if ( k.length == 1 ) {
219 keys[i] = "<code><strong>" + k + "</strong></code>";
219 keys[i] = "<code><strong>" + k + "</strong></code>";
220 continue; // leave individual keys lower-cased
220 continue; // leave individual keys lower-cased
221 }
221 }
222 if (k.indexOf(',') === -1){
222 if (k.indexOf(',') === -1){
223 keys[i] = ( special_case[k] ? special_case[k] : k.charAt(0).toUpperCase() + k.slice(1) );
223 keys[i] = ( special_case[k] ? special_case[k] : k.charAt(0).toUpperCase() + k.slice(1) );
224 }
224 }
225 keys[i] = "<code><strong>" + keys[i] + "</strong></code>";
225 keys[i] = "<code><strong>" + keys[i] + "</strong></code>";
226 }
226 }
227 return keys.join('-');
227 return keys.join('-');
228
228
229
229
230 };
230 };
231
231
232 QuickHelp.prototype.build_edit_help = function (cm_shortcuts) {
232 QuickHelp.prototype.build_edit_help = function (cm_shortcuts) {
233 var edit_shortcuts = this.keyboard_manager.edit_shortcuts.help();
233 var edit_shortcuts = this.keyboard_manager.edit_shortcuts.help();
234 jQuery.merge(cm_shortcuts, edit_shortcuts);
234 jQuery.merge(cm_shortcuts, edit_shortcuts);
235 return build_div('<h4>Edit Mode (press <code>Enter</code> to enable)</h4>', cm_shortcuts);
235 return build_div('<h4>Edit Mode (press <code>Enter</code> to enable)</h4>', cm_shortcuts);
236 };
236 };
237
237
238 var build_one = function (s) {
238 var build_one = function (s) {
239 var help = s.help;
239 var help = s.help;
240 var shortcut = '';
240 var shortcut = '';
241 if(s.shortcut){
241 if(s.shortcut){
242 shortcut = prettify(humanize_sequence(s.shortcut));
242 shortcut = prettify(humanize_sequence(s.shortcut));
243 }
243 }
244 return $('<div>').addClass('quickhelp').
244 return $('<div>').addClass('quickhelp').
245 append($('<span/>').addClass('shortcut_key').append($(shortcut))).
245 append($('<span/>').addClass('shortcut_key').append($(shortcut))).
246 append($('<span/>').addClass('shortcut_descr').text(' : ' + help));
246 append($('<span/>').addClass('shortcut_descr').text(' : ' + help));
247
247
248 };
248 };
249
249
250 var build_div = function (title, shortcuts) {
250 var build_div = function (title, shortcuts) {
251 var i, half, n;
251 var i, half, n;
252 var div = $('<div/>').append($(title));
252 var div = $('<div/>').append($(title));
253 var sub_div = $('<div/>').addClass('hbox');
253 var sub_div = $('<div/>').addClass('container-fluid');
254 var col1 = $('<div/>').addClass('box-flex1');
254 var col1 = $('<div/>').addClass('col-md-6');
255 var col2 = $('<div/>').addClass('box-flex1');
255 var col2 = $('<div/>').addClass('col-md-6');
256 n = shortcuts.length;
256 n = shortcuts.length;
257 half = ~~(n/2); // Truncate :)
257 half = ~~(n/2); // Truncate :)
258 for (i=0; i<half; i++) { col1.append( build_one(shortcuts[i]) ); }
258 for (i=0; i<half; i++) { col1.append( build_one(shortcuts[i]) ); }
259 for (i=half; i<n; i++) { col2.append( build_one(shortcuts[i]) ); }
259 for (i=half; i<n; i++) { col2.append( build_one(shortcuts[i]) ); }
260 sub_div.append(col1).append(col2);
260 sub_div.append(col1).append(col2);
261 div.append(sub_div);
261 div.append(sub_div);
262 return div;
262 return div;
263 };
263 };
264
264
265 // Backwards compatability.
265 // Backwards compatability.
266 IPython.QuickHelp = QuickHelp;
266 IPython.QuickHelp = QuickHelp;
267
267
268 return {'QuickHelp': QuickHelp};
268 return {'QuickHelp': QuickHelp};
269 });
269 });
General Comments 0
You need to be logged in to leave comments. Login now