Show More
notebook.js
2479 lines
| 83.0 KiB
| application/javascript
|
JavascriptLexer
Jonathan Frederic
|
r17192 | // Copyright (c) IPython Development Team. | ||
// Distributed under the terms of the Modified BSD License. | ||||
Jonathan Frederic
|
r19503 | /** | ||
* @module notebook | ||||
*/ | ||||
Matthias Bussonnier
|
r20210 | define(function (require) { | ||
Matthias Bussonnier
|
r18991 | "use strict"; | ||
Matthias Bussonnier
|
r20210 | var IPython = require('base/js/namespace'); | ||
var $ = require('jquery'); | ||||
var utils = require('base/js/utils'); | ||||
var dialog = require('base/js/dialog'); | ||||
var cellmod = require('notebook/js/cell'); | ||||
var textcell = require('notebook/js/textcell'); | ||||
var codecell = require('notebook/js/codecell'); | ||||
Matthias Bussonnier
|
r20211 | var moment = require('moment'); | ||
Matthias Bussonnier
|
r20210 | var configmod = require('services/config'); | ||
var session = require('services/sessions/session'); | ||||
var celltoolbar = require('notebook/js/celltoolbar'); | ||||
var marked = require('components/marked/lib/marked'); | ||||
var CodeMirror = require('codemirror/lib/codemirror'); | ||||
var runMode = require('codemirror/addon/runmode/runmode'); | ||||
var mathjaxutils = require('notebook/js/mathjaxutils'); | ||||
var keyboard = require('base/js/keyboard'); | ||||
var tooltip = require('notebook/js/tooltip'); | ||||
var default_celltoolbar = require('notebook/js/celltoolbarpresets/default'); | ||||
var rawcell_celltoolbar = require('notebook/js/celltoolbarpresets/rawcell'); | ||||
var slideshow_celltoolbar = require('notebook/js/celltoolbarpresets/slideshow'); | ||||
var scrollmanager = require('notebook/js/scrollmanager'); | ||||
Jonathan Frederic
|
r17192 | |||
Jonathan Frederic
|
r19503 | /** | ||
Jonathan Frederic
|
r19505 | * Contains and manages cells. | ||
Jonathan Frederic
|
r19511 | * | ||
Jonathan Frederic
|
r19505 | * @class Notebook | ||
Jonathan Frederic
|
r19511 | * @param {string} selector | ||
* @param {object} options - Dictionary of keyword arguments. | ||||
* @param {jQuery} options.events - selector of Events | ||||
* @param {KeyboardManager} options.keyboard_manager | ||||
* @param {Contents} options.contents | ||||
* @param {SaveWidget} options.save_widget | ||||
* @param {object} options.config | ||||
* @param {string} options.base_url | ||||
* @param {string} options.notebook_path | ||||
* @param {string} options.notebook_name | ||||
Jonathan Frederic
|
r19503 | */ | ||
jon
|
r17210 | var Notebook = function (selector, options) { | ||
Thomas Kluyver
|
r19529 | this.config = options.config; | ||
this.class_config = new configmod.ConfigWithDefaults(this.config, | ||||
Notebook.options_default, 'Notebook'); | ||||
jon
|
r17210 | this.base_url = options.base_url; | ||
this.notebook_path = options.notebook_path; | ||||
this.notebook_name = options.notebook_name; | ||||
this.events = options.events; | ||||
this.keyboard_manager = options.keyboard_manager; | ||||
Jeff Hemmelgarn
|
r18643 | this.contents = options.contents; | ||
jon
|
r17210 | this.save_widget = options.save_widget; | ||
Jonathan Frederic
|
r17216 | this.tooltip = new tooltip.Tooltip(this.events); | ||
MinRK
|
r17308 | this.ws_url = options.ws_url; | ||
MinRK
|
r17649 | this._session_starting = false; | ||
Min RK
|
r20188 | this.last_modified = null; | ||
Jonathan Frederic
|
r17869 | |||
Jonathan Frederic
|
r17872 | // Create default scroll manager. | ||
Jonathan Frederic
|
r17880 | this.scroll_manager = new scrollmanager.ScrollManager(this); | ||
Jonathan Frederic
|
r17872 | |||
Jonathan Frederic
|
r17204 | // TODO: This code smells (and the other `= this` line a couple lines down) | ||
// We need a better way to deal with circular instance references. | ||||
jon
|
r17210 | this.keyboard_manager.notebook = this; | ||
this.save_widget.notebook = this; | ||||
Jonathan Frederic
|
r17198 | |||
mathjaxutils.init(); | ||||
Jonathan Frederic
|
r17194 | |||
if (marked) { | ||||
marked.setOptions({ | ||||
gfm : true, | ||||
tables: true, | ||||
MinRK
|
r18864 | // FIXME: probably want central config for CodeMirror theme when we have js config | ||
langPrefix: "cm-s-ipython language-", | ||||
highlight: function(code, lang, callback) { | ||||
Jonathan Frederic
|
r17194 | if (!lang) { | ||
// no language, no highlight | ||||
MinRK
|
r18864 | if (callback) { | ||
callback(null, code); | ||||
return; | ||||
} else { | ||||
return code; | ||||
} | ||||
Jonathan Frederic
|
r17194 | } | ||
Nicholas Bollweg (Nick)
|
r19286 | utils.requireCodeMirrorMode(lang, function (spec) { | ||
MinRK
|
r18864 | var el = document.createElement("div"); | ||
Nicholas Bollweg (Nick)
|
r19286 | var mode = CodeMirror.getMode({}, spec); | ||
MinRK
|
r18864 | if (!mode) { | ||
console.log("No CodeMirror mode: " + lang); | ||||
callback(null, code); | ||||
return; | ||||
} | ||||
try { | ||||
Nicholas Bollweg (Nick)
|
r19286 | CodeMirror.runMode(code, spec, el); | ||
MinRK
|
r18864 | callback(null, el.innerHTML); | ||
} catch (err) { | ||||
Min RK
|
r19005 | console.log("Failed to highlight " + lang + " code", err); | ||
MinRK
|
r18864 | callback(err, code); | ||
} | ||||
}, function (err) { | ||||
console.log("No CodeMirror mode: " + lang); | ||||
callback(err, code); | ||||
}); | ||||
Jonathan Frederic
|
r17194 | } | ||
}); | ||||
} | ||||
Brian E. Granger
|
r4352 | this.element = $(selector); | ||
this.element.scroll(); | ||||
this.element.data("notebook", this); | ||||
this.next_prompt_number = 1; | ||||
Zachary Sailer
|
r12986 | this.session = null; | ||
Brian E. Granger
|
r4352 | this.kernel = null; | ||
Brian Granger
|
r5879 | this.clipboard = null; | ||
David Warde-Farley
|
r8691 | this.undelete_backup = null; | ||
this.undelete_index = null; | ||||
this.undelete_below = false; | ||||
Brian Granger
|
r5912 | this.paste_enabled = false; | ||
Min RK
|
r19005 | this.writable = false; | ||
Brian E. Granger
|
r14021 | // It is important to start out in command mode to match the intial mode | ||
// of the KeyboardManager. | ||||
Brian E. Granger
|
r14015 | this.mode = 'command'; | ||
MinRK
|
r10781 | this.set_dirty(false); | ||
Brian E. Granger
|
r4637 | this.metadata = {}; | ||
MinRK
|
r10501 | this._checkpoint_after_save = false; | ||
this.last_checkpoint = null; | ||||
MinRK
|
r12050 | this.checkpoints = []; | ||
MinRK
|
r10505 | this.autosave_interval = 0; | ||
this.autosave_timer = null; | ||||
MinRK
|
r10507 | // autosave *at most* every two minutes | ||
this.minimum_autosave_interval = 120000; | ||||
Brian Granger
|
r7229 | this.notebook_name_blacklist_re = /[\/\\:]/; | ||
MinRK
|
r18584 | this.nbformat = 4; // Increment this when changing the nbformat | ||
Min RK
|
r19107 | this.nbformat_minor = this.current_nbformat_minor = 0; // Increment this when changing the nbformat | ||
Matthias BUSSONNIER
|
r17425 | this.codemirror_mode = 'ipython'; | ||
Brian E. Granger
|
r4364 | this.create_elements(); | ||
Brian E. Granger
|
r4352 | this.bind_events(); | ||
Matthias Bussonnier
|
r18991 | this.kernel_selector = null; | ||
this.dirty = null; | ||||
this.trusted = null; | ||||
this._fully_loaded = false; | ||||
Jonathan Frederic
|
r17217 | |||
// Trigger cell toolbar registration. | ||||
Matthias BUSSONNIER
|
r17454 | default_celltoolbar.register(this); | ||
rawcell_celltoolbar.register(this); | ||||
slideshow_celltoolbar.register(this); | ||||
Matthias Bussonnier
|
r18991 | |||
// prevent assign to miss-typed properties. | ||||
Object.seal(this); | ||||
Brian E. Granger
|
r4352 | }; | ||
Jonathan Frederic
|
r17869 | |||
MinRK
|
r17785 | Notebook.options_default = { | ||
// can be any cell type, or the special values of | ||||
// 'above', 'below', or 'selected' to get the value from another cell. | ||||
Thomas Kluyver
|
r19530 | default_cell_type: 'code' | ||
MinRK
|
r17785 | }; | ||
Brian Granger
|
r4292 | |||
David Wyde
|
r10033 | /** | ||
* Create an HTML and CSS representation of the notebook. | ||||
*/ | ||||
Brian E. Granger
|
r4364 | Notebook.prototype.create_elements = function () { | ||
Brian E. Granger
|
r14018 | var that = this; | ||
this.element.attr('tabindex','-1'); | ||||
this.container = $("<div/>").addClass("container").attr("id", "notebook-container"); | ||||
Brian E. Granger
|
r4364 | // 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. | ||||
MinRK
|
r10938 | var end_space = $('<div/>').addClass('end_space'); | ||
Brian E. Granger
|
r4628 | end_space.dblclick(function (e) { | ||
var ncells = that.ncells(); | ||||
Brian Granger
|
r5945 | that.insert_cell_below('code',ncells-1); | ||
Brian E. Granger
|
r4628 | }); | ||
MinRK
|
r10909 | this.element.append(this.container); | ||
Matthias Bussonnier
|
r19872 | this.container.after(end_space); | ||
Brian E. Granger
|
r4364 | }; | ||
David Wyde
|
r10033 | /** | ||
* Bind JavaScript events: key presses and custom IPython events. | ||||
*/ | ||||
Brian E. Granger
|
r4352 | Notebook.prototype.bind_events = function () { | ||
var that = this; | ||||
Matthias BUSSONNIER
|
r7170 | |||
Jonathan Frederic
|
r17195 | this.events.on('set_next_input.Notebook', function (event, data) { | ||
Thomas Kluyver
|
r19250 | if (data.replace) { | ||
data.cell.set_text(data.text); | ||||
Thomas Kluyver
|
r19252 | data.cell.clear_output(); | ||
Thomas Kluyver
|
r19250 | } else { | ||
var index = that.find_cell_index(data.cell); | ||||
var new_cell = that.insert_cell_below('code',index); | ||||
new_cell.set_text(data.text); | ||||
} | ||||
Brian Granger
|
r7168 | that.dirty = true; | ||
}); | ||||
Min RK
|
r19107 | this.events.on('unrecognized_cell.Cell', function () { | ||
that.warn_nbformat_minor(); | ||||
}); | ||||
this.events.on('unrecognized_output.OutputArea', function () { | ||||
that.warn_nbformat_minor(); | ||||
}); | ||||
Jonathan Frederic
|
r17195 | this.events.on('set_dirty.Notebook', function (event, data) { | ||
Brian Granger
|
r7228 | that.dirty = data.value; | ||
}); | ||||
MinRK
|
r18196 | this.events.on('trust_changed.Notebook', function (event, trusted) { | ||
that.trusted = trusted; | ||||
MinRK
|
r15657 | }); | ||
Jonathan Frederic
|
r17195 | this.events.on('select.Cell', function (event, data) { | ||
Brian Granger
|
r7168 | var index = that.find_cell_index(data.cell); | ||
Matthias BUSSONNIER
|
r7170 | that.select(index); | ||
Brian Granger
|
r7168 | }); | ||
Brian E. Granger
|
r14015 | |||
Jonathan Frederic
|
r17195 | this.events.on('edit_mode.Cell', function (event, data) { | ||
Brian E. Granger
|
r15629 | that.handle_edit_mode(data.cell); | ||
Brian E. Granger
|
r14015 | }); | ||
Brian E. Granger
|
r14016 | |||
Jonathan Frederic
|
r17195 | this.events.on('command_mode.Cell', function (event, data) { | ||
Brian E. Granger
|
r15629 | that.handle_command_mode(data.cell); | ||
Brian E. Granger
|
r14016 | }); | ||
Thomas Kluyver
|
r17380 | |||
this.events.on('spec_changed.Kernel', function(event, data) { | ||||
Min RK
|
r20191 | that.metadata.kernelspec = { | ||
name: data.name, | ||||
display_name: data.spec.display_name, | ||||
Min RK
|
r20195 | language: data.spec.language, | ||
Min RK
|
r20191 | }; | ||
Min RK
|
r19886 | // start session if the current session isn't already correct | ||
Min RK
|
r20290 | if (!(that.session && that.session.kernel && that.session.kernel.name === data.name)) { | ||
Min RK
|
r19886 | that.start_session(data.name); | ||
} | ||||
Thomas Kluyver
|
r18468 | }); | ||
this.events.on('kernel_ready.Kernel', function(event, data) { | ||||
Min RK
|
r18752 | var kinfo = data.kernel.info_reply; | ||
Min RK
|
r20048 | if (!kinfo.language_info) { | ||
delete that.metadata.language_info; | ||||
return; | ||||
} | ||||
var langinfo = kinfo.language_info; | ||||
Thomas Kluyver
|
r18468 | that.metadata.language_info = langinfo; | ||
Thomas Kluyver
|
r18380 | // Mode 'null' should be plain, unhighlighted text. | ||
Min RK
|
r19021 | var cm_mode = langinfo.codemirror_mode || langinfo.name || 'null'; | ||
Thomas Kluyver
|
r18380 | that.set_codemirror_mode(cm_mode); | ||
Thomas Kluyver
|
r17380 | }); | ||
Brian Granger
|
r7168 | |||
Brian E. Granger
|
r14015 | var collapse_time = function (time) { | ||
Bussonnier Matthias
|
r9265 | var app_height = $('#ipython-main-app').height(); // content height | ||
Brian E. Granger
|
r4362 | var splitter_height = $('div#pager_splitter').outerHeight(true); | ||
var new_height = app_height - splitter_height; | ||||
Matthias BUSSONNIER
|
r6723 | that.element.animate({height : new_height + 'px'}, time); | ||
Brian E. Granger
|
r14015 | }; | ||
Matthias BUSSONNIER
|
r6723 | |||
Brian E. Granger
|
r14015 | this.element.bind('collapse_pager', function (event, extrap) { | ||
MinRK
|
r15236 | var time = (extrap !== undefined) ? ((extrap.duration !== undefined ) ? extrap.duration : 'fast') : 'fast'; | ||
Matthias BUSSONNIER
|
r6723 | collapse_time(time); | ||
Brian E. Granger
|
r4361 | }); | ||
Brian E. Granger
|
r14015 | var expand_time = function (time) { | ||
Bussonnier Matthias
|
r9265 | var app_height = $('#ipython-main-app').height(); // content height | ||
Brian E. Granger
|
r4362 | var splitter_height = $('div#pager_splitter').outerHeight(true); | ||
Brian E. Granger
|
r4361 | var pager_height = $('div#pager').outerHeight(true); | ||
Mikhail Korobov
|
r8839 | var new_height = app_height - pager_height - splitter_height; | ||
Matthias BUSSONNIER
|
r6723 | that.element.animate({height : new_height + 'px'}, time); | ||
Brian E. Granger
|
r14015 | }; | ||
Matthias BUSSONNIER
|
r6723 | |||
this.element.bind('expand_pager', function (event, extrap) { | ||||
MinRK
|
r15236 | var time = (extrap !== undefined) ? ((extrap.duration !== undefined ) ? extrap.duration : 'fast') : 'fast'; | ||
Matthias BUSSONNIER
|
r6723 | expand_time(time); | ||
Brian E. Granger
|
r4361 | }); | ||
MinRK
|
r11111 | |||
// Firefox 22 broke $(window).on("beforeunload") | ||||
// I'm not sure why or how. | ||||
window.onbeforeunload = function (e) { | ||||
Brian Granger
|
r5857 | // TODO: Make killing the kernel configurable. | ||
var kill_kernel = false; | ||||
Brian E. Granger
|
r4542 | if (kill_kernel) { | ||
Jessica B. Hamrick
|
r18219 | that.session.delete(); | ||
Brian E. Granger
|
r4550 | } | ||
MinRK
|
r11047 | // if we are autosaving, trigger an autosave on nav-away. | ||
// still warn, because if we don't the autosave may fail. | ||||
MinRK
|
r11644 | if (that.dirty) { | ||
MinRK
|
r11047 | if ( that.autosave_interval ) { | ||
MinRK
|
r11625 | // 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); | ||||
MinRK
|
r11048 | return "Autosave in progress, latest changes may be lost."; | ||
} else { | ||||
return "Unsaved changes will be lost."; | ||||
MinRK
|
r11047 | } | ||
MinRK
|
r15236 | } | ||
RickWinter
|
r20938 | // IE treats null as a string. Instead just return which will avoid the dialog. | ||
return; | ||||
MinRK
|
r11111 | }; | ||
Brian E. Granger
|
r4352 | }; | ||
Min RK
|
r19107 | |||
Jonathan Frederic
|
r19508 | /** | ||
Jonathan Frederic
|
r19509 | * Trigger a warning dialog about missing functionality from newer minor versions | ||
Jonathan Frederic
|
r19508 | */ | ||
Min RK
|
r19107 | Notebook.prototype.warn_nbformat_minor = function (event) { | ||
var v = 'v' + this.nbformat + '.'; | ||||
var orig_vs = v + this.nbformat_minor; | ||||
var this_vs = v + this.current_nbformat_minor; | ||||
var msg = "This notebook is version " + orig_vs + ", but we only fully support up to " + | ||||
this_vs + ". You can still work with this notebook, but cell and output types " + | ||||
"introduced in later notebook versions will not be available."; | ||||
dialog.modal({ | ||||
notebook: this, | ||||
keyboard_manager: this.keyboard_manager, | ||||
title : "Newer Notebook", | ||||
body : msg, | ||||
buttons : { | ||||
OK : { | ||||
"class" : "btn-danger" | ||||
} | ||||
} | ||||
}); | ||||
Jonathan Frederic
|
r19510 | }; | ||
Brian Granger
|
r4292 | |||
David Wyde
|
r10033 | /** | ||
MinRK
|
r10781 | * Set the dirty flag, and trigger the set_dirty.Notebook event | ||
*/ | ||||
Notebook.prototype.set_dirty = function (value) { | ||||
if (value === undefined) { | ||||
value = true; | ||||
} | ||||
Matthias Bussonnier
|
r19739 | if (this.dirty === value) { | ||
MinRK
|
r10781 | return; | ||
} | ||||
Jonathan Frederic
|
r17195 | this.events.trigger('set_dirty.Notebook', {value: value}); | ||
MinRK
|
r10781 | }; | ||
/** | ||||
David Wyde
|
r10033 | * Scroll the top of the page to a given cell. | ||
* | ||||
Jonathan Frederic
|
r19511 | * @param {integer} index - An index of the cell to view | ||
* @param {integer} time - Animation time in milliseconds | ||||
* @return {integer} Pixel offset from the top of the container | ||||
David Wyde
|
r10033 | */ | ||
Jonathan Frederic
|
r19511 | Notebook.prototype.scroll_to_cell = function (index, time) { | ||
Matthias BUSSONNIER
|
r8419 | var cells = this.get_cells(); | ||
MinRK
|
r15236 | time = time || 0; | ||
Jonathan Frederic
|
r19511 | index = Math.min(cells.length-1,index); | ||
index = Math.max(0 ,index); | ||||
var scroll_value = cells[index].element.position().top-cells[0].element.position().top ; | ||||
Min RK
|
r19733 | this.scroll_manager.element.animate({scrollTop:scroll_value}, time); | ||
Matthias BUSSONNIER
|
r8419 | return scroll_value; | ||
}; | ||||
David Wyde
|
r10033 | /** | ||
* Scroll to the bottom of the page. | ||||
*/ | ||||
Brian E. Granger
|
r4364 | Notebook.prototype.scroll_to_bottom = function () { | ||
Min RK
|
r19733 | this.scroll_manager.element.animate({scrollTop:this.element.get(0).scrollHeight}, 0); | ||
Brian E. Granger
|
r4364 | }; | ||
David Wyde
|
r10033 | /** | ||
* Scroll to the top of the page. | ||||
*/ | ||||
Brian E. Granger
|
r4488 | Notebook.prototype.scroll_to_top = function () { | ||
Min RK
|
r19733 | this.scroll_manager.element.animate({scrollTop:0}, 0); | ||
Brian E. Granger
|
r4488 | }; | ||
MinRK
|
r12913 | // Edit Notebook metadata | ||
Jonathan Frederic
|
r19509 | /** | ||
* Display a dialog that allows the user to edit the Notebook's metadata. | ||||
*/ | ||||
MinRK
|
r12913 | Notebook.prototype.edit_metadata = function () { | ||
var that = this; | ||||
Jonathan Frederic
|
r17214 | dialog.edit_metadata({ | ||
md: this.metadata, | ||||
callback: function (md) { | ||||
that.metadata = md; | ||||
Thomas Kluyver
|
r18468 | }, | ||
Jonathan Frederic
|
r17214 | name: 'Notebook', | ||
notebook: this, | ||||
keyboard_manager: this.keyboard_manager}); | ||||
MinRK
|
r12913 | }; | ||
Brian E. Granger
|
r4488 | |||
Brian E. Granger
|
r4352 | // Cell indexing, retrieval, etc. | ||
Brian Granger
|
r4292 | |||
David Wyde
|
r10033 | /** | ||
* Get all cell elements in the notebook. | ||||
* | ||||
* @return {jQuery} A selector of all cell elements | ||||
*/ | ||||
Brian Granger
|
r5945 | Notebook.prototype.get_cell_elements = function () { | ||
Bussonnier Matthias
|
r19086 | return this.container.find(".cell").not('.cell .cell'); | ||
Stefan van der Walt
|
r5479 | }; | ||
Brian Granger
|
r4292 | |||
David Wyde
|
r10033 | /** | ||
* Get a particular cell element. | ||||
* | ||||
Jonathan Frederic
|
r19511 | * @param {integer} index An index of a cell to select | ||
David Wyde
|
r10033 | * @return {jQuery} A selector of the given cell. | ||
*/ | ||||
Brian Granger
|
r5945 | 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; | ||||
}; | ||||
David Wyde
|
r10033 | /** | ||
Jonathan Frederic
|
r14371 | * Try to get a particular cell by msg_id. | ||
* | ||||
Jonathan Frederic
|
r19509 | * @param {string} msg_id A message UUID | ||
Jonathan Frederic
|
r14371 | * @return {Cell} Cell or null if no cell was found. | ||
*/ | ||||
Notebook.prototype.get_msg_cell = function (msg_id) { | ||||
Jonathan Frederic
|
r17202 | return codecell.CodeCell.msg_cells[msg_id] || null; | ||
Jonathan Frederic
|
r14371 | }; | ||
/** | ||||
David Wyde
|
r10033 | * Count the cells in this notebook. | ||
* | ||||
Jonathan Frederic
|
r19511 | * @return {integer} The number of cells in this notebook | ||
David Wyde
|
r10033 | */ | ||
Notebook.prototype.ncells = function () { | ||||
Brian Granger
|
r5945 | return this.get_cell_elements().length; | ||
Stefan van der Walt
|
r5479 | }; | ||
Brian Granger
|
r4292 | |||
David Wyde
|
r10033 | /** | ||
* Get all Cell objects in this notebook. | ||||
* | ||||
* @return {Array} This notebook's Cell objects | ||||
*/ | ||||
Brian Granger
|
r5945 | Notebook.prototype.get_cells = function () { | ||
Jonathan Frederic
|
r19508 | // TODO: we are often calling cells as cells()[i], which we should optimize | ||
// to cells(i) or a new method. | ||||
Brian Granger
|
r5945 | return this.get_cell_elements().toArray().map(function (e) { | ||
Brian E. Granger
|
r4352 | return $(e).data("cell"); | ||
}); | ||||
Stefan van der Walt
|
r5479 | }; | ||
Brian Granger
|
r4292 | |||
David Wyde
|
r10033 | /** | ||
Jonathan Frederic
|
r19511 | * Get a Cell objects from this notebook. | ||
David Wyde
|
r10033 | * | ||
Jonathan Frederic
|
r19511 | * @param {integer} index - An index of a cell to retrieve | ||
Doug Blank
|
r18029 | * @return {Cell} Cell or null if no cell was found. | ||
David Wyde
|
r10033 | */ | ||
Brian Granger
|
r5945 | 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; | ||||
MinRK
|
r15236 | }; | ||
Brian Granger
|
r5945 | |||
David Wyde
|
r10033 | /** | ||
* Get the cell below a given cell. | ||||
* | ||||
Jonathan Frederic
|
r19511 | * @param {Cell} cell | ||
Doug Blank
|
r18029 | * @return {Cell} the next cell or null if no cell was found. | ||
David Wyde
|
r10033 | */ | ||
Brian Granger
|
r5945 | Notebook.prototype.get_next_cell = function (cell) { | ||
var result = null; | ||||
var index = this.find_cell_index(cell); | ||||
Matthias BUSSONNIER
|
r9549 | if (this.is_valid_cell_index(index+1)) { | ||
Brian Granger
|
r5945 | result = this.get_cell(index+1); | ||
} | ||||
return result; | ||||
MinRK
|
r15236 | }; | ||
Brian Granger
|
r5945 | |||
David Wyde
|
r10033 | /** | ||
* Get the cell above a given cell. | ||||
* | ||||
Jonathan Frederic
|
r19511 | * @param {Cell} cell | ||
Doug Blank
|
r18029 | * @return {Cell} The previous cell or null if no cell was found. | ||
David Wyde
|
r10033 | */ | ||
Brian Granger
|
r5945 | Notebook.prototype.get_prev_cell = function (cell) { | ||
var result = null; | ||||
var index = this.find_cell_index(cell); | ||||
Doug Blank
|
r18020 | if (index !== null && index > 0) { | ||
Brian Granger
|
r5945 | result = this.get_cell(index-1); | ||
} | ||||
return result; | ||||
MinRK
|
r15236 | }; | ||
David Wyde
|
r10033 | |||
/** | ||||
* Get the numeric index of a given cell. | ||||
* | ||||
Jonathan Frederic
|
r19511 | * @param {Cell} cell | ||
* @return {integer} The cell's numeric index or null if no cell was found. | ||||
David Wyde
|
r10033 | */ | ||
Brian E. Granger
|
r4352 | Notebook.prototype.find_cell_index = function (cell) { | ||
var result = null; | ||||
Brian Granger
|
r5945 | this.get_cell_elements().filter(function (index) { | ||
Brian E. Granger
|
r4352 | if ($(this).data("cell") === cell) { | ||
result = index; | ||||
MinRK
|
r15236 | } | ||
Brian E. Granger
|
r4352 | }); | ||
return result; | ||||
}; | ||||
Brian Granger
|
r4292 | |||
David Wyde
|
r10033 | /** | ||
Jonathan Frederic
|
r19511 | * Return given index if defined, or the selected index if not. | ||
David Wyde
|
r10033 | * | ||
Jonathan Frederic
|
r19511 | * @param {integer} [index] - A cell's index | ||
* @return {integer} cell index | ||||
David Wyde
|
r10033 | */ | ||
Brian E. Granger
|
r4352 | Notebook.prototype.index_or_selected = function (index) { | ||
Brian Granger
|
r5898 | var i; | ||
Brian Granger
|
r5945 | if (index === undefined || index === null) { | ||
i = this.get_selected_index(); | ||||
Brian Granger
|
r5898 | if (i === null) { | ||
i = 0; | ||||
} | ||||
} else { | ||||
i = index; | ||||
} | ||||
return i; | ||||
Stefan van der Walt
|
r5479 | }; | ||
Brian Granger
|
r4292 | |||
David Wyde
|
r10033 | /** | ||
* Get the currently selected cell. | ||||
Jonathan Frederic
|
r19511 | * | ||
David Wyde
|
r10033 | * @return {Cell} The selected cell | ||
*/ | ||||
Brian Granger
|
r5945 | Notebook.prototype.get_selected_cell = function () { | ||
var index = this.get_selected_index(); | ||||
return this.get_cell(index); | ||||
Brian Granger
|
r4292 | }; | ||
David Wyde
|
r10033 | /** | ||
* Check whether a cell index is valid. | ||||
* | ||||
Jonathan Frederic
|
r19511 | * @param {integer} index - A cell index | ||
David Wyde
|
r10033 | * @return True if the index is valid, false otherwise | ||
*/ | ||||
Brian Granger
|
r5945 | Notebook.prototype.is_valid_cell_index = function (index) { | ||
if (index !== null && index >= 0 && index < this.ncells()) { | ||||
return true; | ||||
} else { | ||||
return false; | ||||
MinRK
|
r15236 | } | ||
}; | ||||
Brian Granger
|
r4299 | |||
David Wyde
|
r10033 | /** | ||
* Get the index of the currently selected cell. | ||||
Jonathan Frederic
|
r19511 | * | ||
* @return {integer} The selected cell's numeric index | ||||
David Wyde
|
r10033 | */ | ||
Brian Granger
|
r5945 | Notebook.prototype.get_selected_index = function () { | ||
Brian E. Granger
|
r4352 | var result = null; | ||
Brian Granger
|
r5945 | this.get_cell_elements().filter(function (index) { | ||
Brian E. Granger
|
r4352 | if ($(this).data("cell").selected === true) { | ||
result = index; | ||||
MinRK
|
r15236 | } | ||
Brian E. Granger
|
r4352 | }); | ||
return result; | ||||
}; | ||||
Brian Granger
|
r4292 | |||
Brian Granger
|
r5945 | // Cell selection. | ||
Brian Granger
|
r4292 | |||
David Wyde
|
r10033 | /** | ||
* Programmatically select a cell. | ||||
* | ||||
Jonathan Frederic
|
r19511 | * @param {integer} index - A cell's index | ||
David Wyde
|
r10033 | * @return {Notebook} This notebook | ||
*/ | ||||
Brian Granger
|
r5945 | Notebook.prototype.select = function (index) { | ||
Matthias BUSSONNIER
|
r9549 | if (this.is_valid_cell_index(index)) { | ||
MinRK
|
r15236 | var sindex = this.get_selected_index(); | ||
Brian Granger
|
r5946 | if (sindex !== null && index !== sindex) { | ||
Brian E. Granger
|
r15669 | // If we are about to select a different cell, make sure we are | ||
// first in command mode. | ||||
if (this.mode !== 'command') { | ||||
this.command_mode(); | ||||
} | ||||
Brian Granger
|
r5945 | this.get_cell(sindex).unselect(); | ||
MinRK
|
r15236 | } | ||
Mikhail Korobov
|
r8839 | var cell = this.get_cell(index); | ||
Brian Granger
|
r5994 | cell.select(); | ||
Brian Granger
|
r6028 | if (cell.cell_type === 'heading') { | ||
Jonathan Frederic
|
r17195 | this.events.trigger('selected_cell_type_changed.Notebook', | ||
Brian Granger
|
r6047 | {'cell_type':cell.cell_type,level:cell.level} | ||
); | ||||
Brian Granger
|
r6028 | } else { | ||
Jonathan Frederic
|
r17195 | this.events.trigger('selected_cell_type_changed.Notebook', | ||
Brian Granger
|
r6047 | {'cell_type':cell.cell_type} | ||
); | ||||
MinRK
|
r15236 | } | ||
} | ||||
Brian Granger
|
r4292 | return this; | ||
}; | ||||
David Wyde
|
r10033 | /** | ||
* Programmatically select the next cell. | ||||
* | ||||
* @return {Notebook} This notebook | ||||
*/ | ||||
Brian Granger
|
r5945 | Notebook.prototype.select_next = function () { | ||
var index = this.get_selected_index(); | ||||
Matthias BUSSONNIER
|
r9549 | this.select(index+1); | ||
Stefan van der Walt
|
r5479 | return this; | ||
Brian Granger
|
r4292 | }; | ||
Brian E. Granger
|
r4352 | |||
David Wyde
|
r10033 | /** | ||
* Programmatically select the previous cell. | ||||
* | ||||
* @return {Notebook} This notebook | ||||
*/ | ||||
Brian Granger
|
r5945 | Notebook.prototype.select_prev = function () { | ||
var index = this.get_selected_index(); | ||||
Matthias BUSSONNIER
|
r9549 | this.select(index-1); | ||
Brian E. Granger
|
r4352 | return this; | ||
Brian Granger
|
r4292 | }; | ||
Brian E. Granger
|
r4352 | |||
Brian E. Granger
|
r14015 | // Edit/Command mode | ||
Jonathan Frederic
|
r15500 | /** | ||
* Gets the index of the cell that is in edit mode. | ||||
* | ||||
Jonathan Frederic
|
r19511 | * @return {integer} index | ||
*/ | ||||
Jonathan Frederic
|
r15500 | Notebook.prototype.get_edit_index = function () { | ||
Brian E. Granger
|
r14016 | var result = null; | ||
this.get_cell_elements().filter(function (index) { | ||||
if ($(this).data("cell").mode === 'edit') { | ||||
Jonathan Frederic
|
r15500 | result = index; | ||
MinRK
|
r15236 | } | ||
Brian E. Granger
|
r14016 | }); | ||
return result; | ||||
}; | ||||
Jonathan Frederic
|
r15500 | /** | ||
Brian E. Granger
|
r15629 | * Handle when a a cell blurs and the notebook should enter command mode. | ||
Jonathan Frederic
|
r15500 | * | ||
Jonathan Frederic
|
r19511 | * @param {Cell} [cell] - Cell to enter command mode on. | ||
*/ | ||||
Brian E. Granger
|
r15629 | Notebook.prototype.handle_command_mode = function (cell) { | ||
Brian E. Granger
|
r14015 | if (this.mode !== 'command') { | ||
Brian E. Granger
|
r15669 | cell.command_mode(); | ||
Brian E. Granger
|
r14021 | this.mode = 'command'; | ||
Jonathan Frederic
|
r17195 | this.events.trigger('command_mode.Notebook'); | ||
Jonathan Frederic
|
r17194 | this.keyboard_manager.command_mode(); | ||
MinRK
|
r15236 | } | ||
Brian E. Granger
|
r14015 | }; | ||
Jonathan Frederic
|
r15500 | /** | ||
Brian E. Granger
|
r15629 | * Make the notebook enter command mode. | ||
Jonathan Frederic
|
r19511 | */ | ||
Brian E. Granger
|
r15629 | Notebook.prototype.command_mode = function () { | ||
var cell = this.get_cell(this.get_edit_index()); | ||||
if (cell && this.mode !== 'command') { | ||||
// We don't call cell.command_mode, but rather call cell.focus_cell() | ||||
// which will blur and CM editor and trigger the call to | ||||
// handle_command_mode. | ||||
cell.focus_cell(); | ||||
} | ||||
}; | ||||
/** | ||||
Jonathan Frederic
|
r15531 | * Handle when a cell fires it's edit_mode event. | ||
Jonathan Frederic
|
r15500 | * | ||
Jonathan Frederic
|
r19509 | * @param {Cell} [cell] Cell to enter edit mode on. | ||
Jonathan Frederic
|
r19511 | */ | ||
Brian E. Granger
|
r15629 | Notebook.prototype.handle_edit_mode = function (cell) { | ||
Brian E. Granger
|
r15669 | if (cell && this.mode !== 'edit') { | ||
Jonathan Frederic
|
r15534 | cell.edit_mode(); | ||
Brian E. Granger
|
r14021 | this.mode = 'edit'; | ||
Jonathan Frederic
|
r17195 | this.events.trigger('edit_mode.Notebook'); | ||
Jonathan Frederic
|
r17194 | this.keyboard_manager.edit_mode(); | ||
MinRK
|
r15236 | } | ||
Brian E. Granger
|
r14015 | }; | ||
Jonathan Frederic
|
r15500 | /** | ||
Jonathan Frederic
|
r15531 | * Make a cell enter edit mode. | ||
Jonathan Frederic
|
r19511 | */ | ||
Brian E. Granger
|
r15669 | Notebook.prototype.edit_mode = function () { | ||
var cell = this.get_selected_cell(); | ||||
if (cell && this.mode !== 'edit') { | ||||
Jonathan Frederic
|
r15531 | cell.unrender(); | ||
cell.focus_editor(); | ||||
} | ||||
}; | ||||
Matthias Bussonnier
|
r20677 | |||
/** | ||||
* Ensure either cell, or codemirror is focused. Is none | ||||
* is focused, focus the cell. | ||||
*/ | ||||
Notebook.prototype.ensure_focused = function(){ | ||||
var cell = this.get_selected_cell(); | ||||
if (cell === null) {return;} // No cell is selected | ||||
cell.ensure_focused(); | ||||
} | ||||
Jonathan Frederic
|
r15531 | |||
/** | ||||
Jonathan Frederic
|
r15500 | * Focus the currently selected cell. | ||
Jonathan Frederic
|
r19511 | */ | ||
Brian E. Granger
|
r14073 | Notebook.prototype.focus_cell = function () { | ||
var cell = this.get_selected_cell(); | ||||
if (cell === null) {return;} // No cell is selected | ||||
cell.focus_cell(); | ||||
}; | ||||
Brian E. Granger
|
r14015 | |||
Brian Granger
|
r5945 | // Cell movement | ||
Matthias BUSSONNIER
|
r9788 | /** | ||
David Wyde
|
r10033 | * Move given (or selected) cell up and select it. | ||
* | ||||
Jonathan Frederic
|
r19511 | * @param {integer} [index] - cell index | ||
David Wyde
|
r10033 | * @return {Notebook} This notebook | ||
Jonathan Frederic
|
r19511 | */ | ||
Brian E. Granger
|
r4352 | Notebook.prototype.move_cell_up = function (index) { | ||
Matthias BUSSONNIER
|
r9788 | var i = this.index_or_selected(index); | ||
if (this.is_valid_cell_index(i) && i > 0) { | ||||
Brian Granger
|
r5945 | var pivot = this.get_cell_element(i-1); | ||
var tomove = this.get_cell_element(i); | ||||
Brian E. Granger
|
r4352 | if (pivot !== null && tomove !== null) { | ||
tomove.detach(); | ||||
pivot.before(tomove); | ||||
this.select(i-1); | ||||
Brian E. Granger
|
r14025 | var cell = this.get_selected_cell(); | ||
cell.focus_cell(); | ||||
MinRK
|
r15236 | } | ||
MinRK
|
r10781 | this.set_dirty(true); | ||
MinRK
|
r15236 | } | ||
Brian E. Granger
|
r4352 | return this; | ||
Stefan van der Walt
|
r5479 | }; | ||
Brian E. Granger
|
r4352 | |||
Matthias BUSSONNIER
|
r9788 | /** | ||
Jonathan Frederic
|
r19511 | * Move given (or selected) cell down and select it. | ||
David Wyde
|
r10033 | * | ||
Jonathan Frederic
|
r19511 | * @param {integer} [index] - cell index | ||
David Wyde
|
r10033 | * @return {Notebook} This notebook | ||
Jonathan Frederic
|
r19511 | */ | ||
Brian E. Granger
|
r4352 | Notebook.prototype.move_cell_down = function (index) { | ||
Matthias BUSSONNIER
|
r9788 | var i = this.index_or_selected(index); | ||
Brian E. Granger
|
r14016 | if (this.is_valid_cell_index(i) && this.is_valid_cell_index(i+1)) { | ||
Brian Granger
|
r5945 | var pivot = this.get_cell_element(i+1); | ||
var tomove = this.get_cell_element(i); | ||||
Brian E. Granger
|
r4352 | if (pivot !== null && tomove !== null) { | ||
tomove.detach(); | ||||
pivot.after(tomove); | ||||
this.select(i+1); | ||||
Brian E. Granger
|
r14025 | var cell = this.get_selected_cell(); | ||
cell.focus_cell(); | ||||
MinRK
|
r15236 | } | ||
} | ||||
MinRK
|
r10781 | this.set_dirty(); | ||
Brian E. Granger
|
r4352 | return this; | ||
Stefan van der Walt
|
r5479 | }; | ||
Brian E. Granger
|
r4352 | |||
Brian Granger
|
r5945 | // Insertion, deletion. | ||
Brian Granger
|
r4292 | |||
David Wyde
|
r10033 | /** | ||
Bussonnier Matthias
|
r19582 | * Delete a cell from the notebook without any precautions | ||
* Needed to reload checkpoints and other things like that. | ||||
* | ||||
* @param {integer} [index] - cell's numeric index | ||||
* @return {Notebook} This notebook | ||||
*/ | ||||
Bussonnier Matthias
|
r19588 | Notebook.prototype._unsafe_delete_cell = function (index) { | ||
Bussonnier Matthias
|
r19582 | var i = this.index_or_selected(index); | ||
var cell = this.get_cell(i); | ||||
$('#undelete_cell').addClass('disabled'); | ||||
if (this.is_valid_cell_index(i)) { | ||||
var old_ncells = this.ncells(); | ||||
var ce = this.get_cell_element(i); | ||||
ce.remove(); | ||||
this.set_dirty(true); | ||||
} | ||||
return this; | ||||
}; | ||||
/** | ||||
David Wyde
|
r10033 | * Delete a cell from the notebook. | ||
* | ||||
Jonathan Frederic
|
r19511 | * @param {integer} [index] - cell's numeric index | ||
David Wyde
|
r10033 | * @return {Notebook} This notebook | ||
*/ | ||||
Brian Granger
|
r5945 | Notebook.prototype.delete_cell = function (index) { | ||
Brian E. Granger
|
r4352 | var i = this.index_or_selected(index); | ||
Jessica B. Hamrick
|
r17998 | var cell = this.get_cell(i); | ||
if (!cell.is_deletable()) { | ||||
return this; | ||||
} | ||||
David Warde-Farley
|
r8691 | this.undelete_backup = cell.toJSON(); | ||
MinRK
|
r11118 | $('#undelete_cell').removeClass('disabled'); | ||
Brian Granger
|
r5945 | if (this.is_valid_cell_index(i)) { | ||
Brian E. Granger
|
r14032 | var old_ncells = this.ncells(); | ||
Brian Granger
|
r5945 | var ce = this.get_cell_element(i); | ||
ce.remove(); | ||||
Brian E. Granger
|
r14032 | if (i === 0) { | ||
// Always make sure we have at least one cell. | ||||
if (old_ncells === 1) { | ||||
this.insert_cell_below('code'); | ||||
} | ||||
this.select(0); | ||||
this.undelete_index = 0; | ||||
this.undelete_below = false; | ||||
} else if (i === old_ncells-1 && i !== 0) { | ||||
Brian Granger
|
r5945 | this.select(i-1); | ||
David Warde-Farley
|
r8691 | this.undelete_index = i - 1; | ||
this.undelete_below = true; | ||||
Brian Granger
|
r5945 | } else { | ||
this.select(i); | ||||
David Warde-Farley
|
r8691 | this.undelete_index = i; | ||
this.undelete_below = false; | ||||
MinRK
|
r15236 | } | ||
Jonathan Frederic
|
r17195 | this.events.trigger('delete.Cell', {'cell': cell, 'index': i}); | ||
MinRK
|
r10781 | this.set_dirty(true); | ||
MinRK
|
r15236 | } | ||
Brian Granger
|
r5945 | return this; | ||
Stefan van der Walt
|
r5479 | }; | ||
Brian Granger
|
r4299 | |||
Matthias BUSSONNIER
|
r9550 | /** | ||
Brian E. Granger
|
r14032 | * Restore the most recently deleted cell. | ||
*/ | ||||
Notebook.prototype.undelete_cell = 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); | ||||
if (this.undelete_below) { | ||||
this.select(current_index+1); | ||||
} else { | ||||
this.select(current_index); | ||||
} | ||||
this.undelete_backup = null; | ||||
this.undelete_index = null; | ||||
} | ||||
$('#undelete_cell').addClass('disabled'); | ||||
MinRK
|
r15236 | }; | ||
Brian E. Granger
|
r14032 | |||
/** | ||||
Matthias BUSSONNIER
|
r9550 | * Insert a cell so that after insertion the cell is at given index. | ||
* | ||||
Paul Ivanov
|
r16777 | * If cell type is not provided, it will default to the type of the | ||
* currently active cell. | ||||
* | ||||
Jonathan Frederic
|
r19511 | * Similar to insert_above, but index parameter is mandatory. | ||
Matthias BUSSONNIER
|
r9550 | * | ||
Jonathan Frederic
|
r19511 | * Index will be brought back into the accessible range [0,n]. | ||
Matthias BUSSONNIER
|
r9550 | * | ||
Jonathan Frederic
|
r19511 | * @param {string} [type] - in ['code','markdown', 'raw'], defaults to 'code' | ||
* @param {integer} [index] - a valid index where to insert cell | ||||
Jonathan Frederic
|
r19509 | * @return {Cell|null} created cell or null | ||
Jonathan Frederic
|
r19511 | */ | ||
Matthias BUSSONNIER
|
r13576 | Notebook.prototype.insert_cell_at_index = function(type, index){ | ||
Bussonnier Matthias
|
r9677 | var ncells = this.ncells(); | ||
MinRK
|
r17785 | index = Math.min(index, ncells); | ||
index = Math.max(index, 0); | ||||
Brian Granger
|
r6017 | var cell = null; | ||
Thomas Kluyver
|
r19529 | type = type || this.class_config.get_sync('default_cell_type'); | ||
MinRK
|
r17785 | if (type === 'above') { | ||
if (index > 0) { | ||||
type = this.get_cell(index-1).cell_type; | ||||
} else { | ||||
type = 'code'; | ||||
} | ||||
} else if (type === 'below') { | ||||
if (index < ncells) { | ||||
type = this.get_cell(index).cell_type; | ||||
} else { | ||||
type = 'code'; | ||||
} | ||||
} else if (type === 'selected') { | ||||
type = this.get_selected_cell().cell_type; | ||||
} | ||||
Matthias BUSSONNIER
|
r9550 | |||
Matthias BUSSONNIER
|
r9789 | if (ncells === 0 || this.is_valid_cell_index(index) || index === ncells) { | ||
jon
|
r17210 | var cell_options = { | ||
events: this.events, | ||||
config: this.config, | ||||
keyboard_manager: this.keyboard_manager, | ||||
Jonathan Frederic
|
r17215 | notebook: this, | ||
Bussonnier Matthias
|
r18993 | tooltip: this.tooltip | ||
jon
|
r17210 | }; | ||
MinRK
|
r18596 | switch(type) { | ||
case 'code': | ||||
jon
|
r17210 | cell = new codecell.CodeCell(this.kernel, cell_options); | ||
Brian Granger
|
r5945 | cell.set_input_prompt(); | ||
MinRK
|
r18596 | break; | ||
case 'markdown': | ||||
jon
|
r17211 | cell = new textcell.MarkdownCell(cell_options); | ||
MinRK
|
r18596 | break; | ||
case 'raw': | ||||
jon
|
r17211 | cell = new textcell.RawCell(cell_options); | ||
MinRK
|
r18596 | break; | ||
default: | ||||
Min RK
|
r18999 | console.log("Unrecognized cell type: ", type, cellmod); | ||
cell = new cellmod.UnrecognizedCell(cell_options); | ||||
Bussonnier Matthias
|
r9677 | } | ||
Brian E. Granger
|
r14015 | if(this._insert_element_at_index(cell.element,index)) { | ||
Brian Granger
|
r5946 | cell.render(); | ||
Jonathan Frederic
|
r17195 | this.events.trigger('create.Cell', {'cell': cell, 'index': index}); | ||
Brian E. Granger
|
r14015 | cell.refresh(); | ||
Brian E. Granger
|
r14029 | // We used to select the cell after we refresh it, but there | ||
// are now cases were this method is called where select is | ||||
// not appropriate. The selection logic should be handled by the | ||||
// caller of the the top level insert_cell methods. | ||||
MinRK
|
r10781 | this.set_dirty(true); | ||
Bussonnier Matthias
|
r9677 | } | ||
} | ||||
Brian Granger
|
r6017 | return cell; | ||
Matthias BUSSONNIER
|
r9550 | |||
Stefan van der Walt
|
r5479 | }; | ||
Brian E. Granger
|
r4507 | |||
Bussonnier Matthias
|
r9677 | /** | ||
* Insert an element at given cell index. | ||||
* | ||||
Jonathan Frederic
|
r19509 | * @param {HTMLElement} element - a cell element | ||
Jonathan Frederic
|
r19511 | * @param {integer} [index] - a valid index where to inser cell | ||
* @returns {boolean} success | ||||
*/ | ||||
Bussonnier Matthias
|
r9677 | Notebook.prototype._insert_element_at_index = function(element, index){ | ||
Matthias BUSSONNIER
|
r9788 | if (element === undefined){ | ||
Matthias BUSSONNIER
|
r9688 | return false; | ||
} | ||||
Bussonnier Matthias
|
r9677 | var ncells = this.ncells(); | ||
Matthias BUSSONNIER
|
r9688 | if (ncells === 0) { | ||
// special case append if empty | ||||
Matthias Bussonnier
|
r19872 | this.container.append(element); | ||
Matthias BUSSONNIER
|
r9788 | } else if ( ncells === index ) { | ||
Matthias BUSSONNIER
|
r9688 | // 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; | ||||
} | ||||
Brian E. Granger
|
r4507 | |||
David Warde-Farley
|
r8694 | if (this.undelete_index !== null && index <= this.undelete_index) { | ||
this.undelete_index = this.undelete_index + 1; | ||||
MinRK
|
r10781 | this.set_dirty(true); | ||
David Warde-Farley
|
r8694 | } | ||
Matthias BUSSONNIER
|
r9688 | return true; | ||
Matthias BUSSONNIER
|
r9550 | }; | ||
/** | ||||
* Insert a cell of given type above given index, or at top | ||||
* of notebook if index smaller than 0. | ||||
* | ||||
Jonathan Frederic
|
r19511 | * @param {string} [type] - cell type | ||
* @param {integer} [index] - defaults to the currently selected cell | ||||
Jonathan Frederic
|
r19509 | * @return {Cell|null} handle to created cell or null | ||
Jonathan Frederic
|
r19511 | */ | ||
Matthias BUSSONNIER
|
r9550 | Notebook.prototype.insert_cell_above = function (type, index) { | ||
index = this.index_or_selected(index); | ||||
return this.insert_cell_at_index(type, index); | ||||
Stefan van der Walt
|
r5479 | }; | ||
Brian E. Granger
|
r4507 | |||
Bussonnier Matthias
|
r9677 | /** | ||
* Insert a cell of given type below given index, or at bottom | ||||
Paul Ivanov
|
r16777 | * of notebook if index greater than number of cells | ||
Bussonnier Matthias
|
r9677 | * | ||
Jonathan Frederic
|
r19511 | * @param {string} [type] - cell type | ||
* @param {integer} [index] - defaults to the currently selected cell | ||||
Jonathan Frederic
|
r19509 | * @return {Cell|null} handle to created cell or null | ||
Jonathan Frederic
|
r19511 | */ | ||
Matthias BUSSONNIER
|
r13576 | Notebook.prototype.insert_cell_below = function (type, index) { | ||
Bussonnier Matthias
|
r9677 | index = this.index_or_selected(index); | ||
Matthias BUSSONNIER
|
r13576 | return this.insert_cell_at_index(type, index+1); | ||
Bussonnier Matthias
|
r9677 | }; | ||
/** | ||||
* Insert cell at end of notebook | ||||
* | ||||
Jonathan Frederic
|
r19511 | * @param {string} type - cell type | ||
* @return {Cell|null} handle to created cell or null | ||||
*/ | ||||
Matthias BUSSONNIER
|
r13576 | Notebook.prototype.insert_cell_at_bottom = function (type){ | ||
Bussonnier Matthias
|
r9677 | var len = this.ncells(); | ||
Matthias BUSSONNIER
|
r13576 | return this.insert_cell_below(type,len-1); | ||
Bussonnier Matthias
|
r9677 | }; | ||
David Wyde
|
r10033 | /** | ||
* Turn a cell into a code cell. | ||||
* | ||||
Jonathan Frederic
|
r19511 | * @param {integer} [index] - cell index | ||
David Wyde
|
r10033 | */ | ||
Brian E. Granger
|
r4507 | Notebook.prototype.to_code = function (index) { | ||
Brian E. Granger
|
r4352 | var i = this.index_or_selected(index); | ||
Brian Granger
|
r5945 | if (this.is_valid_cell_index(i)) { | ||
Pierre Gerold
|
r17844 | var source_cell = this.get_cell(i); | ||
Jonathan Frederic
|
r17203 | if (!(source_cell instanceof codecell.CodeCell)) { | ||
Mikhail Korobov
|
r8839 | var target_cell = this.insert_cell_below('code',i); | ||
Brian Granger
|
r5945 | var text = source_cell.get_text(); | ||
if (text === source_cell.placeholder) { | ||||
text = ''; | ||||
} | ||||
Pierre Gerold
|
r17844 | //metadata | ||
target_cell.metadata = source_cell.metadata; | ||||
Pierre Gerold
|
r17843 | |||
Brian Granger
|
r5945 | target_cell.set_text(text); | ||
Paul Ivanov
|
r7587 | // 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(); | ||||
Pierre Gerold
|
r17843 | source_cell.element.remove(); | ||
Brian E. Granger
|
r14018 | this.select(i); | ||
Jonathan Frederic
|
r16818 | var cursor = source_cell.code_mirror.getCursor(); | ||
target_cell.code_mirror.setCursor(cursor); | ||||
MinRK
|
r10781 | this.set_dirty(true); | ||
MinRK
|
r15236 | } | ||
} | ||||
Brian E. Granger
|
r4352 | }; | ||
Brian Granger
|
r4299 | |||
David Wyde
|
r10033 | /** | ||
* Turn a cell into a Markdown cell. | ||||
* | ||||
Jonathan Frederic
|
r19511 | * @param {integer} [index] - cell index | ||
David Wyde
|
r10033 | */ | ||
Brian Granger
|
r4508 | Notebook.prototype.to_markdown = function (index) { | ||
Brian E. Granger
|
r4352 | var i = this.index_or_selected(index); | ||
Brian Granger
|
r5945 | if (this.is_valid_cell_index(i)) { | ||
Pierre Gerold
|
r17844 | var source_cell = this.get_cell(i); | ||
Pierre Gerold
|
r17843 | |||
jon
|
r17211 | if (!(source_cell instanceof textcell.MarkdownCell)) { | ||
Mikhail Korobov
|
r8839 | var target_cell = this.insert_cell_below('markdown',i); | ||
Brian Granger
|
r5945 | var text = source_cell.get_text(); | ||
Pierre Gerold
|
r17843 | |||
Brian Granger
|
r5945 | if (text === source_cell.placeholder) { | ||
Brian Granger
|
r5946 | text = ''; | ||
MinRK
|
r15236 | } | ||
Pierre Gerold
|
r17844 | // metadata | ||
Min RK
|
r18752 | target_cell.metadata = source_cell.metadata; | ||
Brian E. Granger
|
r14016 | // We must show the editor before setting its contents | ||
Brian E. Granger
|
r14014 | target_cell.unrender(); | ||
Brian Granger
|
r6017 | target_cell.set_text(text); | ||
Paul Ivanov
|
r7587 | // 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(); | ||||
Pierre Gerold
|
r17843 | source_cell.element.remove(); | ||
Brian E. Granger
|
r14015 | this.select(i); | ||
jon
|
r17211 | if ((source_cell instanceof textcell.TextCell) && source_cell.rendered) { | ||
Brian E. Granger
|
r14943 | target_cell.render(); | ||
} | ||||
Jonathan Frederic
|
r16818 | var cursor = source_cell.code_mirror.getCursor(); | ||
target_cell.code_mirror.setCursor(cursor); | ||||
MinRK
|
r10781 | this.set_dirty(true); | ||
MinRK
|
r15236 | } | ||
} | ||||
Brian E. Granger
|
r4507 | }; | ||
David Wyde
|
r10033 | /** | ||
* Turn a cell into a raw text cell. | ||||
* | ||||
Jonathan Frederic
|
r19511 | * @param {integer} [index] - cell index | ||
David Wyde
|
r10033 | */ | ||
MinRK
|
r6248 | Notebook.prototype.to_raw = function (index) { | ||
Brian Granger
|
r6017 | var i = this.index_or_selected(index); | ||
if (this.is_valid_cell_index(i)) { | ||||
var target_cell = null; | ||||
Pierre Gerold
|
r17844 | var source_cell = this.get_cell(i); | ||
Pierre Gerold
|
r17843 | |||
jon
|
r17211 | if (!(source_cell instanceof textcell.RawCell)) { | ||
MinRK
|
r6248 | target_cell = this.insert_cell_below('raw',i); | ||
Brian Granger
|
r6017 | var text = source_cell.get_text(); | ||
if (text === source_cell.placeholder) { | ||||
text = ''; | ||||
MinRK
|
r15236 | } | ||
Pierre Gerold
|
r17844 | //metadata | ||
target_cell.metadata = source_cell.metadata; | ||||
Brian E. Granger
|
r14016 | // We must show the editor before setting its contents | ||
Brian E. Granger
|
r14014 | target_cell.unrender(); | ||
Brian Granger
|
r6017 | target_cell.set_text(text); | ||
Paul Ivanov
|
r7587 | // 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(); | ||||
Pierre Gerold
|
r17843 | source_cell.element.remove(); | ||
Brian E. Granger
|
r14016 | this.select(i); | ||
Jonathan Frederic
|
r16818 | var cursor = source_cell.code_mirror.getCursor(); | ||
target_cell.code_mirror.setCursor(cursor); | ||||
MinRK
|
r10781 | this.set_dirty(true); | ||
MinRK
|
r15236 | } | ||
} | ||||
Brian Granger
|
r6017 | }; | ||
Min RK
|
r18680 | |||
Jonathan Frederic
|
r19509 | /** | ||
Jonathan Frederic
|
r19511 | * Warn about heading cell support removal. | ||
Jonathan Frederic
|
r19509 | */ | ||
Min RK
|
r18680 | Notebook.prototype._warn_heading = function () { | ||
dialog.modal({ | ||||
notebook: this, | ||||
keyboard_manager: this.keyboard_manager, | ||||
title : "Use markdown headings", | ||||
body : $("<p/>").text( | ||||
'IPython no longer uses special heading cells. ' + | ||||
'Instead, write your headings in Markdown cells using # characters:' | ||||
).append($('<pre/>').text( | ||||
'## This is a level 2 heading' | ||||
)), | ||||
buttons : { | ||||
Bussonnier Matthias
|
r18993 | "OK" : {} | ||
Min RK
|
r18680 | } | ||
}); | ||||
}; | ||||
David Wyde
|
r10033 | /** | ||
Jonathan Frederic
|
r19511 | * Turn a cell into a heading containing markdown cell. | ||
David Wyde
|
r10033 | * | ||
Jonathan Frederic
|
r19511 | * @param {integer} [index] - cell index | ||
* @param {integer} [level] - heading level (e.g., 1 for h1) | ||||
David Wyde
|
r10033 | */ | ||
Jonathan Frederic
|
r19509 | Notebook.prototype.to_heading = function (index, level) { | ||
Min RK
|
r18680 | this.to_markdown(index); | ||
Brian Granger
|
r6019 | level = level || 1; | ||
var i = this.index_or_selected(index); | ||||
if (this.is_valid_cell_index(i)) { | ||||
Min RK
|
r18680 | var cell = this.get_cell(i); | ||
cell.set_heading_level(level); | ||||
Brian E. Granger
|
r14031 | this.set_dirty(true); | ||
MinRK
|
r15236 | } | ||
Brian Granger
|
r6019 | }; | ||
Brian Granger
|
r5945 | // Cut/Copy/Paste | ||
Brian Granger
|
r5879 | |||
David Wyde
|
r10033 | /** | ||
Jonathan Frederic
|
r19511 | * Enable the UI elements for pasting cells. | ||
David Wyde
|
r10033 | */ | ||
Brian Granger
|
r5879 | Notebook.prototype.enable_paste = function () { | ||
var that = this; | ||||
Brian Granger
|
r5912 | if (!this.paste_enabled) { | ||
MinRK
|
r11118 | $('#paste_cell_replace').removeClass('disabled') | ||
David Warde-Farley
|
r8715 | .on('click', function () {that.paste_cell_replace();}); | ||
MinRK
|
r11118 | $('#paste_cell_above').removeClass('disabled') | ||
Brian Granger
|
r5912 | .on('click', function () {that.paste_cell_above();}); | ||
MinRK
|
r11118 | $('#paste_cell_below').removeClass('disabled') | ||
Brian Granger
|
r5912 | .on('click', function () {that.paste_cell_below();}); | ||
this.paste_enabled = true; | ||||
MinRK
|
r15236 | } | ||
Brian Granger
|
r5879 | }; | ||
David Wyde
|
r10033 | /** | ||
Jonathan Frederic
|
r19511 | * Disable the UI elements for pasting cells. | ||
David Wyde
|
r10033 | */ | ||
Brian Granger
|
r5879 | Notebook.prototype.disable_paste = function () { | ||
Brian Granger
|
r5912 | if (this.paste_enabled) { | ||
MinRK
|
r11118 | $('#paste_cell_replace').addClass('disabled').off('click'); | ||
$('#paste_cell_above').addClass('disabled').off('click'); | ||||
$('#paste_cell_below').addClass('disabled').off('click'); | ||||
Brian Granger
|
r5912 | this.paste_enabled = false; | ||
MinRK
|
r15236 | } | ||
Brian Granger
|
r5879 | }; | ||
David Wyde
|
r10033 | /** | ||
* Cut a cell. | ||||
*/ | ||||
Brian Granger
|
r5879 | Notebook.prototype.cut_cell = function () { | ||
this.copy_cell(); | ||||
this.delete_cell(); | ||||
MinRK
|
r15236 | }; | ||
Brian Granger
|
r5879 | |||
David Wyde
|
r10033 | /** | ||
* Copy a cell. | ||||
*/ | ||||
Brian Granger
|
r5879 | Notebook.prototype.copy_cell = function () { | ||
Brian Granger
|
r5945 | var cell = this.get_selected_cell(); | ||
Brian Granger
|
r5879 | this.clipboard = cell.toJSON(); | ||
Jessica B. Hamrick
|
r17998 | // remove undeletable status from the copied cell | ||
if (this.clipboard.metadata.deletable !== undefined) { | ||||
delete this.clipboard.metadata.deletable; | ||||
} | ||||
Brian Granger
|
r5879 | this.enable_paste(); | ||
}; | ||||
David Wyde
|
r10033 | /** | ||
Jonathan Frederic
|
r19511 | * Replace the selected cell with the cell in the clipboard. | ||
David Wyde
|
r10033 | */ | ||
David Warde-Farley
|
r8715 | Notebook.prototype.paste_cell_replace = function () { | ||
Brian Granger
|
r5912 | if (this.clipboard !== null && this.paste_enabled) { | ||
Brian Granger
|
r5879 | var cell_data = this.clipboard; | ||
Brian Granger
|
r5945 | var new_cell = this.insert_cell_above(cell_data.cell_type); | ||
Brian Granger
|
r5944 | new_cell.fromJSON(cell_data); | ||
Mikhail Korobov
|
r8839 | var old_cell = this.get_next_cell(new_cell); | ||
Brian Granger
|
r5945 | this.delete_cell(this.find_cell_index(old_cell)); | ||
this.select(this.find_cell_index(new_cell)); | ||||
MinRK
|
r15236 | } | ||
Brian Granger
|
r5879 | }; | ||
David Wyde
|
r10033 | /** | ||
* Paste a cell from the clipboard above the selected cell. | ||||
*/ | ||||
Brian Granger
|
r5879 | Notebook.prototype.paste_cell_above = function () { | ||
Brian Granger
|
r5912 | if (this.clipboard !== null && this.paste_enabled) { | ||
Brian Granger
|
r5879 | var cell_data = this.clipboard; | ||
Brian Granger
|
r5945 | var new_cell = this.insert_cell_above(cell_data.cell_type); | ||
Brian Granger
|
r5944 | new_cell.fromJSON(cell_data); | ||
Paul Ivanov
|
r14972 | new_cell.focus_cell(); | ||
MinRK
|
r15236 | } | ||
Brian Granger
|
r5879 | }; | ||
David Wyde
|
r10033 | /** | ||
* Paste a cell from the clipboard below the selected cell. | ||||
*/ | ||||
Brian Granger
|
r5879 | Notebook.prototype.paste_cell_below = function () { | ||
Brian Granger
|
r5912 | if (this.clipboard !== null && this.paste_enabled) { | ||
Brian Granger
|
r5879 | var cell_data = this.clipboard; | ||
Brian Granger
|
r5945 | var new_cell = this.insert_cell_below(cell_data.cell_type); | ||
Brian Granger
|
r5944 | new_cell.fromJSON(cell_data); | ||
Paul Ivanov
|
r14972 | new_cell.focus_cell(); | ||
MinRK
|
r15236 | } | ||
Brian Granger
|
r5879 | }; | ||
Brian Granger
|
r5945 | // Split/merge | ||
David Wyde
|
r10033 | /** | ||
Jonathan Frederic
|
r19511 | * Split the selected cell into two cells. | ||
David Wyde
|
r10033 | */ | ||
Brian Granger
|
r5897 | Notebook.prototype.split_cell = function () { | ||
Brian Granger
|
r5945 | var cell = this.get_selected_cell(); | ||
Brian Granger
|
r5946 | if (cell.is_splittable()) { | ||
Mikhail Korobov
|
r8839 | var texta = cell.get_pre_cursor(); | ||
var textb = cell.get_post_cursor(); | ||||
Paul Ivanov
|
r17417 | cell.set_text(textb); | ||
var new_cell = this.insert_cell_above(cell.cell_type); | ||||
// Unrender the new cell so we can call set_text. | ||||
new_cell.unrender(); | ||||
new_cell.set_text(texta); | ||||
MinRK
|
r15236 | } | ||
Brian Granger
|
r5897 | }; | ||
David Wyde
|
r10033 | /** | ||
Jonathan Frederic
|
r19511 | * Merge the selected cell into the cell above it. | ||
David Wyde
|
r10033 | */ | ||
Brian Granger
|
r5898 | Notebook.prototype.merge_cell_above = function () { | ||
Brian Granger
|
r5945 | var index = this.get_selected_index(); | ||
Brian Granger
|
r5946 | var cell = this.get_cell(index); | ||
Brian E. Granger
|
r14028 | var render = cell.rendered; | ||
MinRK
|
r12509 | if (!cell.is_mergeable()) { | ||
return; | ||||
} | ||||
Brian Granger
|
r5898 | if (index > 0) { | ||
Mikhail Korobov
|
r8839 | var upper_cell = this.get_cell(index-1); | ||
MinRK
|
r12509 | if (!upper_cell.is_mergeable()) { | ||
return; | ||||
} | ||||
Mikhail Korobov
|
r8839 | var upper_text = upper_cell.get_text(); | ||
var text = cell.get_text(); | ||||
Jonathan Frederic
|
r17203 | if (cell instanceof codecell.CodeCell) { | ||
Brian Granger
|
r5946 | cell.set_text(upper_text+'\n'+text); | ||
Paul Ivanov
|
r17417 | } else { | ||
Brian E. Granger
|
r14028 | cell.unrender(); // Must unrender before we set_text. | ||
Brian E. Granger
|
r14026 | cell.set_text(upper_text+'\n\n'+text); | ||
Brian E. Granger
|
r14028 | if (render) { | ||
// The rendered state of the final cell should match | ||||
// that of the original selected cell; | ||||
cell.render(); | ||||
} | ||||
MinRK
|
r15236 | } | ||
Brian Granger
|
r5946 | this.delete_cell(index-1); | ||
this.select(this.find_cell_index(cell)); | ||||
MinRK
|
r15236 | } | ||
Brian Granger
|
r5898 | }; | ||
David Wyde
|
r10033 | /** | ||
Jonathan Frederic
|
r19511 | * Merge the selected cell into the cell below it. | ||
David Wyde
|
r10033 | */ | ||
Brian Granger
|
r5898 | Notebook.prototype.merge_cell_below = function () { | ||
Brian Granger
|
r5945 | var index = this.get_selected_index(); | ||
Brian Granger
|
r5946 | var cell = this.get_cell(index); | ||
Brian E. Granger
|
r14028 | var render = cell.rendered; | ||
MinRK
|
r12509 | if (!cell.is_mergeable()) { | ||
return; | ||||
} | ||||
Brian Granger
|
r5898 | if (index < this.ncells()-1) { | ||
Mikhail Korobov
|
r8839 | var lower_cell = this.get_cell(index+1); | ||
MinRK
|
r12509 | if (!lower_cell.is_mergeable()) { | ||
return; | ||||
} | ||||
Mikhail Korobov
|
r8839 | var lower_text = lower_cell.get_text(); | ||
var text = cell.get_text(); | ||||
Jonathan Frederic
|
r17203 | if (cell instanceof codecell.CodeCell) { | ||
Brian Granger
|
r5946 | cell.set_text(text+'\n'+lower_text); | ||
Paul Ivanov
|
r17417 | } else { | ||
Brian E. Granger
|
r14028 | cell.unrender(); // Must unrender before we set_text. | ||
Brian E. Granger
|
r14026 | cell.set_text(text+'\n\n'+lower_text); | ||
Brian E. Granger
|
r14028 | if (render) { | ||
// The rendered state of the final cell should match | ||||
// that of the original selected cell; | ||||
cell.render(); | ||||
} | ||||
MinRK
|
r15236 | } | ||
Brian Granger
|
r5946 | this.delete_cell(index+1); | ||
this.select(this.find_cell_index(cell)); | ||||
MinRK
|
r15236 | } | ||
Brian Granger
|
r5898 | }; | ||
Brian Granger
|
r5959 | |||
Brian E. Granger
|
r4543 | // Cell collapsing and output clearing | ||
Brian E. Granger
|
r4352 | |||
David Wyde
|
r10033 | /** | ||
* Hide a cell's output. | ||||
* | ||||
Jonathan Frederic
|
r19511 | * @param {integer} index - cell index | ||
David Wyde
|
r10033 | */ | ||
Brian E. Granger
|
r14867 | Notebook.prototype.collapse_output = function (index) { | ||
Brian E. Granger
|
r4352 | var i = this.index_or_selected(index); | ||
Brian E. Granger
|
r14867 | var cell = this.get_cell(i); | ||
Jonathan Frederic
|
r17203 | if (cell !== null && (cell instanceof codecell.CodeCell)) { | ||
Brian E. Granger
|
r14867 | cell.collapse_output(); | ||
this.set_dirty(true); | ||||
} | ||||
}; | ||||
/** | ||||
* Hide each code cell's output area. | ||||
*/ | ||||
Notebook.prototype.collapse_all_output = function () { | ||||
Matthias Bussonnier
|
r18410 | this.get_cells().map(function (cell, i) { | ||
Jonathan Frederic
|
r17203 | if (cell instanceof codecell.CodeCell) { | ||
Brian E. Granger
|
r14870 | cell.collapse_output(); | ||
Brian E. Granger
|
r14867 | } | ||
Brian E. Granger
|
r14870 | }); | ||
Brian E. Granger
|
r14867 | // this should not be set if the `collapse` key is removed from nbformat | ||
MinRK
|
r10781 | this.set_dirty(true); | ||
Brian Granger
|
r4299 | }; | ||
David Wyde
|
r10033 | /** | ||
* Show a cell's output. | ||||
* | ||||
Jonathan Frederic
|
r19511 | * @param {integer} index - cell index | ||
David Wyde
|
r10033 | */ | ||
Brian E. Granger
|
r14867 | Notebook.prototype.expand_output = function (index) { | ||
Brian E. Granger
|
r4352 | var i = this.index_or_selected(index); | ||
Brian E. Granger
|
r14867 | var cell = this.get_cell(i); | ||
Jonathan Frederic
|
r17203 | if (cell !== null && (cell instanceof codecell.CodeCell)) { | ||
Brian E. Granger
|
r14867 | cell.expand_output(); | ||
this.set_dirty(true); | ||||
} | ||||
Brian E. Granger
|
r4352 | }; | ||
Brian E. Granger
|
r14867 | /** | ||
* Expand each code cell's output area, and remove scrollbars. | ||||
David Wyde
|
r10033 | */ | ||
Brian E. Granger
|
r14867 | Notebook.prototype.expand_all_output = function () { | ||
Matthias Bussonnier
|
r18410 | this.get_cells().map(function (cell, i) { | ||
Jonathan Frederic
|
r17203 | if (cell instanceof codecell.CodeCell) { | ||
Brian E. Granger
|
r14870 | cell.expand_output(); | ||
Brian E. Granger
|
r14867 | } | ||
Brian E. Granger
|
r14870 | }); | ||
Brian E. Granger
|
r14867 | // this should not be set if the `collapse` key is removed from nbformat | ||
MinRK
|
r10781 | this.set_dirty(true); | ||
Brian E. Granger
|
r4639 | }; | ||
David Wyde
|
r10033 | /** | ||
Brian E. Granger
|
r14867 | * Clear the selected CodeCell's output area. | ||
David Wyde
|
r10033 | * | ||
Jonathan Frederic
|
r19511 | * @param {integer} index - cell index | ||
David Wyde
|
r10033 | */ | ||
Brian E. Granger
|
r14867 | Notebook.prototype.clear_output = function (index) { | ||
MinRK
|
r7429 | var i = this.index_or_selected(index); | ||
Brian E. Granger
|
r14867 | var cell = this.get_cell(i); | ||
Jonathan Frederic
|
r17203 | if (cell !== null && (cell instanceof codecell.CodeCell)) { | ||
Brian E. Granger
|
r14867 | cell.clear_output(); | ||
this.set_dirty(true); | ||||
} | ||||
MinRK
|
r7429 | }; | ||
David Wyde
|
r10033 | /** | ||
Brian E. Granger
|
r14867 | * Clear each code cell's output area. | ||
David Wyde
|
r10033 | */ | ||
Brian E. Granger
|
r14867 | Notebook.prototype.clear_all_output = function () { | ||
Matthias Bussonnier
|
r18410 | this.get_cells().map(function (cell, i) { | ||
Jonathan Frederic
|
r17203 | if (cell instanceof codecell.CodeCell) { | ||
Brian E. Granger
|
r14870 | cell.clear_output(); | ||
MinRK
|
r7362 | } | ||
Brian E. Granger
|
r14870 | }); | ||
MinRK
|
r10781 | this.set_dirty(true); | ||
MinRK
|
r7362 | }; | ||
David Wyde
|
r10033 | /** | ||
Brian E. Granger
|
r14867 | * Scroll the selected CodeCell's output area. | ||
* | ||||
Jonathan Frederic
|
r19511 | * @param {integer} index - cell index | ||
Brian E. Granger
|
r14867 | */ | ||
Notebook.prototype.scroll_output = function (index) { | ||||
var i = this.index_or_selected(index); | ||||
var cell = this.get_cell(i); | ||||
Jonathan Frederic
|
r17203 | if (cell !== null && (cell instanceof codecell.CodeCell)) { | ||
Brian E. Granger
|
r14867 | cell.scroll_output(); | ||
this.set_dirty(true); | ||||
} | ||||
}; | ||||
/** | ||||
Jonathan Frederic
|
r19511 | * Expand each code cell's output area and add a scrollbar for long output. | ||
David Wyde
|
r10033 | */ | ||
MinRK
|
r7362 | Notebook.prototype.scroll_all_output = function () { | ||
Matthias Bussonnier
|
r18410 | this.get_cells().map(function (cell, i) { | ||
Jonathan Frederic
|
r17203 | if (cell instanceof codecell.CodeCell) { | ||
Brian E. Granger
|
r14870 | cell.scroll_output(); | ||
MinRK
|
r7362 | } | ||
Brian E. Granger
|
r14870 | }); | ||
MinRK
|
r7362 | // this should not be set if the `collapse` key is removed from nbformat | ||
MinRK
|
r10781 | this.set_dirty(true); | ||
MinRK
|
r7362 | }; | ||
Jonathan Frederic
|
r19511 | /** | ||
* Toggle whether a cell's output is collapsed or expanded. | ||||
David Wyde
|
r10033 | * | ||
Jonathan Frederic
|
r19511 | * @param {integer} index - cell index | ||
David Wyde
|
r10033 | */ | ||
Brian E. Granger
|
r14867 | Notebook.prototype.toggle_output = function (index) { | ||
var i = this.index_or_selected(index); | ||||
var cell = this.get_cell(i); | ||||
Jonathan Frederic
|
r17203 | if (cell !== null && (cell instanceof codecell.CodeCell)) { | ||
Brian E. Granger
|
r14867 | cell.toggle_output(); | ||
this.set_dirty(true); | ||||
} | ||||
MinRK
|
r7362 | }; | ||
David Wyde
|
r10033 | /** | ||
Jonathan Frederic
|
r19511 | * Toggle the output of all cells. | ||
Brian E. Granger
|
r14871 | */ | ||
Notebook.prototype.toggle_all_output = function () { | ||||
Matthias Bussonnier
|
r18410 | this.get_cells().map(function (cell, i) { | ||
Jonathan Frederic
|
r17203 | if (cell instanceof codecell.CodeCell) { | ||
Brian E. Granger
|
r14871 | cell.toggle_output(); | ||
} | ||||
}); | ||||
// this should not be set if the `collapse` key is removed from nbformat | ||||
this.set_dirty(true); | ||||
}; | ||||
/** | ||||
Brian E. Granger
|
r14867 | * Toggle a scrollbar for long cell outputs. | ||
David Wyde
|
r10033 | * | ||
Jonathan Frederic
|
r19511 | * @param {integer} index - cell index | ||
David Wyde
|
r10033 | */ | ||
Brian E. Granger
|
r14867 | Notebook.prototype.toggle_output_scroll = function (index) { | ||
var i = this.index_or_selected(index); | ||||
var cell = this.get_cell(i); | ||||
Jonathan Frederic
|
r17203 | if (cell !== null && (cell instanceof codecell.CodeCell)) { | ||
Brian E. Granger
|
r14867 | cell.toggle_output_scroll(); | ||
this.set_dirty(true); | ||||
} | ||||
Brian E. Granger
|
r4543 | }; | ||
Brian E. Granger
|
r14871 | /** | ||
* Toggle the scrolling of long output on all cells. | ||||
*/ | ||||
Notebook.prototype.toggle_all_output_scroll = function () { | ||||
Matthias Bussonnier
|
r18410 | this.get_cells().map(function (cell, i) { | ||
Jonathan Frederic
|
r17203 | if (cell instanceof codecell.CodeCell) { | ||
Brian E. Granger
|
r14871 | cell.toggle_output_scroll(); | ||
} | ||||
}); | ||||
// this should not be set if the `collapse` key is removed from nbformat | ||||
this.set_dirty(true); | ||||
}; | ||||
Brian Granger
|
r5959 | |||
Fernando Perez
|
r5020 | // Other cell functions: line numbers, ... | ||
David Wyde
|
r10033 | /** | ||
* Toggle line numbers in the selected cell's input area. | ||||
*/ | ||||
Fernando Perez
|
r5020 | Notebook.prototype.cell_toggle_line_numbers = function() { | ||
Brian Granger
|
r5945 | this.get_selected_cell().toggle_line_numbers(); | ||
Fernando Perez
|
r5020 | }; | ||
Thomas Kluyver
|
r17376 | |||
/** | ||||
* Set the codemirror mode for all code cells, including the default for | ||||
* new code cells. | ||||
*/ | ||||
Notebook.prototype.set_codemirror_mode = function(newmode){ | ||||
if (newmode === this.codemirror_mode) { | ||||
return; | ||||
} | ||||
this.codemirror_mode = newmode; | ||||
Thomas Kluyver
|
r17383 | codecell.CodeCell.options_default.cm_config.mode = newmode; | ||
MinRK
|
r18596 | |||
Min RK
|
r18752 | var that = this; | ||
Nicholas Bollweg (Nick)
|
r19290 | utils.requireCodeMirrorMode(newmode, function (spec) { | ||
Matthias Bussonnier
|
r18410 | that.get_cells().map(function(cell, i) { | ||
Thomas Kluyver
|
r17383 | if (cell.cell_type === 'code'){ | ||
Nicholas Bollweg (Nick)
|
r19290 | cell.code_mirror.setOption('mode', spec); | ||
Thomas Kluyver
|
r17376 | // This is currently redundant, because cm_config ends up as | ||
// codemirror's own .options object, but I don't want to | ||||
// rely on that. | ||||
Nicholas Bollweg (Nick)
|
r19290 | cell.cm_config.mode = spec; | ||
Thomas Kluyver
|
r17376 | } | ||
Thomas Kluyver
|
r17383 | }); | ||
MinRK
|
r18596 | }); | ||
Thomas Kluyver
|
r17376 | }; | ||
Brian E. Granger
|
r4543 | |||
Zachary Sailer
|
r12986 | // Session related things | ||
Brian Granger
|
r4315 | |||
David Wyde
|
r10033 | /** | ||
Zachary Sailer
|
r12986 | * Start a new session and set it on each code cell. | ||
David Wyde
|
r10033 | */ | ||
Thomas Kluyver
|
r17370 | Notebook.prototype.start_session = function (kernel_name) { | ||
MinRK
|
r17649 | if (this._session_starting) { | ||
throw new session.SessionAlreadyStarting(); | ||||
} | ||||
this._session_starting = true; | ||||
Jessica B. Hamrick
|
r18222 | |||
var options = { | ||||
Jonathan Frederic
|
r17212 | base_url: this.base_url, | ||
MinRK
|
r17308 | ws_url: this.ws_url, | ||
Jonathan Frederic
|
r17212 | notebook_path: this.notebook_path, | ||
notebook_name: this.notebook_name, | ||||
Thomas Kluyver
|
r17370 | kernel_name: kernel_name, | ||
Jessica B. Hamrick
|
r18222 | notebook: this | ||
}; | ||||
var success = $.proxy(this._session_started, this); | ||||
var failure = $.proxy(this._session_start_failed, this); | ||||
Thomas Kluyver
|
r17223 | |||
Jessica B. Hamrick
|
r18222 | if (this.session !== null) { | ||
this.session.restart(options, success, failure); | ||||
} else { | ||||
this.session = new session.Session(options); | ||||
this.session.start(success, failure); | ||||
} | ||||
Brian E. Granger
|
r4545 | }; | ||
Zachary Sailer
|
r12999 | |||
/** | ||||
Jonathan Frederic
|
r14342 | * Once a session is started, link the code cells to the kernel and pass the | ||
Jonathan Frederic
|
r19511 | * comm manager to the widget manager. | ||
David Wyde
|
r10033 | */ | ||
MinRK
|
r17649 | Notebook.prototype._session_started = function (){ | ||
this._session_starting = false; | ||||
MinRK
|
r13133 | this.kernel = this.session.kernel; | ||
Brian Granger
|
r7197 | var ncells = this.ncells(); | ||
for (var i=0; i<ncells; i++) { | ||||
var cell = this.get_cell(i); | ||||
Jonathan Frederic
|
r17203 | if (cell instanceof codecell.CodeCell) { | ||
MinRK
|
r13133 | cell.set_kernel(this.session.kernel); | ||
MinRK
|
r15236 | } | ||
} | ||||
Brian E. Granger
|
r4545 | }; | ||
Jonathan Frederic
|
r19511 | |||
Jonathan Frederic
|
r19503 | /** | ||
Jonathan Frederic
|
r19511 | * Called when the session fails to start. | ||
Jonathan Frederic
|
r19503 | */ | ||
Jonathan Frederic
|
r19511 | Notebook.prototype._session_start_failed = function(jqxhr, status, error){ | ||
MinRK
|
r17649 | this._session_starting = false; | ||
utils.log_ajax_error(jqxhr, status, error); | ||||
}; | ||||
Jonathan Frederic
|
r17870 | |||
David Wyde
|
r10033 | /** | ||
* Prompt the user to restart the IPython kernel. | ||||
*/ | ||||
Brian E. Granger
|
r4545 | Notebook.prototype.restart_kernel = function () { | ||
Fernando Perez
|
r5025 | var that = this; | ||
Jonathan Frederic
|
r17202 | dialog.modal({ | ||
Jonathan Frederic
|
r17212 | notebook: this, | ||
keyboard_manager: this.keyboard_manager, | ||||
MinRK
|
r10895 | title : "Restart kernel or continue running?", | ||
Matthias BUSSONNIER
|
r14634 | body : $("<p/>").text( | ||
MinRK
|
r10895 | 'Do you want to restart the current kernel? You will lose all variables defined in it.' | ||
), | ||||
Fernando Perez
|
r5021 | buttons : { | ||
MinRK
|
r10895 | "Continue running" : {}, | ||
"Restart" : { | ||||
"class" : "btn-danger", | ||||
"click" : function() { | ||||
Jessica B. Hamrick
|
r18199 | that.kernel.restart(); | ||
MinRK
|
r10895 | } | ||
Brian E. Granger
|
r4545 | } | ||
} | ||||
}); | ||||
}; | ||||
Zachary Sailer
|
r12994 | |||
David Wyde
|
r10033 | /** | ||
Brian E. Granger
|
r14085 | * Execute or render cell outputs and go into command mode. | ||
David Wyde
|
r10033 | */ | ||
Brian E. Granger
|
r14085 | Notebook.prototype.execute_cell = function () { | ||
Jonathan Frederic
|
r19503 | // mode = shift, ctrl, alt | ||
Brian E. Granger
|
r14085 | var cell = this.get_selected_cell(); | ||
cell.execute(); | ||||
Jonathan Frederic
|
r15493 | this.command_mode(); | ||
Brian E. Granger
|
r14085 | this.set_dirty(true); | ||
MinRK
|
r15236 | }; | ||
Brian E. Granger
|
r14085 | |||
/** | ||||
* Execute or render cell outputs and insert a new cell below. | ||||
*/ | ||||
Notebook.prototype.execute_cell_and_insert_below = function () { | ||||
Brian E. Granger
|
r14016 | var cell = this.get_selected_cell(); | ||
var cell_index = this.find_cell_index(cell); | ||||
Brian E. Granger
|
r14015 | |||
cell.execute(); | ||||
Brian E. Granger
|
r14016 | |||
// If we are at the end always insert a new cell and return | ||||
Brian E. Granger
|
r14085 | if (cell_index === (this.ncells()-1)) { | ||
Brian E. Granger
|
r15669 | this.command_mode(); | ||
Paul Ivanov
|
r16778 | this.insert_cell_below(); | ||
Brian E. Granger
|
r15669 | this.select(cell_index+1); | ||
this.edit_mode(); | ||||
Brian E. Granger
|
r14016 | this.scroll_to_bottom(); | ||
this.set_dirty(true); | ||||
return; | ||||
Brian E. Granger
|
r14085 | } | ||
Brian E. Granger
|
r15669 | |||
this.command_mode(); | ||||
Paul Ivanov
|
r16778 | this.insert_cell_below(); | ||
Brian E. Granger
|
r15669 | this.select(cell_index+1); | ||
this.edit_mode(); | ||||
Brian E. Granger
|
r14085 | this.set_dirty(true); | ||
}; | ||||
/** | ||||
* Execute or render cell outputs and select the next cell. | ||||
*/ | ||||
Notebook.prototype.execute_cell_and_select_below = function () { | ||||
var cell = this.get_selected_cell(); | ||||
var cell_index = this.find_cell_index(cell); | ||||
cell.execute(); | ||||
// If we are at the end always insert a new cell and return | ||||
if (cell_index === (this.ncells()-1)) { | ||||
Brian E. Granger
|
r15669 | this.command_mode(); | ||
Paul Ivanov
|
r16778 | this.insert_cell_below(); | ||
Brian E. Granger
|
r15669 | this.select(cell_index+1); | ||
this.edit_mode(); | ||||
Brian E. Granger
|
r14085 | this.scroll_to_bottom(); | ||
this.set_dirty(true); | ||||
return; | ||||
Brian E. Granger
|
r14015 | } | ||
Brian E. Granger
|
r14085 | |||
Jonathan Frederic
|
r15493 | this.command_mode(); | ||
Brian E. Granger
|
r15669 | this.select(cell_index+1); | ||
Jonathan Frederic
|
r15749 | this.focus_cell(); | ||
MinRK
|
r10781 | this.set_dirty(true); | ||
Brian E. Granger
|
r4378 | }; | ||
David Wyde
|
r10033 | /** | ||
* Execute all cells below the selected cell. | ||||
*/ | ||||
Paul Ivanov
|
r8606 | Notebook.prototype.execute_cells_below = function () { | ||
this.execute_cell_range(this.get_selected_index(), this.ncells()); | ||||
Matthias BUSSONNIER
|
r8821 | this.scroll_to_bottom(); | ||
Paul Ivanov
|
r8606 | }; | ||
David Wyde
|
r10033 | /** | ||
* Execute all cells above the selected cell. | ||||
*/ | ||||
Paul Ivanov
|
r8606 | Notebook.prototype.execute_cells_above = function () { | ||
this.execute_cell_range(0, this.get_selected_index()); | ||||
}; | ||||
David Wyde
|
r10033 | /** | ||
* Execute all cells. | ||||
*/ | ||||
Brian E. Granger
|
r4378 | Notebook.prototype.execute_all_cells = function () { | ||
Paul Ivanov
|
r8606 | this.execute_cell_range(0, this.ncells()); | ||
Matthias BUSSONNIER
|
r9816 | this.scroll_to_bottom(); | ||
Paul Ivanov
|
r8606 | }; | ||
David Wyde
|
r10033 | /** | ||
* Execute a contiguous range of cells. | ||||
* | ||||
Jonathan Frederic
|
r19511 | * @param {integer} start - index of the first cell to execute (inclusive) | ||
* @param {integer} end - index of the last cell to execute (exclusive) | ||||
David Wyde
|
r10033 | */ | ||
Paul Ivanov
|
r8606 | Notebook.prototype.execute_cell_range = function (start, end) { | ||
Brian E. Granger
|
r15669 | this.command_mode(); | ||
Paul Ivanov
|
r8606 | for (var i=start; i<end; i++) { | ||
Brian E. Granger
|
r4378 | this.select(i); | ||
Brian E. Granger
|
r14085 | this.execute_cell(); | ||
MinRK
|
r15236 | } | ||
Brian E. Granger
|
r4378 | }; | ||
Brian E. Granger
|
r4352 | // Persistance and loading | ||
David Wyde
|
r10033 | /** | ||
* Getter method for this notebook's name. | ||||
* | ||||
Jonathan Frederic
|
r19509 | * @return {string} This notebook's name (excluding file extension) | ||
David Wyde
|
r10033 | */ | ||
Brian Granger
|
r6047 | Notebook.prototype.get_notebook_name = function () { | ||
Zachary Sailer
|
r13032 | var nbname = this.notebook_name.substring(0,this.notebook_name.length-6); | ||
Zachary Sailer
|
r13005 | return nbname; | ||
Brian Granger
|
r6047 | }; | ||
David Wyde
|
r10033 | /** | ||
* Setter method for this notebook's name. | ||||
* | ||||
Jonathan Frederic
|
r19511 | * @param {string} name | ||
David Wyde
|
r10033 | */ | ||
Brian Granger
|
r6047 | Notebook.prototype.set_notebook_name = function (name) { | ||
Min RK
|
r18752 | var parent = utils.url_path_split(this.notebook_path)[0]; | ||
Brian Granger
|
r6047 | this.notebook_name = name; | ||
Min RK
|
r18752 | this.notebook_path = utils.url_path_join(parent, name); | ||
Brian Granger
|
r6047 | }; | ||
David Wyde
|
r10033 | /** | ||
* Check that a notebook's name is valid. | ||||
* | ||||
Jonathan Frederic
|
r19509 | * @param {string} nbname - A name for this notebook | ||
* @return {boolean} True if the name is valid, false if invalid | ||||
David Wyde
|
r10033 | */ | ||
Brian Granger
|
r6047 | Notebook.prototype.test_notebook_name = function (nbname) { | ||
nbname = nbname || ''; | ||||
MinRK
|
r15236 | if (nbname.length>0 && !this.notebook_name_blacklist_re.test(nbname)) { | ||
Brian Granger
|
r6047 | return true; | ||
} else { | ||||
return false; | ||||
MinRK
|
r15236 | } | ||
Brian Granger
|
r6047 | }; | ||
David Wyde
|
r10033 | /** | ||
* Load a notebook from JSON (.ipynb). | ||||
* | ||||
Jonathan Frederic
|
r19511 | * @param {object} data - JSON representation of a notebook | ||
David Wyde
|
r10033 | */ | ||
Brian E. Granger
|
r4352 | Notebook.prototype.fromJSON = function (data) { | ||
MinRK
|
r18249 | |||
Zachary Sailer
|
r13032 | var content = data.content; | ||
Brian E. Granger
|
r4352 | var ncells = this.ncells(); | ||
Stefan van der Walt
|
r5479 | var i; | ||
for (i=0; i<ncells; i++) { | ||||
Brian E. Granger
|
r4352 | // Always delete cell 0 as they get renumbered as they are deleted. | ||
Bussonnier Matthias
|
r19588 | this._unsafe_delete_cell(0); | ||
MinRK
|
r15236 | } | ||
Brian Granger
|
r6047 | // Save the metadata and name. | ||
Zachary Sailer
|
r13032 | this.metadata = content.metadata; | ||
this.notebook_name = data.name; | ||||
Min RK
|
r18752 | this.notebook_path = data.path; | ||
MinRK
|
r15657 | var trusted = true; | ||
Thomas Kluyver
|
r17377 | |||
Thomas Kluyver
|
r18468 | // Set the codemirror mode from language_info metadata | ||
if (this.metadata.language_info !== undefined) { | ||||
var langinfo = this.metadata.language_info; | ||||
// Mode 'null' should be plain, unhighlighted text. | ||||
Min RK
|
r19021 | var cm_mode = langinfo.codemirror_mode || langinfo.name || 'null'; | ||
Thomas Kluyver
|
r18468 | this.set_codemirror_mode(cm_mode); | ||
} | ||||
MinRK
|
r18584 | var new_cells = content.cells; | ||
ncells = new_cells.length; | ||||
var cell_data = null; | ||||
var new_cell = null; | ||||
for (i=0; i<ncells; i++) { | ||||
cell_data = new_cells[i]; | ||||
new_cell = this.insert_cell_at_index(cell_data.cell_type, i); | ||||
new_cell.fromJSON(cell_data); | ||||
Matthias Bussonnier
|
r19739 | if (new_cell.cell_type === 'code' && !new_cell.output_area.trusted) { | ||
MinRK
|
r18584 | trusted = false; | ||
MinRK
|
r15236 | } | ||
} | ||||
Jason Grout
|
r17833 | if (trusted !== this.trusted) { | ||
MinRK
|
r15657 | this.trusted = trusted; | ||
MinRK
|
r18196 | this.events.trigger("trust_changed.Notebook", trusted); | ||
MinRK
|
r15657 | } | ||
Brian Granger
|
r4315 | }; | ||
Brian E. Granger
|
r4352 | |||
David Wyde
|
r10033 | /** | ||
* Dump this notebook into a JSON-friendly object. | ||||
* | ||||
Jonathan Frederic
|
r19511 | * @return {object} A JSON-friendly representation of this notebook. | ||
David Wyde
|
r10033 | */ | ||
Brian E. Granger
|
r4352 | Notebook.prototype.toJSON = function () { | ||
Jonathan Frederic
|
r19503 | // remove the conversion indicator, which only belongs in-memory | ||
MinRK
|
r18584 | delete this.metadata.orig_nbformat; | ||
delete this.metadata.orig_nbformat_minor; | ||||
Brian Granger
|
r5945 | var cells = this.get_cells(); | ||
Brian E. Granger
|
r4352 | var ncells = cells.length; | ||
Brian Granger
|
r7179 | var cell_array = new Array(ncells); | ||
MinRK
|
r15657 | var trusted = true; | ||
Brian E. Granger
|
r4352 | for (var i=0; i<ncells; i++) { | ||
MinRK
|
r15657 | var cell = cells[i]; | ||
Matthias Bussonnier
|
r19739 | if (cell.cell_type === 'code' && !cell.output_area.trusted) { | ||
MinRK
|
r15657 | trusted = false; | ||
} | ||||
cell_array[i] = cell.toJSON(); | ||||
MinRK
|
r15236 | } | ||
Brian Granger
|
r7179 | var data = { | ||
MinRK
|
r18584 | cells: cell_array, | ||
Thomas Kluyver
|
r18675 | metadata: this.metadata, | ||
nbformat: this.nbformat, | ||||
nbformat_minor: this.nbformat_minor | ||||
Stefan van der Walt
|
r5479 | }; | ||
Matthias Bussonnier
|
r19739 | if (trusted !== this.trusted) { | ||
MinRK
|
r15657 | this.trusted = trusted; | ||
Jonathan Frederic
|
r17195 | this.events.trigger("trust_changed.Notebook", trusted); | ||
MinRK
|
r15657 | } | ||
Stefan van der Walt
|
r5479 | return data; | ||
Brian E. Granger
|
r4484 | }; | ||
David Wyde
|
r10033 | /** | ||
Jonathan Frederic
|
r19511 | * Start an autosave timer which periodically saves the notebook. | ||
MinRK
|
r10505 | * | ||
Jonathan Frederic
|
r19509 | * @param {integer} interval - the autosave interval in milliseconds | ||
MinRK
|
r10505 | */ | ||
MinRK
|
r10508 | Notebook.prototype.set_autosave_interval = function (interval) { | ||
MinRK
|
r10505 | var that = this; | ||
// clear previous interval, so we don't get simultaneous timers | ||||
if (this.autosave_timer) { | ||||
clearInterval(this.autosave_timer); | ||||
} | ||||
Min RK
|
r19005 | if (!this.writable) { | ||
// disable autosave if not writable | ||||
interval = 0; | ||||
} | ||||
MinRK
|
r10505 | |||
MinRK
|
r10508 | this.autosave_interval = this.minimum_autosave_interval = interval; | ||
MinRK
|
r10505 | if (interval) { | ||
this.autosave_timer = setInterval(function() { | ||||
MinRK
|
r10506 | if (that.dirty) { | ||
that.save_notebook(); | ||||
} | ||||
MinRK
|
r10505 | }, interval); | ||
Jonathan Frederic
|
r17195 | this.events.trigger("autosave_enabled.Notebook", interval); | ||
MinRK
|
r10505 | } else { | ||
this.autosave_timer = null; | ||||
Jonathan Frederic
|
r17195 | this.events.trigger("autosave_disabled.Notebook"); | ||
MinRK
|
r15236 | } | ||
MinRK
|
r10505 | }; | ||
/** | ||||
Paul Ivanov
|
r15881 | * Save this notebook on the server. This becomes a notebook instance's | ||
* .save_notebook method *after* the entire notebook has been loaded. | ||||
David Wyde
|
r10033 | */ | ||
Min RK
|
r20188 | Notebook.prototype.save_notebook = function (check_last_modified) { | ||
if (check_last_modified === undefined) { | ||||
check_last_modified = true; | ||||
} | ||||
Min RK
|
r19005 | if (!this._fully_loaded) { | ||
Bussonnier Matthias
|
r18992 | this.events.trigger('notebook_save_failed.Notebook', | ||
new Error("Load failed, save is disabled") | ||||
); | ||||
Bussonnier Matthias
|
r18993 | return; | ||
Min RK
|
r19005 | } else if (!this.writable) { | ||
this.events.trigger('notebook_save_failed.Notebook', | ||||
new Error("Notebook is read-only") | ||||
); | ||||
return; | ||||
Matthias Bussonnier
|
r18991 | } | ||
Min RK
|
r19005 | |||
Jonathan Frederic
|
r19359 | // Trigger an event before save, which allows listeners to modify | ||
// the notebook as needed. | ||||
this.events.trigger('before_save.Notebook'); | ||||
Zachary Sailer
|
r13032 | // Create a JSON model to be sent to the server. | ||
Thomas Kluyver
|
r18653 | var model = { | ||
type : "notebook", | ||||
Thomas Kluyver
|
r18675 | content : this.toJSON() | ||
Thomas Kluyver
|
r18653 | }; | ||
MinRK
|
r10505 | // time the ajax call for autosave tuning purposes. | ||
var start = new Date().getTime(); | ||||
Thomas Kluyver
|
r18653 | |||
var that = this; | ||||
Min RK
|
r20188 | var _save = function () { | ||
return that.contents.save(that.notebook_path, model).then( | ||||
$.proxy(that.save_notebook_success, that, start), | ||||
Thomas Kluyver
|
r18829 | function (error) { | ||
Min RK
|
r18752 | that.events.trigger('notebook_save_failed.Notebook', error); | ||
Thomas Kluyver
|
r18653 | } | ||
Thomas Kluyver
|
r18829 | ); | ||
Min RK
|
r20188 | }; | ||
if (check_last_modified) { | ||||
return this.contents.get(this.notebook_path, {content: false}).then( | ||||
function (data) { | ||||
var last_modified = new Date(data.last_modified); | ||||
if (last_modified > that.last_modified) { | ||||
dialog.modal({ | ||||
notebook: that, | ||||
keyboard_manager: that.keyboard_manager, | ||||
title: "Notebook changed", | ||||
body: "Notebook has changed since we opened it. Overwrite the changed file?", | ||||
buttons: { | ||||
Cancel: {}, | ||||
Overwrite: { | ||||
class: 'btn-danger', | ||||
click: function () { | ||||
_save(); | ||||
} | ||||
}, | ||||
} | ||||
}); | ||||
} else { | ||||
return _save(); | ||||
} | ||||
}, function (error) { | ||||
// maybe it has been deleted or renamed? Go ahead and save. | ||||
return _save(); | ||||
} | ||||
); | ||||
} else { | ||||
return _save(); | ||||
} | ||||
Brian Granger
|
r4315 | }; | ||
MinRK
|
r10501 | |||
David Wyde
|
r10033 | /** | ||
* Success callback for saving a notebook. | ||||
* | ||||
Jonathan Frederic
|
r19509 | * @param {integer} start - Time when the save request start | ||
Jonathan Frederic
|
r19511 | * @param {object} data - JSON representation of a notebook | ||
David Wyde
|
r10033 | */ | ||
Kester Tong
|
r18661 | Notebook.prototype.save_notebook_success = function (start, data) { | ||
MinRK
|
r10781 | this.set_dirty(false); | ||
Min RK
|
r20188 | this.last_modified = new Date(data.last_modified); | ||
MinRK
|
r18249 | if (data.message) { | ||
// save succeeded, but validation failed. | ||||
var body = $("<div>"); | ||||
var title = "Notebook validation failed"; | ||||
body.append($("<p>").text( | ||||
"The save operation succeeded," + | ||||
" but the notebook does not appear to be valid." + | ||||
" The validation error was:" | ||||
)).append($("<div>").addClass("validation-error").append( | ||||
$("<pre>").text(data.message) | ||||
)); | ||||
dialog.modal({ | ||||
notebook: this, | ||||
keyboard_manager: this.keyboard_manager, | ||||
title: title, | ||||
body: body, | ||||
buttons : { | ||||
OK : { | ||||
"class" : "btn-primary" | ||||
} | ||||
} | ||||
}); | ||||
} | ||||
Jonathan Frederic
|
r17195 | this.events.trigger('notebook_saved.Notebook'); | ||
MinRK
|
r10505 | this._update_autosave_interval(start); | ||
MinRK
|
r10501 | if (this._checkpoint_after_save) { | ||
this.create_checkpoint(); | ||||
this._checkpoint_after_save = false; | ||||
MinRK
|
r15236 | } | ||
Stefan van der Walt
|
r5479 | }; | ||
MinRK
|
r10501 | |||
David Wyde
|
r10033 | /** | ||
Jonathan Frederic
|
r19511 | * Update the autosave interval based on the duration of the last save. | ||
MinRK
|
r10505 | * | ||
Jonathan Frederic
|
r19509 | * @param {integer} timestamp - when the save request started | ||
MinRK
|
r10505 | */ | ||
Notebook.prototype._update_autosave_interval = function (start) { | ||||
var duration = (new Date().getTime() - start); | ||||
if (this.autosave_interval) { | ||||
// new save interval: higher of 10x save duration or parameter (default 30 seconds) | ||||
var interval = Math.max(10 * duration, this.minimum_autosave_interval); | ||||
// round to 10 seconds, otherwise we will be setting a new interval too often | ||||
interval = 10000 * Math.round(interval / 10000); | ||||
// set new interval, if it's changed | ||||
Matthias Bussonnier
|
r19739 | if (interval !== this.autosave_interval) { | ||
MinRK
|
r10508 | this.set_autosave_interval(interval); | ||
MinRK
|
r10505 | } | ||
} | ||||
}; | ||||
Zachary Sailer
|
r12997 | |||
MinRK
|
r15655 | /** | ||
* Explicitly trust the output of this notebook. | ||||
*/ | ||||
Thomas Kluyver
|
r18774 | Notebook.prototype.trust_notebook = function () { | ||
MinRK
|
r15655 | var body = $("<div>").append($("<p>") | ||
MinRK
|
r15673 | .text("A trusted IPython notebook may execute hidden malicious code ") | ||
.append($("<strong>") | ||||
.append( | ||||
$("<em>").text("when you open it") | ||||
) | ||||
).append(".").append( | ||||
" Selecting trust will immediately reload this notebook in a trusted state." | ||||
).append( | ||||
" For more information, see the " | ||||
MinRK
|
r16046 | ).append($("<a>").attr("href", "http://ipython.org/ipython-doc/2/notebook/security.html") | ||
MinRK
|
r15673 | .text("IPython security documentation") | ||
).append(".") | ||||
); | ||||
MinRK
|
r15655 | |||
var nb = this; | ||||
Jonathan Frederic
|
r17202 | dialog.modal({ | ||
Jonathan Frederic
|
r17212 | notebook: this, | ||
keyboard_manager: this.keyboard_manager, | ||||
MinRK
|
r15655 | title: "Trust this notebook?", | ||
body: body, | ||||
buttons: { | ||||
Cancel : {}, | ||||
Trust : { | ||||
class : "btn-danger", | ||||
click : function () { | ||||
MinRK
|
r15663 | var cells = nb.get_cells(); | ||
for (var i = 0; i < cells.length; i++) { | ||||
var cell = cells[i]; | ||||
Matthias Bussonnier
|
r19739 | if (cell.cell_type === 'code') { | ||
MinRK
|
r15663 | cell.output_area.trusted = true; | ||
} | ||||
} | ||||
Jason Grout
|
r17833 | nb.events.on('notebook_saved.Notebook', function () { | ||
MinRK
|
r15663 | window.location.reload(); | ||
}); | ||||
nb.save_notebook(); | ||||
MinRK
|
r15655 | } | ||
} | ||||
} | ||||
}); | ||||
}; | ||||
Jonathan Frederic
|
r19508 | /** | ||
* Make a copy of the current notebook. | ||||
*/ | ||||
Min RK
|
r19005 | Notebook.prototype.copy_notebook = function () { | ||
var that = this; | ||||
MinRK
|
r15238 | var base_url = this.base_url; | ||
Matthias Bussonnier
|
r20914 | var w = window.open('', IPython._target); | ||
Min RK
|
r18752 | var parent = utils.url_path_split(this.notebook_path)[0]; | ||
Thomas Kluyver
|
r18829 | this.contents.copy(this.notebook_path, parent).then( | ||
function (data) { | ||||
Thomas Kluyver
|
r18774 | w.location = utils.url_join_encode( | ||
Min RK
|
r18752 | base_url, 'notebooks', data.path | ||
Thomas Kluyver
|
r18774 | ); | ||
}, | ||||
Thomas Kluyver
|
r18829 | function(error) { | ||
Thomas Kluyver
|
r18774 | w.close(); | ||
Min RK
|
r19005 | that.events.trigger('notebook_copy_failed', error); | ||
Thomas Kluyver
|
r18829 | } | ||
); | ||||
Zachary Sailer
|
r13017 | }; | ||
Min RK
|
r19684 | |||
/** | ||||
* Ensure a filename has the right extension | ||||
* Returns the filename with the appropriate extension, appending if necessary. | ||||
*/ | ||||
Notebook.prototype.ensure_extension = function (name) { | ||||
if (!name.match(/\.ipynb$/)) { | ||||
name = name + ".ipynb"; | ||||
} | ||||
return name; | ||||
}; | ||||
Zachary Sailer
|
r12997 | |||
Jonathan Frederic
|
r19508 | /** | ||
Jonathan Frederic
|
r19511 | * Rename the notebook. | ||
Jonathan Frederic
|
r19508 | * @param {string} new_name | ||
* @return {Promise} promise that resolves when the notebook is renamed. | ||||
*/ | ||||
Thomas Kluyver
|
r18653 | Notebook.prototype.rename = function (new_name) { | ||
Min RK
|
r19684 | new_name = this.ensure_extension(new_name); | ||
Brian E. Granger
|
r14965 | |||
var that = this; | ||||
Min RK
|
r18752 | var parent = utils.url_path_split(this.notebook_path)[0]; | ||
var new_path = utils.url_path_join(parent, new_name); | ||||
Min RK
|
r18964 | return this.contents.rename(this.notebook_path, new_path).then( | ||
Thomas Kluyver
|
r18829 | function (json) { | ||
Min RK
|
r18752 | that.notebook_name = json.name; | ||
that.notebook_path = json.path; | ||||
Min RK
|
r20835 | that.last_modified = new Date(json.last_modified); | ||
Min RK
|
r18752 | that.session.rename_notebook(json.path); | ||
Thomas Kluyver
|
r18654 | that.events.trigger('notebook_renamed.Notebook', json); | ||
Min RK
|
r18964 | } | ||
Thomas Kluyver
|
r18829 | ); | ||
Brian E. Granger
|
r14965 | }; | ||
Jonathan Frederic
|
r19508 | /** | ||
* Delete this notebook | ||||
*/ | ||||
KesterTong
|
r18634 | Notebook.prototype.delete = function () { | ||
Min RK
|
r18752 | this.contents.delete(this.notebook_path); | ||
MinRK
|
r15236 | }; | ||
Zachary Sailer
|
r13018 | |||
David Wyde
|
r10033 | /** | ||
* Request a notebook's data from the server. | ||||
* | ||||
Jonathan Frederic
|
r19509 | * @param {string} notebook_path - A notebook to load | ||
David Wyde
|
r10033 | */ | ||
Min RK
|
r18752 | Notebook.prototype.load_notebook = function (notebook_path) { | ||
Min RK
|
r19884 | var that = this; | ||
MinRK
|
r13063 | this.notebook_path = notebook_path; | ||
Min RK
|
r18752 | this.notebook_name = utils.url_path_split(this.notebook_path)[1]; | ||
Jonathan Frederic
|
r17195 | this.events.trigger('notebook_loading.Notebook'); | ||
Thomas Kluyver
|
r18827 | this.contents.get(notebook_path, {type: 'notebook'}).then( | ||
Min RK
|
r19885 | $.proxy(this.load_notebook_success, this), | ||
Thomas Kluyver
|
r18827 | $.proxy(this.load_notebook_error, this) | ||
); | ||||
Stefan van der Walt
|
r5479 | }; | ||
Brian E. Granger
|
r4352 | |||
David Wyde
|
r10033 | /** | ||
* Success callback for loading a notebook from the server. | ||||
* | ||||
* Load notebook data from the JSON response. | ||||
* | ||||
Jonathan Frederic
|
r19511 | * @param {object} data JSON representation of a notebook | ||
David Wyde
|
r10033 | */ | ||
Kester Tong
|
r18661 | Notebook.prototype.load_notebook_success = function (data) { | ||
Min RK
|
r18752 | var failed, msg; | ||
MinRK
|
r18249 | try { | ||
this.fromJSON(data); | ||||
} catch (e) { | ||||
failed = e; | ||||
console.log("Notebook failed to load from JSON:", e); | ||||
} | ||||
if (failed || data.message) { | ||||
// *either* fromJSON failed or validation failed | ||||
var body = $("<div>"); | ||||
var title; | ||||
if (failed) { | ||||
title = "Notebook failed to load"; | ||||
body.append($("<p>").text( | ||||
"The error was: " | ||||
)).append($("<div>").addClass("js-error").text( | ||||
failed.toString() | ||||
)).append($("<p>").text( | ||||
"See the error console for details." | ||||
)); | ||||
} else { | ||||
title = "Notebook validation failed"; | ||||
} | ||||
if (data.message) { | ||||
if (failed) { | ||||
Min RK
|
r18752 | msg = "The notebook also failed validation:"; | ||
MinRK
|
r18249 | } else { | ||
msg = "An invalid notebook may not function properly." + | ||||
Min RK
|
r18752 | " The validation error was:"; | ||
MinRK
|
r18249 | } | ||
body.append($("<p>").text( | ||||
msg | ||||
)).append($("<div>").addClass("validation-error").append( | ||||
$("<pre>").text(data.message) | ||||
)); | ||||
} | ||||
dialog.modal({ | ||||
notebook: this, | ||||
keyboard_manager: this.keyboard_manager, | ||||
title: title, | ||||
body: body, | ||||
buttons : { | ||||
OK : { | ||||
"class" : "btn-primary" | ||||
} | ||||
} | ||||
}); | ||||
} | ||||
Brian E. Granger
|
r4484 | if (this.ncells() === 0) { | ||
Brian Granger
|
r5945 | this.insert_cell_below('code'); | ||
Jonathan Frederic
|
r15541 | this.edit_mode(0); | ||
Brian E. Granger
|
r14015 | } else { | ||
this.select(0); | ||||
Brian E. Granger
|
r15629 | this.handle_command_mode(this.get_cell(0)); | ||
MinRK
|
r15236 | } | ||
MinRK
|
r10781 | this.set_dirty(false); | ||
Brian Granger
|
r5950 | this.scroll_to_top(); | ||
Min RK
|
r19005 | this.writable = data.writable || false; | ||
Min RK
|
r20188 | this.last_modified = new Date(data.last_modified); | ||
MinRK
|
r18584 | var nbmodel = data.content; | ||
var orig_nbformat = nbmodel.metadata.orig_nbformat; | ||||
var orig_nbformat_minor = nbmodel.metadata.orig_nbformat_minor; | ||||
if (orig_nbformat !== undefined && nbmodel.nbformat !== orig_nbformat) { | ||||
Min RK
|
r18672 | var src; | ||
v923z
|
r18686 | if (nbmodel.nbformat > orig_nbformat) { | ||
Min RK
|
r18672 | src = " an older notebook format "; | ||
} else { | ||||
src = " a newer notebook format "; | ||||
} | ||||
Min RK
|
r18752 | msg = "This notebook has been converted from" + src + | ||
v923z
|
r18686 | "(v"+orig_nbformat+") to the current notebook " + | ||
v923z
|
r18685 | "format (v"+nbmodel.nbformat+"). The next time you save this notebook, the " + | ||
Min RK
|
r18672 | "current notebook format will be used."; | ||
v923z
|
r18686 | if (nbmodel.nbformat > orig_nbformat) { | ||
Min RK
|
r18672 | msg += " Older versions of IPython may not be able to read the new format."; | ||
} else { | ||||
msg += " Some features of the original notebook may not be available."; | ||||
} | ||||
msg += " To preserve the original version, close the " + | ||||
"notebook without saving it."; | ||||
Jonathan Frederic
|
r17202 | dialog.modal({ | ||
Jonathan Frederic
|
r17212 | notebook: this, | ||
keyboard_manager: this.keyboard_manager, | ||||
MinRK
|
r10895 | title : "Notebook converted", | ||
body : msg, | ||||
Brian Granger
|
r6061 | buttons : { | ||
MinRK
|
r10895 | OK : { | ||
class : "btn-primary" | ||||
Brian Granger
|
r6061 | } | ||
MinRK
|
r10895 | } | ||
Brian Granger
|
r6061 | }); | ||
Min RK
|
r19107 | } else if (this.nbformat_minor < nbmodel.nbformat_minor) { | ||
this.nbformat_minor = nbmodel.nbformat_minor; | ||||
Brian Granger
|
r6061 | } | ||
Matthias Bussonnier
|
r20399 | |||
MinRK
|
r15236 | if (this.session === null) { | ||
Min RK
|
r20192 | var kernel_name = utils.get_url_param('kernel_name'); | ||
Min RK
|
r19886 | if (kernel_name) { | ||
this.kernel_selector.set_kernel(kernel_name); | ||||
Min RK
|
r20192 | } else if (this.metadata.kernelspec) { | ||
this.kernel_selector.set_kernel(this.metadata.kernelspec); | ||||
Matthias Bussonnier
|
r20399 | } else if (this.metadata.language) { | ||
// compat with IJulia, IHaskell, and other early kernels | ||||
Matthias Bussonnier
|
r20418 | // adopters that where setting a language metadata. | ||
Matthias Bussonnier
|
r20402 | this.kernel_selector.set_kernel({ | ||
name: "(No name)", | ||||
language: this.metadata.language | ||||
}); | ||||
Matthias Bussonnier
|
r20419 | // this should be stored in kspec now, delete it. | ||
Matthias Bussonnier
|
r20399 | // remove once we do not support notebook v3 anymore. | ||
Matthias Bussonnier
|
r20402 | delete this.metadata.language; | ||
Min RK
|
r19886 | } else { | ||
Min RK
|
r20192 | // setting kernel via set_kernel above triggers start_session, | ||
// otherwise start a new session with the server's default kernel | ||||
Min RK
|
r19886 | // spec_changed events will fire after kernel is loaded | ||
this.start_session(); | ||||
} | ||||
Zachary Sailer
|
r12986 | } | ||
MinRK
|
r11644 | // load our checkpoint list | ||
MinRK
|
r13679 | this.list_checkpoints(); | ||
// load toolbar state | ||||
if (this.metadata.celltoolbar) { | ||||
Jonathan Frederic
|
r17202 | celltoolbar.CellToolbar.global_show(); | ||
Matthias BUSSONNIER
|
r17454 | celltoolbar.CellToolbar.activate_preset(this.metadata.celltoolbar); | ||
Raffaele De Feo
|
r16534 | } else { | ||
Jonathan Frederic
|
r17202 | celltoolbar.CellToolbar.global_hide(); | ||
MinRK
|
r13679 | } | ||
Min RK
|
r19005 | |||
if (!this.writable) { | ||||
this.set_autosave_interval(0); | ||||
this.events.trigger('notebook_read_only.Notebook'); | ||||
} | ||||
Paul Ivanov
|
r15881 | // now that we're fully loaded, it is safe to restore save functionality | ||
Matthias Bussonnier
|
r18991 | this._fully_loaded = true; | ||
Jonathan Frederic
|
r17195 | this.events.trigger('notebook_loaded.Notebook'); | ||
Brian E. Granger
|
r4484 | }; | ||
Matthias BUSSONNIER
|
r19404 | Notebook.prototype.set_kernelselector = function(k_selector){ | ||
this.kernel_selector = k_selector; | ||||
}; | ||||
David Wyde
|
r10033 | /** | ||
* Failure callback for loading a notebook from the server. | ||||
* | ||||
Kester Tong
|
r18661 | * @param {Error} error | ||
*/ | ||||
Notebook.prototype.load_notebook_error = function (error) { | ||||
this.events.trigger('notebook_load_failed.Notebook', error); | ||||
var msg; | ||||
Min RK
|
r18752 | if (error.name === utils.XHR_ERROR && error.xhr.status === 500) { | ||
Kester Tong
|
r18661 | utils.log_ajax_error(error.xhr, error.xhr_status, error.xhr_error); | ||
msg = "An unknown error occurred while loading this notebook. " + | ||||
MinRK
|
r11643 | "This version can load notebook formats " + | ||
Kester Tong
|
r18661 | "v" + this.nbformat + " or earlier. See the server log for details."; | ||
} else { | ||||
msg = error.message; | ||||
Bussonnier Matthias
|
r20142 | console.warn('Error stack trace while loading notebook was:'); | ||
console.warn(error.stack); | ||||
Brian Granger
|
r6061 | } | ||
Jonathan Frederic
|
r17202 | dialog.modal({ | ||
Jonathan Frederic
|
r17212 | notebook: this, | ||
keyboard_manager: this.keyboard_manager, | ||||
MinRK
|
r11643 | title: "Error loading notebook", | ||
body : msg, | ||||
buttons : { | ||||
"OK": {} | ||||
} | ||||
}); | ||||
MinRK
|
r15236 | }; | ||
Mikhail Korobov
|
r8839 | |||
Jonathan Frederic
|
r19511 | /********************* checkpoint-related ********************/ | ||
MinRK
|
r10501 | |||
/** | ||||
* Save the notebook then immediately create a checkpoint. | ||||
*/ | ||||
Notebook.prototype.save_checkpoint = function () { | ||||
this._checkpoint_after_save = true; | ||||
this.save_notebook(); | ||||
}; | ||||
/** | ||||
MinRK
|
r12050 | * Add a checkpoint for this notebook. | ||
*/ | ||||
Notebook.prototype.add_checkpoint = function (checkpoint) { | ||||
var found = false; | ||||
for (var i = 0; i < this.checkpoints.length; i++) { | ||||
var existing = this.checkpoints[i]; | ||||
Matthias Bussonnier
|
r19739 | if (existing.id === checkpoint.id) { | ||
MinRK
|
r12050 | found = true; | ||
this.checkpoints[i] = checkpoint; | ||||
break; | ||||
} | ||||
} | ||||
if (!found) { | ||||
this.checkpoints.push(checkpoint); | ||||
} | ||||
this.last_checkpoint = this.checkpoints[this.checkpoints.length - 1]; | ||||
}; | ||||
/** | ||||
MinRK
|
r10501 | * List checkpoints for this notebook. | ||
*/ | ||||
Notebook.prototype.list_checkpoints = function () { | ||||
Thomas Kluyver
|
r18652 | var that = this; | ||
Thomas Kluyver
|
r18829 | this.contents.list_checkpoints(this.notebook_path).then( | ||
$.proxy(this.list_checkpoints_success, this), | ||||
function(error) { | ||||
Min RK
|
r18752 | that.events.trigger('list_checkpoints_failed.Notebook', error); | ||
Thomas Kluyver
|
r18652 | } | ||
Thomas Kluyver
|
r18829 | ); | ||
MinRK
|
r10501 | }; | ||
/** | ||||
* Success callback for listing checkpoints. | ||||
* | ||||
Jonathan Frederic
|
r19511 | * @param {object} data - JSON representation of a checkpoint | ||
MinRK
|
r10501 | */ | ||
Kester Tong
|
r18661 | Notebook.prototype.list_checkpoints_success = function (data) { | ||
MinRK
|
r12050 | this.checkpoints = data; | ||
MinRK
|
r10501 | if (data.length) { | ||
MinRK
|
r12050 | this.last_checkpoint = data[data.length - 1]; | ||
MinRK
|
r10501 | } else { | ||
this.last_checkpoint = null; | ||||
} | ||||
Jonathan Frederic
|
r17195 | this.events.trigger('checkpoints_listed.Notebook', [data]); | ||
MinRK
|
r10501 | }; | ||
/** | ||||
* Create a checkpoint of this notebook on the server from the most recent save. | ||||
*/ | ||||
Notebook.prototype.create_checkpoint = function () { | ||||
Thomas Kluyver
|
r18652 | var that = this; | ||
Thomas Kluyver
|
r18829 | this.contents.create_checkpoint(this.notebook_path).then( | ||
$.proxy(this.create_checkpoint_success, this), | ||||
function (error) { | ||||
Min RK
|
r18752 | that.events.trigger('checkpoint_failed.Notebook', error); | ||
Thomas Kluyver
|
r18652 | } | ||
Thomas Kluyver
|
r18829 | ); | ||
MinRK
|
r10501 | }; | ||
/** | ||||
* Success callback for creating a checkpoint. | ||||
* | ||||
Jonathan Frederic
|
r19511 | * @param {object} data - JSON representation of a checkpoint | ||
MinRK
|
r10501 | */ | ||
Kester Tong
|
r18661 | Notebook.prototype.create_checkpoint_success = function (data) { | ||
MinRK
|
r12050 | this.add_checkpoint(data); | ||
Jonathan Frederic
|
r17195 | this.events.trigger('checkpoint_created.Notebook', data); | ||
MinRK
|
r10501 | }; | ||
Jonathan Frederic
|
r19509 | /** | ||
* Display the restore checkpoint dialog | ||||
* @param {string} checkpoint ID | ||||
*/ | ||||
MinRK
|
r10520 | Notebook.prototype.restore_checkpoint_dialog = function (checkpoint) { | ||
MinRK
|
r10501 | var that = this; | ||
MinRK
|
r15236 | checkpoint = checkpoint || this.last_checkpoint; | ||
MinRK
|
r10501 | if ( ! checkpoint ) { | ||
console.log("restore dialog, but no checkpoint to restore to!"); | ||||
return; | ||||
} | ||||
MinRK
|
r10895 | var body = $('<div/>').append( | ||
MinRK
|
r10520 | $('<p/>').addClass("p-space").text( | ||
"Are you sure you want to revert the notebook to " + | ||||
MinRK
|
r10501 | "the latest checkpoint?" | ||
).append( | ||||
$("<strong/>").text( | ||||
" This cannot be undone." | ||||
) | ||||
) | ||||
).append( | ||||
MinRK
|
r10520 | $('<p/>').addClass("p-space").text("The checkpoint was last updated at:") | ||
MinRK
|
r10501 | ).append( | ||
MinRK
|
r10520 | $('<p/>').addClass("p-space").text( | ||
Matthias Bussonnier
|
r20211 | moment(checkpoint.last_modified).format('LLLL') + | ||
' ('+moment(checkpoint.last_modified).fromNow()+')'// Long form: Tuesday, January 27, 2015 12:15 PM | ||||
MinRK
|
r10520 | ).css("text-align", "center") | ||
MinRK
|
r10501 | ); | ||
Jonathan Frederic
|
r17202 | dialog.modal({ | ||
Jonathan Frederic
|
r17212 | notebook: this, | ||
keyboard_manager: this.keyboard_manager, | ||||
MinRK
|
r10895 | title : "Revert notebook to checkpoint", | ||
body : body, | ||||
MinRK
|
r10501 | buttons : { | ||
MinRK
|
r10895 | Revert : { | ||
class : "btn-danger", | ||||
click : function () { | ||||
MinRK
|
r13122 | that.restore_checkpoint(checkpoint.id); | ||
MinRK
|
r10895 | } | ||
MinRK
|
r10501 | }, | ||
MinRK
|
r10895 | Cancel : {} | ||
MinRK
|
r10501 | } | ||
}); | ||||
MinRK
|
r15236 | }; | ||
MinRK
|
r10501 | |||
/** | ||||
* Restore the notebook to a checkpoint state. | ||||
* | ||||
Jonathan Frederic
|
r19509 | * @param {string} checkpoint ID | ||
MinRK
|
r10501 | */ | ||
Notebook.prototype.restore_checkpoint = function (checkpoint) { | ||||
Jonathan Frederic
|
r17195 | this.events.trigger('notebook_restoring.Notebook', checkpoint); | ||
Thomas Kluyver
|
r18652 | var that = this; | ||
Thomas Kluyver
|
r18829 | this.contents.restore_checkpoint(this.notebook_path, checkpoint).then( | ||
$.proxy(this.restore_checkpoint_success, this), | ||||
function (error) { | ||||
Min RK
|
r18752 | that.events.trigger('checkpoint_restore_failed.Notebook', error); | ||
Thomas Kluyver
|
r18652 | } | ||
Thomas Kluyver
|
r18829 | ); | ||
MinRK
|
r10501 | }; | ||
/** | ||||
* Success callback for restoring a notebook to a checkpoint. | ||||
*/ | ||||
Kester Tong
|
r18661 | Notebook.prototype.restore_checkpoint_success = function () { | ||
Jonathan Frederic
|
r17195 | this.events.trigger('checkpoint_restored.Notebook'); | ||
Min RK
|
r18752 | this.load_notebook(this.notebook_path); | ||
MinRK
|
r10501 | }; | ||
/** | ||||
* Delete a notebook checkpoint. | ||||
* | ||||
Jonathan Frederic
|
r19509 | * @param {string} checkpoint ID | ||
MinRK
|
r10501 | */ | ||
Notebook.prototype.delete_checkpoint = function (checkpoint) { | ||||
Jonathan Frederic
|
r17195 | this.events.trigger('notebook_restoring.Notebook', checkpoint); | ||
Thomas Kluyver
|
r18652 | var that = this; | ||
Thomas Kluyver
|
r18829 | this.contents.delete_checkpoint(this.notebook_path, checkpoint).then( | ||
$.proxy(this.delete_checkpoint_success, this), | ||||
function (error) { | ||||
Kester Tong
|
r18661 | that.events.trigger('checkpoint_delete_failed.Notebook', error); | ||
Thomas Kluyver
|
r18652 | } | ||
Thomas Kluyver
|
r18829 | ); | ||
MinRK
|
r10501 | }; | ||
/** | ||||
Jonathan Frederic
|
r19511 | * Success callback for deleting a notebook checkpoint. | ||
MinRK
|
r10501 | */ | ||
Kester Tong
|
r18661 | Notebook.prototype.delete_checkpoint_success = function () { | ||
this.events.trigger('checkpoint_deleted.Notebook'); | ||||
Min RK
|
r18752 | this.load_notebook(this.notebook_path); | ||
MinRK
|
r10501 | }; | ||
Jonathan Frederic
|
r17192 | // For backwards compatability. | ||
Brian E. Granger
|
r4352 | IPython.Notebook = Notebook; | ||
Jonathan Frederic
|
r17201 | return {'Notebook': Notebook}; | ||
Jonathan Frederic
|
r17192 | }); | ||