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