menubar.js
264 lines
| 9.9 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) { | ||
|
r9501 | /** | ||
* A MenuBar Class to generate the menubar of IPython noteboko | ||||
* @Class MenuBar | ||||
* | ||||
* @constructor | ||||
* | ||||
* | ||||
* @param selector {string} selector for the menubar element in DOM | ||||
* @param {object} [options] | ||||
* @param [options.baseProjectUrl] {String} String to use for the | ||||
* Base Project url, default would be to inspect | ||||
* $('body').data('baseProjectUrl'); | ||||
* does not support change for now is set through this option | ||||
*/ | ||||
var MenuBar = function (selector, options) { | ||||
var options = options || {}; | ||||
if(options.baseProjectUrl!= undefined){ | ||||
this._baseProjectUrl = options.baseProjectUrl; | ||||
} | ||||
|
r5857 | this.selector = selector; | ||
if (this.selector !== undefined) { | ||||
this.element = $(selector); | ||||
this.style(); | ||||
this.bind_events(); | ||||
} | ||||
}; | ||||
|
r9500 | MenuBar.prototype.baseProjectUrl = function(){ | ||
|
r9501 | return this._baseProjectUrl || $('body').data('baseProjectUrl'); | ||
|
r9503 | }; | ||
|
r9500 | |||
|
r5857 | |||
MenuBar.prototype.style = function () { | ||||
|
r6193 | this.element.addClass('border-box-sizing'); | ||
|
r5898 | $('ul#menus').menubar({ | ||
select : 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); | ||
} | ||||
}); | ||||
|
r10520 | this.element.find("#restore_checkpoint").find("ul").find("li").hide(); | ||
|
r5857 | }; | ||
MenuBar.prototype.bind_events = function () { | ||||
// File | ||||
|
r9699 | var that = this; | ||
|
r5857 | this.element.find('#new_notebook').click(function () { | ||
|
r9699 | window.open(that.baseProjectUrl()+'new'); | ||
|
r5857 | }); | ||
this.element.find('#open_notebook').click(function () { | ||||
|
r9699 | window.open(that.baseProjectUrl()); | ||
|
r5857 | }); | ||
|
r5859 | this.element.find('#rename_notebook').click(function () { | ||
IPython.save_widget.rename_notebook(); | ||||
}); | ||||
|
r5860 | this.element.find('#copy_notebook').click(function () { | ||
|
r6047 | var notebook_id = IPython.notebook.get_notebook_id(); | ||
|
r9699 | var url = that.baseProjectUrl() + notebook_id + '/copy'; | ||
|
r7838 | window.open(url,'_blank'); | ||
return false; | ||||
|
r5860 | }); | ||
|
r10501 | this.element.find('#save_checkpoint').click(function () { | ||
IPython.notebook.save_checkpoint(); | ||||
}); | ||||
|
r10503 | this.element.find('#restore_checkpoint').click(function () { | ||
}); | ||||
|
r5857 | this.element.find('#download_ipynb').click(function () { | ||
|
r6047 | var notebook_id = IPython.notebook.get_notebook_id(); | ||
|
r9699 | var url = that.baseProjectUrl() + 'notebooks/' + | ||
|
r5857 | notebook_id + '?format=json'; | ||
|
r8870 | window.location.assign(url); | ||
|
r5857 | }); | ||
this.element.find('#download_py').click(function () { | ||||
|
r6047 | var notebook_id = IPython.notebook.get_notebook_id(); | ||
|
r9699 | var url = that.baseProjectUrl() + 'notebooks/' + | ||
|
r5857 | notebook_id + '?format=py'; | ||
|
r8870 | window.location.assign(url); | ||
|
r5857 | }); | ||
|
r6850 | this.element.find('#kill_and_exit').click(function () { | ||
IPython.notebook.kernel.kill(); | ||||
setTimeout(function(){window.close();}, 200); | ||||
}); | ||||
|
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 () { | ||
IPython.notebook.undelete(); | ||||
}); | ||||
|
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(); | ||||
}); | ||||
|
r5858 | this.element.find('#select_previous').click(function () { | ||
IPython.notebook.select_prev(); | ||||
}); | ||||
this.element.find('#select_next').click(function () { | ||||
IPython.notebook.select_next(); | ||||
}); | ||||
|
r5994 | // View | ||
this.element.find('#toggle_header').click(function () { | ||||
$('div#header').toggle(); | ||||
IPython.layout_manager.do_resize(); | ||||
}); | ||||
this.element.find('#toggle_toolbar').click(function () { | ||||
IPython.toolbar.toggle(); | ||||
}); | ||||
|
r5857 | // Insert | ||
this.element.find('#insert_cell_above').click(function () { | ||||
|
r5945 | IPython.notebook.insert_cell_above('code'); | ||
|
r5857 | }); | ||
this.element.find('#insert_cell_below').click(function () { | ||||
|
r5945 | IPython.notebook.insert_cell_below('code'); | ||
|
r5857 | }); | ||
// Cell | ||||
this.element.find('#run_cell').click(function () { | ||||
IPython.notebook.execute_selected_cell(); | ||||
}); | ||||
this.element.find('#run_cell_in_place').click(function () { | ||||
IPython.notebook.execute_selected_cell({terminal:true}); | ||||
}); | ||||
this.element.find('#run_all_cells').click(function () { | ||||
IPython.notebook.execute_all_cells(); | ||||
|
r8607 | }).attr('title', 'Run all cells in the notebook'); | ||
|
r8606 | this.element.find('#run_all_cells_above').click(function () { | ||
IPython.notebook.execute_cells_above(); | ||||
|
r8607 | }).attr('title', 'Run all cells above (but not including) this cell'); | ||
|
r8606 | this.element.find('#run_all_cells_below').click(function () { | ||
IPython.notebook.execute_cells_below(); | ||||
|
r8607 | }).attr('title', 'Run this cell and all cells below it'); | ||
|
r5857 | this.element.find('#to_code').click(function () { | ||
IPython.notebook.to_code(); | ||||
}); | ||||
this.element.find('#to_markdown').click(function () { | ||||
IPython.notebook.to_markdown(); | ||||
}); | ||||
|
r6248 | this.element.find('#to_raw').click(function () { | ||
IPython.notebook.to_raw(); | ||||
|
r6017 | }); | ||
|
r6019 | 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); | ||||
}); | ||||
|
r5857 | this.element.find('#toggle_output').click(function () { | ||
IPython.notebook.toggle_output(); | ||||
}); | ||||
|
r7362 | this.element.find('#collapse_all_output').click(function () { | ||
IPython.notebook.collapse_all_output(); | ||||
}); | ||||
this.element.find('#scroll_all_output').click(function () { | ||||
IPython.notebook.scroll_all_output(); | ||||
}); | ||||
this.element.find('#expand_all_output').click(function () { | ||||
IPython.notebook.expand_all_output(); | ||||
}); | ||||
|
r5857 | this.element.find('#clear_all_output').click(function () { | ||
IPython.notebook.clear_all_output(); | ||||
}); | ||||
// Kernel | ||||
this.element.find('#int_kernel').click(function () { | ||||
IPython.notebook.kernel.interrupt(); | ||||
}); | ||||
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) { | ||||
that.update_restore_checkpoint(data); | ||||
}); | ||||
$([IPython.events]).on('checkpoint_created.Notebook', function (event, data) { | ||||
|
r10520 | that.update_restore_checkpoint([data]); | ||
|
r10503 | }); | ||
|
r5857 | }; | ||
|
r10520 | MenuBar.prototype.update_restore_checkpoint = function(checkpoints) { | ||
if (! checkpoints) { | ||||
checkpoints = []; | ||||
|
r10503 | }; | ||
|
r10520 | this.element.find("#restore_checkpoint").find("ul").find("li").each(function(i) { | ||
var li = $(this); | ||||
var a = li.find("a"); | ||||
a.off("click"); | ||||
if (checkpoints.length <= i) { | ||||
li.hide(); | ||||
return; | ||||
} else { | ||||
li.show(); | ||||
}; | ||||
var checkpoint = checkpoints[i]; | ||||
var d = new Date(checkpoint.last_modified); | ||||
li.find('a').text( | ||||
d.format("mmm dd HH:MM:ss") | ||||
).click(function () { | ||||
IPython.notebook.restore_checkpoint_dialog(checkpoint); | ||||
}); | ||||
}); | ||||
}; | ||||
|
r5857 | |||
IPython.MenuBar = MenuBar; | ||||
return IPython; | ||||
}(IPython)); | ||||