Show More
@@ -1,146 +1,153 | |||||
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 | delete(this.shortcut_dialog); |
|
22 | delete(this.shortcut_dialog); | |
23 | this.force_rebuild = false; |
|
23 | this.force_rebuild = false; | |
24 | } |
|
24 | } | |
25 | if ( this.shortcut_dialog ){ |
|
25 | if ( this.shortcut_dialog ){ | |
26 | // if dialog is already shown, close it |
|
26 | // if dialog is already shown, close it | |
27 | $(this.shortcut_dialog).modal("toggle"); |
|
27 | $(this.shortcut_dialog).modal("toggle"); | |
28 | return; |
|
28 | return; | |
29 | } |
|
29 | } | |
30 | var command_shortcuts = IPython.keyboard_manager.command_shortcuts.help(); |
|
30 | var command_shortcuts = IPython.keyboard_manager.command_shortcuts.help(); | |
31 | var edit_shortcuts = IPython.keyboard_manager.edit_shortcuts.help(); |
|
31 | var edit_shortcuts = IPython.keyboard_manager.edit_shortcuts.help(); | |
32 | var help, shortcut; |
|
32 | var help, shortcut; | |
33 | var i, half, n; |
|
33 | var i, half, n; | |
34 | var element = $('<div/>'); |
|
34 | var element = $('<div/>'); | |
35 |
|
35 | |||
36 | // The documentation |
|
36 | // The documentation | |
37 | var doc = $('<div/>').addClass('alert'); |
|
37 | var doc = $('<div/>').addClass('alert'); | |
38 | doc.append( |
|
38 | doc.append( | |
39 | $('<button/>').addClass('close').attr('data-dismiss','alert').html('×') |
|
39 | $('<button/>').addClass('close').attr('data-dismiss','alert').html('×') | |
40 | ).append( |
|
40 | ).append( | |
41 | 'The IPython Notebook has two different keyboard input modes. <b>Edit mode</b> '+ |
|
41 | 'The IPython Notebook has two different keyboard input modes. <b>Edit mode</b> '+ | |
42 | 'allows you to type code/text into a cell and is indicated by a green cell '+ |
|
42 | 'allows you to type code/text into a cell and is indicated by a green cell '+ | |
43 | 'border. <b>Command mode</b> binds the keyboard to notebook level actions '+ |
|
43 | 'border. <b>Command mode</b> binds the keyboard to notebook level actions '+ | |
44 | 'and is indicated by a grey cell border.' |
|
44 | 'and is indicated by a grey cell border.' | |
45 | ) |
|
45 | ) | |
46 | element.append(doc); |
|
46 | element.append(doc); | |
47 |
|
47 | |||
48 | // Command mode |
|
48 | // Command mode | |
49 | var cmd_div = this.build_command_help(); |
|
49 | var cmd_div = this.build_command_help(); | |
50 | element.append(cmd_div); |
|
50 | element.append(cmd_div); | |
51 |
|
51 | |||
52 | // Edit mode |
|
52 | // Edit mode | |
53 | var edit_div = this.build_edit_help(); |
|
53 | var edit_div = this.build_edit_help(); | |
54 | element.append(edit_div); |
|
54 | element.append(edit_div); | |
55 |
|
55 | |||
56 | this.shortcut_dialog = IPython.dialog.modal({ |
|
56 | this.shortcut_dialog = IPython.dialog.modal({ | |
57 | title : "Keyboard shortcuts", |
|
57 | title : "Keyboard shortcuts", | |
58 | body : element, |
|
58 | body : element, | |
59 | destroy : false, |
|
59 | destroy : false, | |
60 | buttons : { |
|
60 | buttons : { | |
61 | Close : {} |
|
61 | Close : {} | |
62 | } |
|
62 | } | |
63 | }); |
|
63 | }); | |
64 |
|
64 | |||
65 | $([IPython.events]).on('rebuild.QuickHelp', function() { that.force_rebuild = true;}); |
|
65 | $([IPython.events]).on('rebuild.QuickHelp', function() { that.force_rebuild = true;}); | |
66 | }; |
|
66 | }; | |
67 |
|
67 | |||
68 | QuickHelp.prototype.build_command_help = function () { |
|
68 | QuickHelp.prototype.build_command_help = function () { | |
69 | var command_shortcuts = IPython.keyboard_manager.command_shortcuts.help(); |
|
69 | var command_shortcuts = IPython.keyboard_manager.command_shortcuts.help(); | |
70 | var help, shortcut; |
|
70 | var help, shortcut; | |
71 | var i, half, n; |
|
71 | var i, half, n; | |
72 |
|
72 | |||
73 | // Command mode |
|
73 | // Command mode | |
74 | var cmd_div = $('<div/>').append($('<h4>Command Mode (press <code>esc</code> to enable)</h4>')); |
|
74 | var cmd_div = $('<div/>').append($('<h4>Command Mode (press <code>esc</code> to enable)</h4>')); | |
75 | var cmd_sub_div = $('<div/>').addClass('hbox'); |
|
75 | var cmd_sub_div = $('<div/>').addClass('hbox'); | |
76 | var cmd_col1 = $('<div/>').addClass('box-flex0'); |
|
76 | var cmd_col1 = $('<div/>').addClass('box-flex0'); | |
77 | var cmd_col2 = $('<div/>').addClass('box-flex0'); |
|
77 | var cmd_col2 = $('<div/>').addClass('box-flex0'); | |
78 | n = command_shortcuts.length; |
|
78 | n = command_shortcuts.length; | |
79 | half = ~~(n/2); // Truncate :) |
|
79 | half = ~~(n/2); // Truncate :) | |
80 | for (i=0; i<half; i++) { |
|
80 | for (i=0; i<half; i++) { | |
81 | help = command_shortcuts[i]['help']; |
|
81 | help = command_shortcuts[i]['help']; | |
82 |
shortcut = |
|
82 | shortcut = prettify(command_shortcuts[i]['shortcut']); | |
83 | cmd_col1.append($('<div>').addClass('quickhelp'). |
|
83 | cmd_col1.append($('<div>').addClass('quickhelp'). | |
84 | append($('<span/>').addClass('shortcut_key').text(shortcut)). |
|
84 | append($('<span/>').addClass('shortcut_key').text(shortcut)). | |
85 | append($('<span/>').addClass('shortcut_descr').text(' : ' + help)) |
|
85 | append($('<span/>').addClass('shortcut_descr').text(' : ' + help)) | |
86 | ); |
|
86 | ); | |
87 | }; |
|
87 | }; | |
88 | for (i=half; i<n; i++) { |
|
88 | for (i=half; i<n; i++) { | |
89 | help = command_shortcuts[i]['help']; |
|
89 | help = command_shortcuts[i]['help']; | |
90 |
shortcut = |
|
90 | shortcut = prettify(command_shortcuts[i]['shortcut']); | |
91 | cmd_col2.append($('<div>').addClass('quickhelp'). |
|
91 | cmd_col2.append($('<div>').addClass('quickhelp'). | |
92 | append($('<span/>').addClass('shortcut_key').text(shortcut)). |
|
92 | append($('<span/>').addClass('shortcut_key').text(shortcut)). | |
93 | append($('<span/>').addClass('shortcut_descr').text(' : ' + help)) |
|
93 | append($('<span/>').addClass('shortcut_descr').text(' : ' + help)) | |
94 | ); |
|
94 | ); | |
95 | }; |
|
95 | }; | |
96 | cmd_sub_div.append(cmd_col1).append(cmd_col2); |
|
96 | cmd_sub_div.append(cmd_col1).append(cmd_col2); | |
97 | cmd_div.append(cmd_sub_div); |
|
97 | cmd_div.append(cmd_sub_div); | |
98 | return cmd_div; |
|
98 | return cmd_div; | |
99 | } |
|
99 | } | |
100 |
|
100 | |||
101 | var special_case = { pageup: "PageUp", pagedown: "Page Down" }; |
|
101 | var special_case = { pageup: "PageUp", pagedown: "Page Down" }; | |
102 |
var |
|
102 | var prettify = function (s) { | |
103 | return ( special_case[s] ? special_case[s] : s.charAt(0).toUpperCase() + s.slice(1) ); |
|
103 | var keys = s.split('+'); | |
|
104 | var k, i; | |||
|
105 | for (i in keys) { | |||
|
106 | k = keys[i]; | |||
|
107 | if ( k.length == 1 ) continue; // leave individual keys lower-cased | |||
|
108 | keys[i] = ( special_case[k] ? special_case[k] : k.charAt(0).toUpperCase() + k.slice(1) ); | |||
|
109 | } | |||
|
110 | return keys.join('-'); | |||
104 |
|
111 | |||
105 |
|
112 | |||
106 | }; |
|
113 | }; | |
107 |
|
114 | |||
108 | QuickHelp.prototype.build_edit_help = function () { |
|
115 | QuickHelp.prototype.build_edit_help = function () { | |
109 | var edit_shortcuts = IPython.keyboard_manager.edit_shortcuts.help(); |
|
116 | var edit_shortcuts = IPython.keyboard_manager.edit_shortcuts.help(); | |
110 | var help, shortcut; |
|
117 | var help, shortcut; | |
111 | var i, half, n; |
|
118 | var i, half, n; | |
112 |
|
119 | |||
113 | // Edit mode |
|
120 | // Edit mode | |
114 | var edit_div = $('<div/>').append($('<h4>Edit Mode (press <code>enter</code> to enable)</h4>')); |
|
121 | var edit_div = $('<div/>').append($('<h4>Edit Mode (press <code>enter</code> to enable)</h4>')); | |
115 | var edit_sub_div = $('<div/>').addClass('hbox'); |
|
122 | var edit_sub_div = $('<div/>').addClass('hbox'); | |
116 | var edit_col1 = $('<div/>').addClass('box-flex0'); |
|
123 | var edit_col1 = $('<div/>').addClass('box-flex0'); | |
117 | var edit_col2 = $('<div/>').addClass('box-flex0'); |
|
124 | var edit_col2 = $('<div/>').addClass('box-flex0'); | |
118 | n = edit_shortcuts.length; |
|
125 | n = edit_shortcuts.length; | |
119 | half = ~~(n/2); // Truncate :) |
|
126 | half = ~~(n/2); // Truncate :) | |
120 | for (i=0; i<half; i++) { |
|
127 | for (i=0; i<half; i++) { | |
121 | help = edit_shortcuts[i]['help']; |
|
128 | help = edit_shortcuts[i]['help']; | |
122 |
shortcut = |
|
129 | shortcut = prettify(edit_shortcuts[i]['shortcut']); | |
123 | edit_col1.append($('<div>').addClass('quickhelp'). |
|
130 | edit_col1.append($('<div>').addClass('quickhelp'). | |
124 | append($('<span/>').addClass('shortcut_key').text(shortcut)). |
|
131 | append($('<span/>').addClass('shortcut_key').text(shortcut)). | |
125 | append($('<span/>').addClass('shortcut_descr').text(' : ' + help)) |
|
132 | append($('<span/>').addClass('shortcut_descr').text(' : ' + help)) | |
126 | ); |
|
133 | ); | |
127 | }; |
|
134 | }; | |
128 | for (i=half; i<n; i++) { |
|
135 | for (i=half; i<n; i++) { | |
129 | help = edit_shortcuts[i]['help']; |
|
136 | help = edit_shortcuts[i]['help']; | |
130 |
shortcut = |
|
137 | shortcut = prettify(edit_shortcuts[i]['shortcut']); | |
131 | edit_col2.append($('<div>').addClass('quickhelp'). |
|
138 | edit_col2.append($('<div>').addClass('quickhelp'). | |
132 | append($('<span/>').addClass('shortcut_key').text(shortcut)). |
|
139 | append($('<span/>').addClass('shortcut_key').text(shortcut)). | |
133 | append($('<span/>').addClass('shortcut_descr').text(' : ' + help)) |
|
140 | append($('<span/>').addClass('shortcut_descr').text(' : ' + help)) | |
134 | ); |
|
141 | ); | |
135 | }; |
|
142 | }; | |
136 | edit_sub_div.append(edit_col1).append(edit_col2); |
|
143 | edit_sub_div.append(edit_col1).append(edit_col2); | |
137 | edit_div.append(edit_sub_div); |
|
144 | edit_div.append(edit_sub_div); | |
138 | return edit_div; |
|
145 | return edit_div; | |
139 | } |
|
146 | } | |
140 |
|
147 | |||
141 | // Set module variables |
|
148 | // Set module variables | |
142 | IPython.QuickHelp = QuickHelp; |
|
149 | IPython.QuickHelp = QuickHelp; | |
143 |
|
150 | |||
144 | return IPython; |
|
151 | return IPython; | |
145 |
|
152 | |||
146 | }(IPython)); |
|
153 | }(IPython)); |
General Comments 0
You need to be logged in to leave comments.
Login now