##// END OF EJS Templates
remove nbconvert_exporter handling from frontend...
Min RK -
Show More
@@ -1,396 +1,386
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()
79 var w = window.open()
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 this.element.find('#new_notebook').click(function () {
101 this.element.find('#new_notebook').click(function () {
102 var w = window.open();
102 var w = window.open();
103 // Create a new notebook in the same path as the current
103 // Create a new notebook in the same path as the current
104 // notebook's path.
104 // notebook's path.
105 var parent = utils.url_path_split(that.notebook.notebook_path)[0];
105 var parent = utils.url_path_split(that.notebook.notebook_path)[0];
106 that.contents.new_untitled(parent, {type: "notebook"}).then(
106 that.contents.new_untitled(parent, {type: "notebook"}).then(
107 function (data) {
107 function (data) {
108 w.location = utils.url_join_encode(
108 w.location = utils.url_join_encode(
109 that.base_url, 'notebooks', data.path
109 that.base_url, 'notebooks', data.path
110 );
110 );
111 },
111 },
112 function(error) {
112 function(error) {
113 w.close();
113 w.close();
114 dialog.modal({
114 dialog.modal({
115 title : 'Creating Notebook Failed',
115 title : 'Creating Notebook Failed',
116 body : "The error was: " + error.message,
116 body : "The error was: " + error.message,
117 buttons : {'OK' : {'class' : 'btn-primary'}}
117 buttons : {'OK' : {'class' : 'btn-primary'}}
118 });
118 });
119 }
119 }
120 );
120 );
121 });
121 });
122 this.element.find('#open_notebook').click(function () {
122 this.element.find('#open_notebook').click(function () {
123 var parent = utils.url_path_split(that.notebook.notebook_path)[0];
123 var parent = utils.url_path_split(that.notebook.notebook_path)[0];
124 window.open(utils.url_join_encode(that.base_url, 'tree', parent));
124 window.open(utils.url_join_encode(that.base_url, 'tree', parent));
125 });
125 });
126 this.element.find('#copy_notebook').click(function () {
126 this.element.find('#copy_notebook').click(function () {
127 that.notebook.copy_notebook();
127 that.notebook.copy_notebook();
128 return false;
128 return false;
129 });
129 });
130 this.element.find('#download_ipynb').click(function () {
130 this.element.find('#download_ipynb').click(function () {
131 var base_url = that.notebook.base_url;
131 var base_url = that.notebook.base_url;
132 var notebook_path = that.notebook.notebook_path;
132 var notebook_path = that.notebook.notebook_path;
133 if (that.notebook.dirty) {
133 if (that.notebook.dirty) {
134 that.notebook.save_notebook({async : false});
134 that.notebook.save_notebook({async : false});
135 }
135 }
136
136
137 var url = utils.url_join_encode(base_url, 'files', notebook_path);
137 var url = utils.url_join_encode(base_url, 'files', notebook_path);
138 window.open(url + '?download=1');
138 window.open(url + '?download=1');
139 });
139 });
140
140
141 this.element.find('#print_preview').click(function () {
141 this.element.find('#print_preview').click(function () {
142 that._nbconvert('html', false);
142 that._nbconvert('html', false);
143 });
143 });
144
144
145 this.element.find('#download_html').click(function () {
145 this.element.find('#download_html').click(function () {
146 that._nbconvert('html', true);
146 that._nbconvert('html', true);
147 });
147 });
148
148
149 this.element.find('#download_rst').click(function () {
149 this.element.find('#download_rst').click(function () {
150 that._nbconvert('rst', true);
150 that._nbconvert('rst', true);
151 });
151 });
152
152
153 this.element.find('#download_pdf').click(function () {
153 this.element.find('#download_pdf').click(function () {
154 that._nbconvert('pdf', true);
154 that._nbconvert('pdf', true);
155 });
155 });
156
156
157 this.element.find('#download_script').click(function () {
158 that._nbconvert('script', true);
159 });
160
157 this.element.find('#rename_notebook').click(function () {
161 this.element.find('#rename_notebook').click(function () {
158 that.save_widget.rename_notebook({notebook: that.notebook});
162 that.save_widget.rename_notebook({notebook: that.notebook});
159 });
163 });
160 this.element.find('#save_checkpoint').click(function () {
164 this.element.find('#save_checkpoint').click(function () {
161 that.notebook.save_checkpoint();
165 that.notebook.save_checkpoint();
162 });
166 });
163 this.element.find('#restore_checkpoint').click(function () {
167 this.element.find('#restore_checkpoint').click(function () {
164 });
168 });
165 this.element.find('#trust_notebook').click(function () {
169 this.element.find('#trust_notebook').click(function () {
166 that.notebook.trust_notebook();
170 that.notebook.trust_notebook();
167 });
171 });
168 this.events.on('trust_changed.Notebook', function (event, trusted) {
172 this.events.on('trust_changed.Notebook', function (event, trusted) {
169 if (trusted) {
173 if (trusted) {
170 that.element.find('#trust_notebook')
174 that.element.find('#trust_notebook')
171 .addClass("disabled")
175 .addClass("disabled")
172 .find("a").text("Trusted Notebook");
176 .find("a").text("Trusted Notebook");
173 } else {
177 } else {
174 that.element.find('#trust_notebook')
178 that.element.find('#trust_notebook')
175 .removeClass("disabled")
179 .removeClass("disabled")
176 .find("a").text("Trust Notebook");
180 .find("a").text("Trust Notebook");
177 }
181 }
178 });
182 });
179 this.element.find('#kill_and_exit').click(function () {
183 this.element.find('#kill_and_exit').click(function () {
180 var close_window = function () {
184 var close_window = function () {
181 /**
185 /**
182 * allow closing of new tabs in Chromium, impossible in FF
186 * allow closing of new tabs in Chromium, impossible in FF
183 */
187 */
184 window.open('', '_self', '');
188 window.open('', '_self', '');
185 window.close();
189 window.close();
186 };
190 };
187 // finish with close on success or failure
191 // finish with close on success or failure
188 that.notebook.session.delete(close_window, close_window);
192 that.notebook.session.delete(close_window, close_window);
189 });
193 });
190 // Edit
194 // Edit
191 this.element.find('#cut_cell').click(function () {
195 this.element.find('#cut_cell').click(function () {
192 that.notebook.cut_cell();
196 that.notebook.cut_cell();
193 });
197 });
194 this.element.find('#copy_cell').click(function () {
198 this.element.find('#copy_cell').click(function () {
195 that.notebook.copy_cell();
199 that.notebook.copy_cell();
196 });
200 });
197 this.element.find('#delete_cell').click(function () {
201 this.element.find('#delete_cell').click(function () {
198 that.notebook.delete_cell();
202 that.notebook.delete_cell();
199 });
203 });
200 this.element.find('#undelete_cell').click(function () {
204 this.element.find('#undelete_cell').click(function () {
201 that.notebook.undelete_cell();
205 that.notebook.undelete_cell();
202 });
206 });
203 this.element.find('#split_cell').click(function () {
207 this.element.find('#split_cell').click(function () {
204 that.notebook.split_cell();
208 that.notebook.split_cell();
205 });
209 });
206 this.element.find('#merge_cell_above').click(function () {
210 this.element.find('#merge_cell_above').click(function () {
207 that.notebook.merge_cell_above();
211 that.notebook.merge_cell_above();
208 });
212 });
209 this.element.find('#merge_cell_below').click(function () {
213 this.element.find('#merge_cell_below').click(function () {
210 that.notebook.merge_cell_below();
214 that.notebook.merge_cell_below();
211 });
215 });
212 this.element.find('#move_cell_up').click(function () {
216 this.element.find('#move_cell_up').click(function () {
213 that.notebook.move_cell_up();
217 that.notebook.move_cell_up();
214 });
218 });
215 this.element.find('#move_cell_down').click(function () {
219 this.element.find('#move_cell_down').click(function () {
216 that.notebook.move_cell_down();
220 that.notebook.move_cell_down();
217 });
221 });
218 this.element.find('#edit_nb_metadata').click(function () {
222 this.element.find('#edit_nb_metadata').click(function () {
219 that.notebook.edit_metadata({
223 that.notebook.edit_metadata({
220 notebook: that.notebook,
224 notebook: that.notebook,
221 keyboard_manager: that.notebook.keyboard_manager});
225 keyboard_manager: that.notebook.keyboard_manager});
222 });
226 });
223
227
224 // View
228 // View
225 this.element.find('#toggle_header').click(function () {
229 this.element.find('#toggle_header').click(function () {
226 $('div#header-container').toggle();
230 $('div#header-container').toggle();
227 that._size_header();
231 that._size_header();
228 });
232 });
229 this.element.find('#toggle_toolbar').click(function () {
233 this.element.find('#toggle_toolbar').click(function () {
230 $('div#maintoolbar').toggle();
234 $('div#maintoolbar').toggle();
231 that._size_header();
235 that._size_header();
232 });
236 });
233 // Insert
237 // Insert
234 this.element.find('#insert_cell_above').click(function () {
238 this.element.find('#insert_cell_above').click(function () {
235 that.notebook.insert_cell_above('code');
239 that.notebook.insert_cell_above('code');
236 that.notebook.select_prev();
240 that.notebook.select_prev();
237 });
241 });
238 this.element.find('#insert_cell_below').click(function () {
242 this.element.find('#insert_cell_below').click(function () {
239 that.notebook.insert_cell_below('code');
243 that.notebook.insert_cell_below('code');
240 that.notebook.select_next();
244 that.notebook.select_next();
241 });
245 });
242 // Cell
246 // Cell
243 this.element.find('#run_cell').click(function () {
247 this.element.find('#run_cell').click(function () {
244 that.notebook.execute_cell();
248 that.notebook.execute_cell();
245 });
249 });
246 this.element.find('#run_cell_select_below').click(function () {
250 this.element.find('#run_cell_select_below').click(function () {
247 that.notebook.execute_cell_and_select_below();
251 that.notebook.execute_cell_and_select_below();
248 });
252 });
249 this.element.find('#run_cell_insert_below').click(function () {
253 this.element.find('#run_cell_insert_below').click(function () {
250 that.notebook.execute_cell_and_insert_below();
254 that.notebook.execute_cell_and_insert_below();
251 });
255 });
252 this.element.find('#run_all_cells').click(function () {
256 this.element.find('#run_all_cells').click(function () {
253 that.notebook.execute_all_cells();
257 that.notebook.execute_all_cells();
254 });
258 });
255 this.element.find('#run_all_cells_above').click(function () {
259 this.element.find('#run_all_cells_above').click(function () {
256 that.notebook.execute_cells_above();
260 that.notebook.execute_cells_above();
257 });
261 });
258 this.element.find('#run_all_cells_below').click(function () {
262 this.element.find('#run_all_cells_below').click(function () {
259 that.notebook.execute_cells_below();
263 that.notebook.execute_cells_below();
260 });
264 });
261 this.element.find('#to_code').click(function () {
265 this.element.find('#to_code').click(function () {
262 that.notebook.to_code();
266 that.notebook.to_code();
263 });
267 });
264 this.element.find('#to_markdown').click(function () {
268 this.element.find('#to_markdown').click(function () {
265 that.notebook.to_markdown();
269 that.notebook.to_markdown();
266 });
270 });
267 this.element.find('#to_raw').click(function () {
271 this.element.find('#to_raw').click(function () {
268 that.notebook.to_raw();
272 that.notebook.to_raw();
269 });
273 });
270
274
271 this.element.find('#toggle_current_output').click(function () {
275 this.element.find('#toggle_current_output').click(function () {
272 that.notebook.toggle_output();
276 that.notebook.toggle_output();
273 });
277 });
274 this.element.find('#toggle_current_output_scroll').click(function () {
278 this.element.find('#toggle_current_output_scroll').click(function () {
275 that.notebook.toggle_output_scroll();
279 that.notebook.toggle_output_scroll();
276 });
280 });
277 this.element.find('#clear_current_output').click(function () {
281 this.element.find('#clear_current_output').click(function () {
278 that.notebook.clear_output();
282 that.notebook.clear_output();
279 });
283 });
280
284
281 this.element.find('#toggle_all_output').click(function () {
285 this.element.find('#toggle_all_output').click(function () {
282 that.notebook.toggle_all_output();
286 that.notebook.toggle_all_output();
283 });
287 });
284 this.element.find('#toggle_all_output_scroll').click(function () {
288 this.element.find('#toggle_all_output_scroll').click(function () {
285 that.notebook.toggle_all_output_scroll();
289 that.notebook.toggle_all_output_scroll();
286 });
290 });
287 this.element.find('#clear_all_output').click(function () {
291 this.element.find('#clear_all_output').click(function () {
288 that.notebook.clear_all_output();
292 that.notebook.clear_all_output();
289 });
293 });
290
294
291 // Kernel
295 // Kernel
292 this.element.find('#int_kernel').click(function () {
296 this.element.find('#int_kernel').click(function () {
293 that.notebook.kernel.interrupt();
297 that.notebook.kernel.interrupt();
294 });
298 });
295 this.element.find('#restart_kernel').click(function () {
299 this.element.find('#restart_kernel').click(function () {
296 that.notebook.restart_kernel();
300 that.notebook.restart_kernel();
297 });
301 });
298 this.element.find('#reconnect_kernel').click(function () {
302 this.element.find('#reconnect_kernel').click(function () {
299 that.notebook.kernel.reconnect();
303 that.notebook.kernel.reconnect();
300 });
304 });
301 // Help
305 // Help
302 if (this.tour) {
306 if (this.tour) {
303 this.element.find('#notebook_tour').click(function () {
307 this.element.find('#notebook_tour').click(function () {
304 that.tour.start();
308 that.tour.start();
305 });
309 });
306 } else {
310 } else {
307 this.element.find('#notebook_tour').addClass("disabled");
311 this.element.find('#notebook_tour').addClass("disabled");
308 }
312 }
309 this.element.find('#keyboard_shortcuts').click(function () {
313 this.element.find('#keyboard_shortcuts').click(function () {
310 that.quick_help.show_keyboard_shortcuts();
314 that.quick_help.show_keyboard_shortcuts();
311 });
315 });
312
316
313 this.update_restore_checkpoint(null);
317 this.update_restore_checkpoint(null);
314
318
315 this.events.on('checkpoints_listed.Notebook', function (event, data) {
319 this.events.on('checkpoints_listed.Notebook', function (event, data) {
316 that.update_restore_checkpoint(that.notebook.checkpoints);
320 that.update_restore_checkpoint(that.notebook.checkpoints);
317 });
321 });
318
322
319 this.events.on('checkpoint_created.Notebook', function (event, data) {
323 this.events.on('checkpoint_created.Notebook', function (event, data) {
320 that.update_restore_checkpoint(that.notebook.checkpoints);
324 that.update_restore_checkpoint(that.notebook.checkpoints);
321 });
325 });
322
326
323 this.events.on('notebook_loaded.Notebook', function() {
327 this.events.on('notebook_loaded.Notebook', function() {
324 var langinfo = that.notebook.metadata.language_info || {};
328 var langinfo = that.notebook.metadata.language_info || {};
325 that.update_nbconvert_script(langinfo);
329 that.update_nbconvert_script(langinfo);
326 });
330 });
327
331
328 this.events.on('kernel_ready.Kernel', function(event, data) {
332 this.events.on('kernel_ready.Kernel', function(event, data) {
329 var langinfo = data.kernel.info_reply.language_info || {};
333 var langinfo = data.kernel.info_reply.language_info || {};
330 that.update_nbconvert_script(langinfo);
334 that.update_nbconvert_script(langinfo);
331 });
335 });
332 };
336 };
333
337
334 MenuBar.prototype.update_restore_checkpoint = function(checkpoints) {
338 MenuBar.prototype.update_restore_checkpoint = function(checkpoints) {
335 var ul = this.element.find("#restore_checkpoint").find("ul");
339 var ul = this.element.find("#restore_checkpoint").find("ul");
336 ul.empty();
340 ul.empty();
337 if (!checkpoints || checkpoints.length === 0) {
341 if (!checkpoints || checkpoints.length === 0) {
338 ul.append(
342 ul.append(
339 $("<li/>")
343 $("<li/>")
340 .addClass("disabled")
344 .addClass("disabled")
341 .append(
345 .append(
342 $("<a/>")
346 $("<a/>")
343 .text("No checkpoints")
347 .text("No checkpoints")
344 )
348 )
345 );
349 );
346 return;
350 return;
347 }
351 }
348
352
349 var that = this;
353 var that = this;
350 checkpoints.map(function (checkpoint) {
354 checkpoints.map(function (checkpoint) {
351 var d = new Date(checkpoint.last_modified);
355 var d = new Date(checkpoint.last_modified);
352 ul.append(
356 ul.append(
353 $("<li/>").append(
357 $("<li/>").append(
354 $("<a/>")
358 $("<a/>")
355 .attr("href", "#")
359 .attr("href", "#")
356 .text(moment(d).format("LLLL"))
360 .text(moment(d).format("LLLL"))
357 .click(function () {
361 .click(function () {
358 that.notebook.restore_checkpoint_dialog(checkpoint);
362 that.notebook.restore_checkpoint_dialog(checkpoint);
359 })
363 })
360 )
364 )
361 );
365 );
362 });
366 });
363 };
367 };
364
368
365 MenuBar.prototype.update_nbconvert_script = function(langinfo) {
369 MenuBar.prototype.update_nbconvert_script = function(langinfo) {
366 /**
370 /**
367 * Set the 'Download as foo' menu option for the relevant language.
371 * Set the 'Download as foo' menu option for the relevant language.
368 */
372 */
369 var el = this.element.find('#download_script');
373 var el = this.element.find('#download_script');
370 var that = this;
374 var that = this;
371
375
372 // Set menu entry text to e.g. "Python (.py)"
376 // Set menu entry text to e.g. "Python (.py)"
373 var langname = (langinfo.name || 'Script')
377 var langname = (langinfo.name || 'Script')
374 langname = langname.charAt(0).toUpperCase()+langname.substr(1) // Capitalise
378 langname = langname.charAt(0).toUpperCase()+langname.substr(1) // Capitalise
375 el.find('a').text(langname + ' ('+(langinfo.file_extension || 'txt')+')');
379 el.find('a').text(langname + ' ('+(langinfo.file_extension || 'txt')+')');
376
377 // Unregister any previously registered handlers
378 el.off('click');
379 if (langinfo.nbconvert_exporter) {
380 // Metadata specifies a specific exporter, e.g. 'python'
381 el.click(function() {
382 that._nbconvert(langinfo.nbconvert_exporter, true);
383 });
384 } else {
385 // Use generic 'script' exporter
386 el.click(function() {
387 that._nbconvert('script', true);
388 });
389 }
390 };
380 };
391
381
392 // Backwards compatability.
382 // Backwards compatability.
393 IPython.MenuBar = MenuBar;
383 IPython.MenuBar = MenuBar;
394
384
395 return {'MenuBar': MenuBar};
385 return {'MenuBar': MenuBar};
396 });
386 });
General Comments 0
You need to be logged in to leave comments. Login now