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