##// END OF EJS Templates
Removing left over console.logs.
Brian Granger -
Show More
@@ -1,1195 +1,1193 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
15
16 var Notebook = function (selector) {
16 var Notebook = function (selector) {
17 this.read_only = IPython.read_only;
17 this.read_only = IPython.read_only;
18 this.element = $(selector);
18 this.element = $(selector);
19 this.element.scroll();
19 this.element.scroll();
20 this.element.data("notebook", this);
20 this.element.data("notebook", this);
21 this.next_prompt_number = 1;
21 this.next_prompt_number = 1;
22 this.kernel = null;
22 this.kernel = null;
23 this.clipboard = null;
23 this.clipboard = null;
24 this.paste_enabled = false;
24 this.paste_enabled = false;
25 this.dirty = false;
25 this.dirty = false;
26 this.msg_cell_map = {};
26 this.msg_cell_map = {};
27 this.metadata = {};
27 this.metadata = {};
28 this.control_key_active = false;
28 this.control_key_active = false;
29 this.style();
29 this.style();
30 this.create_elements();
30 this.create_elements();
31 this.bind_events();
31 this.bind_events();
32 this.set_tooltipontab(true);
32 this.set_tooltipontab(true);
33 this.set_smartcompleter(true);
33 this.set_smartcompleter(true);
34 this.set_timebeforetooltip(1200);
34 this.set_timebeforetooltip(1200);
35 this.set_autoindent(true);
35 this.set_autoindent(true);
36 };
36 };
37
37
38
38
39 Notebook.prototype.style = function () {
39 Notebook.prototype.style = function () {
40 $('div#notebook').addClass('border-box-sizing');
40 $('div#notebook').addClass('border-box-sizing');
41 };
41 };
42
42
43
43
44 Notebook.prototype.create_elements = function () {
44 Notebook.prototype.create_elements = function () {
45 // We add this end_space div to the end of the notebook div to:
45 // We add this end_space div to the end of the notebook div to:
46 // i) provide a margin between the last cell and the end of the notebook
46 // i) provide a margin between the last cell and the end of the notebook
47 // ii) to prevent the div from scrolling up when the last cell is being
47 // ii) to prevent the div from scrolling up when the last cell is being
48 // edited, but is too low on the page, which browsers will do automatically.
48 // edited, but is too low on the page, which browsers will do automatically.
49 var that = this;
49 var that = this;
50 var end_space = $('<div class="end_space"></div>').height("30%");
50 var end_space = $('<div class="end_space"></div>').height("30%");
51 end_space.dblclick(function (e) {
51 end_space.dblclick(function (e) {
52 if (that.read_only) return;
52 if (that.read_only) return;
53 var ncells = that.ncells();
53 var ncells = that.ncells();
54 that.insert_code_cell_below(ncells-1);
54 that.insert_code_cell_below(ncells-1);
55 });
55 });
56 this.element.append(end_space);
56 this.element.append(end_space);
57 $('div#notebook').addClass('border-box-sizing');
57 $('div#notebook').addClass('border-box-sizing');
58 };
58 };
59
59
60
60
61 Notebook.prototype.bind_events = function () {
61 Notebook.prototype.bind_events = function () {
62 var that = this;
62 var that = this;
63 $(document).keydown(function (event) {
63 $(document).keydown(function (event) {
64 // console.log(event);
64 // console.log(event);
65 if (that.read_only) return true;
65 if (that.read_only) return true;
66 if (event.which === 27) {
66 if (event.which === 27) {
67 // Intercept escape at highest level to avoid closing
67 // Intercept escape at highest level to avoid closing
68 // websocket connection with firefox
68 // websocket connection with firefox
69 event.preventDefault();
69 event.preventDefault();
70 }
70 }
71 if (event.which === 38 && !event.shiftKey) {
71 if (event.which === 38 && !event.shiftKey) {
72 var cell = that.selected_cell();
72 var cell = that.selected_cell();
73 if (cell.at_top()) {
73 if (cell.at_top()) {
74 event.preventDefault();
74 event.preventDefault();
75 that.select_prev();
75 that.select_prev();
76 };
76 };
77 } else if (event.which === 40 && !event.shiftKey) {
77 } else if (event.which === 40 && !event.shiftKey) {
78 var cell = that.selected_cell();
78 var cell = that.selected_cell();
79 if (cell.at_bottom()) {
79 if (cell.at_bottom()) {
80 event.preventDefault();
80 event.preventDefault();
81 that.select_next();
81 that.select_next();
82 };
82 };
83 } else if (event.which === 13 && event.shiftKey) {
83 } else if (event.which === 13 && event.shiftKey) {
84 that.execute_selected_cell();
84 that.execute_selected_cell();
85 return false;
85 return false;
86 } else if (event.which === 13 && event.ctrlKey) {
86 } else if (event.which === 13 && event.ctrlKey) {
87 that.execute_selected_cell({terminal:true});
87 that.execute_selected_cell({terminal:true});
88 return false;
88 return false;
89 } else if (event.which === 77 && event.ctrlKey) {
89 } else if (event.which === 77 && event.ctrlKey) {
90 that.control_key_active = true;
90 that.control_key_active = true;
91 return false;
91 return false;
92 } else if (event.which === 88 && that.control_key_active) {
92 } else if (event.which === 88 && that.control_key_active) {
93 // Cut selected cell = x
93 // Cut selected cell = x
94 that.cut_cell();
94 that.cut_cell();
95 that.control_key_active = false;
95 that.control_key_active = false;
96 return false;
96 return false;
97 } else if (event.which === 67 && that.control_key_active) {
97 } else if (event.which === 67 && that.control_key_active) {
98 // Copy selected cell = c
98 // Copy selected cell = c
99 that.copy_cell();
99 that.copy_cell();
100 that.control_key_active = false;
100 that.control_key_active = false;
101 return false;
101 return false;
102 } else if (event.which === 86 && that.control_key_active) {
102 } else if (event.which === 86 && that.control_key_active) {
103 // Paste selected cell = v
103 // Paste selected cell = v
104 that.paste_cell();
104 that.paste_cell();
105 that.control_key_active = false;
105 that.control_key_active = false;
106 return false;
106 return false;
107 } else if (event.which === 68 && that.control_key_active) {
107 } else if (event.which === 68 && that.control_key_active) {
108 // Delete selected cell = d
108 // Delete selected cell = d
109 that.delete_cell();
109 that.delete_cell();
110 that.control_key_active = false;
110 that.control_key_active = false;
111 return false;
111 return false;
112 } else if (event.which === 65 && that.control_key_active) {
112 } else if (event.which === 65 && that.control_key_active) {
113 // Insert code cell above selected = a
113 // Insert code cell above selected = a
114 that.insert_code_cell_above();
114 that.insert_code_cell_above();
115 that.control_key_active = false;
115 that.control_key_active = false;
116 return false;
116 return false;
117 } else if (event.which === 66 && that.control_key_active) {
117 } else if (event.which === 66 && that.control_key_active) {
118 // Insert code cell below selected = b
118 // Insert code cell below selected = b
119 that.insert_code_cell_below();
119 that.insert_code_cell_below();
120 that.control_key_active = false;
120 that.control_key_active = false;
121 return false;
121 return false;
122 } else if (event.which === 89 && that.control_key_active) {
122 } else if (event.which === 89 && that.control_key_active) {
123 // To code = y
123 // To code = y
124 that.to_code();
124 that.to_code();
125 that.control_key_active = false;
125 that.control_key_active = false;
126 return false;
126 return false;
127 } else if (event.which === 77 && that.control_key_active) {
127 } else if (event.which === 77 && that.control_key_active) {
128 // To markdown = m
128 // To markdown = m
129 that.to_markdown();
129 that.to_markdown();
130 that.control_key_active = false;
130 that.control_key_active = false;
131 return false;
131 return false;
132 } else if (event.which === 84 && that.control_key_active) {
132 } else if (event.which === 84 && that.control_key_active) {
133 // Toggle output = t
133 // Toggle output = t
134 that.toggle_output();
134 that.toggle_output();
135 that.control_key_active = false;
135 that.control_key_active = false;
136 return false;
136 return false;
137 } else if (event.which === 83 && that.control_key_active) {
137 } else if (event.which === 83 && that.control_key_active) {
138 // Save notebook = s
138 // Save notebook = s
139 IPython.save_widget.save_notebook();
139 IPython.save_widget.save_notebook();
140 that.control_key_active = false;
140 that.control_key_active = false;
141 return false;
141 return false;
142 } else if (event.which === 74 && that.control_key_active) {
142 } else if (event.which === 74 && that.control_key_active) {
143 // Move cell down = j
143 // Move cell down = j
144 that.move_cell_down();
144 that.move_cell_down();
145 that.control_key_active = false;
145 that.control_key_active = false;
146 return false;
146 return false;
147 } else if (event.which === 75 && that.control_key_active) {
147 } else if (event.which === 75 && that.control_key_active) {
148 // Move cell up = k
148 // Move cell up = k
149 that.move_cell_up();
149 that.move_cell_up();
150 that.control_key_active = false;
150 that.control_key_active = false;
151 return false;
151 return false;
152 } else if (event.which === 80 && that.control_key_active) {
152 } else if (event.which === 80 && that.control_key_active) {
153 // Select previous = p
153 // Select previous = p
154 that.select_prev();
154 that.select_prev();
155 that.control_key_active = false;
155 that.control_key_active = false;
156 return false;
156 return false;
157 } else if (event.which === 78 && that.control_key_active) {
157 } else if (event.which === 78 && that.control_key_active) {
158 // Select next = n
158 // Select next = n
159 that.select_next();
159 that.select_next();
160 that.control_key_active = false;
160 that.control_key_active = false;
161 return false;
161 return false;
162 } else if (event.which === 76 && that.control_key_active) {
162 } else if (event.which === 76 && that.control_key_active) {
163 // Toggle line numbers = l
163 // Toggle line numbers = l
164 that.cell_toggle_line_numbers();
164 that.cell_toggle_line_numbers();
165 that.control_key_active = false;
165 that.control_key_active = false;
166 return false;
166 return false;
167 } else if (event.which === 73 && that.control_key_active) {
167 } else if (event.which === 73 && that.control_key_active) {
168 // Interrupt kernel = i
168 // Interrupt kernel = i
169 IPython.notebook.kernel.interrupt();
169 IPython.notebook.kernel.interrupt();
170 that.control_key_active = false;
170 that.control_key_active = false;
171 return false;
171 return false;
172 } else if (event.which === 190 && that.control_key_active) {
172 } else if (event.which === 190 && that.control_key_active) {
173 // Restart kernel = . # matches qt console
173 // Restart kernel = . # matches qt console
174 IPython.notebook.restart_kernel();
174 IPython.notebook.restart_kernel();
175 that.control_key_active = false;
175 that.control_key_active = false;
176 return false;
176 return false;
177 } else if (event.which === 72 && that.control_key_active) {
177 } else if (event.which === 72 && that.control_key_active) {
178 // Show keyboard shortcuts = h
178 // Show keyboard shortcuts = h
179 IPython.quick_help.show_keyboard_shortcuts();
179 IPython.quick_help.show_keyboard_shortcuts();
180 that.control_key_active = false;
180 that.control_key_active = false;
181 return false;
181 return false;
182 } else if (event.which === 69 && that.control_key_active) {
182 } else if (event.which === 69 && that.control_key_active) {
183 // Edit in Ace = e
183 // Edit in Ace = e
184 IPython.fulledit_widget.toggle();
184 IPython.fulledit_widget.toggle();
185 that.control_key_active = false;
185 that.control_key_active = false;
186 return false;
186 return false;
187 } else if (that.control_key_active) {
187 } else if (that.control_key_active) {
188 that.control_key_active = false;
188 that.control_key_active = false;
189 return true;
189 return true;
190 };
190 };
191 return true;
191 return true;
192 });
192 });
193
193
194 this.element.bind('collapse_pager', function () {
194 this.element.bind('collapse_pager', function () {
195 var app_height = $('div#main_app').height(); // content height
195 var app_height = $('div#main_app').height(); // content height
196 var splitter_height = $('div#pager_splitter').outerHeight(true);
196 var splitter_height = $('div#pager_splitter').outerHeight(true);
197 var new_height = app_height - splitter_height;
197 var new_height = app_height - splitter_height;
198 that.element.animate({height : new_height + 'px'}, 'fast');
198 that.element.animate({height : new_height + 'px'}, 'fast');
199 });
199 });
200
200
201 this.element.bind('expand_pager', function () {
201 this.element.bind('expand_pager', function () {
202 var app_height = $('div#main_app').height(); // content height
202 var app_height = $('div#main_app').height(); // content height
203 var splitter_height = $('div#pager_splitter').outerHeight(true);
203 var splitter_height = $('div#pager_splitter').outerHeight(true);
204 var pager_height = $('div#pager').outerHeight(true);
204 var pager_height = $('div#pager').outerHeight(true);
205 var new_height = app_height - pager_height - splitter_height;
205 var new_height = app_height - pager_height - splitter_height;
206 that.element.animate({height : new_height + 'px'}, 'fast');
206 that.element.animate({height : new_height + 'px'}, 'fast');
207 });
207 });
208
208
209 $(window).bind('beforeunload', function () {
209 $(window).bind('beforeunload', function () {
210 // TODO: Make killing the kernel configurable.
210 // TODO: Make killing the kernel configurable.
211 var kill_kernel = false;
211 var kill_kernel = false;
212 if (kill_kernel) {
212 if (kill_kernel) {
213 that.kernel.kill();
213 that.kernel.kill();
214 }
214 }
215 if (that.dirty && ! that.read_only) {
215 if (that.dirty && ! that.read_only) {
216 return "You have unsaved changes that will be lost if you leave this page.";
216 return "You have unsaved changes that will be lost if you leave this page.";
217 };
217 };
218 // Null is the *only* return value that will make the browser not
218 // Null is the *only* return value that will make the browser not
219 // pop up the "don't leave" dialog.
219 // pop up the "don't leave" dialog.
220 return null;
220 return null;
221 });
221 });
222 };
222 };
223
223
224
224
225 Notebook.prototype.scroll_to_bottom = function () {
225 Notebook.prototype.scroll_to_bottom = function () {
226 this.element.animate({scrollTop:this.element.get(0).scrollHeight}, 0);
226 this.element.animate({scrollTop:this.element.get(0).scrollHeight}, 0);
227 };
227 };
228
228
229
229
230 Notebook.prototype.scroll_to_top = function () {
230 Notebook.prototype.scroll_to_top = function () {
231 this.element.animate({scrollTop:0}, 0);
231 this.element.animate({scrollTop:0}, 0);
232 };
232 };
233
233
234
234
235 // Cell indexing, retrieval, etc.
235 // Cell indexing, retrieval, etc.
236
236
237
237
238 Notebook.prototype.cell_elements = function () {
238 Notebook.prototype.cell_elements = function () {
239 return this.element.children("div.cell");
239 return this.element.children("div.cell");
240 };
240 };
241
241
242
242
243 Notebook.prototype.ncells = function (cell) {
243 Notebook.prototype.ncells = function (cell) {
244 return this.cell_elements().length;
244 return this.cell_elements().length;
245 };
245 };
246
246
247
247
248 // TODO: we are often calling cells as cells()[i], which we should optimize
248 // TODO: we are often calling cells as cells()[i], which we should optimize
249 // to cells(i) or a new method.
249 // to cells(i) or a new method.
250 Notebook.prototype.cells = function () {
250 Notebook.prototype.cells = function () {
251 return this.cell_elements().toArray().map(function (e) {
251 return this.cell_elements().toArray().map(function (e) {
252 return $(e).data("cell");
252 return $(e).data("cell");
253 });
253 });
254 };
254 };
255
255
256
256
257 Notebook.prototype.find_cell_index = function (cell) {
257 Notebook.prototype.find_cell_index = function (cell) {
258 var result = null;
258 var result = null;
259 this.cell_elements().filter(function (index) {
259 this.cell_elements().filter(function (index) {
260 if ($(this).data("cell") === cell) {
260 if ($(this).data("cell") === cell) {
261 result = index;
261 result = index;
262 };
262 };
263 });
263 });
264 return result;
264 return result;
265 };
265 };
266
266
267
267
268 Notebook.prototype.index_or_selected = function (index) {
268 Notebook.prototype.index_or_selected = function (index) {
269 var i;
269 var i;
270 if (index === undefined) {
270 if (index === undefined) {
271 i = this.selected_index();
271 i = this.selected_index();
272 if (i === null) {
272 if (i === null) {
273 i = 0;
273 i = 0;
274 }
274 }
275 } else {
275 } else {
276 i = index;
276 i = index;
277 }
277 }
278 return i;
278 return i;
279 };
279 };
280
280
281
281
282 Notebook.prototype.select = function (index) {
282 Notebook.prototype.select = function (index) {
283 if (index !== undefined && index >= 0 && index < this.ncells()) {
283 if (index !== undefined && index >= 0 && index < this.ncells()) {
284 if (this.selected_index() !== null) {
284 if (this.selected_index() !== null) {
285 this.selected_cell().unselect();
285 this.selected_cell().unselect();
286 };
286 };
287 this.cells()[index].select();
287 this.cells()[index].select();
288 };
288 };
289 return this;
289 return this;
290 };
290 };
291
291
292
292
293 Notebook.prototype.select_next = function () {
293 Notebook.prototype.select_next = function () {
294 var index = this.selected_index();
294 var index = this.selected_index();
295 if (index !== null && index >= 0 && (index+1) < this.ncells()) {
295 if (index !== null && index >= 0 && (index+1) < this.ncells()) {
296 this.select(index+1);
296 this.select(index+1);
297 };
297 };
298 return this;
298 return this;
299 };
299 };
300
300
301
301
302 Notebook.prototype.select_prev = function () {
302 Notebook.prototype.select_prev = function () {
303 var index = this.selected_index();
303 var index = this.selected_index();
304 if (index !== null && index >= 0 && (index-1) < this.ncells()) {
304 if (index !== null && index >= 0 && (index-1) < this.ncells()) {
305 this.select(index-1);
305 this.select(index-1);
306 };
306 };
307 return this;
307 return this;
308 };
308 };
309
309
310
310
311 Notebook.prototype.selected_index = function () {
311 Notebook.prototype.selected_index = function () {
312 var result = null;
312 var result = null;
313 this.cell_elements().filter(function (index) {
313 this.cell_elements().filter(function (index) {
314 if ($(this).data("cell").selected === true) {
314 if ($(this).data("cell").selected === true) {
315 result = index;
315 result = index;
316 };
316 };
317 });
317 });
318 return result;
318 return result;
319 };
319 };
320
320
321
321
322 Notebook.prototype.cell_for_msg = function (msg_id) {
322 Notebook.prototype.cell_for_msg = function (msg_id) {
323 var cell_id = this.msg_cell_map[msg_id];
323 var cell_id = this.msg_cell_map[msg_id];
324 var result = null;
324 var result = null;
325 this.cell_elements().filter(function (index) {
325 this.cell_elements().filter(function (index) {
326 cell = $(this).data("cell");
326 cell = $(this).data("cell");
327 if (cell.cell_id === cell_id) {
327 if (cell.cell_id === cell_id) {
328 result = cell;
328 result = cell;
329 };
329 };
330 });
330 });
331 return result;
331 return result;
332 };
332 };
333
333
334
334
335 Notebook.prototype.selected_cell = function () {
335 Notebook.prototype.selected_cell = function () {
336 return this.cell_elements().eq(this.selected_index()).data("cell");
336 return this.cell_elements().eq(this.selected_index()).data("cell");
337 };
337 };
338
338
339
339
340 // Cell insertion, deletion and moving.
340 // Cell insertion, deletion and moving.
341
341
342 Notebook.prototype.delete_cell = function (index) {
342 Notebook.prototype.delete_cell = function (index) {
343 var i = this.index_or_selected(index);
343 var i = this.index_or_selected(index);
344 if (i !== null && i >= 0 && i < this.ncells()) {
344 if (i !== null && i >= 0 && i < this.ncells()) {
345 this.cell_elements().eq(i).remove();
345 this.cell_elements().eq(i).remove();
346 if (i === (this.ncells())) {
346 if (i === (this.ncells())) {
347 this.select(i-1);
347 this.select(i-1);
348 } else {
348 } else {
349 this.select(i);
349 this.select(i);
350 };
350 };
351 };
351 };
352 this.dirty = true;
352 this.dirty = true;
353 return this;
353 return this;
354 };
354 };
355
355
356
356
357 Notebook.prototype.append_cell = function (cell) {
357 Notebook.prototype.append_cell = function (cell) {
358 this.element.find('div.end_space').before(cell.element);
358 this.element.find('div.end_space').before(cell.element);
359 this.dirty = true;
359 this.dirty = true;
360 return this;
360 return this;
361 };
361 };
362
362
363
363
364 Notebook.prototype.insert_cell_below = function (cell, index) {
364 Notebook.prototype.insert_cell_below = function (cell, index) {
365 var ncells = this.ncells();
365 var ncells = this.ncells();
366 if (ncells === 0) {
366 if (ncells === 0) {
367 this.append_cell(cell);
367 this.append_cell(cell);
368 return this;
368 return this;
369 };
369 };
370 if (index >= 0 && index < ncells) {
370 if (index >= 0 && index < ncells) {
371 this.cell_elements().eq(index).after(cell.element);
371 this.cell_elements().eq(index).after(cell.element);
372 };
372 };
373 this.dirty = true;
373 this.dirty = true;
374 return this;
374 return this;
375 };
375 };
376
376
377
377
378 Notebook.prototype.insert_cell_above = function (cell, index) {
378 Notebook.prototype.insert_cell_above = function (cell, index) {
379 var ncells = this.ncells();
379 var ncells = this.ncells();
380 if (ncells === 0) {
380 if (ncells === 0) {
381 this.append_cell(cell);
381 this.append_cell(cell);
382 return this;
382 return this;
383 };
383 };
384 if (index >= 0 && index < ncells) {
384 if (index >= 0 && index < ncells) {
385 this.cell_elements().eq(index).before(cell.element);
385 this.cell_elements().eq(index).before(cell.element);
386 };
386 };
387 this.dirty = true;
387 this.dirty = true;
388 return this;
388 return this;
389 };
389 };
390
390
391
391
392 Notebook.prototype.move_cell_up = function (index) {
392 Notebook.prototype.move_cell_up = function (index) {
393 var i = index || this.selected_index();
393 var i = index || this.selected_index();
394 if (i !== null && i < this.ncells() && i > 0) {
394 if (i !== null && i < this.ncells() && i > 0) {
395 var pivot = this.cell_elements().eq(i-1);
395 var pivot = this.cell_elements().eq(i-1);
396 var tomove = this.cell_elements().eq(i);
396 var tomove = this.cell_elements().eq(i);
397 if (pivot !== null && tomove !== null) {
397 if (pivot !== null && tomove !== null) {
398 tomove.detach();
398 tomove.detach();
399 pivot.before(tomove);
399 pivot.before(tomove);
400 this.select(i-1);
400 this.select(i-1);
401 };
401 };
402 };
402 };
403 this.dirty = true;
403 this.dirty = true;
404 return this;
404 return this;
405 };
405 };
406
406
407
407
408 Notebook.prototype.move_cell_down = function (index) {
408 Notebook.prototype.move_cell_down = function (index) {
409 var i = index || this.selected_index();
409 var i = index || this.selected_index();
410 if (i !== null && i < (this.ncells()-1) && i >= 0) {
410 if (i !== null && i < (this.ncells()-1) && i >= 0) {
411 var pivot = this.cell_elements().eq(i+1);
411 var pivot = this.cell_elements().eq(i+1);
412 var tomove = this.cell_elements().eq(i);
412 var tomove = this.cell_elements().eq(i);
413 if (pivot !== null && tomove !== null) {
413 if (pivot !== null && tomove !== null) {
414 tomove.detach();
414 tomove.detach();
415 pivot.after(tomove);
415 pivot.after(tomove);
416 this.select(i+1);
416 this.select(i+1);
417 };
417 };
418 };
418 };
419 this.dirty = true;
419 this.dirty = true;
420 return this;
420 return this;
421 };
421 };
422
422
423
423
424 Notebook.prototype.sort_cells = function () {
424 Notebook.prototype.sort_cells = function () {
425 var ncells = this.ncells();
425 var ncells = this.ncells();
426 var sindex = this.selected_index();
426 var sindex = this.selected_index();
427 var swapped;
427 var swapped;
428 do {
428 do {
429 swapped = false;
429 swapped = false;
430 for (var i=1; i<ncells; i++) {
430 for (var i=1; i<ncells; i++) {
431 current = this.cell_elements().eq(i).data("cell");
431 current = this.cell_elements().eq(i).data("cell");
432 previous = this.cell_elements().eq(i-1).data("cell");
432 previous = this.cell_elements().eq(i-1).data("cell");
433 if (previous.input_prompt_number > current.input_prompt_number) {
433 if (previous.input_prompt_number > current.input_prompt_number) {
434 this.move_cell_up(i);
434 this.move_cell_up(i);
435 swapped = true;
435 swapped = true;
436 };
436 };
437 };
437 };
438 } while (swapped);
438 } while (swapped);
439 this.select(sindex);
439 this.select(sindex);
440 return this;
440 return this;
441 };
441 };
442
442
443
443
444 Notebook.prototype.insert_code_cell_above = function (index) {
444 Notebook.prototype.insert_code_cell_above = function (index) {
445 // TODO: Bounds check for i
445 // TODO: Bounds check for i
446 var i = this.index_or_selected(index);
446 var i = this.index_or_selected(index);
447 var cell = new IPython.CodeCell(this);
447 var cell = new IPython.CodeCell(this);
448 cell.set_input_prompt();
448 cell.set_input_prompt();
449 this.insert_cell_above(cell, i);
449 this.insert_cell_above(cell, i);
450 this.select(this.find_cell_index(cell));
450 this.select(this.find_cell_index(cell));
451 return cell;
451 return cell;
452 };
452 };
453
453
454
454
455 Notebook.prototype.insert_code_cell_below = function (index) {
455 Notebook.prototype.insert_code_cell_below = function (index) {
456 // TODO: Bounds check for i
456 // TODO: Bounds check for i
457 var i = this.index_or_selected(index);
457 var i = this.index_or_selected(index);
458 var cell = new IPython.CodeCell(this);
458 var cell = new IPython.CodeCell(this);
459 cell.set_input_prompt();
459 cell.set_input_prompt();
460 this.insert_cell_below(cell, i);
460 this.insert_cell_below(cell, i);
461 this.select(this.find_cell_index(cell));
461 this.select(this.find_cell_index(cell));
462 return cell;
462 return cell;
463 };
463 };
464
464
465
465
466 Notebook.prototype.insert_html_cell_above = function (index) {
466 Notebook.prototype.insert_html_cell_above = function (index) {
467 // TODO: Bounds check for i
467 // TODO: Bounds check for i
468 var i = this.index_or_selected(index);
468 var i = this.index_or_selected(index);
469 var cell = new IPython.HTMLCell(this);
469 var cell = new IPython.HTMLCell(this);
470 cell.config_mathjax();
470 cell.config_mathjax();
471 this.insert_cell_above(cell, i);
471 this.insert_cell_above(cell, i);
472 this.select(this.find_cell_index(cell));
472 this.select(this.find_cell_index(cell));
473 return cell;
473 return cell;
474 };
474 };
475
475
476
476
477 Notebook.prototype.insert_html_cell_below = function (index) {
477 Notebook.prototype.insert_html_cell_below = function (index) {
478 // TODO: Bounds check for i
478 // TODO: Bounds check for i
479 var i = this.index_or_selected(index);
479 var i = this.index_or_selected(index);
480 var cell = new IPython.HTMLCell(this);
480 var cell = new IPython.HTMLCell(this);
481 cell.config_mathjax();
481 cell.config_mathjax();
482 this.insert_cell_below(cell, i);
482 this.insert_cell_below(cell, i);
483 this.select(this.find_cell_index(cell));
483 this.select(this.find_cell_index(cell));
484 return cell;
484 return cell;
485 };
485 };
486
486
487
487
488 Notebook.prototype.insert_markdown_cell_above = function (index) {
488 Notebook.prototype.insert_markdown_cell_above = function (index) {
489 // TODO: Bounds check for i
489 // TODO: Bounds check for i
490 var i = this.index_or_selected(index);
490 var i = this.index_or_selected(index);
491 var cell = new IPython.MarkdownCell(this);
491 var cell = new IPython.MarkdownCell(this);
492 cell.config_mathjax();
492 cell.config_mathjax();
493 this.insert_cell_above(cell, i);
493 this.insert_cell_above(cell, i);
494 this.select(this.find_cell_index(cell));
494 this.select(this.find_cell_index(cell));
495 return cell;
495 return cell;
496 };
496 };
497
497
498
498
499 Notebook.prototype.insert_markdown_cell_below = function (index) {
499 Notebook.prototype.insert_markdown_cell_below = function (index) {
500 // TODO: Bounds check for i
500 // TODO: Bounds check for i
501 var i = this.index_or_selected(index);
501 var i = this.index_or_selected(index);
502 var cell = new IPython.MarkdownCell(this);
502 var cell = new IPython.MarkdownCell(this);
503 cell.config_mathjax();
503 cell.config_mathjax();
504 this.insert_cell_below(cell, i);
504 this.insert_cell_below(cell, i);
505 this.select(this.find_cell_index(cell));
505 this.select(this.find_cell_index(cell));
506 return cell;
506 return cell;
507 };
507 };
508
508
509
509
510 Notebook.prototype.to_code = function (index) {
510 Notebook.prototype.to_code = function (index) {
511 // TODO: Bounds check for i
511 // TODO: Bounds check for i
512 var i = this.index_or_selected(index);
512 var i = this.index_or_selected(index);
513 var source_element = this.cell_elements().eq(i);
513 var source_element = this.cell_elements().eq(i);
514 var source_cell = source_element.data("cell");
514 var source_cell = source_element.data("cell");
515 if (source_cell instanceof IPython.HTMLCell ||
515 if (source_cell instanceof IPython.HTMLCell ||
516 source_cell instanceof IPython.MarkdownCell) {
516 source_cell instanceof IPython.MarkdownCell) {
517 this.insert_code_cell_below(i);
517 this.insert_code_cell_below(i);
518 var target_cell = this.cells()[i+1];
518 var target_cell = this.cells()[i+1];
519 target_cell.set_code(source_cell.get_source());
519 target_cell.set_code(source_cell.get_source());
520 source_element.remove();
520 source_element.remove();
521 target_cell.select();
521 target_cell.select();
522 };
522 };
523 this.dirty = true;
523 this.dirty = true;
524 };
524 };
525
525
526
526
527 Notebook.prototype.to_markdown = function (index) {
527 Notebook.prototype.to_markdown = function (index) {
528 // TODO: Bounds check for i
528 // TODO: Bounds check for i
529 var i = this.index_or_selected(index);
529 var i = this.index_or_selected(index);
530 var source_element = this.cell_elements().eq(i);
530 var source_element = this.cell_elements().eq(i);
531 var source_cell = source_element.data("cell");
531 var source_cell = source_element.data("cell");
532 var target_cell = null;
532 var target_cell = null;
533 if (source_cell instanceof IPython.CodeCell) {
533 if (source_cell instanceof IPython.CodeCell) {
534 this.insert_markdown_cell_below(i);
534 this.insert_markdown_cell_below(i);
535 target_cell = this.cells()[i+1];
535 target_cell = this.cells()[i+1];
536 var text = source_cell.get_code();
536 var text = source_cell.get_code();
537 } else if (source_cell instanceof IPython.HTMLCell) {
537 } else if (source_cell instanceof IPython.HTMLCell) {
538 this.insert_markdown_cell_below(i);
538 this.insert_markdown_cell_below(i);
539 target_cell = this.cells()[i+1];
539 target_cell = this.cells()[i+1];
540 var text = source_cell.get_source();
540 var text = source_cell.get_source();
541 if (text === source_cell.placeholder) {
541 if (text === source_cell.placeholder) {
542 text = target_cell.placeholder;
542 text = target_cell.placeholder;
543 }
543 }
544 }
544 }
545 if (target_cell !== null) {
545 if (target_cell !== null) {
546 if (text === "") {text = target_cell.placeholder;};
546 if (text === "") {text = target_cell.placeholder;};
547 target_cell.set_source(text);
547 target_cell.set_source(text);
548 source_element.remove();
548 source_element.remove();
549 target_cell.edit();
549 target_cell.edit();
550 }
550 }
551 this.dirty = true;
551 this.dirty = true;
552 };
552 };
553
553
554
554
555 Notebook.prototype.to_html = function (index) {
555 Notebook.prototype.to_html = function (index) {
556 // TODO: Bounds check for i
556 // TODO: Bounds check for i
557 var i = this.index_or_selected(index);
557 var i = this.index_or_selected(index);
558 var source_element = this.cell_elements().eq(i);
558 var source_element = this.cell_elements().eq(i);
559 var source_cell = source_element.data("cell");
559 var source_cell = source_element.data("cell");
560 var target_cell = null;
560 var target_cell = null;
561 if (source_cell instanceof IPython.CodeCell) {
561 if (source_cell instanceof IPython.CodeCell) {
562 this.insert_html_cell_below(i);
562 this.insert_html_cell_below(i);
563 target_cell = this.cells()[i+1];
563 target_cell = this.cells()[i+1];
564 var text = source_cell.get_code();
564 var text = source_cell.get_code();
565 } else if (source_cell instanceof IPython.MarkdownCell) {
565 } else if (source_cell instanceof IPython.MarkdownCell) {
566 this.insert_html_cell_below(i);
566 this.insert_html_cell_below(i);
567 target_cell = this.cells()[i+1];
567 target_cell = this.cells()[i+1];
568 var text = source_cell.get_source();
568 var text = source_cell.get_source();
569 if (text === source_cell.placeholder) {
569 if (text === source_cell.placeholder) {
570 text = target_cell.placeholder;
570 text = target_cell.placeholder;
571 }
571 }
572 }
572 }
573 if (target_cell !== null) {
573 if (target_cell !== null) {
574 if (text === "") {text = target_cell.placeholder;};
574 if (text === "") {text = target_cell.placeholder;};
575 target_cell.set_source(text);
575 target_cell.set_source(text);
576 source_element.remove();
576 source_element.remove();
577 target_cell.edit();
577 target_cell.edit();
578 }
578 }
579 this.dirty = true;
579 this.dirty = true;
580 };
580 };
581
581
582
582
583 // Copy/Paste/Merge/Split
583 // Copy/Paste/Merge/Split
584
584
585 Notebook.prototype.enable_paste = function () {
585 Notebook.prototype.enable_paste = function () {
586 var that = this;
586 var that = this;
587 if (!this.paste_enabled) {
587 if (!this.paste_enabled) {
588 $('#paste_cell').removeClass('ui-state-disabled')
588 $('#paste_cell').removeClass('ui-state-disabled')
589 .on('click', function () {that.paste_cell();});
589 .on('click', function () {that.paste_cell();});
590 $('#paste_cell_above').removeClass('ui-state-disabled')
590 $('#paste_cell_above').removeClass('ui-state-disabled')
591 .on('click', function () {that.paste_cell_above();});
591 .on('click', function () {that.paste_cell_above();});
592 $('#paste_cell_below').removeClass('ui-state-disabled')
592 $('#paste_cell_below').removeClass('ui-state-disabled')
593 .on('click', function () {that.paste_cell_below();});
593 .on('click', function () {that.paste_cell_below();});
594 this.paste_enabled = true;
594 this.paste_enabled = true;
595 };
595 };
596 };
596 };
597
597
598
598
599 Notebook.prototype.disable_paste = function () {
599 Notebook.prototype.disable_paste = function () {
600 if (this.paste_enabled) {
600 if (this.paste_enabled) {
601 $('#paste_cell').addClass('ui-state-disabled').off('click');
601 $('#paste_cell').addClass('ui-state-disabled').off('click');
602 $('#paste_cell_above').addClass('ui-state-disabled').off('click');
602 $('#paste_cell_above').addClass('ui-state-disabled').off('click');
603 $('#paste_cell_below').addClass('ui-state-disabled').off('click');
603 $('#paste_cell_below').addClass('ui-state-disabled').off('click');
604 this.paste_enabled = false;
604 this.paste_enabled = false;
605 };
605 };
606 };
606 };
607
607
608
608
609 Notebook.prototype.cut_cell = function () {
609 Notebook.prototype.cut_cell = function () {
610 console.log('cut_cell');
611 this.copy_cell();
610 this.copy_cell();
612 this.delete_cell();
611 this.delete_cell();
613 }
612 }
614
613
615 Notebook.prototype.copy_cell = function () {
614 Notebook.prototype.copy_cell = function () {
616 var cell = this.selected_cell();
615 var cell = this.selected_cell();
617 this.clipboard = cell.toJSON();
616 this.clipboard = cell.toJSON();
618 this.enable_paste();
617 this.enable_paste();
619 };
618 };
620
619
621
620
622 Notebook.prototype.paste_cell = function () {
621 Notebook.prototype.paste_cell = function () {
623 console.log('paste_cell');
624 if (this.clipboard !== null && this.paste_enabled) {
622 if (this.clipboard !== null && this.paste_enabled) {
625 var cell_data = this.clipboard;
623 var cell_data = this.clipboard;
626 if (cell_data.cell_type == 'code') {
624 if (cell_data.cell_type == 'code') {
627 new_cell = this.insert_code_cell_above();
625 new_cell = this.insert_code_cell_above();
628 new_cell.fromJSON(cell_data);
626 new_cell.fromJSON(cell_data);
629 } else if (cell_data.cell_type === 'html') {
627 } else if (cell_data.cell_type === 'html') {
630 new_cell = this.insert_html_cell_above();
628 new_cell = this.insert_html_cell_above();
631 new_cell.fromJSON(cell_data);
629 new_cell.fromJSON(cell_data);
632 } else if (cell_data.cell_type === 'markdown') {
630 } else if (cell_data.cell_type === 'markdown') {
633 new_cell = this.insert_markdown_cell_above();
631 new_cell = this.insert_markdown_cell_above();
634 new_cell.fromJSON(cell_data);
632 new_cell.fromJSON(cell_data);
635 };
633 };
636 this.select_next();
634 this.select_next();
637 this.delete_cell();
635 this.delete_cell();
638 };
636 };
639 };
637 };
640
638
641
639
642 Notebook.prototype.paste_cell_above = function () {
640 Notebook.prototype.paste_cell_above = function () {
643 if (this.clipboard !== null && this.paste_enabled) {
641 if (this.clipboard !== null && this.paste_enabled) {
644 var cell_data = this.clipboard;
642 var cell_data = this.clipboard;
645 if (cell_data.cell_type == 'code') {
643 if (cell_data.cell_type == 'code') {
646 new_cell = this.insert_code_cell_above();
644 new_cell = this.insert_code_cell_above();
647 new_cell.fromJSON(cell_data);
645 new_cell.fromJSON(cell_data);
648 } else if (cell_data.cell_type === 'html') {
646 } else if (cell_data.cell_type === 'html') {
649 new_cell = this.insert_html_cell_above();
647 new_cell = this.insert_html_cell_above();
650 new_cell.fromJSON(cell_data);
648 new_cell.fromJSON(cell_data);
651 } else if (cell_data.cell_type === 'markdown') {
649 } else if (cell_data.cell_type === 'markdown') {
652 new_cell = this.insert_markdown_cell_above();
650 new_cell = this.insert_markdown_cell_above();
653 new_cell.fromJSON(cell_data);
651 new_cell.fromJSON(cell_data);
654 };
652 };
655 };
653 };
656 };
654 };
657
655
658
656
659 Notebook.prototype.paste_cell_below = function () {
657 Notebook.prototype.paste_cell_below = function () {
660 if (this.clipboard !== null && this.paste_enabled) {
658 if (this.clipboard !== null && this.paste_enabled) {
661 var cell_data = this.clipboard;
659 var cell_data = this.clipboard;
662 if (cell_data.cell_type == 'code') {
660 if (cell_data.cell_type == 'code') {
663 new_cell = this.insert_code_cell_below();
661 new_cell = this.insert_code_cell_below();
664 new_cell.fromJSON(cell_data);
662 new_cell.fromJSON(cell_data);
665 } else if (cell_data.cell_type === 'html') {
663 } else if (cell_data.cell_type === 'html') {
666 new_cell = this.insert_html_cell_below();
664 new_cell = this.insert_html_cell_below();
667 new_cell.fromJSON(cell_data);
665 new_cell.fromJSON(cell_data);
668 } else if (cell_data.cell_type === 'markdown') {
666 } else if (cell_data.cell_type === 'markdown') {
669 new_cell = this.insert_markdown_cell_below();
667 new_cell = this.insert_markdown_cell_below();
670 new_cell.fromJSON(cell_data);
668 new_cell.fromJSON(cell_data);
671 };
669 };
672 };
670 };
673 };
671 };
674
672
675
673
676 Notebook.prototype.split_cell = function () {
674 Notebook.prototype.split_cell = function () {
677 // Todo: implement spliting for other cell types.
675 // Todo: implement spliting for other cell types.
678 var cell = this.selected_cell();
676 var cell = this.selected_cell();
679 if (cell instanceof IPython.CodeCell) {
677 if (cell instanceof IPython.CodeCell) {
680 var cursor = cell.code_mirror.getCursor();
678 var cursor = cell.code_mirror.getCursor();
681 var last_line_num = cell.code_mirror.lineCount()-1;
679 var last_line_num = cell.code_mirror.lineCount()-1;
682 var last_line_len = cell.code_mirror.getLine(last_line_num).length;
680 var last_line_len = cell.code_mirror.getLine(last_line_num).length;
683 var end = {line:last_line_num, ch:last_line_len}
681 var end = {line:last_line_num, ch:last_line_len}
684 var texta = cell.code_mirror.getRange({line:0,ch:0}, cursor);
682 var texta = cell.code_mirror.getRange({line:0,ch:0}, cursor);
685 var textb = cell.code_mirror.getRange(cursor, end);
683 var textb = cell.code_mirror.getRange(cursor, end);
686 texta = texta.replace(/^\n+/, '').replace(/\n+$/, '');
684 texta = texta.replace(/^\n+/, '').replace(/\n+$/, '');
687 textb = textb.replace(/^\n+/, '').replace(/\n+$/, '');
685 textb = textb.replace(/^\n+/, '').replace(/\n+$/, '');
688 cell.set_code(texta);
686 cell.set_code(texta);
689 var new_cell = this.insert_code_cell_below();
687 var new_cell = this.insert_code_cell_below();
690 new_cell.set_code(textb);
688 new_cell.set_code(textb);
691 };
689 };
692 };
690 };
693
691
694
692
695 Notebook.prototype.merge_cell_above = function () {
693 Notebook.prototype.merge_cell_above = function () {
696 // Todo: implement merging for other cell types.
694 // Todo: implement merging for other cell types.
697 var cell = this.selected_cell();
695 var cell = this.selected_cell();
698 var index = this.selected_index();
696 var index = this.selected_index();
699 if (index > 0) {
697 if (index > 0) {
700 upper_cell = this.cells()[index-1];
698 upper_cell = this.cells()[index-1];
701 lower_cell = this.cells()[index];
699 lower_cell = this.cells()[index];
702 if (upper_cell instanceof IPython.CodeCell && lower_cell instanceof IPython.CodeCell) {
700 if (upper_cell instanceof IPython.CodeCell && lower_cell instanceof IPython.CodeCell) {
703 upper_text = upper_cell.get_code();
701 upper_text = upper_cell.get_code();
704 lower_text = lower_cell.get_code();
702 lower_text = lower_cell.get_code();
705 lower_cell.set_code(upper_text+'\n'+lower_text);
703 lower_cell.set_code(upper_text+'\n'+lower_text);
706 this.delete_cell(index-1);
704 this.delete_cell(index-1);
707 };
705 };
708 };
706 };
709 };
707 };
710
708
711
709
712 Notebook.prototype.merge_cell_below = function () {
710 Notebook.prototype.merge_cell_below = function () {
713 // Todo: implement merging for other cell types.
711 // Todo: implement merging for other cell types.
714 var cell = this.selected_cell();
712 var cell = this.selected_cell();
715 var index = this.selected_index();
713 var index = this.selected_index();
716 if (index < this.ncells()-1) {
714 if (index < this.ncells()-1) {
717 upper_cell = this.cells()[index];
715 upper_cell = this.cells()[index];
718 lower_cell = this.cells()[index+1];
716 lower_cell = this.cells()[index+1];
719 if (upper_cell instanceof IPython.CodeCell && lower_cell instanceof IPython.CodeCell) {
717 if (upper_cell instanceof IPython.CodeCell && lower_cell instanceof IPython.CodeCell) {
720 upper_text = upper_cell.get_code();
718 upper_text = upper_cell.get_code();
721 lower_text = lower_cell.get_code();
719 lower_text = lower_cell.get_code();
722 upper_cell.set_code(upper_text+'\n'+lower_text);
720 upper_cell.set_code(upper_text+'\n'+lower_text);
723 this.delete_cell(index+1);
721 this.delete_cell(index+1);
724 };
722 };
725 };
723 };
726 };
724 };
727
725
728 // Cell collapsing and output clearing
726 // Cell collapsing and output clearing
729
727
730 Notebook.prototype.collapse = function (index) {
728 Notebook.prototype.collapse = function (index) {
731 var i = this.index_or_selected(index);
729 var i = this.index_or_selected(index);
732 this.cells()[i].collapse();
730 this.cells()[i].collapse();
733 this.dirty = true;
731 this.dirty = true;
734 };
732 };
735
733
736
734
737 Notebook.prototype.expand = function (index) {
735 Notebook.prototype.expand = function (index) {
738 var i = this.index_or_selected(index);
736 var i = this.index_or_selected(index);
739 this.cells()[i].expand();
737 this.cells()[i].expand();
740 this.dirty = true;
738 this.dirty = true;
741 };
739 };
742
740
743
741
744 Notebook.prototype.toggle_output = function (index) {
742 Notebook.prototype.toggle_output = function (index) {
745 var i = this.index_or_selected(index);
743 var i = this.index_or_selected(index);
746 this.cells()[i].toggle_output();
744 this.cells()[i].toggle_output();
747 this.dirty = true;
745 this.dirty = true;
748 };
746 };
749
747
750
748
751 Notebook.prototype.set_timebeforetooltip = function (time) {
749 Notebook.prototype.set_timebeforetooltip = function (time) {
752 this.time_before_tooltip = time;
750 this.time_before_tooltip = time;
753 };
751 };
754
752
755 Notebook.prototype.set_tooltipontab = function (state) {
753 Notebook.prototype.set_tooltipontab = function (state) {
756 this.tooltip_on_tab = state;
754 this.tooltip_on_tab = state;
757 };
755 };
758
756
759 Notebook.prototype.set_smartcompleter = function (state) {
757 Notebook.prototype.set_smartcompleter = function (state) {
760 this.smart_completer = state;
758 this.smart_completer = state;
761 };
759 };
762
760
763 Notebook.prototype.set_autoindent = function (state) {
761 Notebook.prototype.set_autoindent = function (state) {
764 var cells = this.cells();
762 var cells = this.cells();
765 len = cells.length;
763 len = cells.length;
766 for (var i=0; i<len; i++) {
764 for (var i=0; i<len; i++) {
767 cells[i].set_autoindent(state);
765 cells[i].set_autoindent(state);
768 };
766 };
769 };
767 };
770
768
771
769
772 Notebook.prototype.clear_all_output = function () {
770 Notebook.prototype.clear_all_output = function () {
773 var ncells = this.ncells();
771 var ncells = this.ncells();
774 var cells = this.cells();
772 var cells = this.cells();
775 for (var i=0; i<ncells; i++) {
773 for (var i=0; i<ncells; i++) {
776 if (cells[i] instanceof IPython.CodeCell) {
774 if (cells[i] instanceof IPython.CodeCell) {
777 cells[i].clear_output(true,true,true);
775 cells[i].clear_output(true,true,true);
778 }
776 }
779 };
777 };
780 this.dirty = true;
778 this.dirty = true;
781 };
779 };
782
780
783 // Other cell functions: line numbers, ...
781 // Other cell functions: line numbers, ...
784
782
785 Notebook.prototype.cell_toggle_line_numbers = function() {
783 Notebook.prototype.cell_toggle_line_numbers = function() {
786 this.selected_cell().toggle_line_numbers();
784 this.selected_cell().toggle_line_numbers();
787 };
785 };
788
786
789 // Kernel related things
787 // Kernel related things
790
788
791 Notebook.prototype.start_kernel = function () {
789 Notebook.prototype.start_kernel = function () {
792 this.kernel = new IPython.Kernel();
790 this.kernel = new IPython.Kernel();
793 var notebook_id = IPython.save_widget.get_notebook_id();
791 var notebook_id = IPython.save_widget.get_notebook_id();
794 this.kernel.start(notebook_id, $.proxy(this.kernel_started, this));
792 this.kernel.start(notebook_id, $.proxy(this.kernel_started, this));
795 };
793 };
796
794
797
795
798 Notebook.prototype.restart_kernel = function () {
796 Notebook.prototype.restart_kernel = function () {
799 var that = this;
797 var that = this;
800 var notebook_id = IPython.save_widget.get_notebook_id();
798 var notebook_id = IPython.save_widget.get_notebook_id();
801
799
802 var dialog = $('<div/>');
800 var dialog = $('<div/>');
803 dialog.html('Do you want to restart the current kernel? You will lose all variables defined in it.');
801 dialog.html('Do you want to restart the current kernel? You will lose all variables defined in it.');
804 $(document).append(dialog);
802 $(document).append(dialog);
805 dialog.dialog({
803 dialog.dialog({
806 resizable: false,
804 resizable: false,
807 modal: true,
805 modal: true,
808 title: "Restart kernel or continue running?",
806 title: "Restart kernel or continue running?",
809 closeText: '',
807 closeText: '',
810 buttons : {
808 buttons : {
811 "Restart": function () {
809 "Restart": function () {
812 that.kernel.restart($.proxy(that.kernel_started, that));
810 that.kernel.restart($.proxy(that.kernel_started, that));
813 $(this).dialog('close');
811 $(this).dialog('close');
814 },
812 },
815 "Continue running": function () {
813 "Continue running": function () {
816 $(this).dialog('close');
814 $(this).dialog('close');
817 }
815 }
818 }
816 }
819 });
817 });
820 };
818 };
821
819
822
820
823 Notebook.prototype.kernel_started = function () {
821 Notebook.prototype.kernel_started = function () {
824 console.log("Kernel started: ", this.kernel.kernel_id);
822 console.log("Kernel started: ", this.kernel.kernel_id);
825 this.kernel.shell_channel.onmessage = $.proxy(this.handle_shell_reply,this);
823 this.kernel.shell_channel.onmessage = $.proxy(this.handle_shell_reply,this);
826 this.kernel.iopub_channel.onmessage = $.proxy(this.handle_iopub_reply,this);
824 this.kernel.iopub_channel.onmessage = $.proxy(this.handle_iopub_reply,this);
827 };
825 };
828
826
829
827
830 Notebook.prototype.handle_shell_reply = function (e) {
828 Notebook.prototype.handle_shell_reply = function (e) {
831 reply = $.parseJSON(e.data);
829 reply = $.parseJSON(e.data);
832 var header = reply.header;
830 var header = reply.header;
833 var content = reply.content;
831 var content = reply.content;
834 var msg_type = header.msg_type;
832 var msg_type = header.msg_type;
835 // console.log(reply);
833 // console.log(reply);
836 var cell = this.cell_for_msg(reply.parent_header.msg_id);
834 var cell = this.cell_for_msg(reply.parent_header.msg_id);
837 if (msg_type === "execute_reply") {
835 if (msg_type === "execute_reply") {
838 cell.set_input_prompt(content.execution_count);
836 cell.set_input_prompt(content.execution_count);
839 cell.element.removeClass("running");
837 cell.element.removeClass("running");
840 this.dirty = true;
838 this.dirty = true;
841 } else if (msg_type === "complete_reply") {
839 } else if (msg_type === "complete_reply") {
842 cell.finish_completing(content.matched_text, content.matches);
840 cell.finish_completing(content.matched_text, content.matches);
843 } else if (msg_type === "object_info_reply"){
841 } else if (msg_type === "object_info_reply"){
844 //console.log('back from object_info_request : ')
842 //console.log('back from object_info_request : ')
845 rep = reply.content;
843 rep = reply.content;
846 if(rep.found)
844 if(rep.found)
847 {
845 {
848 cell.finish_tooltip(rep);
846 cell.finish_tooltip(rep);
849 }
847 }
850 } else {
848 } else {
851 //console.log("unknown reply:"+msg_type);
849 //console.log("unknown reply:"+msg_type);
852 }
850 }
853 // when having a rely from object_info_reply,
851 // when having a rely from object_info_reply,
854 // no payload so no nned to handle it
852 // no payload so no nned to handle it
855 if(typeof(content.payload)!='undefined') {
853 if(typeof(content.payload)!='undefined') {
856 var payload = content.payload || [];
854 var payload = content.payload || [];
857 this.handle_payload(cell, payload);
855 this.handle_payload(cell, payload);
858 }
856 }
859 };
857 };
860
858
861
859
862 Notebook.prototype.handle_payload = function (cell, payload) {
860 Notebook.prototype.handle_payload = function (cell, payload) {
863 var l = payload.length;
861 var l = payload.length;
864 for (var i=0; i<l; i++) {
862 for (var i=0; i<l; i++) {
865 if (payload[i].source === 'IPython.zmq.page.page') {
863 if (payload[i].source === 'IPython.zmq.page.page') {
866 if (payload[i].text.trim() !== '') {
864 if (payload[i].text.trim() !== '') {
867 IPython.pager.clear();
865 IPython.pager.clear();
868 IPython.pager.expand();
866 IPython.pager.expand();
869 IPython.pager.append_text(payload[i].text);
867 IPython.pager.append_text(payload[i].text);
870 }
868 }
871 } else if (payload[i].source === 'IPython.zmq.zmqshell.ZMQInteractiveShell.set_next_input') {
869 } else if (payload[i].source === 'IPython.zmq.zmqshell.ZMQInteractiveShell.set_next_input') {
872 var index = this.find_cell_index(cell);
870 var index = this.find_cell_index(cell);
873 var new_cell = this.insert_code_cell_below(index);
871 var new_cell = this.insert_code_cell_below(index);
874 new_cell.set_code(payload[i].text);
872 new_cell.set_code(payload[i].text);
875 this.dirty = true;
873 this.dirty = true;
876 }
874 }
877 };
875 };
878 };
876 };
879
877
880
878
881 Notebook.prototype.handle_iopub_reply = function (e) {
879 Notebook.prototype.handle_iopub_reply = function (e) {
882 reply = $.parseJSON(e.data);
880 reply = $.parseJSON(e.data);
883 var content = reply.content;
881 var content = reply.content;
884 // console.log(reply);
882 // console.log(reply);
885 var msg_type = reply.header.msg_type;
883 var msg_type = reply.header.msg_type;
886 var cell = this.cell_for_msg(reply.parent_header.msg_id);
884 var cell = this.cell_for_msg(reply.parent_header.msg_id);
887 if (msg_type !== 'status' && !cell){
885 if (msg_type !== 'status' && !cell){
888 // message not from this notebook, but should be attached to a cell
886 // message not from this notebook, but should be attached to a cell
889 console.log("Received IOPub message not caused by one of my cells");
887 console.log("Received IOPub message not caused by one of my cells");
890 console.log(reply);
888 console.log(reply);
891 return;
889 return;
892 }
890 }
893 var output_types = ['stream','display_data','pyout','pyerr'];
891 var output_types = ['stream','display_data','pyout','pyerr'];
894 if (output_types.indexOf(msg_type) >= 0) {
892 if (output_types.indexOf(msg_type) >= 0) {
895 this.handle_output(cell, msg_type, content);
893 this.handle_output(cell, msg_type, content);
896 } else if (msg_type === 'status') {
894 } else if (msg_type === 'status') {
897 if (content.execution_state === 'busy') {
895 if (content.execution_state === 'busy') {
898 IPython.kernel_status_widget.status_busy();
896 IPython.kernel_status_widget.status_busy();
899 } else if (content.execution_state === 'idle') {
897 } else if (content.execution_state === 'idle') {
900 IPython.kernel_status_widget.status_idle();
898 IPython.kernel_status_widget.status_idle();
901 } else if (content.execution_state === 'dead') {
899 } else if (content.execution_state === 'dead') {
902 this.handle_status_dead();
900 this.handle_status_dead();
903 };
901 };
904 } else if (msg_type === 'clear_output') {
902 } else if (msg_type === 'clear_output') {
905 cell.clear_output(content.stdout, content.stderr, content.other);
903 cell.clear_output(content.stdout, content.stderr, content.other);
906 };
904 };
907 };
905 };
908
906
909
907
910 Notebook.prototype.handle_status_dead = function () {
908 Notebook.prototype.handle_status_dead = function () {
911 var that = this;
909 var that = this;
912 this.kernel.stop_channels();
910 this.kernel.stop_channels();
913 var dialog = $('<div/>');
911 var dialog = $('<div/>');
914 dialog.html('The kernel has died, would you like to restart it? If you do not restart the kernel, you will be able to save the notebook, but running code will not work until the notebook is reopened.');
912 dialog.html('The kernel has died, would you like to restart it? If you do not restart the kernel, you will be able to save the notebook, but running code will not work until the notebook is reopened.');
915 $(document).append(dialog);
913 $(document).append(dialog);
916 dialog.dialog({
914 dialog.dialog({
917 resizable: false,
915 resizable: false,
918 modal: true,
916 modal: true,
919 title: "Dead kernel",
917 title: "Dead kernel",
920 buttons : {
918 buttons : {
921 "Restart": function () {
919 "Restart": function () {
922 that.start_kernel();
920 that.start_kernel();
923 $(this).dialog('close');
921 $(this).dialog('close');
924 },
922 },
925 "Continue running": function () {
923 "Continue running": function () {
926 $(this).dialog('close');
924 $(this).dialog('close');
927 }
925 }
928 }
926 }
929 });
927 });
930 };
928 };
931
929
932
930
933 Notebook.prototype.handle_output = function (cell, msg_type, content) {
931 Notebook.prototype.handle_output = function (cell, msg_type, content) {
934 var json = {};
932 var json = {};
935 json.output_type = msg_type;
933 json.output_type = msg_type;
936 if (msg_type === "stream") {
934 if (msg_type === "stream") {
937 json.text = utils.fixConsole(content.data);
935 json.text = utils.fixConsole(content.data);
938 json.stream = content.name;
936 json.stream = content.name;
939 } else if (msg_type === "display_data") {
937 } else if (msg_type === "display_data") {
940 json = this.convert_mime_types(json, content.data);
938 json = this.convert_mime_types(json, content.data);
941 } else if (msg_type === "pyout") {
939 } else if (msg_type === "pyout") {
942 json.prompt_number = content.execution_count;
940 json.prompt_number = content.execution_count;
943 json = this.convert_mime_types(json, content.data);
941 json = this.convert_mime_types(json, content.data);
944 } else if (msg_type === "pyerr") {
942 } else if (msg_type === "pyerr") {
945 json.ename = content.ename;
943 json.ename = content.ename;
946 json.evalue = content.evalue;
944 json.evalue = content.evalue;
947 var traceback = [];
945 var traceback = [];
948 for (var i=0; i<content.traceback.length; i++) {
946 for (var i=0; i<content.traceback.length; i++) {
949 traceback.push(utils.fixConsole(content.traceback[i]));
947 traceback.push(utils.fixConsole(content.traceback[i]));
950 }
948 }
951 json.traceback = traceback;
949 json.traceback = traceback;
952 };
950 };
953 cell.append_output(json);
951 cell.append_output(json);
954 this.dirty = true;
952 this.dirty = true;
955 };
953 };
956
954
957
955
958 Notebook.prototype.convert_mime_types = function (json, data) {
956 Notebook.prototype.convert_mime_types = function (json, data) {
959 if (data['text/plain'] !== undefined) {
957 if (data['text/plain'] !== undefined) {
960 json.text = utils.fixConsole(data['text/plain']);
958 json.text = utils.fixConsole(data['text/plain']);
961 };
959 };
962 if (data['text/html'] !== undefined) {
960 if (data['text/html'] !== undefined) {
963 json.html = data['text/html'];
961 json.html = data['text/html'];
964 };
962 };
965 if (data['image/svg+xml'] !== undefined) {
963 if (data['image/svg+xml'] !== undefined) {
966 json.svg = data['image/svg+xml'];
964 json.svg = data['image/svg+xml'];
967 };
965 };
968 if (data['image/png'] !== undefined) {
966 if (data['image/png'] !== undefined) {
969 json.png = data['image/png'];
967 json.png = data['image/png'];
970 };
968 };
971 if (data['image/jpeg'] !== undefined) {
969 if (data['image/jpeg'] !== undefined) {
972 json.jpeg = data['image/jpeg'];
970 json.jpeg = data['image/jpeg'];
973 };
971 };
974 if (data['text/latex'] !== undefined) {
972 if (data['text/latex'] !== undefined) {
975 json.latex = data['text/latex'];
973 json.latex = data['text/latex'];
976 };
974 };
977 if (data['application/json'] !== undefined) {
975 if (data['application/json'] !== undefined) {
978 json.json = data['application/json'];
976 json.json = data['application/json'];
979 };
977 };
980 if (data['application/javascript'] !== undefined) {
978 if (data['application/javascript'] !== undefined) {
981 json.javascript = data['application/javascript'];
979 json.javascript = data['application/javascript'];
982 }
980 }
983 return json;
981 return json;
984 };
982 };
985
983
986
984
987 Notebook.prototype.execute_selected_cell = function (options) {
985 Notebook.prototype.execute_selected_cell = function (options) {
988 // add_new: should a new cell be added if we are at the end of the nb
986 // add_new: should a new cell be added if we are at the end of the nb
989 // terminal: execute in terminal mode, which stays in the current cell
987 // terminal: execute in terminal mode, which stays in the current cell
990 default_options = {terminal: false, add_new: true};
988 default_options = {terminal: false, add_new: true};
991 $.extend(default_options, options);
989 $.extend(default_options, options);
992 var that = this;
990 var that = this;
993 var cell = that.selected_cell();
991 var cell = that.selected_cell();
994 var cell_index = that.find_cell_index(cell);
992 var cell_index = that.find_cell_index(cell);
995 if (cell instanceof IPython.CodeCell) {
993 if (cell instanceof IPython.CodeCell) {
996 cell.clear_output(true, true, true);
994 cell.clear_output(true, true, true);
997 cell.set_input_prompt('*');
995 cell.set_input_prompt('*');
998 cell.element.addClass("running");
996 cell.element.addClass("running");
999 var code = cell.get_code();
997 var code = cell.get_code();
1000 var msg_id = that.kernel.execute(cell.get_code());
998 var msg_id = that.kernel.execute(cell.get_code());
1001 that.msg_cell_map[msg_id] = cell.cell_id;
999 that.msg_cell_map[msg_id] = cell.cell_id;
1002 } else if (cell instanceof IPython.HTMLCell) {
1000 } else if (cell instanceof IPython.HTMLCell) {
1003 cell.render();
1001 cell.render();
1004 }
1002 }
1005 if (default_options.terminal) {
1003 if (default_options.terminal) {
1006 cell.select_all();
1004 cell.select_all();
1007 } else {
1005 } else {
1008 if ((cell_index === (that.ncells()-1)) && default_options.add_new) {
1006 if ((cell_index === (that.ncells()-1)) && default_options.add_new) {
1009 that.insert_code_cell_below();
1007 that.insert_code_cell_below();
1010 // If we are adding a new cell at the end, scroll down to show it.
1008 // If we are adding a new cell at the end, scroll down to show it.
1011 that.scroll_to_bottom();
1009 that.scroll_to_bottom();
1012 } else {
1010 } else {
1013 that.select(cell_index+1);
1011 that.select(cell_index+1);
1014 };
1012 };
1015 };
1013 };
1016 this.dirty = true;
1014 this.dirty = true;
1017 };
1015 };
1018
1016
1019
1017
1020 Notebook.prototype.execute_all_cells = function () {
1018 Notebook.prototype.execute_all_cells = function () {
1021 var ncells = this.ncells();
1019 var ncells = this.ncells();
1022 for (var i=0; i<ncells; i++) {
1020 for (var i=0; i<ncells; i++) {
1023 this.select(i);
1021 this.select(i);
1024 this.execute_selected_cell({add_new:false});
1022 this.execute_selected_cell({add_new:false});
1025 };
1023 };
1026 this.scroll_to_bottom();
1024 this.scroll_to_bottom();
1027 };
1025 };
1028
1026
1029
1027
1030 Notebook.prototype.request_tool_tip = function (cell,func) {
1028 Notebook.prototype.request_tool_tip = function (cell,func) {
1031 // Feel free to shorten this logic if you are better
1029 // Feel free to shorten this logic if you are better
1032 // than me in regEx
1030 // than me in regEx
1033 // basicaly you shoul be able to get xxx.xxx.xxx from
1031 // basicaly you shoul be able to get xxx.xxx.xxx from
1034 // something(range(10), kwarg=smth) ; xxx.xxx.xxx( firstarg, rand(234,23), kwarg1=2,
1032 // something(range(10), kwarg=smth) ; xxx.xxx.xxx( firstarg, rand(234,23), kwarg1=2,
1035 // remove everything between matchin bracket (need to iterate)
1033 // remove everything between matchin bracket (need to iterate)
1036 matchBracket = /\([^\(\)]+\)/g;
1034 matchBracket = /\([^\(\)]+\)/g;
1037 oldfunc = func;
1035 oldfunc = func;
1038 func = func.replace(matchBracket,"");
1036 func = func.replace(matchBracket,"");
1039 while( oldfunc != func )
1037 while( oldfunc != func )
1040 {
1038 {
1041 oldfunc = func;
1039 oldfunc = func;
1042 func = func.replace(matchBracket,"");
1040 func = func.replace(matchBracket,"");
1043 }
1041 }
1044 // remove everythin after last open bracket
1042 // remove everythin after last open bracket
1045 endBracket = /\([^\(]*$/g;
1043 endBracket = /\([^\(]*$/g;
1046 func = func.replace(endBracket,"");
1044 func = func.replace(endBracket,"");
1047 var re = /[a-zA-Z._]+$/g;
1045 var re = /[a-zA-Z._]+$/g;
1048 var msg_id = this.kernel.object_info_request(re.exec(func));
1046 var msg_id = this.kernel.object_info_request(re.exec(func));
1049 if(typeof(msg_id)!='undefined'){
1047 if(typeof(msg_id)!='undefined'){
1050 this.msg_cell_map[msg_id] = cell.cell_id;
1048 this.msg_cell_map[msg_id] = cell.cell_id;
1051 }
1049 }
1052 };
1050 };
1053
1051
1054 Notebook.prototype.complete_cell = function (cell, line, cursor_pos) {
1052 Notebook.prototype.complete_cell = function (cell, line, cursor_pos) {
1055 var msg_id = this.kernel.complete(line, cursor_pos);
1053 var msg_id = this.kernel.complete(line, cursor_pos);
1056 this.msg_cell_map[msg_id] = cell.cell_id;
1054 this.msg_cell_map[msg_id] = cell.cell_id;
1057 };
1055 };
1058
1056
1059 // Persistance and loading
1057 // Persistance and loading
1060
1058
1061
1059
1062 Notebook.prototype.fromJSON = function (data) {
1060 Notebook.prototype.fromJSON = function (data) {
1063 var ncells = this.ncells();
1061 var ncells = this.ncells();
1064 var i;
1062 var i;
1065 for (i=0; i<ncells; i++) {
1063 for (i=0; i<ncells; i++) {
1066 // Always delete cell 0 as they get renumbered as they are deleted.
1064 // Always delete cell 0 as they get renumbered as they are deleted.
1067 this.delete_cell(0);
1065 this.delete_cell(0);
1068 };
1066 };
1069 // Save the metadata
1067 // Save the metadata
1070 this.metadata = data.metadata;
1068 this.metadata = data.metadata;
1071 // Only handle 1 worksheet for now.
1069 // Only handle 1 worksheet for now.
1072 var worksheet = data.worksheets[0];
1070 var worksheet = data.worksheets[0];
1073 if (worksheet !== undefined) {
1071 if (worksheet !== undefined) {
1074 var new_cells = worksheet.cells;
1072 var new_cells = worksheet.cells;
1075 ncells = new_cells.length;
1073 ncells = new_cells.length;
1076 var cell_data = null;
1074 var cell_data = null;
1077 var new_cell = null;
1075 var new_cell = null;
1078 for (i=0; i<ncells; i++) {
1076 for (i=0; i<ncells; i++) {
1079 cell_data = new_cells[i];
1077 cell_data = new_cells[i];
1080 if (cell_data.cell_type == 'code') {
1078 if (cell_data.cell_type == 'code') {
1081 new_cell = this.insert_code_cell_below();
1079 new_cell = this.insert_code_cell_below();
1082 new_cell.fromJSON(cell_data);
1080 new_cell.fromJSON(cell_data);
1083 } else if (cell_data.cell_type === 'html') {
1081 } else if (cell_data.cell_type === 'html') {
1084 new_cell = this.insert_html_cell_below();
1082 new_cell = this.insert_html_cell_below();
1085 new_cell.fromJSON(cell_data);
1083 new_cell.fromJSON(cell_data);
1086 } else if (cell_data.cell_type === 'markdown') {
1084 } else if (cell_data.cell_type === 'markdown') {
1087 new_cell = this.insert_markdown_cell_below();
1085 new_cell = this.insert_markdown_cell_below();
1088 new_cell.fromJSON(cell_data);
1086 new_cell.fromJSON(cell_data);
1089 };
1087 };
1090 };
1088 };
1091 };
1089 };
1092 };
1090 };
1093
1091
1094
1092
1095 Notebook.prototype.toJSON = function () {
1093 Notebook.prototype.toJSON = function () {
1096 var cells = this.cells();
1094 var cells = this.cells();
1097 var ncells = cells.length;
1095 var ncells = cells.length;
1098 cell_array = new Array(ncells);
1096 cell_array = new Array(ncells);
1099 for (var i=0; i<ncells; i++) {
1097 for (var i=0; i<ncells; i++) {
1100 cell_array[i] = cells[i].toJSON();
1098 cell_array[i] = cells[i].toJSON();
1101 };
1099 };
1102 data = {
1100 data = {
1103 // Only handle 1 worksheet for now.
1101 // Only handle 1 worksheet for now.
1104 worksheets : [{cells:cell_array}],
1102 worksheets : [{cells:cell_array}],
1105 metadata : this.metadata
1103 metadata : this.metadata
1106 };
1104 };
1107 return data;
1105 return data;
1108 };
1106 };
1109
1107
1110 Notebook.prototype.save_notebook = function () {
1108 Notebook.prototype.save_notebook = function () {
1111 if (IPython.save_widget.test_notebook_name()) {
1109 if (IPython.save_widget.test_notebook_name()) {
1112 var notebook_id = IPython.save_widget.get_notebook_id();
1110 var notebook_id = IPython.save_widget.get_notebook_id();
1113 var nbname = IPython.save_widget.get_notebook_name();
1111 var nbname = IPython.save_widget.get_notebook_name();
1114 // We may want to move the name/id/nbformat logic inside toJSON?
1112 // We may want to move the name/id/nbformat logic inside toJSON?
1115 var data = this.toJSON();
1113 var data = this.toJSON();
1116 data.metadata.name = nbname;
1114 data.metadata.name = nbname;
1117 data.nbformat = 2;
1115 data.nbformat = 2;
1118 // We do the call with settings so we can set cache to false.
1116 // We do the call with settings so we can set cache to false.
1119 var settings = {
1117 var settings = {
1120 processData : false,
1118 processData : false,
1121 cache : false,
1119 cache : false,
1122 type : "PUT",
1120 type : "PUT",
1123 data : JSON.stringify(data),
1121 data : JSON.stringify(data),
1124 headers : {'Content-Type': 'application/json'},
1122 headers : {'Content-Type': 'application/json'},
1125 success : $.proxy(this.notebook_saved,this),
1123 success : $.proxy(this.notebook_saved,this),
1126 error : $.proxy(this.notebook_save_failed,this)
1124 error : $.proxy(this.notebook_save_failed,this)
1127 };
1125 };
1128 IPython.save_widget.status_saving();
1126 IPython.save_widget.status_saving();
1129 var url = $('body').data('baseProjectUrl') + 'notebooks/' + notebook_id;
1127 var url = $('body').data('baseProjectUrl') + 'notebooks/' + notebook_id;
1130 $.ajax(url, settings);
1128 $.ajax(url, settings);
1131 };
1129 };
1132 };
1130 };
1133
1131
1134
1132
1135 Notebook.prototype.notebook_saved = function (data, status, xhr) {
1133 Notebook.prototype.notebook_saved = function (data, status, xhr) {
1136 this.dirty = false;
1134 this.dirty = false;
1137 IPython.save_widget.notebook_saved();
1135 IPython.save_widget.notebook_saved();
1138 IPython.save_widget.status_last_saved();
1136 IPython.save_widget.status_last_saved();
1139 };
1137 };
1140
1138
1141
1139
1142 Notebook.prototype.notebook_save_failed = function (xhr, status, error_msg) {
1140 Notebook.prototype.notebook_save_failed = function (xhr, status, error_msg) {
1143 IPython.save_widget.status_save_failed();
1141 IPython.save_widget.status_save_failed();
1144 };
1142 };
1145
1143
1146
1144
1147 Notebook.prototype.load_notebook = function (callback) {
1145 Notebook.prototype.load_notebook = function (callback) {
1148 var that = this;
1146 var that = this;
1149 var notebook_id = IPython.save_widget.get_notebook_id();
1147 var notebook_id = IPython.save_widget.get_notebook_id();
1150 // We do the call with settings so we can set cache to false.
1148 // We do the call with settings so we can set cache to false.
1151 var settings = {
1149 var settings = {
1152 processData : false,
1150 processData : false,
1153 cache : false,
1151 cache : false,
1154 type : "GET",
1152 type : "GET",
1155 dataType : "json",
1153 dataType : "json",
1156 success : function (data, status, xhr) {
1154 success : function (data, status, xhr) {
1157 that.notebook_loaded(data, status, xhr);
1155 that.notebook_loaded(data, status, xhr);
1158 if (callback !== undefined) {
1156 if (callback !== undefined) {
1159 callback();
1157 callback();
1160 };
1158 };
1161 }
1159 }
1162 };
1160 };
1163 IPython.save_widget.status_loading();
1161 IPython.save_widget.status_loading();
1164 var url = $('body').data('baseProjectUrl') + 'notebooks/' + notebook_id;
1162 var url = $('body').data('baseProjectUrl') + 'notebooks/' + notebook_id;
1165 $.ajax(url, settings);
1163 $.ajax(url, settings);
1166 };
1164 };
1167
1165
1168
1166
1169 Notebook.prototype.notebook_loaded = function (data, status, xhr) {
1167 Notebook.prototype.notebook_loaded = function (data, status, xhr) {
1170 var allowed = xhr.getResponseHeader('Allow');
1168 var allowed = xhr.getResponseHeader('Allow');
1171 this.fromJSON(data);
1169 this.fromJSON(data);
1172 if (this.ncells() === 0) {
1170 if (this.ncells() === 0) {
1173 this.insert_code_cell_below();
1171 this.insert_code_cell_below();
1174 };
1172 };
1175 IPython.save_widget.status_last_saved();
1173 IPython.save_widget.status_last_saved();
1176 IPython.save_widget.set_notebook_name(data.metadata.name);
1174 IPython.save_widget.set_notebook_name(data.metadata.name);
1177 this.dirty = false;
1175 this.dirty = false;
1178 if (! this.read_only) {
1176 if (! this.read_only) {
1179 this.start_kernel();
1177 this.start_kernel();
1180 }
1178 }
1181 // fromJSON always selects the last cell inserted. We need to wait
1179 // fromJSON always selects the last cell inserted. We need to wait
1182 // until that is done before scrolling to the top.
1180 // until that is done before scrolling to the top.
1183 setTimeout(function () {
1181 setTimeout(function () {
1184 IPython.notebook.select(0);
1182 IPython.notebook.select(0);
1185 IPython.notebook.scroll_to_top();
1183 IPython.notebook.scroll_to_top();
1186 }, 50);
1184 }, 50);
1187 };
1185 };
1188
1186
1189 IPython.Notebook = Notebook;
1187 IPython.Notebook = Notebook;
1190
1188
1191
1189
1192 return IPython;
1190 return IPython;
1193
1191
1194 }(IPython));
1192 }(IPython));
1195
1193
General Comments 0
You need to be logged in to leave comments. Login now