notebook.js
1371 lines
| 49.2 KiB
| application/javascript
|
JavascriptLexer
|
r4609 | //---------------------------------------------------------------------------- | ||
// Copyright (C) 2008-2011 The IPython Development Team | ||||
// | ||||
// Distributed under the terms of the BSD License. The full license is in | ||||
// the file COPYING, distributed as part of this software. | ||||
//---------------------------------------------------------------------------- | ||||
|
r4315 | |||
|
r4299 | //============================================================================ | ||
|
r4292 | // Notebook | ||
//============================================================================ | ||||
|
r4352 | var IPython = (function (IPython) { | ||
|
r4356 | var utils = IPython.utils; | ||
|
r7139 | var key = IPython.utils.keycodes; | ||
|
r4356 | |||
|
r4352 | var Notebook = function (selector) { | ||
|
r5213 | this.read_only = IPython.read_only; | ||
|
r4352 | this.element = $(selector); | ||
this.element.scroll(); | ||||
this.element.data("notebook", this); | ||||
this.next_prompt_number = 1; | ||||
this.kernel = null; | ||||
|
r5879 | this.clipboard = null; | ||
|
r8691 | this.undelete_backup = null; | ||
this.undelete_index = null; | ||||
this.undelete_below = false; | ||||
|
r5912 | this.paste_enabled = false; | ||
|
r4550 | this.dirty = false; | ||
|
r4637 | this.metadata = {}; | ||
|
r7523 | // single worksheet for now | ||
this.worksheet_metadata = {}; | ||||
|
r4645 | this.control_key_active = false; | ||
|
r6047 | this.notebook_id = null; | ||
this.notebook_name = null; | ||||
|
r7229 | this.notebook_name_blacklist_re = /[\/\\:]/; | ||
|
r6061 | this.nbformat = 3 // Increment this when changing the nbformat | ||
|
r7546 | this.nbformat_minor = 0 // Increment this when changing the nbformat | ||
|
r4357 | this.style(); | ||
|
r4364 | this.create_elements(); | ||
|
r4352 | this.bind_events(); | ||
}; | ||||
|
r4292 | |||
|
r4352 | |||
|
r4357 | Notebook.prototype.style = function () { | ||
|
r4363 | $('div#notebook').addClass('border-box-sizing'); | ||
|
r4357 | }; | ||
|
r4364 | Notebook.prototype.create_elements = function () { | ||
// We add this end_space div to the end of the notebook div to: | ||||
// i) provide a margin between the last cell and the end of the notebook | ||||
// ii) to prevent the div from scrolling up when the last cell is being | ||||
// edited, but is too low on the page, which browsers will do automatically. | ||||
|
r4628 | var that = this; | ||
|
r5945 | var end_space = $('<div/>').addClass('end_space').height("30%"); | ||
|
r4628 | end_space.dblclick(function (e) { | ||
|
r5200 | if (that.read_only) return; | ||
|
r4628 | var ncells = that.ncells(); | ||
|
r5945 | that.insert_cell_below('code',ncells-1); | ||
|
r4628 | }); | ||
|
r4604 | this.element.append(end_space); | ||
|
r4364 | $('div#notebook').addClass('border-box-sizing'); | ||
}; | ||||
|
r4352 | Notebook.prototype.bind_events = function () { | ||
var that = this; | ||||
|
r7170 | |||
|
r7168 | $([IPython.events]).on('set_next_input.Notebook', function (event, data) { | ||
var index = that.find_cell_index(data.cell); | ||||
var new_cell = that.insert_cell_below('code',index); | ||||
new_cell.set_text(data.text); | ||||
that.dirty = true; | ||||
}); | ||||
|
r7228 | $([IPython.events]).on('set_dirty.Notebook', function (event, data) { | ||
that.dirty = data.value; | ||||
}); | ||||
|
r7168 | $([IPython.events]).on('select.Cell', function (event, data) { | ||
var index = that.find_cell_index(data.cell); | ||||
|
r7170 | that.select(index); | ||
|
r7168 | }); | ||
|
r4352 | $(document).keydown(function (event) { | ||
|
r4647 | // console.log(event); | ||
|
r5546 | if (that.read_only) return true; | ||
|
r8839 | |||
// Save (CTRL+S) or (AppleKey+S) | ||||
|
r5990 | //metaKey = applekey on mac | ||
|
r8839 | if ((event.ctrlKey || event.metaKey) && event.keyCode==83) { | ||
|
r6047 | that.save_notebook(); | ||
|
r5990 | event.preventDefault(); | ||
|
r5991 | return false; | ||
|
r7193 | } else if (event.which === key.ESC) { | ||
|
r8839 | // Intercept escape at highest level to avoid closing | ||
|
r5389 | // websocket connection with firefox | ||
event.preventDefault(); | ||||
|
r7723 | } else if (event.which === key.SHIFT) { | ||
// ignore shift keydown | ||||
return true; | ||||
|
r5389 | } | ||
|
r7193 | if (event.which === key.UPARROW && !event.shiftKey) { | ||
|
r5945 | var cell = that.get_selected_cell(); | ||
|
r4352 | if (cell.at_top()) { | ||
event.preventDefault(); | ||||
that.select_prev(); | ||||
}; | ||||
|
r7193 | } else if (event.which === key.DOWNARROW && !event.shiftKey) { | ||
|
r5945 | var cell = that.get_selected_cell(); | ||
|
r4352 | if (cell.at_bottom()) { | ||
event.preventDefault(); | ||||
that.select_next(); | ||||
}; | ||||
|
r7193 | } else if (event.which === key.ENTER && event.shiftKey) { | ||
|
r4394 | that.execute_selected_cell(); | ||
|
r4380 | return false; | ||
|
r7806 | } else if (event.which === key.ENTER && event.altKey) { | ||
|
r7910 | // Execute code cell, and insert new in place | ||
|
r7806 | that.execute_selected_cell(); | ||
|
r7910 | // Only insert a new cell, if we ended up in an already populated cell | ||
|
r7921 | if (/\S/.test(that.get_selected_cell().get_text()) == true) { | ||
|
r7910 | that.insert_cell_above('code'); | ||
} | ||||
|
r7806 | return false; | ||
|
r7193 | } else if (event.which === key.ENTER && event.ctrlKey) { | ||
|
r4394 | that.execute_selected_cell({terminal:true}); | ||
|
r4390 | return false; | ||
|
r6049 | } else if (event.which === 77 && event.ctrlKey && that.control_key_active == false) { | ||
|
r4645 | that.control_key_active = true; | ||
return false; | ||||
|
r5880 | } else if (event.which === 88 && that.control_key_active) { | ||
// Cut selected cell = x | ||||
that.cut_cell(); | ||||
that.control_key_active = false; | ||||
return false; | ||||
} else if (event.which === 67 && that.control_key_active) { | ||||
// Copy selected cell = c | ||||
that.copy_cell(); | ||||
that.control_key_active = false; | ||||
return false; | ||||
} else if (event.which === 86 && that.control_key_active) { | ||||
|
r8770 | // Paste below selected cell = v | ||
that.paste_cell_below(); | ||||
|
r5880 | that.control_key_active = false; | ||
return false; | ||||
|
r4645 | } else if (event.which === 68 && that.control_key_active) { | ||
// Delete selected cell = d | ||||
that.delete_cell(); | ||||
that.control_key_active = false; | ||||
return false; | ||||
} else if (event.which === 65 && that.control_key_active) { | ||||
|
r4659 | // Insert code cell above selected = a | ||
|
r5945 | that.insert_cell_above('code'); | ||
|
r4645 | that.control_key_active = false; | ||
return false; | ||||
} else if (event.which === 66 && that.control_key_active) { | ||||
|
r4659 | // Insert code cell below selected = b | ||
|
r5945 | that.insert_cell_below('code'); | ||
|
r4645 | that.control_key_active = false; | ||
return false; | ||||
|
r5880 | } else if (event.which === 89 && that.control_key_active) { | ||
// To code = y | ||||
|
r4645 | that.to_code(); | ||
that.control_key_active = false; | ||||
return false; | ||||
} else if (event.which === 77 && that.control_key_active) { | ||||
// To markdown = m | ||||
that.to_markdown(); | ||||
that.control_key_active = false; | ||||
return false; | ||||
|
r6027 | } else if (event.which === 84 && that.control_key_active) { | ||
|
r6248 | // To Raw = t | ||
that.to_raw(); | ||||
|
r6017 | that.control_key_active = false; | ||
return false; | ||||
|
r6019 | } else if (event.which === 49 && that.control_key_active) { | ||
// To Heading 1 = 1 | ||||
that.to_heading(undefined, 1); | ||||
that.control_key_active = false; | ||||
return false; | ||||
} else if (event.which === 50 && that.control_key_active) { | ||||
// To Heading 2 = 2 | ||||
that.to_heading(undefined, 2); | ||||
that.control_key_active = false; | ||||
return false; | ||||
} else if (event.which === 51 && that.control_key_active) { | ||||
// To Heading 3 = 3 | ||||
that.to_heading(undefined, 3); | ||||
that.control_key_active = false; | ||||
return false; | ||||
} else if (event.which === 52 && that.control_key_active) { | ||||
// To Heading 4 = 4 | ||||
that.to_heading(undefined, 4); | ||||
that.control_key_active = false; | ||||
return false; | ||||
} else if (event.which === 53 && that.control_key_active) { | ||||
// To Heading 5 = 5 | ||||
that.to_heading(undefined, 5); | ||||
that.control_key_active = false; | ||||
return false; | ||||
} else if (event.which === 54 && that.control_key_active) { | ||||
// To Heading 6 = 6 | ||||
that.to_heading(undefined, 6); | ||||
that.control_key_active = false; | ||||
return false; | ||||
|
r6027 | } else if (event.which === 79 && that.control_key_active) { | ||
// Toggle output = o | ||||
|
r7429 | if (event.shiftKey){ | ||
that.toggle_output_scroll(); | ||||
} else { | ||||
that.toggle_output(); | ||||
} | ||||
|
r4645 | that.control_key_active = false; | ||
return false; | ||||
} else if (event.which === 83 && that.control_key_active) { | ||||
// Save notebook = s | ||||
|
r6047 | that.save_notebook(); | ||
|
r4645 | that.control_key_active = false; | ||
return false; | ||||
} else if (event.which === 74 && that.control_key_active) { | ||||
// Move cell down = j | ||||
that.move_cell_down(); | ||||
that.control_key_active = false; | ||||
return false; | ||||
} else if (event.which === 75 && that.control_key_active) { | ||||
// Move cell up = k | ||||
that.move_cell_up(); | ||||
that.control_key_active = false; | ||||
return false; | ||||
|
r4648 | } else if (event.which === 80 && that.control_key_active) { | ||
// Select previous = p | ||||
|
r4645 | that.select_prev(); | ||
that.control_key_active = false; | ||||
return false; | ||||
|
r4648 | } else if (event.which === 78 && that.control_key_active) { | ||
// Select next = n | ||||
|
r4645 | that.select_next(); | ||
that.control_key_active = false; | ||||
return false; | ||||
|
r5026 | } else if (event.which === 76 && that.control_key_active) { | ||
// Toggle line numbers = l | ||||
that.cell_toggle_line_numbers(); | ||||
|
r4646 | that.control_key_active = false; | ||
return false; | ||||
|
r5018 | } else if (event.which === 73 && that.control_key_active) { | ||
// Interrupt kernel = i | ||||
|
r6047 | that.kernel.interrupt(); | ||
|
r5018 | that.control_key_active = false; | ||
return false; | ||||
} else if (event.which === 190 && that.control_key_active) { | ||||
// Restart kernel = . # matches qt console | ||||
|
r6047 | that.restart_kernel(); | ||
|
r5018 | that.control_key_active = false; | ||
return false; | ||||
|
r4646 | } else if (event.which === 72 && that.control_key_active) { | ||
// Show keyboard shortcuts = h | ||||
|
r5862 | IPython.quick_help.show_keyboard_shortcuts(); | ||
|
r4646 | that.control_key_active = false; | ||
return false; | ||||
|
r8691 | } else if (event.which === 90 && that.control_key_active) { | ||
|
r8693 | // Undo last cell delete = z | ||
|
r8691 | that.undelete(); | ||
that.control_key_active = false; | ||||
return false; | ||||
|
r4645 | } else if (that.control_key_active) { | ||
that.control_key_active = false; | ||||
return true; | ||||
|
r4334 | }; | ||
|
r5479 | return true; | ||
|
r4352 | }); | ||
|
r4361 | |||
|
r6723 | var collapse_time = function(time){ | ||
|
r9265 | var app_height = $('#ipython-main-app').height(); // content height | ||
|
r4362 | var splitter_height = $('div#pager_splitter').outerHeight(true); | ||
var new_height = app_height - splitter_height; | ||||
|
r6723 | that.element.animate({height : new_height + 'px'}, time); | ||
} | ||||
this.element.bind('collapse_pager', function (event,extrap) { | ||||
|
r8839 | var time = (extrap != undefined) ? ((extrap.duration != undefined ) ? extrap.duration : 'fast') : 'fast'; | ||
|
r6723 | collapse_time(time); | ||
|
r4361 | }); | ||
|
r6723 | var expand_time = function(time) { | ||
|
r9265 | var app_height = $('#ipython-main-app').height(); // content height | ||
|
r4362 | var splitter_height = $('div#pager_splitter').outerHeight(true); | ||
|
r4361 | var pager_height = $('div#pager').outerHeight(true); | ||
|
r8839 | var new_height = app_height - pager_height - splitter_height; | ||
|
r6723 | that.element.animate({height : new_height + 'px'}, time); | ||
} | ||||
this.element.bind('expand_pager', function (event, extrap) { | ||||
|
r8839 | var time = (extrap != undefined) ? ((extrap.duration != undefined ) ? extrap.duration : 'fast') : 'fast'; | ||
|
r6723 | expand_time(time); | ||
|
r4361 | }); | ||
|
r4363 | |||
|
r4542 | $(window).bind('beforeunload', function () { | ||
|
r5857 | // TODO: Make killing the kernel configurable. | ||
var kill_kernel = false; | ||||
|
r4542 | if (kill_kernel) { | ||
that.kernel.kill(); | ||||
|
r4550 | } | ||
|
r5200 | if (that.dirty && ! that.read_only) { | ||
|
r4550 | return "You have unsaved changes that will be lost if you leave this page."; | ||
|
r4542 | }; | ||
|
r5502 | // Null is the *only* return value that will make the browser not | ||
// pop up the "don't leave" dialog. | ||||
return null; | ||||
|
r4542 | }); | ||
|
r4352 | }; | ||
|
r4292 | |||
|
r8419 | Notebook.prototype.scroll_to_cell = function (cell_number, time) { | ||
var cells = this.get_cells(); | ||||
var time = time || 0; | ||||
cell_number = Math.min(cells.length-1,cell_number); | ||||
cell_number = Math.max(0 ,cell_number); | ||||
|
r8839 | var scroll_value = cells[cell_number].element.position().top-cells[0].element.position().top ; | ||
|
r8419 | this.element.animate({scrollTop:scroll_value}, time); | ||
return scroll_value; | ||||
}; | ||||
|
r4292 | |||
|
r4364 | Notebook.prototype.scroll_to_bottom = function () { | ||
|
r4366 | this.element.animate({scrollTop:this.element.get(0).scrollHeight}, 0); | ||
|
r4364 | }; | ||
|
r4488 | |||
Notebook.prototype.scroll_to_top = function () { | ||||
this.element.animate({scrollTop:0}, 0); | ||||
}; | ||||
|
r4352 | // Cell indexing, retrieval, etc. | ||
|
r4292 | |||
|
r5945 | Notebook.prototype.get_cell_elements = function () { | ||
|
r4352 | return this.element.children("div.cell"); | ||
|
r5479 | }; | ||
|
r4292 | |||
|
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; | ||||
}; | ||||
|
r4352 | Notebook.prototype.ncells = function (cell) { | ||
|
r5945 | return this.get_cell_elements().length; | ||
|
r5479 | }; | ||
|
r4292 | |||
|
r4352 | // TODO: we are often calling cells as cells()[i], which we should optimize | ||
// to cells(i) or a new method. | ||||
|
r5945 | Notebook.prototype.get_cells = function () { | ||
return this.get_cell_elements().toArray().map(function (e) { | ||||
|
r4352 | return $(e).data("cell"); | ||
}); | ||||
|
r5479 | }; | ||
|
r4292 | |||
|
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; | ||||
} | ||||
Notebook.prototype.get_next_cell = function (cell) { | ||||
var result = null; | ||||
var index = this.find_cell_index(cell); | ||||
if (index !== null && index < this.ncells()) { | ||||
result = this.get_cell(index+1); | ||||
} | ||||
return result; | ||||
} | ||||
Notebook.prototype.get_prev_cell = function (cell) { | ||||
var result = null; | ||||
var index = this.find_cell_index(cell); | ||||
if (index !== null && index > 1) { | ||||
result = this.get_cell(index-1); | ||||
} | ||||
return result; | ||||
} | ||||
|
r4352 | Notebook.prototype.find_cell_index = function (cell) { | ||
var result = null; | ||||
|
r5945 | this.get_cell_elements().filter(function (index) { | ||
|
r4352 | if ($(this).data("cell") === cell) { | ||
result = index; | ||||
}; | ||||
}); | ||||
return result; | ||||
}; | ||||
|
r4292 | |||
|
r4352 | Notebook.prototype.index_or_selected = function (index) { | ||
|
r5898 | var i; | ||
|
r5945 | if (index === undefined || index === null) { | ||
i = this.get_selected_index(); | ||||
|
r5898 | if (i === null) { | ||
i = 0; | ||||
} | ||||
} else { | ||||
i = index; | ||||
} | ||||
return i; | ||||
|
r5479 | }; | ||
|
r4292 | |||
|
r5945 | Notebook.prototype.get_selected_cell = function () { | ||
var index = this.get_selected_index(); | ||||
return this.get_cell(index); | ||||
|
r4292 | }; | ||
|
r5945 | Notebook.prototype.is_valid_cell_index = function (index) { | ||
if (index !== null && index >= 0 && index < this.ncells()) { | ||||
return true; | ||||
} else { | ||||
return false; | ||||
|
r4299 | }; | ||
|
r5945 | } | ||
|
r4299 | |||
|
r5945 | Notebook.prototype.get_selected_index = function () { | ||
|
r4352 | var result = null; | ||
|
r5945 | this.get_cell_elements().filter(function (index) { | ||
|
r4352 | if ($(this).data("cell").selected === true) { | ||
result = index; | ||||
}; | ||||
}); | ||||
return result; | ||||
}; | ||||
|
r4292 | |||
|
r5945 | // Cell selection. | ||
|
r4292 | |||
|
r5945 | Notebook.prototype.select = function (index) { | ||
if (index !== undefined && index >= 0 && index < this.ncells()) { | ||||
|
r8839 | var sindex = this.get_selected_index() | ||
|
r5946 | if (sindex !== null && index !== sindex) { | ||
|
r5945 | this.get_cell(sindex).unselect(); | ||
|
r4352 | }; | ||
|
r8839 | var cell = this.get_cell(index); | ||
|
r5994 | cell.select(); | ||
|
r6028 | if (cell.cell_type === 'heading') { | ||
|
r6047 | $([IPython.events]).trigger('selected_cell_type_changed.Notebook', | ||
{'cell_type':cell.cell_type,level:cell.level} | ||||
); | ||||
|
r6028 | } else { | ||
|
r6047 | $([IPython.events]).trigger('selected_cell_type_changed.Notebook', | ||
{'cell_type':cell.cell_type} | ||||
); | ||||
}; | ||||
|
r4352 | }; | ||
|
r4292 | return this; | ||
}; | ||||
|
r5945 | Notebook.prototype.select_next = function () { | ||
var index = this.get_selected_index(); | ||||
if (index !== null && index >= 0 && (index+1) < this.ncells()) { | ||||
this.select(index+1); | ||||
|
r4352 | }; | ||
|
r5479 | return this; | ||
|
r4292 | }; | ||
|
r4352 | |||
|
r5945 | Notebook.prototype.select_prev = function () { | ||
var index = this.get_selected_index(); | ||||
if (index !== null && index >= 0 && (index-1) < this.ncells()) { | ||||
this.select(index-1); | ||||
|
r4292 | }; | ||
|
r4352 | return this; | ||
|
r4292 | }; | ||
|
r4352 | |||
|
r5945 | // Cell movement | ||
|
r4352 | Notebook.prototype.move_cell_up = function (index) { | ||
|
r5945 | var i = this.index_or_selected(); | ||
|
r4352 | if (i !== null && i < this.ncells() && i > 0) { | ||
|
r5945 | var pivot = this.get_cell_element(i-1); | ||
var tomove = this.get_cell_element(i); | ||||
|
r4352 | if (pivot !== null && tomove !== null) { | ||
tomove.detach(); | ||||
pivot.before(tomove); | ||||
this.select(i-1); | ||||
|
r4292 | }; | ||
}; | ||||
|
r4550 | this.dirty = true; | ||
|
r4352 | return this; | ||
|
r5479 | }; | ||
|
r4352 | |||
Notebook.prototype.move_cell_down = function (index) { | ||||
|
r5945 | var i = this.index_or_selected(); | ||
|
r4352 | if (i !== null && i < (this.ncells()-1) && i >= 0) { | ||
|
r5945 | var pivot = this.get_cell_element(i+1); | ||
var tomove = this.get_cell_element(i); | ||||
|
r4352 | if (pivot !== null && tomove !== null) { | ||
tomove.detach(); | ||||
pivot.after(tomove); | ||||
this.select(i+1); | ||||
}; | ||||
}; | ||||
|
r4550 | this.dirty = true; | ||
|
r4352 | return this; | ||
|
r5479 | }; | ||
|
r4352 | |||
Notebook.prototype.sort_cells = function () { | ||||
|
r5945 | // This is not working right now. Calling this will actually crash | ||
// the browser. I think there is an infinite loop in here... | ||||
|
r4352 | var ncells = this.ncells(); | ||
|
r5945 | var sindex = this.get_selected_index(); | ||
|
r4352 | var swapped; | ||
do { | ||||
|
r5479 | swapped = false; | ||
|
r4352 | for (var i=1; i<ncells; i++) { | ||
|
r5945 | current = this.get_cell(i); | ||
previous = this.get_cell(i-1); | ||||
|
r4352 | if (previous.input_prompt_number > current.input_prompt_number) { | ||
this.move_cell_up(i); | ||||
swapped = true; | ||||
}; | ||||
}; | ||||
} while (swapped); | ||||
this.select(sindex); | ||||
return this; | ||||
|
r4292 | }; | ||
|
r5945 | // Insertion, deletion. | ||
|
r4292 | |||
|
r5945 | Notebook.prototype.delete_cell = function (index) { | ||
|
r4352 | var i = this.index_or_selected(index); | ||
|
r8691 | var cell = this.get_selected_cell(); | ||
this.undelete_backup = cell.toJSON(); | ||||
|
r5945 | if (this.is_valid_cell_index(i)) { | ||
var ce = this.get_cell_element(i); | ||||
ce.remove(); | ||||
if (i === (this.ncells())) { | ||||
this.select(i-1); | ||||
|
r8691 | this.undelete_index = i - 1; | ||
this.undelete_below = true; | ||||
|
r5945 | } else { | ||
this.select(i); | ||||
|
r8691 | this.undelete_index = i; | ||
this.undelete_below = false; | ||||
|
r5945 | }; | ||
this.dirty = true; | ||||
}; | ||||
return this; | ||||
|
r5479 | }; | ||
|
r4299 | |||
|
r8466 | Notebook.prototype.insert_cell_at_bottom = function (type){ | ||
var len = this.ncells(); | ||||
return this.insert_cell_below(type,len-1); | ||||
} | ||||
|
r5945 | Notebook.prototype.insert_cell_below = function (type, index) { | ||
// type = ('code','html','markdown') | ||||
// index = cell index or undefined to insert below selected | ||||
index = this.index_or_selected(index); | ||||
|
r6017 | var cell = null; | ||
|
r8694 | // This is intentionally < rather than <= for the sake of more | ||
// sensible behavior in some cases. | ||||
if (this.undelete_index !== null && index < this.undelete_index) { | ||||
this.undelete_index = this.undelete_index + 1; | ||||
} | ||||
|
r5945 | if (this.ncells() === 0 || this.is_valid_cell_index(index)) { | ||
if (type === 'code') { | ||||
|
r7168 | cell = new IPython.CodeCell(this.kernel); | ||
|
r5945 | cell.set_input_prompt(); | ||
} else if (type === 'markdown') { | ||||
|
r7168 | cell = new IPython.MarkdownCell(); | ||
|
r5945 | } else if (type === 'html') { | ||
|
r7168 | cell = new IPython.HTMLCell(); | ||
|
r6248 | } else if (type === 'raw') { | ||
|
r7168 | cell = new IPython.RawCell(); | ||
|
r6019 | } else if (type === 'heading') { | ||
|
r7168 | cell = new IPython.HeadingCell(); | ||
|
r5945 | }; | ||
if (cell !== null) { | ||||
if (this.ncells() === 0) { | ||||
this.element.find('div.end_space').before(cell.element); | ||||
} else if (this.is_valid_cell_index(index)) { | ||||
this.get_cell_element(index).after(cell.element); | ||||
}; | ||||
|
r5946 | cell.render(); | ||
this.select(this.find_cell_index(cell)); | ||||
this.dirty = true; | ||||
|
r5945 | return cell; | ||
}; | ||||
}; | ||||
|
r6017 | return cell; | ||
|
r5479 | }; | ||
|
r4507 | |||
|
r5945 | Notebook.prototype.insert_cell_above = function (type, index) { | ||
// type = ('code','html','markdown') | ||||
// index = cell index or undefined to insert above selected | ||||
index = this.index_or_selected(index); | ||||
|
r6017 | var cell = null; | ||
|
r8694 | if (this.undelete_index !== null && index <= this.undelete_index) { | ||
this.undelete_index = this.undelete_index + 1; | ||||
} | ||||
|
r5945 | if (this.ncells() === 0 || this.is_valid_cell_index(index)) { | ||
if (type === 'code') { | ||||
|
r7168 | cell = new IPython.CodeCell(this.kernel); | ||
|
r5945 | cell.set_input_prompt(); | ||
} else if (type === 'markdown') { | ||||
|
r7168 | cell = new IPython.MarkdownCell(); | ||
|
r5945 | } else if (type === 'html') { | ||
|
r7168 | cell = new IPython.HTMLCell(); | ||
|
r6248 | } else if (type === 'raw') { | ||
|
r7168 | cell = new IPython.RawCell(); | ||
|
r6019 | } else if (type === 'heading') { | ||
|
r7168 | cell = new IPython.HeadingCell(); | ||
|
r5945 | }; | ||
if (cell !== null) { | ||||
if (this.ncells() === 0) { | ||||
this.element.find('div.end_space').before(cell.element); | ||||
} else if (this.is_valid_cell_index(index)) { | ||||
this.get_cell_element(index).before(cell.element); | ||||
}; | ||||
|
r5946 | cell.render(); | ||
this.select(this.find_cell_index(cell)); | ||||
this.dirty = true; | ||||
|
r5945 | return cell; | ||
}; | ||||
}; | ||||
|
r6017 | return cell; | ||
|
r5479 | }; | ||
|
r4507 | |||
Notebook.prototype.to_code = function (index) { | ||||
|
r4352 | var i = this.index_or_selected(index); | ||
|
r5945 | if (this.is_valid_cell_index(i)) { | ||
var source_element = this.get_cell_element(i); | ||||
var source_cell = source_element.data("cell"); | ||||
if (!(source_cell instanceof IPython.CodeCell)) { | ||||
|
r8839 | var target_cell = this.insert_cell_below('code',i); | ||
|
r5945 | var text = source_cell.get_text(); | ||
if (text === source_cell.placeholder) { | ||||
text = ''; | ||||
} | ||||
target_cell.set_text(text); | ||||
|
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(); | ||||
|
r5945 | source_element.remove(); | ||
|
r6017 | this.dirty = true; | ||
|
r5945 | }; | ||
|
r4352 | }; | ||
}; | ||||
|
r4299 | |||
|
r4352 | |||
|
r4508 | Notebook.prototype.to_markdown = function (index) { | ||
|
r4352 | var i = this.index_or_selected(index); | ||
|
r5945 | if (this.is_valid_cell_index(i)) { | ||
var source_element = this.get_cell_element(i); | ||||
var source_cell = source_element.data("cell"); | ||||
if (!(source_cell instanceof IPython.MarkdownCell)) { | ||||
|
r8839 | var target_cell = this.insert_cell_below('markdown',i); | ||
|
r5945 | var text = source_cell.get_text(); | ||
if (text === source_cell.placeholder) { | ||||
|
r5946 | text = ''; | ||
|
r5945 | }; | ||
|
r6017 | // The edit must come before the set_text. | ||
target_cell.edit(); | ||||
target_cell.set_text(text); | ||||
|
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(); | ||||
|
r6017 | source_element.remove(); | ||
|
r5945 | this.dirty = true; | ||
|
r5944 | }; | ||
}; | ||||
|
r4507 | }; | ||
Notebook.prototype.to_html = function (index) { | ||||
var i = this.index_or_selected(index); | ||||
|
r5945 | if (this.is_valid_cell_index(i)) { | ||
var source_element = this.get_cell_element(i); | ||||
var source_cell = source_element.data("cell"); | ||||
var target_cell = null; | ||||
if (!(source_cell instanceof IPython.HTMLCell)) { | ||||
target_cell = this.insert_cell_below('html',i); | ||||
var text = source_cell.get_text(); | ||||
if (text === source_cell.placeholder) { | ||||
|
r5946 | text = ''; | ||
|
r5945 | }; | ||
|
r6017 | // The edit must come before the set_text. | ||
target_cell.edit(); | ||||
target_cell.set_text(text); | ||||
|
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(); | ||||
|
r6017 | source_element.remove(); | ||
|
r5945 | this.dirty = true; | ||
|
r5944 | }; | ||
}; | ||||
|
r4299 | }; | ||
|
r4352 | |||
|
r6248 | Notebook.prototype.to_raw = function (index) { | ||
|
r6017 | var i = this.index_or_selected(index); | ||
if (this.is_valid_cell_index(i)) { | ||||
var source_element = this.get_cell_element(i); | ||||
var source_cell = source_element.data("cell"); | ||||
var target_cell = null; | ||||
|
r6248 | if (!(source_cell instanceof IPython.RawCell)) { | ||
target_cell = this.insert_cell_below('raw',i); | ||||
|
r6017 | var text = source_cell.get_text(); | ||
if (text === source_cell.placeholder) { | ||||
text = ''; | ||||
}; | ||||
// The edit must come before the set_text. | ||||
target_cell.edit(); | ||||
target_cell.set_text(text); | ||||
|
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(); | ||||
|
r6017 | source_element.remove(); | ||
this.dirty = true; | ||||
}; | ||||
}; | ||||
}; | ||||
|
r6019 | |||
Notebook.prototype.to_heading = function (index, level) { | ||||
level = level || 1; | ||||
var i = this.index_or_selected(index); | ||||
if (this.is_valid_cell_index(i)) { | ||||
var source_element = this.get_cell_element(i); | ||||
var source_cell = source_element.data("cell"); | ||||
var target_cell = null; | ||||
if (source_cell instanceof IPython.HeadingCell) { | ||||
source_cell.set_level(level); | ||||
} else { | ||||
target_cell = this.insert_cell_below('heading',i); | ||||
var text = source_cell.get_text(); | ||||
if (text === source_cell.placeholder) { | ||||
text = ''; | ||||
}; | ||||
// The edit must come before the set_text. | ||||
target_cell.set_level(level); | ||||
target_cell.edit(); | ||||
target_cell.set_text(text); | ||||
|
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(); | ||||
|
r6019 | source_element.remove(); | ||
this.dirty = true; | ||||
}; | ||||
|
r6047 | $([IPython.events]).trigger('selected_cell_type_changed.Notebook', | ||
{'cell_type':'heading',level:level} | ||||
); | ||||
|
r6019 | }; | ||
}; | ||||
|
r5945 | // Cut/Copy/Paste | ||
|
r5879 | |||
Notebook.prototype.enable_paste = function () { | ||||
var that = this; | ||||
|
r5912 | if (!this.paste_enabled) { | ||
|
r8715 | $('#paste_cell_replace').removeClass('ui-state-disabled') | ||
.on('click', function () {that.paste_cell_replace();}); | ||||
|
r5912 | $('#paste_cell_above').removeClass('ui-state-disabled') | ||
.on('click', function () {that.paste_cell_above();}); | ||||
$('#paste_cell_below').removeClass('ui-state-disabled') | ||||
.on('click', function () {that.paste_cell_below();}); | ||||
this.paste_enabled = true; | ||||
}; | ||||
|
r5879 | }; | ||
Notebook.prototype.disable_paste = function () { | ||||
|
r5912 | if (this.paste_enabled) { | ||
|
r8715 | $('#paste_cell_replace').addClass('ui-state-disabled').off('click'); | ||
|
r5912 | $('#paste_cell_above').addClass('ui-state-disabled').off('click'); | ||
$('#paste_cell_below').addClass('ui-state-disabled').off('click'); | ||||
this.paste_enabled = false; | ||||
}; | ||||
|
r5879 | }; | ||
Notebook.prototype.cut_cell = function () { | ||||
this.copy_cell(); | ||||
this.delete_cell(); | ||||
} | ||||
Notebook.prototype.copy_cell = function () { | ||||
|
r5945 | var cell = this.get_selected_cell(); | ||
|
r5879 | this.clipboard = cell.toJSON(); | ||
this.enable_paste(); | ||||
}; | ||||
|
r8715 | Notebook.prototype.paste_cell_replace = function () { | ||
|
r5912 | if (this.clipboard !== null && this.paste_enabled) { | ||
|
r5879 | var cell_data = this.clipboard; | ||
|
r5945 | var new_cell = this.insert_cell_above(cell_data.cell_type); | ||
|
r5944 | new_cell.fromJSON(cell_data); | ||
|
r8839 | var old_cell = this.get_next_cell(new_cell); | ||
|
r5945 | this.delete_cell(this.find_cell_index(old_cell)); | ||
this.select(this.find_cell_index(new_cell)); | ||||
|
r5879 | }; | ||
}; | ||||
Notebook.prototype.paste_cell_above = function () { | ||||
|
r5912 | if (this.clipboard !== null && this.paste_enabled) { | ||
|
r5879 | var cell_data = this.clipboard; | ||
|
r5945 | var new_cell = this.insert_cell_above(cell_data.cell_type); | ||
|
r5944 | new_cell.fromJSON(cell_data); | ||
|
r5879 | }; | ||
}; | ||||
Notebook.prototype.paste_cell_below = function () { | ||||
|
r5912 | if (this.clipboard !== null && this.paste_enabled) { | ||
|
r5879 | var cell_data = this.clipboard; | ||
|
r5945 | var new_cell = this.insert_cell_below(cell_data.cell_type); | ||
|
r5944 | new_cell.fromJSON(cell_data); | ||
|
r5879 | }; | ||
}; | ||||
|
r8691 | // Cell undelete | ||
Notebook.prototype.undelete = function() { | ||||
if (this.undelete_backup !== null && this.undelete_index !== null) { | ||||
var current_index = this.get_selected_index(); | ||||
if (this.undelete_index < current_index) { | ||||
current_index = current_index + 1; | ||||
} | ||||
if (this.undelete_index >= this.ncells()) { | ||||
this.select(this.ncells() - 1); | ||||
} | ||||
else { | ||||
this.select(this.undelete_index); | ||||
} | ||||
var cell_data = this.undelete_backup; | ||||
var new_cell = null; | ||||
if (this.undelete_below) { | ||||
new_cell = this.insert_cell_below(cell_data.cell_type); | ||||
} else { | ||||
new_cell = this.insert_cell_above(cell_data.cell_type); | ||||
} | ||||
new_cell.fromJSON(cell_data); | ||||
this.select(current_index); | ||||
this.undelete_backup = null; | ||||
this.undelete_index = null; | ||||
} | ||||
} | ||||
|
r5879 | |||
|
r5945 | // Split/merge | ||
|
r5897 | Notebook.prototype.split_cell = function () { | ||
|
r5898 | // Todo: implement spliting for other cell types. | ||
|
r5945 | var cell = this.get_selected_cell(); | ||
|
r5946 | if (cell.is_splittable()) { | ||
|
r8839 | var texta = cell.get_pre_cursor(); | ||
var textb = cell.get_post_cursor(); | ||||
|
r5946 | if (cell instanceof IPython.CodeCell) { | ||
cell.set_text(texta); | ||||
var new_cell = this.insert_cell_below('code'); | ||||
new_cell.set_text(textb); | ||||
} else if (cell instanceof IPython.MarkdownCell) { | ||||
cell.set_text(texta); | ||||
cell.render(); | ||||
var new_cell = this.insert_cell_below('markdown'); | ||||
new_cell.edit(); // editor must be visible to call set_text | ||||
new_cell.set_text(textb); | ||||
new_cell.render(); | ||||
} else if (cell instanceof IPython.HTMLCell) { | ||||
cell.set_text(texta); | ||||
cell.render(); | ||||
var new_cell = this.insert_cell_below('html'); | ||||
new_cell.edit(); // editor must be visible to call set_text | ||||
new_cell.set_text(textb); | ||||
new_cell.render(); | ||||
}; | ||||
|
r5897 | }; | ||
}; | ||||
|
r5898 | |||
Notebook.prototype.merge_cell_above = function () { | ||||
|
r5945 | var index = this.get_selected_index(); | ||
|
r5946 | var cell = this.get_cell(index); | ||
|
r5898 | if (index > 0) { | ||
|
r8839 | var upper_cell = this.get_cell(index-1); | ||
var upper_text = upper_cell.get_text(); | ||||
var text = cell.get_text(); | ||||
|
r5946 | if (cell instanceof IPython.CodeCell) { | ||
cell.set_text(upper_text+'\n'+text); | ||||
} else if (cell instanceof IPython.MarkdownCell || cell instanceof IPython.HTMLCell) { | ||||
cell.edit(); | ||||
cell.set_text(upper_text+'\n'+text); | ||||
cell.render(); | ||||
|
r5898 | }; | ||
|
r5946 | this.delete_cell(index-1); | ||
this.select(this.find_cell_index(cell)); | ||||
|
r5898 | }; | ||
}; | ||||
Notebook.prototype.merge_cell_below = function () { | ||||
|
r5945 | var index = this.get_selected_index(); | ||
|
r5946 | var cell = this.get_cell(index); | ||
|
r5898 | if (index < this.ncells()-1) { | ||
|
r8839 | var lower_cell = this.get_cell(index+1); | ||
var lower_text = lower_cell.get_text(); | ||||
var text = cell.get_text(); | ||||
|
r5946 | if (cell instanceof IPython.CodeCell) { | ||
cell.set_text(text+'\n'+lower_text); | ||||
} else if (cell instanceof IPython.MarkdownCell || cell instanceof IPython.HTMLCell) { | ||||
cell.edit(); | ||||
cell.set_text(text+'\n'+lower_text); | ||||
cell.render(); | ||||
|
r5898 | }; | ||
|
r5946 | this.delete_cell(index+1); | ||
this.select(this.find_cell_index(cell)); | ||||
|
r5898 | }; | ||
}; | ||||
|
r5959 | |||
|
r4543 | // Cell collapsing and output clearing | ||
|
r4352 | |||
Notebook.prototype.collapse = function (index) { | ||||
var i = this.index_or_selected(index); | ||||
|
r5945 | this.get_cell(i).collapse(); | ||
|
r4550 | this.dirty = true; | ||
|
r4299 | }; | ||
|
r4352 | Notebook.prototype.expand = function (index) { | ||
var i = this.index_or_selected(index); | ||||
|
r5945 | this.get_cell(i).expand(); | ||
|
r4550 | this.dirty = true; | ||
|
r4352 | }; | ||
|
r4315 | |||
|
r4639 | Notebook.prototype.toggle_output = function (index) { | ||
var i = this.index_or_selected(index); | ||||
|
r5945 | this.get_cell(i).toggle_output(); | ||
|
r4639 | this.dirty = true; | ||
}; | ||||
|
r7429 | Notebook.prototype.toggle_output_scroll = function (index) { | ||
var i = this.index_or_selected(index); | ||||
this.get_cell(i).toggle_output_scroll(); | ||||
}; | ||||
|
r7362 | Notebook.prototype.collapse_all_output = function () { | ||
var ncells = this.ncells(); | ||||
var cells = this.get_cells(); | ||||
for (var i=0; i<ncells; i++) { | ||||
if (cells[i] instanceof IPython.CodeCell) { | ||||
cells[i].output_area.collapse(); | ||||
} | ||||
}; | ||||
// this should not be set if the `collapse` key is removed from nbformat | ||||
this.dirty = true; | ||||
}; | ||||
Notebook.prototype.scroll_all_output = function () { | ||||
var ncells = this.ncells(); | ||||
var cells = this.get_cells(); | ||||
for (var i=0; i<ncells; i++) { | ||||
if (cells[i] instanceof IPython.CodeCell) { | ||||
cells[i].output_area.expand(); | ||||
cells[i].output_area.scroll_if_long(20); | ||||
} | ||||
}; | ||||
// this should not be set if the `collapse` key is removed from nbformat | ||||
this.dirty = true; | ||||
}; | ||||
Notebook.prototype.expand_all_output = function () { | ||||
var ncells = this.ncells(); | ||||
var cells = this.get_cells(); | ||||
for (var i=0; i<ncells; i++) { | ||||
if (cells[i] instanceof IPython.CodeCell) { | ||||
cells[i].output_area.expand(); | ||||
cells[i].output_area.unscroll_area(); | ||||
} | ||||
}; | ||||
// this should not be set if the `collapse` key is removed from nbformat | ||||
this.dirty = true; | ||||
}; | ||||
|
r4543 | Notebook.prototype.clear_all_output = function () { | ||
var ncells = this.ncells(); | ||||
|
r5945 | var cells = this.get_cells(); | ||
|
r4543 | for (var i=0; i<ncells; i++) { | ||
if (cells[i] instanceof IPython.CodeCell) { | ||||
|
r5085 | cells[i].clear_output(true,true,true); | ||
|
r6545 | // Make all In[] prompts blank, as well | ||
// TODO: make this configurable (via checkbox?) | ||||
cells[i].set_input_prompt(); | ||||
|
r4543 | } | ||
}; | ||||
|
r4550 | this.dirty = true; | ||
|
r4543 | }; | ||
|
r5959 | |||
|
r5020 | // Other cell functions: line numbers, ... | ||
Notebook.prototype.cell_toggle_line_numbers = function() { | ||||
|
r5945 | this.get_selected_cell().toggle_line_numbers(); | ||
|
r5020 | }; | ||
|
r4543 | |||
|
r4352 | // Kernel related things | ||
|
r4315 | |||
|
r4352 | Notebook.prototype.start_kernel = function () { | ||
|
r7170 | var base_url = $('body').data('baseKernelUrl') + "kernels"; | ||
|
r7168 | this.kernel = new IPython.Kernel(base_url); | ||
this.kernel.start(this.notebook_id); | ||||
|
r7197 | // Now that the kernel has been created, tell the CodeCells about it. | ||
var ncells = this.ncells(); | ||||
for (var i=0; i<ncells; i++) { | ||||
var cell = this.get_cell(i); | ||||
if (cell instanceof IPython.CodeCell) { | ||||
cell.set_kernel(this.kernel) | ||||
}; | ||||
}; | ||||
|
r4545 | }; | ||
Notebook.prototype.restart_kernel = function () { | ||||
|
r5025 | var that = this; | ||
|
r5021 | var dialog = $('<div/>'); | ||
dialog.html('Do you want to restart the current kernel? You will lose all variables defined in it.'); | ||||
$(document).append(dialog); | ||||
dialog.dialog({ | ||||
resizable: false, | ||||
modal: true, | ||||
title: "Restart kernel or continue running?", | ||||
|
r5858 | closeText: '', | ||
|
r5021 | buttons : { | ||
"Restart": function () { | ||||
|
r7168 | that.kernel.restart(); | ||
|
r4545 | $(this).dialog('close'); | ||
}, | ||||
|
r5022 | "Continue running": function () { | ||
|
r4545 | $(this).dialog('close'); | ||
} | ||||
} | ||||
}); | ||||
}; | ||||
|
r4394 | Notebook.prototype.execute_selected_cell = function (options) { | ||
// add_new: should a new cell be added if we are at the end of the nb | ||||
// terminal: execute in terminal mode, which stays in the current cell | ||||
|
r8839 | var default_options = {terminal: false, add_new: true}; | ||
|
r5479 | $.extend(default_options, options); | ||
|
r4378 | var that = this; | ||
|
r5945 | var cell = that.get_selected_cell(); | ||
|
r4378 | var cell_index = that.find_cell_index(cell); | ||
if (cell instanceof IPython.CodeCell) { | ||||
|
r7170 | cell.execute(); | ||
|
r4499 | } else if (cell instanceof IPython.HTMLCell) { | ||
|
r4378 | cell.render(); | ||
} | ||||
|
r4394 | if (default_options.terminal) { | ||
|
r4675 | cell.select_all(); | ||
|
r4378 | } else { | ||
|
r4394 | if ((cell_index === (that.ncells()-1)) && default_options.add_new) { | ||
|
r5945 | that.insert_cell_below('code'); | ||
|
r4394 | // If we are adding a new cell at the end, scroll down to show it. | ||
that.scroll_to_bottom(); | ||||
} else { | ||||
that.select(cell_index+1); | ||||
}; | ||||
|
r4378 | }; | ||
|
r4550 | this.dirty = true; | ||
|
r4378 | }; | ||
|
r8606 | Notebook.prototype.execute_cells_below = function () { | ||
this.execute_cell_range(this.get_selected_index(), this.ncells()); | ||||
|
r8821 | this.scroll_to_bottom(); | ||
|
r8606 | }; | ||
Notebook.prototype.execute_cells_above = function () { | ||||
this.execute_cell_range(0, this.get_selected_index()); | ||||
}; | ||||
|
r4378 | Notebook.prototype.execute_all_cells = function () { | ||
|
r8606 | this.execute_cell_range(0, this.ncells()); | ||
that.scroll_to_bottom(); | ||||
}; | ||||
Notebook.prototype.execute_cell_range = function (start, end) { | ||||
for (var i=start; i<end; i++) { | ||||
|
r4378 | this.select(i); | ||
|
r5967 | this.execute_selected_cell({add_new:false}); | ||
|
r4378 | }; | ||
}; | ||||
|
r4352 | // Persistance and loading | ||
|
r6047 | Notebook.prototype.get_notebook_id = function () { | ||
return this.notebook_id; | ||||
}; | ||||
Notebook.prototype.get_notebook_name = function () { | ||||
return this.notebook_name; | ||||
}; | ||||
Notebook.prototype.set_notebook_name = function (name) { | ||||
this.notebook_name = name; | ||||
}; | ||||
Notebook.prototype.test_notebook_name = function (nbname) { | ||||
nbname = nbname || ''; | ||||
if (this.notebook_name_blacklist_re.test(nbname) == false && nbname.length>0) { | ||||
return true; | ||||
} else { | ||||
return false; | ||||
}; | ||||
}; | ||||
|
r4352 | |||
Notebook.prototype.fromJSON = function (data) { | ||||
var ncells = this.ncells(); | ||||
|
r5479 | var i; | ||
for (i=0; i<ncells; i++) { | ||||
|
r4352 | // Always delete cell 0 as they get renumbered as they are deleted. | ||
this.delete_cell(0); | ||||
}; | ||||
|
r6047 | // Save the metadata and name. | ||
|
r4637 | this.metadata = data.metadata; | ||
|
r6047 | this.notebook_name = data.metadata.name; | ||
|
r4484 | // Only handle 1 worksheet for now. | ||
var worksheet = data.worksheets[0]; | ||||
if (worksheet !== undefined) { | ||||
|
r7523 | if (worksheet.metadata) { | ||
this.worksheet_metadata = worksheet.metadata; | ||||
} | ||||
|
r4484 | var new_cells = worksheet.cells; | ||
ncells = new_cells.length; | ||||
var cell_data = null; | ||||
|
r4488 | var new_cell = null; | ||
|
r5479 | for (i=0; i<ncells; i++) { | ||
|
r4484 | cell_data = new_cells[i]; | ||
|
r6477 | // VERSIONHACK: plaintext -> raw | ||
// handle never-released plaintext name for raw cells | ||||
|
r6255 | if (cell_data.cell_type === 'plaintext'){ | ||
cell_data.cell_type = 'raw'; | ||||
} | ||||
|
r8839 | |||
|
r5945 | new_cell = this.insert_cell_below(cell_data.cell_type); | ||
new_cell.fromJSON(cell_data); | ||||
|
r5879 | }; | ||
|
r4352 | }; | ||
|
r7610 | if (data.worksheets.length > 1) { | ||
var dialog = $('<div/>'); | ||||
dialog.html("This notebook has " + data.worksheets.length + " worksheets, " + | ||||
"but this version of IPython can only handle the first. " + | ||||
"If you save this notebook, worksheets after the first will be lost." | ||||
); | ||||
this.element.append(dialog); | ||||
dialog.dialog({ | ||||
resizable: false, | ||||
modal: true, | ||||
title: "Multiple worksheets", | ||||
closeText: "", | ||||
close: function(event, ui) {$(this).dialog('destroy').remove();}, | ||||
buttons : { | ||||
"OK": function () { | ||||
$(this).dialog('close'); | ||||
} | ||||
}, | ||||
width: 400 | ||||
}); | ||||
} | ||||
|
r4315 | }; | ||
|
r4352 | |||
Notebook.prototype.toJSON = function () { | ||||
|
r5945 | var cells = this.get_cells(); | ||
|
r4352 | var ncells = cells.length; | ||
|
r7179 | var cell_array = new Array(ncells); | ||
|
r4352 | for (var i=0; i<ncells; i++) { | ||
cell_array[i] = cells[i].toJSON(); | ||||
}; | ||||
|
r7179 | var data = { | ||
|
r4484 | // Only handle 1 worksheet for now. | ||
|
r7523 | worksheets : [{ | ||
cells: cell_array, | ||||
metadata: this.worksheet_metadata | ||||
}], | ||||
|
r4637 | metadata : this.metadata | ||
|
r5479 | }; | ||
return data; | ||||
|
r4484 | }; | ||
Notebook.prototype.save_notebook = function () { | ||||
|
r5958 | // We may want to move the name/id/nbformat logic inside toJSON? | ||
var data = this.toJSON(); | ||||
|
r6047 | data.metadata.name = this.notebook_name; | ||
|
r6061 | data.nbformat = this.nbformat; | ||
|
r7546 | data.nbformat_minor = this.nbformat_minor; | ||
|
r5958 | // We do the call with settings so we can set cache to false. | ||
var settings = { | ||||
processData : false, | ||||
cache : false, | ||||
type : "PUT", | ||||
data : JSON.stringify(data), | ||||
headers : {'Content-Type': 'application/json'}, | ||||
|
r6061 | success : $.proxy(this.save_notebook_success,this), | ||
error : $.proxy(this.save_notebook_error,this) | ||||
|
r4352 | }; | ||
|
r6047 | $([IPython.events]).trigger('notebook_saving.Notebook'); | ||
var url = $('body').data('baseProjectUrl') + 'notebooks/' + this.notebook_id; | ||||
|
r5958 | $.ajax(url, settings); | ||
|
r4315 | }; | ||
|
r4352 | |||
|
r6061 | Notebook.prototype.save_notebook_success = function (data, status, xhr) { | ||
|
r4550 | this.dirty = false; | ||
|
r6047 | $([IPython.events]).trigger('notebook_saved.Notebook'); | ||
|
r5479 | }; | ||
|
r4352 | |||
|
r6061 | Notebook.prototype.save_notebook_error = function (xhr, status, error_msg) { | ||
|
r6047 | $([IPython.events]).trigger('notebook_save_failed.Notebook'); | ||
|
r5479 | }; | ||
|
r5007 | |||
|
r6047 | Notebook.prototype.load_notebook = function (notebook_id) { | ||
|
r4488 | var that = this; | ||
|
r6047 | this.notebook_id = notebook_id; | ||
|
r4352 | // We do the call with settings so we can set cache to false. | ||
var settings = { | ||||
|
r4488 | processData : false, | ||
cache : false, | ||||
type : "GET", | ||||
dataType : "json", | ||||
|
r6061 | success : $.proxy(this.load_notebook_success,this), | ||
error : $.proxy(this.load_notebook_error,this), | ||||
|
r4352 | }; | ||
|
r6047 | $([IPython.events]).trigger('notebook_loading.Notebook'); | ||
var url = $('body').data('baseProjectUrl') + 'notebooks/' + this.notebook_id; | ||||
|
r5106 | $.ajax(url, settings); | ||
|
r5479 | }; | ||
|
r4352 | |||
|
r4484 | |||
|
r6047 | Notebook.prototype.load_notebook_success = function (data, status, xhr) { | ||
|
r4484 | this.fromJSON(data); | ||
if (this.ncells() === 0) { | ||||
|
r5945 | this.insert_cell_below('code'); | ||
|
r4484 | }; | ||
|
r4550 | this.dirty = false; | ||
|
r5949 | this.select(0); | ||
|
r5950 | this.scroll_to_top(); | ||
|
r6061 | if (data.orig_nbformat !== undefined && data.nbformat !== data.orig_nbformat) { | ||
msg = "This notebook has been converted from an older " + | ||||
"notebook format (v"+data.orig_nbformat+") to the current notebook " + | ||||
"format (v"+data.nbformat+"). The next time you save this notebook, the " + | ||||
"newer notebook format will be used and older verions of IPython " + | ||||
"may not be able to read it. To keep the older version, close the " + | ||||
"notebook without saving it."; | ||||
var dialog = $('<div/>'); | ||||
dialog.html(msg); | ||||
this.element.append(dialog); | ||||
dialog.dialog({ | ||||
resizable: false, | ||||
modal: true, | ||||
title: "Notebook converted", | ||||
closeText: "", | ||||
close: function(event, ui) {$(this).dialog('destroy').remove();}, | ||||
buttons : { | ||||
"OK": function () { | ||||
$(this).dialog('close'); | ||||
} | ||||
}, | ||||
width: 400 | ||||
}); | ||||
|
r7546 | } else if (data.orig_nbformat_minor !== undefined && data.nbformat_minor !== data.orig_nbformat_minor) { | ||
var that = this; | ||||
var orig_vs = 'v' + data.nbformat + '.' + data.orig_nbformat_minor; | ||||
var this_vs = 'v' + data.nbformat + '.' + this.nbformat_minor; | ||||
|
r8839 | var msg = "This notebook is version " + orig_vs + ", but we only fully support up to " + | ||
|
r7546 | this_vs + ". You can still work with this notebook, but some features " + | ||
"introduced in later notebook versions may not be available." | ||||
|
r8839 | |||
|
r7546 | var dialog = $('<div/>'); | ||
dialog.html(msg); | ||||
this.element.append(dialog); | ||||
dialog.dialog({ | ||||
resizable: false, | ||||
modal: true, | ||||
title: "Newer Notebook", | ||||
closeText: "", | ||||
close: function(event, ui) {$(this).dialog('destroy').remove();}, | ||||
buttons : { | ||||
"OK": function () { | ||||
$(this).dialog('close'); | ||||
} | ||||
}, | ||||
width: 400 | ||||
}); | ||||
|
r8839 | |||
|
r6061 | } | ||
|
r7197 | // Create the kernel after the notebook is completely loaded to prevent | ||
// code execution upon loading, which is a security risk. | ||||
if (! this.read_only) { | ||||
this.start_kernel(); | ||||
} | ||||
|
r6047 | $([IPython.events]).trigger('notebook_loaded.Notebook'); | ||
|
r4484 | }; | ||
|
r6061 | |||
Notebook.prototype.load_notebook_error = function (xhr, textStatus, errorThrow) { | ||||
if (xhr.status === 500) { | ||||
|
r8839 | var msg = "An error occurred while loading this notebook. Most likely " + | ||
|
r6061 | "this notebook is in a newer format than is supported by this " + | ||
"version of IPython. This version can load notebook formats " + | ||||
"v"+this.nbformat+" or earlier."; | ||||
var dialog = $('<div/>'); | ||||
dialog.html(msg); | ||||
this.element.append(dialog); | ||||
dialog.dialog({ | ||||
resizable: false, | ||||
modal: true, | ||||
title: "Error loading notebook", | ||||
closeText: "", | ||||
close: function(event, ui) {$(this).dialog('destroy').remove();}, | ||||
buttons : { | ||||
"OK": function () { | ||||
$(this).dialog('close'); | ||||
} | ||||
}, | ||||
width: 400 | ||||
}); | ||||
} | ||||
} | ||||
|
r8839 | |||
|
r4352 | IPython.Notebook = Notebook; | ||
|
r5108 | |||
|
r4352 | return IPython; | ||
}(IPython)); | ||||
|
r4315 | |||