menubar.js
332 lines
| 11.8 KiB
| application/javascript
|
JavascriptLexer
|
r5857 | //---------------------------------------------------------------------------- | ||
// Copyright (C) 2008-2011 The IPython Development Team | ||||
// | ||||
// Distributed under the terms of the BSD License. The full license is in | ||||
// the file COPYING, distributed as part of this software. | ||||
//---------------------------------------------------------------------------- | ||||
//============================================================================ | ||||
// MenuBar | ||||
//============================================================================ | ||||
|
r9501 | /** | ||
* @module IPython | ||||
* @namespace IPython | ||||
* @submodule MenuBar | ||||
*/ | ||||
|
r5857 | var IPython = (function (IPython) { | ||
|
r12103 | "use strict"; | ||
|
r13076 | |||
var utils = IPython.utils; | ||||
|
r5857 | |||
|
r9501 | /** | ||
|
r12995 | * A MenuBar Class to generate the menubar of IPython notebook | ||
|
r9501 | * @Class MenuBar | ||
* | ||||
* @constructor | ||||
* | ||||
* | ||||
* @param selector {string} selector for the menubar element in DOM | ||||
* @param {object} [options] | ||||
|
r15238 | * @param [options.base_url] {String} String to use for the | ||
|
r15234 | * base project url. Default is to inspect | ||
|
r15238 | * $('body').data('baseUrl'); | ||
|
r9501 | * does not support change for now is set through this option | ||
*/ | ||||
var MenuBar = function (selector, options) { | ||||
|
r13103 | options = options || {}; | ||
|
r15240 | this.base_url = options.base_url || IPython.utils.get_body_data("baseUrl"); | ||
|
r5857 | this.selector = selector; | ||
if (this.selector !== undefined) { | ||||
this.element = $(selector); | ||||
this.style(); | ||||
this.bind_events(); | ||||
} | ||||
}; | ||||
MenuBar.prototype.style = function () { | ||||
|
r6193 | this.element.addClass('border-box-sizing'); | ||
|
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. | ||
|
r5945 | var i = IPython.notebook.get_selected_index(); | ||
|
r5898 | IPython.notebook.select(i); | ||
} | ||||
|
r10888 | ); | ||
|
r5857 | }; | ||
|
r13829 | MenuBar.prototype._nbconvert = function (format, download) { | ||
download = download || false; | ||||
|
r15239 | var notebook_path = IPython.notebook.notebook_path; | ||
var notebook_name = IPython.notebook.notebook_name; | ||||
|
r13829 | if (IPython.notebook.dirty) { | ||
IPython.notebook.save_notebook({async : false}); | ||||
} | ||||
|
r15234 | var url = utils.url_join_encode( | ||
|
r15238 | this.base_url, | ||
|
r13829 | 'nbconvert', | ||
format, | ||||
|
r15239 | notebook_path, | ||
notebook_name | ||||
|
r13829 | ) + "?download=" + download.toString(); | ||
|
r13838 | window.open(url); | ||
|
r15234 | }; | ||
|
r5857 | |||
MenuBar.prototype.bind_events = function () { | ||||
// File | ||||
|
r9699 | var that = this; | ||
|
r12992 | this.element.find('#new_notebook').click(function () { | ||
|
r13016 | IPython.notebook.new_notebook(); | ||
|
r12992 | }); | ||
this.element.find('#open_notebook').click(function () { | ||||
|
r13693 | window.open(utils.url_join_encode( | ||
|
r15234 | IPython.notebook.base_url, | ||
|
r13103 | 'tree', | ||
|
r15234 | IPython.notebook.notebook_path | ||
|
r13103 | )); | ||
|
r12992 | }); | ||
this.element.find('#copy_notebook').click(function () { | ||||
|
r13017 | IPython.notebook.copy_notebook(); | ||
|
r12992 | return false; | ||
}); | ||||
this.element.find('#download_ipynb').click(function () { | ||||
|
r15234 | var base_url = IPython.notebook.base_url; | ||
var notebook_path = IPython.notebook.notebook_path; | ||||
var notebook_name = IPython.notebook.notebook_name; | ||||
|
r13076 | if (IPython.notebook.dirty) { | ||
IPython.notebook.save_notebook({async : false}); | ||||
} | ||||
|
r13693 | var url = utils.url_join_encode( | ||
|
r15234 | base_url, | ||
|
r13114 | 'files', | ||
|
r15234 | notebook_path, | ||
|
r15239 | notebook_name | ||
|
r13076 | ); | ||
|
r12992 | window.location.assign(url); | ||
}); | ||||
|
r13108 | |||
|
r13829 | this.element.find('#print_preview').click(function () { | ||
that._nbconvert('html', false); | ||||
}); | ||||
|
r12992 | this.element.find('#download_py').click(function () { | ||
|
r13829 | that._nbconvert('python', true); | ||
|
r12992 | }); | ||
|
r13829 | |||
this.element.find('#download_html').click(function () { | ||||
that._nbconvert('html', true); | ||||
|
r12992 | }); | ||
|
r13831 | |||
this.element.find('#download_rst').click(function () { | ||||
that._nbconvert('rst', true); | ||||
}); | ||||
|
r5859 | this.element.find('#rename_notebook').click(function () { | ||
IPython.save_widget.rename_notebook(); | ||||
}); | ||||
|
r10501 | this.element.find('#save_checkpoint').click(function () { | ||
IPython.notebook.save_checkpoint(); | ||||
}); | ||||
|
r10503 | this.element.find('#restore_checkpoint').click(function () { | ||
}); | ||||
|
r15655 | this.element.find('#trust_notebook').click(function () { | ||
IPython.notebook.trust_notebook(); | ||||
}); | ||||
|
r15657 | $([IPython.events]).on('trust_changed.Notebook', function (event, trusted) { | ||
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"); | ||||
} | ||||
}); | ||||
|
r6850 | this.element.find('#kill_and_exit').click(function () { | ||
|
r13146 | IPython.notebook.session.delete(); | ||
|
r13338 | setTimeout(function(){ | ||
// allow closing of new tabs in Chromium, impossible in FF | ||||
window.open('', '_self', ''); | ||||
window.close(); | ||||
}, 500); | ||||
|
r6850 | }); | ||
|
r5857 | // Edit | ||
|
r5879 | this.element.find('#cut_cell').click(function () { | ||
IPython.notebook.cut_cell(); | ||||
}); | ||||
this.element.find('#copy_cell').click(function () { | ||||
IPython.notebook.copy_cell(); | ||||
}); | ||||
|
r5857 | this.element.find('#delete_cell').click(function () { | ||
IPython.notebook.delete_cell(); | ||||
}); | ||||
|
r9551 | this.element.find('#undelete_cell').click(function () { | ||
|
r14032 | IPython.notebook.undelete_cell(); | ||
|
r9551 | }); | ||
|
r5896 | this.element.find('#split_cell').click(function () { | ||
IPython.notebook.split_cell(); | ||||
}); | ||||
this.element.find('#merge_cell_above').click(function () { | ||||
IPython.notebook.merge_cell_above(); | ||||
}); | ||||
this.element.find('#merge_cell_below').click(function () { | ||||
IPython.notebook.merge_cell_below(); | ||||
}); | ||||
|
r5857 | this.element.find('#move_cell_up').click(function () { | ||
IPython.notebook.move_cell_up(); | ||||
}); | ||||
this.element.find('#move_cell_down').click(function () { | ||||
IPython.notebook.move_cell_down(); | ||||
}); | ||||
|
r12913 | this.element.find('#edit_nb_metadata').click(function () { | ||
IPython.notebook.edit_metadata(); | ||||
}); | ||||
|
r5994 | // View | ||
this.element.find('#toggle_header').click(function () { | ||||
$('div#header').toggle(); | ||||
IPython.layout_manager.do_resize(); | ||||
}); | ||||
this.element.find('#toggle_toolbar').click(function () { | ||||
|
r10906 | $('div#maintoolbar').toggle(); | ||
IPython.layout_manager.do_resize(); | ||||
|
r5994 | }); | ||
|
r5857 | // Insert | ||
this.element.find('#insert_cell_above').click(function () { | ||||
|
r5945 | IPython.notebook.insert_cell_above('code'); | ||
|
r14029 | IPython.notebook.select_prev(); | ||
|
r5857 | }); | ||
this.element.find('#insert_cell_below').click(function () { | ||||
|
r5945 | IPython.notebook.insert_cell_below('code'); | ||
|
r14029 | IPython.notebook.select_next(); | ||
|
r5857 | }); | ||
// Cell | ||||
this.element.find('#run_cell').click(function () { | ||||
|
r14085 | IPython.notebook.execute_cell(); | ||
|
r5857 | }); | ||
|
r14085 | this.element.find('#run_cell_select_below').click(function () { | ||
IPython.notebook.execute_cell_and_select_below(); | ||||
}); | ||||
this.element.find('#run_cell_insert_below').click(function () { | ||||
IPython.notebook.execute_cell_and_insert_below(); | ||||
|
r5857 | }); | ||
this.element.find('#run_all_cells').click(function () { | ||||
IPython.notebook.execute_all_cells(); | ||||
|
r13159 | }); | ||
|
r8606 | this.element.find('#run_all_cells_above').click(function () { | ||
IPython.notebook.execute_cells_above(); | ||||
|
r13159 | }); | ||
|
r8606 | this.element.find('#run_all_cells_below').click(function () { | ||
IPython.notebook.execute_cells_below(); | ||||
|
r13159 | }); | ||
|
r14869 | this.element.find('#to_code').click(function () { | ||
IPython.notebook.to_code(); | ||||
}); | ||||
this.element.find('#to_markdown').click(function () { | ||||
IPython.notebook.to_markdown(); | ||||
}); | ||||
this.element.find('#to_raw').click(function () { | ||||
IPython.notebook.to_raw(); | ||||
}); | ||||
this.element.find('#to_heading1').click(function () { | ||||
IPython.notebook.to_heading(undefined, 1); | ||||
}); | ||||
this.element.find('#to_heading2').click(function () { | ||||
IPython.notebook.to_heading(undefined, 2); | ||||
}); | ||||
this.element.find('#to_heading3').click(function () { | ||||
IPython.notebook.to_heading(undefined, 3); | ||||
}); | ||||
this.element.find('#to_heading4').click(function () { | ||||
IPython.notebook.to_heading(undefined, 4); | ||||
}); | ||||
this.element.find('#to_heading5').click(function () { | ||||
IPython.notebook.to_heading(undefined, 5); | ||||
}); | ||||
this.element.find('#to_heading6').click(function () { | ||||
IPython.notebook.to_heading(undefined, 6); | ||||
}); | ||||
|
r14871 | |||
this.element.find('#toggle_current_output').click(function () { | ||||
IPython.notebook.toggle_output(); | ||||
|
r5857 | }); | ||
|
r14871 | this.element.find('#toggle_current_output_scroll').click(function () { | ||
IPython.notebook.toggle_output_scroll(); | ||||
|
r6017 | }); | ||
|
r14867 | this.element.find('#clear_current_output').click(function () { | ||
IPython.notebook.clear_output(); | ||||
|
r5857 | }); | ||
|
r14871 | |||
this.element.find('#toggle_all_output').click(function () { | ||||
IPython.notebook.toggle_all_output(); | ||||
|
r7362 | }); | ||
|
r14871 | this.element.find('#toggle_all_output_scroll').click(function () { | ||
IPython.notebook.toggle_all_output_scroll(); | ||||
|
r7362 | }); | ||
|
r5857 | this.element.find('#clear_all_output').click(function () { | ||
IPython.notebook.clear_all_output(); | ||||
}); | ||||
|
r14871 | |||
|
r14869 | // Kernel | ||
this.element.find('#int_kernel').click(function () { | ||||
IPython.notebook.session.interrupt_kernel(); | ||||
}); | ||||
this.element.find('#restart_kernel').click(function () { | ||||
IPython.notebook.restart_kernel(); | ||||
}); | ||||
|
r5858 | // Help | ||
this.element.find('#keyboard_shortcuts').click(function () { | ||||
IPython.quick_help.show_keyboard_shortcuts(); | ||||
}); | ||||
|
r10503 | |||
this.update_restore_checkpoint(null); | ||||
$([IPython.events]).on('checkpoints_listed.Notebook', function (event, data) { | ||||
|
r12050 | that.update_restore_checkpoint(IPython.notebook.checkpoints); | ||
|
r10503 | }); | ||
$([IPython.events]).on('checkpoint_created.Notebook', function (event, data) { | ||||
|
r12050 | that.update_restore_checkpoint(IPython.notebook.checkpoints); | ||
|
r10503 | }); | ||
|
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 | |||
|
r12050 | checkpoints.map(function (checkpoint) { | ||
|
r10520 | var d = new Date(checkpoint.last_modified); | ||
|
r11117 | ul.append( | ||
$("<li/>").append( | ||||
$("<a/>") | ||||
.attr("href", "#") | ||||
.text(d.format("mmm dd HH:MM:ss")) | ||||
.click(function () { | ||||
IPython.notebook.restore_checkpoint_dialog(checkpoint); | ||||
}) | ||||
) | ||||
); | ||||
|
r12050 | }); | ||
|
r10520 | }; | ||
|
r5857 | |||
IPython.MenuBar = MenuBar; | ||||
return IPython; | ||||
}(IPython)); | ||||