menubar.js
386 lines
| 13.7 KiB
| application/javascript
|
JavascriptLexer
MinRK
|
r16266 | // Copyright (c) IPython Development Team. | ||
// Distributed under the terms of the Modified BSD License. | ||||
Brian Granger
|
r5857 | |||
Jonathan Frederic
|
r17198 | define([ | ||
Jonathan Frederic
|
r17200 | 'jquery', | ||
Min RK
|
r18752 | 'base/js/namespace', | ||
'base/js/dialog', | ||||
Jonathan Frederic
|
r17198 | 'base/js/utils', | ||
'notebook/js/tour', | ||||
MinRK
|
r17312 | 'bootstrap', | ||
Matthias BUSSONNIER
|
r17474 | 'moment', | ||
Min RK
|
r18752 | ], function($, IPython, dialog, utils, tour, bootstrap, moment) { | ||
Matthias BUSSONNIER
|
r12103 | "use strict"; | ||
MinRK
|
r13076 | |||
jon
|
r17210 | var MenuBar = function (selector, options) { | ||
Jonathan Frederic
|
r19176 | /** | ||
* Constructor | ||||
* | ||||
* A MenuBar Class to generate the menubar of IPython notebook | ||||
* | ||||
* Parameters: | ||||
* selector: string | ||||
* options: dictionary | ||||
* Dictionary of keyword arguments. | ||||
* notebook: Notebook instance | ||||
* contents: ContentManager instance | ||||
* events: $(Events) instance | ||||
* save_widget: SaveWidget instance | ||||
* quick_help: QuickHelp instance | ||||
* base_url : string | ||||
* notebook_path : string | ||||
* notebook_name : string | ||||
*/ | ||||
MinRK
|
r13103 | options = options || {}; | ||
Jonathan Frederic
|
r17198 | this.base_url = options.base_url || utils.get_body_data("baseUrl"); | ||
Brian Granger
|
r5857 | this.selector = selector; | ||
jon
|
r17210 | this.notebook = options.notebook; | ||
Jeff Hemmelgarn
|
r18643 | this.contents = options.contents; | ||
jon
|
r17210 | this.events = options.events; | ||
this.save_widget = options.save_widget; | ||||
this.quick_help = options.quick_help; | ||||
Jonathan Frederic
|
r17198 | |||
try { | ||||
jon
|
r17210 | this.tour = new tour.Tour(this.notebook, this.events); | ||
Jonathan Frederic
|
r17198 | } catch (e) { | ||
this.tour = undefined; | ||||
console.log("Failed to instantiate Notebook Tour", e); | ||||
} | ||||
Brian Granger
|
r5857 | if (this.selector !== undefined) { | ||
this.element = $(selector); | ||||
this.style(); | ||||
this.bind_events(); | ||||
} | ||||
}; | ||||
Matthias BUSSONNIER
|
r17439 | // TODO: This has definitively nothing to do with style ... | ||
Brian Granger
|
r5857 | MenuBar.prototype.style = function () { | ||
Jonathan Frederic
|
r17200 | var that = this; | ||
MinRK
|
r10888 | this.element.find("li").click(function (event, ui) { | ||
Brian Granger
|
r5908 | // The selected cell loses focus when the menu is entered, so we | ||
Brian Granger
|
r5898 | // re-select it upon selection. | ||
Jonathan Frederic
|
r17200 | var i = that.notebook.get_selected_index(); | ||
that.notebook.select(i); | ||||
Brian Granger
|
r5898 | } | ||
MinRK
|
r10888 | ); | ||
Brian Granger
|
r5857 | }; | ||
Thomas Kluyver
|
r13829 | MenuBar.prototype._nbconvert = function (format, download) { | ||
download = download || false; | ||||
Jonathan Frederic
|
r17198 | var notebook_path = this.notebook.notebook_path; | ||
MinRK
|
r15234 | var url = utils.url_join_encode( | ||
MinRK
|
r15238 | this.base_url, | ||
Thomas Kluyver
|
r13829 | 'nbconvert', | ||
format, | ||||
Thomas Kluyver
|
r18859 | notebook_path | ||
Thomas Kluyver
|
r13829 | ) + "?download=" + download.toString(); | ||
Thomas Kluyver
|
r18969 | |||
var w = window.open() | ||||
if (this.notebook.dirty) { | ||||
this.notebook.save_notebook().then(function() { | ||||
w.location = url; | ||||
}); | ||||
} else { | ||||
w.location = url; | ||||
} | ||||
MinRK
|
r15234 | }; | ||
Brian Granger
|
r5857 | |||
Jonathan Frederic
|
r19179 | MenuBar.prototype._size_header = function() { | ||
/** | ||||
* Update header spacer size. | ||||
*/ | ||||
this.events.trigger('resize-header.Page'); | ||||
}; | ||||
Brian Granger
|
r5857 | MenuBar.prototype.bind_events = function () { | ||
Jonathan Frederic
|
r19176 | /** | ||
* File | ||||
*/ | ||||
Matthias BUSSONNIER
|
r9699 | var that = this; | ||
Zachary Sailer
|
r12992 | this.element.find('#new_notebook').click(function () { | ||
Thomas Kluyver
|
r18771 | var w = window.open(); | ||
KesterTong
|
r18624 | // Create a new notebook in the same path as the current | ||
// notebook's path. | ||||
Min RK
|
r18758 | var parent = utils.url_path_split(that.notebook.notebook_path)[0]; | ||
Thomas Kluyver
|
r18829 | that.contents.new_untitled(parent, {type: "notebook"}).then( | ||
function (data) { | ||||
Thomas Kluyver
|
r18771 | w.location = utils.url_join_encode( | ||
Min RK
|
r18752 | that.base_url, 'notebooks', data.path | ||
Thomas Kluyver
|
r18771 | ); | ||
Thomas Kluyver
|
r18829 | }, | ||
function(error) { | ||||
Thomas Kluyver
|
r18771 | w.close(); | ||
Thomas Kluyver
|
r18649 | dialog.modal({ | ||
title : 'Creating Notebook Failed', | ||||
Kester Tong
|
r18661 | body : "The error was: " + error.message, | ||
Thomas Kluyver
|
r18649 | buttons : {'OK' : {'class' : 'btn-primary'}} | ||
}); | ||||
} | ||||
Thomas Kluyver
|
r18829 | ); | ||
Zachary Sailer
|
r12992 | }); | ||
this.element.find('#open_notebook').click(function () { | ||||
Thomas Kluyver
|
r18783 | var parent = utils.url_path_split(that.notebook.notebook_path)[0]; | ||
window.open(utils.url_join_encode(that.base_url, 'tree', parent)); | ||||
Zachary Sailer
|
r12992 | }); | ||
this.element.find('#copy_notebook').click(function () { | ||||
Jonathan Frederic
|
r17198 | that.notebook.copy_notebook(); | ||
Zachary Sailer
|
r12992 | return false; | ||
}); | ||||
this.element.find('#download_ipynb').click(function () { | ||||
Jonathan Frederic
|
r17198 | var base_url = that.notebook.base_url; | ||
var notebook_path = that.notebook.notebook_path; | ||||
if (that.notebook.dirty) { | ||||
that.notebook.save_notebook({async : false}); | ||||
MinRK
|
r13076 | } | ||
Thomas Kluyver
|
r18856 | var url = utils.url_join_encode(base_url, 'files', notebook_path); | ||
Min RK
|
r18556 | window.open(url + '?download=1'); | ||
Zachary Sailer
|
r12992 | }); | ||
MinRK
|
r13108 | |||
Thomas Kluyver
|
r13829 | this.element.find('#print_preview').click(function () { | ||
that._nbconvert('html', false); | ||||
}); | ||||
this.element.find('#download_html').click(function () { | ||||
that._nbconvert('html', true); | ||||
Zachary Sailer
|
r12992 | }); | ||
Thomas Kluyver
|
r13831 | |||
this.element.find('#download_rst').click(function () { | ||||
that._nbconvert('rst', true); | ||||
}); | ||||
MinRK
|
r16266 | this.element.find('#download_pdf').click(function () { | ||
that._nbconvert('pdf', true); | ||||
}); | ||||
Min RK
|
r19277 | this.element.find('#download_script').click(function () { | ||
that._nbconvert('script', true); | ||||
}); | ||||
Brian Granger
|
r5859 | this.element.find('#rename_notebook').click(function () { | ||
Jonathan Frederic
|
r17213 | that.save_widget.rename_notebook({notebook: that.notebook}); | ||
Brian Granger
|
r5859 | }); | ||
MinRK
|
r10501 | this.element.find('#save_checkpoint').click(function () { | ||
Jonathan Frederic
|
r17198 | that.notebook.save_checkpoint(); | ||
MinRK
|
r10501 | }); | ||
MinRK
|
r10503 | this.element.find('#restore_checkpoint').click(function () { | ||
}); | ||||
MinRK
|
r15655 | this.element.find('#trust_notebook').click(function () { | ||
Jonathan Frederic
|
r17198 | that.notebook.trust_notebook(); | ||
MinRK
|
r15655 | }); | ||
Jonathan Frederic
|
r17198 | this.events.on('trust_changed.Notebook', function (event, trusted) { | ||
MinRK
|
r15657 | if (trusted) { | ||
that.element.find('#trust_notebook') | ||||
.addClass("disabled") | ||||
.find("a").text("Trusted Notebook"); | ||||
} else { | ||||
that.element.find('#trust_notebook') | ||||
.removeClass("disabled") | ||||
.find("a").text("Trust Notebook"); | ||||
} | ||||
}); | ||||
Matthias BUSSONNIER
|
r6850 | this.element.find('#kill_and_exit').click(function () { | ||
MinRK
|
r17649 | var close_window = function () { | ||
Jonathan Frederic
|
r19176 | /** | ||
* allow closing of new tabs in Chromium, impossible in FF | ||||
*/ | ||||
Paul Ivanov
|
r13338 | window.open('', '_self', ''); | ||
window.close(); | ||||
MinRK
|
r17649 | }; | ||
// finish with close on success or failure | ||||
that.notebook.session.delete(close_window, close_window); | ||||
Matthias BUSSONNIER
|
r6850 | }); | ||
Brian Granger
|
r5857 | // Edit | ||
Brian Granger
|
r5879 | this.element.find('#cut_cell').click(function () { | ||
Jonathan Frederic
|
r17198 | that.notebook.cut_cell(); | ||
Brian Granger
|
r5879 | }); | ||
this.element.find('#copy_cell').click(function () { | ||||
Jonathan Frederic
|
r17198 | that.notebook.copy_cell(); | ||
Brian Granger
|
r5879 | }); | ||
Brian Granger
|
r5857 | this.element.find('#delete_cell').click(function () { | ||
Jonathan Frederic
|
r17198 | that.notebook.delete_cell(); | ||
Brian Granger
|
r5857 | }); | ||
MinRK
|
r9551 | this.element.find('#undelete_cell').click(function () { | ||
Jonathan Frederic
|
r17198 | that.notebook.undelete_cell(); | ||
MinRK
|
r9551 | }); | ||
Brian Granger
|
r5896 | this.element.find('#split_cell').click(function () { | ||
Jonathan Frederic
|
r17198 | that.notebook.split_cell(); | ||
Brian Granger
|
r5896 | }); | ||
this.element.find('#merge_cell_above').click(function () { | ||||
Jonathan Frederic
|
r17198 | that.notebook.merge_cell_above(); | ||
Brian Granger
|
r5896 | }); | ||
this.element.find('#merge_cell_below').click(function () { | ||||
Jonathan Frederic
|
r17198 | that.notebook.merge_cell_below(); | ||
Brian Granger
|
r5896 | }); | ||
Brian Granger
|
r5857 | this.element.find('#move_cell_up').click(function () { | ||
Jonathan Frederic
|
r17198 | that.notebook.move_cell_up(); | ||
Brian Granger
|
r5857 | }); | ||
this.element.find('#move_cell_down').click(function () { | ||||
Jonathan Frederic
|
r17198 | that.notebook.move_cell_down(); | ||
Brian Granger
|
r5857 | }); | ||
MinRK
|
r12913 | this.element.find('#edit_nb_metadata').click(function () { | ||
Jonathan Frederic
|
r17214 | that.notebook.edit_metadata({ | ||
notebook: that.notebook, | ||||
keyboard_manager: that.notebook.keyboard_manager}); | ||||
MinRK
|
r12913 | }); | ||
Brian Granger
|
r5994 | // View | ||
this.element.find('#toggle_header').click(function () { | ||||
Jonathan Frederic
|
r19179 | $('div#header-container').toggle(); | ||
that._size_header(); | ||||
Brian Granger
|
r5994 | }); | ||
this.element.find('#toggle_toolbar').click(function () { | ||||
MinRK
|
r10906 | $('div#maintoolbar').toggle(); | ||
Jonathan Frederic
|
r19179 | that._size_header(); | ||
Brian Granger
|
r5994 | }); | ||
Brian Granger
|
r5857 | // Insert | ||
this.element.find('#insert_cell_above').click(function () { | ||||
Jonathan Frederic
|
r17198 | that.notebook.insert_cell_above('code'); | ||
that.notebook.select_prev(); | ||||
Brian Granger
|
r5857 | }); | ||
this.element.find('#insert_cell_below').click(function () { | ||||
Jonathan Frederic
|
r17198 | that.notebook.insert_cell_below('code'); | ||
that.notebook.select_next(); | ||||
Brian Granger
|
r5857 | }); | ||
// Cell | ||||
this.element.find('#run_cell').click(function () { | ||||
Jonathan Frederic
|
r17198 | that.notebook.execute_cell(); | ||
Brian Granger
|
r5857 | }); | ||
Brian E. Granger
|
r14085 | this.element.find('#run_cell_select_below').click(function () { | ||
Jonathan Frederic
|
r17198 | that.notebook.execute_cell_and_select_below(); | ||
Brian E. Granger
|
r14085 | }); | ||
this.element.find('#run_cell_insert_below').click(function () { | ||||
Jonathan Frederic
|
r17198 | that.notebook.execute_cell_and_insert_below(); | ||
Brian Granger
|
r5857 | }); | ||
this.element.find('#run_all_cells').click(function () { | ||||
Jonathan Frederic
|
r17198 | that.notebook.execute_all_cells(); | ||
Paul Ivanov
|
r13159 | }); | ||
Paul Ivanov
|
r8606 | this.element.find('#run_all_cells_above').click(function () { | ||
Jonathan Frederic
|
r17198 | that.notebook.execute_cells_above(); | ||
Paul Ivanov
|
r13159 | }); | ||
Paul Ivanov
|
r8606 | this.element.find('#run_all_cells_below').click(function () { | ||
Jonathan Frederic
|
r17198 | that.notebook.execute_cells_below(); | ||
Paul Ivanov
|
r13159 | }); | ||
Brian E. Granger
|
r14869 | this.element.find('#to_code').click(function () { | ||
Jonathan Frederic
|
r17198 | that.notebook.to_code(); | ||
Brian E. Granger
|
r14869 | }); | ||
this.element.find('#to_markdown').click(function () { | ||||
Jonathan Frederic
|
r17198 | that.notebook.to_markdown(); | ||
Brian E. Granger
|
r14869 | }); | ||
this.element.find('#to_raw').click(function () { | ||||
Jonathan Frederic
|
r17198 | that.notebook.to_raw(); | ||
Brian E. Granger
|
r14869 | }); | ||
Brian E. Granger
|
r14871 | |||
this.element.find('#toggle_current_output').click(function () { | ||||
Jonathan Frederic
|
r17198 | that.notebook.toggle_output(); | ||
Brian Granger
|
r5857 | }); | ||
Brian E. Granger
|
r14871 | this.element.find('#toggle_current_output_scroll').click(function () { | ||
Jonathan Frederic
|
r17198 | that.notebook.toggle_output_scroll(); | ||
Brian Granger
|
r6017 | }); | ||
Brian E. Granger
|
r14867 | this.element.find('#clear_current_output').click(function () { | ||
Jonathan Frederic
|
r17198 | that.notebook.clear_output(); | ||
Brian Granger
|
r5857 | }); | ||
Brian E. Granger
|
r14871 | |||
this.element.find('#toggle_all_output').click(function () { | ||||
Jonathan Frederic
|
r17198 | that.notebook.toggle_all_output(); | ||
MinRK
|
r7362 | }); | ||
Brian E. Granger
|
r14871 | this.element.find('#toggle_all_output_scroll').click(function () { | ||
Jonathan Frederic
|
r17198 | that.notebook.toggle_all_output_scroll(); | ||
MinRK
|
r7362 | }); | ||
Brian Granger
|
r5857 | this.element.find('#clear_all_output').click(function () { | ||
Jonathan Frederic
|
r17198 | that.notebook.clear_all_output(); | ||
Brian Granger
|
r5857 | }); | ||
Brian E. Granger
|
r14871 | |||
Brian E. Granger
|
r14869 | // Kernel | ||
this.element.find('#int_kernel').click(function () { | ||||
Jessica B. Hamrick
|
r18204 | that.notebook.kernel.interrupt(); | ||
Brian E. Granger
|
r14869 | }); | ||
this.element.find('#restart_kernel').click(function () { | ||||
Jonathan Frederic
|
r17198 | that.notebook.restart_kernel(); | ||
Brian E. Granger
|
r14869 | }); | ||
Min RK
|
r18731 | this.element.find('#reconnect_kernel').click(function () { | ||
that.notebook.kernel.reconnect(); | ||||
}); | ||||
Brian Granger
|
r5858 | // Help | ||
Jonathan Frederic
|
r17198 | if (this.tour) { | ||
MinRK
|
r16672 | this.element.find('#notebook_tour').click(function () { | ||
Jonathan Frederic
|
r17198 | that.tour.start(); | ||
MinRK
|
r16672 | }); | ||
} else { | ||||
this.element.find('#notebook_tour').addClass("disabled"); | ||||
} | ||||
Brian Granger
|
r5858 | this.element.find('#keyboard_shortcuts').click(function () { | ||
Jonathan Frederic
|
r17198 | that.quick_help.show_keyboard_shortcuts(); | ||
Brian Granger
|
r5858 | }); | ||
MinRK
|
r10503 | |||
this.update_restore_checkpoint(null); | ||||
Jonathan Frederic
|
r17198 | this.events.on('checkpoints_listed.Notebook', function (event, data) { | ||
that.update_restore_checkpoint(that.notebook.checkpoints); | ||||
MinRK
|
r10503 | }); | ||
Jonathan Frederic
|
r17198 | this.events.on('checkpoint_created.Notebook', function (event, data) { | ||
that.update_restore_checkpoint(that.notebook.checkpoints); | ||||
MinRK
|
r10503 | }); | ||
Thomas Kluyver
|
r18967 | |||
this.events.on('notebook_loaded.Notebook', function() { | ||||
var langinfo = that.notebook.metadata.language_info || {}; | ||||
that.update_nbconvert_script(langinfo); | ||||
}); | ||||
this.events.on('kernel_ready.Kernel', function(event, data) { | ||||
var langinfo = data.kernel.info_reply.language_info || {}; | ||||
that.update_nbconvert_script(langinfo); | ||||
}); | ||||
Brian Granger
|
r5857 | }; | ||
MinRK
|
r10520 | MenuBar.prototype.update_restore_checkpoint = function(checkpoints) { | ||
MinRK
|
r11117 | var ul = this.element.find("#restore_checkpoint").find("ul"); | ||
ul.empty(); | ||||
MinRK
|
r13103 | if (!checkpoints || checkpoints.length === 0) { | ||
MinRK
|
r11117 | ul.append( | ||
$("<li/>") | ||||
.addClass("disabled") | ||||
.append( | ||||
$("<a/>") | ||||
.text("No checkpoints") | ||||
) | ||||
); | ||||
return; | ||||
MinRK
|
r13103 | } | ||
MinRK
|
r11164 | |||
Jonathan Frederic
|
r17198 | var that = this; | ||
MinRK
|
r12050 | checkpoints.map(function (checkpoint) { | ||
MinRK
|
r10520 | var d = new Date(checkpoint.last_modified); | ||
MinRK
|
r11117 | ul.append( | ||
$("<li/>").append( | ||||
$("<a/>") | ||||
.attr("href", "#") | ||||
Matthias BUSSONNIER
|
r17474 | .text(moment(d).format("LLLL")) | ||
MinRK
|
r11117 | .click(function () { | ||
Jonathan Frederic
|
r17198 | that.notebook.restore_checkpoint_dialog(checkpoint); | ||
MinRK
|
r11117 | }) | ||
) | ||||
); | ||||
MinRK
|
r12050 | }); | ||
MinRK
|
r10520 | }; | ||
Thomas Kluyver
|
r18967 | |||
MenuBar.prototype.update_nbconvert_script = function(langinfo) { | ||||
Jonathan Frederic
|
r19176 | /** | ||
* Set the 'Download as foo' menu option for the relevant language. | ||||
*/ | ||||
Thomas Kluyver
|
r18967 | var el = this.element.find('#download_script'); | ||
var that = this; | ||||
// Set menu entry text to e.g. "Python (.py)" | ||||
var langname = (langinfo.name || 'Script') | ||||
langname = langname.charAt(0).toUpperCase()+langname.substr(1) // Capitalise | ||||
Thomas Kluyver
|
r19027 | el.find('a').text(langname + ' ('+(langinfo.file_extension || 'txt')+')'); | ||
Thomas Kluyver
|
r18967 | }; | ||
Brian Granger
|
r5857 | |||
Jonathan Frederic
|
r17198 | // Backwards compatability. | ||
Brian Granger
|
r5857 | IPython.MenuBar = MenuBar; | ||
Jonathan Frederic
|
r17201 | return {'MenuBar': MenuBar}; | ||
Jonathan Frederic
|
r17198 | }); | ||