##// END OF EJS Templates
past below by default
Matthias BUSSONNIER -
Show More
@@ -1,179 +1,179 b''
1 1 //----------------------------------------------------------------------------
2 2 // Copyright (C) 2011 The IPython Development Team
3 3 //
4 4 // Distributed under the terms of the BSD License. The full license is in
5 5 // the file COPYING, distributed as part of this software.
6 6 //----------------------------------------------------------------------------
7 7
8 8 //============================================================================
9 9 // ToolBar
10 10 //============================================================================
11 11
12 12 var IPython = (function (IPython) {
13 13
14 14 var MainToolBar = function (selector) {
15 15 this.selector = selector;
16 16 IPython.ToolBar.apply(this, arguments);
17 17 this.construct();
18 18 this.add_drop_down_list();
19 19 this.bind_events();
20 20 };
21 21
22 22 MainToolBar.prototype = new IPython.ToolBar();
23 23
24 24 MainToolBar.prototype.construct = function () {
25 25 this.add_buttons_group([
26 26 {
27 27 id : 'save_b',
28 28 label : 'Save',
29 29 icon : 'ui-icon-disk',
30 30 callback : function () {
31 31 IPython.notebook.save_notebook();
32 32 }
33 33 }
34 34 ]);
35 35 this.add_buttons_group([
36 36 {
37 37 id : 'cut_b',
38 38 label : 'Cut Cell',
39 39 icon : 'ui-icon-scissors',
40 40 callback : function () {
41 41 IPython.notebook.cut_cell();
42 42 }
43 43 },
44 44 {
45 45 id : 'copy_b',
46 46 label : 'Copy Cell',
47 47 icon : 'ui-icon-copy',
48 48 callback : function () {
49 49 IPython.notebook.copy_cell();
50 50 }
51 51 },
52 52 {
53 53 id : 'paste_b',
54 label : 'Paste Cell Above',
54 label : 'Paste Cell Below',
55 55 icon : 'ui-icon-clipboard',
56 56 callback : function () {
57 IPython.notebook.paste_cell_above();
57 IPython.notebook.paste_cell_below();
58 58 }
59 59 }
60 60 ],'cut_copy_paste');
61 61
62 62 this.add_buttons_group([
63 63 {
64 64 id : 'move_up_b',
65 65 label : 'Move Cell Up',
66 66 icon : 'ui-icon-arrowthick-1-n',
67 67 callback : function () {
68 68 IPython.notebook.move_cell_up();
69 69 }
70 70 },
71 71 {
72 72 id : 'move_down_b',
73 73 label : 'Move Cell Down',
74 74 icon : 'ui-icon-arrowthick-1-s',
75 75 callback : function () {
76 76 IPython.notebook.move_cell_down();
77 77 }
78 78 }
79 79 ],'move_up_down');
80 80
81 81 this.add_buttons_group([
82 82 {
83 83 id : 'insert_above_b',
84 84 label : 'Insert Cell Above',
85 85 icon : 'ui-icon-arrowthickstop-1-n',
86 86 callback : function () {
87 87 IPython.notebook.insert_cell_above('code');
88 88 }
89 89 },
90 90 {
91 91 id : 'insert_below_b',
92 92 label : 'Insert Cell Below',
93 93 icon : 'ui-icon-arrowthickstop-1-s',
94 94 callback : function () {
95 95 IPython.notebook.insert_cell_below('code');
96 96 }
97 97 }
98 98 ],'insert_above_below');
99 99
100 100 this.add_buttons_group([
101 101 {
102 102 id : 'run_b',
103 103 label : 'Run Cell',
104 104 icon : 'ui-icon-play',
105 105 callback : function () {
106 106 IPython.notebook.execute_selected_cell();
107 107 }
108 108 },
109 109 {
110 110 id : 'interrupt_b',
111 111 label : 'Interrupt',
112 112 icon : 'ui-icon-stop',
113 113 callback : function () {
114 114 IPython.notebook.kernel.interrupt();
115 115 }
116 116 }
117 117 ],'run_int');
118 118
119 119
120 120 };
121 121
122 122 MainToolBar.prototype.add_drop_down_list = function () {
123 123 var select = $(this.selector)
124 124 .append($('<select/>')
125 125 .attr('id','cell_type')
126 126 .addClass('ui-widget ui-widget-content')
127 127 .append($('<option/>').attr('value','code').text('Code'))
128 128 .append($('<option/>').attr('value','markdown').text('Markdown'))
129 129 .append($('<option/>').attr('value','raw').text('Raw Text'))
130 130 .append($('<option/>').attr('value','heading1').text('Heading 1'))
131 131 .append($('<option/>').attr('value','heading2').text('Heading 2'))
132 132 .append($('<option/>').attr('value','heading3').text('Heading 3'))
133 133 .append($('<option/>').attr('value','heading4').text('Heading 4'))
134 134 .append($('<option/>').attr('value','heading5').text('Heading 5'))
135 135 .append($('<option/>').attr('value','heading6').text('Heading 6'))
136 136 .append($('<option/>').attr('value','heading7').text('Heading 7'))
137 137 .append($('<option/>').attr('value','heading8').text('Heading 8'))
138 138 );
139 139 };
140 140
141 141 MainToolBar.prototype.bind_events = function () {
142 142 var that = this;
143 143
144 144 this.element.find('#cell_type').change(function () {
145 145 var cell_type = $(this).val();
146 146 if (cell_type === 'code') {
147 147 IPython.notebook.to_code();
148 148 } else if (cell_type === 'markdown') {
149 149 IPython.notebook.to_markdown();
150 150 } else if (cell_type === 'raw') {
151 151 IPython.notebook.to_raw();
152 152 } else if (cell_type === 'heading1') {
153 153 IPython.notebook.to_heading(undefined, 1);
154 154 } else if (cell_type === 'heading2') {
155 155 IPython.notebook.to_heading(undefined, 2);
156 156 } else if (cell_type === 'heading3') {
157 157 IPython.notebook.to_heading(undefined, 3);
158 158 } else if (cell_type === 'heading4') {
159 159 IPython.notebook.to_heading(undefined, 4);
160 160 } else if (cell_type === 'heading5') {
161 161 IPython.notebook.to_heading(undefined, 5);
162 162 } else if (cell_type === 'heading6') {
163 163 IPython.notebook.to_heading(undefined, 6);
164 164 }
165 165 });
166 166 $([IPython.events]).on('selected_cell_type_changed.Notebook', function (event, data) {
167 167 if (data.cell_type === 'heading') {
168 168 that.element.find('#cell_type').val(data.cell_type+data.level);
169 169 } else {
170 170 that.element.find('#cell_type').val(data.cell_type);
171 171 }
172 172 });
173 173 };
174 174
175 175 IPython.MainToolBar = MainToolBar;
176 176
177 177 return IPython;
178 178
179 179 }(IPython));
@@ -1,1371 +1,1371 b''
1 1 //----------------------------------------------------------------------------
2 2 // Copyright (C) 2008-2011 The IPython Development Team
3 3 //
4 4 // Distributed under the terms of the BSD License. The full license is in
5 5 // the file COPYING, distributed as part of this software.
6 6 //----------------------------------------------------------------------------
7 7
8 8 //============================================================================
9 9 // Notebook
10 10 //============================================================================
11 11
12 12 var IPython = (function (IPython) {
13 13
14 14 var utils = IPython.utils;
15 15 var key = IPython.utils.keycodes;
16 16
17 17 var Notebook = function (selector) {
18 18 this.read_only = IPython.read_only;
19 19 this.element = $(selector);
20 20 this.element.scroll();
21 21 this.element.data("notebook", this);
22 22 this.next_prompt_number = 1;
23 23 this.kernel = null;
24 24 this.clipboard = null;
25 25 this.undelete_backup = null;
26 26 this.undelete_index = null;
27 27 this.undelete_below = false;
28 28 this.paste_enabled = false;
29 29 this.dirty = false;
30 30 this.metadata = {};
31 31 // single worksheet for now
32 32 this.worksheet_metadata = {};
33 33 this.control_key_active = false;
34 34 this.notebook_id = null;
35 35 this.notebook_name = null;
36 36 this.notebook_name_blacklist_re = /[\/\\:]/;
37 37 this.nbformat = 3 // Increment this when changing the nbformat
38 38 this.nbformat_minor = 0 // Increment this when changing the nbformat
39 39 this.style();
40 40 this.create_elements();
41 41 this.bind_events();
42 42 };
43 43
44 44
45 45 Notebook.prototype.style = function () {
46 46 $('div#notebook').addClass('border-box-sizing');
47 47 };
48 48
49 49
50 50 Notebook.prototype.create_elements = function () {
51 51 // We add this end_space div to the end of the notebook div to:
52 52 // i) provide a margin between the last cell and the end of the notebook
53 53 // ii) to prevent the div from scrolling up when the last cell is being
54 54 // edited, but is too low on the page, which browsers will do automatically.
55 55 var that = this;
56 56 var end_space = $('<div/>').addClass('end_space').height("30%");
57 57 end_space.dblclick(function (e) {
58 58 if (that.read_only) return;
59 59 var ncells = that.ncells();
60 60 that.insert_cell_below('code',ncells-1);
61 61 });
62 62 this.element.append(end_space);
63 63 $('div#notebook').addClass('border-box-sizing');
64 64 };
65 65
66 66
67 67 Notebook.prototype.bind_events = function () {
68 68 var that = this;
69 69
70 70 $([IPython.events]).on('set_next_input.Notebook', function (event, data) {
71 71 var index = that.find_cell_index(data.cell);
72 72 var new_cell = that.insert_cell_below('code',index);
73 73 new_cell.set_text(data.text);
74 74 that.dirty = true;
75 75 });
76 76
77 77 $([IPython.events]).on('set_dirty.Notebook', function (event, data) {
78 78 that.dirty = data.value;
79 79 });
80 80
81 81 $([IPython.events]).on('select.Cell', function (event, data) {
82 82 var index = that.find_cell_index(data.cell);
83 83 that.select(index);
84 84 });
85 85
86 86
87 87 $(document).keydown(function (event) {
88 88 // console.log(event);
89 89 if (that.read_only) return true;
90 90
91 91 // Save (CTRL+S) or (AppleKey+S)
92 92 //metaKey = applekey on mac
93 93 if ((event.ctrlKey || event.metaKey) && event.keyCode==83) {
94 94 that.save_notebook();
95 95 event.preventDefault();
96 96 return false;
97 97 } else if (event.which === key.ESC) {
98 98 // Intercept escape at highest level to avoid closing
99 99 // websocket connection with firefox
100 100 event.preventDefault();
101 101 } else if (event.which === key.SHIFT) {
102 102 // ignore shift keydown
103 103 return true;
104 104 }
105 105 if (event.which === key.UPARROW && !event.shiftKey) {
106 106 var cell = that.get_selected_cell();
107 107 if (cell.at_top()) {
108 108 event.preventDefault();
109 109 that.select_prev();
110 110 };
111 111 } else if (event.which === key.DOWNARROW && !event.shiftKey) {
112 112 var cell = that.get_selected_cell();
113 113 if (cell.at_bottom()) {
114 114 event.preventDefault();
115 115 that.select_next();
116 116 };
117 117 } else if (event.which === key.ENTER && event.shiftKey) {
118 118 that.execute_selected_cell();
119 119 return false;
120 120 } else if (event.which === key.ENTER && event.altKey) {
121 121 // Execute code cell, and insert new in place
122 122 that.execute_selected_cell();
123 123 // Only insert a new cell, if we ended up in an already populated cell
124 124 if (/\S/.test(that.get_selected_cell().get_text()) == true) {
125 125 that.insert_cell_above('code');
126 126 }
127 127 return false;
128 128 } else if (event.which === key.ENTER && event.ctrlKey) {
129 129 that.execute_selected_cell({terminal:true});
130 130 return false;
131 131 } else if (event.which === 77 && event.ctrlKey && that.control_key_active == false) {
132 132 that.control_key_active = true;
133 133 return false;
134 134 } else if (event.which === 88 && that.control_key_active) {
135 135 // Cut selected cell = x
136 136 that.cut_cell();
137 137 that.control_key_active = false;
138 138 return false;
139 139 } else if (event.which === 67 && that.control_key_active) {
140 140 // Copy selected cell = c
141 141 that.copy_cell();
142 142 that.control_key_active = false;
143 143 return false;
144 144 } else if (event.which === 86 && that.control_key_active) {
145 // Paste above selected cell = v
146 that.paste_cell_above();
145 // Paste below selected cell = v
146 that.paste_cell_below();
147 147 that.control_key_active = false;
148 148 return false;
149 149 } else if (event.which === 68 && that.control_key_active) {
150 150 // Delete selected cell = d
151 151 that.delete_cell();
152 152 that.control_key_active = false;
153 153 return false;
154 154 } else if (event.which === 65 && that.control_key_active) {
155 155 // Insert code cell above selected = a
156 156 that.insert_cell_above('code');
157 157 that.control_key_active = false;
158 158 return false;
159 159 } else if (event.which === 66 && that.control_key_active) {
160 160 // Insert code cell below selected = b
161 161 that.insert_cell_below('code');
162 162 that.control_key_active = false;
163 163 return false;
164 164 } else if (event.which === 89 && that.control_key_active) {
165 165 // To code = y
166 166 that.to_code();
167 167 that.control_key_active = false;
168 168 return false;
169 169 } else if (event.which === 77 && that.control_key_active) {
170 170 // To markdown = m
171 171 that.to_markdown();
172 172 that.control_key_active = false;
173 173 return false;
174 174 } else if (event.which === 84 && that.control_key_active) {
175 175 // To Raw = t
176 176 that.to_raw();
177 177 that.control_key_active = false;
178 178 return false;
179 179 } else if (event.which === 49 && that.control_key_active) {
180 180 // To Heading 1 = 1
181 181 that.to_heading(undefined, 1);
182 182 that.control_key_active = false;
183 183 return false;
184 184 } else if (event.which === 50 && that.control_key_active) {
185 185 // To Heading 2 = 2
186 186 that.to_heading(undefined, 2);
187 187 that.control_key_active = false;
188 188 return false;
189 189 } else if (event.which === 51 && that.control_key_active) {
190 190 // To Heading 3 = 3
191 191 that.to_heading(undefined, 3);
192 192 that.control_key_active = false;
193 193 return false;
194 194 } else if (event.which === 52 && that.control_key_active) {
195 195 // To Heading 4 = 4
196 196 that.to_heading(undefined, 4);
197 197 that.control_key_active = false;
198 198 return false;
199 199 } else if (event.which === 53 && that.control_key_active) {
200 200 // To Heading 5 = 5
201 201 that.to_heading(undefined, 5);
202 202 that.control_key_active = false;
203 203 return false;
204 204 } else if (event.which === 54 && that.control_key_active) {
205 205 // To Heading 6 = 6
206 206 that.to_heading(undefined, 6);
207 207 that.control_key_active = false;
208 208 return false;
209 209 } else if (event.which === 79 && that.control_key_active) {
210 210 // Toggle output = o
211 211 if (event.shiftKey){
212 212 that.toggle_output_scroll();
213 213 } else {
214 214 that.toggle_output();
215 215 }
216 216 that.control_key_active = false;
217 217 return false;
218 218 } else if (event.which === 83 && that.control_key_active) {
219 219 // Save notebook = s
220 220 that.save_notebook();
221 221 that.control_key_active = false;
222 222 return false;
223 223 } else if (event.which === 74 && that.control_key_active) {
224 224 // Move cell down = j
225 225 that.move_cell_down();
226 226 that.control_key_active = false;
227 227 return false;
228 228 } else if (event.which === 75 && that.control_key_active) {
229 229 // Move cell up = k
230 230 that.move_cell_up();
231 231 that.control_key_active = false;
232 232 return false;
233 233 } else if (event.which === 80 && that.control_key_active) {
234 234 // Select previous = p
235 235 that.select_prev();
236 236 that.control_key_active = false;
237 237 return false;
238 238 } else if (event.which === 78 && that.control_key_active) {
239 239 // Select next = n
240 240 that.select_next();
241 241 that.control_key_active = false;
242 242 return false;
243 243 } else if (event.which === 76 && that.control_key_active) {
244 244 // Toggle line numbers = l
245 245 that.cell_toggle_line_numbers();
246 246 that.control_key_active = false;
247 247 return false;
248 248 } else if (event.which === 73 && that.control_key_active) {
249 249 // Interrupt kernel = i
250 250 that.kernel.interrupt();
251 251 that.control_key_active = false;
252 252 return false;
253 253 } else if (event.which === 190 && that.control_key_active) {
254 254 // Restart kernel = . # matches qt console
255 255 that.restart_kernel();
256 256 that.control_key_active = false;
257 257 return false;
258 258 } else if (event.which === 72 && that.control_key_active) {
259 259 // Show keyboard shortcuts = h
260 260 IPython.quick_help.show_keyboard_shortcuts();
261 261 that.control_key_active = false;
262 262 return false;
263 263 } else if (event.which === 90 && that.control_key_active) {
264 264 // Undo last cell delete = z
265 265 that.undelete();
266 266 that.control_key_active = false;
267 267 return false;
268 268 } else if (that.control_key_active) {
269 269 that.control_key_active = false;
270 270 return true;
271 271 };
272 272 return true;
273 273 });
274 274
275 275 var collapse_time = function(time){
276 276 var app_height = $('div#main_app').height(); // content height
277 277 var splitter_height = $('div#pager_splitter').outerHeight(true);
278 278 var new_height = app_height - splitter_height;
279 279 that.element.animate({height : new_height + 'px'}, time);
280 280 }
281 281
282 282 this.element.bind('collapse_pager', function (event,extrap) {
283 283 time = (extrap != undefined) ? ((extrap.duration != undefined ) ? extrap.duration : 'fast') : 'fast';
284 284 collapse_time(time);
285 285 });
286 286
287 287 var expand_time = function(time) {
288 288 var app_height = $('div#main_app').height(); // content height
289 289 var splitter_height = $('div#pager_splitter').outerHeight(true);
290 290 var pager_height = $('div#pager').outerHeight(true);
291 291 var new_height = app_height - pager_height - splitter_height;
292 292 that.element.animate({height : new_height + 'px'}, time);
293 293 }
294 294
295 295 this.element.bind('expand_pager', function (event, extrap) {
296 296 time = (extrap != undefined) ? ((extrap.duration != undefined ) ? extrap.duration : 'fast') : 'fast';
297 297 expand_time(time);
298 298 });
299 299
300 300 $(window).bind('beforeunload', function () {
301 301 // TODO: Make killing the kernel configurable.
302 302 var kill_kernel = false;
303 303 if (kill_kernel) {
304 304 that.kernel.kill();
305 305 }
306 306 if (that.dirty && ! that.read_only) {
307 307 return "You have unsaved changes that will be lost if you leave this page.";
308 308 };
309 309 // Null is the *only* return value that will make the browser not
310 310 // pop up the "don't leave" dialog.
311 311 return null;
312 312 });
313 313 };
314 314
315 315 Notebook.prototype.scroll_to_cell = function (cell_number, time) {
316 316 var cells = this.get_cells();
317 317 var time = time || 0;
318 318 cell_number = Math.min(cells.length-1,cell_number);
319 319 cell_number = Math.max(0 ,cell_number);
320 320 scroll_value = cells[cell_number].element.position().top-cells[0].element.position().top ;
321 321 this.element.animate({scrollTop:scroll_value}, time);
322 322 return scroll_value;
323 323 };
324 324
325 325
326 326 Notebook.prototype.scroll_to_bottom = function () {
327 327 this.element.animate({scrollTop:this.element.get(0).scrollHeight}, 0);
328 328 };
329 329
330 330
331 331 Notebook.prototype.scroll_to_top = function () {
332 332 this.element.animate({scrollTop:0}, 0);
333 333 };
334 334
335 335
336 336 // Cell indexing, retrieval, etc.
337 337
338 338 Notebook.prototype.get_cell_elements = function () {
339 339 return this.element.children("div.cell");
340 340 };
341 341
342 342
343 343 Notebook.prototype.get_cell_element = function (index) {
344 344 var result = null;
345 345 var e = this.get_cell_elements().eq(index);
346 346 if (e.length !== 0) {
347 347 result = e;
348 348 }
349 349 return result;
350 350 };
351 351
352 352
353 353 Notebook.prototype.ncells = function (cell) {
354 354 return this.get_cell_elements().length;
355 355 };
356 356
357 357
358 358 // TODO: we are often calling cells as cells()[i], which we should optimize
359 359 // to cells(i) or a new method.
360 360 Notebook.prototype.get_cells = function () {
361 361 return this.get_cell_elements().toArray().map(function (e) {
362 362 return $(e).data("cell");
363 363 });
364 364 };
365 365
366 366
367 367 Notebook.prototype.get_cell = function (index) {
368 368 var result = null;
369 369 var ce = this.get_cell_element(index);
370 370 if (ce !== null) {
371 371 result = ce.data('cell');
372 372 }
373 373 return result;
374 374 }
375 375
376 376
377 377 Notebook.prototype.get_next_cell = function (cell) {
378 378 var result = null;
379 379 var index = this.find_cell_index(cell);
380 380 if (index !== null && index < this.ncells()) {
381 381 result = this.get_cell(index+1);
382 382 }
383 383 return result;
384 384 }
385 385
386 386
387 387 Notebook.prototype.get_prev_cell = function (cell) {
388 388 var result = null;
389 389 var index = this.find_cell_index(cell);
390 390 if (index !== null && index > 1) {
391 391 result = this.get_cell(index-1);
392 392 }
393 393 return result;
394 394 }
395 395
396 396 Notebook.prototype.find_cell_index = function (cell) {
397 397 var result = null;
398 398 this.get_cell_elements().filter(function (index) {
399 399 if ($(this).data("cell") === cell) {
400 400 result = index;
401 401 };
402 402 });
403 403 return result;
404 404 };
405 405
406 406
407 407 Notebook.prototype.index_or_selected = function (index) {
408 408 var i;
409 409 if (index === undefined || index === null) {
410 410 i = this.get_selected_index();
411 411 if (i === null) {
412 412 i = 0;
413 413 }
414 414 } else {
415 415 i = index;
416 416 }
417 417 return i;
418 418 };
419 419
420 420
421 421 Notebook.prototype.get_selected_cell = function () {
422 422 var index = this.get_selected_index();
423 423 return this.get_cell(index);
424 424 };
425 425
426 426
427 427 Notebook.prototype.is_valid_cell_index = function (index) {
428 428 if (index !== null && index >= 0 && index < this.ncells()) {
429 429 return true;
430 430 } else {
431 431 return false;
432 432 };
433 433 }
434 434
435 435 Notebook.prototype.get_selected_index = function () {
436 436 var result = null;
437 437 this.get_cell_elements().filter(function (index) {
438 438 if ($(this).data("cell").selected === true) {
439 439 result = index;
440 440 };
441 441 });
442 442 return result;
443 443 };
444 444
445 445
446 446 // Cell selection.
447 447
448 448 Notebook.prototype.select = function (index) {
449 449 if (index !== undefined && index >= 0 && index < this.ncells()) {
450 450 sindex = this.get_selected_index()
451 451 if (sindex !== null && index !== sindex) {
452 452 this.get_cell(sindex).unselect();
453 453 };
454 454 var cell = this.get_cell(index)
455 455 cell.select();
456 456 if (cell.cell_type === 'heading') {
457 457 $([IPython.events]).trigger('selected_cell_type_changed.Notebook',
458 458 {'cell_type':cell.cell_type,level:cell.level}
459 459 );
460 460 } else {
461 461 $([IPython.events]).trigger('selected_cell_type_changed.Notebook',
462 462 {'cell_type':cell.cell_type}
463 463 );
464 464 };
465 465 };
466 466 return this;
467 467 };
468 468
469 469
470 470 Notebook.prototype.select_next = function () {
471 471 var index = this.get_selected_index();
472 472 if (index !== null && index >= 0 && (index+1) < this.ncells()) {
473 473 this.select(index+1);
474 474 };
475 475 return this;
476 476 };
477 477
478 478
479 479 Notebook.prototype.select_prev = function () {
480 480 var index = this.get_selected_index();
481 481 if (index !== null && index >= 0 && (index-1) < this.ncells()) {
482 482 this.select(index-1);
483 483 };
484 484 return this;
485 485 };
486 486
487 487
488 488 // Cell movement
489 489
490 490 Notebook.prototype.move_cell_up = function (index) {
491 491 var i = this.index_or_selected();
492 492 if (i !== null && i < this.ncells() && i > 0) {
493 493 var pivot = this.get_cell_element(i-1);
494 494 var tomove = this.get_cell_element(i);
495 495 if (pivot !== null && tomove !== null) {
496 496 tomove.detach();
497 497 pivot.before(tomove);
498 498 this.select(i-1);
499 499 };
500 500 };
501 501 this.dirty = true;
502 502 return this;
503 503 };
504 504
505 505
506 506 Notebook.prototype.move_cell_down = function (index) {
507 507 var i = this.index_or_selected();
508 508 if (i !== null && i < (this.ncells()-1) && i >= 0) {
509 509 var pivot = this.get_cell_element(i+1);
510 510 var tomove = this.get_cell_element(i);
511 511 if (pivot !== null && tomove !== null) {
512 512 tomove.detach();
513 513 pivot.after(tomove);
514 514 this.select(i+1);
515 515 };
516 516 };
517 517 this.dirty = true;
518 518 return this;
519 519 };
520 520
521 521
522 522 Notebook.prototype.sort_cells = function () {
523 523 // This is not working right now. Calling this will actually crash
524 524 // the browser. I think there is an infinite loop in here...
525 525 var ncells = this.ncells();
526 526 var sindex = this.get_selected_index();
527 527 var swapped;
528 528 do {
529 529 swapped = false;
530 530 for (var i=1; i<ncells; i++) {
531 531 current = this.get_cell(i);
532 532 previous = this.get_cell(i-1);
533 533 if (previous.input_prompt_number > current.input_prompt_number) {
534 534 this.move_cell_up(i);
535 535 swapped = true;
536 536 };
537 537 };
538 538 } while (swapped);
539 539 this.select(sindex);
540 540 return this;
541 541 };
542 542
543 543 // Insertion, deletion.
544 544
545 545 Notebook.prototype.delete_cell = function (index) {
546 546 var i = this.index_or_selected(index);
547 547 var cell = this.get_selected_cell();
548 548 this.undelete_backup = cell.toJSON();
549 549 if (this.is_valid_cell_index(i)) {
550 550 var ce = this.get_cell_element(i);
551 551 ce.remove();
552 552 if (i === (this.ncells())) {
553 553 this.select(i-1);
554 554 this.undelete_index = i - 1;
555 555 this.undelete_below = true;
556 556 } else {
557 557 this.select(i);
558 558 this.undelete_index = i;
559 559 this.undelete_below = false;
560 560 };
561 561 this.dirty = true;
562 562 };
563 563 return this;
564 564 };
565 565
566 566
567 567 Notebook.prototype.insert_cell_at_bottom = function (type){
568 568 var len = this.ncells();
569 569 return this.insert_cell_below(type,len-1);
570 570 }
571 571
572 572 Notebook.prototype.insert_cell_below = function (type, index) {
573 573 // type = ('code','html','markdown')
574 574 // index = cell index or undefined to insert below selected
575 575 index = this.index_or_selected(index);
576 576 var cell = null;
577 577 // This is intentionally < rather than <= for the sake of more
578 578 // sensible behavior in some cases.
579 579 if (this.undelete_index !== null && index < this.undelete_index) {
580 580 this.undelete_index = this.undelete_index + 1;
581 581 }
582 582 if (this.ncells() === 0 || this.is_valid_cell_index(index)) {
583 583 if (type === 'code') {
584 584 cell = new IPython.CodeCell(this.kernel);
585 585 cell.set_input_prompt();
586 586 } else if (type === 'markdown') {
587 587 cell = new IPython.MarkdownCell();
588 588 } else if (type === 'html') {
589 589 cell = new IPython.HTMLCell();
590 590 } else if (type === 'raw') {
591 591 cell = new IPython.RawCell();
592 592 } else if (type === 'heading') {
593 593 cell = new IPython.HeadingCell();
594 594 };
595 595 if (cell !== null) {
596 596 if (this.ncells() === 0) {
597 597 this.element.find('div.end_space').before(cell.element);
598 598 } else if (this.is_valid_cell_index(index)) {
599 599 this.get_cell_element(index).after(cell.element);
600 600 };
601 601 cell.render();
602 602 this.select(this.find_cell_index(cell));
603 603 this.dirty = true;
604 604 return cell;
605 605 };
606 606 };
607 607 return cell;
608 608 };
609 609
610 610
611 611 Notebook.prototype.insert_cell_above = function (type, index) {
612 612 // type = ('code','html','markdown')
613 613 // index = cell index or undefined to insert above selected
614 614 index = this.index_or_selected(index);
615 615 var cell = null;
616 616 if (this.undelete_index !== null && index <= this.undelete_index) {
617 617 this.undelete_index = this.undelete_index + 1;
618 618 }
619 619 if (this.ncells() === 0 || this.is_valid_cell_index(index)) {
620 620 if (type === 'code') {
621 621 cell = new IPython.CodeCell(this.kernel);
622 622 cell.set_input_prompt();
623 623 } else if (type === 'markdown') {
624 624 cell = new IPython.MarkdownCell();
625 625 } else if (type === 'html') {
626 626 cell = new IPython.HTMLCell();
627 627 } else if (type === 'raw') {
628 628 cell = new IPython.RawCell();
629 629 } else if (type === 'heading') {
630 630 cell = new IPython.HeadingCell();
631 631 };
632 632 if (cell !== null) {
633 633 if (this.ncells() === 0) {
634 634 this.element.find('div.end_space').before(cell.element);
635 635 } else if (this.is_valid_cell_index(index)) {
636 636 this.get_cell_element(index).before(cell.element);
637 637 };
638 638 cell.render();
639 639 this.select(this.find_cell_index(cell));
640 640 this.dirty = true;
641 641 return cell;
642 642 };
643 643 };
644 644 return cell;
645 645 };
646 646
647 647
648 648 Notebook.prototype.to_code = function (index) {
649 649 var i = this.index_or_selected(index);
650 650 if (this.is_valid_cell_index(i)) {
651 651 var source_element = this.get_cell_element(i);
652 652 var source_cell = source_element.data("cell");
653 653 if (!(source_cell instanceof IPython.CodeCell)) {
654 654 target_cell = this.insert_cell_below('code',i);
655 655 var text = source_cell.get_text();
656 656 if (text === source_cell.placeholder) {
657 657 text = '';
658 658 }
659 659 target_cell.set_text(text);
660 660 // make this value the starting point, so that we can only undo
661 661 // to this state, instead of a blank cell
662 662 target_cell.code_mirror.clearHistory();
663 663 source_element.remove();
664 664 this.dirty = true;
665 665 };
666 666 };
667 667 };
668 668
669 669
670 670 Notebook.prototype.to_markdown = function (index) {
671 671 var i = this.index_or_selected(index);
672 672 if (this.is_valid_cell_index(i)) {
673 673 var source_element = this.get_cell_element(i);
674 674 var source_cell = source_element.data("cell");
675 675 if (!(source_cell instanceof IPython.MarkdownCell)) {
676 676 target_cell = this.insert_cell_below('markdown',i);
677 677 var text = source_cell.get_text();
678 678 if (text === source_cell.placeholder) {
679 679 text = '';
680 680 };
681 681 // The edit must come before the set_text.
682 682 target_cell.edit();
683 683 target_cell.set_text(text);
684 684 // make this value the starting point, so that we can only undo
685 685 // to this state, instead of a blank cell
686 686 target_cell.code_mirror.clearHistory();
687 687 source_element.remove();
688 688 this.dirty = true;
689 689 };
690 690 };
691 691 };
692 692
693 693
694 694 Notebook.prototype.to_html = function (index) {
695 695 var i = this.index_or_selected(index);
696 696 if (this.is_valid_cell_index(i)) {
697 697 var source_element = this.get_cell_element(i);
698 698 var source_cell = source_element.data("cell");
699 699 var target_cell = null;
700 700 if (!(source_cell instanceof IPython.HTMLCell)) {
701 701 target_cell = this.insert_cell_below('html',i);
702 702 var text = source_cell.get_text();
703 703 if (text === source_cell.placeholder) {
704 704 text = '';
705 705 };
706 706 // The edit must come before the set_text.
707 707 target_cell.edit();
708 708 target_cell.set_text(text);
709 709 // make this value the starting point, so that we can only undo
710 710 // to this state, instead of a blank cell
711 711 target_cell.code_mirror.clearHistory();
712 712 source_element.remove();
713 713 this.dirty = true;
714 714 };
715 715 };
716 716 };
717 717
718 718
719 719 Notebook.prototype.to_raw = function (index) {
720 720 var i = this.index_or_selected(index);
721 721 if (this.is_valid_cell_index(i)) {
722 722 var source_element = this.get_cell_element(i);
723 723 var source_cell = source_element.data("cell");
724 724 var target_cell = null;
725 725 if (!(source_cell instanceof IPython.RawCell)) {
726 726 target_cell = this.insert_cell_below('raw',i);
727 727 var text = source_cell.get_text();
728 728 if (text === source_cell.placeholder) {
729 729 text = '';
730 730 };
731 731 // The edit must come before the set_text.
732 732 target_cell.edit();
733 733 target_cell.set_text(text);
734 734 // make this value the starting point, so that we can only undo
735 735 // to this state, instead of a blank cell
736 736 target_cell.code_mirror.clearHistory();
737 737 source_element.remove();
738 738 this.dirty = true;
739 739 };
740 740 };
741 741 };
742 742
743 743
744 744 Notebook.prototype.to_heading = function (index, level) {
745 745 level = level || 1;
746 746 var i = this.index_or_selected(index);
747 747 if (this.is_valid_cell_index(i)) {
748 748 var source_element = this.get_cell_element(i);
749 749 var source_cell = source_element.data("cell");
750 750 var target_cell = null;
751 751 if (source_cell instanceof IPython.HeadingCell) {
752 752 source_cell.set_level(level);
753 753 } else {
754 754 target_cell = this.insert_cell_below('heading',i);
755 755 var text = source_cell.get_text();
756 756 if (text === source_cell.placeholder) {
757 757 text = '';
758 758 };
759 759 // The edit must come before the set_text.
760 760 target_cell.set_level(level);
761 761 target_cell.edit();
762 762 target_cell.set_text(text);
763 763 // make this value the starting point, so that we can only undo
764 764 // to this state, instead of a blank cell
765 765 target_cell.code_mirror.clearHistory();
766 766 source_element.remove();
767 767 this.dirty = true;
768 768 };
769 769 $([IPython.events]).trigger('selected_cell_type_changed.Notebook',
770 770 {'cell_type':'heading',level:level}
771 771 );
772 772 };
773 773 };
774 774
775 775
776 776 // Cut/Copy/Paste
777 777
778 778 Notebook.prototype.enable_paste = function () {
779 779 var that = this;
780 780 if (!this.paste_enabled) {
781 781 $('#paste_cell_replace').removeClass('ui-state-disabled')
782 782 .on('click', function () {that.paste_cell_replace();});
783 783 $('#paste_cell_above').removeClass('ui-state-disabled')
784 784 .on('click', function () {that.paste_cell_above();});
785 785 $('#paste_cell_below').removeClass('ui-state-disabled')
786 786 .on('click', function () {that.paste_cell_below();});
787 787 this.paste_enabled = true;
788 788 };
789 789 };
790 790
791 791
792 792 Notebook.prototype.disable_paste = function () {
793 793 if (this.paste_enabled) {
794 794 $('#paste_cell_replace').addClass('ui-state-disabled').off('click');
795 795 $('#paste_cell_above').addClass('ui-state-disabled').off('click');
796 796 $('#paste_cell_below').addClass('ui-state-disabled').off('click');
797 797 this.paste_enabled = false;
798 798 };
799 799 };
800 800
801 801
802 802 Notebook.prototype.cut_cell = function () {
803 803 this.copy_cell();
804 804 this.delete_cell();
805 805 }
806 806
807 807 Notebook.prototype.copy_cell = function () {
808 808 var cell = this.get_selected_cell();
809 809 this.clipboard = cell.toJSON();
810 810 this.enable_paste();
811 811 };
812 812
813 813
814 814 Notebook.prototype.paste_cell_replace = function () {
815 815 if (this.clipboard !== null && this.paste_enabled) {
816 816 var cell_data = this.clipboard;
817 817 var new_cell = this.insert_cell_above(cell_data.cell_type);
818 818 new_cell.fromJSON(cell_data);
819 819 old_cell = this.get_next_cell(new_cell);
820 820 this.delete_cell(this.find_cell_index(old_cell));
821 821 this.select(this.find_cell_index(new_cell));
822 822 };
823 823 };
824 824
825 825
826 826 Notebook.prototype.paste_cell_above = function () {
827 827 if (this.clipboard !== null && this.paste_enabled) {
828 828 var cell_data = this.clipboard;
829 829 var new_cell = this.insert_cell_above(cell_data.cell_type);
830 830 new_cell.fromJSON(cell_data);
831 831 };
832 832 };
833 833
834 834
835 835 Notebook.prototype.paste_cell_below = function () {
836 836 if (this.clipboard !== null && this.paste_enabled) {
837 837 var cell_data = this.clipboard;
838 838 var new_cell = this.insert_cell_below(cell_data.cell_type);
839 839 new_cell.fromJSON(cell_data);
840 840 };
841 841 };
842 842
843 843 // Cell undelete
844 844
845 845 Notebook.prototype.undelete = function() {
846 846 if (this.undelete_backup !== null && this.undelete_index !== null) {
847 847 var current_index = this.get_selected_index();
848 848 if (this.undelete_index < current_index) {
849 849 current_index = current_index + 1;
850 850 }
851 851 if (this.undelete_index >= this.ncells()) {
852 852 this.select(this.ncells() - 1);
853 853 }
854 854 else {
855 855 this.select(this.undelete_index);
856 856 }
857 857 var cell_data = this.undelete_backup;
858 858 var new_cell = null;
859 859 if (this.undelete_below) {
860 860 new_cell = this.insert_cell_below(cell_data.cell_type);
861 861 } else {
862 862 new_cell = this.insert_cell_above(cell_data.cell_type);
863 863 }
864 864 new_cell.fromJSON(cell_data);
865 865 this.select(current_index);
866 866 this.undelete_backup = null;
867 867 this.undelete_index = null;
868 868 }
869 869 }
870 870
871 871 // Split/merge
872 872
873 873 Notebook.prototype.split_cell = function () {
874 874 // Todo: implement spliting for other cell types.
875 875 var cell = this.get_selected_cell();
876 876 if (cell.is_splittable()) {
877 877 texta = cell.get_pre_cursor();
878 878 textb = cell.get_post_cursor();
879 879 if (cell instanceof IPython.CodeCell) {
880 880 cell.set_text(texta);
881 881 var new_cell = this.insert_cell_below('code');
882 882 new_cell.set_text(textb);
883 883 } else if (cell instanceof IPython.MarkdownCell) {
884 884 cell.set_text(texta);
885 885 cell.render();
886 886 var new_cell = this.insert_cell_below('markdown');
887 887 new_cell.edit(); // editor must be visible to call set_text
888 888 new_cell.set_text(textb);
889 889 new_cell.render();
890 890 } else if (cell instanceof IPython.HTMLCell) {
891 891 cell.set_text(texta);
892 892 cell.render();
893 893 var new_cell = this.insert_cell_below('html');
894 894 new_cell.edit(); // editor must be visible to call set_text
895 895 new_cell.set_text(textb);
896 896 new_cell.render();
897 897 };
898 898 };
899 899 };
900 900
901 901
902 902 Notebook.prototype.merge_cell_above = function () {
903 903 var index = this.get_selected_index();
904 904 var cell = this.get_cell(index);
905 905 if (index > 0) {
906 906 upper_cell = this.get_cell(index-1);
907 907 upper_text = upper_cell.get_text();
908 908 text = cell.get_text();
909 909 if (cell instanceof IPython.CodeCell) {
910 910 cell.set_text(upper_text+'\n'+text);
911 911 } else if (cell instanceof IPython.MarkdownCell || cell instanceof IPython.HTMLCell) {
912 912 cell.edit();
913 913 cell.set_text(upper_text+'\n'+text);
914 914 cell.render();
915 915 };
916 916 this.delete_cell(index-1);
917 917 this.select(this.find_cell_index(cell));
918 918 };
919 919 };
920 920
921 921
922 922 Notebook.prototype.merge_cell_below = function () {
923 923 var index = this.get_selected_index();
924 924 var cell = this.get_cell(index);
925 925 if (index < this.ncells()-1) {
926 926 lower_cell = this.get_cell(index+1);
927 927 lower_text = lower_cell.get_text();
928 928 text = cell.get_text();
929 929 if (cell instanceof IPython.CodeCell) {
930 930 cell.set_text(text+'\n'+lower_text);
931 931 } else if (cell instanceof IPython.MarkdownCell || cell instanceof IPython.HTMLCell) {
932 932 cell.edit();
933 933 cell.set_text(text+'\n'+lower_text);
934 934 cell.render();
935 935 };
936 936 this.delete_cell(index+1);
937 937 this.select(this.find_cell_index(cell));
938 938 };
939 939 };
940 940
941 941
942 942 // Cell collapsing and output clearing
943 943
944 944 Notebook.prototype.collapse = function (index) {
945 945 var i = this.index_or_selected(index);
946 946 this.get_cell(i).collapse();
947 947 this.dirty = true;
948 948 };
949 949
950 950
951 951 Notebook.prototype.expand = function (index) {
952 952 var i = this.index_or_selected(index);
953 953 this.get_cell(i).expand();
954 954 this.dirty = true;
955 955 };
956 956
957 957
958 958 Notebook.prototype.toggle_output = function (index) {
959 959 var i = this.index_or_selected(index);
960 960 this.get_cell(i).toggle_output();
961 961 this.dirty = true;
962 962 };
963 963
964 964
965 965 Notebook.prototype.toggle_output_scroll = function (index) {
966 966 var i = this.index_or_selected(index);
967 967 this.get_cell(i).toggle_output_scroll();
968 968 };
969 969
970 970
971 971 Notebook.prototype.collapse_all_output = function () {
972 972 var ncells = this.ncells();
973 973 var cells = this.get_cells();
974 974 for (var i=0; i<ncells; i++) {
975 975 if (cells[i] instanceof IPython.CodeCell) {
976 976 cells[i].output_area.collapse();
977 977 }
978 978 };
979 979 // this should not be set if the `collapse` key is removed from nbformat
980 980 this.dirty = true;
981 981 };
982 982
983 983
984 984 Notebook.prototype.scroll_all_output = function () {
985 985 var ncells = this.ncells();
986 986 var cells = this.get_cells();
987 987 for (var i=0; i<ncells; i++) {
988 988 if (cells[i] instanceof IPython.CodeCell) {
989 989 cells[i].output_area.expand();
990 990 cells[i].output_area.scroll_if_long(20);
991 991 }
992 992 };
993 993 // this should not be set if the `collapse` key is removed from nbformat
994 994 this.dirty = true;
995 995 };
996 996
997 997
998 998 Notebook.prototype.expand_all_output = function () {
999 999 var ncells = this.ncells();
1000 1000 var cells = this.get_cells();
1001 1001 for (var i=0; i<ncells; i++) {
1002 1002 if (cells[i] instanceof IPython.CodeCell) {
1003 1003 cells[i].output_area.expand();
1004 1004 cells[i].output_area.unscroll_area();
1005 1005 }
1006 1006 };
1007 1007 // this should not be set if the `collapse` key is removed from nbformat
1008 1008 this.dirty = true;
1009 1009 };
1010 1010
1011 1011
1012 1012 Notebook.prototype.clear_all_output = function () {
1013 1013 var ncells = this.ncells();
1014 1014 var cells = this.get_cells();
1015 1015 for (var i=0; i<ncells; i++) {
1016 1016 if (cells[i] instanceof IPython.CodeCell) {
1017 1017 cells[i].clear_output(true,true,true);
1018 1018 // Make all In[] prompts blank, as well
1019 1019 // TODO: make this configurable (via checkbox?)
1020 1020 cells[i].set_input_prompt();
1021 1021 }
1022 1022 };
1023 1023 this.dirty = true;
1024 1024 };
1025 1025
1026 1026
1027 1027 // Other cell functions: line numbers, ...
1028 1028
1029 1029 Notebook.prototype.cell_toggle_line_numbers = function() {
1030 1030 this.get_selected_cell().toggle_line_numbers();
1031 1031 };
1032 1032
1033 1033 // Kernel related things
1034 1034
1035 1035 Notebook.prototype.start_kernel = function () {
1036 1036 var base_url = $('body').data('baseKernelUrl') + "kernels";
1037 1037 this.kernel = new IPython.Kernel(base_url);
1038 1038 this.kernel.start(this.notebook_id);
1039 1039 // Now that the kernel has been created, tell the CodeCells about it.
1040 1040 var ncells = this.ncells();
1041 1041 for (var i=0; i<ncells; i++) {
1042 1042 var cell = this.get_cell(i);
1043 1043 if (cell instanceof IPython.CodeCell) {
1044 1044 cell.set_kernel(this.kernel)
1045 1045 };
1046 1046 };
1047 1047 };
1048 1048
1049 1049
1050 1050 Notebook.prototype.restart_kernel = function () {
1051 1051 var that = this;
1052 1052 var dialog = $('<div/>');
1053 1053 dialog.html('Do you want to restart the current kernel? You will lose all variables defined in it.');
1054 1054 $(document).append(dialog);
1055 1055 dialog.dialog({
1056 1056 resizable: false,
1057 1057 modal: true,
1058 1058 title: "Restart kernel or continue running?",
1059 1059 closeText: '',
1060 1060 buttons : {
1061 1061 "Restart": function () {
1062 1062 that.kernel.restart();
1063 1063 $(this).dialog('close');
1064 1064 },
1065 1065 "Continue running": function () {
1066 1066 $(this).dialog('close');
1067 1067 }
1068 1068 }
1069 1069 });
1070 1070 };
1071 1071
1072 1072
1073 1073 Notebook.prototype.execute_selected_cell = function (options) {
1074 1074 // add_new: should a new cell be added if we are at the end of the nb
1075 1075 // terminal: execute in terminal mode, which stays in the current cell
1076 1076 default_options = {terminal: false, add_new: true};
1077 1077 $.extend(default_options, options);
1078 1078 var that = this;
1079 1079 var cell = that.get_selected_cell();
1080 1080 var cell_index = that.find_cell_index(cell);
1081 1081 if (cell instanceof IPython.CodeCell) {
1082 1082 cell.execute();
1083 1083 } else if (cell instanceof IPython.HTMLCell) {
1084 1084 cell.render();
1085 1085 }
1086 1086 if (default_options.terminal) {
1087 1087 cell.select_all();
1088 1088 } else {
1089 1089 if ((cell_index === (that.ncells()-1)) && default_options.add_new) {
1090 1090 that.insert_cell_below('code');
1091 1091 // If we are adding a new cell at the end, scroll down to show it.
1092 1092 that.scroll_to_bottom();
1093 1093 } else {
1094 1094 that.select(cell_index+1);
1095 1095 };
1096 1096 };
1097 1097 this.dirty = true;
1098 1098 };
1099 1099
1100 1100
1101 1101 Notebook.prototype.execute_cells_below = function () {
1102 1102 this.execute_cell_range(this.get_selected_index(), this.ncells());
1103 1103 that.scroll_to_bottom();
1104 1104 };
1105 1105
1106 1106 Notebook.prototype.execute_cells_above = function () {
1107 1107 this.execute_cell_range(0, this.get_selected_index());
1108 1108 };
1109 1109
1110 1110 Notebook.prototype.execute_all_cells = function () {
1111 1111 this.execute_cell_range(0, this.ncells());
1112 1112 that.scroll_to_bottom();
1113 1113 };
1114 1114
1115 1115 Notebook.prototype.execute_cell_range = function (start, end) {
1116 1116 for (var i=start; i<end; i++) {
1117 1117 this.select(i);
1118 1118 this.execute_selected_cell({add_new:false});
1119 1119 };
1120 1120 };
1121 1121
1122 1122 // Persistance and loading
1123 1123
1124 1124 Notebook.prototype.get_notebook_id = function () {
1125 1125 return this.notebook_id;
1126 1126 };
1127 1127
1128 1128
1129 1129 Notebook.prototype.get_notebook_name = function () {
1130 1130 return this.notebook_name;
1131 1131 };
1132 1132
1133 1133
1134 1134 Notebook.prototype.set_notebook_name = function (name) {
1135 1135 this.notebook_name = name;
1136 1136 };
1137 1137
1138 1138
1139 1139 Notebook.prototype.test_notebook_name = function (nbname) {
1140 1140 nbname = nbname || '';
1141 1141 if (this.notebook_name_blacklist_re.test(nbname) == false && nbname.length>0) {
1142 1142 return true;
1143 1143 } else {
1144 1144 return false;
1145 1145 };
1146 1146 };
1147 1147
1148 1148
1149 1149 Notebook.prototype.fromJSON = function (data) {
1150 1150 var ncells = this.ncells();
1151 1151 var i;
1152 1152 for (i=0; i<ncells; i++) {
1153 1153 // Always delete cell 0 as they get renumbered as they are deleted.
1154 1154 this.delete_cell(0);
1155 1155 };
1156 1156 // Save the metadata and name.
1157 1157 this.metadata = data.metadata;
1158 1158 this.notebook_name = data.metadata.name;
1159 1159 // Only handle 1 worksheet for now.
1160 1160 var worksheet = data.worksheets[0];
1161 1161 if (worksheet !== undefined) {
1162 1162 if (worksheet.metadata) {
1163 1163 this.worksheet_metadata = worksheet.metadata;
1164 1164 }
1165 1165 var new_cells = worksheet.cells;
1166 1166 ncells = new_cells.length;
1167 1167 var cell_data = null;
1168 1168 var new_cell = null;
1169 1169 for (i=0; i<ncells; i++) {
1170 1170 cell_data = new_cells[i];
1171 1171 // VERSIONHACK: plaintext -> raw
1172 1172 // handle never-released plaintext name for raw cells
1173 1173 if (cell_data.cell_type === 'plaintext'){
1174 1174 cell_data.cell_type = 'raw';
1175 1175 }
1176 1176
1177 1177 new_cell = this.insert_cell_below(cell_data.cell_type);
1178 1178 new_cell.fromJSON(cell_data);
1179 1179 };
1180 1180 };
1181 1181 if (data.worksheets.length > 1) {
1182 1182 var dialog = $('<div/>');
1183 1183 dialog.html("This notebook has " + data.worksheets.length + " worksheets, " +
1184 1184 "but this version of IPython can only handle the first. " +
1185 1185 "If you save this notebook, worksheets after the first will be lost."
1186 1186 );
1187 1187 this.element.append(dialog);
1188 1188 dialog.dialog({
1189 1189 resizable: false,
1190 1190 modal: true,
1191 1191 title: "Multiple worksheets",
1192 1192 closeText: "",
1193 1193 close: function(event, ui) {$(this).dialog('destroy').remove();},
1194 1194 buttons : {
1195 1195 "OK": function () {
1196 1196 $(this).dialog('close');
1197 1197 }
1198 1198 },
1199 1199 width: 400
1200 1200 });
1201 1201 }
1202 1202 };
1203 1203
1204 1204
1205 1205 Notebook.prototype.toJSON = function () {
1206 1206 var cells = this.get_cells();
1207 1207 var ncells = cells.length;
1208 1208 var cell_array = new Array(ncells);
1209 1209 for (var i=0; i<ncells; i++) {
1210 1210 cell_array[i] = cells[i].toJSON();
1211 1211 };
1212 1212 var data = {
1213 1213 // Only handle 1 worksheet for now.
1214 1214 worksheets : [{
1215 1215 cells: cell_array,
1216 1216 metadata: this.worksheet_metadata
1217 1217 }],
1218 1218 metadata : this.metadata
1219 1219 };
1220 1220 return data;
1221 1221 };
1222 1222
1223 1223 Notebook.prototype.save_notebook = function () {
1224 1224 // We may want to move the name/id/nbformat logic inside toJSON?
1225 1225 var data = this.toJSON();
1226 1226 data.metadata.name = this.notebook_name;
1227 1227 data.nbformat = this.nbformat;
1228 1228 data.nbformat_minor = this.nbformat_minor;
1229 1229 // We do the call with settings so we can set cache to false.
1230 1230 var settings = {
1231 1231 processData : false,
1232 1232 cache : false,
1233 1233 type : "PUT",
1234 1234 data : JSON.stringify(data),
1235 1235 headers : {'Content-Type': 'application/json'},
1236 1236 success : $.proxy(this.save_notebook_success,this),
1237 1237 error : $.proxy(this.save_notebook_error,this)
1238 1238 };
1239 1239 $([IPython.events]).trigger('notebook_saving.Notebook');
1240 1240 var url = $('body').data('baseProjectUrl') + 'notebooks/' + this.notebook_id;
1241 1241 $.ajax(url, settings);
1242 1242 };
1243 1243
1244 1244
1245 1245 Notebook.prototype.save_notebook_success = function (data, status, xhr) {
1246 1246 this.dirty = false;
1247 1247 $([IPython.events]).trigger('notebook_saved.Notebook');
1248 1248 };
1249 1249
1250 1250
1251 1251 Notebook.prototype.save_notebook_error = function (xhr, status, error_msg) {
1252 1252 $([IPython.events]).trigger('notebook_save_failed.Notebook');
1253 1253 };
1254 1254
1255 1255
1256 1256 Notebook.prototype.load_notebook = function (notebook_id) {
1257 1257 var that = this;
1258 1258 this.notebook_id = notebook_id;
1259 1259 // We do the call with settings so we can set cache to false.
1260 1260 var settings = {
1261 1261 processData : false,
1262 1262 cache : false,
1263 1263 type : "GET",
1264 1264 dataType : "json",
1265 1265 success : $.proxy(this.load_notebook_success,this),
1266 1266 error : $.proxy(this.load_notebook_error,this),
1267 1267 };
1268 1268 $([IPython.events]).trigger('notebook_loading.Notebook');
1269 1269 var url = $('body').data('baseProjectUrl') + 'notebooks/' + this.notebook_id;
1270 1270 $.ajax(url, settings);
1271 1271 };
1272 1272
1273 1273
1274 1274 Notebook.prototype.load_notebook_success = function (data, status, xhr) {
1275 1275 this.fromJSON(data);
1276 1276 if (this.ncells() === 0) {
1277 1277 this.insert_cell_below('code');
1278 1278 };
1279 1279 this.dirty = false;
1280 1280 this.select(0);
1281 1281 this.scroll_to_top();
1282 1282 if (data.orig_nbformat !== undefined && data.nbformat !== data.orig_nbformat) {
1283 1283 msg = "This notebook has been converted from an older " +
1284 1284 "notebook format (v"+data.orig_nbformat+") to the current notebook " +
1285 1285 "format (v"+data.nbformat+"). The next time you save this notebook, the " +
1286 1286 "newer notebook format will be used and older verions of IPython " +
1287 1287 "may not be able to read it. To keep the older version, close the " +
1288 1288 "notebook without saving it.";
1289 1289 var dialog = $('<div/>');
1290 1290 dialog.html(msg);
1291 1291 this.element.append(dialog);
1292 1292 dialog.dialog({
1293 1293 resizable: false,
1294 1294 modal: true,
1295 1295 title: "Notebook converted",
1296 1296 closeText: "",
1297 1297 close: function(event, ui) {$(this).dialog('destroy').remove();},
1298 1298 buttons : {
1299 1299 "OK": function () {
1300 1300 $(this).dialog('close');
1301 1301 }
1302 1302 },
1303 1303 width: 400
1304 1304 });
1305 1305 } else if (data.orig_nbformat_minor !== undefined && data.nbformat_minor !== data.orig_nbformat_minor) {
1306 1306 var that = this;
1307 1307 var orig_vs = 'v' + data.nbformat + '.' + data.orig_nbformat_minor;
1308 1308 var this_vs = 'v' + data.nbformat + '.' + this.nbformat_minor;
1309 1309 msg = "This notebook is version " + orig_vs + ", but we only fully support up to " +
1310 1310 this_vs + ". You can still work with this notebook, but some features " +
1311 1311 "introduced in later notebook versions may not be available."
1312 1312
1313 1313 var dialog = $('<div/>');
1314 1314 dialog.html(msg);
1315 1315 this.element.append(dialog);
1316 1316 dialog.dialog({
1317 1317 resizable: false,
1318 1318 modal: true,
1319 1319 title: "Newer Notebook",
1320 1320 closeText: "",
1321 1321 close: function(event, ui) {$(this).dialog('destroy').remove();},
1322 1322 buttons : {
1323 1323 "OK": function () {
1324 1324 $(this).dialog('close');
1325 1325 }
1326 1326 },
1327 1327 width: 400
1328 1328 });
1329 1329
1330 1330 }
1331 1331 // Create the kernel after the notebook is completely loaded to prevent
1332 1332 // code execution upon loading, which is a security risk.
1333 1333 if (! this.read_only) {
1334 1334 this.start_kernel();
1335 1335 }
1336 1336 $([IPython.events]).trigger('notebook_loaded.Notebook');
1337 1337 };
1338 1338
1339 1339
1340 1340 Notebook.prototype.load_notebook_error = function (xhr, textStatus, errorThrow) {
1341 1341 if (xhr.status === 500) {
1342 1342 msg = "An error occurred while loading this notebook. Most likely " +
1343 1343 "this notebook is in a newer format than is supported by this " +
1344 1344 "version of IPython. This version can load notebook formats " +
1345 1345 "v"+this.nbformat+" or earlier.";
1346 1346 var dialog = $('<div/>');
1347 1347 dialog.html(msg);
1348 1348 this.element.append(dialog);
1349 1349 dialog.dialog({
1350 1350 resizable: false,
1351 1351 modal: true,
1352 1352 title: "Error loading notebook",
1353 1353 closeText: "",
1354 1354 close: function(event, ui) {$(this).dialog('destroy').remove();},
1355 1355 buttons : {
1356 1356 "OK": function () {
1357 1357 $(this).dialog('close');
1358 1358 }
1359 1359 },
1360 1360 width: 400
1361 1361 });
1362 1362 }
1363 1363 }
1364 1364
1365 1365 IPython.Notebook = Notebook;
1366 1366
1367 1367
1368 1368 return IPython;
1369 1369
1370 1370 }(IPython));
1371 1371
General Comments 0
You need to be logged in to leave comments. Login now