//---------------------------------------------------------------------------- // 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. //---------------------------------------------------------------------------- //============================================================================ // Notebook //============================================================================ var IPython = (function (IPython) { var utils = IPython.utils; var Notebook = function (selector) { this.read_only = IPython.read_only; this.element = $(selector); this.element.scroll(); this.element.data("notebook", this); this.next_prompt_number = 1; this.kernel = null; this.clipboard = null; this.dirty = false; this.msg_cell_map = {}; this.metadata = {}; this.control_key_active = false; this.style(); this.create_elements(); this.bind_events(); this.set_tooltipontab(true); this.set_smartcompleter(true); this.set_timebeforetooltip(1200); this.set_autoindent(true); }; Notebook.prototype.style = function () { $('div#notebook').addClass('border-box-sizing'); }; Notebook.prototype.create_elements = function () { // We add this end_space div to the end of the notebook div to: // i) provide a margin between the last cell and the end of the notebook // ii) to prevent the div from scrolling up when the last cell is being // edited, but is too low on the page, which browsers will do automatically. var that = this; var end_space = $('
').height("30%"); end_space.dblclick(function (e) { if (that.read_only) return; var ncells = that.ncells(); that.insert_code_cell_below(ncells-1); }); this.element.append(end_space); $('div#notebook').addClass('border-box-sizing'); }; Notebook.prototype.bind_events = function () { var that = this; $(document).keydown(function (event) { // console.log(event); if (that.read_only) return true; if (event.which === 27) { // Intercept escape at highest level to avoid closing // websocket connection with firefox event.preventDefault(); } if (event.which === 38 && !event.shiftKey) { var cell = that.selected_cell(); if (cell.at_top()) { event.preventDefault(); that.select_prev(); }; } else if (event.which === 40 && !event.shiftKey) { var cell = that.selected_cell(); if (cell.at_bottom()) { event.preventDefault(); that.select_next(); }; } else if (event.which === 13 && event.shiftKey) { that.execute_selected_cell(); return false; } else if (event.which === 13 && event.ctrlKey) { that.execute_selected_cell({terminal:true}); return false; } else if (event.which === 77 && event.ctrlKey) { that.control_key_active = true; return false; } else if (event.which === 68 && that.control_key_active) { // Delete selected cell = d that.delete_cell(); that.control_key_active = false; return false; } else if (event.which === 65 && that.control_key_active) { // Insert code cell above selected = a that.insert_code_cell_above(); that.control_key_active = false; return false; } else if (event.which === 66 && that.control_key_active) { // Insert code cell below selected = b that.insert_code_cell_below(); that.control_key_active = false; return false; } else if (event.which === 67 && that.control_key_active) { // To code = c that.to_code(); that.control_key_active = false; return false; } else if (event.which === 77 && that.control_key_active) { // To markdown = m that.to_markdown(); that.control_key_active = false; return false; } else if (event.which === 84 && that.control_key_active) { // Toggle output = t that.toggle_output(); that.control_key_active = false; return false; } else if (event.which === 83 && that.control_key_active) { // Save notebook = s IPython.save_widget.save_notebook(); that.control_key_active = false; return false; } else if (event.which === 74 && that.control_key_active) { // Move cell down = j that.move_cell_down(); that.control_key_active = false; return false; } else if (event.which === 75 && that.control_key_active) { // Move cell up = k that.move_cell_up(); that.control_key_active = false; return false; } else if (event.which === 80 && that.control_key_active) { // Select previous = p that.select_prev(); that.control_key_active = false; return false; } else if (event.which === 78 && that.control_key_active) { // Select next = n that.select_next(); that.control_key_active = false; return false; } else if (event.which === 76 && that.control_key_active) { // Toggle line numbers = l that.cell_toggle_line_numbers(); that.control_key_active = false; return false; } else if (event.which === 73 && that.control_key_active) { // Interrupt kernel = i IPython.notebook.kernel.interrupt(); that.control_key_active = false; return false; } else if (event.which === 190 && that.control_key_active) { // Restart kernel = . # matches qt console IPython.notebook.restart_kernel(); that.control_key_active = false; return false; } else if (event.which === 72 && that.control_key_active) { // Show keyboard shortcuts = h IPython.quick_help.show_keyboard_shortcuts(); that.control_key_active = false; return false; } else if (that.control_key_active) { that.control_key_active = false; return true; }; return true; }); this.element.bind('collapse_pager', function () { var app_height = $('div#main_app').height(); // content height var splitter_height = $('div#pager_splitter').outerHeight(true); var new_height = app_height - splitter_height; that.element.animate({height : new_height + 'px'}, 'fast'); }); this.element.bind('expand_pager', function () { var app_height = $('div#main_app').height(); // content height var splitter_height = $('div#pager_splitter').outerHeight(true); var pager_height = $('div#pager').outerHeight(true); var new_height = app_height - pager_height - splitter_height; that.element.animate({height : new_height + 'px'}, 'fast'); }); this.element.bind('collapse_left_panel', function () { var splitter_width = $('div#left_panel_splitter').outerWidth(true); var new_margin = splitter_width; $('div#notebook_panel').animate({marginLeft : new_margin + 'px'}, 'fast'); }); this.element.bind('expand_left_panel', function () { var splitter_width = $('div#left_panel_splitter').outerWidth(true); var left_panel_width = IPython.left_panel.width; var new_margin = splitter_width + left_panel_width; $('div#notebook_panel').animate({marginLeft : new_margin + 'px'}, 'fast'); }); $(window).bind('beforeunload', function () { // TODO: Make killing the kernel configurable. var kill_kernel = false; if (kill_kernel) { that.kernel.kill(); } if (that.dirty && ! that.read_only) { return "You have unsaved changes that will be lost if you leave this page."; }; // Null is the *only* return value that will make the browser not // pop up the "don't leave" dialog. return null; }); }; Notebook.prototype.scroll_to_bottom = function () { this.element.animate({scrollTop:this.element.get(0).scrollHeight}, 0); }; Notebook.prototype.scroll_to_top = function () { this.element.animate({scrollTop:0}, 0); }; // Cell indexing, retrieval, etc. Notebook.prototype.cell_elements = function () { return this.element.children("div.cell"); }; Notebook.prototype.ncells = function (cell) { return this.cell_elements().length; }; // TODO: we are often calling cells as cells()[i], which we should optimize // to cells(i) or a new method. Notebook.prototype.cells = function () { return this.cell_elements().toArray().map(function (e) { return $(e).data("cell"); }); }; Notebook.prototype.find_cell_index = function (cell) { var result = null; this.cell_elements().filter(function (index) { if ($(this).data("cell") === cell) { result = index; }; }); return result; }; Notebook.prototype.index_or_selected = function (index) { return index || this.selected_index() || 0; }; Notebook.prototype.select = function (index) { if (index !== undefined && index >= 0 && index < this.ncells()) { if (this.selected_index() !== null) { this.selected_cell().unselect(); }; this.cells()[index].select(); }; return this; }; Notebook.prototype.select_next = function () { var index = this.selected_index(); if (index !== null && index >= 0 && (index+1) < this.ncells()) { this.select(index+1); }; return this; }; Notebook.prototype.select_prev = function () { var index = this.selected_index(); if (index !== null && index >= 0 && (index-1) < this.ncells()) { this.select(index-1); }; return this; }; Notebook.prototype.selected_index = function () { var result = null; this.cell_elements().filter(function (index) { if ($(this).data("cell").selected === true) { result = index; }; }); return result; }; Notebook.prototype.cell_for_msg = function (msg_id) { var cell_id = this.msg_cell_map[msg_id]; var result = null; this.cell_elements().filter(function (index) { cell = $(this).data("cell"); if (cell.cell_id === cell_id) { result = cell; }; }); return result; }; Notebook.prototype.selected_cell = function () { return this.cell_elements().eq(this.selected_index()).data("cell"); }; // Cell insertion, deletion and moving. Notebook.prototype.delete_cell = function (index) { var i = index || this.selected_index(); if (i !== null && i >= 0 && i < this.ncells()) { this.cell_elements().eq(i).remove(); if (i === (this.ncells())) { this.select(i-1); } else { this.select(i); }; }; this.dirty = true; return this; }; Notebook.prototype.append_cell = function (cell) { this.element.find('div.end_space').before(cell.element); this.dirty = true; return this; }; Notebook.prototype.insert_cell_below = function (cell, index) { var ncells = this.ncells(); if (ncells === 0) { this.append_cell(cell); return this; }; if (index >= 0 && index < ncells) { this.cell_elements().eq(index).after(cell.element); }; this.dirty = true; return this; }; Notebook.prototype.insert_cell_above = function (cell, index) { var ncells = this.ncells(); if (ncells === 0) { this.append_cell(cell); return this; }; if (index >= 0 && index < ncells) { this.cell_elements().eq(index).before(cell.element); }; this.dirty = true; return this; }; Notebook.prototype.move_cell_up = function (index) { var i = index || this.selected_index(); if (i !== null && i < this.ncells() && i > 0) { var pivot = this.cell_elements().eq(i-1); var tomove = this.cell_elements().eq(i); if (pivot !== null && tomove !== null) { tomove.detach(); pivot.before(tomove); this.select(i-1); }; }; this.dirty = true; return this; }; Notebook.prototype.move_cell_down = function (index) { var i = index || this.selected_index(); if (i !== null && i < (this.ncells()-1) && i >= 0) { var pivot = this.cell_elements().eq(i+1); var tomove = this.cell_elements().eq(i); if (pivot !== null && tomove !== null) { tomove.detach(); pivot.after(tomove); this.select(i+1); }; }; this.dirty = true; return this; }; Notebook.prototype.sort_cells = function () { var ncells = this.ncells(); var sindex = this.selected_index(); var swapped; do { swapped = false; for (var i=1; i