Show More
@@ -1,263 +1,270 | |||||
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 | // MenuBar |
|
9 | // MenuBar | |
10 | //============================================================================ |
|
10 | //============================================================================ | |
11 |
|
11 | |||
12 | /** |
|
12 | /** | |
13 | * @module IPython |
|
13 | * @module IPython | |
14 | * @namespace IPython |
|
14 | * @namespace IPython | |
15 | * @submodule MenuBar |
|
15 | * @submodule MenuBar | |
16 | */ |
|
16 | */ | |
17 |
|
17 | |||
18 |
|
18 | |||
19 | var IPython = (function (IPython) { |
|
19 | var IPython = (function (IPython) { | |
20 |
|
20 | |||
21 | /** |
|
21 | /** | |
22 | * A MenuBar Class to generate the menubar of IPython noteboko |
|
22 | * A MenuBar Class to generate the menubar of IPython noteboko | |
23 | * @Class MenuBar |
|
23 | * @Class MenuBar | |
24 | * |
|
24 | * | |
25 | * @constructor |
|
25 | * @constructor | |
26 | * |
|
26 | * | |
27 | * |
|
27 | * | |
28 | * @param selector {string} selector for the menubar element in DOM |
|
28 | * @param selector {string} selector for the menubar element in DOM | |
29 | * @param {object} [options] |
|
29 | * @param {object} [options] | |
30 | * @param [options.baseProjectUrl] {String} String to use for the |
|
30 | * @param [options.baseProjectUrl] {String} String to use for the | |
31 | * Base Project url, default would be to inspect |
|
31 | * Base Project url, default would be to inspect | |
32 | * $('body').data('baseProjectUrl'); |
|
32 | * $('body').data('baseProjectUrl'); | |
33 | * does not support change for now is set through this option |
|
33 | * does not support change for now is set through this option | |
34 | */ |
|
34 | */ | |
35 | var MenuBar = function (selector, options) { |
|
35 | var MenuBar = function (selector, options) { | |
36 | var options = options || {}; |
|
36 | var options = options || {}; | |
37 | if(options.baseProjectUrl!= undefined){ |
|
37 | if(options.baseProjectUrl!= undefined){ | |
38 | this._baseProjectUrl = options.baseProjectUrl; |
|
38 | this._baseProjectUrl = options.baseProjectUrl; | |
39 | } |
|
39 | } | |
40 | this.selector = selector; |
|
40 | this.selector = selector; | |
41 | if (this.selector !== undefined) { |
|
41 | if (this.selector !== undefined) { | |
42 | this.element = $(selector); |
|
42 | this.element = $(selector); | |
43 | this.style(); |
|
43 | this.style(); | |
44 | this.bind_events(); |
|
44 | this.bind_events(); | |
45 | } |
|
45 | } | |
46 | }; |
|
46 | }; | |
47 |
|
47 | |||
48 | MenuBar.prototype.baseProjectUrl = function(){ |
|
48 | MenuBar.prototype.baseProjectUrl = function(){ | |
49 | return this._baseProjectUrl || $('body').data('baseProjectUrl'); |
|
49 | return this._baseProjectUrl || $('body').data('baseProjectUrl'); | |
50 | }; |
|
50 | }; | |
51 |
|
51 | |||
52 |
|
52 | |||
53 | MenuBar.prototype.style = function () { |
|
53 | MenuBar.prototype.style = function () { | |
54 | this.element.addClass('border-box-sizing'); |
|
54 | this.element.addClass('border-box-sizing'); | |
55 | this.element.find("li").click(function (event, ui) { |
|
55 | this.element.find("li").click(function (event, ui) { | |
56 | // The selected cell loses focus when the menu is entered, so we |
|
56 | // The selected cell loses focus when the menu is entered, so we | |
57 | // re-select it upon selection. |
|
57 | // re-select it upon selection. | |
58 | var i = IPython.notebook.get_selected_index(); |
|
58 | var i = IPython.notebook.get_selected_index(); | |
59 | IPython.notebook.select(i); |
|
59 | IPython.notebook.select(i); | |
60 | } |
|
60 | } | |
61 | ); |
|
61 | ); | |
62 | }; |
|
62 | }; | |
63 |
|
63 | |||
64 |
|
64 | |||
65 | MenuBar.prototype.bind_events = function () { |
|
65 | MenuBar.prototype.bind_events = function () { | |
66 | // File |
|
66 | // File | |
67 | var that = this; |
|
67 | var that = this; | |
68 | this.element.find('#new_notebook').click(function () { |
|
68 | this.element.find('#new_notebook').click(function () { | |
69 | window.open(that.baseProjectUrl()+'new'); |
|
69 | window.open(that.baseProjectUrl()+'new'); | |
70 | }); |
|
70 | }); | |
71 | this.element.find('#open_notebook').click(function () { |
|
71 | this.element.find('#open_notebook').click(function () { | |
72 | window.open(that.baseProjectUrl()); |
|
72 | window.open(that.baseProjectUrl()); | |
73 | }); |
|
73 | }); | |
74 | this.element.find('#rename_notebook').click(function () { |
|
74 | this.element.find('#rename_notebook').click(function () { | |
75 | IPython.save_widget.rename_notebook(); |
|
75 | IPython.save_widget.rename_notebook(); | |
76 | }); |
|
76 | }); | |
77 | this.element.find('#copy_notebook').click(function () { |
|
77 | this.element.find('#copy_notebook').click(function () { | |
78 | var notebook_id = IPython.notebook.get_notebook_id(); |
|
78 | var notebook_id = IPython.notebook.get_notebook_id(); | |
79 | var url = that.baseProjectUrl() + notebook_id + '/copy'; |
|
79 | var url = that.baseProjectUrl() + notebook_id + '/copy'; | |
80 | window.open(url,'_blank'); |
|
80 | window.open(url,'_blank'); | |
81 | return false; |
|
81 | return false; | |
82 | }); |
|
82 | }); | |
83 | this.element.find('#save_checkpoint').click(function () { |
|
83 | this.element.find('#save_checkpoint').click(function () { | |
84 | IPython.notebook.save_checkpoint(); |
|
84 | IPython.notebook.save_checkpoint(); | |
85 | }); |
|
85 | }); | |
86 | this.element.find('#restore_checkpoint').click(function () { |
|
86 | this.element.find('#restore_checkpoint').click(function () { | |
87 | }); |
|
87 | }); | |
88 | this.element.find('#download_ipynb').click(function () { |
|
88 | this.element.find('#download_ipynb').click(function () { | |
89 | var notebook_id = IPython.notebook.get_notebook_id(); |
|
89 | var notebook_id = IPython.notebook.get_notebook_id(); | |
90 | var url = that.baseProjectUrl() + 'notebooks/' + |
|
90 | var url = that.baseProjectUrl() + 'notebooks/' + | |
91 | notebook_id + '?format=json'; |
|
91 | notebook_id + '?format=json'; | |
92 | window.location.assign(url); |
|
92 | window.location.assign(url); | |
93 | }); |
|
93 | }); | |
94 | this.element.find('#download_py').click(function () { |
|
94 | this.element.find('#download_py').click(function () { | |
95 | var notebook_id = IPython.notebook.get_notebook_id(); |
|
95 | var notebook_id = IPython.notebook.get_notebook_id(); | |
96 | var url = that.baseProjectUrl() + 'notebooks/' + |
|
96 | var url = that.baseProjectUrl() + 'notebooks/' + | |
97 | notebook_id + '?format=py'; |
|
97 | notebook_id + '?format=py'; | |
98 | window.location.assign(url); |
|
98 | window.location.assign(url); | |
99 | }); |
|
99 | }); | |
100 | this.element.find('#kill_and_exit').click(function () { |
|
100 | this.element.find('#kill_and_exit').click(function () { | |
101 | IPython.notebook.kernel.kill(); |
|
101 | IPython.notebook.kernel.kill(); | |
102 | setTimeout(function(){window.close();}, 200); |
|
102 | setTimeout(function(){window.close();}, 200); | |
103 | }); |
|
103 | }); | |
104 | // Edit |
|
104 | // Edit | |
105 | this.element.find('#cut_cell').click(function () { |
|
105 | this.element.find('#cut_cell').click(function () { | |
106 | IPython.notebook.cut_cell(); |
|
106 | IPython.notebook.cut_cell(); | |
107 | }); |
|
107 | }); | |
108 | this.element.find('#copy_cell').click(function () { |
|
108 | this.element.find('#copy_cell').click(function () { | |
109 | IPython.notebook.copy_cell(); |
|
109 | IPython.notebook.copy_cell(); | |
110 | }); |
|
110 | }); | |
111 | this.element.find('#delete_cell').click(function () { |
|
111 | this.element.find('#delete_cell').click(function () { | |
112 | IPython.notebook.delete_cell(); |
|
112 | IPython.notebook.delete_cell(); | |
113 | }); |
|
113 | }); | |
114 | this.element.find('#undelete_cell').click(function () { |
|
114 | this.element.find('#undelete_cell').click(function () { | |
115 | IPython.notebook.undelete(); |
|
115 | IPython.notebook.undelete(); | |
116 | }); |
|
116 | }); | |
117 | this.element.find('#split_cell').click(function () { |
|
117 | this.element.find('#split_cell').click(function () { | |
118 | IPython.notebook.split_cell(); |
|
118 | IPython.notebook.split_cell(); | |
119 | }); |
|
119 | }); | |
120 | this.element.find('#merge_cell_above').click(function () { |
|
120 | this.element.find('#merge_cell_above').click(function () { | |
121 | IPython.notebook.merge_cell_above(); |
|
121 | IPython.notebook.merge_cell_above(); | |
122 | }); |
|
122 | }); | |
123 | this.element.find('#merge_cell_below').click(function () { |
|
123 | this.element.find('#merge_cell_below').click(function () { | |
124 | IPython.notebook.merge_cell_below(); |
|
124 | IPython.notebook.merge_cell_below(); | |
125 | }); |
|
125 | }); | |
126 | this.element.find('#move_cell_up').click(function () { |
|
126 | this.element.find('#move_cell_up').click(function () { | |
127 | IPython.notebook.move_cell_up(); |
|
127 | IPython.notebook.move_cell_up(); | |
128 | }); |
|
128 | }); | |
129 | this.element.find('#move_cell_down').click(function () { |
|
129 | this.element.find('#move_cell_down').click(function () { | |
130 | IPython.notebook.move_cell_down(); |
|
130 | IPython.notebook.move_cell_down(); | |
131 | }); |
|
131 | }); | |
132 | this.element.find('#select_previous').click(function () { |
|
132 | this.element.find('#select_previous').click(function () { | |
133 | IPython.notebook.select_prev(); |
|
133 | IPython.notebook.select_prev(); | |
134 | }); |
|
134 | }); | |
135 | this.element.find('#select_next').click(function () { |
|
135 | this.element.find('#select_next').click(function () { | |
136 | IPython.notebook.select_next(); |
|
136 | IPython.notebook.select_next(); | |
137 | }); |
|
137 | }); | |
138 | // View |
|
138 | // View | |
139 | this.element.find('#toggle_header').click(function () { |
|
139 | this.element.find('#toggle_header').click(function () { | |
140 | $('div#header').toggle(); |
|
140 | $('div#header').toggle(); | |
141 | IPython.layout_manager.do_resize(); |
|
141 | IPython.layout_manager.do_resize(); | |
142 | }); |
|
142 | }); | |
143 | this.element.find('#toggle_toolbar').click(function () { |
|
143 | this.element.find('#toggle_toolbar').click(function () { | |
144 | $('div#maintoolbar').toggle(); |
|
144 | $('div#maintoolbar').toggle(); | |
145 | IPython.layout_manager.do_resize(); |
|
145 | IPython.layout_manager.do_resize(); | |
146 | }); |
|
146 | }); | |
147 | // Insert |
|
147 | // Insert | |
148 | this.element.find('#insert_cell_above').click(function () { |
|
148 | this.element.find('#insert_cell_above').click(function () { | |
149 | IPython.notebook.insert_cell_above('code'); |
|
149 | IPython.notebook.insert_cell_above('code'); | |
150 | }); |
|
150 | }); | |
151 | this.element.find('#insert_cell_below').click(function () { |
|
151 | this.element.find('#insert_cell_below').click(function () { | |
152 | IPython.notebook.insert_cell_below('code'); |
|
152 | IPython.notebook.insert_cell_below('code'); | |
153 | }); |
|
153 | }); | |
154 | // Cell |
|
154 | // Cell | |
155 | this.element.find('#run_cell').click(function () { |
|
155 | this.element.find('#run_cell').click(function () { | |
156 | IPython.notebook.execute_selected_cell(); |
|
156 | IPython.notebook.execute_selected_cell(); | |
157 | }); |
|
157 | }); | |
158 | this.element.find('#run_cell_in_place').click(function () { |
|
158 | this.element.find('#run_cell_in_place').click(function () { | |
159 | IPython.notebook.execute_selected_cell({terminal:true}); |
|
159 | IPython.notebook.execute_selected_cell({terminal:true}); | |
160 | }); |
|
160 | }); | |
161 | this.element.find('#run_all_cells').click(function () { |
|
161 | this.element.find('#run_all_cells').click(function () { | |
162 | IPython.notebook.execute_all_cells(); |
|
162 | IPython.notebook.execute_all_cells(); | |
163 | }).attr('title', 'Run all cells in the notebook'); |
|
163 | }).attr('title', 'Run all cells in the notebook'); | |
164 | this.element.find('#run_all_cells_above').click(function () { |
|
164 | this.element.find('#run_all_cells_above').click(function () { | |
165 | IPython.notebook.execute_cells_above(); |
|
165 | IPython.notebook.execute_cells_above(); | |
166 | }).attr('title', 'Run all cells above (but not including) this cell'); |
|
166 | }).attr('title', 'Run all cells above (but not including) this cell'); | |
167 | this.element.find('#run_all_cells_below').click(function () { |
|
167 | this.element.find('#run_all_cells_below').click(function () { | |
168 | IPython.notebook.execute_cells_below(); |
|
168 | IPython.notebook.execute_cells_below(); | |
169 | }).attr('title', 'Run this cell and all cells below it'); |
|
169 | }).attr('title', 'Run this cell and all cells below it'); | |
170 | this.element.find('#to_code').click(function () { |
|
170 | this.element.find('#to_code').click(function () { | |
171 | IPython.notebook.to_code(); |
|
171 | IPython.notebook.to_code(); | |
172 | }); |
|
172 | }); | |
173 | this.element.find('#to_markdown').click(function () { |
|
173 | this.element.find('#to_markdown').click(function () { | |
174 | IPython.notebook.to_markdown(); |
|
174 | IPython.notebook.to_markdown(); | |
175 | }); |
|
175 | }); | |
176 | this.element.find('#to_raw').click(function () { |
|
176 | this.element.find('#to_raw').click(function () { | |
177 | IPython.notebook.to_raw(); |
|
177 | IPython.notebook.to_raw(); | |
178 | }); |
|
178 | }); | |
179 | this.element.find('#to_heading1').click(function () { |
|
179 | this.element.find('#to_heading1').click(function () { | |
180 | IPython.notebook.to_heading(undefined, 1); |
|
180 | IPython.notebook.to_heading(undefined, 1); | |
181 | }); |
|
181 | }); | |
182 | this.element.find('#to_heading2').click(function () { |
|
182 | this.element.find('#to_heading2').click(function () { | |
183 | IPython.notebook.to_heading(undefined, 2); |
|
183 | IPython.notebook.to_heading(undefined, 2); | |
184 | }); |
|
184 | }); | |
185 | this.element.find('#to_heading3').click(function () { |
|
185 | this.element.find('#to_heading3').click(function () { | |
186 | IPython.notebook.to_heading(undefined, 3); |
|
186 | IPython.notebook.to_heading(undefined, 3); | |
187 | }); |
|
187 | }); | |
188 | this.element.find('#to_heading4').click(function () { |
|
188 | this.element.find('#to_heading4').click(function () { | |
189 | IPython.notebook.to_heading(undefined, 4); |
|
189 | IPython.notebook.to_heading(undefined, 4); | |
190 | }); |
|
190 | }); | |
191 | this.element.find('#to_heading5').click(function () { |
|
191 | this.element.find('#to_heading5').click(function () { | |
192 | IPython.notebook.to_heading(undefined, 5); |
|
192 | IPython.notebook.to_heading(undefined, 5); | |
193 | }); |
|
193 | }); | |
194 | this.element.find('#to_heading6').click(function () { |
|
194 | this.element.find('#to_heading6').click(function () { | |
195 | IPython.notebook.to_heading(undefined, 6); |
|
195 | IPython.notebook.to_heading(undefined, 6); | |
196 | }); |
|
196 | }); | |
197 | this.element.find('#toggle_output').click(function () { |
|
197 | this.element.find('#toggle_output').click(function () { | |
198 | IPython.notebook.toggle_output(); |
|
198 | IPython.notebook.toggle_output(); | |
199 | }); |
|
199 | }); | |
200 | this.element.find('#collapse_all_output').click(function () { |
|
200 | this.element.find('#collapse_all_output').click(function () { | |
201 | IPython.notebook.collapse_all_output(); |
|
201 | IPython.notebook.collapse_all_output(); | |
202 | }); |
|
202 | }); | |
203 | this.element.find('#scroll_all_output').click(function () { |
|
203 | this.element.find('#scroll_all_output').click(function () { | |
204 | IPython.notebook.scroll_all_output(); |
|
204 | IPython.notebook.scroll_all_output(); | |
205 | }); |
|
205 | }); | |
206 | this.element.find('#expand_all_output').click(function () { |
|
206 | this.element.find('#expand_all_output').click(function () { | |
207 | IPython.notebook.expand_all_output(); |
|
207 | IPython.notebook.expand_all_output(); | |
208 | }); |
|
208 | }); | |
209 | this.element.find('#clear_all_output').click(function () { |
|
209 | this.element.find('#clear_all_output').click(function () { | |
210 | IPython.notebook.clear_all_output(); |
|
210 | IPython.notebook.clear_all_output(); | |
211 | }); |
|
211 | }); | |
212 | // Kernel |
|
212 | // Kernel | |
213 | this.element.find('#int_kernel').click(function () { |
|
213 | this.element.find('#int_kernel').click(function () { | |
214 | IPython.notebook.kernel.interrupt(); |
|
214 | IPython.notebook.kernel.interrupt(); | |
215 | }); |
|
215 | }); | |
216 | this.element.find('#restart_kernel').click(function () { |
|
216 | this.element.find('#restart_kernel').click(function () { | |
217 | IPython.notebook.restart_kernel(); |
|
217 | IPython.notebook.restart_kernel(); | |
218 | }); |
|
218 | }); | |
219 | // Help |
|
219 | // Help | |
220 | this.element.find('#keyboard_shortcuts').click(function () { |
|
220 | this.element.find('#keyboard_shortcuts').click(function () { | |
221 | IPython.quick_help.show_keyboard_shortcuts(); |
|
221 | IPython.quick_help.show_keyboard_shortcuts(); | |
222 | }); |
|
222 | }); | |
223 |
|
223 | |||
224 | this.update_restore_checkpoint(null); |
|
224 | this.update_restore_checkpoint(null); | |
225 |
|
225 | |||
226 | $([IPython.events]).on('checkpoints_listed.Notebook', function (event, data) { |
|
226 | $([IPython.events]).on('checkpoints_listed.Notebook', function (event, data) { | |
227 | that.update_restore_checkpoint(data); |
|
227 | that.update_restore_checkpoint(data); | |
228 | }); |
|
228 | }); | |
229 |
|
229 | |||
230 | $([IPython.events]).on('checkpoint_created.Notebook', function (event, data) { |
|
230 | $([IPython.events]).on('checkpoint_created.Notebook', function (event, data) { | |
231 | that.update_restore_checkpoint([data]); |
|
231 | that.update_restore_checkpoint([data]); | |
232 | }); |
|
232 | }); | |
233 | }; |
|
233 | }; | |
234 |
|
234 | |||
235 | MenuBar.prototype.update_restore_checkpoint = function(checkpoints) { |
|
235 | MenuBar.prototype.update_restore_checkpoint = function(checkpoints) { | |
236 | if (! checkpoints) { |
|
236 | var ul = this.element.find("#restore_checkpoint").find("ul"); | |
237 | checkpoints = []; |
|
237 | ul.empty(); | |
|
238 | if (! checkpoints || checkpoints.length == 0) { | |||
|
239 | ul.append( | |||
|
240 | $("<li/>") | |||
|
241 | .addClass("disabled") | |||
|
242 | .append( | |||
|
243 | $("<a/>") | |||
|
244 | .text("No checkpoints") | |||
|
245 | ) | |||
|
246 | ); | |||
|
247 | return; | |||
238 | }; |
|
248 | }; | |
239 | this.element.find("#restore_checkpoint").find("ul").find("li").each(function(i) { |
|
249 | ||
240 | var li = $(this); |
|
250 | for (var i = 0; i < checkpoints.length; i++) { | |
241 | var a = li.find("a"); |
|
|||
242 | a.off("click"); |
|
|||
243 | if (checkpoints.length <= i) { |
|
|||
244 | li.hide(); |
|
|||
245 | return; |
|
|||
246 | } else { |
|
|||
247 | li.show(); |
|
|||
248 | }; |
|
|||
249 | var checkpoint = checkpoints[i]; |
|
251 | var checkpoint = checkpoints[i]; | |
250 | var d = new Date(checkpoint.last_modified); |
|
252 | var d = new Date(checkpoint.last_modified); | |
251 |
l |
|
253 | ul.append( | |
252 | d.format("mmm dd HH:MM:ss") |
|
254 | $("<li/>").append( | |
253 | ).click(function () { |
|
255 | $("<a/>") | |
254 | IPython.notebook.restore_checkpoint_dialog(checkpoint); |
|
256 | .attr("href", "#") | |
255 | }); |
|
257 | .text(d.format("mmm dd HH:MM:ss")) | |
256 | }); |
|
258 | .click(function () { | |
|
259 | IPython.notebook.restore_checkpoint_dialog(checkpoint); | |||
|
260 | }) | |||
|
261 | ) | |||
|
262 | ); | |||
|
263 | }; | |||
257 | }; |
|
264 | }; | |
258 |
|
265 | |||
259 | IPython.MenuBar = MenuBar; |
|
266 | IPython.MenuBar = MenuBar; | |
260 |
|
267 | |||
261 | return IPython; |
|
268 | return IPython; | |
262 |
|
269 | |||
263 | }(IPython)); |
|
270 | }(IPython)); |
General Comments 0
You need to be logged in to leave comments.
Login now