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