##// END OF EJS Templates
Merge pull request #6940 from takluyver/i6937...
Thomas Kluyver -
r18862:8c620291 merge
parent child Browse files
Show More
@@ -1,357 +1,349 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 // Constructor
16 // Constructor
17 //
17 //
18 // A MenuBar Class to generate the menubar of IPython notebook
18 // A MenuBar Class to generate the menubar of IPython notebook
19 //
19 //
20 // Parameters:
20 // Parameters:
21 // selector: string
21 // selector: string
22 // options: dictionary
22 // options: dictionary
23 // Dictionary of keyword arguments.
23 // Dictionary of keyword arguments.
24 // notebook: Notebook instance
24 // notebook: Notebook instance
25 // contents: ContentManager instance
25 // contents: ContentManager instance
26 // layout_manager: LayoutManager instance
26 // layout_manager: LayoutManager 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 options = options || {};
33 options = options || {};
34 this.base_url = options.base_url || utils.get_body_data("baseUrl");
34 this.base_url = options.base_url || utils.get_body_data("baseUrl");
35 this.selector = selector;
35 this.selector = selector;
36 this.notebook = options.notebook;
36 this.notebook = options.notebook;
37 this.contents = options.contents;
37 this.contents = options.contents;
38 this.layout_manager = options.layout_manager;
38 this.layout_manager = options.layout_manager;
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 notebook_name = this.notebook.notebook_name;
73 if (this.notebook.dirty) {
72 if (this.notebook.dirty) {
74 this.notebook.save_notebook({async : false});
73 this.notebook.save_notebook({async : false});
75 }
74 }
76 var url = utils.url_join_encode(
75 var url = utils.url_join_encode(
77 this.base_url,
76 this.base_url,
78 'nbconvert',
77 'nbconvert',
79 format,
78 format,
80 notebook_path,
79 notebook_path
81 notebook_name
82 ) + "?download=" + download.toString();
80 ) + "?download=" + download.toString();
83
81
84 window.open(url);
82 window.open(url);
85 };
83 };
86
84
87 MenuBar.prototype.bind_events = function () {
85 MenuBar.prototype.bind_events = function () {
88 // File
86 // File
89 var that = this;
87 var that = this;
90 this.element.find('#new_notebook').click(function () {
88 this.element.find('#new_notebook').click(function () {
91 var w = window.open();
89 var w = window.open();
92 // Create a new notebook in the same path as the current
90 // Create a new notebook in the same path as the current
93 // notebook's path.
91 // notebook's path.
94 var parent = utils.url_path_split(that.notebook.notebook_path)[0];
92 var parent = utils.url_path_split(that.notebook.notebook_path)[0];
95 that.contents.new_untitled(parent, {
93 that.contents.new_untitled(parent, {
96 type: "notebook",
94 type: "notebook",
97 success: function (data) {
95 success: function (data) {
98 w.location = utils.url_join_encode(
96 w.location = utils.url_join_encode(
99 that.base_url, 'notebooks', data.path
97 that.base_url, 'notebooks', data.path
100 );
98 );
101 },
99 },
102 error: function(error) {
100 error: function(error) {
103 w.close();
101 w.close();
104 dialog.modal({
102 dialog.modal({
105 title : 'Creating Notebook Failed',
103 title : 'Creating Notebook Failed',
106 body : "The error was: " + error.message,
104 body : "The error was: " + error.message,
107 buttons : {'OK' : {'class' : 'btn-primary'}}
105 buttons : {'OK' : {'class' : 'btn-primary'}}
108 });
106 });
109 }
107 }
110 });
108 });
111 });
109 });
112 this.element.find('#open_notebook').click(function () {
110 this.element.find('#open_notebook').click(function () {
113 var parent = utils.url_path_split(that.notebook.notebook_path)[0];
111 var parent = utils.url_path_split(that.notebook.notebook_path)[0];
114 window.open(utils.url_join_encode(that.base_url, 'tree', parent));
112 window.open(utils.url_join_encode(that.base_url, 'tree', parent));
115 });
113 });
116 this.element.find('#copy_notebook').click(function () {
114 this.element.find('#copy_notebook').click(function () {
117 that.notebook.copy_notebook();
115 that.notebook.copy_notebook();
118 return false;
116 return false;
119 });
117 });
120 this.element.find('#download_ipynb').click(function () {
118 this.element.find('#download_ipynb').click(function () {
121 var base_url = that.notebook.base_url;
119 var base_url = that.notebook.base_url;
122 var notebook_path = that.notebook.notebook_path;
120 var notebook_path = that.notebook.notebook_path;
123 var notebook_name = that.notebook.notebook_name;
124 if (that.notebook.dirty) {
121 if (that.notebook.dirty) {
125 that.notebook.save_notebook({async : false});
122 that.notebook.save_notebook({async : false});
126 }
123 }
127
124
128 var url = utils.url_join_encode(
125 var url = utils.url_join_encode(base_url, 'files', notebook_path);
129 base_url,
130 'files',
131 notebook_path,
132 notebook_name
133 );
134 window.open(url + '?download=1');
126 window.open(url + '?download=1');
135 });
127 });
136
128
137 this.element.find('#print_preview').click(function () {
129 this.element.find('#print_preview').click(function () {
138 that._nbconvert('html', false);
130 that._nbconvert('html', false);
139 });
131 });
140
132
141 this.element.find('#download_py').click(function () {
133 this.element.find('#download_py').click(function () {
142 that._nbconvert('python', true);
134 that._nbconvert('python', true);
143 });
135 });
144
136
145 this.element.find('#download_html').click(function () {
137 this.element.find('#download_html').click(function () {
146 that._nbconvert('html', true);
138 that._nbconvert('html', true);
147 });
139 });
148
140
149 this.element.find('#download_rst').click(function () {
141 this.element.find('#download_rst').click(function () {
150 that._nbconvert('rst', true);
142 that._nbconvert('rst', true);
151 });
143 });
152
144
153 this.element.find('#download_pdf').click(function () {
145 this.element.find('#download_pdf').click(function () {
154 that._nbconvert('pdf', true);
146 that._nbconvert('pdf', true);
155 });
147 });
156
148
157 this.element.find('#rename_notebook').click(function () {
149 this.element.find('#rename_notebook').click(function () {
158 that.save_widget.rename_notebook({notebook: that.notebook});
150 that.save_widget.rename_notebook({notebook: that.notebook});
159 });
151 });
160 this.element.find('#save_checkpoint').click(function () {
152 this.element.find('#save_checkpoint').click(function () {
161 that.notebook.save_checkpoint();
153 that.notebook.save_checkpoint();
162 });
154 });
163 this.element.find('#restore_checkpoint').click(function () {
155 this.element.find('#restore_checkpoint').click(function () {
164 });
156 });
165 this.element.find('#trust_notebook').click(function () {
157 this.element.find('#trust_notebook').click(function () {
166 that.notebook.trust_notebook();
158 that.notebook.trust_notebook();
167 });
159 });
168 this.events.on('trust_changed.Notebook', function (event, trusted) {
160 this.events.on('trust_changed.Notebook', function (event, trusted) {
169 if (trusted) {
161 if (trusted) {
170 that.element.find('#trust_notebook')
162 that.element.find('#trust_notebook')
171 .addClass("disabled")
163 .addClass("disabled")
172 .find("a").text("Trusted Notebook");
164 .find("a").text("Trusted Notebook");
173 } else {
165 } else {
174 that.element.find('#trust_notebook')
166 that.element.find('#trust_notebook')
175 .removeClass("disabled")
167 .removeClass("disabled")
176 .find("a").text("Trust Notebook");
168 .find("a").text("Trust Notebook");
177 }
169 }
178 });
170 });
179 this.element.find('#kill_and_exit').click(function () {
171 this.element.find('#kill_and_exit').click(function () {
180 var close_window = function () {
172 var close_window = function () {
181 // allow closing of new tabs in Chromium, impossible in FF
173 // allow closing of new tabs in Chromium, impossible in FF
182 window.open('', '_self', '');
174 window.open('', '_self', '');
183 window.close();
175 window.close();
184 };
176 };
185 // finish with close on success or failure
177 // finish with close on success or failure
186 that.notebook.session.delete(close_window, close_window);
178 that.notebook.session.delete(close_window, close_window);
187 });
179 });
188 // Edit
180 // Edit
189 this.element.find('#cut_cell').click(function () {
181 this.element.find('#cut_cell').click(function () {
190 that.notebook.cut_cell();
182 that.notebook.cut_cell();
191 });
183 });
192 this.element.find('#copy_cell').click(function () {
184 this.element.find('#copy_cell').click(function () {
193 that.notebook.copy_cell();
185 that.notebook.copy_cell();
194 });
186 });
195 this.element.find('#delete_cell').click(function () {
187 this.element.find('#delete_cell').click(function () {
196 that.notebook.delete_cell();
188 that.notebook.delete_cell();
197 });
189 });
198 this.element.find('#undelete_cell').click(function () {
190 this.element.find('#undelete_cell').click(function () {
199 that.notebook.undelete_cell();
191 that.notebook.undelete_cell();
200 });
192 });
201 this.element.find('#split_cell').click(function () {
193 this.element.find('#split_cell').click(function () {
202 that.notebook.split_cell();
194 that.notebook.split_cell();
203 });
195 });
204 this.element.find('#merge_cell_above').click(function () {
196 this.element.find('#merge_cell_above').click(function () {
205 that.notebook.merge_cell_above();
197 that.notebook.merge_cell_above();
206 });
198 });
207 this.element.find('#merge_cell_below').click(function () {
199 this.element.find('#merge_cell_below').click(function () {
208 that.notebook.merge_cell_below();
200 that.notebook.merge_cell_below();
209 });
201 });
210 this.element.find('#move_cell_up').click(function () {
202 this.element.find('#move_cell_up').click(function () {
211 that.notebook.move_cell_up();
203 that.notebook.move_cell_up();
212 });
204 });
213 this.element.find('#move_cell_down').click(function () {
205 this.element.find('#move_cell_down').click(function () {
214 that.notebook.move_cell_down();
206 that.notebook.move_cell_down();
215 });
207 });
216 this.element.find('#edit_nb_metadata').click(function () {
208 this.element.find('#edit_nb_metadata').click(function () {
217 that.notebook.edit_metadata({
209 that.notebook.edit_metadata({
218 notebook: that.notebook,
210 notebook: that.notebook,
219 keyboard_manager: that.notebook.keyboard_manager});
211 keyboard_manager: that.notebook.keyboard_manager});
220 });
212 });
221
213
222 // View
214 // View
223 this.element.find('#toggle_header').click(function () {
215 this.element.find('#toggle_header').click(function () {
224 $('div#header').toggle();
216 $('div#header').toggle();
225 that.layout_manager.do_resize();
217 that.layout_manager.do_resize();
226 });
218 });
227 this.element.find('#toggle_toolbar').click(function () {
219 this.element.find('#toggle_toolbar').click(function () {
228 $('div#maintoolbar').toggle();
220 $('div#maintoolbar').toggle();
229 that.layout_manager.do_resize();
221 that.layout_manager.do_resize();
230 });
222 });
231 // Insert
223 // Insert
232 this.element.find('#insert_cell_above').click(function () {
224 this.element.find('#insert_cell_above').click(function () {
233 that.notebook.insert_cell_above('code');
225 that.notebook.insert_cell_above('code');
234 that.notebook.select_prev();
226 that.notebook.select_prev();
235 });
227 });
236 this.element.find('#insert_cell_below').click(function () {
228 this.element.find('#insert_cell_below').click(function () {
237 that.notebook.insert_cell_below('code');
229 that.notebook.insert_cell_below('code');
238 that.notebook.select_next();
230 that.notebook.select_next();
239 });
231 });
240 // Cell
232 // Cell
241 this.element.find('#run_cell').click(function () {
233 this.element.find('#run_cell').click(function () {
242 that.notebook.execute_cell();
234 that.notebook.execute_cell();
243 });
235 });
244 this.element.find('#run_cell_select_below').click(function () {
236 this.element.find('#run_cell_select_below').click(function () {
245 that.notebook.execute_cell_and_select_below();
237 that.notebook.execute_cell_and_select_below();
246 });
238 });
247 this.element.find('#run_cell_insert_below').click(function () {
239 this.element.find('#run_cell_insert_below').click(function () {
248 that.notebook.execute_cell_and_insert_below();
240 that.notebook.execute_cell_and_insert_below();
249 });
241 });
250 this.element.find('#run_all_cells').click(function () {
242 this.element.find('#run_all_cells').click(function () {
251 that.notebook.execute_all_cells();
243 that.notebook.execute_all_cells();
252 });
244 });
253 this.element.find('#run_all_cells_above').click(function () {
245 this.element.find('#run_all_cells_above').click(function () {
254 that.notebook.execute_cells_above();
246 that.notebook.execute_cells_above();
255 });
247 });
256 this.element.find('#run_all_cells_below').click(function () {
248 this.element.find('#run_all_cells_below').click(function () {
257 that.notebook.execute_cells_below();
249 that.notebook.execute_cells_below();
258 });
250 });
259 this.element.find('#to_code').click(function () {
251 this.element.find('#to_code').click(function () {
260 that.notebook.to_code();
252 that.notebook.to_code();
261 });
253 });
262 this.element.find('#to_markdown').click(function () {
254 this.element.find('#to_markdown').click(function () {
263 that.notebook.to_markdown();
255 that.notebook.to_markdown();
264 });
256 });
265 this.element.find('#to_raw').click(function () {
257 this.element.find('#to_raw').click(function () {
266 that.notebook.to_raw();
258 that.notebook.to_raw();
267 });
259 });
268
260
269 this.element.find('#toggle_current_output').click(function () {
261 this.element.find('#toggle_current_output').click(function () {
270 that.notebook.toggle_output();
262 that.notebook.toggle_output();
271 });
263 });
272 this.element.find('#toggle_current_output_scroll').click(function () {
264 this.element.find('#toggle_current_output_scroll').click(function () {
273 that.notebook.toggle_output_scroll();
265 that.notebook.toggle_output_scroll();
274 });
266 });
275 this.element.find('#clear_current_output').click(function () {
267 this.element.find('#clear_current_output').click(function () {
276 that.notebook.clear_output();
268 that.notebook.clear_output();
277 });
269 });
278
270
279 this.element.find('#toggle_all_output').click(function () {
271 this.element.find('#toggle_all_output').click(function () {
280 that.notebook.toggle_all_output();
272 that.notebook.toggle_all_output();
281 });
273 });
282 this.element.find('#toggle_all_output_scroll').click(function () {
274 this.element.find('#toggle_all_output_scroll').click(function () {
283 that.notebook.toggle_all_output_scroll();
275 that.notebook.toggle_all_output_scroll();
284 });
276 });
285 this.element.find('#clear_all_output').click(function () {
277 this.element.find('#clear_all_output').click(function () {
286 that.notebook.clear_all_output();
278 that.notebook.clear_all_output();
287 });
279 });
288
280
289 // Kernel
281 // Kernel
290 this.element.find('#int_kernel').click(function () {
282 this.element.find('#int_kernel').click(function () {
291 that.notebook.kernel.interrupt();
283 that.notebook.kernel.interrupt();
292 });
284 });
293 this.element.find('#restart_kernel').click(function () {
285 this.element.find('#restart_kernel').click(function () {
294 that.notebook.restart_kernel();
286 that.notebook.restart_kernel();
295 });
287 });
296 this.element.find('#reconnect_kernel').click(function () {
288 this.element.find('#reconnect_kernel').click(function () {
297 that.notebook.kernel.reconnect();
289 that.notebook.kernel.reconnect();
298 });
290 });
299 // Help
291 // Help
300 if (this.tour) {
292 if (this.tour) {
301 this.element.find('#notebook_tour').click(function () {
293 this.element.find('#notebook_tour').click(function () {
302 that.tour.start();
294 that.tour.start();
303 });
295 });
304 } else {
296 } else {
305 this.element.find('#notebook_tour').addClass("disabled");
297 this.element.find('#notebook_tour').addClass("disabled");
306 }
298 }
307 this.element.find('#keyboard_shortcuts').click(function () {
299 this.element.find('#keyboard_shortcuts').click(function () {
308 that.quick_help.show_keyboard_shortcuts();
300 that.quick_help.show_keyboard_shortcuts();
309 });
301 });
310
302
311 this.update_restore_checkpoint(null);
303 this.update_restore_checkpoint(null);
312
304
313 this.events.on('checkpoints_listed.Notebook', function (event, data) {
305 this.events.on('checkpoints_listed.Notebook', function (event, data) {
314 that.update_restore_checkpoint(that.notebook.checkpoints);
306 that.update_restore_checkpoint(that.notebook.checkpoints);
315 });
307 });
316
308
317 this.events.on('checkpoint_created.Notebook', function (event, data) {
309 this.events.on('checkpoint_created.Notebook', function (event, data) {
318 that.update_restore_checkpoint(that.notebook.checkpoints);
310 that.update_restore_checkpoint(that.notebook.checkpoints);
319 });
311 });
320 };
312 };
321
313
322 MenuBar.prototype.update_restore_checkpoint = function(checkpoints) {
314 MenuBar.prototype.update_restore_checkpoint = function(checkpoints) {
323 var ul = this.element.find("#restore_checkpoint").find("ul");
315 var ul = this.element.find("#restore_checkpoint").find("ul");
324 ul.empty();
316 ul.empty();
325 if (!checkpoints || checkpoints.length === 0) {
317 if (!checkpoints || checkpoints.length === 0) {
326 ul.append(
318 ul.append(
327 $("<li/>")
319 $("<li/>")
328 .addClass("disabled")
320 .addClass("disabled")
329 .append(
321 .append(
330 $("<a/>")
322 $("<a/>")
331 .text("No checkpoints")
323 .text("No checkpoints")
332 )
324 )
333 );
325 );
334 return;
326 return;
335 }
327 }
336
328
337 var that = this;
329 var that = this;
338 checkpoints.map(function (checkpoint) {
330 checkpoints.map(function (checkpoint) {
339 var d = new Date(checkpoint.last_modified);
331 var d = new Date(checkpoint.last_modified);
340 ul.append(
332 ul.append(
341 $("<li/>").append(
333 $("<li/>").append(
342 $("<a/>")
334 $("<a/>")
343 .attr("href", "#")
335 .attr("href", "#")
344 .text(moment(d).format("LLLL"))
336 .text(moment(d).format("LLLL"))
345 .click(function () {
337 .click(function () {
346 that.notebook.restore_checkpoint_dialog(checkpoint);
338 that.notebook.restore_checkpoint_dialog(checkpoint);
347 })
339 })
348 )
340 )
349 );
341 );
350 });
342 });
351 };
343 };
352
344
353 // Backwards compatability.
345 // Backwards compatability.
354 IPython.MenuBar = MenuBar;
346 IPython.MenuBar = MenuBar;
355
347
356 return {'MenuBar': MenuBar};
348 return {'MenuBar': MenuBar};
357 });
349 });
General Comments 0
You need to be logged in to leave comments. Login now