//---------------------------------------------------------------------------- // Copyright (C) 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) { "use strict"; var utils = IPython.utils; var key = IPython.utils.keycodes; /** * A notebook contains and manages cells. * * @class Notebook * @constructor * @param {String} selector A jQuery selector for the notebook's DOM element * @param {Object} [options] A config object */ var Notebook = function (selector, options) { var options = options || {}; this._baseProjectUrl = options.baseProjectUrl; this.notebook_path = options.notebookPath; this.notebook_name = options.notebookName; this.element = $(selector); this.element.scroll(); this.element.data("notebook", this); this.next_prompt_number = 1; this.session = null; this.kernel = null; this.clipboard = null; this.undelete_backup = null; this.undelete_index = null; this.undelete_below = false; this.paste_enabled = false; this.set_dirty(false); this.metadata = {}; this._checkpoint_after_save = false; this.last_checkpoint = null; this.checkpoints = []; this.autosave_interval = 0; this.autosave_timer = null; // autosave *at most* every two minutes this.minimum_autosave_interval = 120000; // single worksheet for now this.worksheet_metadata = {}; this.control_key_active = false; this.notebook_name_blacklist_re = /[\/\\:]/; this.nbformat = 3 // Increment this when changing the nbformat this.nbformat_minor = 0 // Increment this when changing the nbformat this.style(); this.create_elements(); this.bind_events(); }; /** * Tweak the notebook's CSS style. * * @method style */ Notebook.prototype.style = function () { $('div#notebook').addClass('border-box-sizing'); }; /** * Get the root URL of the notebook server. * * @method baseProjectUrl * @return {String} The base project URL */ Notebook.prototype.baseProjectUrl = function(){ return this._baseProjectUrl || $('body').data('baseProjectUrl'); }; Notebook.prototype.notebookName = function() { var name = $('body').data('notebookName'); name = decodeURIComponent(name); return name; }; Notebook.prototype.notebookPath = function() { var path = $('body').data('notebookPath'); path = decodeURIComponent(path); return path }; /** * Create an HTML and CSS representation of the notebook. * * @method create_elements */ 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; this.container = $("
").addClass("container").attr("id", "notebook-container"); var end_space = $('').addClass('end_space'); end_space.dblclick(function (e) { var ncells = that.ncells(); that.insert_cell_below('code',ncells-1); }); this.element.append(this.container); this.container.append(end_space); $('div#notebook').addClass('border-box-sizing'); }; /** * Bind JavaScript events: key presses and custom IPython events. * * @method bind_events */ Notebook.prototype.bind_events = function () { var that = this; $([IPython.events]).on('set_next_input.Notebook', function (event, data) { var index = that.find_cell_index(data.cell); var new_cell = that.insert_cell_below('code',index); new_cell.set_text(data.text); that.dirty = true; }); $([IPython.events]).on('set_dirty.Notebook', function (event, data) { that.dirty = data.value; }); $([IPython.events]).on('select.Cell', function (event, data) { var index = that.find_cell_index(data.cell); that.select(index); }); $([IPython.events]).on('status_autorestarting.Kernel', function () { IPython.dialog.modal({ title: "Kernel Restarting", body: "The kernel appears to have died. It will restart automatically.", buttons: { OK : { class : "btn-primary" } } }); }); $(document).keydown(function (event) { // Save (CTRL+S) or (AppleKey+S) //metaKey = applekey on mac if ((event.ctrlKey || event.metaKey) && event.keyCode==83) { that.save_checkpoint(); event.preventDefault(); return false; } else if (event.which === key.ESC) { // Intercept escape at highest level to avoid closing // websocket connection with firefox IPython.pager.collapse(); event.preventDefault(); } else if (event.which === key.SHIFT) { // ignore shift keydown return true; } if (event.which === key.UPARROW && !event.shiftKey) { var cell = that.get_selected_cell(); if (cell && cell.at_top()) { event.preventDefault(); that.select_prev(); }; } else if (event.which === key.DOWNARROW && !event.shiftKey) { var cell = that.get_selected_cell(); if (cell && cell.at_bottom()) { event.preventDefault(); that.select_next(); }; } else if (event.which === key.ENTER && event.shiftKey) { that.execute_selected_cell(); return false; } else if (event.which === key.ENTER && event.altKey) { // Execute code cell, and insert new in place that.execute_selected_cell(); // Only insert a new cell, if we ended up in an already populated cell if (/\S/.test(that.get_selected_cell().get_text()) == true) { that.insert_cell_above('code'); } return false; } else if (event.which === key.ENTER && event.ctrlKey) { that.execute_selected_cell({terminal:true}); return false; } else if (event.which === 77 && event.ctrlKey && that.control_key_active == false) { that.control_key_active = true; return false; } else if (event.which === 88 && that.control_key_active) { // Cut selected cell = x that.cut_cell(); that.control_key_active = false; return false; } else if (event.which === 67 && that.control_key_active) { // Copy selected cell = c that.copy_cell(); that.control_key_active = false; return false; } else if (event.which === 86 && that.control_key_active) { // Paste below selected cell = v that.paste_cell_below(); that.control_key_active = false; 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_cell_above('code'); that.control_key_active = false; return false; } else if (event.which === 66 && that.control_key_active) { // Insert code cell below selected = b that.insert_cell_below('code'); that.control_key_active = false; return false; } else if (event.which === 89 && that.control_key_active) { // To code = y 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) { // To Raw = t that.to_raw(); that.control_key_active = false; return false; } else if (event.which === 49 && that.control_key_active) { // To Heading 1 = 1 that.to_heading(undefined, 1); that.control_key_active = false; return false; } else if (event.which === 50 && that.control_key_active) { // To Heading 2 = 2 that.to_heading(undefined, 2); that.control_key_active = false; return false; } else if (event.which === 51 && that.control_key_active) { // To Heading 3 = 3 that.to_heading(undefined, 3); that.control_key_active = false; return false; } else if (event.which === 52 && that.control_key_active) { // To Heading 4 = 4 that.to_heading(undefined, 4); that.control_key_active = false; return false; } else if (event.which === 53 && that.control_key_active) { // To Heading 5 = 5 that.to_heading(undefined, 5); that.control_key_active = false; return false; } else if (event.which === 54 && that.control_key_active) { // To Heading 6 = 6 that.to_heading(undefined, 6); that.control_key_active = false; return false; } else if (event.which === 79 && that.control_key_active) { // Toggle output = o if (event.shiftKey){ that.toggle_output_scroll(); } else { that.toggle_output(); } that.control_key_active = false; return false; } else if (event.which === 83 && that.control_key_active) { // Save notebook = s that.save_checkpoint(); 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 that.session.interrupt_kernel(); that.control_key_active = false; return false; } else if (event.which === 190 && that.control_key_active) { // Restart kernel = . # matches qt console that.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 (event.which === 90 && that.control_key_active) { // Undo last cell delete = z that.undelete(); that.control_key_active = false; return false; } else if ((event.which === 189 || event.which === 173) && that.control_key_active) { // how fun! '-' is 189 in Chrome, but 173 in FF and Opera // Split cell = - that.split_cell(); that.control_key_active = false; return false; } else if (that.control_key_active) { that.control_key_active = false; return true; } return true; }); var collapse_time = function(time){ var app_height = $('#ipython-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'}, time); } this.element.bind('collapse_pager', function (event,extrap) { var time = (extrap != undefined) ? ((extrap.duration != undefined ) ? extrap.duration : 'fast') : 'fast'; collapse_time(time); }); var expand_time = function(time) { var app_height = $('#ipython-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'}, time); } this.element.bind('expand_pager', function (event, extrap) { var time = (extrap != undefined) ? ((extrap.duration != undefined ) ? extrap.duration : 'fast') : 'fast'; expand_time(time); }); // Firefox 22 broke $(window).on("beforeunload") // I'm not sure why or how. window.onbeforeunload = function (e) { // TODO: Make killing the kernel configurable. var kill_kernel = false; if (kill_kernel) { that.session.kill_kernel(); } // if we are autosaving, trigger an autosave on nav-away. // still warn, because if we don't the autosave may fail. if (that.dirty) { if ( that.autosave_interval ) { // schedule autosave in a timeout // this gives you a chance to forcefully discard changes // by reloading the page if you *really* want to. // the timer doesn't start until you *dismiss* the dialog. setTimeout(function () { if (that.dirty) { that.save_notebook(); } }, 1000); return "Autosave in progress, latest changes may be lost."; } else { return "Unsaved changes will be lost."; } }; // Null is the *only* return value that will make the browser not // pop up the "don't leave" dialog. return null; }; }; /** * Set the dirty flag, and trigger the set_dirty.Notebook event * * @method set_dirty */ Notebook.prototype.set_dirty = function (value) { if (value === undefined) { value = true; } if (this.dirty == value) { return; } $([IPython.events]).trigger('set_dirty.Notebook', {value: value}); }; /** * Scroll the top of the page to a given cell. * * @method scroll_to_cell * @param {Number} cell_number An index of the cell to view * @param {Number} time Animation time in milliseconds * @return {Number} Pixel offset from the top of the container */ Notebook.prototype.scroll_to_cell = function (cell_number, time) { var cells = this.get_cells(); var time = time || 0; cell_number = Math.min(cells.length-1,cell_number); cell_number = Math.max(0 ,cell_number); var scroll_value = cells[cell_number].element.position().top-cells[0].element.position().top ; this.element.animate({scrollTop:scroll_value}, time); return scroll_value; }; /** * Scroll to the bottom of the page. * * @method scroll_to_bottom */ Notebook.prototype.scroll_to_bottom = function () { this.element.animate({scrollTop:this.element.get(0).scrollHeight}, 0); }; /** * Scroll to the top of the page. * * @method scroll_to_top */ Notebook.prototype.scroll_to_top = function () { this.element.animate({scrollTop:0}, 0); }; // Edit Notebook metadata Notebook.prototype.edit_metadata = function () { var that = this; IPython.dialog.edit_metadata(this.metadata, function (md) { that.metadata = md; }, 'Notebook'); }; // Cell indexing, retrieval, etc. /** * Get all cell elements in the notebook. * * @method get_cell_elements * @return {jQuery} A selector of all cell elements */ Notebook.prototype.get_cell_elements = function () { return this.container.children("div.cell"); }; /** * Get a particular cell element. * * @method get_cell_element * @param {Number} index An index of a cell to select * @return {jQuery} A selector of the given cell. */ Notebook.prototype.get_cell_element = function (index) { var result = null; var e = this.get_cell_elements().eq(index); if (e.length !== 0) { result = e; } return result; }; /** * Count the cells in this notebook. * * @method ncells * @return {Number} The number of cells in this notebook */ Notebook.prototype.ncells = function () { return this.get_cell_elements().length; }; /** * Get all Cell objects in this notebook. * * @method get_cells * @return {Array} This notebook's Cell objects */ // TODO: we are often calling cells as cells()[i], which we should optimize // to cells(i) or a new method. Notebook.prototype.get_cells = function () { return this.get_cell_elements().toArray().map(function (e) { return $(e).data("cell"); }); }; /** * Get a Cell object from this notebook. * * @method get_cell * @param {Number} index An index of a cell to retrieve * @return {Cell} A particular cell */ Notebook.prototype.get_cell = function (index) { var result = null; var ce = this.get_cell_element(index); if (ce !== null) { result = ce.data('cell'); } return result; } /** * Get the cell below a given cell. * * @method get_next_cell * @param {Cell} cell The provided cell * @return {Cell} The next cell */ Notebook.prototype.get_next_cell = function (cell) { var result = null; var index = this.find_cell_index(cell); if (this.is_valid_cell_index(index+1)) { result = this.get_cell(index+1); } return result; } /** * Get the cell above a given cell. * * @method get_prev_cell * @param {Cell} cell The provided cell * @return {Cell} The previous cell */ Notebook.prototype.get_prev_cell = function (cell) { // TODO: off-by-one // nb.get_prev_cell(nb.get_cell(1)) is null var result = null; var index = this.find_cell_index(cell); if (index !== null && index > 1) { result = this.get_cell(index-1); } return result; } /** * Get the numeric index of a given cell. * * @method find_cell_index * @param {Cell} cell The provided cell * @return {Number} The cell's numeric index */ Notebook.prototype.find_cell_index = function (cell) { var result = null; this.get_cell_elements().filter(function (index) { if ($(this).data("cell") === cell) { result = index; }; }); return result; }; /** * Get a given index , or the selected index if none is provided. * * @method index_or_selected * @param {Number} index A cell's index * @return {Number} The given index, or selected index if none is provided. */ Notebook.prototype.index_or_selected = function (index) { var i; if (index === undefined || index === null) { i = this.get_selected_index(); if (i === null) { i = 0; } } else { i = index; } return i; }; /** * Get the currently selected cell. * @method get_selected_cell * @return {Cell} The selected cell */ Notebook.prototype.get_selected_cell = function () { var index = this.get_selected_index(); return this.get_cell(index); }; /** * Check whether a cell index is valid. * * @method is_valid_cell_index * @param {Number} index A cell index * @return True if the index is valid, false otherwise */ Notebook.prototype.is_valid_cell_index = function (index) { if (index !== null && index >= 0 && index < this.ncells()) { return true; } else { return false; }; } /** * Get the index of the currently selected cell. * @method get_selected_index * @return {Number} The selected cell's numeric index */ Notebook.prototype.get_selected_index = function () { var result = null; this.get_cell_elements().filter(function (index) { if ($(this).data("cell").selected === true) { result = index; }; }); return result; }; // Cell selection. /** * Programmatically select a cell. * * @method select * @param {Number} index A cell's index * @return {Notebook} This notebook */ Notebook.prototype.select = function (index) { if (this.is_valid_cell_index(index)) { var sindex = this.get_selected_index() if (sindex !== null && index !== sindex) { this.get_cell(sindex).unselect(); }; var cell = this.get_cell(index); cell.select(); if (cell.cell_type === 'heading') { $([IPython.events]).trigger('selected_cell_type_changed.Notebook', {'cell_type':cell.cell_type,level:cell.level} ); } else { $([IPython.events]).trigger('selected_cell_type_changed.Notebook', {'cell_type':cell.cell_type} ); }; }; return this; }; /** * Programmatically select the next cell. * * @method select_next * @return {Notebook} This notebook */ Notebook.prototype.select_next = function () { var index = this.get_selected_index(); this.select(index+1); return this; }; /** * Programmatically select the previous cell. * * @method select_prev * @return {Notebook} This notebook */ Notebook.prototype.select_prev = function () { var index = this.get_selected_index(); this.select(index-1); return this; }; // Cell movement /** * Move given (or selected) cell up and select it. * * @method move_cell_up * @param [index] {integer} cell index * @return {Notebook} This notebook **/ Notebook.prototype.move_cell_up = function (index) { var i = this.index_or_selected(index); if (this.is_valid_cell_index(i) && i > 0) { var pivot = this.get_cell_element(i-1); var tomove = this.get_cell_element(i); if (pivot !== null && tomove !== null) { tomove.detach(); pivot.before(tomove); this.select(i-1); }; this.set_dirty(true); }; return this; }; /** * Move given (or selected) cell down and select it * * @method move_cell_down * @param [index] {integer} cell index * @return {Notebook} This notebook **/ Notebook.prototype.move_cell_down = function (index) { var i = this.index_or_selected(index); if ( this.is_valid_cell_index(i) && this.is_valid_cell_index(i+1)) { var pivot = this.get_cell_element(i+1); var tomove = this.get_cell_element(i); if (pivot !== null && tomove !== null) { tomove.detach(); pivot.after(tomove); this.select(i+1); }; }; this.set_dirty(); return this; }; // Insertion, deletion. /** * Delete a cell from the notebook. * * @method delete_cell * @param [index] A cell's numeric index * @return {Notebook} This notebook */ Notebook.prototype.delete_cell = function (index) { var i = this.index_or_selected(index); var cell = this.get_selected_cell(); this.undelete_backup = cell.toJSON(); $('#undelete_cell').removeClass('disabled'); if (this.is_valid_cell_index(i)) { var ce = this.get_cell_element(i); ce.remove(); if (i === (this.ncells())) { this.select(i-1); this.undelete_index = i - 1; this.undelete_below = true; } else { this.select(i); this.undelete_index = i; this.undelete_below = false; }; $([IPython.events]).trigger('delete.Cell', {'cell': cell, 'index': i}); this.set_dirty(true); }; return this; }; /** * Insert a cell so that after insertion the cell is at given index. * * Similar to insert_above, but index parameter is mandatory * * Index will be brought back into the accissible range [0,n] * * @method insert_cell_at_index * @param type {string} in ['code','markdown','heading'] * @param [index] {int} a valid index where to inser cell * * @return cell {cell|null} created cell or null **/ Notebook.prototype.insert_cell_at_index = function(type, index){ var ncells = this.ncells(); var index = Math.min(index,ncells); index = Math.max(index,0); var cell = null; if (ncells === 0 || this.is_valid_cell_index(index) || index === ncells) { if (type === 'code') { cell = new IPython.CodeCell(this.kernel); cell.set_input_prompt(); } else if (type === 'markdown') { cell = new IPython.MarkdownCell(); } else if (type === 'raw') { cell = new IPython.RawCell(); } else if (type === 'heading') { cell = new IPython.HeadingCell(); } if(this._insert_element_at_index(cell.element,index)){ cell.render(); this.select(this.find_cell_index(cell)); $([IPython.events]).trigger('create.Cell', {'cell': cell, 'index': index}); this.set_dirty(true); } } return cell; }; /** * Insert an element at given cell index. * * @method _insert_element_at_index * @param element {dom element} a cell element * @param [index] {int} a valid index where to inser cell * @private * * return true if everything whent fine. **/ Notebook.prototype._insert_element_at_index = function(element, index){ if (element === undefined){ return false; } var ncells = this.ncells(); if (ncells === 0) { // special case append if empty this.element.find('div.end_space').before(element); } else if ( ncells === index ) { // special case append it the end, but not empty this.get_cell_element(index-1).after(element); } else if (this.is_valid_cell_index(index)) { // otherwise always somewhere to append to this.get_cell_element(index).before(element); } else { return false; } if (this.undelete_index !== null && index <= this.undelete_index) { this.undelete_index = this.undelete_index + 1; this.set_dirty(true); } return true; }; /** * Insert a cell of given type above given index, or at top * of notebook if index smaller than 0. * * default index value is the one of currently selected cell * * @method insert_cell_above * @param type {string} cell type * @param [index] {integer} * * @return handle to created cell or null **/ Notebook.prototype.insert_cell_above = function (type, index) { index = this.index_or_selected(index); return this.insert_cell_at_index(type, index); }; /** * Insert a cell of given type below given index, or at bottom * of notebook if index greater thatn number of cell * * default index value is the one of currently selected cell * * @method insert_cell_below * @param type {string} cell type * @param [index] {integer} * * @return handle to created cell or null * **/ Notebook.prototype.insert_cell_below = function (type, index) { index = this.index_or_selected(index); return this.insert_cell_at_index(type, index+1); }; /** * Insert cell at end of notebook * * @method insert_cell_at_bottom * @param {String} type cell type * * @return the added cell; or null **/ Notebook.prototype.insert_cell_at_bottom = function (type){ var len = this.ncells(); return this.insert_cell_below(type,len-1); }; /** * Turn a cell into a code cell. * * @method to_code * @param {Number} [index] A cell's index */ Notebook.prototype.to_code = function (index) { var i = this.index_or_selected(index); if (this.is_valid_cell_index(i)) { var source_element = this.get_cell_element(i); var source_cell = source_element.data("cell"); if (!(source_cell instanceof IPython.CodeCell)) { var target_cell = this.insert_cell_below('code',i); var text = source_cell.get_text(); if (text === source_cell.placeholder) { text = ''; } target_cell.set_text(text); // make this value the starting point, so that we can only undo // to this state, instead of a blank cell target_cell.code_mirror.clearHistory(); source_element.remove(); this.set_dirty(true); }; }; }; /** * Turn a cell into a Markdown cell. * * @method to_markdown * @param {Number} [index] A cell's index */ Notebook.prototype.to_markdown = function (index) { var i = this.index_or_selected(index); if (this.is_valid_cell_index(i)) { var source_element = this.get_cell_element(i); var source_cell = source_element.data("cell"); if (!(source_cell instanceof IPython.MarkdownCell)) { var target_cell = this.insert_cell_below('markdown',i); var text = source_cell.get_text(); if (text === source_cell.placeholder) { text = ''; }; // The edit must come before the set_text. target_cell.edit(); target_cell.set_text(text); // make this value the starting point, so that we can only undo // to this state, instead of a blank cell target_cell.code_mirror.clearHistory(); source_element.remove(); this.set_dirty(true); }; }; }; /** * Turn a cell into a raw text cell. * * @method to_raw * @param {Number} [index] A cell's index */ Notebook.prototype.to_raw = function (index) { var i = this.index_or_selected(index); if (this.is_valid_cell_index(i)) { var source_element = this.get_cell_element(i); var source_cell = source_element.data("cell"); var target_cell = null; if (!(source_cell instanceof IPython.RawCell)) { target_cell = this.insert_cell_below('raw',i); var text = source_cell.get_text(); if (text === source_cell.placeholder) { text = ''; }; // The edit must come before the set_text. target_cell.edit(); target_cell.set_text(text); // make this value the starting point, so that we can only undo // to this state, instead of a blank cell target_cell.code_mirror.clearHistory(); source_element.remove(); this.set_dirty(true); }; }; }; /** * Turn a cell into a heading cell. * * @method to_heading * @param {Number} [index] A cell's index * @param {Number} [level] A heading level (e.g., 1 becomes <h1>) */ Notebook.prototype.to_heading = function (index, level) { level = level || 1; var i = this.index_or_selected(index); if (this.is_valid_cell_index(i)) { var source_element = this.get_cell_element(i); var source_cell = source_element.data("cell"); var target_cell = null; if (source_cell instanceof IPython.HeadingCell) { source_cell.set_level(level); } else { target_cell = this.insert_cell_below('heading',i); var text = source_cell.get_text(); if (text === source_cell.placeholder) { text = ''; }; // The edit must come before the set_text. target_cell.set_level(level); target_cell.edit(); target_cell.set_text(text); // make this value the starting point, so that we can only undo // to this state, instead of a blank cell target_cell.code_mirror.clearHistory(); source_element.remove(); this.set_dirty(true); }; $([IPython.events]).trigger('selected_cell_type_changed.Notebook', {'cell_type':'heading',level:level} ); }; }; // Cut/Copy/Paste /** * Enable UI elements for pasting cells. * * @method enable_paste */ Notebook.prototype.enable_paste = function () { var that = this; if (!this.paste_enabled) { $('#paste_cell_replace').removeClass('disabled') .on('click', function () {that.paste_cell_replace();}); $('#paste_cell_above').removeClass('disabled') .on('click', function () {that.paste_cell_above();}); $('#paste_cell_below').removeClass('disabled') .on('click', function () {that.paste_cell_below();}); this.paste_enabled = true; }; }; /** * Disable UI elements for pasting cells. * * @method disable_paste */ Notebook.prototype.disable_paste = function () { if (this.paste_enabled) { $('#paste_cell_replace').addClass('disabled').off('click'); $('#paste_cell_above').addClass('disabled').off('click'); $('#paste_cell_below').addClass('disabled').off('click'); this.paste_enabled = false; }; }; /** * Cut a cell. * * @method cut_cell */ Notebook.prototype.cut_cell = function () { this.copy_cell(); this.delete_cell(); } /** * Copy a cell. * * @method copy_cell */ Notebook.prototype.copy_cell = function () { var cell = this.get_selected_cell(); this.clipboard = cell.toJSON(); this.enable_paste(); }; /** * Replace the selected cell with a cell in the clipboard. * * @method paste_cell_replace */ Notebook.prototype.paste_cell_replace = function () { if (this.clipboard !== null && this.paste_enabled) { var cell_data = this.clipboard; var new_cell = this.insert_cell_above(cell_data.cell_type); new_cell.fromJSON(cell_data); var old_cell = this.get_next_cell(new_cell); this.delete_cell(this.find_cell_index(old_cell)); this.select(this.find_cell_index(new_cell)); }; }; /** * Paste a cell from the clipboard above the selected cell. * * @method paste_cell_above */ Notebook.prototype.paste_cell_above = function () { if (this.clipboard !== null && this.paste_enabled) { var cell_data = this.clipboard; var new_cell = this.insert_cell_above(cell_data.cell_type); new_cell.fromJSON(cell_data); }; }; /** * Paste a cell from the clipboard below the selected cell. * * @method paste_cell_below */ Notebook.prototype.paste_cell_below = function () { if (this.clipboard !== null && this.paste_enabled) { var cell_data = this.clipboard; var new_cell = this.insert_cell_below(cell_data.cell_type); new_cell.fromJSON(cell_data); }; }; // Cell undelete /** * Restore the most recently deleted cell. * * @method undelete */ Notebook.prototype.undelete = function() { if (this.undelete_backup !== null && this.undelete_index !== null) { var current_index = this.get_selected_index(); if (this.undelete_index < current_index) { current_index = current_index + 1; } if (this.undelete_index >= this.ncells()) { this.select(this.ncells() - 1); } else { this.select(this.undelete_index); } var cell_data = this.undelete_backup; var new_cell = null; if (this.undelete_below) { new_cell = this.insert_cell_below(cell_data.cell_type); } else { new_cell = this.insert_cell_above(cell_data.cell_type); } new_cell.fromJSON(cell_data); this.select(current_index); this.undelete_backup = null; this.undelete_index = null; } $('#undelete_cell').addClass('disabled'); } // Split/merge /** * Split the selected cell into two, at the cursor. * * @method split_cell */ Notebook.prototype.split_cell = function () { // Todo: implement spliting for other cell types. var cell = this.get_selected_cell(); if (cell.is_splittable()) { var texta = cell.get_pre_cursor(); var textb = cell.get_post_cursor(); if (cell instanceof IPython.CodeCell) { cell.set_text(textb); var new_cell = this.insert_cell_above('code'); new_cell.set_text(texta); this.select_next(); } else if (cell instanceof IPython.MarkdownCell) { cell.set_text(textb); cell.render(); var new_cell = this.insert_cell_above('markdown'); new_cell.edit(); // editor must be visible to call set_text new_cell.set_text(texta); new_cell.render(); this.select_next(); } }; }; /** * Combine the selected cell into the cell above it. * * @method merge_cell_above */ Notebook.prototype.merge_cell_above = function () { var index = this.get_selected_index(); var cell = this.get_cell(index); if (!cell.is_mergeable()) { return; } if (index > 0) { var upper_cell = this.get_cell(index-1); if (!upper_cell.is_mergeable()) { return; } var upper_text = upper_cell.get_text(); var text = cell.get_text(); if (cell instanceof IPython.CodeCell) { cell.set_text(upper_text+'\n'+text); } else if (cell instanceof IPython.MarkdownCell) { cell.edit(); cell.set_text(upper_text+'\n'+text); cell.render(); }; this.delete_cell(index-1); this.select(this.find_cell_index(cell)); }; }; /** * Combine the selected cell into the cell below it. * * @method merge_cell_below */ Notebook.prototype.merge_cell_below = function () { var index = this.get_selected_index(); var cell = this.get_cell(index); if (!cell.is_mergeable()) { return; } if (index < this.ncells()-1) { var lower_cell = this.get_cell(index+1); if (!lower_cell.is_mergeable()) { return; } var lower_text = lower_cell.get_text(); var text = cell.get_text(); if (cell instanceof IPython.CodeCell) { cell.set_text(text+'\n'+lower_text); } else if (cell instanceof IPython.MarkdownCell) { cell.edit(); cell.set_text(text+'\n'+lower_text); cell.render(); }; this.delete_cell(index+1); this.select(this.find_cell_index(cell)); }; }; // Cell collapsing and output clearing /** * Hide a cell's output. * * @method collapse * @param {Number} index A cell's numeric index */ Notebook.prototype.collapse = function (index) { var i = this.index_or_selected(index); this.get_cell(i).collapse(); this.set_dirty(true); }; /** * Show a cell's output. * * @method expand * @param {Number} index A cell's numeric index */ Notebook.prototype.expand = function (index) { var i = this.index_or_selected(index); this.get_cell(i).expand(); this.set_dirty(true); }; /** Toggle whether a cell's output is collapsed or expanded. * * @method toggle_output * @param {Number} index A cell's numeric index */ Notebook.prototype.toggle_output = function (index) { var i = this.index_or_selected(index); this.get_cell(i).toggle_output(); this.set_dirty(true); }; /** * Toggle a scrollbar for long cell outputs. * * @method toggle_output_scroll * @param {Number} index A cell's numeric index */ Notebook.prototype.toggle_output_scroll = function (index) { var i = this.index_or_selected(index); this.get_cell(i).toggle_output_scroll(); }; /** * Hide each code cell's output area. * * @method collapse_all_output */ Notebook.prototype.collapse_all_output = function () { var ncells = this.ncells(); var cells = this.get_cells(); for (var i=0; i