##// END OF EJS Templates
Added up/down and markdown tests
Jonathan Frederic -
Show More
@@ -1,309 +1,370 b''
1 // Test the notebook dual mode feature.
1 // Test the notebook dual mode feature.
2
2
3 // Test
3 // Test
4 casper.notebook_test(function () {
4 casper.notebook_test(function () {
5 var index = this.append_cell('print("a")');
5 var index = this.append_cell('print("a")');
6 this.execute_cell_then(index);
6 this.execute_cell_then(index);
7 index = this.append_cell('print("b")');
7 index = this.append_cell('print("b")');
8 this.execute_cell_then(index);
8 this.execute_cell_then(index);
9 index = this.append_cell('print("c")');
9 index = this.append_cell('print("c")');
10 this.execute_cell_then(index);
10 this.execute_cell_then(index);
11
11
12 this.then(function () {
12 this.then(function () {
13 this.validate_state('initial state', 'edit', 0);
13 this.validate_state('initial state', 'edit', 0);
14 this.trigger_keydown('esc');
14 this.trigger_keydown('esc');
15 this.validate_state('esc', 'command', 0);
15 this.validate_state('esc', 'command', 0);
16 this.trigger_keydown('down');
16 this.trigger_keydown('down');
17 this.validate_state('down', 'command', 1);
17 this.validate_state('down', 'command', 1);
18 this.trigger_keydown('enter');
18 this.trigger_keydown('enter');
19 this.validate_state('enter', 'edit', 1);
19 this.validate_state('enter', 'edit', 1);
20 this.trigger_keydown('j');
20 this.trigger_keydown('j');
21 this.validate_state('j in edit mode', 'edit', 1);
21 this.validate_state('j in edit mode', 'edit', 1);
22 this.trigger_keydown('esc');
22 this.trigger_keydown('esc');
23 this.validate_state('esc', 'command', 1);
23 this.validate_state('esc', 'command', 1);
24 this.trigger_keydown('j');
24 this.trigger_keydown('j');
25 this.validate_state('j in command mode', 'command', 2);
25 this.validate_state('j in command mode', 'command', 2);
26 this.click_cell(0);
26 this.click_cell_editor(0);
27 this.validate_state('click cell 0', 'edit', 0);
27 this.validate_state('click cell 0', 'edit', 0);
28 this.click_cell(3);
28 this.click_cell_editor(3);
29 this.validate_state('click cell 3', 'edit', 3);
29 this.validate_state('click cell 3', 'edit', 3);
30 this.trigger_keydown('esc');
30 this.trigger_keydown('esc');
31 this.validate_state('esc', 'command', 3);
31 this.validate_state('esc', 'command', 3);
32
32
33 // Open keyboard help
33 // Open keyboard help
34 this.evaluate(function(){
34 this.evaluate(function(){
35 $('#keyboard_shortcuts a').click();
35 $('#keyboard_shortcuts a').click();
36 }, {});
36 }, {});
37
37
38 this.trigger_keydown('k');
38 this.trigger_keydown('k');
39 this.validate_state('k in command mode while keyboard help is up', 'command', 3);
39 this.validate_state('k in command mode while keyboard help is up', 'command', 3);
40
40
41 // Close keyboard help
41 // Close keyboard help
42 this.evaluate(function(){
42 this.evaluate(function(){
43 $('div.modal button.close').click();
43 $('div.modal button.close').click();
44 }, {});
44 }, {});
45
45
46 this.trigger_keydown('k');
46 this.trigger_keydown('k');
47 this.validate_state('k in command mode', 'command', 2);
47 this.validate_state('k in command mode', 'command', 2);
48 this.click_cell(0);
48 this.click_cell_editor(0);
49 this.validate_state('click cell 0', 'edit', 0);
49 this.validate_state('click cell 0', 'edit', 0);
50 this.focus_notebook();
50 this.focus_notebook();
51 this.validate_state('focus #notebook', 'command', 0);
51 this.validate_state('focus #notebook', 'command', 0);
52 this.click_cell(0);
52 this.click_cell_editor(0);
53 this.validate_state('click cell 0', 'edit', 0);
53 this.validate_state('click cell 0', 'edit', 0);
54 this.focus_notebook();
54 this.focus_notebook();
55 this.validate_state('focus #notebook', 'command', 0);
55 this.validate_state('focus #notebook', 'command', 0);
56 this.click_cell(3);
56 this.click_cell_editor(3);
57 this.validate_state('click cell 3', 'edit', 3);
57 this.validate_state('click cell 3', 'edit', 3);
58
58
59 // shift+enter tests.
59 // shift+enter tests.
60 // last cell in notebook
60 // last cell in notebook
61 var base_index = 3;
61 var base_index = 3;
62 this.trigger_keydown('shift+enter'); // Creates one cell
62 this.trigger_keydown('shift+enter'); // Creates one cell
63 this.validate_state('shift+enter (no cell below)', 'edit', base_index + 1);
63 this.validate_state('shift+enter (no cell below)', 'edit', base_index + 1);
64 // not last cell in notebook & starts in edit mode
64 // not last cell in notebook & starts in edit mode
65 this.click_cell(base_index);
65 this.click_cell_editor(base_index);
66 this.validate_state('click cell ' + base_index, 'edit', base_index);
66 this.validate_state('click cell ' + base_index, 'edit', base_index);
67 this.trigger_keydown('shift+enter');
67 this.trigger_keydown('shift+enter');
68 this.validate_state('shift+enter (cell exists below)', 'command', base_index + 1);
68 this.validate_state('shift+enter (cell exists below)', 'command', base_index + 1);
69 // starts in command mode
69 // starts in command mode
70 this.trigger_keydown('k');
70 this.trigger_keydown('k');
71 this.validate_state('k in comand mode', 'command', base_index);
71 this.validate_state('k in comand mode', 'command', base_index);
72 this.trigger_keydown('shift+enter');
72 this.trigger_keydown('shift+enter');
73 this.validate_state('shift+enter (start in command mode)', 'command', base_index + 1);
73 this.validate_state('shift+enter (start in command mode)', 'command', base_index + 1);
74
74
75 // ctrl+enter tests.
75 // ctrl+enter tests.
76 // last cell in notebook
76 // last cell in notebook
77 base_index++;
77 base_index++;
78 this.trigger_keydown('ctrl+enter');
78 this.trigger_keydown('ctrl+enter');
79 this.validate_state('ctrl+enter (no cell below)', 'command', base_index);
79 this.validate_state('ctrl+enter (no cell below)', 'command', base_index);
80 // not last cell in notebook & starts in edit mode
80 // not last cell in notebook & starts in edit mode
81 this.click_cell(base_index-1);
81 this.click_cell_editor(base_index-1);
82 this.validate_state('click cell ' + (base_index-1), 'edit', base_index-1);
82 this.validate_state('click cell ' + (base_index-1), 'edit', base_index-1);
83 this.trigger_keydown('ctrl+enter');
83 this.trigger_keydown('ctrl+enter');
84 this.validate_state('ctrl+enter (cell exists below)', 'command', base_index-1);
84 this.validate_state('ctrl+enter (cell exists below)', 'command', base_index-1);
85 // starts in command mode
85 // starts in command mode
86 this.trigger_keydown('j');
86 this.trigger_keydown('j');
87 this.validate_state('j in comand mode', 'command', base_index);
87 this.validate_state('j in comand mode', 'command', base_index);
88 this.trigger_keydown('ctrl+enter');
88 this.trigger_keydown('ctrl+enter');
89 this.validate_state('ctrl+enter (start in command mode)', 'command', base_index);
89 this.validate_state('ctrl+enter (start in command mode)', 'command', base_index);
90
90
91 // alt+enter tests.
91 // alt+enter tests.
92 // last cell in notebook
92 // last cell in notebook
93 this.trigger_keydown('alt+enter'); // Creates one cell
93 this.trigger_keydown('alt+enter'); // Creates one cell
94 this.validate_state('alt+enter (no cell below)', 'edit', base_index + 1);
94 this.validate_state('alt+enter (no cell below)', 'edit', base_index + 1);
95 // not last cell in notebook & starts in edit mode
95 // not last cell in notebook & starts in edit mode
96 this.click_cell(base_index);
96 this.click_cell_editor(base_index);
97 this.validate_state('click cell ' + base_index, 'edit', base_index);
97 this.validate_state('click cell ' + base_index, 'edit', base_index);
98 this.trigger_keydown('alt+enter'); // Creates one cell
98 this.trigger_keydown('alt+enter'); // Creates one cell
99 this.validate_state('alt+enter (cell exists below)', 'edit', base_index + 1);
99 this.validate_state('alt+enter (cell exists below)', 'edit', base_index + 1);
100 // starts in command mode
100 // starts in command mode
101 this.trigger_keydown('esc');
101 this.trigger_keydown('esc', 'k');
102 this.trigger_keydown('k');
103 this.validate_state('k in comand mode', 'command', base_index);
102 this.validate_state('k in comand mode', 'command', base_index);
104 this.trigger_keydown('alt+enter'); // Creates one cell
103 this.trigger_keydown('alt+enter'); // Creates one cell
105 this.validate_state('alt+enter (start in command mode)', 'edit', base_index + 1);
104 this.validate_state('alt+enter (start in command mode)', 'edit', base_index + 1);
106
105
107 // Notebook will now have 8 cells, the index of the last cell will be 7.
106 // Notebook will now have 8 cells, the index of the last cell will be 7.
108 this.test.assertEquals(this.get_cells().length, 8, '*-enter commands added cells where needed.');
107 this.test.assertEquals(this.get_cells().length, 8, '*-enter commands added cells where needed.');
109 this.click_cell(7);
108 this.select_cell(7);
110 this.trigger_keydown('esc');
111 this.validate_state('click cell ' + 7 + ' and esc', 'command', 7);
109 this.validate_state('click cell ' + 7 + ' and esc', 'command', 7);
112
110
113 this.trigger_keydown('r');
111 this.trigger_keydown('r');
114 this.test.assertEquals(this.get_cell(7).cell_type, 'raw', 'r; cell is raw');
112 this.test.assertEquals(this.get_cell(7).cell_type, 'raw', 'r; cell is raw');
115 this.trigger_keydown('1');
113 this.trigger_keydown('1');
116 this.test.assertEquals(this.get_cell(7).cell_type, 'heading', '1; cell is heading');
114 this.test.assertEquals(this.get_cell(7).cell_type, 'heading', '1; cell is heading');
117 this.test.assertEquals(this.get_cell(7).level, 1, '1; cell is level 1 heading');
115 this.test.assertEquals(this.get_cell(7).level, 1, '1; cell is level 1 heading');
118 this.trigger_keydown('2');
116 this.trigger_keydown('2');
119 this.test.assertEquals(this.get_cell(7).level, 2, '2; cell is level 2 heading');
117 this.test.assertEquals(this.get_cell(7).level, 2, '2; cell is level 2 heading');
120 this.trigger_keydown('3');
118 this.trigger_keydown('3');
121 this.test.assertEquals(this.get_cell(7).level, 3, '3; cell is level 3 heading');
119 this.test.assertEquals(this.get_cell(7).level, 3, '3; cell is level 3 heading');
122 this.trigger_keydown('4');
120 this.trigger_keydown('4');
123 this.test.assertEquals(this.get_cell(7).level, 4, '4; cell is level 4 heading');
121 this.test.assertEquals(this.get_cell(7).level, 4, '4; cell is level 4 heading');
124 this.trigger_keydown('5');
122 this.trigger_keydown('5');
125 this.test.assertEquals(this.get_cell(7).level, 5, '5; cell is level 5 heading');
123 this.test.assertEquals(this.get_cell(7).level, 5, '5; cell is level 5 heading');
126 this.trigger_keydown('6');
124 this.trigger_keydown('6');
127 this.test.assertEquals(this.get_cell(7).level, 6, '6; cell is level 6 heading');
125 this.test.assertEquals(this.get_cell(7).level, 6, '6; cell is level 6 heading');
128 this.trigger_keydown('m');
126 this.trigger_keydown('m');
129 this.test.assertEquals(this.get_cell(7).cell_type, 'markdown', 'm; cell is markdown');
127 this.test.assertEquals(this.get_cell(7).cell_type, 'markdown', 'm; cell is markdown');
130 this.trigger_keydown('y');
128 this.trigger_keydown('y');
131 this.test.assertEquals(this.get_cell(7).cell_type, 'code', 'y; cell is code');
129 this.test.assertEquals(this.get_cell(7).cell_type, 'code', 'y; cell is code');
132
130
133 this.trigger_keydown('d');
131 this.trigger_keydown('d', 'd');
134 this.trigger_keydown('d');
135 this.test.assertEquals(this.get_cells().length, 7, 'dd actually deletes a cell');
132 this.test.assertEquals(this.get_cells().length, 7, 'dd actually deletes a cell');
136 this.validate_state('dd', 'command', 6);
133 this.validate_state('dd', 'command', 6);
137
134
138 // Make sure that if the time between d presses is too long
135 // Make sure that if the time between d presses is too long
139 this.trigger_keydown('d');
136 this.trigger_keydown('d');
140 });
137 });
141 this.wait(1000);
138 this.wait(1000);
142 this.then(function () {
139 this.then(function () {
143 this.trigger_keydown('d');
140 this.trigger_keydown('d');
144
141
145 this.test.assertEquals(this.get_cells().length, 7, "d, 1 second wait, d doesn't delete a cell");
142 this.test.assertEquals(this.get_cells().length, 7, "d, 1 second wait, d doesn't delete a cell");
146 this.validate_state('d, 1 second wait, d', 'command', 6);
143 this.validate_state('d, 1 second wait, d', 'command', 6);
144 this.trigger_keydown('j');
145 this.validate_state('j at end of notebook', 'command', 6);
146 this.trigger_keydown('down');
147 this.validate_state('down at end of notebook', 'command', 6);
148 this.trigger_keydown('up');
149 this.validate_state('up', 'command', 5);
150 this.select_cell(0);
151 this.validate_state('select 0', 'command', 0);
152 this.trigger_keydown('k');
153 this.validate_state('k at top of notebook', 'command', 0);
154 this.trigger_keydown('up');
155 this.validate_state('up at top of notebook', 'command', 0);
156 this.trigger_keydown('down');
157 this.validate_state('down', 'command', 1);
158
159 this.click_cell_editor(6);
160 this.validate_state('click cell 6', 'edit', 6);
161 this.trigger_keydown('down');
162 this.validate_state('down at end of notebook', 'edit', 6);
163 this.trigger_keydown('up');
164 this.validate_state('up', 'edit', 5);
165 this.click_cell_editor(0);
166 this.validate_state('click 0', 'edit', 0);
167 this.trigger_keydown('up');
168 this.validate_state('up at top of notebook', 'edit', 0);
169 this.trigger_keydown('down');
170 this.validate_state('down', 'edit', 1);
171
172 this.select_cell(6);
173 this.validate_state('select 6', 'command', 6);
174 this.trigger_keydown('m');
175 this.test.assertEquals(this.get_cell(6).cell_type, 'markdown', 'm; cell is markdown');
176 this.test.assertEquals(this.get_cell(6).rendered, true, 'm; cell is rendered');
177 this.trigger_keydown('enter');
178 this.test.assertEquals(this.get_cell(6).rendered, false, 'enter; cell is unrendered');
179 this.validate_state('enter', 'edit', 6);
180 this.trigger_keydown('ctrl+enter');
181 this.test.assertEquals(this.get_cell(6).rendered, true, 'enter; cell is rendered');
182 this.validate_state('enter', 'command', 6);
183 this.trigger_keydown('enter');
184 this.test.assertEquals(this.get_cell(6).rendered, false, 'enter; cell is unrendered');
185 this.select_cell(5);
186 this.test.assertEquals(this.get_cell(6).rendered, false, 'select 5; cell 6 is still unrendered');
187 this.validate_state('select 5', 'command', 5);
188 this.select_cell(6);
189 this.validate_state('select 6', 'command', 5);
190 this.trigger_keydown('ctrl+enter');
191 this.test.assertEquals(this.get_cell(6).rendered, true, 'enter; cell is rendered');
192 this.select_cell(5);
193 this.validate_state('select 5', 'command', 5);
194 this.trigger_keydown('shift+enter');
195 this.validate_state('shift+enter', 'command', 6);
196 this.test.assertEquals(this.get_cell(6).rendered, true, 'enter; cell is rendered');
197 this.trigger_keydown('shift+enter'); // Creates one cell
198 this.validate_state('shift+enter', 'edit', 7);
199 this.test.assertEquals(this.get_cell(6).rendered, true, 'enter; cell is rendered');
200
147
201
148 });
202 });
149
203
150 // Utility functions.
204 // Utility functions.
151 this.validate_state = function(message, mode, cell_index) {
205 this.validate_state = function(message, mode, cell_index) {
152 // General tests.
206 // General tests.
153 this.test.assertEquals(this.get_keyboard_mode(), this.get_notebook_mode(),
207 this.test.assertEquals(this.get_keyboard_mode(), this.get_notebook_mode(),
154 message + '; keyboard and notebook modes match');
208 message + '; keyboard and notebook modes match');
155 // Is codemirror focused appropriately?
209 // Is codemirror focused appropriately?
156 this.test.assert(this.is_editor_focus_valid(), message + '; cell editor focused appropriately');
210 this.test.assert(this.is_editor_focus_valid(), message + '; cell editor focused appropriately');
157 // Is the selected cell the only cell that is selected?
211 // Is the selected cell the only cell that is selected?
158 if (cell_index!==undefined) {
212 if (cell_index!==undefined) {
159 this.test.assert(this.is_only_cell_selected(cell_index),
213 this.test.assert(this.is_only_cell_selected(cell_index),
160 message + '; cell ' + cell_index + ' is the only cell selected');
214 message + '; cell ' + cell_index + ' is the only cell selected');
161 }
215 }
162
216
163 // Mode specific tests.
217 // Mode specific tests.
164 if (mode==='command') {
218 if (mode==='command') {
165 // Are the notebook and keyboard manager in command mode?
219 // Are the notebook and keyboard manager in command mode?
166 this.test.assertEquals(this.get_keyboard_mode(), 'command',
220 this.test.assertEquals(this.get_keyboard_mode(), 'command',
167 message + '; in command mode');
221 message + '; in command mode');
168 // Make sure there isn't a single cell in edit mode.
222 // Make sure there isn't a single cell in edit mode.
169 this.test.assert(this.is_only_cell_edit(null),
223 this.test.assert(this.is_only_cell_edit(null),
170 message + '; all cells in command mode');
224 message + '; all cells in command mode');
171
225
172 } else if (mode==='edit') {
226 } else if (mode==='edit') {
173 // Are the notebook and keyboard manager in edit mode?
227 // Are the notebook and keyboard manager in edit mode?
174 this.test.assertEquals(this.get_keyboard_mode(), 'edit',
228 this.test.assertEquals(this.get_keyboard_mode(), 'edit',
175 message + '; in edit mode');
229 message + '; in edit mode');
176 // Is the specified cell the only cell in edit mode?
230 // Is the specified cell the only cell in edit mode?
177 if (cell_index!==undefined) {
231 if (cell_index!==undefined) {
178 this.test.assert(this.is_only_cell_edit(cell_index),
232 this.test.assert(this.is_only_cell_edit(cell_index),
179 message + '; cell ' + cell_index + ' is the only cell in edit mode');
233 message + '; cell ' + cell_index + ' is the only cell in edit mode');
180 }
234 }
181
235
182 } else {
236 } else {
183 this.test.assert(false, message + '; ' + mode + ' is an unknown mode');
237 this.test.assert(false, message + '; ' + mode + ' is an unknown mode');
184 }
238 }
185 };
239 };
186
240
187 this.is_editor_focus_valid = function() {
241 this.is_editor_focus_valid = function() {
188 var cells = this.get_cells();
242 var cells = this.get_cells();
189 for (var i = 0; i < cells.length; i++) {
243 for (var i = 0; i < cells.length; i++) {
190 if (!this.is_cell_editor_focus_valid(i)) {
244 if (!this.is_cell_editor_focus_valid(i)) {
191 return false;
245 return false;
192 }
246 }
193 }
247 }
194 return true;
248 return true;
195 };
249 };
196
250
197 this.is_cell_editor_focus_valid = function(index) {
251 this.is_cell_editor_focus_valid = function(index) {
198 var cell = this.get_cell(index);
252 var cell = this.get_cell(index);
199 if (cell) {
253 if (cell) {
200 if (cell.mode == 'edit') {
254 if (cell.mode == 'edit') {
201 return this.is_cell_editor_focused(index);
255 return this.is_cell_editor_focused(index);
202 } else {
256 } else {
203 return !this.is_cell_editor_focused(index);
257 return !this.is_cell_editor_focused(index);
204 }
258 }
205 }
259 }
206 return true;
260 return true;
207 };
261 };
208
262
209
263
210 /* TODO: MOVE EVERYTHING BELOW THIS LINE INTO THE BASE (utils.js) */
264 /* TODO: MOVE EVERYTHING BELOW THIS LINE INTO THE BASE (utils.js) */
211
265
212
266
267 this.select_cell = function (index) {
268 this.evaluate(function (i) {
269 IPython.notebook.select(i);
270 }, {i: index});
271 };
213
272
214 this.click_cell = function(index) {
273 this.click_cell_editor = function(index) {
215 // Code Mirror does not play nicely with emulated brower events.
274 // Code Mirror does not play nicely with emulated brower events.
216 // Instead of trying to emulate a click, here we run code similar to
275 // Instead of trying to emulate a click, here we run code similar to
217 // the code used in Code Mirror that handles the mousedown event on a
276 // the code used in Code Mirror that handles the mousedown event on a
218 // region of codemirror that the user can focus.
277 // region of codemirror that the user can focus.
219 this.evaluate(function (i) {
278 this.evaluate(function (i) {
220 cm = IPython.notebook.get_cell(i).code_mirror;
279 cm = IPython.notebook.get_cell(i).code_mirror;
221 if (cm.options.readOnly != "nocursor" && (document.activeElement != cm.display.input))
280 if (cm.options.readOnly != "nocursor" && (document.activeElement != cm.display.input))
222 cm.display.input.focus();
281 cm.display.input.focus();
223 }, {i: index});
282 }, {i: index});
224 };
283 };
225
284
226 this.focus_notebook = function() {
285 this.focus_notebook = function() {
227 this.evaluate(function (){
286 this.evaluate(function (){
228 $('#notebook').focus();
287 $('#notebook').focus();
229 }, {});
288 }, {});
230 };
289 };
231
290
232 this.trigger_keydown = function(key) {
291 this.trigger_keydown = function() {
233 this.evaluate(function (k) {
292 for (var i = 0; i < arguments.length; i++) {
234 IPython.keyboard.trigger_keydown(k);
293 this.evaluate(function (k) {
235 }, {k: key});
294 IPython.keyboard.trigger_keydown(k);
295 }, {k: arguments[i]});
296 }
236 };
297 };
237
298
238 this.get_keyboard_mode = function() {
299 this.get_keyboard_mode = function() {
239 return this.evaluate(function() {
300 return this.evaluate(function() {
240 return IPython.keyboard_manager.mode;
301 return IPython.keyboard_manager.mode;
241 }, {});
302 }, {});
242 };
303 };
243
304
244 this.get_notebook_mode = function() {
305 this.get_notebook_mode = function() {
245 return this.evaluate(function() {
306 return this.evaluate(function() {
246 return IPython.notebook.mode;
307 return IPython.notebook.mode;
247 }, {});
308 }, {});
248 };
309 };
249
310
250 this.get_cells = function() {
311 this.get_cells = function() {
251 return this.evaluate(function() {
312 return this.evaluate(function() {
252 return IPython.notebook.get_cells();
313 return IPython.notebook.get_cells();
253 }, {});
314 }, {});
254 };
315 };
255
316
256 this.get_cell = function(index) {
317 this.get_cell = function(index) {
257 return this.evaluate(function(i) {
318 return this.evaluate(function(i) {
258 var cell = IPython.notebook.get_cell(i);
319 var cell = IPython.notebook.get_cell(i);
259 if (cell) {
320 if (cell) {
260 return cell;
321 return cell;
261 }
322 }
262 return null;
323 return null;
263 }, {i : index});
324 }, {i : index});
264 };
325 };
265
326
266 this.is_cell_editor_focused = function(index) {
327 this.is_cell_editor_focused = function(index) {
267 return this.evaluate(function(i) {
328 return this.evaluate(function(i) {
268 var cell = IPython.notebook.get_cell(i);
329 var cell = IPython.notebook.get_cell(i);
269 if (cell) {
330 if (cell) {
270 return $(cell.code_mirror.getInputField()).is('.CodeMirror-focused *');
331 return $(cell.code_mirror.getInputField()).is('.CodeMirror-focused *');
271 }
332 }
272 return false;
333 return false;
273 }, {i : index});
334 }, {i : index});
274 };
335 };
275
336
276 this.is_only_cell_selected = function(index) {
337 this.is_only_cell_selected = function(index) {
277 return this.is_only_cell_on(index, 'selected', 'unselected');
338 return this.is_only_cell_on(index, 'selected', 'unselected');
278 };
339 };
279
340
280 this.is_only_cell_edit = function(index) {
341 this.is_only_cell_edit = function(index) {
281 return this.is_only_cell_on(index, 'edit_mode', 'command_mode');
342 return this.is_only_cell_on(index, 'edit_mode', 'command_mode');
282 };
343 };
283
344
284 this.is_only_cell_on = function(i, on_class, off_class) {
345 this.is_only_cell_on = function(i, on_class, off_class) {
285 var cells = this.get_cells();
346 var cells = this.get_cells();
286 for (var j = 0; j < cells.length; j++) {
347 for (var j = 0; j < cells.length; j++) {
287 if (j === i) {
348 if (j === i) {
288 if (this._has_cell_class(j, off_class) || !this._has_cell_class(j, on_class)) {
349 if (this._has_cell_class(j, off_class) || !this._has_cell_class(j, on_class)) {
289 return false;
350 return false;
290 }
351 }
291 } else {
352 } else {
292 if (!this._has_cell_class(j, off_class) || this._has_cell_class(j, on_class)) {
353 if (!this._has_cell_class(j, off_class) || this._has_cell_class(j, on_class)) {
293 return false;
354 return false;
294 }
355 }
295 }
356 }
296 }
357 }
297 return true;
358 return true;
298 };
359 };
299
360
300 this._has_cell_class = function(index, classes) {
361 this._has_cell_class = function(index, classes) {
301 return this.evaluate(function(i, c) {
362 return this.evaluate(function(i, c) {
302 var cell = IPython.notebook.get_cell(i);
363 var cell = IPython.notebook.get_cell(i);
303 if (cell) {
364 if (cell) {
304 return cell.element.hasClass(c);
365 return cell.element.hasClass(c);
305 }
366 }
306 return false;
367 return false;
307 }, {i : index, c: classes});
368 }, {i : index, c: classes});
308 };
369 };
309 });
370 });
General Comments 0
You need to be logged in to leave comments. Login now