##// END OF EJS Templates
Merge pull request #6408 from minrk/new-cell-code...
Fernando Perez -
r17786:0f99ba31 merge
parent child Browse files
Show More
@@ -514,6 +514,12 b' define(['
514 }
514 }
515 };
515 };
516
516
517 var mergeopt = function(_class, options, overwrite){
518 options = options || {};
519 overwrite = overwrite || {};
520 return $.extend(true, {}, _class.options_default, options, overwrite);
521 };
522
517 var ajax_error_msg = function (jqXHR) {
523 var ajax_error_msg = function (jqXHR) {
518 // Return a JSON error message if there is one,
524 // Return a JSON error message if there is one,
519 // otherwise the basic HTTP status text.
525 // otherwise the basic HTTP status text.
@@ -552,6 +558,7 b' define(['
552 platform: platform,
558 platform: platform,
553 is_or_has : is_or_has,
559 is_or_has : is_or_has,
554 is_focused : is_focused,
560 is_focused : is_focused,
561 mergeopt: mergeopt,
555 ajax_error_msg : ajax_error_msg,
562 ajax_error_msg : ajax_error_msg,
556 log_ajax_error : log_ajax_error,
563 log_ajax_error : log_ajax_error,
557 };
564 };
@@ -42,7 +42,7 b' define(['
42 options = options || {};
42 options = options || {};
43 this.keyboard_manager = options.keyboard_manager;
43 this.keyboard_manager = options.keyboard_manager;
44 this.events = options.events;
44 this.events = options.events;
45 var config = this.mergeopt(Cell, options.config);
45 var config = utils.mergeopt(Cell, options.config);
46 // superclass default overwrite our default
46 // superclass default overwrite our default
47
47
48 this.placeholder = config.placeholder || '';
48 this.placeholder = config.placeholder || '';
@@ -94,12 +94,6 b' define(['
94 Cell.options_default.cm_config.dragDrop = false;
94 Cell.options_default.cm_config.dragDrop = false;
95 }
95 }
96
96
97 Cell.prototype.mergeopt = function(_class, options, overwrite){
98 options = options || {};
99 overwrite = overwrite || {};
100 return $.extend(true, {}, _class.options_default, options, overwrite);
101 };
102
103 /**
97 /**
104 * Empty. Subclasses must implement create_element.
98 * Empty. Subclasses must implement create_element.
105 * This should contain all the code to create the DOM element in notebook
99 * This should contain all the code to create the DOM element in notebook
@@ -76,7 +76,7 b' define(['
76 onKeyEvent: $.proxy(this.handle_keyevent,this)
76 onKeyEvent: $.proxy(this.handle_keyevent,this)
77 };
77 };
78
78
79 var config = this.mergeopt(CodeCell, this.config, {cm_config: cm_overwrite_options});
79 var config = utils.mergeopt(CodeCell, this.config, {cm_config: cm_overwrite_options});
80 Cell.apply(this,[{
80 Cell.apply(this,[{
81 config: config,
81 config: config,
82 keyboard_manager: options.keyboard_manager,
82 keyboard_manager: options.keyboard_manager,
@@ -53,7 +53,7 b' define(['
53 // base_url : string
53 // base_url : string
54 // notebook_path : string
54 // notebook_path : string
55 // notebook_name : string
55 // notebook_name : string
56 this.config = options.config || {};
56 this.config = utils.mergeopt(Notebook, options.config);
57 this.base_url = options.base_url;
57 this.base_url = options.base_url;
58 this.notebook_path = options.notebook_path;
58 this.notebook_path = options.notebook_path;
59 this.notebook_name = options.notebook_name;
59 this.notebook_name = options.notebook_name;
@@ -63,6 +63,7 b' define(['
63 this.tooltip = new tooltip.Tooltip(this.events);
63 this.tooltip = new tooltip.Tooltip(this.events);
64 this.ws_url = options.ws_url;
64 this.ws_url = options.ws_url;
65 this._session_starting = false;
65 this._session_starting = false;
66 this.default_cell_type = this.config.default_cell_type || 'code';
66 // default_kernel_name is a temporary measure while we implement proper
67 // default_kernel_name is a temporary measure while we implement proper
67 // kernel selection and delayed start. Do not rely on it.
68 // kernel selection and delayed start. Do not rely on it.
68 this.default_kernel_name = 'python';
69 this.default_kernel_name = 'python';
@@ -134,6 +135,14 b' define(['
134 rawcell_celltoolbar.register(this);
135 rawcell_celltoolbar.register(this);
135 slideshow_celltoolbar.register(this);
136 slideshow_celltoolbar.register(this);
136 };
137 };
138
139 Notebook.options_default = {
140 // can be any cell type, or the special values of
141 // 'above', 'below', or 'selected' to get the value from another cell.
142 Notebook: {
143 default_cell_type: 'code',
144 }
145 };
137
146
138
147
139 /**
148 /**
@@ -835,10 +844,25 b' define(['
835 Notebook.prototype.insert_cell_at_index = function(type, index){
844 Notebook.prototype.insert_cell_at_index = function(type, index){
836
845
837 var ncells = this.ncells();
846 var ncells = this.ncells();
838 index = Math.min(index,ncells);
847 index = Math.min(index, ncells);
839 index = Math.max(index,0);
848 index = Math.max(index, 0);
840 var cell = null;
849 var cell = null;
841 type = type || this.get_selected_cell().cell_type;
850 type = type || this.default_cell_type;
851 if (type === 'above') {
852 if (index > 0) {
853 type = this.get_cell(index-1).cell_type;
854 } else {
855 type = 'code';
856 }
857 } else if (type === 'below') {
858 if (index < ncells) {
859 type = this.get_cell(index).cell_type;
860 } else {
861 type = 'code';
862 }
863 } else if (type === 'selected') {
864 type = this.get_selected_cell().cell_type;
865 }
842
866
843 if (ncells === 0 || this.is_valid_cell_index(index) || index === ncells) {
867 if (ncells === 0 || this.is_valid_cell_index(index) || index === ncells) {
844 var cell_options = {
868 var cell_options = {
@@ -3,13 +3,14 b''
3
3
4 define([
4 define([
5 'base/js/namespace',
5 'base/js/namespace',
6 'base/js/utils',
6 'jquery',
7 'jquery',
7 'notebook/js/cell',
8 'notebook/js/cell',
8 'base/js/security',
9 'base/js/security',
9 'notebook/js/mathjaxutils',
10 'notebook/js/mathjaxutils',
10 'notebook/js/celltoolbar',
11 'notebook/js/celltoolbar',
11 'components/marked/lib/marked',
12 'components/marked/lib/marked',
12 ], function(IPython, $, cell, security, mathjaxutils, celltoolbar, marked) {
13 ], function(IPython, utils, $, cell, security, mathjaxutils, celltoolbar, marked) {
13 "use strict";
14 "use strict";
14 var Cell = cell.Cell;
15 var Cell = cell.Cell;
15
16
@@ -40,7 +41,7 b' define(['
40 var cm_overwrite_options = {
41 var cm_overwrite_options = {
41 onKeyEvent: $.proxy(this.handle_keyevent,this)
42 onKeyEvent: $.proxy(this.handle_keyevent,this)
42 };
43 };
43 var config = this.mergeopt(TextCell, this.config, {cm_config:cm_overwrite_options});
44 var config = utils.mergeopt(TextCell, this.config, {cm_config:cm_overwrite_options});
44 Cell.apply(this, [{
45 Cell.apply(this, [{
45 config: config,
46 config: config,
46 keyboard_manager: options.keyboard_manager,
47 keyboard_manager: options.keyboard_manager,
@@ -226,7 +227,7 b' define(['
226 // keyboard_manager: KeyboardManager instance
227 // keyboard_manager: KeyboardManager instance
227 // notebook: Notebook instance
228 // notebook: Notebook instance
228 options = options || {};
229 options = options || {};
229 var config = this.mergeopt(MarkdownCell, options.config);
230 var config = utils.mergeopt(MarkdownCell, options.config);
230 TextCell.apply(this, [$.extend({}, options, {config: config})]);
231 TextCell.apply(this, [$.extend({}, options, {config: config})]);
231
232
232 this.cell_type = 'markdown';
233 this.cell_type = 'markdown';
@@ -279,7 +280,7 b' define(['
279 // keyboard_manager: KeyboardManager instance
280 // keyboard_manager: KeyboardManager instance
280 // notebook: Notebook instance
281 // notebook: Notebook instance
281 options = options || {};
282 options = options || {};
282 var config = this.mergeopt(RawCell, options.config);
283 var config = utils.mergeopt(RawCell, options.config);
283 TextCell.apply(this, [$.extend({}, options, {config: config})]);
284 TextCell.apply(this, [$.extend({}, options, {config: config})]);
284
285
285 // RawCell should always hide its rendered div
286 // RawCell should always hide its rendered div
@@ -339,7 +340,7 b' define(['
339 // keyboard_manager: KeyboardManager instance
340 // keyboard_manager: KeyboardManager instance
340 // notebook: Notebook instance
341 // notebook: Notebook instance
341 options = options || {};
342 options = options || {};
342 var config = this.mergeopt(HeadingCell, options.config);
343 var config = utils.mergeopt(HeadingCell, options.config);
343 TextCell.apply(this, [$.extend({}, options, {config: config})]);
344 TextCell.apply(this, [$.extend({}, options, {config: config})]);
344
345
345 this.level = 1;
346 this.level = 1;
@@ -12,31 +12,66 b' casper.notebook_test(function () {'
12 var c = 'print("c")';
12 var c = 'print("c")';
13 index = this.append_cell(c);
13 index = this.append_cell(c);
14 this.execute_cell_then(index);
14 this.execute_cell_then(index);
15
15
16 this.thenEvaluate(function() {
17 IPython.notebook.default_cell_type = 'code';
18 });
19
16 this.then(function () {
20 this.then(function () {
17 // Cell insertion
21 // Cell insertion
18 this.select_cell(2);
22 this.select_cell(2);
23 this.trigger_keydown('m'); // Make it markdown
19 this.trigger_keydown('a'); // Creates one cell
24 this.trigger_keydown('a'); // Creates one cell
20 this.test.assertEquals(this.get_cell_text(2), '', 'a; New cell 2 text is empty');
25 this.test.assertEquals(this.get_cell_text(2), '', 'a; New cell 2 text is empty');
21 this.test.assertEquals(this.get_cell(2).cell_type, 'code', 'a; inserts a code cell when on code cell');
26 this.test.assertEquals(this.get_cell(2).cell_type, 'code', 'a; inserts a code cell');
22 this.validate_notebook_state('a', 'command', 2);
27 this.validate_notebook_state('a', 'command', 2);
23 this.trigger_keydown('b'); // Creates one cell
28 this.trigger_keydown('b'); // Creates one cell
24 this.test.assertEquals(this.get_cell_text(2), '', 'b; Cell 2 text is still empty');
29 this.test.assertEquals(this.get_cell_text(2), '', 'b; Cell 2 text is still empty');
25 this.test.assertEquals(this.get_cell_text(3), '', 'b; New cell 3 text is empty');
30 this.test.assertEquals(this.get_cell_text(3), '', 'b; New cell 3 text is empty');
26 this.test.assertEquals(this.get_cell(3).cell_type, 'code', 'b; inserts a code cell when on code cell');
31 this.test.assertEquals(this.get_cell(3).cell_type, 'code', 'b; inserts a code cell');
27 this.validate_notebook_state('b', 'command', 3);
32 this.validate_notebook_state('b', 'command', 3);
28 });
33 });
34
35 this.thenEvaluate(function() {
36 IPython.notebook.default_cell_type = 'selected';
37 });
38
29 this.then(function () {
39 this.then(function () {
30 // Cell insertion
31 this.select_cell(2);
40 this.select_cell(2);
32 this.trigger_keydown('m'); // switch it to markdown for the next test
41 this.trigger_keydown('m'); // switch it to markdown for the next test
33 this.trigger_keydown('a'); // Creates one cell
42 this.test.assertEquals(this.get_cell(2).cell_type, 'markdown', 'test cell is markdown');
34 this.test.assertEquals(this.get_cell_text(2), '', 'a; New cell 2 text is empty');
43 this.trigger_keydown('a'); // new cell above
35 this.test.assertEquals(this.get_cell(2).cell_type, 'markdown', 'a; inserts a markdown cell when on markdown cell');
44 this.test.assertEquals(this.get_cell(2).cell_type, 'markdown', 'a; inserts a markdown cell when markdown selected');
36 this.validate_notebook_state('a', 'command', 2);
45 this.trigger_keydown('b'); // new cell below
37 this.trigger_keydown('b'); // Creates one cell
46 this.test.assertEquals(this.get_cell(3).cell_type, 'markdown', 'b; inserts a markdown cell when markdown selected');
38 this.test.assertEquals(this.get_cell_text(2), '', 'b; Cell 2 text is still empty');
47 });
39 this.test.assertEquals(this.get_cell(3).cell_type, 'markdown', 'b; inserts a markdown cell when on markdown cell');
48
40 this.validate_notebook_state('b', 'command', 3);
49 this.thenEvaluate(function() {
50 IPython.notebook.default_cell_type = 'above';
51 });
52
53 this.then(function () {
54 this.select_cell(2);
55 this.trigger_keydown('1'); // switch it to heading for the next test
56 this.test.assertEquals(this.get_cell(2).cell_type, 'heading', 'test cell is heading');
57 this.trigger_keydown('b'); // new cell below
58 this.test.assertEquals(this.get_cell(3).cell_type, 'heading', 'b; inserts a heading cell below heading cell');
59 this.trigger_keydown('a'); // new cell above
60 this.test.assertEquals(this.get_cell(3).cell_type, 'heading', 'a; inserts a heading cell below heading cell');
61 });
62
63 this.thenEvaluate(function() {
64 IPython.notebook.default_cell_type = 'below';
65 });
66
67 this.then(function () {
68 this.select_cell(2);
69 this.trigger_keydown('r'); // switch it to markdown for the next test
70 this.test.assertEquals(this.get_cell(2).cell_type, 'raw', 'test cell is raw');
71 this.trigger_keydown('a'); // new cell above
72 this.test.assertEquals(this.get_cell(2).cell_type, 'raw', 'a; inserts a raw cell above raw cell');
73 this.trigger_keydown('y'); // switch it to code for the next test
74 this.trigger_keydown('b'); // new cell below
75 this.test.assertEquals(this.get_cell(3).cell_type, 'raw', 'b; inserts a raw cell above raw cell');
41 });
76 });
42 });
77 });
General Comments 0
You need to be logged in to leave comments. Login now