##// END OF EJS Templates
only autosave when dirty
MinRK -
Show More
@@ -1,2023 +1,2025 b''
1 //----------------------------------------------------------------------------
1 //----------------------------------------------------------------------------
2 // Copyright (C) 2008-2011 The IPython Development Team
2 // Copyright (C) 2008-2011 The IPython Development Team
3 //
3 //
4 // Distributed under the terms of the BSD License. The full license is in
4 // Distributed under the terms of the BSD License. The full license is in
5 // the file COPYING, distributed as part of this software.
5 // the file COPYING, distributed as part of this software.
6 //----------------------------------------------------------------------------
6 //----------------------------------------------------------------------------
7
7
8 //============================================================================
8 //============================================================================
9 // Notebook
9 // Notebook
10 //============================================================================
10 //============================================================================
11
11
12 var IPython = (function (IPython) {
12 var IPython = (function (IPython) {
13
13
14 var utils = IPython.utils;
14 var utils = IPython.utils;
15 var key = IPython.utils.keycodes;
15 var key = IPython.utils.keycodes;
16
16
17 /**
17 /**
18 * A notebook contains and manages cells.
18 * A notebook contains and manages cells.
19 *
19 *
20 * @class Notebook
20 * @class Notebook
21 * @constructor
21 * @constructor
22 * @param {String} selector A jQuery selector for the notebook's DOM element
22 * @param {String} selector A jQuery selector for the notebook's DOM element
23 * @param {Object} [options] A config object
23 * @param {Object} [options] A config object
24 */
24 */
25 var Notebook = function (selector, options) {
25 var Notebook = function (selector, options) {
26 var options = options || {};
26 var options = options || {};
27 this._baseProjectUrl = options.baseProjectUrl;
27 this._baseProjectUrl = options.baseProjectUrl;
28 this.read_only = options.read_only || IPython.read_only;
28 this.read_only = options.read_only || IPython.read_only;
29
29
30 this.element = $(selector);
30 this.element = $(selector);
31 this.element.scroll();
31 this.element.scroll();
32 this.element.data("notebook", this);
32 this.element.data("notebook", this);
33 this.next_prompt_number = 1;
33 this.next_prompt_number = 1;
34 this.kernel = null;
34 this.kernel = null;
35 this.clipboard = null;
35 this.clipboard = null;
36 this.undelete_backup = null;
36 this.undelete_backup = null;
37 this.undelete_index = null;
37 this.undelete_index = null;
38 this.undelete_below = false;
38 this.undelete_below = false;
39 this.paste_enabled = false;
39 this.paste_enabled = false;
40 this.dirty = false;
40 this.dirty = false;
41 this.metadata = {};
41 this.metadata = {};
42 this._checkpoint_after_save = false;
42 this._checkpoint_after_save = false;
43 this.last_checkpoint = null;
43 this.last_checkpoint = null;
44 this.autosave_interval = 0;
44 this.autosave_interval = 0;
45 this.autosave_timer = null;
45 this.autosave_timer = null;
46 this.minimum_autosave_interval = 30000;
46 this.minimum_autosave_interval = 30000;
47 // single worksheet for now
47 // single worksheet for now
48 this.worksheet_metadata = {};
48 this.worksheet_metadata = {};
49 this.control_key_active = false;
49 this.control_key_active = false;
50 this.notebook_id = null;
50 this.notebook_id = null;
51 this.notebook_name = null;
51 this.notebook_name = null;
52 this.notebook_name_blacklist_re = /[\/\\:]/;
52 this.notebook_name_blacklist_re = /[\/\\:]/;
53 this.nbformat = 3 // Increment this when changing the nbformat
53 this.nbformat = 3 // Increment this when changing the nbformat
54 this.nbformat_minor = 0 // Increment this when changing the nbformat
54 this.nbformat_minor = 0 // Increment this when changing the nbformat
55 this.style();
55 this.style();
56 this.create_elements();
56 this.create_elements();
57 this.bind_events();
57 this.bind_events();
58 };
58 };
59
59
60 /**
60 /**
61 * Tweak the notebook's CSS style.
61 * Tweak the notebook's CSS style.
62 *
62 *
63 * @method style
63 * @method style
64 */
64 */
65 Notebook.prototype.style = function () {
65 Notebook.prototype.style = function () {
66 $('div#notebook').addClass('border-box-sizing');
66 $('div#notebook').addClass('border-box-sizing');
67 };
67 };
68
68
69 /**
69 /**
70 * Get the root URL of the notebook server.
70 * Get the root URL of the notebook server.
71 *
71 *
72 * @method baseProjectUrl
72 * @method baseProjectUrl
73 * @return {String} The base project URL
73 * @return {String} The base project URL
74 */
74 */
75 Notebook.prototype.baseProjectUrl = function(){
75 Notebook.prototype.baseProjectUrl = function(){
76 return this._baseProjectUrl || $('body').data('baseProjectUrl');
76 return this._baseProjectUrl || $('body').data('baseProjectUrl');
77 };
77 };
78
78
79 /**
79 /**
80 * Create an HTML and CSS representation of the notebook.
80 * Create an HTML and CSS representation of the notebook.
81 *
81 *
82 * @method create_elements
82 * @method create_elements
83 */
83 */
84 Notebook.prototype.create_elements = function () {
84 Notebook.prototype.create_elements = function () {
85 // We add this end_space div to the end of the notebook div to:
85 // We add this end_space div to the end of the notebook div to:
86 // i) provide a margin between the last cell and the end of the notebook
86 // i) provide a margin between the last cell and the end of the notebook
87 // ii) to prevent the div from scrolling up when the last cell is being
87 // ii) to prevent the div from scrolling up when the last cell is being
88 // edited, but is too low on the page, which browsers will do automatically.
88 // edited, but is too low on the page, which browsers will do automatically.
89 var that = this;
89 var that = this;
90 var end_space = $('<div/>').addClass('end_space').height("30%");
90 var end_space = $('<div/>').addClass('end_space').height("30%");
91 end_space.dblclick(function (e) {
91 end_space.dblclick(function (e) {
92 if (that.read_only) return;
92 if (that.read_only) return;
93 var ncells = that.ncells();
93 var ncells = that.ncells();
94 that.insert_cell_below('code',ncells-1);
94 that.insert_cell_below('code',ncells-1);
95 });
95 });
96 this.element.append(end_space);
96 this.element.append(end_space);
97 $('div#notebook').addClass('border-box-sizing');
97 $('div#notebook').addClass('border-box-sizing');
98 };
98 };
99
99
100 /**
100 /**
101 * Bind JavaScript events: key presses and custom IPython events.
101 * Bind JavaScript events: key presses and custom IPython events.
102 *
102 *
103 * @method bind_events
103 * @method bind_events
104 */
104 */
105 Notebook.prototype.bind_events = function () {
105 Notebook.prototype.bind_events = function () {
106 var that = this;
106 var that = this;
107
107
108 $([IPython.events]).on('set_next_input.Notebook', function (event, data) {
108 $([IPython.events]).on('set_next_input.Notebook', function (event, data) {
109 var index = that.find_cell_index(data.cell);
109 var index = that.find_cell_index(data.cell);
110 var new_cell = that.insert_cell_below('code',index);
110 var new_cell = that.insert_cell_below('code',index);
111 new_cell.set_text(data.text);
111 new_cell.set_text(data.text);
112 that.dirty = true;
112 that.dirty = true;
113 });
113 });
114
114
115 $([IPython.events]).on('set_dirty.Notebook', function (event, data) {
115 $([IPython.events]).on('set_dirty.Notebook', function (event, data) {
116 that.dirty = data.value;
116 that.dirty = data.value;
117 });
117 });
118
118
119 $([IPython.events]).on('select.Cell', function (event, data) {
119 $([IPython.events]).on('select.Cell', function (event, data) {
120 var index = that.find_cell_index(data.cell);
120 var index = that.find_cell_index(data.cell);
121 that.select(index);
121 that.select(index);
122 });
122 });
123
123
124
124
125 $(document).keydown(function (event) {
125 $(document).keydown(function (event) {
126 // console.log(event);
126 // console.log(event);
127 if (that.read_only) return true;
127 if (that.read_only) return true;
128
128
129 // Save (CTRL+S) or (AppleKey+S)
129 // Save (CTRL+S) or (AppleKey+S)
130 //metaKey = applekey on mac
130 //metaKey = applekey on mac
131 if ((event.ctrlKey || event.metaKey) && event.keyCode==83) {
131 if ((event.ctrlKey || event.metaKey) && event.keyCode==83) {
132 that.save_notebook();
132 that.save_notebook();
133 event.preventDefault();
133 event.preventDefault();
134 return false;
134 return false;
135 } else if (event.which === key.ESC) {
135 } else if (event.which === key.ESC) {
136 // Intercept escape at highest level to avoid closing
136 // Intercept escape at highest level to avoid closing
137 // websocket connection with firefox
137 // websocket connection with firefox
138 event.preventDefault();
138 event.preventDefault();
139 } else if (event.which === key.SHIFT) {
139 } else if (event.which === key.SHIFT) {
140 // ignore shift keydown
140 // ignore shift keydown
141 return true;
141 return true;
142 }
142 }
143 if (event.which === key.UPARROW && !event.shiftKey) {
143 if (event.which === key.UPARROW && !event.shiftKey) {
144 var cell = that.get_selected_cell();
144 var cell = that.get_selected_cell();
145 if (cell && cell.at_top()) {
145 if (cell && cell.at_top()) {
146 event.preventDefault();
146 event.preventDefault();
147 that.select_prev();
147 that.select_prev();
148 };
148 };
149 } else if (event.which === key.DOWNARROW && !event.shiftKey) {
149 } else if (event.which === key.DOWNARROW && !event.shiftKey) {
150 var cell = that.get_selected_cell();
150 var cell = that.get_selected_cell();
151 if (cell && cell.at_bottom()) {
151 if (cell && cell.at_bottom()) {
152 event.preventDefault();
152 event.preventDefault();
153 that.select_next();
153 that.select_next();
154 };
154 };
155 } else if (event.which === key.ENTER && event.shiftKey) {
155 } else if (event.which === key.ENTER && event.shiftKey) {
156 that.execute_selected_cell();
156 that.execute_selected_cell();
157 return false;
157 return false;
158 } else if (event.which === key.ENTER && event.altKey) {
158 } else if (event.which === key.ENTER && event.altKey) {
159 // Execute code cell, and insert new in place
159 // Execute code cell, and insert new in place
160 that.execute_selected_cell();
160 that.execute_selected_cell();
161 // Only insert a new cell, if we ended up in an already populated cell
161 // Only insert a new cell, if we ended up in an already populated cell
162 if (/\S/.test(that.get_selected_cell().get_text()) == true) {
162 if (/\S/.test(that.get_selected_cell().get_text()) == true) {
163 that.insert_cell_above('code');
163 that.insert_cell_above('code');
164 }
164 }
165 return false;
165 return false;
166 } else if (event.which === key.ENTER && event.ctrlKey) {
166 } else if (event.which === key.ENTER && event.ctrlKey) {
167 that.execute_selected_cell({terminal:true});
167 that.execute_selected_cell({terminal:true});
168 return false;
168 return false;
169 } else if (event.which === 77 && event.ctrlKey && that.control_key_active == false) {
169 } else if (event.which === 77 && event.ctrlKey && that.control_key_active == false) {
170 that.control_key_active = true;
170 that.control_key_active = true;
171 return false;
171 return false;
172 } else if (event.which === 88 && that.control_key_active) {
172 } else if (event.which === 88 && that.control_key_active) {
173 // Cut selected cell = x
173 // Cut selected cell = x
174 that.cut_cell();
174 that.cut_cell();
175 that.control_key_active = false;
175 that.control_key_active = false;
176 return false;
176 return false;
177 } else if (event.which === 67 && that.control_key_active) {
177 } else if (event.which === 67 && that.control_key_active) {
178 // Copy selected cell = c
178 // Copy selected cell = c
179 that.copy_cell();
179 that.copy_cell();
180 that.control_key_active = false;
180 that.control_key_active = false;
181 return false;
181 return false;
182 } else if (event.which === 86 && that.control_key_active) {
182 } else if (event.which === 86 && that.control_key_active) {
183 // Paste below selected cell = v
183 // Paste below selected cell = v
184 that.paste_cell_below();
184 that.paste_cell_below();
185 that.control_key_active = false;
185 that.control_key_active = false;
186 return false;
186 return false;
187 } else if (event.which === 68 && that.control_key_active) {
187 } else if (event.which === 68 && that.control_key_active) {
188 // Delete selected cell = d
188 // Delete selected cell = d
189 that.delete_cell();
189 that.delete_cell();
190 that.control_key_active = false;
190 that.control_key_active = false;
191 return false;
191 return false;
192 } else if (event.which === 65 && that.control_key_active) {
192 } else if (event.which === 65 && that.control_key_active) {
193 // Insert code cell above selected = a
193 // Insert code cell above selected = a
194 that.insert_cell_above('code');
194 that.insert_cell_above('code');
195 that.control_key_active = false;
195 that.control_key_active = false;
196 return false;
196 return false;
197 } else if (event.which === 66 && that.control_key_active) {
197 } else if (event.which === 66 && that.control_key_active) {
198 // Insert code cell below selected = b
198 // Insert code cell below selected = b
199 that.insert_cell_below('code');
199 that.insert_cell_below('code');
200 that.control_key_active = false;
200 that.control_key_active = false;
201 return false;
201 return false;
202 } else if (event.which === 89 && that.control_key_active) {
202 } else if (event.which === 89 && that.control_key_active) {
203 // To code = y
203 // To code = y
204 that.to_code();
204 that.to_code();
205 that.control_key_active = false;
205 that.control_key_active = false;
206 return false;
206 return false;
207 } else if (event.which === 77 && that.control_key_active) {
207 } else if (event.which === 77 && that.control_key_active) {
208 // To markdown = m
208 // To markdown = m
209 that.to_markdown();
209 that.to_markdown();
210 that.control_key_active = false;
210 that.control_key_active = false;
211 return false;
211 return false;
212 } else if (event.which === 84 && that.control_key_active) {
212 } else if (event.which === 84 && that.control_key_active) {
213 // To Raw = t
213 // To Raw = t
214 that.to_raw();
214 that.to_raw();
215 that.control_key_active = false;
215 that.control_key_active = false;
216 return false;
216 return false;
217 } else if (event.which === 49 && that.control_key_active) {
217 } else if (event.which === 49 && that.control_key_active) {
218 // To Heading 1 = 1
218 // To Heading 1 = 1
219 that.to_heading(undefined, 1);
219 that.to_heading(undefined, 1);
220 that.control_key_active = false;
220 that.control_key_active = false;
221 return false;
221 return false;
222 } else if (event.which === 50 && that.control_key_active) {
222 } else if (event.which === 50 && that.control_key_active) {
223 // To Heading 2 = 2
223 // To Heading 2 = 2
224 that.to_heading(undefined, 2);
224 that.to_heading(undefined, 2);
225 that.control_key_active = false;
225 that.control_key_active = false;
226 return false;
226 return false;
227 } else if (event.which === 51 && that.control_key_active) {
227 } else if (event.which === 51 && that.control_key_active) {
228 // To Heading 3 = 3
228 // To Heading 3 = 3
229 that.to_heading(undefined, 3);
229 that.to_heading(undefined, 3);
230 that.control_key_active = false;
230 that.control_key_active = false;
231 return false;
231 return false;
232 } else if (event.which === 52 && that.control_key_active) {
232 } else if (event.which === 52 && that.control_key_active) {
233 // To Heading 4 = 4
233 // To Heading 4 = 4
234 that.to_heading(undefined, 4);
234 that.to_heading(undefined, 4);
235 that.control_key_active = false;
235 that.control_key_active = false;
236 return false;
236 return false;
237 } else if (event.which === 53 && that.control_key_active) {
237 } else if (event.which === 53 && that.control_key_active) {
238 // To Heading 5 = 5
238 // To Heading 5 = 5
239 that.to_heading(undefined, 5);
239 that.to_heading(undefined, 5);
240 that.control_key_active = false;
240 that.control_key_active = false;
241 return false;
241 return false;
242 } else if (event.which === 54 && that.control_key_active) {
242 } else if (event.which === 54 && that.control_key_active) {
243 // To Heading 6 = 6
243 // To Heading 6 = 6
244 that.to_heading(undefined, 6);
244 that.to_heading(undefined, 6);
245 that.control_key_active = false;
245 that.control_key_active = false;
246 return false;
246 return false;
247 } else if (event.which === 79 && that.control_key_active) {
247 } else if (event.which === 79 && that.control_key_active) {
248 // Toggle output = o
248 // Toggle output = o
249 if (event.shiftKey){
249 if (event.shiftKey){
250 that.toggle_output_scroll();
250 that.toggle_output_scroll();
251 } else {
251 } else {
252 that.toggle_output();
252 that.toggle_output();
253 }
253 }
254 that.control_key_active = false;
254 that.control_key_active = false;
255 return false;
255 return false;
256 } else if (event.which === 83 && that.control_key_active) {
256 } else if (event.which === 83 && that.control_key_active) {
257 // Save notebook = s
257 // Save notebook = s
258 that.save_checkpoint();
258 that.save_checkpoint();
259 that.control_key_active = false;
259 that.control_key_active = false;
260 return false;
260 return false;
261 } else if (event.which === 74 && that.control_key_active) {
261 } else if (event.which === 74 && that.control_key_active) {
262 // Move cell down = j
262 // Move cell down = j
263 that.move_cell_down();
263 that.move_cell_down();
264 that.control_key_active = false;
264 that.control_key_active = false;
265 return false;
265 return false;
266 } else if (event.which === 75 && that.control_key_active) {
266 } else if (event.which === 75 && that.control_key_active) {
267 // Move cell up = k
267 // Move cell up = k
268 that.move_cell_up();
268 that.move_cell_up();
269 that.control_key_active = false;
269 that.control_key_active = false;
270 return false;
270 return false;
271 } else if (event.which === 80 && that.control_key_active) {
271 } else if (event.which === 80 && that.control_key_active) {
272 // Select previous = p
272 // Select previous = p
273 that.select_prev();
273 that.select_prev();
274 that.control_key_active = false;
274 that.control_key_active = false;
275 return false;
275 return false;
276 } else if (event.which === 78 && that.control_key_active) {
276 } else if (event.which === 78 && that.control_key_active) {
277 // Select next = n
277 // Select next = n
278 that.select_next();
278 that.select_next();
279 that.control_key_active = false;
279 that.control_key_active = false;
280 return false;
280 return false;
281 } else if (event.which === 76 && that.control_key_active) {
281 } else if (event.which === 76 && that.control_key_active) {
282 // Toggle line numbers = l
282 // Toggle line numbers = l
283 that.cell_toggle_line_numbers();
283 that.cell_toggle_line_numbers();
284 that.control_key_active = false;
284 that.control_key_active = false;
285 return false;
285 return false;
286 } else if (event.which === 73 && that.control_key_active) {
286 } else if (event.which === 73 && that.control_key_active) {
287 // Interrupt kernel = i
287 // Interrupt kernel = i
288 that.kernel.interrupt();
288 that.kernel.interrupt();
289 that.control_key_active = false;
289 that.control_key_active = false;
290 return false;
290 return false;
291 } else if (event.which === 190 && that.control_key_active) {
291 } else if (event.which === 190 && that.control_key_active) {
292 // Restart kernel = . # matches qt console
292 // Restart kernel = . # matches qt console
293 that.restart_kernel();
293 that.restart_kernel();
294 that.control_key_active = false;
294 that.control_key_active = false;
295 return false;
295 return false;
296 } else if (event.which === 72 && that.control_key_active) {
296 } else if (event.which === 72 && that.control_key_active) {
297 // Show keyboard shortcuts = h
297 // Show keyboard shortcuts = h
298 IPython.quick_help.show_keyboard_shortcuts();
298 IPython.quick_help.show_keyboard_shortcuts();
299 that.control_key_active = false;
299 that.control_key_active = false;
300 return false;
300 return false;
301 } else if (event.which === 90 && that.control_key_active) {
301 } else if (event.which === 90 && that.control_key_active) {
302 // Undo last cell delete = z
302 // Undo last cell delete = z
303 that.undelete();
303 that.undelete();
304 that.control_key_active = false;
304 that.control_key_active = false;
305 return false;
305 return false;
306 } else if (that.control_key_active) {
306 } else if (that.control_key_active) {
307 that.control_key_active = false;
307 that.control_key_active = false;
308 return true;
308 return true;
309 };
309 };
310 return true;
310 return true;
311 });
311 });
312
312
313 var collapse_time = function(time){
313 var collapse_time = function(time){
314 var app_height = $('#ipython-main-app').height(); // content height
314 var app_height = $('#ipython-main-app').height(); // content height
315 var splitter_height = $('div#pager_splitter').outerHeight(true);
315 var splitter_height = $('div#pager_splitter').outerHeight(true);
316 var new_height = app_height - splitter_height;
316 var new_height = app_height - splitter_height;
317 that.element.animate({height : new_height + 'px'}, time);
317 that.element.animate({height : new_height + 'px'}, time);
318 }
318 }
319
319
320 this.element.bind('collapse_pager', function (event,extrap) {
320 this.element.bind('collapse_pager', function (event,extrap) {
321 var time = (extrap != undefined) ? ((extrap.duration != undefined ) ? extrap.duration : 'fast') : 'fast';
321 var time = (extrap != undefined) ? ((extrap.duration != undefined ) ? extrap.duration : 'fast') : 'fast';
322 collapse_time(time);
322 collapse_time(time);
323 });
323 });
324
324
325 var expand_time = function(time) {
325 var expand_time = function(time) {
326 var app_height = $('#ipython-main-app').height(); // content height
326 var app_height = $('#ipython-main-app').height(); // content height
327 var splitter_height = $('div#pager_splitter').outerHeight(true);
327 var splitter_height = $('div#pager_splitter').outerHeight(true);
328 var pager_height = $('div#pager').outerHeight(true);
328 var pager_height = $('div#pager').outerHeight(true);
329 var new_height = app_height - pager_height - splitter_height;
329 var new_height = app_height - pager_height - splitter_height;
330 that.element.animate({height : new_height + 'px'}, time);
330 that.element.animate({height : new_height + 'px'}, time);
331 }
331 }
332
332
333 this.element.bind('expand_pager', function (event, extrap) {
333 this.element.bind('expand_pager', function (event, extrap) {
334 var time = (extrap != undefined) ? ((extrap.duration != undefined ) ? extrap.duration : 'fast') : 'fast';
334 var time = (extrap != undefined) ? ((extrap.duration != undefined ) ? extrap.duration : 'fast') : 'fast';
335 expand_time(time);
335 expand_time(time);
336 });
336 });
337
337
338 $(window).bind('beforeunload', function () {
338 $(window).bind('beforeunload', function () {
339 // TODO: Make killing the kernel configurable.
339 // TODO: Make killing the kernel configurable.
340 var kill_kernel = false;
340 var kill_kernel = false;
341 if (kill_kernel) {
341 if (kill_kernel) {
342 that.kernel.kill();
342 that.kernel.kill();
343 }
343 }
344 if (that.dirty && ! that.read_only) {
344 if (that.dirty && ! that.read_only) {
345 that.save_notebook();
345 that.save_notebook();
346 };
346 };
347 // Null is the *only* return value that will make the browser not
347 // Null is the *only* return value that will make the browser not
348 // pop up the "don't leave" dialog.
348 // pop up the "don't leave" dialog.
349 return null;
349 return null;
350 });
350 });
351 };
351 };
352
352
353 /**
353 /**
354 * Scroll the top of the page to a given cell.
354 * Scroll the top of the page to a given cell.
355 *
355 *
356 * @method scroll_to_cell
356 * @method scroll_to_cell
357 * @param {Number} cell_number An index of the cell to view
357 * @param {Number} cell_number An index of the cell to view
358 * @param {Number} time Animation time in milliseconds
358 * @param {Number} time Animation time in milliseconds
359 * @return {Number} Pixel offset from the top of the container
359 * @return {Number} Pixel offset from the top of the container
360 */
360 */
361 Notebook.prototype.scroll_to_cell = function (cell_number, time) {
361 Notebook.prototype.scroll_to_cell = function (cell_number, time) {
362 var cells = this.get_cells();
362 var cells = this.get_cells();
363 var time = time || 0;
363 var time = time || 0;
364 cell_number = Math.min(cells.length-1,cell_number);
364 cell_number = Math.min(cells.length-1,cell_number);
365 cell_number = Math.max(0 ,cell_number);
365 cell_number = Math.max(0 ,cell_number);
366 var scroll_value = cells[cell_number].element.position().top-cells[0].element.position().top ;
366 var scroll_value = cells[cell_number].element.position().top-cells[0].element.position().top ;
367 this.element.animate({scrollTop:scroll_value}, time);
367 this.element.animate({scrollTop:scroll_value}, time);
368 return scroll_value;
368 return scroll_value;
369 };
369 };
370
370
371 /**
371 /**
372 * Scroll to the bottom of the page.
372 * Scroll to the bottom of the page.
373 *
373 *
374 * @method scroll_to_bottom
374 * @method scroll_to_bottom
375 */
375 */
376 Notebook.prototype.scroll_to_bottom = function () {
376 Notebook.prototype.scroll_to_bottom = function () {
377 this.element.animate({scrollTop:this.element.get(0).scrollHeight}, 0);
377 this.element.animate({scrollTop:this.element.get(0).scrollHeight}, 0);
378 };
378 };
379
379
380 /**
380 /**
381 * Scroll to the top of the page.
381 * Scroll to the top of the page.
382 *
382 *
383 * @method scroll_to_top
383 * @method scroll_to_top
384 */
384 */
385 Notebook.prototype.scroll_to_top = function () {
385 Notebook.prototype.scroll_to_top = function () {
386 this.element.animate({scrollTop:0}, 0);
386 this.element.animate({scrollTop:0}, 0);
387 };
387 };
388
388
389
389
390 // Cell indexing, retrieval, etc.
390 // Cell indexing, retrieval, etc.
391
391
392 /**
392 /**
393 * Get all cell elements in the notebook.
393 * Get all cell elements in the notebook.
394 *
394 *
395 * @method get_cell_elements
395 * @method get_cell_elements
396 * @return {jQuery} A selector of all cell elements
396 * @return {jQuery} A selector of all cell elements
397 */
397 */
398 Notebook.prototype.get_cell_elements = function () {
398 Notebook.prototype.get_cell_elements = function () {
399 return this.element.children("div.cell");
399 return this.element.children("div.cell");
400 };
400 };
401
401
402 /**
402 /**
403 * Get a particular cell element.
403 * Get a particular cell element.
404 *
404 *
405 * @method get_cell_element
405 * @method get_cell_element
406 * @param {Number} index An index of a cell to select
406 * @param {Number} index An index of a cell to select
407 * @return {jQuery} A selector of the given cell.
407 * @return {jQuery} A selector of the given cell.
408 */
408 */
409 Notebook.prototype.get_cell_element = function (index) {
409 Notebook.prototype.get_cell_element = function (index) {
410 var result = null;
410 var result = null;
411 var e = this.get_cell_elements().eq(index);
411 var e = this.get_cell_elements().eq(index);
412 if (e.length !== 0) {
412 if (e.length !== 0) {
413 result = e;
413 result = e;
414 }
414 }
415 return result;
415 return result;
416 };
416 };
417
417
418 /**
418 /**
419 * Count the cells in this notebook.
419 * Count the cells in this notebook.
420 *
420 *
421 * @method ncells
421 * @method ncells
422 * @return {Number} The number of cells in this notebook
422 * @return {Number} The number of cells in this notebook
423 */
423 */
424 Notebook.prototype.ncells = function () {
424 Notebook.prototype.ncells = function () {
425 return this.get_cell_elements().length;
425 return this.get_cell_elements().length;
426 };
426 };
427
427
428 /**
428 /**
429 * Get all Cell objects in this notebook.
429 * Get all Cell objects in this notebook.
430 *
430 *
431 * @method get_cells
431 * @method get_cells
432 * @return {Array} This notebook's Cell objects
432 * @return {Array} This notebook's Cell objects
433 */
433 */
434 // TODO: we are often calling cells as cells()[i], which we should optimize
434 // TODO: we are often calling cells as cells()[i], which we should optimize
435 // to cells(i) or a new method.
435 // to cells(i) or a new method.
436 Notebook.prototype.get_cells = function () {
436 Notebook.prototype.get_cells = function () {
437 return this.get_cell_elements().toArray().map(function (e) {
437 return this.get_cell_elements().toArray().map(function (e) {
438 return $(e).data("cell");
438 return $(e).data("cell");
439 });
439 });
440 };
440 };
441
441
442 /**
442 /**
443 * Get a Cell object from this notebook.
443 * Get a Cell object from this notebook.
444 *
444 *
445 * @method get_cell
445 * @method get_cell
446 * @param {Number} index An index of a cell to retrieve
446 * @param {Number} index An index of a cell to retrieve
447 * @return {Cell} A particular cell
447 * @return {Cell} A particular cell
448 */
448 */
449 Notebook.prototype.get_cell = function (index) {
449 Notebook.prototype.get_cell = function (index) {
450 var result = null;
450 var result = null;
451 var ce = this.get_cell_element(index);
451 var ce = this.get_cell_element(index);
452 if (ce !== null) {
452 if (ce !== null) {
453 result = ce.data('cell');
453 result = ce.data('cell');
454 }
454 }
455 return result;
455 return result;
456 }
456 }
457
457
458 /**
458 /**
459 * Get the cell below a given cell.
459 * Get the cell below a given cell.
460 *
460 *
461 * @method get_next_cell
461 * @method get_next_cell
462 * @param {Cell} cell The provided cell
462 * @param {Cell} cell The provided cell
463 * @return {Cell} The next cell
463 * @return {Cell} The next cell
464 */
464 */
465 Notebook.prototype.get_next_cell = function (cell) {
465 Notebook.prototype.get_next_cell = function (cell) {
466 var result = null;
466 var result = null;
467 var index = this.find_cell_index(cell);
467 var index = this.find_cell_index(cell);
468 if (this.is_valid_cell_index(index+1)) {
468 if (this.is_valid_cell_index(index+1)) {
469 result = this.get_cell(index+1);
469 result = this.get_cell(index+1);
470 }
470 }
471 return result;
471 return result;
472 }
472 }
473
473
474 /**
474 /**
475 * Get the cell above a given cell.
475 * Get the cell above a given cell.
476 *
476 *
477 * @method get_prev_cell
477 * @method get_prev_cell
478 * @param {Cell} cell The provided cell
478 * @param {Cell} cell The provided cell
479 * @return {Cell} The previous cell
479 * @return {Cell} The previous cell
480 */
480 */
481 Notebook.prototype.get_prev_cell = function (cell) {
481 Notebook.prototype.get_prev_cell = function (cell) {
482 // TODO: off-by-one
482 // TODO: off-by-one
483 // nb.get_prev_cell(nb.get_cell(1)) is null
483 // nb.get_prev_cell(nb.get_cell(1)) is null
484 var result = null;
484 var result = null;
485 var index = this.find_cell_index(cell);
485 var index = this.find_cell_index(cell);
486 if (index !== null && index > 1) {
486 if (index !== null && index > 1) {
487 result = this.get_cell(index-1);
487 result = this.get_cell(index-1);
488 }
488 }
489 return result;
489 return result;
490 }
490 }
491
491
492 /**
492 /**
493 * Get the numeric index of a given cell.
493 * Get the numeric index of a given cell.
494 *
494 *
495 * @method find_cell_index
495 * @method find_cell_index
496 * @param {Cell} cell The provided cell
496 * @param {Cell} cell The provided cell
497 * @return {Number} The cell's numeric index
497 * @return {Number} The cell's numeric index
498 */
498 */
499 Notebook.prototype.find_cell_index = function (cell) {
499 Notebook.prototype.find_cell_index = function (cell) {
500 var result = null;
500 var result = null;
501 this.get_cell_elements().filter(function (index) {
501 this.get_cell_elements().filter(function (index) {
502 if ($(this).data("cell") === cell) {
502 if ($(this).data("cell") === cell) {
503 result = index;
503 result = index;
504 };
504 };
505 });
505 });
506 return result;
506 return result;
507 };
507 };
508
508
509 /**
509 /**
510 * Get a given index , or the selected index if none is provided.
510 * Get a given index , or the selected index if none is provided.
511 *
511 *
512 * @method index_or_selected
512 * @method index_or_selected
513 * @param {Number} index A cell's index
513 * @param {Number} index A cell's index
514 * @return {Number} The given index, or selected index if none is provided.
514 * @return {Number} The given index, or selected index if none is provided.
515 */
515 */
516 Notebook.prototype.index_or_selected = function (index) {
516 Notebook.prototype.index_or_selected = function (index) {
517 var i;
517 var i;
518 if (index === undefined || index === null) {
518 if (index === undefined || index === null) {
519 i = this.get_selected_index();
519 i = this.get_selected_index();
520 if (i === null) {
520 if (i === null) {
521 i = 0;
521 i = 0;
522 }
522 }
523 } else {
523 } else {
524 i = index;
524 i = index;
525 }
525 }
526 return i;
526 return i;
527 };
527 };
528
528
529 /**
529 /**
530 * Get the currently selected cell.
530 * Get the currently selected cell.
531 * @method get_selected_cell
531 * @method get_selected_cell
532 * @return {Cell} The selected cell
532 * @return {Cell} The selected cell
533 */
533 */
534 Notebook.prototype.get_selected_cell = function () {
534 Notebook.prototype.get_selected_cell = function () {
535 var index = this.get_selected_index();
535 var index = this.get_selected_index();
536 return this.get_cell(index);
536 return this.get_cell(index);
537 };
537 };
538
538
539 /**
539 /**
540 * Check whether a cell index is valid.
540 * Check whether a cell index is valid.
541 *
541 *
542 * @method is_valid_cell_index
542 * @method is_valid_cell_index
543 * @param {Number} index A cell index
543 * @param {Number} index A cell index
544 * @return True if the index is valid, false otherwise
544 * @return True if the index is valid, false otherwise
545 */
545 */
546 Notebook.prototype.is_valid_cell_index = function (index) {
546 Notebook.prototype.is_valid_cell_index = function (index) {
547 if (index !== null && index >= 0 && index < this.ncells()) {
547 if (index !== null && index >= 0 && index < this.ncells()) {
548 return true;
548 return true;
549 } else {
549 } else {
550 return false;
550 return false;
551 };
551 };
552 }
552 }
553
553
554 /**
554 /**
555 * Get the index of the currently selected cell.
555 * Get the index of the currently selected cell.
556
556
557 * @method get_selected_index
557 * @method get_selected_index
558 * @return {Number} The selected cell's numeric index
558 * @return {Number} The selected cell's numeric index
559 */
559 */
560 Notebook.prototype.get_selected_index = function () {
560 Notebook.prototype.get_selected_index = function () {
561 var result = null;
561 var result = null;
562 this.get_cell_elements().filter(function (index) {
562 this.get_cell_elements().filter(function (index) {
563 if ($(this).data("cell").selected === true) {
563 if ($(this).data("cell").selected === true) {
564 result = index;
564 result = index;
565 };
565 };
566 });
566 });
567 return result;
567 return result;
568 };
568 };
569
569
570
570
571 // Cell selection.
571 // Cell selection.
572
572
573 /**
573 /**
574 * Programmatically select a cell.
574 * Programmatically select a cell.
575 *
575 *
576 * @method select
576 * @method select
577 * @param {Number} index A cell's index
577 * @param {Number} index A cell's index
578 * @return {Notebook} This notebook
578 * @return {Notebook} This notebook
579 */
579 */
580 Notebook.prototype.select = function (index) {
580 Notebook.prototype.select = function (index) {
581 if (this.is_valid_cell_index(index)) {
581 if (this.is_valid_cell_index(index)) {
582 var sindex = this.get_selected_index()
582 var sindex = this.get_selected_index()
583 if (sindex !== null && index !== sindex) {
583 if (sindex !== null && index !== sindex) {
584 this.get_cell(sindex).unselect();
584 this.get_cell(sindex).unselect();
585 };
585 };
586 var cell = this.get_cell(index);
586 var cell = this.get_cell(index);
587 cell.select();
587 cell.select();
588 if (cell.cell_type === 'heading') {
588 if (cell.cell_type === 'heading') {
589 $([IPython.events]).trigger('selected_cell_type_changed.Notebook',
589 $([IPython.events]).trigger('selected_cell_type_changed.Notebook',
590 {'cell_type':cell.cell_type,level:cell.level}
590 {'cell_type':cell.cell_type,level:cell.level}
591 );
591 );
592 } else {
592 } else {
593 $([IPython.events]).trigger('selected_cell_type_changed.Notebook',
593 $([IPython.events]).trigger('selected_cell_type_changed.Notebook',
594 {'cell_type':cell.cell_type}
594 {'cell_type':cell.cell_type}
595 );
595 );
596 };
596 };
597 };
597 };
598 return this;
598 return this;
599 };
599 };
600
600
601 /**
601 /**
602 * Programmatically select the next cell.
602 * Programmatically select the next cell.
603 *
603 *
604 * @method select_next
604 * @method select_next
605 * @return {Notebook} This notebook
605 * @return {Notebook} This notebook
606 */
606 */
607 Notebook.prototype.select_next = function () {
607 Notebook.prototype.select_next = function () {
608 var index = this.get_selected_index();
608 var index = this.get_selected_index();
609 this.select(index+1);
609 this.select(index+1);
610 return this;
610 return this;
611 };
611 };
612
612
613 /**
613 /**
614 * Programmatically select the previous cell.
614 * Programmatically select the previous cell.
615 *
615 *
616 * @method select_prev
616 * @method select_prev
617 * @return {Notebook} This notebook
617 * @return {Notebook} This notebook
618 */
618 */
619 Notebook.prototype.select_prev = function () {
619 Notebook.prototype.select_prev = function () {
620 var index = this.get_selected_index();
620 var index = this.get_selected_index();
621 this.select(index-1);
621 this.select(index-1);
622 return this;
622 return this;
623 };
623 };
624
624
625
625
626 // Cell movement
626 // Cell movement
627
627
628 /**
628 /**
629 * Move given (or selected) cell up and select it.
629 * Move given (or selected) cell up and select it.
630 *
630 *
631 * @method move_cell_up
631 * @method move_cell_up
632 * @param [index] {integer} cell index
632 * @param [index] {integer} cell index
633 * @return {Notebook} This notebook
633 * @return {Notebook} This notebook
634 **/
634 **/
635 Notebook.prototype.move_cell_up = function (index) {
635 Notebook.prototype.move_cell_up = function (index) {
636 var i = this.index_or_selected(index);
636 var i = this.index_or_selected(index);
637 if (this.is_valid_cell_index(i) && i > 0) {
637 if (this.is_valid_cell_index(i) && i > 0) {
638 var pivot = this.get_cell_element(i-1);
638 var pivot = this.get_cell_element(i-1);
639 var tomove = this.get_cell_element(i);
639 var tomove = this.get_cell_element(i);
640 if (pivot !== null && tomove !== null) {
640 if (pivot !== null && tomove !== null) {
641 tomove.detach();
641 tomove.detach();
642 pivot.before(tomove);
642 pivot.before(tomove);
643 this.select(i-1);
643 this.select(i-1);
644 };
644 };
645 this.dirty = true;
645 this.dirty = true;
646 };
646 };
647 return this;
647 return this;
648 };
648 };
649
649
650
650
651 /**
651 /**
652 * Move given (or selected) cell down and select it
652 * Move given (or selected) cell down and select it
653 *
653 *
654 * @method move_cell_down
654 * @method move_cell_down
655 * @param [index] {integer} cell index
655 * @param [index] {integer} cell index
656 * @return {Notebook} This notebook
656 * @return {Notebook} This notebook
657 **/
657 **/
658 Notebook.prototype.move_cell_down = function (index) {
658 Notebook.prototype.move_cell_down = function (index) {
659 var i = this.index_or_selected(index);
659 var i = this.index_or_selected(index);
660 if ( this.is_valid_cell_index(i) && this.is_valid_cell_index(i+1)) {
660 if ( this.is_valid_cell_index(i) && this.is_valid_cell_index(i+1)) {
661 var pivot = this.get_cell_element(i+1);
661 var pivot = this.get_cell_element(i+1);
662 var tomove = this.get_cell_element(i);
662 var tomove = this.get_cell_element(i);
663 if (pivot !== null && tomove !== null) {
663 if (pivot !== null && tomove !== null) {
664 tomove.detach();
664 tomove.detach();
665 pivot.after(tomove);
665 pivot.after(tomove);
666 this.select(i+1);
666 this.select(i+1);
667 };
667 };
668 };
668 };
669 this.dirty = true;
669 this.dirty = true;
670 return this;
670 return this;
671 };
671 };
672
672
673
673
674 // Insertion, deletion.
674 // Insertion, deletion.
675
675
676 /**
676 /**
677 * Delete a cell from the notebook.
677 * Delete a cell from the notebook.
678 *
678 *
679 * @method delete_cell
679 * @method delete_cell
680 * @param [index] A cell's numeric index
680 * @param [index] A cell's numeric index
681 * @return {Notebook} This notebook
681 * @return {Notebook} This notebook
682 */
682 */
683 Notebook.prototype.delete_cell = function (index) {
683 Notebook.prototype.delete_cell = function (index) {
684 var i = this.index_or_selected(index);
684 var i = this.index_or_selected(index);
685 var cell = this.get_selected_cell();
685 var cell = this.get_selected_cell();
686 this.undelete_backup = cell.toJSON();
686 this.undelete_backup = cell.toJSON();
687 $('#undelete_cell').removeClass('ui-state-disabled');
687 $('#undelete_cell').removeClass('ui-state-disabled');
688 if (this.is_valid_cell_index(i)) {
688 if (this.is_valid_cell_index(i)) {
689 var ce = this.get_cell_element(i);
689 var ce = this.get_cell_element(i);
690 ce.remove();
690 ce.remove();
691 if (i === (this.ncells())) {
691 if (i === (this.ncells())) {
692 this.select(i-1);
692 this.select(i-1);
693 this.undelete_index = i - 1;
693 this.undelete_index = i - 1;
694 this.undelete_below = true;
694 this.undelete_below = true;
695 } else {
695 } else {
696 this.select(i);
696 this.select(i);
697 this.undelete_index = i;
697 this.undelete_index = i;
698 this.undelete_below = false;
698 this.undelete_below = false;
699 };
699 };
700 this.dirty = true;
700 this.dirty = true;
701 };
701 };
702 return this;
702 return this;
703 };
703 };
704
704
705 /**
705 /**
706 * Insert a cell so that after insertion the cell is at given index.
706 * Insert a cell so that after insertion the cell is at given index.
707 *
707 *
708 * Similar to insert_above, but index parameter is mandatory
708 * Similar to insert_above, but index parameter is mandatory
709 *
709 *
710 * Index will be brought back into the accissible range [0,n]
710 * Index will be brought back into the accissible range [0,n]
711 *
711 *
712 * @method insert_cell_at_index
712 * @method insert_cell_at_index
713 * @param type {string} in ['code','markdown','heading']
713 * @param type {string} in ['code','markdown','heading']
714 * @param [index] {int} a valid index where to inser cell
714 * @param [index] {int} a valid index where to inser cell
715 *
715 *
716 * @return cell {cell|null} created cell or null
716 * @return cell {cell|null} created cell or null
717 **/
717 **/
718 Notebook.prototype.insert_cell_at_index = function(type, index){
718 Notebook.prototype.insert_cell_at_index = function(type, index){
719
719
720 var ncells = this.ncells();
720 var ncells = this.ncells();
721 var index = Math.min(index,ncells);
721 var index = Math.min(index,ncells);
722 index = Math.max(index,0);
722 index = Math.max(index,0);
723 var cell = null;
723 var cell = null;
724
724
725 if (ncells === 0 || this.is_valid_cell_index(index) || index === ncells) {
725 if (ncells === 0 || this.is_valid_cell_index(index) || index === ncells) {
726 if (type === 'code') {
726 if (type === 'code') {
727 cell = new IPython.CodeCell(this.kernel);
727 cell = new IPython.CodeCell(this.kernel);
728 cell.set_input_prompt();
728 cell.set_input_prompt();
729 } else if (type === 'markdown') {
729 } else if (type === 'markdown') {
730 cell = new IPython.MarkdownCell();
730 cell = new IPython.MarkdownCell();
731 } else if (type === 'raw') {
731 } else if (type === 'raw') {
732 cell = new IPython.RawCell();
732 cell = new IPython.RawCell();
733 } else if (type === 'heading') {
733 } else if (type === 'heading') {
734 cell = new IPython.HeadingCell();
734 cell = new IPython.HeadingCell();
735 }
735 }
736
736
737 if(this._insert_element_at_index(cell.element,index)){
737 if(this._insert_element_at_index(cell.element,index)){
738 cell.render();
738 cell.render();
739 this.select(this.find_cell_index(cell));
739 this.select(this.find_cell_index(cell));
740 this.dirty = true;
740 this.dirty = true;
741 }
741 }
742 }
742 }
743 return cell;
743 return cell;
744
744
745 };
745 };
746
746
747 /**
747 /**
748 * Insert an element at given cell index.
748 * Insert an element at given cell index.
749 *
749 *
750 * @method _insert_element_at_index
750 * @method _insert_element_at_index
751 * @param element {dom element} a cell element
751 * @param element {dom element} a cell element
752 * @param [index] {int} a valid index where to inser cell
752 * @param [index] {int} a valid index where to inser cell
753 * @private
753 * @private
754 *
754 *
755 * return true if everything whent fine.
755 * return true if everything whent fine.
756 **/
756 **/
757 Notebook.prototype._insert_element_at_index = function(element, index){
757 Notebook.prototype._insert_element_at_index = function(element, index){
758 if (element === undefined){
758 if (element === undefined){
759 return false;
759 return false;
760 }
760 }
761
761
762 var ncells = this.ncells();
762 var ncells = this.ncells();
763
763
764 if (ncells === 0) {
764 if (ncells === 0) {
765 // special case append if empty
765 // special case append if empty
766 this.element.find('div.end_space').before(element);
766 this.element.find('div.end_space').before(element);
767 } else if ( ncells === index ) {
767 } else if ( ncells === index ) {
768 // special case append it the end, but not empty
768 // special case append it the end, but not empty
769 this.get_cell_element(index-1).after(element);
769 this.get_cell_element(index-1).after(element);
770 } else if (this.is_valid_cell_index(index)) {
770 } else if (this.is_valid_cell_index(index)) {
771 // otherwise always somewhere to append to
771 // otherwise always somewhere to append to
772 this.get_cell_element(index).before(element);
772 this.get_cell_element(index).before(element);
773 } else {
773 } else {
774 return false;
774 return false;
775 }
775 }
776
776
777 if (this.undelete_index !== null && index <= this.undelete_index) {
777 if (this.undelete_index !== null && index <= this.undelete_index) {
778 this.undelete_index = this.undelete_index + 1;
778 this.undelete_index = this.undelete_index + 1;
779 this.dirty = true;
779 this.dirty = true;
780 }
780 }
781 return true;
781 return true;
782 };
782 };
783
783
784 /**
784 /**
785 * Insert a cell of given type above given index, or at top
785 * Insert a cell of given type above given index, or at top
786 * of notebook if index smaller than 0.
786 * of notebook if index smaller than 0.
787 *
787 *
788 * default index value is the one of currently selected cell
788 * default index value is the one of currently selected cell
789 *
789 *
790 * @method insert_cell_above
790 * @method insert_cell_above
791 * @param type {string} cell type
791 * @param type {string} cell type
792 * @param [index] {integer}
792 * @param [index] {integer}
793 *
793 *
794 * @return handle to created cell or null
794 * @return handle to created cell or null
795 **/
795 **/
796 Notebook.prototype.insert_cell_above = function (type, index) {
796 Notebook.prototype.insert_cell_above = function (type, index) {
797 index = this.index_or_selected(index);
797 index = this.index_or_selected(index);
798 return this.insert_cell_at_index(type, index);
798 return this.insert_cell_at_index(type, index);
799 };
799 };
800
800
801 /**
801 /**
802 * Insert a cell of given type below given index, or at bottom
802 * Insert a cell of given type below given index, or at bottom
803 * of notebook if index greater thatn number of cell
803 * of notebook if index greater thatn number of cell
804 *
804 *
805 * default index value is the one of currently selected cell
805 * default index value is the one of currently selected cell
806 *
806 *
807 * @method insert_cell_below
807 * @method insert_cell_below
808 * @param type {string} cell type
808 * @param type {string} cell type
809 * @param [index] {integer}
809 * @param [index] {integer}
810 *
810 *
811 * @return handle to created cell or null
811 * @return handle to created cell or null
812 *
812 *
813 **/
813 **/
814 Notebook.prototype.insert_cell_below = function (type, index) {
814 Notebook.prototype.insert_cell_below = function (type, index) {
815 index = this.index_or_selected(index);
815 index = this.index_or_selected(index);
816 return this.insert_cell_at_index(type, index+1);
816 return this.insert_cell_at_index(type, index+1);
817 };
817 };
818
818
819
819
820 /**
820 /**
821 * Insert cell at end of notebook
821 * Insert cell at end of notebook
822 *
822 *
823 * @method insert_cell_at_bottom
823 * @method insert_cell_at_bottom
824 * @param {String} type cell type
824 * @param {String} type cell type
825 *
825 *
826 * @return the added cell; or null
826 * @return the added cell; or null
827 **/
827 **/
828 Notebook.prototype.insert_cell_at_bottom = function (type){
828 Notebook.prototype.insert_cell_at_bottom = function (type){
829 var len = this.ncells();
829 var len = this.ncells();
830 return this.insert_cell_below(type,len-1);
830 return this.insert_cell_below(type,len-1);
831 };
831 };
832
832
833 /**
833 /**
834 * Turn a cell into a code cell.
834 * Turn a cell into a code cell.
835 *
835 *
836 * @method to_code
836 * @method to_code
837 * @param {Number} [index] A cell's index
837 * @param {Number} [index] A cell's index
838 */
838 */
839 Notebook.prototype.to_code = function (index) {
839 Notebook.prototype.to_code = function (index) {
840 var i = this.index_or_selected(index);
840 var i = this.index_or_selected(index);
841 if (this.is_valid_cell_index(i)) {
841 if (this.is_valid_cell_index(i)) {
842 var source_element = this.get_cell_element(i);
842 var source_element = this.get_cell_element(i);
843 var source_cell = source_element.data("cell");
843 var source_cell = source_element.data("cell");
844 if (!(source_cell instanceof IPython.CodeCell)) {
844 if (!(source_cell instanceof IPython.CodeCell)) {
845 var target_cell = this.insert_cell_below('code',i);
845 var target_cell = this.insert_cell_below('code',i);
846 var text = source_cell.get_text();
846 var text = source_cell.get_text();
847 if (text === source_cell.placeholder) {
847 if (text === source_cell.placeholder) {
848 text = '';
848 text = '';
849 }
849 }
850 target_cell.set_text(text);
850 target_cell.set_text(text);
851 // make this value the starting point, so that we can only undo
851 // make this value the starting point, so that we can only undo
852 // to this state, instead of a blank cell
852 // to this state, instead of a blank cell
853 target_cell.code_mirror.clearHistory();
853 target_cell.code_mirror.clearHistory();
854 source_element.remove();
854 source_element.remove();
855 this.dirty = true;
855 this.dirty = true;
856 };
856 };
857 };
857 };
858 };
858 };
859
859
860 /**
860 /**
861 * Turn a cell into a Markdown cell.
861 * Turn a cell into a Markdown cell.
862 *
862 *
863 * @method to_markdown
863 * @method to_markdown
864 * @param {Number} [index] A cell's index
864 * @param {Number} [index] A cell's index
865 */
865 */
866 Notebook.prototype.to_markdown = function (index) {
866 Notebook.prototype.to_markdown = function (index) {
867 var i = this.index_or_selected(index);
867 var i = this.index_or_selected(index);
868 if (this.is_valid_cell_index(i)) {
868 if (this.is_valid_cell_index(i)) {
869 var source_element = this.get_cell_element(i);
869 var source_element = this.get_cell_element(i);
870 var source_cell = source_element.data("cell");
870 var source_cell = source_element.data("cell");
871 if (!(source_cell instanceof IPython.MarkdownCell)) {
871 if (!(source_cell instanceof IPython.MarkdownCell)) {
872 var target_cell = this.insert_cell_below('markdown',i);
872 var target_cell = this.insert_cell_below('markdown',i);
873 var text = source_cell.get_text();
873 var text = source_cell.get_text();
874 if (text === source_cell.placeholder) {
874 if (text === source_cell.placeholder) {
875 text = '';
875 text = '';
876 };
876 };
877 // The edit must come before the set_text.
877 // The edit must come before the set_text.
878 target_cell.edit();
878 target_cell.edit();
879 target_cell.set_text(text);
879 target_cell.set_text(text);
880 // make this value the starting point, so that we can only undo
880 // make this value the starting point, so that we can only undo
881 // to this state, instead of a blank cell
881 // to this state, instead of a blank cell
882 target_cell.code_mirror.clearHistory();
882 target_cell.code_mirror.clearHistory();
883 source_element.remove();
883 source_element.remove();
884 this.dirty = true;
884 this.dirty = true;
885 };
885 };
886 };
886 };
887 };
887 };
888
888
889 /**
889 /**
890 * Turn a cell into a raw text cell.
890 * Turn a cell into a raw text cell.
891 *
891 *
892 * @method to_raw
892 * @method to_raw
893 * @param {Number} [index] A cell's index
893 * @param {Number} [index] A cell's index
894 */
894 */
895 Notebook.prototype.to_raw = function (index) {
895 Notebook.prototype.to_raw = function (index) {
896 var i = this.index_or_selected(index);
896 var i = this.index_or_selected(index);
897 if (this.is_valid_cell_index(i)) {
897 if (this.is_valid_cell_index(i)) {
898 var source_element = this.get_cell_element(i);
898 var source_element = this.get_cell_element(i);
899 var source_cell = source_element.data("cell");
899 var source_cell = source_element.data("cell");
900 var target_cell = null;
900 var target_cell = null;
901 if (!(source_cell instanceof IPython.RawCell)) {
901 if (!(source_cell instanceof IPython.RawCell)) {
902 target_cell = this.insert_cell_below('raw',i);
902 target_cell = this.insert_cell_below('raw',i);
903 var text = source_cell.get_text();
903 var text = source_cell.get_text();
904 if (text === source_cell.placeholder) {
904 if (text === source_cell.placeholder) {
905 text = '';
905 text = '';
906 };
906 };
907 // The edit must come before the set_text.
907 // The edit must come before the set_text.
908 target_cell.edit();
908 target_cell.edit();
909 target_cell.set_text(text);
909 target_cell.set_text(text);
910 // make this value the starting point, so that we can only undo
910 // make this value the starting point, so that we can only undo
911 // to this state, instead of a blank cell
911 // to this state, instead of a blank cell
912 target_cell.code_mirror.clearHistory();
912 target_cell.code_mirror.clearHistory();
913 source_element.remove();
913 source_element.remove();
914 this.dirty = true;
914 this.dirty = true;
915 };
915 };
916 };
916 };
917 };
917 };
918
918
919 /**
919 /**
920 * Turn a cell into a heading cell.
920 * Turn a cell into a heading cell.
921 *
921 *
922 * @method to_heading
922 * @method to_heading
923 * @param {Number} [index] A cell's index
923 * @param {Number} [index] A cell's index
924 * @param {Number} [level] A heading level (e.g., 1 becomes &lt;h1&gt;)
924 * @param {Number} [level] A heading level (e.g., 1 becomes &lt;h1&gt;)
925 */
925 */
926 Notebook.prototype.to_heading = function (index, level) {
926 Notebook.prototype.to_heading = function (index, level) {
927 level = level || 1;
927 level = level || 1;
928 var i = this.index_or_selected(index);
928 var i = this.index_or_selected(index);
929 if (this.is_valid_cell_index(i)) {
929 if (this.is_valid_cell_index(i)) {
930 var source_element = this.get_cell_element(i);
930 var source_element = this.get_cell_element(i);
931 var source_cell = source_element.data("cell");
931 var source_cell = source_element.data("cell");
932 var target_cell = null;
932 var target_cell = null;
933 if (source_cell instanceof IPython.HeadingCell) {
933 if (source_cell instanceof IPython.HeadingCell) {
934 source_cell.set_level(level);
934 source_cell.set_level(level);
935 } else {
935 } else {
936 target_cell = this.insert_cell_below('heading',i);
936 target_cell = this.insert_cell_below('heading',i);
937 var text = source_cell.get_text();
937 var text = source_cell.get_text();
938 if (text === source_cell.placeholder) {
938 if (text === source_cell.placeholder) {
939 text = '';
939 text = '';
940 };
940 };
941 // The edit must come before the set_text.
941 // The edit must come before the set_text.
942 target_cell.set_level(level);
942 target_cell.set_level(level);
943 target_cell.edit();
943 target_cell.edit();
944 target_cell.set_text(text);
944 target_cell.set_text(text);
945 // make this value the starting point, so that we can only undo
945 // make this value the starting point, so that we can only undo
946 // to this state, instead of a blank cell
946 // to this state, instead of a blank cell
947 target_cell.code_mirror.clearHistory();
947 target_cell.code_mirror.clearHistory();
948 source_element.remove();
948 source_element.remove();
949 this.dirty = true;
949 this.dirty = true;
950 };
950 };
951 $([IPython.events]).trigger('selected_cell_type_changed.Notebook',
951 $([IPython.events]).trigger('selected_cell_type_changed.Notebook',
952 {'cell_type':'heading',level:level}
952 {'cell_type':'heading',level:level}
953 );
953 );
954 };
954 };
955 };
955 };
956
956
957
957
958 // Cut/Copy/Paste
958 // Cut/Copy/Paste
959
959
960 /**
960 /**
961 * Enable UI elements for pasting cells.
961 * Enable UI elements for pasting cells.
962 *
962 *
963 * @method enable_paste
963 * @method enable_paste
964 */
964 */
965 Notebook.prototype.enable_paste = function () {
965 Notebook.prototype.enable_paste = function () {
966 var that = this;
966 var that = this;
967 if (!this.paste_enabled) {
967 if (!this.paste_enabled) {
968 $('#paste_cell_replace').removeClass('ui-state-disabled')
968 $('#paste_cell_replace').removeClass('ui-state-disabled')
969 .on('click', function () {that.paste_cell_replace();});
969 .on('click', function () {that.paste_cell_replace();});
970 $('#paste_cell_above').removeClass('ui-state-disabled')
970 $('#paste_cell_above').removeClass('ui-state-disabled')
971 .on('click', function () {that.paste_cell_above();});
971 .on('click', function () {that.paste_cell_above();});
972 $('#paste_cell_below').removeClass('ui-state-disabled')
972 $('#paste_cell_below').removeClass('ui-state-disabled')
973 .on('click', function () {that.paste_cell_below();});
973 .on('click', function () {that.paste_cell_below();});
974 this.paste_enabled = true;
974 this.paste_enabled = true;
975 };
975 };
976 };
976 };
977
977
978 /**
978 /**
979 * Disable UI elements for pasting cells.
979 * Disable UI elements for pasting cells.
980 *
980 *
981 * @method disable_paste
981 * @method disable_paste
982 */
982 */
983 Notebook.prototype.disable_paste = function () {
983 Notebook.prototype.disable_paste = function () {
984 if (this.paste_enabled) {
984 if (this.paste_enabled) {
985 $('#paste_cell_replace').addClass('ui-state-disabled').off('click');
985 $('#paste_cell_replace').addClass('ui-state-disabled').off('click');
986 $('#paste_cell_above').addClass('ui-state-disabled').off('click');
986 $('#paste_cell_above').addClass('ui-state-disabled').off('click');
987 $('#paste_cell_below').addClass('ui-state-disabled').off('click');
987 $('#paste_cell_below').addClass('ui-state-disabled').off('click');
988 this.paste_enabled = false;
988 this.paste_enabled = false;
989 };
989 };
990 };
990 };
991
991
992 /**
992 /**
993 * Cut a cell.
993 * Cut a cell.
994 *
994 *
995 * @method cut_cell
995 * @method cut_cell
996 */
996 */
997 Notebook.prototype.cut_cell = function () {
997 Notebook.prototype.cut_cell = function () {
998 this.copy_cell();
998 this.copy_cell();
999 this.delete_cell();
999 this.delete_cell();
1000 }
1000 }
1001
1001
1002 /**
1002 /**
1003 * Copy a cell.
1003 * Copy a cell.
1004 *
1004 *
1005 * @method copy_cell
1005 * @method copy_cell
1006 */
1006 */
1007 Notebook.prototype.copy_cell = function () {
1007 Notebook.prototype.copy_cell = function () {
1008 var cell = this.get_selected_cell();
1008 var cell = this.get_selected_cell();
1009 this.clipboard = cell.toJSON();
1009 this.clipboard = cell.toJSON();
1010 this.enable_paste();
1010 this.enable_paste();
1011 };
1011 };
1012
1012
1013 /**
1013 /**
1014 * Replace the selected cell with a cell in the clipboard.
1014 * Replace the selected cell with a cell in the clipboard.
1015 *
1015 *
1016 * @method paste_cell_replace
1016 * @method paste_cell_replace
1017 */
1017 */
1018 Notebook.prototype.paste_cell_replace = function () {
1018 Notebook.prototype.paste_cell_replace = function () {
1019 if (this.clipboard !== null && this.paste_enabled) {
1019 if (this.clipboard !== null && this.paste_enabled) {
1020 var cell_data = this.clipboard;
1020 var cell_data = this.clipboard;
1021 var new_cell = this.insert_cell_above(cell_data.cell_type);
1021 var new_cell = this.insert_cell_above(cell_data.cell_type);
1022 new_cell.fromJSON(cell_data);
1022 new_cell.fromJSON(cell_data);
1023 var old_cell = this.get_next_cell(new_cell);
1023 var old_cell = this.get_next_cell(new_cell);
1024 this.delete_cell(this.find_cell_index(old_cell));
1024 this.delete_cell(this.find_cell_index(old_cell));
1025 this.select(this.find_cell_index(new_cell));
1025 this.select(this.find_cell_index(new_cell));
1026 };
1026 };
1027 };
1027 };
1028
1028
1029 /**
1029 /**
1030 * Paste a cell from the clipboard above the selected cell.
1030 * Paste a cell from the clipboard above the selected cell.
1031 *
1031 *
1032 * @method paste_cell_above
1032 * @method paste_cell_above
1033 */
1033 */
1034 Notebook.prototype.paste_cell_above = function () {
1034 Notebook.prototype.paste_cell_above = function () {
1035 if (this.clipboard !== null && this.paste_enabled) {
1035 if (this.clipboard !== null && this.paste_enabled) {
1036 var cell_data = this.clipboard;
1036 var cell_data = this.clipboard;
1037 var new_cell = this.insert_cell_above(cell_data.cell_type);
1037 var new_cell = this.insert_cell_above(cell_data.cell_type);
1038 new_cell.fromJSON(cell_data);
1038 new_cell.fromJSON(cell_data);
1039 };
1039 };
1040 };
1040 };
1041
1041
1042 /**
1042 /**
1043 * Paste a cell from the clipboard below the selected cell.
1043 * Paste a cell from the clipboard below the selected cell.
1044 *
1044 *
1045 * @method paste_cell_below
1045 * @method paste_cell_below
1046 */
1046 */
1047 Notebook.prototype.paste_cell_below = function () {
1047 Notebook.prototype.paste_cell_below = function () {
1048 if (this.clipboard !== null && this.paste_enabled) {
1048 if (this.clipboard !== null && this.paste_enabled) {
1049 var cell_data = this.clipboard;
1049 var cell_data = this.clipboard;
1050 var new_cell = this.insert_cell_below(cell_data.cell_type);
1050 var new_cell = this.insert_cell_below(cell_data.cell_type);
1051 new_cell.fromJSON(cell_data);
1051 new_cell.fromJSON(cell_data);
1052 };
1052 };
1053 };
1053 };
1054
1054
1055 // Cell undelete
1055 // Cell undelete
1056
1056
1057 /**
1057 /**
1058 * Restore the most recently deleted cell.
1058 * Restore the most recently deleted cell.
1059 *
1059 *
1060 * @method undelete
1060 * @method undelete
1061 */
1061 */
1062 Notebook.prototype.undelete = function() {
1062 Notebook.prototype.undelete = function() {
1063 if (this.undelete_backup !== null && this.undelete_index !== null) {
1063 if (this.undelete_backup !== null && this.undelete_index !== null) {
1064 var current_index = this.get_selected_index();
1064 var current_index = this.get_selected_index();
1065 if (this.undelete_index < current_index) {
1065 if (this.undelete_index < current_index) {
1066 current_index = current_index + 1;
1066 current_index = current_index + 1;
1067 }
1067 }
1068 if (this.undelete_index >= this.ncells()) {
1068 if (this.undelete_index >= this.ncells()) {
1069 this.select(this.ncells() - 1);
1069 this.select(this.ncells() - 1);
1070 }
1070 }
1071 else {
1071 else {
1072 this.select(this.undelete_index);
1072 this.select(this.undelete_index);
1073 }
1073 }
1074 var cell_data = this.undelete_backup;
1074 var cell_data = this.undelete_backup;
1075 var new_cell = null;
1075 var new_cell = null;
1076 if (this.undelete_below) {
1076 if (this.undelete_below) {
1077 new_cell = this.insert_cell_below(cell_data.cell_type);
1077 new_cell = this.insert_cell_below(cell_data.cell_type);
1078 } else {
1078 } else {
1079 new_cell = this.insert_cell_above(cell_data.cell_type);
1079 new_cell = this.insert_cell_above(cell_data.cell_type);
1080 }
1080 }
1081 new_cell.fromJSON(cell_data);
1081 new_cell.fromJSON(cell_data);
1082 this.select(current_index);
1082 this.select(current_index);
1083 this.undelete_backup = null;
1083 this.undelete_backup = null;
1084 this.undelete_index = null;
1084 this.undelete_index = null;
1085 }
1085 }
1086 $('#undelete_cell').addClass('ui-state-disabled');
1086 $('#undelete_cell').addClass('ui-state-disabled');
1087 }
1087 }
1088
1088
1089 // Split/merge
1089 // Split/merge
1090
1090
1091 /**
1091 /**
1092 * Split the selected cell into two, at the cursor.
1092 * Split the selected cell into two, at the cursor.
1093 *
1093 *
1094 * @method split_cell
1094 * @method split_cell
1095 */
1095 */
1096 Notebook.prototype.split_cell = function () {
1096 Notebook.prototype.split_cell = function () {
1097 // Todo: implement spliting for other cell types.
1097 // Todo: implement spliting for other cell types.
1098 var cell = this.get_selected_cell();
1098 var cell = this.get_selected_cell();
1099 if (cell.is_splittable()) {
1099 if (cell.is_splittable()) {
1100 var texta = cell.get_pre_cursor();
1100 var texta = cell.get_pre_cursor();
1101 var textb = cell.get_post_cursor();
1101 var textb = cell.get_post_cursor();
1102 if (cell instanceof IPython.CodeCell) {
1102 if (cell instanceof IPython.CodeCell) {
1103 cell.set_text(texta);
1103 cell.set_text(texta);
1104 var new_cell = this.insert_cell_below('code');
1104 var new_cell = this.insert_cell_below('code');
1105 new_cell.set_text(textb);
1105 new_cell.set_text(textb);
1106 } else if (cell instanceof IPython.MarkdownCell) {
1106 } else if (cell instanceof IPython.MarkdownCell) {
1107 cell.set_text(texta);
1107 cell.set_text(texta);
1108 cell.render();
1108 cell.render();
1109 var new_cell = this.insert_cell_below('markdown');
1109 var new_cell = this.insert_cell_below('markdown');
1110 new_cell.edit(); // editor must be visible to call set_text
1110 new_cell.edit(); // editor must be visible to call set_text
1111 new_cell.set_text(textb);
1111 new_cell.set_text(textb);
1112 new_cell.render();
1112 new_cell.render();
1113 }
1113 }
1114 };
1114 };
1115 };
1115 };
1116
1116
1117 /**
1117 /**
1118 * Combine the selected cell into the cell above it.
1118 * Combine the selected cell into the cell above it.
1119 *
1119 *
1120 * @method merge_cell_above
1120 * @method merge_cell_above
1121 */
1121 */
1122 Notebook.prototype.merge_cell_above = function () {
1122 Notebook.prototype.merge_cell_above = function () {
1123 var index = this.get_selected_index();
1123 var index = this.get_selected_index();
1124 var cell = this.get_cell(index);
1124 var cell = this.get_cell(index);
1125 if (index > 0) {
1125 if (index > 0) {
1126 var upper_cell = this.get_cell(index-1);
1126 var upper_cell = this.get_cell(index-1);
1127 var upper_text = upper_cell.get_text();
1127 var upper_text = upper_cell.get_text();
1128 var text = cell.get_text();
1128 var text = cell.get_text();
1129 if (cell instanceof IPython.CodeCell) {
1129 if (cell instanceof IPython.CodeCell) {
1130 cell.set_text(upper_text+'\n'+text);
1130 cell.set_text(upper_text+'\n'+text);
1131 } else if (cell instanceof IPython.MarkdownCell) {
1131 } else if (cell instanceof IPython.MarkdownCell) {
1132 cell.edit();
1132 cell.edit();
1133 cell.set_text(upper_text+'\n'+text);
1133 cell.set_text(upper_text+'\n'+text);
1134 cell.render();
1134 cell.render();
1135 };
1135 };
1136 this.delete_cell(index-1);
1136 this.delete_cell(index-1);
1137 this.select(this.find_cell_index(cell));
1137 this.select(this.find_cell_index(cell));
1138 };
1138 };
1139 };
1139 };
1140
1140
1141 /**
1141 /**
1142 * Combine the selected cell into the cell below it.
1142 * Combine the selected cell into the cell below it.
1143 *
1143 *
1144 * @method merge_cell_below
1144 * @method merge_cell_below
1145 */
1145 */
1146 Notebook.prototype.merge_cell_below = function () {
1146 Notebook.prototype.merge_cell_below = function () {
1147 var index = this.get_selected_index();
1147 var index = this.get_selected_index();
1148 var cell = this.get_cell(index);
1148 var cell = this.get_cell(index);
1149 if (index < this.ncells()-1) {
1149 if (index < this.ncells()-1) {
1150 var lower_cell = this.get_cell(index+1);
1150 var lower_cell = this.get_cell(index+1);
1151 var lower_text = lower_cell.get_text();
1151 var lower_text = lower_cell.get_text();
1152 var text = cell.get_text();
1152 var text = cell.get_text();
1153 if (cell instanceof IPython.CodeCell) {
1153 if (cell instanceof IPython.CodeCell) {
1154 cell.set_text(text+'\n'+lower_text);
1154 cell.set_text(text+'\n'+lower_text);
1155 } else if (cell instanceof IPython.MarkdownCell) {
1155 } else if (cell instanceof IPython.MarkdownCell) {
1156 cell.edit();
1156 cell.edit();
1157 cell.set_text(text+'\n'+lower_text);
1157 cell.set_text(text+'\n'+lower_text);
1158 cell.render();
1158 cell.render();
1159 };
1159 };
1160 this.delete_cell(index+1);
1160 this.delete_cell(index+1);
1161 this.select(this.find_cell_index(cell));
1161 this.select(this.find_cell_index(cell));
1162 };
1162 };
1163 };
1163 };
1164
1164
1165
1165
1166 // Cell collapsing and output clearing
1166 // Cell collapsing and output clearing
1167
1167
1168 /**
1168 /**
1169 * Hide a cell's output.
1169 * Hide a cell's output.
1170 *
1170 *
1171 * @method collapse
1171 * @method collapse
1172 * @param {Number} index A cell's numeric index
1172 * @param {Number} index A cell's numeric index
1173 */
1173 */
1174 Notebook.prototype.collapse = function (index) {
1174 Notebook.prototype.collapse = function (index) {
1175 var i = this.index_or_selected(index);
1175 var i = this.index_or_selected(index);
1176 this.get_cell(i).collapse();
1176 this.get_cell(i).collapse();
1177 this.dirty = true;
1177 this.dirty = true;
1178 };
1178 };
1179
1179
1180 /**
1180 /**
1181 * Show a cell's output.
1181 * Show a cell's output.
1182 *
1182 *
1183 * @method expand
1183 * @method expand
1184 * @param {Number} index A cell's numeric index
1184 * @param {Number} index A cell's numeric index
1185 */
1185 */
1186 Notebook.prototype.expand = function (index) {
1186 Notebook.prototype.expand = function (index) {
1187 var i = this.index_or_selected(index);
1187 var i = this.index_or_selected(index);
1188 this.get_cell(i).expand();
1188 this.get_cell(i).expand();
1189 this.dirty = true;
1189 this.dirty = true;
1190 };
1190 };
1191
1191
1192 /** Toggle whether a cell's output is collapsed or expanded.
1192 /** Toggle whether a cell's output is collapsed or expanded.
1193 *
1193 *
1194 * @method toggle_output
1194 * @method toggle_output
1195 * @param {Number} index A cell's numeric index
1195 * @param {Number} index A cell's numeric index
1196 */
1196 */
1197 Notebook.prototype.toggle_output = function (index) {
1197 Notebook.prototype.toggle_output = function (index) {
1198 var i = this.index_or_selected(index);
1198 var i = this.index_or_selected(index);
1199 this.get_cell(i).toggle_output();
1199 this.get_cell(i).toggle_output();
1200 this.dirty = true;
1200 this.dirty = true;
1201 };
1201 };
1202
1202
1203 /**
1203 /**
1204 * Toggle a scrollbar for long cell outputs.
1204 * Toggle a scrollbar for long cell outputs.
1205 *
1205 *
1206 * @method toggle_output_scroll
1206 * @method toggle_output_scroll
1207 * @param {Number} index A cell's numeric index
1207 * @param {Number} index A cell's numeric index
1208 */
1208 */
1209 Notebook.prototype.toggle_output_scroll = function (index) {
1209 Notebook.prototype.toggle_output_scroll = function (index) {
1210 var i = this.index_or_selected(index);
1210 var i = this.index_or_selected(index);
1211 this.get_cell(i).toggle_output_scroll();
1211 this.get_cell(i).toggle_output_scroll();
1212 };
1212 };
1213
1213
1214 /**
1214 /**
1215 * Hide each code cell's output area.
1215 * Hide each code cell's output area.
1216 *
1216 *
1217 * @method collapse_all_output
1217 * @method collapse_all_output
1218 */
1218 */
1219 Notebook.prototype.collapse_all_output = function () {
1219 Notebook.prototype.collapse_all_output = function () {
1220 var ncells = this.ncells();
1220 var ncells = this.ncells();
1221 var cells = this.get_cells();
1221 var cells = this.get_cells();
1222 for (var i=0; i<ncells; i++) {
1222 for (var i=0; i<ncells; i++) {
1223 if (cells[i] instanceof IPython.CodeCell) {
1223 if (cells[i] instanceof IPython.CodeCell) {
1224 cells[i].output_area.collapse();
1224 cells[i].output_area.collapse();
1225 }
1225 }
1226 };
1226 };
1227 // this should not be set if the `collapse` key is removed from nbformat
1227 // this should not be set if the `collapse` key is removed from nbformat
1228 this.dirty = true;
1228 this.dirty = true;
1229 };
1229 };
1230
1230
1231 /**
1231 /**
1232 * Expand each code cell's output area, and add a scrollbar for long output.
1232 * Expand each code cell's output area, and add a scrollbar for long output.
1233 *
1233 *
1234 * @method scroll_all_output
1234 * @method scroll_all_output
1235 */
1235 */
1236 Notebook.prototype.scroll_all_output = function () {
1236 Notebook.prototype.scroll_all_output = function () {
1237 var ncells = this.ncells();
1237 var ncells = this.ncells();
1238 var cells = this.get_cells();
1238 var cells = this.get_cells();
1239 for (var i=0; i<ncells; i++) {
1239 for (var i=0; i<ncells; i++) {
1240 if (cells[i] instanceof IPython.CodeCell) {
1240 if (cells[i] instanceof IPython.CodeCell) {
1241 cells[i].output_area.expand();
1241 cells[i].output_area.expand();
1242 cells[i].output_area.scroll_if_long(20);
1242 cells[i].output_area.scroll_if_long(20);
1243 }
1243 }
1244 };
1244 };
1245 // this should not be set if the `collapse` key is removed from nbformat
1245 // this should not be set if the `collapse` key is removed from nbformat
1246 this.dirty = true;
1246 this.dirty = true;
1247 };
1247 };
1248
1248
1249 /**
1249 /**
1250 * Expand each code cell's output area, and remove scrollbars.
1250 * Expand each code cell's output area, and remove scrollbars.
1251 *
1251 *
1252 * @method expand_all_output
1252 * @method expand_all_output
1253 */
1253 */
1254 Notebook.prototype.expand_all_output = function () {
1254 Notebook.prototype.expand_all_output = function () {
1255 var ncells = this.ncells();
1255 var ncells = this.ncells();
1256 var cells = this.get_cells();
1256 var cells = this.get_cells();
1257 for (var i=0; i<ncells; i++) {
1257 for (var i=0; i<ncells; i++) {
1258 if (cells[i] instanceof IPython.CodeCell) {
1258 if (cells[i] instanceof IPython.CodeCell) {
1259 cells[i].output_area.expand();
1259 cells[i].output_area.expand();
1260 cells[i].output_area.unscroll_area();
1260 cells[i].output_area.unscroll_area();
1261 }
1261 }
1262 };
1262 };
1263 // this should not be set if the `collapse` key is removed from nbformat
1263 // this should not be set if the `collapse` key is removed from nbformat
1264 this.dirty = true;
1264 this.dirty = true;
1265 };
1265 };
1266
1266
1267 /**
1267 /**
1268 * Clear each code cell's output area.
1268 * Clear each code cell's output area.
1269 *
1269 *
1270 * @method clear_all_output
1270 * @method clear_all_output
1271 */
1271 */
1272 Notebook.prototype.clear_all_output = function () {
1272 Notebook.prototype.clear_all_output = function () {
1273 var ncells = this.ncells();
1273 var ncells = this.ncells();
1274 var cells = this.get_cells();
1274 var cells = this.get_cells();
1275 for (var i=0; i<ncells; i++) {
1275 for (var i=0; i<ncells; i++) {
1276 if (cells[i] instanceof IPython.CodeCell) {
1276 if (cells[i] instanceof IPython.CodeCell) {
1277 cells[i].clear_output(true,true,true);
1277 cells[i].clear_output(true,true,true);
1278 // Make all In[] prompts blank, as well
1278 // Make all In[] prompts blank, as well
1279 // TODO: make this configurable (via checkbox?)
1279 // TODO: make this configurable (via checkbox?)
1280 cells[i].set_input_prompt();
1280 cells[i].set_input_prompt();
1281 }
1281 }
1282 };
1282 };
1283 this.dirty = true;
1283 this.dirty = true;
1284 };
1284 };
1285
1285
1286
1286
1287 // Other cell functions: line numbers, ...
1287 // Other cell functions: line numbers, ...
1288
1288
1289 /**
1289 /**
1290 * Toggle line numbers in the selected cell's input area.
1290 * Toggle line numbers in the selected cell's input area.
1291 *
1291 *
1292 * @method cell_toggle_line_numbers
1292 * @method cell_toggle_line_numbers
1293 */
1293 */
1294 Notebook.prototype.cell_toggle_line_numbers = function() {
1294 Notebook.prototype.cell_toggle_line_numbers = function() {
1295 this.get_selected_cell().toggle_line_numbers();
1295 this.get_selected_cell().toggle_line_numbers();
1296 };
1296 };
1297
1297
1298 // Kernel related things
1298 // Kernel related things
1299
1299
1300 /**
1300 /**
1301 * Start a new kernel and set it on each code cell.
1301 * Start a new kernel and set it on each code cell.
1302 *
1302 *
1303 * @method start_kernel
1303 * @method start_kernel
1304 */
1304 */
1305 Notebook.prototype.start_kernel = function () {
1305 Notebook.prototype.start_kernel = function () {
1306 var base_url = $('body').data('baseKernelUrl') + "kernels";
1306 var base_url = $('body').data('baseKernelUrl') + "kernels";
1307 this.kernel = new IPython.Kernel(base_url);
1307 this.kernel = new IPython.Kernel(base_url);
1308 this.kernel.start(this.notebook_id);
1308 this.kernel.start(this.notebook_id);
1309 // Now that the kernel has been created, tell the CodeCells about it.
1309 // Now that the kernel has been created, tell the CodeCells about it.
1310 var ncells = this.ncells();
1310 var ncells = this.ncells();
1311 for (var i=0; i<ncells; i++) {
1311 for (var i=0; i<ncells; i++) {
1312 var cell = this.get_cell(i);
1312 var cell = this.get_cell(i);
1313 if (cell instanceof IPython.CodeCell) {
1313 if (cell instanceof IPython.CodeCell) {
1314 cell.set_kernel(this.kernel)
1314 cell.set_kernel(this.kernel)
1315 };
1315 };
1316 };
1316 };
1317 };
1317 };
1318
1318
1319 /**
1319 /**
1320 * Prompt the user to restart the IPython kernel.
1320 * Prompt the user to restart the IPython kernel.
1321 *
1321 *
1322 * @method restart_kernel
1322 * @method restart_kernel
1323 */
1323 */
1324 Notebook.prototype.restart_kernel = function () {
1324 Notebook.prototype.restart_kernel = function () {
1325 var that = this;
1325 var that = this;
1326 var dialog = $('<div/>');
1326 var dialog = $('<div/>');
1327 dialog.html('Do you want to restart the current kernel? You will lose all variables defined in it.');
1327 dialog.html('Do you want to restart the current kernel? You will lose all variables defined in it.');
1328 $(document).append(dialog);
1328 $(document).append(dialog);
1329 dialog.dialog({
1329 dialog.dialog({
1330 resizable: false,
1330 resizable: false,
1331 modal: true,
1331 modal: true,
1332 title: "Restart kernel or continue running?",
1332 title: "Restart kernel or continue running?",
1333 closeText: '',
1333 closeText: '',
1334 buttons : {
1334 buttons : {
1335 "Restart": function () {
1335 "Restart": function () {
1336 that.kernel.restart();
1336 that.kernel.restart();
1337 $(this).dialog('close');
1337 $(this).dialog('close');
1338 },
1338 },
1339 "Continue running": function () {
1339 "Continue running": function () {
1340 $(this).dialog('close');
1340 $(this).dialog('close');
1341 }
1341 }
1342 }
1342 }
1343 });
1343 });
1344 };
1344 };
1345
1345
1346 /**
1346 /**
1347 * Run the selected cell.
1347 * Run the selected cell.
1348 *
1348 *
1349 * Execute or render cell outputs.
1349 * Execute or render cell outputs.
1350 *
1350 *
1351 * @method execute_selected_cell
1351 * @method execute_selected_cell
1352 * @param {Object} options Customize post-execution behavior
1352 * @param {Object} options Customize post-execution behavior
1353 */
1353 */
1354 Notebook.prototype.execute_selected_cell = function (options) {
1354 Notebook.prototype.execute_selected_cell = function (options) {
1355 // add_new: should a new cell be added if we are at the end of the nb
1355 // add_new: should a new cell be added if we are at the end of the nb
1356 // terminal: execute in terminal mode, which stays in the current cell
1356 // terminal: execute in terminal mode, which stays in the current cell
1357 var default_options = {terminal: false, add_new: true};
1357 var default_options = {terminal: false, add_new: true};
1358 $.extend(default_options, options);
1358 $.extend(default_options, options);
1359 var that = this;
1359 var that = this;
1360 var cell = that.get_selected_cell();
1360 var cell = that.get_selected_cell();
1361 var cell_index = that.find_cell_index(cell);
1361 var cell_index = that.find_cell_index(cell);
1362 if (cell instanceof IPython.CodeCell) {
1362 if (cell instanceof IPython.CodeCell) {
1363 cell.execute();
1363 cell.execute();
1364 }
1364 }
1365 if (default_options.terminal) {
1365 if (default_options.terminal) {
1366 cell.select_all();
1366 cell.select_all();
1367 } else {
1367 } else {
1368 if ((cell_index === (that.ncells()-1)) && default_options.add_new) {
1368 if ((cell_index === (that.ncells()-1)) && default_options.add_new) {
1369 that.insert_cell_below('code');
1369 that.insert_cell_below('code');
1370 // If we are adding a new cell at the end, scroll down to show it.
1370 // If we are adding a new cell at the end, scroll down to show it.
1371 that.scroll_to_bottom();
1371 that.scroll_to_bottom();
1372 } else {
1372 } else {
1373 that.select(cell_index+1);
1373 that.select(cell_index+1);
1374 };
1374 };
1375 };
1375 };
1376 this.dirty = true;
1376 this.dirty = true;
1377 };
1377 };
1378
1378
1379 /**
1379 /**
1380 * Execute all cells below the selected cell.
1380 * Execute all cells below the selected cell.
1381 *
1381 *
1382 * @method execute_cells_below
1382 * @method execute_cells_below
1383 */
1383 */
1384 Notebook.prototype.execute_cells_below = function () {
1384 Notebook.prototype.execute_cells_below = function () {
1385 this.execute_cell_range(this.get_selected_index(), this.ncells());
1385 this.execute_cell_range(this.get_selected_index(), this.ncells());
1386 this.scroll_to_bottom();
1386 this.scroll_to_bottom();
1387 };
1387 };
1388
1388
1389 /**
1389 /**
1390 * Execute all cells above the selected cell.
1390 * Execute all cells above the selected cell.
1391 *
1391 *
1392 * @method execute_cells_above
1392 * @method execute_cells_above
1393 */
1393 */
1394 Notebook.prototype.execute_cells_above = function () {
1394 Notebook.prototype.execute_cells_above = function () {
1395 this.execute_cell_range(0, this.get_selected_index());
1395 this.execute_cell_range(0, this.get_selected_index());
1396 };
1396 };
1397
1397
1398 /**
1398 /**
1399 * Execute all cells.
1399 * Execute all cells.
1400 *
1400 *
1401 * @method execute_all_cells
1401 * @method execute_all_cells
1402 */
1402 */
1403 Notebook.prototype.execute_all_cells = function () {
1403 Notebook.prototype.execute_all_cells = function () {
1404 this.execute_cell_range(0, this.ncells());
1404 this.execute_cell_range(0, this.ncells());
1405 this.scroll_to_bottom();
1405 this.scroll_to_bottom();
1406 };
1406 };
1407
1407
1408 /**
1408 /**
1409 * Execute a contiguous range of cells.
1409 * Execute a contiguous range of cells.
1410 *
1410 *
1411 * @method execute_cell_range
1411 * @method execute_cell_range
1412 * @param {Number} start Index of the first cell to execute (inclusive)
1412 * @param {Number} start Index of the first cell to execute (inclusive)
1413 * @param {Number} end Index of the last cell to execute (exclusive)
1413 * @param {Number} end Index of the last cell to execute (exclusive)
1414 */
1414 */
1415 Notebook.prototype.execute_cell_range = function (start, end) {
1415 Notebook.prototype.execute_cell_range = function (start, end) {
1416 for (var i=start; i<end; i++) {
1416 for (var i=start; i<end; i++) {
1417 this.select(i);
1417 this.select(i);
1418 this.execute_selected_cell({add_new:false});
1418 this.execute_selected_cell({add_new:false});
1419 };
1419 };
1420 };
1420 };
1421
1421
1422 // Persistance and loading
1422 // Persistance and loading
1423
1423
1424 /**
1424 /**
1425 * Getter method for this notebook's ID.
1425 * Getter method for this notebook's ID.
1426 *
1426 *
1427 * @method get_notebook_id
1427 * @method get_notebook_id
1428 * @return {String} This notebook's ID
1428 * @return {String} This notebook's ID
1429 */
1429 */
1430 Notebook.prototype.get_notebook_id = function () {
1430 Notebook.prototype.get_notebook_id = function () {
1431 return this.notebook_id;
1431 return this.notebook_id;
1432 };
1432 };
1433
1433
1434 /**
1434 /**
1435 * Getter method for this notebook's name.
1435 * Getter method for this notebook's name.
1436 *
1436 *
1437 * @method get_notebook_name
1437 * @method get_notebook_name
1438 * @return {String} This notebook's name
1438 * @return {String} This notebook's name
1439 */
1439 */
1440 Notebook.prototype.get_notebook_name = function () {
1440 Notebook.prototype.get_notebook_name = function () {
1441 return this.notebook_name;
1441 return this.notebook_name;
1442 };
1442 };
1443
1443
1444 /**
1444 /**
1445 * Setter method for this notebook's name.
1445 * Setter method for this notebook's name.
1446 *
1446 *
1447 * @method set_notebook_name
1447 * @method set_notebook_name
1448 * @param {String} name A new name for this notebook
1448 * @param {String} name A new name for this notebook
1449 */
1449 */
1450 Notebook.prototype.set_notebook_name = function (name) {
1450 Notebook.prototype.set_notebook_name = function (name) {
1451 this.notebook_name = name;
1451 this.notebook_name = name;
1452 };
1452 };
1453
1453
1454 /**
1454 /**
1455 * Check that a notebook's name is valid.
1455 * Check that a notebook's name is valid.
1456 *
1456 *
1457 * @method test_notebook_name
1457 * @method test_notebook_name
1458 * @param {String} nbname A name for this notebook
1458 * @param {String} nbname A name for this notebook
1459 * @return {Boolean} True if the name is valid, false if invalid
1459 * @return {Boolean} True if the name is valid, false if invalid
1460 */
1460 */
1461 Notebook.prototype.test_notebook_name = function (nbname) {
1461 Notebook.prototype.test_notebook_name = function (nbname) {
1462 nbname = nbname || '';
1462 nbname = nbname || '';
1463 if (this.notebook_name_blacklist_re.test(nbname) == false && nbname.length>0) {
1463 if (this.notebook_name_blacklist_re.test(nbname) == false && nbname.length>0) {
1464 return true;
1464 return true;
1465 } else {
1465 } else {
1466 return false;
1466 return false;
1467 };
1467 };
1468 };
1468 };
1469
1469
1470 /**
1470 /**
1471 * Load a notebook from JSON (.ipynb).
1471 * Load a notebook from JSON (.ipynb).
1472 *
1472 *
1473 * This currently handles one worksheet: others are deleted.
1473 * This currently handles one worksheet: others are deleted.
1474 *
1474 *
1475 * @method fromJSON
1475 * @method fromJSON
1476 * @param {Object} data JSON representation of a notebook
1476 * @param {Object} data JSON representation of a notebook
1477 */
1477 */
1478 Notebook.prototype.fromJSON = function (data) {
1478 Notebook.prototype.fromJSON = function (data) {
1479 var ncells = this.ncells();
1479 var ncells = this.ncells();
1480 var i;
1480 var i;
1481 for (i=0; i<ncells; i++) {
1481 for (i=0; i<ncells; i++) {
1482 // Always delete cell 0 as they get renumbered as they are deleted.
1482 // Always delete cell 0 as they get renumbered as they are deleted.
1483 this.delete_cell(0);
1483 this.delete_cell(0);
1484 };
1484 };
1485 // Save the metadata and name.
1485 // Save the metadata and name.
1486 this.metadata = data.metadata;
1486 this.metadata = data.metadata;
1487 this.notebook_name = data.metadata.name;
1487 this.notebook_name = data.metadata.name;
1488 // Only handle 1 worksheet for now.
1488 // Only handle 1 worksheet for now.
1489 var worksheet = data.worksheets[0];
1489 var worksheet = data.worksheets[0];
1490 if (worksheet !== undefined) {
1490 if (worksheet !== undefined) {
1491 if (worksheet.metadata) {
1491 if (worksheet.metadata) {
1492 this.worksheet_metadata = worksheet.metadata;
1492 this.worksheet_metadata = worksheet.metadata;
1493 }
1493 }
1494 var new_cells = worksheet.cells;
1494 var new_cells = worksheet.cells;
1495 ncells = new_cells.length;
1495 ncells = new_cells.length;
1496 var cell_data = null;
1496 var cell_data = null;
1497 var new_cell = null;
1497 var new_cell = null;
1498 for (i=0; i<ncells; i++) {
1498 for (i=0; i<ncells; i++) {
1499 cell_data = new_cells[i];
1499 cell_data = new_cells[i];
1500 // VERSIONHACK: plaintext -> raw
1500 // VERSIONHACK: plaintext -> raw
1501 // handle never-released plaintext name for raw cells
1501 // handle never-released plaintext name for raw cells
1502 if (cell_data.cell_type === 'plaintext'){
1502 if (cell_data.cell_type === 'plaintext'){
1503 cell_data.cell_type = 'raw';
1503 cell_data.cell_type = 'raw';
1504 }
1504 }
1505
1505
1506 new_cell = this.insert_cell_below(cell_data.cell_type);
1506 new_cell = this.insert_cell_below(cell_data.cell_type);
1507 new_cell.fromJSON(cell_data);
1507 new_cell.fromJSON(cell_data);
1508 };
1508 };
1509 };
1509 };
1510 if (data.worksheets.length > 1) {
1510 if (data.worksheets.length > 1) {
1511 var dialog = $('<div/>');
1511 var dialog = $('<div/>');
1512 dialog.html("This notebook has " + data.worksheets.length + " worksheets, " +
1512 dialog.html("This notebook has " + data.worksheets.length + " worksheets, " +
1513 "but this version of IPython can only handle the first. " +
1513 "but this version of IPython can only handle the first. " +
1514 "If you save this notebook, worksheets after the first will be lost."
1514 "If you save this notebook, worksheets after the first will be lost."
1515 );
1515 );
1516 this.element.append(dialog);
1516 this.element.append(dialog);
1517 dialog.dialog({
1517 dialog.dialog({
1518 resizable: false,
1518 resizable: false,
1519 modal: true,
1519 modal: true,
1520 title: "Multiple worksheets",
1520 title: "Multiple worksheets",
1521 closeText: "",
1521 closeText: "",
1522 close: function(event, ui) {$(this).dialog('destroy').remove();},
1522 close: function(event, ui) {$(this).dialog('destroy').remove();},
1523 buttons : {
1523 buttons : {
1524 "OK": function () {
1524 "OK": function () {
1525 $(this).dialog('close');
1525 $(this).dialog('close');
1526 }
1526 }
1527 },
1527 },
1528 width: 400
1528 width: 400
1529 });
1529 });
1530 }
1530 }
1531 };
1531 };
1532
1532
1533 /**
1533 /**
1534 * Dump this notebook into a JSON-friendly object.
1534 * Dump this notebook into a JSON-friendly object.
1535 *
1535 *
1536 * @method toJSON
1536 * @method toJSON
1537 * @return {Object} A JSON-friendly representation of this notebook.
1537 * @return {Object} A JSON-friendly representation of this notebook.
1538 */
1538 */
1539 Notebook.prototype.toJSON = function () {
1539 Notebook.prototype.toJSON = function () {
1540 var cells = this.get_cells();
1540 var cells = this.get_cells();
1541 var ncells = cells.length;
1541 var ncells = cells.length;
1542 var cell_array = new Array(ncells);
1542 var cell_array = new Array(ncells);
1543 for (var i=0; i<ncells; i++) {
1543 for (var i=0; i<ncells; i++) {
1544 cell_array[i] = cells[i].toJSON();
1544 cell_array[i] = cells[i].toJSON();
1545 };
1545 };
1546 var data = {
1546 var data = {
1547 // Only handle 1 worksheet for now.
1547 // Only handle 1 worksheet for now.
1548 worksheets : [{
1548 worksheets : [{
1549 cells: cell_array,
1549 cells: cell_array,
1550 metadata: this.worksheet_metadata
1550 metadata: this.worksheet_metadata
1551 }],
1551 }],
1552 metadata : this.metadata
1552 metadata : this.metadata
1553 };
1553 };
1554 return data;
1554 return data;
1555 };
1555 };
1556
1556
1557 /**
1557 /**
1558 * Start an autosave timer, for periodically saving the notebook.
1558 * Start an autosave timer, for periodically saving the notebook.
1559 *
1559 *
1560 * @method autosave_notebook
1560 * @method autosave_notebook
1561 * @param {Integer} interval the autosave interval in milliseconds
1561 * @param {Integer} interval the autosave interval in milliseconds
1562 */
1562 */
1563 Notebook.prototype.autosave_notebook = function (interval) {
1563 Notebook.prototype.autosave_notebook = function (interval) {
1564 var that = this;
1564 var that = this;
1565 // clear previous interval, so we don't get simultaneous timers
1565 // clear previous interval, so we don't get simultaneous timers
1566 if (this.autosave_timer) {
1566 if (this.autosave_timer) {
1567 clearInterval(this.autosave_timer);
1567 clearInterval(this.autosave_timer);
1568 }
1568 }
1569
1569
1570 this.autosave_interval = interval;
1570 this.autosave_interval = interval;
1571 if (interval) {
1571 if (interval) {
1572 this.autosave_timer = setInterval(function() {
1572 this.autosave_timer = setInterval(function() {
1573 that.save_notebook();
1573 if (that.dirty) {
1574 that.save_notebook();
1575 }
1574 }, interval);
1576 }, interval);
1575 $([IPython.events]).trigger("autosave_enabled.Notebook", interval);
1577 $([IPython.events]).trigger("autosave_enabled.Notebook", interval);
1576 } else {
1578 } else {
1577 this.autosave_timer = null;
1579 this.autosave_timer = null;
1578 $([IPython.events]).trigger("autosave_disabled.Notebook");
1580 $([IPython.events]).trigger("autosave_disabled.Notebook");
1579 };
1581 };
1580 };
1582 };
1581
1583
1582 /**
1584 /**
1583 * Save this notebook on the server.
1585 * Save this notebook on the server.
1584 *
1586 *
1585 * @method save_notebook
1587 * @method save_notebook
1586 */
1588 */
1587 Notebook.prototype.save_notebook = function () {
1589 Notebook.prototype.save_notebook = function () {
1588 // We may want to move the name/id/nbformat logic inside toJSON?
1590 // We may want to move the name/id/nbformat logic inside toJSON?
1589 var data = this.toJSON();
1591 var data = this.toJSON();
1590 data.metadata.name = this.notebook_name;
1592 data.metadata.name = this.notebook_name;
1591 data.nbformat = this.nbformat;
1593 data.nbformat = this.nbformat;
1592 data.nbformat_minor = this.nbformat_minor;
1594 data.nbformat_minor = this.nbformat_minor;
1593
1595
1594 // time the ajax call for autosave tuning purposes.
1596 // time the ajax call for autosave tuning purposes.
1595 var start = new Date().getTime();
1597 var start = new Date().getTime();
1596
1598
1597 // We do the call with settings so we can set cache to false.
1599 // We do the call with settings so we can set cache to false.
1598 var settings = {
1600 var settings = {
1599 processData : false,
1601 processData : false,
1600 cache : false,
1602 cache : false,
1601 type : "PUT",
1603 type : "PUT",
1602 data : JSON.stringify(data),
1604 data : JSON.stringify(data),
1603 headers : {'Content-Type': 'application/json'},
1605 headers : {'Content-Type': 'application/json'},
1604 success : $.proxy(this.save_notebook_success, this, start),
1606 success : $.proxy(this.save_notebook_success, this, start),
1605 error : $.proxy(this.save_notebook_error, this)
1607 error : $.proxy(this.save_notebook_error, this)
1606 };
1608 };
1607 $([IPython.events]).trigger('notebook_saving.Notebook');
1609 $([IPython.events]).trigger('notebook_saving.Notebook');
1608 var url = this.baseProjectUrl() + 'notebooks/' + this.notebook_id;
1610 var url = this.baseProjectUrl() + 'notebooks/' + this.notebook_id;
1609 $.ajax(url, settings);
1611 $.ajax(url, settings);
1610 };
1612 };
1611
1613
1612 /**
1614 /**
1613 * Success callback for saving a notebook.
1615 * Success callback for saving a notebook.
1614 *
1616 *
1615 * @method save_notebook_success
1617 * @method save_notebook_success
1616 * @param {Integer} start the time when the save request started
1618 * @param {Integer} start the time when the save request started
1617 * @param {Object} data JSON representation of a notebook
1619 * @param {Object} data JSON representation of a notebook
1618 * @param {String} status Description of response status
1620 * @param {String} status Description of response status
1619 * @param {jqXHR} xhr jQuery Ajax object
1621 * @param {jqXHR} xhr jQuery Ajax object
1620 */
1622 */
1621 Notebook.prototype.save_notebook_success = function (start, data, status, xhr) {
1623 Notebook.prototype.save_notebook_success = function (start, data, status, xhr) {
1622 this.dirty = false;
1624 this.dirty = false;
1623 $([IPython.events]).trigger('notebook_saved.Notebook');
1625 $([IPython.events]).trigger('notebook_saved.Notebook');
1624 this._update_autosave_interval(start);
1626 this._update_autosave_interval(start);
1625 if (this._checkpoint_after_save) {
1627 if (this._checkpoint_after_save) {
1626 this.create_checkpoint();
1628 this.create_checkpoint();
1627 this._checkpoint_after_save = false;
1629 this._checkpoint_after_save = false;
1628 };
1630 };
1629 };
1631 };
1630
1632
1631 /**
1633 /**
1632 * update the autosave interval based on how long the last save took
1634 * update the autosave interval based on how long the last save took
1633 *
1635 *
1634 * @method _update_autosave_interval
1636 * @method _update_autosave_interval
1635 * @param {Integer} timestamp when the save request started
1637 * @param {Integer} timestamp when the save request started
1636 */
1638 */
1637 Notebook.prototype._update_autosave_interval = function (start) {
1639 Notebook.prototype._update_autosave_interval = function (start) {
1638 var duration = (new Date().getTime() - start);
1640 var duration = (new Date().getTime() - start);
1639 if (this.autosave_interval) {
1641 if (this.autosave_interval) {
1640 // new save interval: higher of 10x save duration or parameter (default 30 seconds)
1642 // new save interval: higher of 10x save duration or parameter (default 30 seconds)
1641 var interval = Math.max(10 * duration, this.minimum_autosave_interval);
1643 var interval = Math.max(10 * duration, this.minimum_autosave_interval);
1642 // round to 10 seconds, otherwise we will be setting a new interval too often
1644 // round to 10 seconds, otherwise we will be setting a new interval too often
1643 interval = 10000 * Math.round(interval / 10000);
1645 interval = 10000 * Math.round(interval / 10000);
1644 // set new interval, if it's changed
1646 // set new interval, if it's changed
1645 if (interval != this.autosave_interval) {
1647 if (interval != this.autosave_interval) {
1646 this.autosave_notebook(interval);
1648 this.autosave_notebook(interval);
1647 }
1649 }
1648 }
1650 }
1649 };
1651 };
1650
1652
1651 /**
1653 /**
1652 * Failure callback for saving a notebook.
1654 * Failure callback for saving a notebook.
1653 *
1655 *
1654 * @method save_notebook_error
1656 * @method save_notebook_error
1655 * @param {jqXHR} xhr jQuery Ajax object
1657 * @param {jqXHR} xhr jQuery Ajax object
1656 * @param {String} status Description of response status
1658 * @param {String} status Description of response status
1657 * @param {String} error_msg HTTP error message
1659 * @param {String} error_msg HTTP error message
1658 */
1660 */
1659 Notebook.prototype.save_notebook_error = function (xhr, status, error_msg) {
1661 Notebook.prototype.save_notebook_error = function (xhr, status, error_msg) {
1660 $([IPython.events]).trigger('notebook_save_failed.Notebook');
1662 $([IPython.events]).trigger('notebook_save_failed.Notebook');
1661 };
1663 };
1662
1664
1663 /**
1665 /**
1664 * Request a notebook's data from the server.
1666 * Request a notebook's data from the server.
1665 *
1667 *
1666 * @method load_notebook
1668 * @method load_notebook
1667 * @param {String} notebook_id A notebook to load
1669 * @param {String} notebook_id A notebook to load
1668 */
1670 */
1669 Notebook.prototype.load_notebook = function (notebook_id) {
1671 Notebook.prototype.load_notebook = function (notebook_id) {
1670 var that = this;
1672 var that = this;
1671 this.notebook_id = notebook_id;
1673 this.notebook_id = notebook_id;
1672 // We do the call with settings so we can set cache to false.
1674 // We do the call with settings so we can set cache to false.
1673 var settings = {
1675 var settings = {
1674 processData : false,
1676 processData : false,
1675 cache : false,
1677 cache : false,
1676 type : "GET",
1678 type : "GET",
1677 dataType : "json",
1679 dataType : "json",
1678 success : $.proxy(this.load_notebook_success,this),
1680 success : $.proxy(this.load_notebook_success,this),
1679 error : $.proxy(this.load_notebook_error,this),
1681 error : $.proxy(this.load_notebook_error,this),
1680 };
1682 };
1681 $([IPython.events]).trigger('notebook_loading.Notebook');
1683 $([IPython.events]).trigger('notebook_loading.Notebook');
1682 var url = this.baseProjectUrl() + 'notebooks/' + this.notebook_id;
1684 var url = this.baseProjectUrl() + 'notebooks/' + this.notebook_id;
1683 $.ajax(url, settings);
1685 $.ajax(url, settings);
1684 };
1686 };
1685
1687
1686 /**
1688 /**
1687 * Success callback for loading a notebook from the server.
1689 * Success callback for loading a notebook from the server.
1688 *
1690 *
1689 * Load notebook data from the JSON response.
1691 * Load notebook data from the JSON response.
1690 *
1692 *
1691 * @method load_notebook_success
1693 * @method load_notebook_success
1692 * @param {Object} data JSON representation of a notebook
1694 * @param {Object} data JSON representation of a notebook
1693 * @param {String} status Description of response status
1695 * @param {String} status Description of response status
1694 * @param {jqXHR} xhr jQuery Ajax object
1696 * @param {jqXHR} xhr jQuery Ajax object
1695 */
1697 */
1696 Notebook.prototype.load_notebook_success = function (data, status, xhr) {
1698 Notebook.prototype.load_notebook_success = function (data, status, xhr) {
1697 this.fromJSON(data);
1699 this.fromJSON(data);
1698 if (this.ncells() === 0) {
1700 if (this.ncells() === 0) {
1699 this.insert_cell_below('code');
1701 this.insert_cell_below('code');
1700 };
1702 };
1701 this.dirty = false;
1703 this.dirty = false;
1702 this.select(0);
1704 this.select(0);
1703 this.scroll_to_top();
1705 this.scroll_to_top();
1704 if (data.orig_nbformat !== undefined && data.nbformat !== data.orig_nbformat) {
1706 if (data.orig_nbformat !== undefined && data.nbformat !== data.orig_nbformat) {
1705 msg = "This notebook has been converted from an older " +
1707 msg = "This notebook has been converted from an older " +
1706 "notebook format (v"+data.orig_nbformat+") to the current notebook " +
1708 "notebook format (v"+data.orig_nbformat+") to the current notebook " +
1707 "format (v"+data.nbformat+"). The next time you save this notebook, the " +
1709 "format (v"+data.nbformat+"). The next time you save this notebook, the " +
1708 "newer notebook format will be used and older verions of IPython " +
1710 "newer notebook format will be used and older verions of IPython " +
1709 "may not be able to read it. To keep the older version, close the " +
1711 "may not be able to read it. To keep the older version, close the " +
1710 "notebook without saving it.";
1712 "notebook without saving it.";
1711 var dialog = $('<div/>');
1713 var dialog = $('<div/>');
1712 dialog.html(msg);
1714 dialog.html(msg);
1713 this.element.append(dialog);
1715 this.element.append(dialog);
1714 dialog.dialog({
1716 dialog.dialog({
1715 resizable: false,
1717 resizable: false,
1716 modal: true,
1718 modal: true,
1717 title: "Notebook converted",
1719 title: "Notebook converted",
1718 closeText: "",
1720 closeText: "",
1719 close: function(event, ui) {$(this).dialog('destroy').remove();},
1721 close: function(event, ui) {$(this).dialog('destroy').remove();},
1720 buttons : {
1722 buttons : {
1721 "OK": function () {
1723 "OK": function () {
1722 $(this).dialog('close');
1724 $(this).dialog('close');
1723 }
1725 }
1724 },
1726 },
1725 width: 400
1727 width: 400
1726 });
1728 });
1727 } else if (data.orig_nbformat_minor !== undefined && data.nbformat_minor !== data.orig_nbformat_minor) {
1729 } else if (data.orig_nbformat_minor !== undefined && data.nbformat_minor !== data.orig_nbformat_minor) {
1728 var that = this;
1730 var that = this;
1729 var orig_vs = 'v' + data.nbformat + '.' + data.orig_nbformat_minor;
1731 var orig_vs = 'v' + data.nbformat + '.' + data.orig_nbformat_minor;
1730 var this_vs = 'v' + data.nbformat + '.' + this.nbformat_minor;
1732 var this_vs = 'v' + data.nbformat + '.' + this.nbformat_minor;
1731 var msg = "This notebook is version " + orig_vs + ", but we only fully support up to " +
1733 var msg = "This notebook is version " + orig_vs + ", but we only fully support up to " +
1732 this_vs + ". You can still work with this notebook, but some features " +
1734 this_vs + ". You can still work with this notebook, but some features " +
1733 "introduced in later notebook versions may not be available."
1735 "introduced in later notebook versions may not be available."
1734
1736
1735 var dialog = $('<div/>');
1737 var dialog = $('<div/>');
1736 dialog.html(msg);
1738 dialog.html(msg);
1737 this.element.append(dialog);
1739 this.element.append(dialog);
1738 dialog.dialog({
1740 dialog.dialog({
1739 resizable: false,
1741 resizable: false,
1740 modal: true,
1742 modal: true,
1741 title: "Newer Notebook",
1743 title: "Newer Notebook",
1742 closeText: "",
1744 closeText: "",
1743 close: function(event, ui) {$(this).dialog('destroy').remove();},
1745 close: function(event, ui) {$(this).dialog('destroy').remove();},
1744 buttons : {
1746 buttons : {
1745 "OK": function () {
1747 "OK": function () {
1746 $(this).dialog('close');
1748 $(this).dialog('close');
1747 }
1749 }
1748 },
1750 },
1749 width: 400
1751 width: 400
1750 });
1752 });
1751
1753
1752 }
1754 }
1753
1755
1754 // Create the kernel after the notebook is completely loaded to prevent
1756 // Create the kernel after the notebook is completely loaded to prevent
1755 // code execution upon loading, which is a security risk.
1757 // code execution upon loading, which is a security risk.
1756 if (! this.read_only) {
1758 if (! this.read_only) {
1757 this.start_kernel();
1759 this.start_kernel();
1758 // load our checkpoint list
1760 // load our checkpoint list
1759 IPython.notebook.list_checkpoints();
1761 IPython.notebook.list_checkpoints();
1760 }
1762 }
1761 $([IPython.events]).trigger('notebook_loaded.Notebook');
1763 $([IPython.events]).trigger('notebook_loaded.Notebook');
1762 };
1764 };
1763
1765
1764 /**
1766 /**
1765 * Failure callback for loading a notebook from the server.
1767 * Failure callback for loading a notebook from the server.
1766 *
1768 *
1767 * @method load_notebook_error
1769 * @method load_notebook_error
1768 * @param {jqXHR} xhr jQuery Ajax object
1770 * @param {jqXHR} xhr jQuery Ajax object
1769 * @param {String} textStatus Description of response status
1771 * @param {String} textStatus Description of response status
1770 * @param {String} errorThrow HTTP error message
1772 * @param {String} errorThrow HTTP error message
1771 */
1773 */
1772 Notebook.prototype.load_notebook_error = function (xhr, textStatus, errorThrow) {
1774 Notebook.prototype.load_notebook_error = function (xhr, textStatus, errorThrow) {
1773 if (xhr.status === 500) {
1775 if (xhr.status === 500) {
1774 var msg = "An error occurred while loading this notebook. Most likely " +
1776 var msg = "An error occurred while loading this notebook. Most likely " +
1775 "this notebook is in a newer format than is supported by this " +
1777 "this notebook is in a newer format than is supported by this " +
1776 "version of IPython. This version can load notebook formats " +
1778 "version of IPython. This version can load notebook formats " +
1777 "v"+this.nbformat+" or earlier.";
1779 "v"+this.nbformat+" or earlier.";
1778 var dialog = $('<div/>');
1780 var dialog = $('<div/>');
1779 dialog.html(msg);
1781 dialog.html(msg);
1780 this.element.append(dialog);
1782 this.element.append(dialog);
1781 dialog.dialog({
1783 dialog.dialog({
1782 resizable: false,
1784 resizable: false,
1783 modal: true,
1785 modal: true,
1784 title: "Error loading notebook",
1786 title: "Error loading notebook",
1785 closeText: "",
1787 closeText: "",
1786 close: function(event, ui) {$(this).dialog('destroy').remove();},
1788 close: function(event, ui) {$(this).dialog('destroy').remove();},
1787 buttons : {
1789 buttons : {
1788 "OK": function () {
1790 "OK": function () {
1789 $(this).dialog('close');
1791 $(this).dialog('close');
1790 }
1792 }
1791 },
1793 },
1792 width: 400
1794 width: 400
1793 });
1795 });
1794 }
1796 }
1795 }
1797 }
1796
1798
1797 /********************* checkpoint-related *********************/
1799 /********************* checkpoint-related *********************/
1798
1800
1799 /**
1801 /**
1800 * Save the notebook then immediately create a checkpoint.
1802 * Save the notebook then immediately create a checkpoint.
1801 *
1803 *
1802 * @method save_checkpoint
1804 * @method save_checkpoint
1803 */
1805 */
1804 Notebook.prototype.save_checkpoint = function () {
1806 Notebook.prototype.save_checkpoint = function () {
1805 this._checkpoint_after_save = true;
1807 this._checkpoint_after_save = true;
1806 this.save_notebook();
1808 this.save_notebook();
1807 };
1809 };
1808
1810
1809 /**
1811 /**
1810 * List checkpoints for this notebook.
1812 * List checkpoints for this notebook.
1811 *
1813 *
1812 * @method list_checkpoint
1814 * @method list_checkpoint
1813 */
1815 */
1814 Notebook.prototype.list_checkpoints = function () {
1816 Notebook.prototype.list_checkpoints = function () {
1815 var url = this.baseProjectUrl() + 'notebooks/' + this.notebook_id + '/checkpoints';
1817 var url = this.baseProjectUrl() + 'notebooks/' + this.notebook_id + '/checkpoints';
1816 $.get(url).done(
1818 $.get(url).done(
1817 $.proxy(this.list_checkpoints_success, this)
1819 $.proxy(this.list_checkpoints_success, this)
1818 ).fail(
1820 ).fail(
1819 $.proxy(this.list_checkpoints_error, this)
1821 $.proxy(this.list_checkpoints_error, this)
1820 );
1822 );
1821 };
1823 };
1822
1824
1823 /**
1825 /**
1824 * Success callback for listing checkpoints.
1826 * Success callback for listing checkpoints.
1825 *
1827 *
1826 * @method list_checkpoint_success
1828 * @method list_checkpoint_success
1827 * @param {Object} data JSON representation of a checkpoint
1829 * @param {Object} data JSON representation of a checkpoint
1828 * @param {String} status Description of response status
1830 * @param {String} status Description of response status
1829 * @param {jqXHR} xhr jQuery Ajax object
1831 * @param {jqXHR} xhr jQuery Ajax object
1830 */
1832 */
1831 Notebook.prototype.list_checkpoints_success = function (data, status, xhr) {
1833 Notebook.prototype.list_checkpoints_success = function (data, status, xhr) {
1832 var data = $.parseJSON(data);
1834 var data = $.parseJSON(data);
1833 if (data.length) {
1835 if (data.length) {
1834 this.last_checkpoint = data[0];
1836 this.last_checkpoint = data[0];
1835 } else {
1837 } else {
1836 this.last_checkpoint = null;
1838 this.last_checkpoint = null;
1837 }
1839 }
1838 $([IPython.events]).trigger('checkpoints_listed.Notebook', data);
1840 $([IPython.events]).trigger('checkpoints_listed.Notebook', data);
1839 };
1841 };
1840
1842
1841 /**
1843 /**
1842 * Failure callback for listing a checkpoint.
1844 * Failure callback for listing a checkpoint.
1843 *
1845 *
1844 * @method list_checkpoint_error
1846 * @method list_checkpoint_error
1845 * @param {jqXHR} xhr jQuery Ajax object
1847 * @param {jqXHR} xhr jQuery Ajax object
1846 * @param {String} status Description of response status
1848 * @param {String} status Description of response status
1847 * @param {String} error_msg HTTP error message
1849 * @param {String} error_msg HTTP error message
1848 */
1850 */
1849 Notebook.prototype.list_checkpoints_error = function (xhr, status, error_msg) {
1851 Notebook.prototype.list_checkpoints_error = function (xhr, status, error_msg) {
1850 $([IPython.events]).trigger('list_checkpoints_failed.Notebook');
1852 $([IPython.events]).trigger('list_checkpoints_failed.Notebook');
1851 };
1853 };
1852
1854
1853 /**
1855 /**
1854 * Create a checkpoint of this notebook on the server from the most recent save.
1856 * Create a checkpoint of this notebook on the server from the most recent save.
1855 *
1857 *
1856 * @method create_checkpoint
1858 * @method create_checkpoint
1857 */
1859 */
1858 Notebook.prototype.create_checkpoint = function () {
1860 Notebook.prototype.create_checkpoint = function () {
1859 var url = this.baseProjectUrl() + 'notebooks/' + this.notebook_id + '/checkpoints';
1861 var url = this.baseProjectUrl() + 'notebooks/' + this.notebook_id + '/checkpoints';
1860 $.post(url).done(
1862 $.post(url).done(
1861 $.proxy(this.create_checkpoint_success, this)
1863 $.proxy(this.create_checkpoint_success, this)
1862 ).fail(
1864 ).fail(
1863 $.proxy(this.create_checkpoint_error, this)
1865 $.proxy(this.create_checkpoint_error, this)
1864 );
1866 );
1865 };
1867 };
1866
1868
1867 /**
1869 /**
1868 * Success callback for creating a checkpoint.
1870 * Success callback for creating a checkpoint.
1869 *
1871 *
1870 * @method create_checkpoint_success
1872 * @method create_checkpoint_success
1871 * @param {Object} data JSON representation of a checkpoint
1873 * @param {Object} data JSON representation of a checkpoint
1872 * @param {String} status Description of response status
1874 * @param {String} status Description of response status
1873 * @param {jqXHR} xhr jQuery Ajax object
1875 * @param {jqXHR} xhr jQuery Ajax object
1874 */
1876 */
1875 Notebook.prototype.create_checkpoint_success = function (data, status, xhr) {
1877 Notebook.prototype.create_checkpoint_success = function (data, status, xhr) {
1876 var data = $.parseJSON(data);
1878 var data = $.parseJSON(data);
1877 this.last_checkpoint = data;
1879 this.last_checkpoint = data;
1878 $([IPython.events]).trigger('checkpoint_created.Notebook', data);
1880 $([IPython.events]).trigger('checkpoint_created.Notebook', data);
1879 };
1881 };
1880
1882
1881 /**
1883 /**
1882 * Failure callback for creating a checkpoint.
1884 * Failure callback for creating a checkpoint.
1883 *
1885 *
1884 * @method create_checkpoint_error
1886 * @method create_checkpoint_error
1885 * @param {jqXHR} xhr jQuery Ajax object
1887 * @param {jqXHR} xhr jQuery Ajax object
1886 * @param {String} status Description of response status
1888 * @param {String} status Description of response status
1887 * @param {String} error_msg HTTP error message
1889 * @param {String} error_msg HTTP error message
1888 */
1890 */
1889 Notebook.prototype.create_checkpoint_error = function (xhr, status, error_msg) {
1891 Notebook.prototype.create_checkpoint_error = function (xhr, status, error_msg) {
1890 $([IPython.events]).trigger('checkpoint_failed.Notebook');
1892 $([IPython.events]).trigger('checkpoint_failed.Notebook');
1891 };
1893 };
1892
1894
1893 Notebook.prototype.restore_checkpoint_dialog = function () {
1895 Notebook.prototype.restore_checkpoint_dialog = function () {
1894 var that = this;
1896 var that = this;
1895 var checkpoint = this.last_checkpoint;
1897 var checkpoint = this.last_checkpoint;
1896 if ( ! checkpoint ) {
1898 if ( ! checkpoint ) {
1897 console.log("restore dialog, but no checkpoint to restore to!");
1899 console.log("restore dialog, but no checkpoint to restore to!");
1898 return;
1900 return;
1899 }
1901 }
1900 var dialog = $('<div/>').append(
1902 var dialog = $('<div/>').append(
1901 $('<p/>').text("Are you sure you want to revert the notebook to " +
1903 $('<p/>').text("Are you sure you want to revert the notebook to " +
1902 "the latest checkpoint?"
1904 "the latest checkpoint?"
1903 ).append(
1905 ).append(
1904 $("<strong/>").text(
1906 $("<strong/>").text(
1905 " This cannot be undone."
1907 " This cannot be undone."
1906 )
1908 )
1907 )
1909 )
1908 ).append(
1910 ).append(
1909 $('<p/>').text("The checkpoint was last updated at")
1911 $('<p/>').text("The checkpoint was last updated at")
1910 ).append(
1912 ).append(
1911 $('<p/>').text(Date(checkpoint.last_modified))
1913 $('<p/>').text(Date(checkpoint.last_modified))
1912 );
1914 );
1913
1915
1914 $(document).append(dialog);
1916 $(document).append(dialog);
1915
1917
1916 dialog.dialog({
1918 dialog.dialog({
1917 resizable: false,
1919 resizable: false,
1918 modal: true,
1920 modal: true,
1919 title: "Revert notebook to checkpoint",
1921 title: "Revert notebook to checkpoint",
1920 closeText: '',
1922 closeText: '',
1921 buttons : {
1923 buttons : {
1922 "Revert": function () {
1924 "Revert": function () {
1923 that.restore_checkpoint(checkpoint.checkpoint_id);
1925 that.restore_checkpoint(checkpoint.checkpoint_id);
1924 $(this).dialog('close');
1926 $(this).dialog('close');
1925 },
1927 },
1926 "Cancel": function () {
1928 "Cancel": function () {
1927 $(this).dialog('close');
1929 $(this).dialog('close');
1928 }
1930 }
1929 },
1931 },
1930 width: 400
1932 width: 400
1931 });
1933 });
1932 }
1934 }
1933
1935
1934 /**
1936 /**
1935 * Restore the notebook to a checkpoint state.
1937 * Restore the notebook to a checkpoint state.
1936 *
1938 *
1937 * @method restore_checkpoint
1939 * @method restore_checkpoint
1938 * @param {String} checkpoint ID
1940 * @param {String} checkpoint ID
1939 */
1941 */
1940 Notebook.prototype.restore_checkpoint = function (checkpoint) {
1942 Notebook.prototype.restore_checkpoint = function (checkpoint) {
1941 $([IPython.events]).trigger('notebook_restoring.Notebook', checkpoint);
1943 $([IPython.events]).trigger('notebook_restoring.Notebook', checkpoint);
1942 var url = this.baseProjectUrl() + 'notebooks/' + this.notebook_id + '/checkpoints/' + checkpoint;
1944 var url = this.baseProjectUrl() + 'notebooks/' + this.notebook_id + '/checkpoints/' + checkpoint;
1943 $.post(url).done(
1945 $.post(url).done(
1944 $.proxy(this.restore_checkpoint_success, this)
1946 $.proxy(this.restore_checkpoint_success, this)
1945 ).fail(
1947 ).fail(
1946 $.proxy(this.restore_checkpoint_error, this)
1948 $.proxy(this.restore_checkpoint_error, this)
1947 );
1949 );
1948 };
1950 };
1949
1951
1950 /**
1952 /**
1951 * Success callback for restoring a notebook to a checkpoint.
1953 * Success callback for restoring a notebook to a checkpoint.
1952 *
1954 *
1953 * @method restore_checkpoint_success
1955 * @method restore_checkpoint_success
1954 * @param {Object} data (ignored, should be empty)
1956 * @param {Object} data (ignored, should be empty)
1955 * @param {String} status Description of response status
1957 * @param {String} status Description of response status
1956 * @param {jqXHR} xhr jQuery Ajax object
1958 * @param {jqXHR} xhr jQuery Ajax object
1957 */
1959 */
1958 Notebook.prototype.restore_checkpoint_success = function (data, status, xhr) {
1960 Notebook.prototype.restore_checkpoint_success = function (data, status, xhr) {
1959 $([IPython.events]).trigger('checkpoint_restored.Notebook');
1961 $([IPython.events]).trigger('checkpoint_restored.Notebook');
1960 this.load_notebook(this.notebook_id);
1962 this.load_notebook(this.notebook_id);
1961 };
1963 };
1962
1964
1963 /**
1965 /**
1964 * Failure callback for restoring a notebook to a checkpoint.
1966 * Failure callback for restoring a notebook to a checkpoint.
1965 *
1967 *
1966 * @method restore_checkpoint_error
1968 * @method restore_checkpoint_error
1967 * @param {jqXHR} xhr jQuery Ajax object
1969 * @param {jqXHR} xhr jQuery Ajax object
1968 * @param {String} status Description of response status
1970 * @param {String} status Description of response status
1969 * @param {String} error_msg HTTP error message
1971 * @param {String} error_msg HTTP error message
1970 */
1972 */
1971 Notebook.prototype.restore_checkpoint_error = function (xhr, status, error_msg) {
1973 Notebook.prototype.restore_checkpoint_error = function (xhr, status, error_msg) {
1972 $([IPython.events]).trigger('checkpoint_restore_failed.Notebook');
1974 $([IPython.events]).trigger('checkpoint_restore_failed.Notebook');
1973 };
1975 };
1974
1976
1975 /**
1977 /**
1976 * Delete a notebook checkpoint.
1978 * Delete a notebook checkpoint.
1977 *
1979 *
1978 * @method delete_checkpoint
1980 * @method delete_checkpoint
1979 * @param {String} checkpoint ID
1981 * @param {String} checkpoint ID
1980 */
1982 */
1981 Notebook.prototype.delete_checkpoint = function (checkpoint) {
1983 Notebook.prototype.delete_checkpoint = function (checkpoint) {
1982 $([IPython.events]).trigger('notebook_restoring.Notebook', checkpoint);
1984 $([IPython.events]).trigger('notebook_restoring.Notebook', checkpoint);
1983 var url = this.baseProjectUrl() + 'notebooks/' + this.notebook_id + '/checkpoints/' + checkpoint;
1985 var url = this.baseProjectUrl() + 'notebooks/' + this.notebook_id + '/checkpoints/' + checkpoint;
1984 $.ajax(url, {
1986 $.ajax(url, {
1985 type: 'DELETE',
1987 type: 'DELETE',
1986 success: $.proxy(this.delete_checkpoint_success, this),
1988 success: $.proxy(this.delete_checkpoint_success, this),
1987 error: $.proxy(this.delete_notebook_error,this)
1989 error: $.proxy(this.delete_notebook_error,this)
1988 });
1990 });
1989 };
1991 };
1990
1992
1991 /**
1993 /**
1992 * Success callback for deleting a notebook checkpoint
1994 * Success callback for deleting a notebook checkpoint
1993 *
1995 *
1994 * @method delete_checkpoint_success
1996 * @method delete_checkpoint_success
1995 * @param {Object} data (ignored, should be empty)
1997 * @param {Object} data (ignored, should be empty)
1996 * @param {String} status Description of response status
1998 * @param {String} status Description of response status
1997 * @param {jqXHR} xhr jQuery Ajax object
1999 * @param {jqXHR} xhr jQuery Ajax object
1998 */
2000 */
1999 Notebook.prototype.delete_checkpoint_success = function (data, status, xhr) {
2001 Notebook.prototype.delete_checkpoint_success = function (data, status, xhr) {
2000 $([IPython.events]).trigger('checkpoint_deleted.Notebook', data);
2002 $([IPython.events]).trigger('checkpoint_deleted.Notebook', data);
2001 this.load_notebook(this.notebook_id);
2003 this.load_notebook(this.notebook_id);
2002 };
2004 };
2003
2005
2004 /**
2006 /**
2005 * Failure callback for deleting a notebook checkpoint.
2007 * Failure callback for deleting a notebook checkpoint.
2006 *
2008 *
2007 * @method delete_checkpoint_error
2009 * @method delete_checkpoint_error
2008 * @param {jqXHR} xhr jQuery Ajax object
2010 * @param {jqXHR} xhr jQuery Ajax object
2009 * @param {String} status Description of response status
2011 * @param {String} status Description of response status
2010 * @param {String} error_msg HTTP error message
2012 * @param {String} error_msg HTTP error message
2011 */
2013 */
2012 Notebook.prototype.delete_checkpoint_error = function (xhr, status, error_msg) {
2014 Notebook.prototype.delete_checkpoint_error = function (xhr, status, error_msg) {
2013 $([IPython.events]).trigger('checkpoint_delete_failed.Notebook');
2015 $([IPython.events]).trigger('checkpoint_delete_failed.Notebook');
2014 };
2016 };
2015
2017
2016
2018
2017 IPython.Notebook = Notebook;
2019 IPython.Notebook = Notebook;
2018
2020
2019
2021
2020 return IPython;
2022 return IPython;
2021
2023
2022 }(IPython));
2024 }(IPython));
2023
2025
General Comments 0
You need to be logged in to leave comments. Login now