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