menubar.js
412 lines
| 14.4 KiB
| application/javascript
|
JavascriptLexer
|
r16266 | // Copyright (c) IPython Development Team. | ||
// Distributed under the terms of the Modified BSD License. | ||||
|
r5857 | |||
|
r17198 | define([ | ||
|
r17200 | 'jquery', | ||
|
r18752 | 'base/js/namespace', | ||
'base/js/dialog', | ||||
|
r17198 | 'base/js/utils', | ||
'notebook/js/tour', | ||||
|
r17312 | 'bootstrap', | ||
|
r17474 | 'moment', | ||
|
r18752 | ], function($, IPython, dialog, utils, tour, bootstrap, moment) { | ||
|
r12103 | "use strict"; | ||
|
r13076 | |||
|
r17210 | var MenuBar = function (selector, options) { | ||
|
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 | ||||
*/ | ||||
|
r13103 | options = options || {}; | ||
|
r17198 | this.base_url = options.base_url || utils.get_body_data("baseUrl"); | ||
|
r5857 | this.selector = selector; | ||
|
r17210 | this.notebook = options.notebook; | ||
|
r18643 | this.contents = options.contents; | ||
|
r17210 | this.events = options.events; | ||
this.save_widget = options.save_widget; | ||||
this.quick_help = options.quick_help; | ||||
|
r17198 | |||
try { | ||||
|
r17210 | this.tour = new tour.Tour(this.notebook, this.events); | ||
|
r17198 | } catch (e) { | ||
this.tour = undefined; | ||||
console.log("Failed to instantiate Notebook Tour", e); | ||||
} | ||||
|
r5857 | if (this.selector !== undefined) { | ||
this.element = $(selector); | ||||
this.style(); | ||||
this.bind_events(); | ||||
} | ||||
}; | ||||
|
r17439 | // TODO: This has definitively nothing to do with style ... | ||
|
r5857 | MenuBar.prototype.style = function () { | ||
|
r17200 | var that = this; | ||
|
r10888 | this.element.find("li").click(function (event, ui) { | ||
|
r5908 | // The selected cell loses focus when the menu is entered, so we | ||
|
r5898 | // re-select it upon selection. | ||
|
r17200 | var i = that.notebook.get_selected_index(); | ||
that.notebook.select(i); | ||||
|
r5898 | } | ||
|
r10888 | ); | ||
|
r5857 | }; | ||
|
r13829 | MenuBar.prototype._nbconvert = function (format, download) { | ||
download = download || false; | ||||
|
r17198 | var notebook_path = this.notebook.notebook_path; | ||
|
r15234 | var url = utils.url_join_encode( | ||
|
r15238 | this.base_url, | ||
|
r13829 | 'nbconvert', | ||
format, | ||||
|
r18859 | notebook_path | ||
|
r13829 | ) + "?download=" + download.toString(); | ||
|
r18969 | |||
|
r20325 | var w = window.open(undefined, IPython._target); | ||
|
r18969 | if (this.notebook.dirty) { | ||
this.notebook.save_notebook().then(function() { | ||||
w.location = url; | ||||
}); | ||||
} else { | ||||
w.location = url; | ||||
} | ||||
|
r15234 | }; | ||
|
r5857 | |||
|
r19179 | MenuBar.prototype._size_header = function() { | ||
/** | ||||
* Update header spacer size. | ||||
*/ | ||||
this.events.trigger('resize-header.Page'); | ||||
}; | ||||
|
r5857 | MenuBar.prototype.bind_events = function () { | ||
|
r19176 | /** | ||
* File | ||||
*/ | ||||
|
r9699 | var that = this; | ||
|
r19844 | |||
|
r12992 | this.element.find('#open_notebook').click(function () { | ||
|
r18783 | var parent = utils.url_path_split(that.notebook.notebook_path)[0]; | ||
|
r20325 | window.open(utils.url_join_encode(that.base_url, 'tree', parent), IPython._target); | ||
|
r12992 | }); | ||
this.element.find('#copy_notebook').click(function () { | ||||
|
r17198 | that.notebook.copy_notebook(); | ||
|
r12992 | return false; | ||
}); | ||||
this.element.find('#download_ipynb').click(function () { | ||||
|
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}); | ||||
|
r13076 | } | ||
|
r18856 | var url = utils.url_join_encode(base_url, 'files', notebook_path); | ||
|
r18556 | window.open(url + '?download=1'); | ||
|
r12992 | }); | ||
|
r13108 | |||
|
r13829 | this.element.find('#print_preview').click(function () { | ||
that._nbconvert('html', false); | ||||
}); | ||||
this.element.find('#download_html').click(function () { | ||||
that._nbconvert('html', true); | ||||
|
r12992 | }); | ||
|
r13831 | |||
this.element.find('#download_rst').click(function () { | ||||
that._nbconvert('rst', true); | ||||
}); | ||||
|
r16266 | this.element.find('#download_pdf').click(function () { | ||
that._nbconvert('pdf', true); | ||||
}); | ||||
|
r19277 | this.element.find('#download_script').click(function () { | ||
that._nbconvert('script', true); | ||||
}); | ||||
|
r5859 | this.element.find('#rename_notebook').click(function () { | ||
|
r17213 | that.save_widget.rename_notebook({notebook: that.notebook}); | ||
|
r5859 | }); | ||
|
r19908 | |||
|
r10501 | this.element.find('#save_checkpoint').click(function () { | ||
|
r17198 | that.notebook.save_checkpoint(); | ||
|
r10501 | }); | ||
|
r19908 | |||
|
r10503 | this.element.find('#restore_checkpoint').click(function () { | ||
}); | ||||
|
r19908 | |||
|
r15655 | this.element.find('#trust_notebook').click(function () { | ||
|
r17198 | that.notebook.trust_notebook(); | ||
|
r15655 | }); | ||
|
r17198 | this.events.on('trust_changed.Notebook', function (event, trusted) { | ||
|
r15657 | if (trusted) { | ||
that.element.find('#trust_notebook') | ||||
|
r19917 | .addClass("disabled").off('click') | ||
|
r15657 | .find("a").text("Trusted Notebook"); | ||
} else { | ||||
that.element.find('#trust_notebook') | ||||
|
r19917 | .removeClass("disabled").on('click', function () { | ||
that.notebook.trust_notebook(); | ||||
}) | ||||
|
r15657 | .find("a").text("Trust Notebook"); | ||
} | ||||
}); | ||||
|
r19908 | |||
|
r6850 | this.element.find('#kill_and_exit').click(function () { | ||
|
r17649 | var close_window = function () { | ||
|
r19176 | /** | ||
* allow closing of new tabs in Chromium, impossible in FF | ||||
*/ | ||||
|
r13338 | window.open('', '_self', ''); | ||
window.close(); | ||||
|
r17649 | }; | ||
// finish with close on success or failure | ||||
that.notebook.session.delete(close_window, close_window); | ||||
|
r6850 | }); | ||
|
r19908 | |||
|
r5857 | // Edit | ||
|
r5879 | this.element.find('#cut_cell').click(function () { | ||
|
r17198 | that.notebook.cut_cell(); | ||
|
r5879 | }); | ||
this.element.find('#copy_cell').click(function () { | ||||
|
r17198 | that.notebook.copy_cell(); | ||
|
r5879 | }); | ||
|
r5857 | this.element.find('#delete_cell').click(function () { | ||
|
r17198 | that.notebook.delete_cell(); | ||
|
r5857 | }); | ||
|
r9551 | this.element.find('#undelete_cell').click(function () { | ||
|
r17198 | that.notebook.undelete_cell(); | ||
|
r9551 | }); | ||
|
r5896 | this.element.find('#split_cell').click(function () { | ||
|
r17198 | that.notebook.split_cell(); | ||
|
r5896 | }); | ||
this.element.find('#merge_cell_above').click(function () { | ||||
|
r17198 | that.notebook.merge_cell_above(); | ||
|
r5896 | }); | ||
this.element.find('#merge_cell_below').click(function () { | ||||
|
r17198 | that.notebook.merge_cell_below(); | ||
|
r5896 | }); | ||
|
r5857 | this.element.find('#move_cell_up').click(function () { | ||
|
r17198 | that.notebook.move_cell_up(); | ||
|
r5857 | }); | ||
this.element.find('#move_cell_down').click(function () { | ||||
|
r17198 | that.notebook.move_cell_down(); | ||
|
r5857 | }); | ||
|
r12913 | this.element.find('#edit_nb_metadata').click(function () { | ||
|
r17214 | that.notebook.edit_metadata({ | ||
notebook: that.notebook, | ||||
keyboard_manager: that.notebook.keyboard_manager}); | ||||
|
r12913 | }); | ||
|
r5994 | // View | ||
this.element.find('#toggle_header').click(function () { | ||||
|
r19902 | $('#header-container').toggle(); | ||
$('.header-bar').toggle(); | ||||
|
r19179 | that._size_header(); | ||
|
r5994 | }); | ||
this.element.find('#toggle_toolbar').click(function () { | ||||
|
r10906 | $('div#maintoolbar').toggle(); | ||
|
r19179 | that._size_header(); | ||
|
r5994 | }); | ||
|
r5857 | // Insert | ||
this.element.find('#insert_cell_above').click(function () { | ||||
|
r17198 | that.notebook.insert_cell_above('code'); | ||
that.notebook.select_prev(); | ||||
|
r5857 | }); | ||
this.element.find('#insert_cell_below').click(function () { | ||||
|
r17198 | that.notebook.insert_cell_below('code'); | ||
that.notebook.select_next(); | ||||
|
r5857 | }); | ||
// Cell | ||||
this.element.find('#run_cell').click(function () { | ||||
|
r17198 | that.notebook.execute_cell(); | ||
|
r5857 | }); | ||
|
r14085 | this.element.find('#run_cell_select_below').click(function () { | ||
|
r17198 | that.notebook.execute_cell_and_select_below(); | ||
|
r14085 | }); | ||
this.element.find('#run_cell_insert_below').click(function () { | ||||
|
r17198 | that.notebook.execute_cell_and_insert_below(); | ||
|
r5857 | }); | ||
this.element.find('#run_all_cells').click(function () { | ||||
|
r17198 | that.notebook.execute_all_cells(); | ||
|
r13159 | }); | ||
|
r8606 | this.element.find('#run_all_cells_above').click(function () { | ||
|
r17198 | that.notebook.execute_cells_above(); | ||
|
r13159 | }); | ||
|
r8606 | this.element.find('#run_all_cells_below').click(function () { | ||
|
r17198 | that.notebook.execute_cells_below(); | ||
|
r13159 | }); | ||
|
r14869 | this.element.find('#to_code').click(function () { | ||
|
r17198 | that.notebook.to_code(); | ||
|
r14869 | }); | ||
this.element.find('#to_markdown').click(function () { | ||||
|
r17198 | that.notebook.to_markdown(); | ||
|
r14869 | }); | ||
this.element.find('#to_raw').click(function () { | ||||
|
r17198 | that.notebook.to_raw(); | ||
|
r14869 | }); | ||
|
r14871 | |||
this.element.find('#toggle_current_output').click(function () { | ||||
|
r17198 | that.notebook.toggle_output(); | ||
|
r5857 | }); | ||
|
r14871 | this.element.find('#toggle_current_output_scroll').click(function () { | ||
|
r17198 | that.notebook.toggle_output_scroll(); | ||
|
r6017 | }); | ||
|
r14867 | this.element.find('#clear_current_output').click(function () { | ||
|
r17198 | that.notebook.clear_output(); | ||
|
r5857 | }); | ||
|
r14871 | |||
this.element.find('#toggle_all_output').click(function () { | ||||
|
r17198 | that.notebook.toggle_all_output(); | ||
|
r7362 | }); | ||
|
r14871 | this.element.find('#toggle_all_output_scroll').click(function () { | ||
|
r17198 | that.notebook.toggle_all_output_scroll(); | ||
|
r7362 | }); | ||
|
r5857 | this.element.find('#clear_all_output').click(function () { | ||
|
r17198 | that.notebook.clear_all_output(); | ||
|
r5857 | }); | ||
|
r14871 | |||
|
r14869 | // Kernel | ||
this.element.find('#int_kernel').click(function () { | ||||
|
r18204 | that.notebook.kernel.interrupt(); | ||
|
r14869 | }); | ||
this.element.find('#restart_kernel').click(function () { | ||||
|
r17198 | that.notebook.restart_kernel(); | ||
|
r14869 | }); | ||
|
r18731 | this.element.find('#reconnect_kernel').click(function () { | ||
that.notebook.kernel.reconnect(); | ||||
}); | ||||
|
r5858 | // Help | ||
|
r17198 | if (this.tour) { | ||
|
r16672 | this.element.find('#notebook_tour').click(function () { | ||
|
r17198 | that.tour.start(); | ||
|
r16672 | }); | ||
} else { | ||||
this.element.find('#notebook_tour').addClass("disabled"); | ||||
} | ||||
|
r5858 | this.element.find('#keyboard_shortcuts').click(function () { | ||
|
r17198 | that.quick_help.show_keyboard_shortcuts(); | ||
|
r5858 | }); | ||
|
r10503 | |||
this.update_restore_checkpoint(null); | ||||
|
r17198 | this.events.on('checkpoints_listed.Notebook', function (event, data) { | ||
that.update_restore_checkpoint(that.notebook.checkpoints); | ||||
|
r10503 | }); | ||
|
r17198 | this.events.on('checkpoint_created.Notebook', function (event, data) { | ||
that.update_restore_checkpoint(that.notebook.checkpoints); | ||||
|
r10503 | }); | ||
|
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); | ||||
|
r19732 | that.add_kernel_help_links(data.kernel.info_reply.help_links || []); | ||
|
r18967 | }); | ||
|
r5857 | }; | ||
|
r10520 | MenuBar.prototype.update_restore_checkpoint = function(checkpoints) { | ||
|
r11117 | var ul = this.element.find("#restore_checkpoint").find("ul"); | ||
ul.empty(); | ||||
|
r13103 | if (!checkpoints || checkpoints.length === 0) { | ||
|
r11117 | ul.append( | ||
$("<li/>") | ||||
.addClass("disabled") | ||||
.append( | ||||
$("<a/>") | ||||
.text("No checkpoints") | ||||
) | ||||
); | ||||
return; | ||||
|
r13103 | } | ||
|
r11164 | |||
|
r17198 | var that = this; | ||
|
r12050 | checkpoints.map(function (checkpoint) { | ||
|
r10520 | var d = new Date(checkpoint.last_modified); | ||
|
r11117 | ul.append( | ||
$("<li/>").append( | ||||
$("<a/>") | ||||
.attr("href", "#") | ||||
|
r17474 | .text(moment(d).format("LLLL")) | ||
|
r11117 | .click(function () { | ||
|
r17198 | that.notebook.restore_checkpoint_dialog(checkpoint); | ||
|
r11117 | }) | ||
) | ||||
); | ||||
|
r12050 | }); | ||
|
r10520 | }; | ||
|
r18967 | |||
MenuBar.prototype.update_nbconvert_script = function(langinfo) { | ||||
|
r19176 | /** | ||
* Set the 'Download as foo' menu option for the relevant language. | ||||
*/ | ||||
|
r18967 | var el = this.element.find('#download_script'); | ||
// Set menu entry text to e.g. "Python (.py)" | ||||
|
r19732 | var langname = (langinfo.name || 'Script'); | ||
langname = langname.charAt(0).toUpperCase()+langname.substr(1); // Capitalise | ||||
|
r19027 | el.find('a').text(langname + ' ('+(langinfo.file_extension || 'txt')+')'); | ||
|
r18967 | }; | ||
|
r5857 | |||
|
r19732 | MenuBar.prototype.add_kernel_help_links = function(help_links) { | ||
/** add links from kernel_info to the help menu */ | ||||
var divider = $("#kernel-help-links"); | ||||
if (divider.length === 0) { | ||||
// insert kernel help section above about link | ||||
var about = $("#notebook_about").parent(); | ||||
divider = $("<li>") | ||||
.attr('id', "kernel-help-links") | ||||
.addClass('divider'); | ||||
about.prev().before(divider); | ||||
} | ||||
// remove previous entries | ||||
while (!divider.next().hasClass('divider')) { | ||||
divider.next().remove(); | ||||
} | ||||
if (help_links.length === 0) { | ||||
// no help links, remove the divider | ||||
divider.remove(); | ||||
return; | ||||
} | ||||
var cursor = divider; | ||||
help_links.map(function (link) { | ||||
cursor.after($("<li>") | ||||
.append($("<a>") | ||||
.attr('target', '_blank') | ||||
.attr('title', 'Opens in a new window') | ||||
.attr('href', link.url) | ||||
.text(link.text) | ||||
.append($("<i>") | ||||
.addClass("fa fa-external-link menu-icon pull-right") | ||||
) | ||||
) | ||||
); | ||||
cursor = cursor.next(); | ||||
}); | ||||
}; | ||||
|
r17198 | // Backwards compatability. | ||
|
r5857 | IPython.MenuBar = MenuBar; | ||
|
r17201 | return {'MenuBar': MenuBar}; | ||
|
r17198 | }); | ||