Show More
@@ -1,156 +1,164 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 | //============================================================================ |
|
4 | //============================================================================ | |
5 | // QuickHelp button |
|
5 | // QuickHelp button | |
6 | //============================================================================ |
|
6 | //============================================================================ | |
7 |
|
7 | |||
8 | var IPython = (function (IPython) { |
|
8 | var IPython = (function (IPython) { | |
9 | "use strict"; |
|
9 | "use strict"; | |
10 |
|
10 | |||
|
11 | var platform = IPython.utils.platform; | |||
|
12 | ||||
11 | var QuickHelp = function (selector) { |
|
13 | var QuickHelp = function (selector) { | |
12 | }; |
|
14 | }; | |
|
15 | var cmd_ctrl = 'Ctrl'; | |||
|
16 | ||||
|
17 | if (platform === 'MacOS') { | |||
|
18 | cmd_ctrl = 'Cmd'; | |||
|
19 | } | |||
13 |
|
20 | |||
14 | var cm_shortcuts = [ |
|
21 | var cm_shortcuts = [ | |
15 | { shortcut:"Insert", help:"toggle overwrite" }, |
|
22 | { shortcut:"Insert", help:"toggle overwrite" }, | |
16 | { shortcut:"Tab", help:"code completion" }, |
|
23 | { shortcut:"Tab", help:"code completion" }, | |
17 | { shortcut:"Shift-Tab", help:"help introspection" }, |
|
24 | { shortcut:"Shift-Tab", help:"help introspection" }, | |
18 |
{ shortcut: |
|
25 | { shortcut: cmd_ctrl + "-]", help:"indent" }, | |
19 |
{ shortcut: |
|
26 | { shortcut: cmd_ctrl + "-[", help:"dedent" }, | |
20 |
{ shortcut: |
|
27 | { shortcut: cmd_ctrl + "-A", help:"select all" }, | |
21 |
{ shortcut: |
|
28 | { shortcut: cmd_ctrl + "-D", help:"delete line" }, | |
22 |
{ shortcut: |
|
29 | { shortcut: cmd_ctrl + "-Z", help:"undo" }, | |
23 |
{ shortcut: |
|
30 | { shortcut: cmd_ctrl + "-Shift-Z", help:"redo" }, | |
24 |
{ shortcut: |
|
31 | { shortcut: cmd_ctrl + "-Y", help:"redo" }, | |
25 |
{ shortcut: |
|
32 | { shortcut: cmd_ctrl + "-Up", help:"go to cell start" }, | |
26 |
{ shortcut: |
|
33 | { shortcut: cmd_ctrl + "-End", help:"go to cell start" }, | |
27 | { shortcut:"PageUp", help:"go to cell start" }, |
|
34 | { shortcut:"PageUp", help:"go to cell start" }, | |
28 | { shortcut:"---", help:"go to cell end" }, |
|
35 | { shortcut:"---", help:"go to cell end" }, | |
29 |
{ shortcut: |
|
36 | { shortcut: cmd_ctrl + "-Down", help:"go to cell end" }, | |
30 | { shortcut:"PageDown", help:"go to cell end" }, |
|
37 | { shortcut:"PageDown", help:"go to cell end" }, | |
31 | { shortcut:"Alt-Left", help:"go one word left" }, |
|
38 | { shortcut:"Alt-Left", help:"go one word left" }, | |
32 | { shortcut:"Alt-Right", help:"go one word right" }, |
|
39 | { shortcut:"Alt-Right", help:"go one word right" }, | |
33 |
{ shortcut: |
|
40 | { shortcut: cmd_ctrl + "-Left", help:"go to line start" }, | |
34 | { shortcut:"Home", help:"go to line start" }, |
|
41 | { shortcut:"Home", help:"go to line start" }, | |
35 |
{ shortcut: |
|
42 | { shortcut: cmd_ctrl + "-Right", help:"go to line end" }, | |
36 | { shortcut:"End", help:"go to line end" }, |
|
43 | { shortcut:"End", help:"go to line end" }, | |
37 | { shortcut:"Alt-Backspace", help:"del word before" }, |
|
44 | { shortcut:"Alt-Backspace", help:"del word before" }, | |
38 |
|
45 | |||
|
46 | ||||
39 | ] |
|
47 | ] | |
40 |
|
48 | |||
41 |
|
49 | |||
42 | QuickHelp.prototype.show_keyboard_shortcuts = function () { |
|
50 | QuickHelp.prototype.show_keyboard_shortcuts = function () { | |
43 | // toggles display of keyboard shortcut dialog |
|
51 | // toggles display of keyboard shortcut dialog | |
44 | var that = this; |
|
52 | var that = this; | |
45 | if ( this.force_rebuild ) { |
|
53 | if ( this.force_rebuild ) { | |
46 | this.shortcut_dialog.remove(); |
|
54 | this.shortcut_dialog.remove(); | |
47 | delete(this.shortcut_dialog); |
|
55 | delete(this.shortcut_dialog); | |
48 | this.force_rebuild = false; |
|
56 | this.force_rebuild = false; | |
49 | } |
|
57 | } | |
50 | if ( this.shortcut_dialog ){ |
|
58 | if ( this.shortcut_dialog ){ | |
51 | // if dialog is already shown, close it |
|
59 | // if dialog is already shown, close it | |
52 | $(this.shortcut_dialog).modal("toggle"); |
|
60 | $(this.shortcut_dialog).modal("toggle"); | |
53 | return; |
|
61 | return; | |
54 | } |
|
62 | } | |
55 | var command_shortcuts = IPython.keyboard_manager.command_shortcuts.help(); |
|
63 | var command_shortcuts = IPython.keyboard_manager.command_shortcuts.help(); | |
56 | var edit_shortcuts = IPython.keyboard_manager.edit_shortcuts.help(); |
|
64 | var edit_shortcuts = IPython.keyboard_manager.edit_shortcuts.help(); | |
57 | var help, shortcut; |
|
65 | var help, shortcut; | |
58 | var i, half, n; |
|
66 | var i, half, n; | |
59 | var element = $('<div/>'); |
|
67 | var element = $('<div/>'); | |
60 |
|
68 | |||
61 | // The documentation |
|
69 | // The documentation | |
62 | var doc = $('<div/>').addClass('alert'); |
|
70 | var doc = $('<div/>').addClass('alert'); | |
63 | doc.append( |
|
71 | doc.append( | |
64 | $('<button/>').addClass('close').attr('data-dismiss','alert').html('×') |
|
72 | $('<button/>').addClass('close').attr('data-dismiss','alert').html('×') | |
65 | ).append( |
|
73 | ).append( | |
66 | 'The IPython Notebook has two different keyboard input modes. <b>Edit mode</b> '+ |
|
74 | 'The IPython Notebook has two different keyboard input modes. <b>Edit mode</b> '+ | |
67 | 'allows you to type code/text into a cell and is indicated by a green cell '+ |
|
75 | 'allows you to type code/text into a cell and is indicated by a green cell '+ | |
68 | 'border. <b>Command mode</b> binds the keyboard to notebook level actions '+ |
|
76 | 'border. <b>Command mode</b> binds the keyboard to notebook level actions '+ | |
69 | 'and is indicated by a grey cell border.' |
|
77 | 'and is indicated by a grey cell border.' | |
70 | ); |
|
78 | ); | |
71 | element.append(doc); |
|
79 | element.append(doc); | |
72 |
|
80 | |||
73 | // Command mode |
|
81 | // Command mode | |
74 | var cmd_div = this.build_command_help(); |
|
82 | var cmd_div = this.build_command_help(); | |
75 | element.append(cmd_div); |
|
83 | element.append(cmd_div); | |
76 |
|
84 | |||
77 | // Edit mode |
|
85 | // Edit mode | |
78 | var edit_div = this.build_edit_help(); |
|
86 | var edit_div = this.build_edit_help(); | |
79 | element.append(edit_div); |
|
87 | element.append(edit_div); | |
80 |
|
88 | |||
81 | // CodeMirror shortcuts |
|
89 | // CodeMirror shortcuts | |
82 | var cm_div = build_div('', cm_shortcuts); |
|
90 | var cm_div = build_div('', cm_shortcuts); | |
83 | element.append(cm_div); |
|
91 | element.append(cm_div); | |
84 |
|
92 | |||
85 | this.shortcut_dialog = IPython.dialog.modal({ |
|
93 | this.shortcut_dialog = IPython.dialog.modal({ | |
86 | title : "Keyboard shortcuts", |
|
94 | title : "Keyboard shortcuts", | |
87 | body : element, |
|
95 | body : element, | |
88 | destroy : false, |
|
96 | destroy : false, | |
89 | buttons : { |
|
97 | buttons : { | |
90 | Close : {} |
|
98 | Close : {} | |
91 | } |
|
99 | } | |
92 | }); |
|
100 | }); | |
93 |
|
101 | |||
94 | $([IPython.events]).on('rebuild.QuickHelp', function() { that.force_rebuild = true;}); |
|
102 | $([IPython.events]).on('rebuild.QuickHelp', function() { that.force_rebuild = true;}); | |
95 | }; |
|
103 | }; | |
96 |
|
104 | |||
97 | QuickHelp.prototype.build_command_help = function () { |
|
105 | QuickHelp.prototype.build_command_help = function () { | |
98 | var command_shortcuts = IPython.keyboard_manager.command_shortcuts.help(); |
|
106 | var command_shortcuts = IPython.keyboard_manager.command_shortcuts.help(); | |
99 | return build_div('<h4>Command Mode (press <code>Esc</code> to enable)</h4>', command_shortcuts); |
|
107 | return build_div('<h4>Command Mode (press <code>Esc</code> to enable)</h4>', command_shortcuts); | |
100 | }; |
|
108 | }; | |
101 |
|
109 | |||
102 | var special_case = { pageup: "PageUp", pagedown: "Page Down", 'minus': '-' }; |
|
110 | var special_case = { pageup: "PageUp", pagedown: "Page Down", 'minus': '-' }; | |
103 | var prettify = function (s) { |
|
111 | var prettify = function (s) { | |
104 | s = s.replace(/-$/, 'minus'); // catch shortcuts using '-' key |
|
112 | s = s.replace(/-$/, 'minus'); // catch shortcuts using '-' key | |
105 | var keys = s.split('-'); |
|
113 | var keys = s.split('-'); | |
106 | var k, i; |
|
114 | var k, i; | |
107 | for (i in keys) { |
|
115 | for (i in keys) { | |
108 | k = keys[i]; |
|
116 | k = keys[i]; | |
109 | if ( k.length == 1 ) { |
|
117 | if ( k.length == 1 ) { | |
110 | keys[i] = "<code><strong>" + k + "</strong></code>"; |
|
118 | keys[i] = "<code><strong>" + k + "</strong></code>"; | |
111 | continue; // leave individual keys lower-cased |
|
119 | continue; // leave individual keys lower-cased | |
112 | } |
|
120 | } | |
113 | keys[i] = ( special_case[k] ? special_case[k] : k.charAt(0).toUpperCase() + k.slice(1) ); |
|
121 | keys[i] = ( special_case[k] ? special_case[k] : k.charAt(0).toUpperCase() + k.slice(1) ); | |
114 | keys[i] = "<code><strong>" + keys[i] + "</strong></code>"; |
|
122 | keys[i] = "<code><strong>" + keys[i] + "</strong></code>"; | |
115 | } |
|
123 | } | |
116 | return keys.join('-'); |
|
124 | return keys.join('-'); | |
117 |
|
125 | |||
118 |
|
126 | |||
119 | }; |
|
127 | }; | |
120 |
|
128 | |||
121 | QuickHelp.prototype.build_edit_help = function () { |
|
129 | QuickHelp.prototype.build_edit_help = function () { | |
122 | var edit_shortcuts = IPython.keyboard_manager.edit_shortcuts.help(); |
|
130 | var edit_shortcuts = IPython.keyboard_manager.edit_shortcuts.help(); | |
123 | // Edit mode |
|
131 | // Edit mode | |
124 | return build_div('<h4>Edit Mode (press <code>Enter</code> to enable)</h4>', edit_shortcuts); |
|
132 | return build_div('<h4>Edit Mode (press <code>Enter</code> to enable)</h4>', edit_shortcuts); | |
125 | }; |
|
133 | }; | |
126 |
|
134 | |||
127 | var build_one = function (s) { |
|
135 | var build_one = function (s) { | |
128 | var help = s.help; |
|
136 | var help = s.help; | |
129 | var shortcut = prettify(s.shortcut); |
|
137 | var shortcut = prettify(s.shortcut); | |
130 | return $('<div>').addClass('quickhelp'). |
|
138 | return $('<div>').addClass('quickhelp'). | |
131 | append($('<span/>').addClass('shortcut_key').append($(shortcut))). |
|
139 | append($('<span/>').addClass('shortcut_key').append($(shortcut))). | |
132 | append($('<span/>').addClass('shortcut_descr').text(' : ' + help)); |
|
140 | append($('<span/>').addClass('shortcut_descr').text(' : ' + help)); | |
133 |
|
141 | |||
134 | }; |
|
142 | }; | |
135 |
|
143 | |||
136 | var build_div = function (title, shortcuts) { |
|
144 | var build_div = function (title, shortcuts) { | |
137 | var i, half, n; |
|
145 | var i, half, n; | |
138 | var div = $('<div/>').append($(title)); |
|
146 | var div = $('<div/>').append($(title)); | |
139 | var sub_div = $('<div/>').addClass('hbox'); |
|
147 | var sub_div = $('<div/>').addClass('hbox'); | |
140 | var col1 = $('<div/>').addClass('box-flex1'); |
|
148 | var col1 = $('<div/>').addClass('box-flex1'); | |
141 | var col2 = $('<div/>').addClass('box-flex1'); |
|
149 | var col2 = $('<div/>').addClass('box-flex1'); | |
142 | n = shortcuts.length; |
|
150 | n = shortcuts.length; | |
143 | half = ~~(n/2); // Truncate :) |
|
151 | half = ~~(n/2); // Truncate :) | |
144 | for (i=0; i<half; i++) { col1.append( build_one(shortcuts[i]) ); } |
|
152 | for (i=0; i<half; i++) { col1.append( build_one(shortcuts[i]) ); } | |
145 | for (i=half; i<n; i++) { col2.append( build_one(shortcuts[i]) ); } |
|
153 | for (i=half; i<n; i++) { col2.append( build_one(shortcuts[i]) ); } | |
146 | sub_div.append(col1).append(col2); |
|
154 | sub_div.append(col1).append(col2); | |
147 | div.append(sub_div); |
|
155 | div.append(sub_div); | |
148 | return div; |
|
156 | return div; | |
149 | }; |
|
157 | }; | |
150 |
|
158 | |||
151 | // Set module variables |
|
159 | // Set module variables | |
152 | IPython.QuickHelp = QuickHelp; |
|
160 | IPython.QuickHelp = QuickHelp; | |
153 |
|
161 | |||
154 | return IPython; |
|
162 | return IPython; | |
155 |
|
163 | |||
156 | }(IPython)); |
|
164 | }(IPython)); |
General Comments 0
You need to be logged in to leave comments.
Login now