##// END OF EJS Templates
Refactoring of the notebooks cell management....
Brian Granger -
Show More
@@ -49,7 +49,7 b' var IPython = (function (IPython) {'
49
49
50
50
51 FullEditWidget.prototype.open = function () {
51 FullEditWidget.prototype.open = function () {
52 var cell = IPython.notebook.selected_cell();
52 var cell = IPython.notebook.get_selected_cell();
53 if (!this.opened && cell instanceof IPython.CodeCell) {
53 if (!this.opened && cell instanceof IPython.CodeCell) {
54 $('#fulledit_widget').show();
54 $('#fulledit_widget').show();
55 $('#main_app').hide();
55 $('#main_app').hide();
@@ -75,7 +75,7 b' var IPython = (function (IPython) {'
75 $('#menubar').show();
75 $('#menubar').show();
76 $('body').css({overflow : 'hidden'});
76 $('body').css({overflow : 'hidden'});
77 var code = this.ace_editor.getSession().getValue();
77 var code = this.ace_editor.getSession().getValue();
78 var cell = IPython.notebook.selected_cell();
78 var cell = IPython.notebook.get_selected_cell();
79 cell.set_text(code);
79 cell.set_text(code);
80 cell.select();
80 cell.select();
81 this.opened = false;
81 this.opened = false;
@@ -26,7 +26,7 b' var IPython = (function (IPython) {'
26 select : function (event, ui) {
26 select : function (event, ui) {
27 // The selected cell loses focus when the menu is entered, so we
27 // The selected cell loses focus when the menu is entered, so we
28 // re-select it upon selection.
28 // re-select it upon selection.
29 var i = IPython.notebook.selected_index();
29 var i = IPython.notebook.get_selected_index();
30 IPython.notebook.select(i);
30 IPython.notebook.select(i);
31 }
31 }
32 });
32 });
@@ -100,10 +100,10 b' var IPython = (function (IPython) {'
100 });
100 });
101 // Insert
101 // Insert
102 this.element.find('#insert_cell_above').click(function () {
102 this.element.find('#insert_cell_above').click(function () {
103 IPython.notebook.insert_code_cell_above();
103 IPython.notebook.insert_cell_above('code');
104 });
104 });
105 this.element.find('#insert_cell_below').click(function () {
105 this.element.find('#insert_cell_below').click(function () {
106 IPython.notebook.insert_code_cell_below();
106 IPython.notebook.insert_cell_below('code');
107 });
107 });
108 // Cell
108 // Cell
109 this.element.find('#full_edit_cell').click(function () {
109 this.element.find('#full_edit_cell').click(function () {
This diff has been collapsed as it changes many lines, (504 lines changed) Show them Hide them
@@ -47,11 +47,11 b' var IPython = (function (IPython) {'
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/>').addClass('end_space').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_cell_below('code',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');
@@ -69,13 +69,13 b' var IPython = (function (IPython) {'
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.get_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.get_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();
@@ -111,12 +111,12 b' var IPython = (function (IPython) {'
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_cell_above('code');
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_cell_below('code');
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) {
@@ -234,29 +234,67 b' var IPython = (function (IPython) {'
234
234
235 // Cell indexing, retrieval, etc.
235 // Cell indexing, retrieval, etc.
236
236
237
237 Notebook.prototype.get_cell_elements = function () {
238 Notebook.prototype.cell_elements = function () {
239 return this.element.children("div.cell");
238 return this.element.children("div.cell");
240 };
239 };
241
240
242
241
242 Notebook.prototype.get_cell_element = function (index) {
243 var result = null;
244 var e = this.get_cell_elements().eq(index);
245 if (e.length !== 0) {
246 result = e;
247 }
248 return result;
249 };
250
251
243 Notebook.prototype.ncells = function (cell) {
252 Notebook.prototype.ncells = function (cell) {
244 return this.cell_elements().length;
253 return this.get_cell_elements().length;
245 };
254 };
246
255
247
256
248 // TODO: we are often calling cells as cells()[i], which we should optimize
257 // TODO: we are often calling cells as cells()[i], which we should optimize
249 // to cells(i) or a new method.
258 // to cells(i) or a new method.
250 Notebook.prototype.cells = function () {
259 Notebook.prototype.get_cells = function () {
251 return this.cell_elements().toArray().map(function (e) {
260 return this.get_cell_elements().toArray().map(function (e) {
252 return $(e).data("cell");
261 return $(e).data("cell");
253 });
262 });
254 };
263 };
255
264
256
265
266 Notebook.prototype.get_cell = function (index) {
267 var result = null;
268 var ce = this.get_cell_element(index);
269 if (ce !== null) {
270 result = ce.data('cell');
271 }
272 return result;
273 }
274
275
276 Notebook.prototype.get_next_cell = function (cell) {
277 var result = null;
278 var index = this.find_cell_index(cell);
279 if (index !== null && index < this.ncells()) {
280 result = this.get_cell(index+1);
281 }
282 return result;
283 }
284
285
286 Notebook.prototype.get_prev_cell = function (cell) {
287 var result = null;
288 var index = this.find_cell_index(cell);
289 if (index !== null && index > 1) {
290 result = this.get_cell(index-1);
291 }
292 return result;
293 }
294
257 Notebook.prototype.find_cell_index = function (cell) {
295 Notebook.prototype.find_cell_index = function (cell) {
258 var result = null;
296 var result = null;
259 this.cell_elements().filter(function (index) {
297 this.get_cell_elements().filter(function (index) {
260 if ($(this).data("cell") === cell) {
298 if ($(this).data("cell") === cell) {
261 result = index;
299 result = index;
262 };
300 };
@@ -267,8 +305,8 b' var IPython = (function (IPython) {'
267
305
268 Notebook.prototype.index_or_selected = function (index) {
306 Notebook.prototype.index_or_selected = function (index) {
269 var i;
307 var i;
270 if (index === undefined) {
308 if (index === undefined || index === null) {
271 i = this.selected_index();
309 i = this.get_selected_index();
272 if (i === null) {
310 if (i === null) {
273 i = 0;
311 i = 0;
274 }
312 }
@@ -279,38 +317,23 b' var IPython = (function (IPython) {'
279 };
317 };
280
318
281
319
282 Notebook.prototype.select = function (index) {
320 Notebook.prototype.get_selected_cell = function () {
283 if (index !== undefined && index >= 0 && index < this.ncells()) {
321 var index = this.get_selected_index();
284 if (this.selected_index() !== null) {
322 return this.get_cell(index);
285 this.selected_cell().unselect();
286 };
287 this.cells()[index].select();
288 };
289 return this;
290 };
291
292
293 Notebook.prototype.select_next = function () {
294 var index = this.selected_index();
295 if (index !== null && index >= 0 && (index+1) < this.ncells()) {
296 this.select(index+1);
297 };
298 return this;
299 };
323 };
300
324
301
325
302 Notebook.prototype.select_prev = function () {
326 Notebook.prototype.is_valid_cell_index = function (index) {
303 var index = this.selected_index();
327 if (index !== null && index >= 0 && index < this.ncells()) {
304 if (index !== null && index >= 0 && (index-1) < this.ncells()) {
328 return true;
305 this.select(index-1);
329 } else {
330 return false;
306 };
331 };
307 return this;
332 }
308 };
309
310
333
311 Notebook.prototype.selected_index = function () {
334 Notebook.prototype.get_selected_index = function () {
312 var result = null;
335 var result = null;
313 this.cell_elements().filter(function (index) {
336 this.get_cell_elements().filter(function (index) {
314 if ($(this).data("cell").selected === true) {
337 if ($(this).data("cell").selected === true) {
315 result = index;
338 result = index;
316 };
339 };
@@ -322,7 +345,7 b' var IPython = (function (IPython) {'
322 Notebook.prototype.cell_for_msg = function (msg_id) {
345 Notebook.prototype.cell_for_msg = function (msg_id) {
323 var cell_id = this.msg_cell_map[msg_id];
346 var cell_id = this.msg_cell_map[msg_id];
324 var result = null;
347 var result = null;
325 this.cell_elements().filter(function (index) {
348 this.get_cell_elements().filter(function (index) {
326 cell = $(this).data("cell");
349 cell = $(this).data("cell");
327 if (cell.cell_id === cell_id) {
350 if (cell.cell_id === cell_id) {
328 result = cell;
351 result = cell;
@@ -332,68 +355,45 b' var IPython = (function (IPython) {'
332 };
355 };
333
356
334
357
335 Notebook.prototype.selected_cell = function () {
358 // Cell selection.
336 return this.cell_elements().eq(this.selected_index()).data("cell");
337 };
338
339
340 // Cell insertion, deletion and moving.
341
359
342 Notebook.prototype.delete_cell = function (index) {
360 Notebook.prototype.select = function (index) {
343 var i = this.index_or_selected(index);
361 if (index !== undefined && index >= 0 && index < this.ncells()) {
344 if (i !== null && i >= 0 && i < this.ncells()) {
362 sindex = this.get_selected_index()
345 this.cell_elements().eq(i).remove();
363 if (sindex !== null) {
346 if (i === (this.ncells())) {
364 this.get_cell(sindex).unselect();
347 this.select(i-1);
348 } else {
349 this.select(i);
350 };
365 };
366 this.get_cell(index).select();
351 };
367 };
352 this.dirty = true;
353 return this;
368 return this;
354 };
369 };
355
370
356
371
357 Notebook.prototype.append_cell = function (cell) {
372 Notebook.prototype.select_next = function () {
358 this.element.find('div.end_space').before(cell.element);
373 var index = this.get_selected_index();
359 this.dirty = true;
374 if (index !== null && index >= 0 && (index+1) < this.ncells()) {
360 return this;
375 this.select(index+1);
361 };
362
363
364 Notebook.prototype.insert_cell_below = function (cell, index) {
365 var ncells = this.ncells();
366 if (ncells === 0) {
367 this.append_cell(cell);
368 return this;
369 };
370 if (index >= 0 && index < ncells) {
371 this.cell_elements().eq(index).after(cell.element);
372 };
376 };
373 this.dirty = true;
374 return this;
377 return this;
375 };
378 };
376
379
377
380
378 Notebook.prototype.insert_cell_above = function (cell, index) {
381 Notebook.prototype.select_prev = function () {
379 var ncells = this.ncells();
382 var index = this.get_selected_index();
380 if (ncells === 0) {
383 if (index !== null && index >= 0 && (index-1) < this.ncells()) {
381 this.append_cell(cell);
384 this.select(index-1);
382 return this;
383 };
384 if (index >= 0 && index < ncells) {
385 this.cell_elements().eq(index).before(cell.element);
386 };
385 };
387 this.dirty = true;
388 return this;
386 return this;
389 };
387 };
390
388
391
389
390 // Cell movement
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 = this.index_or_selected();
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.get_cell_element(i-1);
396 var tomove = this.cell_elements().eq(i);
396 var tomove = this.get_cell_element(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);
@@ -406,10 +406,10 b' var IPython = (function (IPython) {'
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 = this.index_or_selected();
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.get_cell_element(i+1);
412 var tomove = this.cell_elements().eq(i);
412 var tomove = this.get_cell_element(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);
@@ -422,14 +422,16 b' var IPython = (function (IPython) {'
422
422
423
423
424 Notebook.prototype.sort_cells = function () {
424 Notebook.prototype.sort_cells = function () {
425 // This is not working right now. Calling this will actually crash
426 // the browser. I think there is an infinite loop in here...
425 var ncells = this.ncells();
427 var ncells = this.ncells();
426 var sindex = this.selected_index();
428 var sindex = this.get_selected_index();
427 var swapped;
429 var swapped;
428 do {
430 do {
429 swapped = false;
431 swapped = false;
430 for (var i=1; i<ncells; i++) {
432 for (var i=1; i<ncells; i++) {
431 current = this.cell_elements().eq(i).data("cell");
433 current = this.get_cell(i);
432 previous = this.cell_elements().eq(i-1).data("cell");
434 previous = this.get_cell(i-1);
433 if (previous.input_prompt_number > current.input_prompt_number) {
435 if (previous.input_prompt_number > current.input_prompt_number) {
434 this.move_cell_up(i);
436 this.move_cell_up(i);
435 swapped = true;
437 swapped = true;
@@ -440,146 +442,161 b' var IPython = (function (IPython) {'
440 return this;
442 return this;
441 };
443 };
442
444
445 // Insertion, deletion.
443
446
444 Notebook.prototype.insert_code_cell_above = function (index) {
447 Notebook.prototype.delete_cell = function (index) {
445 // TODO: Bounds check for i
446 var i = this.index_or_selected(index);
447 var cell = new IPython.CodeCell(this);
448 cell.set_input_prompt();
449 this.insert_cell_above(cell, i);
450 this.select(this.find_cell_index(cell));
451 return cell;
452 };
453
454
455 Notebook.prototype.insert_code_cell_below = function (index) {
456 // TODO: Bounds check for i
457 var i = this.index_or_selected(index);
458 var cell = new IPython.CodeCell(this);
459 cell.set_input_prompt();
460 this.insert_cell_below(cell, i);
461 this.select(this.find_cell_index(cell));
462 return cell;
463 };
464
465
466 Notebook.prototype.insert_html_cell_above = function (index) {
467 // TODO: Bounds check for i
468 var i = this.index_or_selected(index);
469 var cell = new IPython.HTMLCell(this);
470 cell.config_mathjax();
471 this.insert_cell_above(cell, i);
472 this.select(this.find_cell_index(cell));
473 return cell;
474 };
475
476
477 Notebook.prototype.insert_html_cell_below = function (index) {
478 // TODO: Bounds check for i
479 var i = this.index_or_selected(index);
448 var i = this.index_or_selected(index);
480 var cell = new IPython.HTMLCell(this);
449 if (this.is_valid_cell_index(i)) {
481 cell.config_mathjax();
450 var ce = this.get_cell_element(i);
482 this.insert_cell_below(cell, i);
451 ce.remove();
483 this.select(this.find_cell_index(cell));
452 if (i === (this.ncells())) {
484 return cell;
453 this.select(i-1);
454 } else {
455 this.select(i);
456 };
457 this.dirty = true;
458 };
459 return this;
485 };
460 };
486
461
487
462
488 Notebook.prototype.insert_markdown_cell_above = function (index) {
463 Notebook.prototype.insert_cell_below = function (type, index) {
489 // TODO: Bounds check for i
464 // type = ('code','html','markdown')
490 var i = this.index_or_selected(index);
465 // index = cell index or undefined to insert below selected
491 var cell = new IPython.MarkdownCell(this);
466 index = this.index_or_selected(index);
492 cell.config_mathjax();
467 if (this.ncells() === 0 || this.is_valid_cell_index(index)) {
493 this.insert_cell_above(cell, i);
468 var cell = null;
494 this.select(this.find_cell_index(cell));
469 if (type === 'code') {
495 return cell;
470 var cell = new IPython.CodeCell(this);
471 cell.set_input_prompt();
472 } else if (type === 'markdown') {
473 var cell = new IPython.MarkdownCell(this);
474 cell.config_mathjax();
475 } else if (type === 'html') {
476 var cell = new IPython.HTMLCell(this);
477 cell.config_mathjax();
478 };
479 if (cell !== null) {
480 if (this.ncells() === 0) {
481 this.element.find('div.end_space').before(cell.element);
482 this.select(this.find_cell_index(cell));
483 this.dirty = true;
484 } else if (this.is_valid_cell_index(index)) {
485 this.get_cell_element(index).after(cell.element);
486 this.select(this.find_cell_index(cell));
487 this.dirty = true;
488 };
489 return cell;
490 };
491 };
496 };
492 };
497
493
498
494
499 Notebook.prototype.insert_markdown_cell_below = function (index) {
495 Notebook.prototype.insert_cell_above = function (type, index) {
500 // TODO: Bounds check for i
496 // type = ('code','html','markdown')
501 var i = this.index_or_selected(index);
497 // index = cell index or undefined to insert above selected
502 var cell = new IPython.MarkdownCell(this);
498 index = this.index_or_selected(index);
503 cell.config_mathjax();
499 if (this.ncells() === 0 || this.is_valid_cell_index(index)) {
504 this.insert_cell_below(cell, i);
500 var cell = null;
505 this.select(this.find_cell_index(cell));
501 if (type === 'code') {
506 return cell;
502 var cell = new IPython.CodeCell(this);
503 cell.set_input_prompt();
504 } else if (type === 'markdown') {
505 var cell = new IPython.MarkdownCell(this);
506 cell.config_mathjax();
507 } else if (type === 'html') {
508 var cell = new IPython.HTMLCell(this);
509 cell.config_mathjax();
510 };
511 if (cell !== null) {
512 if (this.ncells() === 0) {
513 this.element.find('div.end_space').before(cell.element);
514 this.select(this.find_cell_index(cell));
515 this.dirty = true;
516 } else if (this.is_valid_cell_index(index)) {
517 this.get_cell_element(index).before(cell.element);
518 this.select(this.find_cell_index(cell));
519 this.dirty = true;
520 };
521 return cell;
522 };
523 };
507 };
524 };
508
525
509
526
510 Notebook.prototype.to_code = function (index) {
527 Notebook.prototype.to_code = function (index) {
511 // TODO: Bounds check for i
512 var i = this.index_or_selected(index);
528 var i = this.index_or_selected(index);
513 var source_element = this.cell_elements().eq(i);
529 if (this.is_valid_cell_index(i)) {
514 var source_cell = source_element.data("cell");
530 var source_element = this.get_cell_element(i);
515 if (!(source_cell instanceof IPython.CodeCell)) {
531 var source_cell = source_element.data("cell");
516 this.insert_code_cell_below(i);
532 if (!(source_cell instanceof IPython.CodeCell)) {
517 var target_cell = this.cells()[i+1];
533 target_cell = this.insert_cell_below('code',i);
518 var text = source_cell.get_text();
534 var text = source_cell.get_text();
519 if (text === source_cell.placeholder) {
535 if (text === source_cell.placeholder) {
520 text = '';
536 text = '';
521 }
537 }
522 target_cell.set_text(text);
538 target_cell.set_text(text);
523 source_element.remove();
539 source_element.remove();
524 target_cell.select();
540 target_cell.select();
541 };
542 this.dirty = true;
525 };
543 };
526 this.dirty = true;
527 };
544 };
528
545
529
546
530 Notebook.prototype.to_markdown = function (index) {
547 Notebook.prototype.to_markdown = function (index) {
531 // TODO: Bounds check for i
532 var i = this.index_or_selected(index);
548 var i = this.index_or_selected(index);
533 var source_element = this.cell_elements().eq(i);
549 if (this.is_valid_cell_index(i)) {
534 var source_cell = source_element.data("cell");
550 var source_element = this.get_cell_element(i);
535 var target_cell = null;
551 var source_cell = source_element.data("cell");
536 if (!(source_cell instanceof IPython.MarkdownCell)) {
552 var target_cell = null;
537 this.insert_markdown_cell_below(i);
553 if (!(source_cell instanceof IPython.MarkdownCell)) {
538 target_cell = this.cells()[i+1];
554 target_cell = this.insert_cell_below('markdown',i);
539 var text = source_cell.get_text();
555 var text = source_cell.get_text();
540 if (text === source_cell.placeholder) {
556 if (text === source_cell.placeholder) {
541 text = target_cell.placeholder;
557 text = target_cell.placeholder;
558 };
559 if (target_cell !== null) {
560 if (text === "") {text = target_cell.placeholder;};
561 // The edit must come before the set_text.
562 target_cell.edit();
563 target_cell.set_text(text);
564 source_element.remove();
565 target_cell.select();
566 }
567 this.dirty = true;
542 };
568 };
543 if (target_cell !== null) {
544 if (text === "") {text = target_cell.placeholder;};
545 // The edit must come before the set_text.
546 target_cell.edit();
547 target_cell.set_text(text);
548 source_element.remove();
549 target_cell.select();
550 }
551 this.dirty = true;
552 };
569 };
553 };
570 };
554
571
555
572
556 Notebook.prototype.to_html = function (index) {
573 Notebook.prototype.to_html = function (index) {
557 // TODO: Bounds check for i
558 var i = this.index_or_selected(index);
574 var i = this.index_or_selected(index);
559 var source_element = this.cell_elements().eq(i);
575 if (this.is_valid_cell_index(i)) {
560 var source_cell = source_element.data("cell");
576 var source_element = this.get_cell_element(i);
561 var target_cell = null;
577 var source_cell = source_element.data("cell");
562 if (!(source_cell instanceof IPython.HTMLCell)) {
578 var target_cell = null;
563 this.insert_html_cell_below(i);
579 if (!(source_cell instanceof IPython.HTMLCell)) {
564 target_cell = this.cells()[i+1];
580 target_cell = this.insert_cell_below('html',i);
565 var text = source_cell.get_text();
581 var text = source_cell.get_text();
566 if (text === source_cell.placeholder) {
582 if (text === source_cell.placeholder) {
567 text = target_cell.placeholder;
583 text = target_cell.placeholder;
584 };
585 if (target_cell !== null) {
586 if (text === "") {text = target_cell.placeholder;};
587 // The edit must come before the set_text.
588 target_cell.edit();
589 target_cell.set_text(text);
590 source_element.remove();
591 target_cell.select();
592 }
593 this.dirty = true;
568 };
594 };
569 if (target_cell !== null) {
570 if (text === "") {text = target_cell.placeholder;};
571 // The edit must come before the set_text.
572 target_cell.edit();
573 target_cell.set_text(text);
574 source_element.remove();
575 target_cell.select();
576 }
577 this.dirty = true;
578 };
595 };
579 };
596 };
580
597
581
598
582 // Copy/Paste/Merge/Split
599 // Cut/Copy/Paste
583
600
584 Notebook.prototype.enable_paste = function () {
601 Notebook.prototype.enable_paste = function () {
585 var that = this;
602 var that = this;
@@ -611,7 +628,7 b' var IPython = (function (IPython) {'
611 }
628 }
612
629
613 Notebook.prototype.copy_cell = function () {
630 Notebook.prototype.copy_cell = function () {
614 var cell = this.selected_cell();
631 var cell = this.get_selected_cell();
615 this.clipboard = cell.toJSON();
632 this.clipboard = cell.toJSON();
616 this.enable_paste();
633 this.enable_paste();
617 };
634 };
@@ -620,16 +637,11 b' var IPython = (function (IPython) {'
620 Notebook.prototype.paste_cell = function () {
637 Notebook.prototype.paste_cell = function () {
621 if (this.clipboard !== null && this.paste_enabled) {
638 if (this.clipboard !== null && this.paste_enabled) {
622 var cell_data = this.clipboard;
639 var cell_data = this.clipboard;
623 if (cell_data.cell_type == 'code') {
640 var new_cell = this.insert_cell_above(cell_data.cell_type);
624 new_cell = this.insert_code_cell_above();
625 } else if (cell_data.cell_type === 'html') {
626 new_cell = this.insert_html_cell_above();
627 } else if (cell_data.cell_type === 'markdown') {
628 new_cell = this.insert_markdown_cell_above();
629 };
630 new_cell.fromJSON(cell_data);
641 new_cell.fromJSON(cell_data);
631 this.select_next();
642 old_cell = this.get_next_cell(new_cell);
632 this.delete_cell();
643 this.delete_cell(this.find_cell_index(old_cell));
644 this.select(this.find_cell_index(new_cell));
633 };
645 };
634 };
646 };
635
647
@@ -637,13 +649,7 b' var IPython = (function (IPython) {'
637 Notebook.prototype.paste_cell_above = function () {
649 Notebook.prototype.paste_cell_above = function () {
638 if (this.clipboard !== null && this.paste_enabled) {
650 if (this.clipboard !== null && this.paste_enabled) {
639 var cell_data = this.clipboard;
651 var cell_data = this.clipboard;
640 if (cell_data.cell_type == 'code') {
652 var new_cell = this.insert_cell_above(cell_data.cell_type);
641 new_cell = this.insert_code_cell_above();
642 } else if (cell_data.cell_type === 'html') {
643 new_cell = this.insert_html_cell_above();
644 } else if (cell_data.cell_type === 'markdown') {
645 new_cell = this.insert_markdown_cell_above();
646 };
647 new_cell.fromJSON(cell_data);
653 new_cell.fromJSON(cell_data);
648 };
654 };
649 };
655 };
@@ -652,21 +658,17 b' var IPython = (function (IPython) {'
652 Notebook.prototype.paste_cell_below = function () {
658 Notebook.prototype.paste_cell_below = function () {
653 if (this.clipboard !== null && this.paste_enabled) {
659 if (this.clipboard !== null && this.paste_enabled) {
654 var cell_data = this.clipboard;
660 var cell_data = this.clipboard;
655 if (cell_data.cell_type == 'code') {
661 var new_cell = this.insert_cell_below(cell_data.cell_type);
656 new_cell = this.insert_code_cell_below();
657 } else if (cell_data.cell_type === 'html') {
658 new_cell = this.insert_html_cell_below();
659 } else if (cell_data.cell_type === 'markdown') {
660 new_cell = this.insert_markdown_cell_below();
661 };
662 new_cell.fromJSON(cell_data);
662 new_cell.fromJSON(cell_data);
663 };
663 };
664 };
664 };
665
665
666
666
667 // Split/merge
668
667 Notebook.prototype.split_cell = function () {
669 Notebook.prototype.split_cell = function () {
668 // Todo: implement spliting for other cell types.
670 // Todo: implement spliting for other cell types.
669 var cell = this.selected_cell();
671 var cell = this.get_selected_cell();
670 if (cell instanceof IPython.CodeCell) {
672 if (cell instanceof IPython.CodeCell) {
671 var cursor = cell.code_mirror.getCursor();
673 var cursor = cell.code_mirror.getCursor();
672 var last_line_num = cell.code_mirror.lineCount()-1;
674 var last_line_num = cell.code_mirror.lineCount()-1;
@@ -677,7 +679,7 b' var IPython = (function (IPython) {'
677 texta = texta.replace(/^\n+/, '').replace(/\n+$/, '');
679 texta = texta.replace(/^\n+/, '').replace(/\n+$/, '');
678 textb = textb.replace(/^\n+/, '').replace(/\n+$/, '');
680 textb = textb.replace(/^\n+/, '').replace(/\n+$/, '');
679 cell.set_text(texta);
681 cell.set_text(texta);
680 var new_cell = this.insert_code_cell_below();
682 var new_cell = this.insert_cell_below('code');
681 new_cell.set_text(textb);
683 new_cell.set_text(textb);
682 };
684 };
683 };
685 };
@@ -685,11 +687,11 b' var IPython = (function (IPython) {'
685
687
686 Notebook.prototype.merge_cell_above = function () {
688 Notebook.prototype.merge_cell_above = function () {
687 // Todo: implement merging for other cell types.
689 // Todo: implement merging for other cell types.
688 var cell = this.selected_cell();
690 var cell = this.get_selected_cell();
689 var index = this.selected_index();
691 var index = this.get_selected_index();
690 if (index > 0) {
692 if (index > 0) {
691 upper_cell = this.cells()[index-1];
693 upper_cell = this.get_cell(index-1);
692 lower_cell = this.cells()[index];
694 lower_cell = this.get_cell(index);
693 if (upper_cell instanceof IPython.CodeCell && lower_cell instanceof IPython.CodeCell) {
695 if (upper_cell instanceof IPython.CodeCell && lower_cell instanceof IPython.CodeCell) {
694 upper_text = upper_cell.get_text();
696 upper_text = upper_cell.get_text();
695 lower_text = lower_cell.get_text();
697 lower_text = lower_cell.get_text();
@@ -702,11 +704,11 b' var IPython = (function (IPython) {'
702
704
703 Notebook.prototype.merge_cell_below = function () {
705 Notebook.prototype.merge_cell_below = function () {
704 // Todo: implement merging for other cell types.
706 // Todo: implement merging for other cell types.
705 var cell = this.selected_cell();
707 var cell = this.get_selected_cell();
706 var index = this.selected_index();
708 var index = this.get_selected_index();
707 if (index < this.ncells()-1) {
709 if (index < this.ncells()-1) {
708 upper_cell = this.cells()[index];
710 upper_cell = this.get_cell(index);
709 lower_cell = this.cells()[index+1];
711 lower_cell = this.get_cell(index+1);
710 if (upper_cell instanceof IPython.CodeCell && lower_cell instanceof IPython.CodeCell) {
712 if (upper_cell instanceof IPython.CodeCell && lower_cell instanceof IPython.CodeCell) {
711 upper_text = upper_cell.get_text();
713 upper_text = upper_cell.get_text();
712 lower_text = lower_cell.get_text();
714 lower_text = lower_cell.get_text();
@@ -720,21 +722,21 b' var IPython = (function (IPython) {'
720
722
721 Notebook.prototype.collapse = function (index) {
723 Notebook.prototype.collapse = function (index) {
722 var i = this.index_or_selected(index);
724 var i = this.index_or_selected(index);
723 this.cells()[i].collapse();
725 this.get_cell(i).collapse();
724 this.dirty = true;
726 this.dirty = true;
725 };
727 };
726
728
727
729
728 Notebook.prototype.expand = function (index) {
730 Notebook.prototype.expand = function (index) {
729 var i = this.index_or_selected(index);
731 var i = this.index_or_selected(index);
730 this.cells()[i].expand();
732 this.get_cell(i).expand();
731 this.dirty = true;
733 this.dirty = true;
732 };
734 };
733
735
734
736
735 Notebook.prototype.toggle_output = function (index) {
737 Notebook.prototype.toggle_output = function (index) {
736 var i = this.index_or_selected(index);
738 var i = this.index_or_selected(index);
737 this.cells()[i].toggle_output();
739 this.get_cell(i).toggle_output();
738 this.dirty = true;
740 this.dirty = true;
739 };
741 };
740
742
@@ -752,7 +754,7 b' var IPython = (function (IPython) {'
752 };
754 };
753
755
754 Notebook.prototype.set_autoindent = function (state) {
756 Notebook.prototype.set_autoindent = function (state) {
755 var cells = this.cells();
757 var cells = this.get_cells();
756 len = cells.length;
758 len = cells.length;
757 for (var i=0; i<len; i++) {
759 for (var i=0; i<len; i++) {
758 cells[i].set_autoindent(state);
760 cells[i].set_autoindent(state);
@@ -762,7 +764,7 b' var IPython = (function (IPython) {'
762
764
763 Notebook.prototype.clear_all_output = function () {
765 Notebook.prototype.clear_all_output = function () {
764 var ncells = this.ncells();
766 var ncells = this.ncells();
765 var cells = this.cells();
767 var cells = this.get_cells();
766 for (var i=0; i<ncells; i++) {
768 for (var i=0; i<ncells; i++) {
767 if (cells[i] instanceof IPython.CodeCell) {
769 if (cells[i] instanceof IPython.CodeCell) {
768 cells[i].clear_output(true,true,true);
770 cells[i].clear_output(true,true,true);
@@ -774,7 +776,7 b' var IPython = (function (IPython) {'
774 // Other cell functions: line numbers, ...
776 // Other cell functions: line numbers, ...
775
777
776 Notebook.prototype.cell_toggle_line_numbers = function() {
778 Notebook.prototype.cell_toggle_line_numbers = function() {
777 this.selected_cell().toggle_line_numbers();
779 this.get_selected_cell().toggle_line_numbers();
778 };
780 };
779
781
780 // Kernel related things
782 // Kernel related things
@@ -861,7 +863,7 b' var IPython = (function (IPython) {'
861 }
863 }
862 } else if (payload[i].source === 'IPython.zmq.zmqshell.ZMQInteractiveShell.set_next_input') {
864 } else if (payload[i].source === 'IPython.zmq.zmqshell.ZMQInteractiveShell.set_next_input') {
863 var index = this.find_cell_index(cell);
865 var index = this.find_cell_index(cell);
864 var new_cell = this.insert_code_cell_below(index);
866 var new_cell = this.insert_cell_below('code',index);
865 new_cell.set_text(payload[i].text);
867 new_cell.set_text(payload[i].text);
866 this.dirty = true;
868 this.dirty = true;
867 }
869 }
@@ -981,7 +983,7 b' var IPython = (function (IPython) {'
981 default_options = {terminal: false, add_new: true};
983 default_options = {terminal: false, add_new: true};
982 $.extend(default_options, options);
984 $.extend(default_options, options);
983 var that = this;
985 var that = this;
984 var cell = that.selected_cell();
986 var cell = that.get_selected_cell();
985 var cell_index = that.find_cell_index(cell);
987 var cell_index = that.find_cell_index(cell);
986 if (cell instanceof IPython.CodeCell) {
988 if (cell instanceof IPython.CodeCell) {
987 cell.clear_output(true, true, true);
989 cell.clear_output(true, true, true);
@@ -997,7 +999,7 b' var IPython = (function (IPython) {'
997 cell.select_all();
999 cell.select_all();
998 } else {
1000 } else {
999 if ((cell_index === (that.ncells()-1)) && default_options.add_new) {
1001 if ((cell_index === (that.ncells()-1)) && default_options.add_new) {
1000 that.insert_code_cell_below();
1002 that.insert_cell_below('code');
1001 // If we are adding a new cell at the end, scroll down to show it.
1003 // If we are adding a new cell at the end, scroll down to show it.
1002 that.scroll_to_bottom();
1004 that.scroll_to_bottom();
1003 } else {
1005 } else {
@@ -1012,7 +1014,7 b' var IPython = (function (IPython) {'
1012 var ncells = this.ncells();
1014 var ncells = this.ncells();
1013 for (var i=0; i<ncells; i++) {
1015 for (var i=0; i<ncells; i++) {
1014 this.select(i);
1016 this.select(i);
1015 this.execute_selected_cell({add_new:false});
1017 this.execute_get_selected_cell({add_new:false});
1016 };
1018 };
1017 this.scroll_to_bottom();
1019 this.scroll_to_bottom();
1018 };
1020 };
@@ -1068,23 +1070,15 b' var IPython = (function (IPython) {'
1068 var new_cell = null;
1070 var new_cell = null;
1069 for (i=0; i<ncells; i++) {
1071 for (i=0; i<ncells; i++) {
1070 cell_data = new_cells[i];
1072 cell_data = new_cells[i];
1071 if (cell_data.cell_type == 'code') {
1073 new_cell = this.insert_cell_below(cell_data.cell_type);
1072 new_cell = this.insert_code_cell_below();
1074 new_cell.fromJSON(cell_data);
1073 new_cell.fromJSON(cell_data);
1074 } else if (cell_data.cell_type === 'html') {
1075 new_cell = this.insert_html_cell_below();
1076 new_cell.fromJSON(cell_data);
1077 } else if (cell_data.cell_type === 'markdown') {
1078 new_cell = this.insert_markdown_cell_below();
1079 new_cell.fromJSON(cell_data);
1080 };
1081 };
1075 };
1082 };
1076 };
1083 };
1077 };
1084
1078
1085
1079
1086 Notebook.prototype.toJSON = function () {
1080 Notebook.prototype.toJSON = function () {
1087 var cells = this.cells();
1081 var cells = this.get_cells();
1088 var ncells = cells.length;
1082 var ncells = cells.length;
1089 cell_array = new Array(ncells);
1083 cell_array = new Array(ncells);
1090 for (var i=0; i<ncells; i++) {
1084 for (var i=0; i<ncells; i++) {
@@ -1161,7 +1155,7 b' var IPython = (function (IPython) {'
1161 var allowed = xhr.getResponseHeader('Allow');
1155 var allowed = xhr.getResponseHeader('Allow');
1162 this.fromJSON(data);
1156 this.fromJSON(data);
1163 if (this.ncells() === 0) {
1157 if (this.ncells() === 0) {
1164 this.insert_code_cell_below();
1158 this.insert_cell_below('code');
1165 };
1159 };
1166 IPython.save_widget.status_last_saved();
1160 IPython.save_widget.status_last_saved();
1167 IPython.save_widget.set_notebook_name(data.metadata.name);
1161 IPython.save_widget.set_notebook_name(data.metadata.name);
General Comments 0
You need to be logged in to leave comments. Login now