Show More
@@ -1,418 +1,421 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 | 'jquery', |
|
5 | 'jquery', | |
6 | 'base/js/namespace', |
|
6 | 'base/js/namespace', | |
7 | 'base/js/dialog', |
|
7 | 'base/js/dialog', | |
8 | 'base/js/utils', |
|
8 | 'base/js/utils', | |
9 | 'notebook/js/tour', |
|
9 | 'notebook/js/tour', | |
10 | 'bootstrap', |
|
10 | 'bootstrap', | |
11 | 'moment', |
|
11 | 'moment', | |
12 | ], function($, IPython, dialog, utils, tour, bootstrap, moment) { |
|
12 | ], function($, IPython, dialog, utils, tour, bootstrap, moment) { | |
13 | "use strict"; |
|
13 | "use strict"; | |
14 |
|
14 | |||
15 | var MenuBar = function (selector, options) { |
|
15 | var MenuBar = function (selector, options) { | |
16 | /** |
|
16 | /** | |
17 | * Constructor |
|
17 | * Constructor | |
18 | * |
|
18 | * | |
19 | * A MenuBar Class to generate the menubar of IPython notebook |
|
19 | * A MenuBar Class to generate the menubar of IPython notebook | |
20 | * |
|
20 | * | |
21 | * Parameters: |
|
21 | * Parameters: | |
22 | * selector: string |
|
22 | * selector: string | |
23 | * options: dictionary |
|
23 | * options: dictionary | |
24 | * Dictionary of keyword arguments. |
|
24 | * Dictionary of keyword arguments. | |
25 | * notebook: Notebook instance |
|
25 | * notebook: Notebook instance | |
26 | * contents: ContentManager instance |
|
26 | * contents: ContentManager instance | |
27 | * events: $(Events) instance |
|
27 | * events: $(Events) instance | |
28 | * save_widget: SaveWidget instance |
|
28 | * save_widget: SaveWidget instance | |
29 | * quick_help: QuickHelp instance |
|
29 | * quick_help: QuickHelp instance | |
30 | * base_url : string |
|
30 | * base_url : string | |
31 | * notebook_path : string |
|
31 | * notebook_path : string | |
32 | * notebook_name : string |
|
32 | * notebook_name : string | |
33 | */ |
|
33 | */ | |
34 | options = options || {}; |
|
34 | options = options || {}; | |
35 | this.base_url = options.base_url || utils.get_body_data("baseUrl"); |
|
35 | this.base_url = options.base_url || utils.get_body_data("baseUrl"); | |
36 | this.selector = selector; |
|
36 | this.selector = selector; | |
37 | this.notebook = options.notebook; |
|
37 | this.notebook = options.notebook; | |
38 | this.contents = options.contents; |
|
38 | this.contents = options.contents; | |
39 | this.events = options.events; |
|
39 | this.events = options.events; | |
40 | this.save_widget = options.save_widget; |
|
40 | this.save_widget = options.save_widget; | |
41 | this.quick_help = options.quick_help; |
|
41 | this.quick_help = options.quick_help; | |
42 |
|
42 | |||
43 | try { |
|
43 | try { | |
44 | this.tour = new tour.Tour(this.notebook, this.events); |
|
44 | this.tour = new tour.Tour(this.notebook, this.events); | |
45 | } catch (e) { |
|
45 | } catch (e) { | |
46 | this.tour = undefined; |
|
46 | this.tour = undefined; | |
47 | console.log("Failed to instantiate Notebook Tour", e); |
|
47 | console.log("Failed to instantiate Notebook Tour", e); | |
48 | } |
|
48 | } | |
49 |
|
49 | |||
50 | if (this.selector !== undefined) { |
|
50 | if (this.selector !== undefined) { | |
51 | this.element = $(selector); |
|
51 | this.element = $(selector); | |
52 | this.style(); |
|
52 | this.style(); | |
53 | this.bind_events(); |
|
53 | this.bind_events(); | |
54 | } |
|
54 | } | |
55 | }; |
|
55 | }; | |
56 |
|
56 | |||
57 | // TODO: This has definitively nothing to do with style ... |
|
57 | // TODO: This has definitively nothing to do with style ... | |
58 | MenuBar.prototype.style = function () { |
|
58 | MenuBar.prototype.style = function () { | |
59 | var that = this; |
|
59 | var that = this; | |
60 | this.element.find("li").click(function (event, ui) { |
|
60 | this.element.find("li").click(function (event, ui) { | |
61 | // The selected cell loses focus when the menu is entered, so we |
|
61 | // The selected cell loses focus when the menu is entered, so we | |
62 | // re-select it upon selection. |
|
62 | // re-select it upon selection. | |
63 | var i = that.notebook.get_selected_index(); |
|
63 | var i = that.notebook.get_selected_index(); | |
64 | that.notebook.select(i); |
|
64 | that.notebook.select(i); | |
65 | } |
|
65 | } | |
66 | ); |
|
66 | ); | |
67 | }; |
|
67 | }; | |
68 |
|
68 | |||
69 | MenuBar.prototype._nbconvert = function (format, download) { |
|
69 | MenuBar.prototype._nbconvert = function (format, download) { | |
70 | download = download || false; |
|
70 | download = download || false; | |
71 | var notebook_path = this.notebook.notebook_path; |
|
71 | var notebook_path = this.notebook.notebook_path; | |
72 | var url = utils.url_join_encode( |
|
72 | var url = utils.url_join_encode( | |
73 | this.base_url, |
|
73 | this.base_url, | |
74 | 'nbconvert', |
|
74 | 'nbconvert', | |
75 | format, |
|
75 | format, | |
76 | notebook_path |
|
76 | notebook_path | |
77 | ) + "?download=" + download.toString(); |
|
77 | ) + "?download=" + download.toString(); | |
78 |
|
78 | |||
79 | var w = window.open(undefined, IPython._target); |
|
79 | var w = window.open(undefined, IPython._target); | |
80 | if (this.notebook.dirty) { |
|
80 | if (this.notebook.dirty) { | |
81 | this.notebook.save_notebook().then(function() { |
|
81 | this.notebook.save_notebook().then(function() { | |
82 | w.location = url; |
|
82 | w.location = url; | |
83 | }); |
|
83 | }); | |
84 | } else { |
|
84 | } else { | |
85 | w.location = url; |
|
85 | w.location = url; | |
86 | } |
|
86 | } | |
87 | }; |
|
87 | }; | |
88 |
|
88 | |||
89 | MenuBar.prototype._size_header = function() { |
|
89 | MenuBar.prototype._size_header = function() { | |
90 | /** |
|
90 | /** | |
91 | * Update header spacer size. |
|
91 | * Update header spacer size. | |
92 | */ |
|
92 | */ | |
93 | this.events.trigger('resize-header.Page'); |
|
93 | this.events.trigger('resize-header.Page'); | |
94 | }; |
|
94 | }; | |
95 |
|
95 | |||
96 | MenuBar.prototype.bind_events = function () { |
|
96 | MenuBar.prototype.bind_events = function () { | |
97 | /** |
|
97 | /** | |
98 | * File |
|
98 | * File | |
99 | */ |
|
99 | */ | |
100 | var that = this; |
|
100 | var that = this; | |
101 |
|
101 | |||
102 | this.element.find('#open_notebook').click(function () { |
|
102 | this.element.find('#open_notebook').click(function () { | |
103 | var parent = utils.url_path_split(that.notebook.notebook_path)[0]; |
|
103 | var parent = utils.url_path_split(that.notebook.notebook_path)[0]; | |
104 | window.open(utils.url_join_encode(that.base_url, 'tree', parent), IPython._target); |
|
104 | window.open(utils.url_join_encode(that.base_url, 'tree', parent), IPython._target); | |
105 | }); |
|
105 | }); | |
106 | this.element.find('#copy_notebook').click(function () { |
|
106 | this.element.find('#copy_notebook').click(function () { | |
|
107 | if (that.notebook.dirty) { | |||
|
108 | that.notebook.save_notebook({async : false}); | |||
|
109 | } | |||
107 | that.notebook.copy_notebook(); |
|
110 | that.notebook.copy_notebook(); | |
108 | return false; |
|
111 | return false; | |
109 | }); |
|
112 | }); | |
110 | this.element.find('#download_ipynb').click(function () { |
|
113 | this.element.find('#download_ipynb').click(function () { | |
111 | var base_url = that.notebook.base_url; |
|
114 | var base_url = that.notebook.base_url; | |
112 | var notebook_path = that.notebook.notebook_path; |
|
115 | var notebook_path = that.notebook.notebook_path; | |
113 | if (that.notebook.dirty) { |
|
116 | if (that.notebook.dirty) { | |
114 | that.notebook.save_notebook({async : false}); |
|
117 | that.notebook.save_notebook({async : false}); | |
115 | } |
|
118 | } | |
116 |
|
119 | |||
117 | var url = utils.url_join_encode(base_url, 'files', notebook_path); |
|
120 | var url = utils.url_join_encode(base_url, 'files', notebook_path); | |
118 | window.open(url + '?download=1'); |
|
121 | window.open(url + '?download=1'); | |
119 | }); |
|
122 | }); | |
120 |
|
123 | |||
121 | this.element.find('#print_preview').click(function () { |
|
124 | this.element.find('#print_preview').click(function () { | |
122 | that._nbconvert('html', false); |
|
125 | that._nbconvert('html', false); | |
123 | }); |
|
126 | }); | |
124 |
|
127 | |||
125 | this.element.find('#download_html').click(function () { |
|
128 | this.element.find('#download_html').click(function () { | |
126 | that._nbconvert('html', true); |
|
129 | that._nbconvert('html', true); | |
127 | }); |
|
130 | }); | |
128 |
|
131 | |||
129 | this.element.find('#download_markdown').click(function () { |
|
132 | this.element.find('#download_markdown').click(function () { | |
130 | that._nbconvert('markdown', true); |
|
133 | that._nbconvert('markdown', true); | |
131 | }); |
|
134 | }); | |
132 |
|
135 | |||
133 | this.element.find('#download_rst').click(function () { |
|
136 | this.element.find('#download_rst').click(function () { | |
134 | that._nbconvert('rst', true); |
|
137 | that._nbconvert('rst', true); | |
135 | }); |
|
138 | }); | |
136 |
|
139 | |||
137 | this.element.find('#download_pdf').click(function () { |
|
140 | this.element.find('#download_pdf').click(function () { | |
138 | that._nbconvert('pdf', true); |
|
141 | that._nbconvert('pdf', true); | |
139 | }); |
|
142 | }); | |
140 |
|
143 | |||
141 | this.element.find('#download_script').click(function () { |
|
144 | this.element.find('#download_script').click(function () { | |
142 | that._nbconvert('script', true); |
|
145 | that._nbconvert('script', true); | |
143 | }); |
|
146 | }); | |
144 |
|
147 | |||
145 | this.element.find('#rename_notebook').click(function () { |
|
148 | this.element.find('#rename_notebook').click(function () { | |
146 | that.save_widget.rename_notebook({notebook: that.notebook}); |
|
149 | that.save_widget.rename_notebook({notebook: that.notebook}); | |
147 | }); |
|
150 | }); | |
148 |
|
151 | |||
149 | this.element.find('#save_checkpoint').click(function () { |
|
152 | this.element.find('#save_checkpoint').click(function () { | |
150 | that.notebook.save_checkpoint(); |
|
153 | that.notebook.save_checkpoint(); | |
151 | }); |
|
154 | }); | |
152 |
|
155 | |||
153 | this.element.find('#restore_checkpoint').click(function () { |
|
156 | this.element.find('#restore_checkpoint').click(function () { | |
154 | }); |
|
157 | }); | |
155 |
|
158 | |||
156 | this.element.find('#trust_notebook').click(function () { |
|
159 | this.element.find('#trust_notebook').click(function () { | |
157 | that.notebook.trust_notebook(); |
|
160 | that.notebook.trust_notebook(); | |
158 | }); |
|
161 | }); | |
159 | this.events.on('trust_changed.Notebook', function (event, trusted) { |
|
162 | this.events.on('trust_changed.Notebook', function (event, trusted) { | |
160 | if (trusted) { |
|
163 | if (trusted) { | |
161 | that.element.find('#trust_notebook') |
|
164 | that.element.find('#trust_notebook') | |
162 | .addClass("disabled").off('click') |
|
165 | .addClass("disabled").off('click') | |
163 | .find("a").text("Trusted Notebook"); |
|
166 | .find("a").text("Trusted Notebook"); | |
164 | } else { |
|
167 | } else { | |
165 | that.element.find('#trust_notebook') |
|
168 | that.element.find('#trust_notebook') | |
166 | .removeClass("disabled").on('click', function () { |
|
169 | .removeClass("disabled").on('click', function () { | |
167 | that.notebook.trust_notebook(); |
|
170 | that.notebook.trust_notebook(); | |
168 | }) |
|
171 | }) | |
169 | .find("a").text("Trust Notebook"); |
|
172 | .find("a").text("Trust Notebook"); | |
170 | } |
|
173 | } | |
171 | }); |
|
174 | }); | |
172 |
|
175 | |||
173 | this.element.find('#kill_and_exit').click(function () { |
|
176 | this.element.find('#kill_and_exit').click(function () { | |
174 | var close_window = function () { |
|
177 | var close_window = function () { | |
175 | /** |
|
178 | /** | |
176 | * allow closing of new tabs in Chromium, impossible in FF |
|
179 | * allow closing of new tabs in Chromium, impossible in FF | |
177 | */ |
|
180 | */ | |
178 | window.open('', '_self', ''); |
|
181 | window.open('', '_self', ''); | |
179 | window.close(); |
|
182 | window.close(); | |
180 | }; |
|
183 | }; | |
181 | // finish with close on success or failure |
|
184 | // finish with close on success or failure | |
182 | that.notebook.session.delete(close_window, close_window); |
|
185 | that.notebook.session.delete(close_window, close_window); | |
183 | }); |
|
186 | }); | |
184 |
|
187 | |||
185 | // Edit |
|
188 | // Edit | |
186 | this.element.find('#cut_cell').click(function () { |
|
189 | this.element.find('#cut_cell').click(function () { | |
187 | that.notebook.cut_cell(); |
|
190 | that.notebook.cut_cell(); | |
188 | }); |
|
191 | }); | |
189 | this.element.find('#copy_cell').click(function () { |
|
192 | this.element.find('#copy_cell').click(function () { | |
190 | that.notebook.copy_cell(); |
|
193 | that.notebook.copy_cell(); | |
191 | }); |
|
194 | }); | |
192 | this.element.find('#delete_cell').click(function () { |
|
195 | this.element.find('#delete_cell').click(function () { | |
193 | that.notebook.delete_cell(); |
|
196 | that.notebook.delete_cell(); | |
194 | }); |
|
197 | }); | |
195 | this.element.find('#undelete_cell').click(function () { |
|
198 | this.element.find('#undelete_cell').click(function () { | |
196 | that.notebook.undelete_cell(); |
|
199 | that.notebook.undelete_cell(); | |
197 | }); |
|
200 | }); | |
198 | this.element.find('#split_cell').click(function () { |
|
201 | this.element.find('#split_cell').click(function () { | |
199 | that.notebook.split_cell(); |
|
202 | that.notebook.split_cell(); | |
200 | }); |
|
203 | }); | |
201 | this.element.find('#merge_cell_above').click(function () { |
|
204 | this.element.find('#merge_cell_above').click(function () { | |
202 | that.notebook.merge_cell_above(); |
|
205 | that.notebook.merge_cell_above(); | |
203 | }); |
|
206 | }); | |
204 | this.element.find('#merge_cell_below').click(function () { |
|
207 | this.element.find('#merge_cell_below').click(function () { | |
205 | that.notebook.merge_cell_below(); |
|
208 | that.notebook.merge_cell_below(); | |
206 | }); |
|
209 | }); | |
207 | this.element.find('#move_cell_up').click(function () { |
|
210 | this.element.find('#move_cell_up').click(function () { | |
208 | that.notebook.move_cell_up(); |
|
211 | that.notebook.move_cell_up(); | |
209 | }); |
|
212 | }); | |
210 | this.element.find('#move_cell_down').click(function () { |
|
213 | this.element.find('#move_cell_down').click(function () { | |
211 | that.notebook.move_cell_down(); |
|
214 | that.notebook.move_cell_down(); | |
212 | }); |
|
215 | }); | |
213 | this.element.find('#edit_nb_metadata').click(function () { |
|
216 | this.element.find('#edit_nb_metadata').click(function () { | |
214 | that.notebook.edit_metadata({ |
|
217 | that.notebook.edit_metadata({ | |
215 | notebook: that.notebook, |
|
218 | notebook: that.notebook, | |
216 | keyboard_manager: that.notebook.keyboard_manager}); |
|
219 | keyboard_manager: that.notebook.keyboard_manager}); | |
217 | }); |
|
220 | }); | |
218 |
|
221 | |||
219 | // View |
|
222 | // View | |
220 | this.element.find('#toggle_header').click(function () { |
|
223 | this.element.find('#toggle_header').click(function () { | |
221 | $('#header-container').toggle(); |
|
224 | $('#header-container').toggle(); | |
222 | $('.header-bar').toggle(); |
|
225 | $('.header-bar').toggle(); | |
223 | that._size_header(); |
|
226 | that._size_header(); | |
224 | }); |
|
227 | }); | |
225 | this.element.find('#toggle_toolbar').click(function () { |
|
228 | this.element.find('#toggle_toolbar').click(function () { | |
226 | $('div#maintoolbar').toggle(); |
|
229 | $('div#maintoolbar').toggle(); | |
227 | that._size_header(); |
|
230 | that._size_header(); | |
228 | }); |
|
231 | }); | |
229 | // Insert |
|
232 | // Insert | |
230 | this.element.find('#insert_cell_above').click(function () { |
|
233 | this.element.find('#insert_cell_above').click(function () { | |
231 | that.notebook.insert_cell_above('code'); |
|
234 | that.notebook.insert_cell_above('code'); | |
232 | that.notebook.select_prev(); |
|
235 | that.notebook.select_prev(); | |
233 | }); |
|
236 | }); | |
234 | this.element.find('#insert_cell_below').click(function () { |
|
237 | this.element.find('#insert_cell_below').click(function () { | |
235 | that.notebook.insert_cell_below('code'); |
|
238 | that.notebook.insert_cell_below('code'); | |
236 | that.notebook.select_next(); |
|
239 | that.notebook.select_next(); | |
237 | }); |
|
240 | }); | |
238 | // Cell |
|
241 | // Cell | |
239 | this.element.find('#run_cell').click(function () { |
|
242 | this.element.find('#run_cell').click(function () { | |
240 | that.notebook.execute_cell(); |
|
243 | that.notebook.execute_cell(); | |
241 | }); |
|
244 | }); | |
242 | this.element.find('#run_cell_select_below').click(function () { |
|
245 | this.element.find('#run_cell_select_below').click(function () { | |
243 | that.notebook.execute_cell_and_select_below(); |
|
246 | that.notebook.execute_cell_and_select_below(); | |
244 | }); |
|
247 | }); | |
245 | this.element.find('#run_cell_insert_below').click(function () { |
|
248 | this.element.find('#run_cell_insert_below').click(function () { | |
246 | that.notebook.execute_cell_and_insert_below(); |
|
249 | that.notebook.execute_cell_and_insert_below(); | |
247 | }); |
|
250 | }); | |
248 | this.element.find('#run_all_cells').click(function () { |
|
251 | this.element.find('#run_all_cells').click(function () { | |
249 | that.notebook.execute_all_cells(); |
|
252 | that.notebook.execute_all_cells(); | |
250 | }); |
|
253 | }); | |
251 | this.element.find('#run_all_cells_above').click(function () { |
|
254 | this.element.find('#run_all_cells_above').click(function () { | |
252 | that.notebook.execute_cells_above(); |
|
255 | that.notebook.execute_cells_above(); | |
253 | }); |
|
256 | }); | |
254 | this.element.find('#run_all_cells_below').click(function () { |
|
257 | this.element.find('#run_all_cells_below').click(function () { | |
255 | that.notebook.execute_cells_below(); |
|
258 | that.notebook.execute_cells_below(); | |
256 | }); |
|
259 | }); | |
257 | this.element.find('#to_code').click(function () { |
|
260 | this.element.find('#to_code').click(function () { | |
258 | that.notebook.to_code(); |
|
261 | that.notebook.to_code(); | |
259 | }); |
|
262 | }); | |
260 | this.element.find('#to_markdown').click(function () { |
|
263 | this.element.find('#to_markdown').click(function () { | |
261 | that.notebook.to_markdown(); |
|
264 | that.notebook.to_markdown(); | |
262 | }); |
|
265 | }); | |
263 | this.element.find('#to_raw').click(function () { |
|
266 | this.element.find('#to_raw').click(function () { | |
264 | that.notebook.to_raw(); |
|
267 | that.notebook.to_raw(); | |
265 | }); |
|
268 | }); | |
266 |
|
269 | |||
267 | this.element.find('#toggle_current_output').click(function () { |
|
270 | this.element.find('#toggle_current_output').click(function () { | |
268 | that.notebook.toggle_output(); |
|
271 | that.notebook.toggle_output(); | |
269 | }); |
|
272 | }); | |
270 | this.element.find('#toggle_current_output_scroll').click(function () { |
|
273 | this.element.find('#toggle_current_output_scroll').click(function () { | |
271 | that.notebook.toggle_output_scroll(); |
|
274 | that.notebook.toggle_output_scroll(); | |
272 | }); |
|
275 | }); | |
273 | this.element.find('#clear_current_output').click(function () { |
|
276 | this.element.find('#clear_current_output').click(function () { | |
274 | that.notebook.clear_output(); |
|
277 | that.notebook.clear_output(); | |
275 | }); |
|
278 | }); | |
276 |
|
279 | |||
277 | this.element.find('#toggle_all_output').click(function () { |
|
280 | this.element.find('#toggle_all_output').click(function () { | |
278 | that.notebook.toggle_all_output(); |
|
281 | that.notebook.toggle_all_output(); | |
279 | }); |
|
282 | }); | |
280 | this.element.find('#toggle_all_output_scroll').click(function () { |
|
283 | this.element.find('#toggle_all_output_scroll').click(function () { | |
281 | that.notebook.toggle_all_output_scroll(); |
|
284 | that.notebook.toggle_all_output_scroll(); | |
282 | }); |
|
285 | }); | |
283 | this.element.find('#clear_all_output').click(function () { |
|
286 | this.element.find('#clear_all_output').click(function () { | |
284 | that.notebook.clear_all_output(); |
|
287 | that.notebook.clear_all_output(); | |
285 | }); |
|
288 | }); | |
286 |
|
289 | |||
287 | // Kernel |
|
290 | // Kernel | |
288 | this.element.find('#int_kernel').click(function () { |
|
291 | this.element.find('#int_kernel').click(function () { | |
289 | that.notebook.kernel.interrupt(); |
|
292 | that.notebook.kernel.interrupt(); | |
290 | }); |
|
293 | }); | |
291 | this.element.find('#restart_kernel').click(function () { |
|
294 | this.element.find('#restart_kernel').click(function () { | |
292 | that.notebook.restart_kernel(); |
|
295 | that.notebook.restart_kernel(); | |
293 | }); |
|
296 | }); | |
294 | this.element.find('#reconnect_kernel').click(function () { |
|
297 | this.element.find('#reconnect_kernel').click(function () { | |
295 | that.notebook.kernel.reconnect(); |
|
298 | that.notebook.kernel.reconnect(); | |
296 | }); |
|
299 | }); | |
297 | // Help |
|
300 | // Help | |
298 | if (this.tour) { |
|
301 | if (this.tour) { | |
299 | this.element.find('#notebook_tour').click(function () { |
|
302 | this.element.find('#notebook_tour').click(function () { | |
300 | that.tour.start(); |
|
303 | that.tour.start(); | |
301 | }); |
|
304 | }); | |
302 | } else { |
|
305 | } else { | |
303 | this.element.find('#notebook_tour').addClass("disabled"); |
|
306 | this.element.find('#notebook_tour').addClass("disabled"); | |
304 | } |
|
307 | } | |
305 | this.element.find('#keyboard_shortcuts').click(function () { |
|
308 | this.element.find('#keyboard_shortcuts').click(function () { | |
306 | that.quick_help.show_keyboard_shortcuts(); |
|
309 | that.quick_help.show_keyboard_shortcuts(); | |
307 | }); |
|
310 | }); | |
308 |
|
311 | |||
309 | this.update_restore_checkpoint(null); |
|
312 | this.update_restore_checkpoint(null); | |
310 |
|
313 | |||
311 | this.events.on('checkpoints_listed.Notebook', function (event, data) { |
|
314 | this.events.on('checkpoints_listed.Notebook', function (event, data) { | |
312 | that.update_restore_checkpoint(that.notebook.checkpoints); |
|
315 | that.update_restore_checkpoint(that.notebook.checkpoints); | |
313 | }); |
|
316 | }); | |
314 |
|
317 | |||
315 | this.events.on('checkpoint_created.Notebook', function (event, data) { |
|
318 | this.events.on('checkpoint_created.Notebook', function (event, data) { | |
316 | that.update_restore_checkpoint(that.notebook.checkpoints); |
|
319 | that.update_restore_checkpoint(that.notebook.checkpoints); | |
317 | }); |
|
320 | }); | |
318 |
|
321 | |||
319 | this.events.on('notebook_loaded.Notebook', function() { |
|
322 | this.events.on('notebook_loaded.Notebook', function() { | |
320 | var langinfo = that.notebook.metadata.language_info || {}; |
|
323 | var langinfo = that.notebook.metadata.language_info || {}; | |
321 | that.update_nbconvert_script(langinfo); |
|
324 | that.update_nbconvert_script(langinfo); | |
322 | }); |
|
325 | }); | |
323 |
|
326 | |||
324 | this.events.on('kernel_ready.Kernel', function(event, data) { |
|
327 | this.events.on('kernel_ready.Kernel', function(event, data) { | |
325 | var langinfo = data.kernel.info_reply.language_info || {}; |
|
328 | var langinfo = data.kernel.info_reply.language_info || {}; | |
326 | that.update_nbconvert_script(langinfo); |
|
329 | that.update_nbconvert_script(langinfo); | |
327 | that.add_kernel_help_links(data.kernel.info_reply.help_links || []); |
|
330 | that.add_kernel_help_links(data.kernel.info_reply.help_links || []); | |
328 | }); |
|
331 | }); | |
329 | }; |
|
332 | }; | |
330 |
|
333 | |||
331 | MenuBar.prototype.update_restore_checkpoint = function(checkpoints) { |
|
334 | MenuBar.prototype.update_restore_checkpoint = function(checkpoints) { | |
332 | var ul = this.element.find("#restore_checkpoint").find("ul"); |
|
335 | var ul = this.element.find("#restore_checkpoint").find("ul"); | |
333 | ul.empty(); |
|
336 | ul.empty(); | |
334 | if (!checkpoints || checkpoints.length === 0) { |
|
337 | if (!checkpoints || checkpoints.length === 0) { | |
335 | ul.append( |
|
338 | ul.append( | |
336 | $("<li/>") |
|
339 | $("<li/>") | |
337 | .addClass("disabled") |
|
340 | .addClass("disabled") | |
338 | .append( |
|
341 | .append( | |
339 | $("<a/>") |
|
342 | $("<a/>") | |
340 | .text("No checkpoints") |
|
343 | .text("No checkpoints") | |
341 | ) |
|
344 | ) | |
342 | ); |
|
345 | ); | |
343 | return; |
|
346 | return; | |
344 | } |
|
347 | } | |
345 |
|
348 | |||
346 | var that = this; |
|
349 | var that = this; | |
347 | checkpoints.map(function (checkpoint) { |
|
350 | checkpoints.map(function (checkpoint) { | |
348 | var d = new Date(checkpoint.last_modified); |
|
351 | var d = new Date(checkpoint.last_modified); | |
349 | ul.append( |
|
352 | ul.append( | |
350 | $("<li/>").append( |
|
353 | $("<li/>").append( | |
351 | $("<a/>") |
|
354 | $("<a/>") | |
352 | .attr("href", "#") |
|
355 | .attr("href", "#") | |
353 | .text(moment(d).format("LLLL")) |
|
356 | .text(moment(d).format("LLLL")) | |
354 | .click(function () { |
|
357 | .click(function () { | |
355 | that.notebook.restore_checkpoint_dialog(checkpoint); |
|
358 | that.notebook.restore_checkpoint_dialog(checkpoint); | |
356 | }) |
|
359 | }) | |
357 | ) |
|
360 | ) | |
358 | ); |
|
361 | ); | |
359 | }); |
|
362 | }); | |
360 | }; |
|
363 | }; | |
361 |
|
364 | |||
362 | MenuBar.prototype.update_nbconvert_script = function(langinfo) { |
|
365 | MenuBar.prototype.update_nbconvert_script = function(langinfo) { | |
363 | /** |
|
366 | /** | |
364 | * Set the 'Download as foo' menu option for the relevant language. |
|
367 | * Set the 'Download as foo' menu option for the relevant language. | |
365 | */ |
|
368 | */ | |
366 | var el = this.element.find('#download_script'); |
|
369 | var el = this.element.find('#download_script'); | |
367 |
|
370 | |||
368 | // Set menu entry text to e.g. "Python (.py)" |
|
371 | // Set menu entry text to e.g. "Python (.py)" | |
369 | var langname = (langinfo.name || 'Script'); |
|
372 | var langname = (langinfo.name || 'Script'); | |
370 | langname = langname.charAt(0).toUpperCase()+langname.substr(1); // Capitalise |
|
373 | langname = langname.charAt(0).toUpperCase()+langname.substr(1); // Capitalise | |
371 | el.find('a').text(langname + ' ('+(langinfo.file_extension || 'txt')+')'); |
|
374 | el.find('a').text(langname + ' ('+(langinfo.file_extension || 'txt')+')'); | |
372 | }; |
|
375 | }; | |
373 |
|
376 | |||
374 | MenuBar.prototype.add_kernel_help_links = function(help_links) { |
|
377 | MenuBar.prototype.add_kernel_help_links = function(help_links) { | |
375 | /** add links from kernel_info to the help menu */ |
|
378 | /** add links from kernel_info to the help menu */ | |
376 | var divider = $("#kernel-help-links"); |
|
379 | var divider = $("#kernel-help-links"); | |
377 | if (divider.length === 0) { |
|
380 | if (divider.length === 0) { | |
378 | // insert kernel help section above about link |
|
381 | // insert kernel help section above about link | |
379 | var about = $("#notebook_about").parent(); |
|
382 | var about = $("#notebook_about").parent(); | |
380 | divider = $("<li>") |
|
383 | divider = $("<li>") | |
381 | .attr('id', "kernel-help-links") |
|
384 | .attr('id', "kernel-help-links") | |
382 | .addClass('divider'); |
|
385 | .addClass('divider'); | |
383 | about.prev().before(divider); |
|
386 | about.prev().before(divider); | |
384 | } |
|
387 | } | |
385 | // remove previous entries |
|
388 | // remove previous entries | |
386 | while (!divider.next().hasClass('divider')) { |
|
389 | while (!divider.next().hasClass('divider')) { | |
387 | divider.next().remove(); |
|
390 | divider.next().remove(); | |
388 | } |
|
391 | } | |
389 | if (help_links.length === 0) { |
|
392 | if (help_links.length === 0) { | |
390 | // no help links, remove the divider |
|
393 | // no help links, remove the divider | |
391 | divider.remove(); |
|
394 | divider.remove(); | |
392 | return; |
|
395 | return; | |
393 | } |
|
396 | } | |
394 | var cursor = divider; |
|
397 | var cursor = divider; | |
395 | help_links.map(function (link) { |
|
398 | help_links.map(function (link) { | |
396 | cursor.after($("<li>") |
|
399 | cursor.after($("<li>") | |
397 | .append($("<a>") |
|
400 | .append($("<a>") | |
398 | .attr('target', '_blank') |
|
401 | .attr('target', '_blank') | |
399 | .attr('title', 'Opens in a new window') |
|
402 | .attr('title', 'Opens in a new window') | |
400 | .attr('href', link.url) |
|
403 | .attr('href', link.url) | |
401 | .append($("<i>") |
|
404 | .append($("<i>") | |
402 | .addClass("fa fa-external-link menu-icon pull-right") |
|
405 | .addClass("fa fa-external-link menu-icon pull-right") | |
403 | ) |
|
406 | ) | |
404 | .append($("<span>") |
|
407 | .append($("<span>") | |
405 | .text(link.text) |
|
408 | .text(link.text) | |
406 | ) |
|
409 | ) | |
407 | ) |
|
410 | ) | |
408 | ); |
|
411 | ); | |
409 | cursor = cursor.next(); |
|
412 | cursor = cursor.next(); | |
410 | }); |
|
413 | }); | |
411 |
|
414 | |||
412 | }; |
|
415 | }; | |
413 |
|
416 | |||
414 | // Backwards compatability. |
|
417 | // Backwards compatability. | |
415 | IPython.MenuBar = MenuBar; |
|
418 | IPython.MenuBar = MenuBar; | |
416 |
|
419 | |||
417 | return {'MenuBar': MenuBar}; |
|
420 | return {'MenuBar': MenuBar}; | |
418 | }); |
|
421 | }); |
General Comments 0
You need to be logged in to leave comments.
Login now