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