Show More
@@ -1,309 +1,309 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(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(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(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(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(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(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(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(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'); | |
102 | this.trigger_keydown('k'); |
|
102 | this.trigger_keydown('k'); | |
103 | this.validate_state('k in comand mode', 'command', base_index); |
|
103 | this.validate_state('k in comand mode', 'command', base_index); | |
104 | this.trigger_keydown('alt+enter'); // Creates one cell |
|
104 | this.trigger_keydown('alt+enter'); // Creates one cell | |
105 | this.validate_state('alt+enter (start in command mode)', 'edit', base_index + 1); |
|
105 | this.validate_state('alt+enter (start in command mode)', 'edit', base_index + 1); | |
106 |
|
106 | |||
107 | // Notebook will now have 8 cells, the index of the last cell will be 7. |
|
107 | // 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.'); |
|
108 | this.test.assertEquals(this.get_cells().length, 8, '*-enter commands added cells where needed.'); | |
109 | this.click_cell(7); |
|
109 | this.click_cell(7); | |
110 | this.trigger_keydown('esc'); |
|
110 | this.trigger_keydown('esc'); | |
111 | this.validate_state('click cell ' + 7 + ' and esc', 'command', 7); |
|
111 | this.validate_state('click cell ' + 7 + ' and esc', 'command', 7); | |
112 |
|
112 | |||
113 | this.trigger_keydown('r'); |
|
113 | this.trigger_keydown('r'); | |
114 | this.test.assertEquals(this.get_cell(7).cell_type, 'raw', 'r; cell is raw'); |
|
114 | this.test.assertEquals(this.get_cell(7).cell_type, 'raw', 'r; cell is raw'); | |
115 | this.trigger_keydown('1'); |
|
115 | this.trigger_keydown('1'); | |
116 | this.test.assertEquals(this.get_cell(7).cell_type, 'heading', '1; cell is heading'); |
|
116 | 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'); |
|
117 | this.test.assertEquals(this.get_cell(7).level, 1, '1; cell is level 1 heading'); | |
118 | this.trigger_keydown('2'); |
|
118 | this.trigger_keydown('2'); | |
119 | this.test.assertEquals(this.get_cell(7).level, 2, '2; cell is level 2 heading'); |
|
119 | this.test.assertEquals(this.get_cell(7).level, 2, '2; cell is level 2 heading'); | |
120 | this.trigger_keydown('3'); |
|
120 | this.trigger_keydown('3'); | |
121 | this.test.assertEquals(this.get_cell(7).level, 3, '3; cell is level 3 heading'); |
|
121 | this.test.assertEquals(this.get_cell(7).level, 3, '3; cell is level 3 heading'); | |
122 | this.trigger_keydown('4'); |
|
122 | this.trigger_keydown('4'); | |
123 | this.test.assertEquals(this.get_cell(7).level, 4, '4; cell is level 4 heading'); |
|
123 | this.test.assertEquals(this.get_cell(7).level, 4, '4; cell is level 4 heading'); | |
124 | this.trigger_keydown('5'); |
|
124 | this.trigger_keydown('5'); | |
125 | this.test.assertEquals(this.get_cell(7).level, 5, '5; cell is level 5 heading'); |
|
125 | this.test.assertEquals(this.get_cell(7).level, 5, '5; cell is level 5 heading'); | |
126 | this.trigger_keydown('6'); |
|
126 | this.trigger_keydown('6'); | |
127 | this.test.assertEquals(this.get_cell(7).level, 6, '6; cell is level 6 heading'); |
|
127 | this.test.assertEquals(this.get_cell(7).level, 6, '6; cell is level 6 heading'); | |
128 | this.trigger_keydown('m'); |
|
128 | this.trigger_keydown('m'); | |
129 | this.test.assertEquals(this.get_cell(7).cell_type, 'markdown', 'm; cell is markdown'); |
|
129 | this.test.assertEquals(this.get_cell(7).cell_type, 'markdown', 'm; cell is markdown'); | |
130 | this.trigger_keydown('y'); |
|
130 | this.trigger_keydown('y'); | |
131 | this.test.assertEquals(this.get_cell(7).cell_type, 'code', 'y; cell is code'); |
|
131 | this.test.assertEquals(this.get_cell(7).cell_type, 'code', 'y; cell is code'); | |
132 |
|
132 | |||
133 | this.trigger_keydown('d'); |
|
133 | this.trigger_keydown('d'); | |
134 | this.trigger_keydown('d'); |
|
134 | this.trigger_keydown('d'); | |
135 | this.test.assertEquals(this.get_cells().length, 7, 'dd actually deletes a cell'); |
|
135 | this.test.assertEquals(this.get_cells().length, 7, 'dd actually deletes a cell'); | |
136 | this.validate_state('dd', 'command', 6); |
|
136 | this.validate_state('dd', 'command', 6); | |
137 |
|
137 | |||
138 | // Make sure that if the time between d presses is too long |
|
138 | // Make sure that if the time between d presses is too long | |
139 | this.trigger_keydown('d'); |
|
139 | this.trigger_keydown('d'); | |
140 | }); |
|
140 | }); | |
141 | this.wait(1000); |
|
141 | this.wait(1000); | |
142 | this.then(function () { |
|
142 | this.then(function () { | |
143 | this.trigger_keydown('d'); |
|
143 | this.trigger_keydown('d'); | |
144 |
|
144 | |||
145 |
this.test.assertEquals(this.get_cells().length, |
|
145 | 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); |
|
146 | this.validate_state('d, 1 second wait, d', 'command', 6); | |
147 |
|
147 | |||
148 | }); |
|
148 | }); | |
149 |
|
149 | |||
150 | // Utility functions. |
|
150 | // Utility functions. | |
151 | this.validate_state = function(message, mode, cell_index) { |
|
151 | this.validate_state = function(message, mode, cell_index) { | |
152 | // General tests. |
|
152 | // General tests. | |
153 | this.test.assertEquals(this.get_keyboard_mode(), this.get_notebook_mode(), |
|
153 | this.test.assertEquals(this.get_keyboard_mode(), this.get_notebook_mode(), | |
154 | message + '; keyboard and notebook modes match'); |
|
154 | message + '; keyboard and notebook modes match'); | |
155 | // Is codemirror focused appropriately? |
|
155 | // Is codemirror focused appropriately? | |
156 | this.test.assert(this.is_editor_focus_valid(), message + '; cell editor focused appropriately'); |
|
156 | this.test.assert(this.is_editor_focus_valid(), message + '; cell editor focused appropriately'); | |
157 | // Is the selected cell the only cell that is selected? |
|
157 | // Is the selected cell the only cell that is selected? | |
158 | if (cell_index!==undefined) { |
|
158 | if (cell_index!==undefined) { | |
159 | this.test.assert(this.is_only_cell_selected(cell_index), |
|
159 | this.test.assert(this.is_only_cell_selected(cell_index), | |
160 | message + '; cell ' + cell_index + ' is the only cell selected'); |
|
160 | message + '; cell ' + cell_index + ' is the only cell selected'); | |
161 | } |
|
161 | } | |
162 |
|
162 | |||
163 | // Mode specific tests. |
|
163 | // Mode specific tests. | |
164 | if (mode==='command') { |
|
164 | if (mode==='command') { | |
165 | // Are the notebook and keyboard manager in command mode? |
|
165 | // Are the notebook and keyboard manager in command mode? | |
166 | this.test.assertEquals(this.get_keyboard_mode(), 'command', |
|
166 | this.test.assertEquals(this.get_keyboard_mode(), 'command', | |
167 | message + '; in command mode'); |
|
167 | message + '; in command mode'); | |
168 | // Make sure there isn't a single cell in edit mode. |
|
168 | // Make sure there isn't a single cell in edit mode. | |
169 | this.test.assert(this.is_only_cell_edit(null), |
|
169 | this.test.assert(this.is_only_cell_edit(null), | |
170 | message + '; all cells in command mode'); |
|
170 | message + '; all cells in command mode'); | |
171 |
|
171 | |||
172 | } else if (mode==='edit') { |
|
172 | } else if (mode==='edit') { | |
173 | // Are the notebook and keyboard manager in edit mode? |
|
173 | // Are the notebook and keyboard manager in edit mode? | |
174 | this.test.assertEquals(this.get_keyboard_mode(), 'edit', |
|
174 | this.test.assertEquals(this.get_keyboard_mode(), 'edit', | |
175 | message + '; in edit mode'); |
|
175 | message + '; in edit mode'); | |
176 | // Is the specified cell the only cell in edit mode? |
|
176 | // Is the specified cell the only cell in edit mode? | |
177 | if (cell_index!==undefined) { |
|
177 | if (cell_index!==undefined) { | |
178 | this.test.assert(this.is_only_cell_edit(cell_index), |
|
178 | this.test.assert(this.is_only_cell_edit(cell_index), | |
179 | message + '; cell ' + cell_index + ' is the only cell in edit mode'); |
|
179 | message + '; cell ' + cell_index + ' is the only cell in edit mode'); | |
180 | } |
|
180 | } | |
181 |
|
181 | |||
182 | } else { |
|
182 | } else { | |
183 | this.test.assert(false, message + '; ' + mode + ' is an unknown mode'); |
|
183 | this.test.assert(false, message + '; ' + mode + ' is an unknown mode'); | |
184 | } |
|
184 | } | |
185 | }; |
|
185 | }; | |
186 |
|
186 | |||
187 | this.is_editor_focus_valid = function() { |
|
187 | this.is_editor_focus_valid = function() { | |
188 | var cells = this.get_cells(); |
|
188 | var cells = this.get_cells(); | |
189 | for (var i = 0; i < cells.length; i++) { |
|
189 | for (var i = 0; i < cells.length; i++) { | |
190 | if (!this.is_cell_editor_focus_valid(i)) { |
|
190 | if (!this.is_cell_editor_focus_valid(i)) { | |
191 | return false; |
|
191 | return false; | |
192 | } |
|
192 | } | |
193 | } |
|
193 | } | |
194 | return true; |
|
194 | return true; | |
195 | }; |
|
195 | }; | |
196 |
|
196 | |||
197 | this.is_cell_editor_focus_valid = function(index) { |
|
197 | this.is_cell_editor_focus_valid = function(index) { | |
198 | var cell = this.get_cell(index); |
|
198 | var cell = this.get_cell(index); | |
199 | if (cell) { |
|
199 | if (cell) { | |
200 | if (cell.mode == 'edit') { |
|
200 | if (cell.mode == 'edit') { | |
201 | return this.is_cell_editor_focused(index); |
|
201 | return this.is_cell_editor_focused(index); | |
202 | } else { |
|
202 | } else { | |
203 | return !this.is_cell_editor_focused(index); |
|
203 | return !this.is_cell_editor_focused(index); | |
204 | } |
|
204 | } | |
205 | } |
|
205 | } | |
206 | return true; |
|
206 | return true; | |
207 | }; |
|
207 | }; | |
208 |
|
208 | |||
209 |
|
209 | |||
210 | /* TODO: MOVE EVERYTHING BELOW THIS LINE INTO THE BASE (utils.js) */ |
|
210 | /* TODO: MOVE EVERYTHING BELOW THIS LINE INTO THE BASE (utils.js) */ | |
211 |
|
211 | |||
212 |
|
212 | |||
213 |
|
213 | |||
214 | this.click_cell = function(index) { |
|
214 | this.click_cell = function(index) { | |
215 | // Code Mirror does not play nicely with emulated brower events. |
|
215 | // Code Mirror does not play nicely with emulated brower events. | |
216 | // Instead of trying to emulate a click, here we run code similar to |
|
216 | // 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 |
|
217 | // the code used in Code Mirror that handles the mousedown event on a | |
218 | // region of codemirror that the user can focus. |
|
218 | // region of codemirror that the user can focus. | |
219 | this.evaluate(function (i) { |
|
219 | this.evaluate(function (i) { | |
220 | cm = IPython.notebook.get_cell(i).code_mirror; |
|
220 | cm = IPython.notebook.get_cell(i).code_mirror; | |
221 | if (cm.options.readOnly != "nocursor" && (document.activeElement != cm.display.input)) |
|
221 | if (cm.options.readOnly != "nocursor" && (document.activeElement != cm.display.input)) | |
222 | cm.display.input.focus(); |
|
222 | cm.display.input.focus(); | |
223 | }, {i: index}); |
|
223 | }, {i: index}); | |
224 | }; |
|
224 | }; | |
225 |
|
225 | |||
226 | this.focus_notebook = function() { |
|
226 | this.focus_notebook = function() { | |
227 | this.evaluate(function (){ |
|
227 | this.evaluate(function (){ | |
228 | $('#notebook').focus(); |
|
228 | $('#notebook').focus(); | |
229 | }, {}); |
|
229 | }, {}); | |
230 | }; |
|
230 | }; | |
231 |
|
231 | |||
232 | this.trigger_keydown = function(key) { |
|
232 | this.trigger_keydown = function(key) { | |
233 | this.evaluate(function (k) { |
|
233 | this.evaluate(function (k) { | |
234 | IPython.keyboard.trigger_keydown(k); |
|
234 | IPython.keyboard.trigger_keydown(k); | |
235 | }, {k: key}); |
|
235 | }, {k: key}); | |
236 | }; |
|
236 | }; | |
237 |
|
237 | |||
238 | this.get_keyboard_mode = function() { |
|
238 | this.get_keyboard_mode = function() { | |
239 | return this.evaluate(function() { |
|
239 | return this.evaluate(function() { | |
240 | return IPython.keyboard_manager.mode; |
|
240 | return IPython.keyboard_manager.mode; | |
241 | }, {}); |
|
241 | }, {}); | |
242 | }; |
|
242 | }; | |
243 |
|
243 | |||
244 | this.get_notebook_mode = function() { |
|
244 | this.get_notebook_mode = function() { | |
245 | return this.evaluate(function() { |
|
245 | return this.evaluate(function() { | |
246 | return IPython.notebook.mode; |
|
246 | return IPython.notebook.mode; | |
247 | }, {}); |
|
247 | }, {}); | |
248 | }; |
|
248 | }; | |
249 |
|
249 | |||
250 | this.get_cells = function() { |
|
250 | this.get_cells = function() { | |
251 | return this.evaluate(function() { |
|
251 | return this.evaluate(function() { | |
252 | return IPython.notebook.get_cells(); |
|
252 | return IPython.notebook.get_cells(); | |
253 | }, {}); |
|
253 | }, {}); | |
254 | }; |
|
254 | }; | |
255 |
|
255 | |||
256 | this.get_cell = function(index) { |
|
256 | this.get_cell = function(index) { | |
257 | return this.evaluate(function(i) { |
|
257 | return this.evaluate(function(i) { | |
258 | var cell = IPython.notebook.get_cell(i); |
|
258 | var cell = IPython.notebook.get_cell(i); | |
259 | if (cell) { |
|
259 | if (cell) { | |
260 | return cell; |
|
260 | return cell; | |
261 | } |
|
261 | } | |
262 | return null; |
|
262 | return null; | |
263 | }, {i : index}); |
|
263 | }, {i : index}); | |
264 | }; |
|
264 | }; | |
265 |
|
265 | |||
266 | this.is_cell_editor_focused = function(index) { |
|
266 | this.is_cell_editor_focused = function(index) { | |
267 | return this.evaluate(function(i) { |
|
267 | return this.evaluate(function(i) { | |
268 | var cell = IPython.notebook.get_cell(i); |
|
268 | var cell = IPython.notebook.get_cell(i); | |
269 | if (cell) { |
|
269 | if (cell) { | |
270 | return $(cell.code_mirror.getInputField()).is('.CodeMirror-focused *'); |
|
270 | return $(cell.code_mirror.getInputField()).is('.CodeMirror-focused *'); | |
271 | } |
|
271 | } | |
272 | return false; |
|
272 | return false; | |
273 | }, {i : index}); |
|
273 | }, {i : index}); | |
274 | }; |
|
274 | }; | |
275 |
|
275 | |||
276 | this.is_only_cell_selected = function(index) { |
|
276 | this.is_only_cell_selected = function(index) { | |
277 | return this.is_only_cell_on(index, 'selected', 'unselected'); |
|
277 | return this.is_only_cell_on(index, 'selected', 'unselected'); | |
278 | }; |
|
278 | }; | |
279 |
|
279 | |||
280 | this.is_only_cell_edit = function(index) { |
|
280 | this.is_only_cell_edit = function(index) { | |
281 | return this.is_only_cell_on(index, 'edit_mode', 'command_mode'); |
|
281 | return this.is_only_cell_on(index, 'edit_mode', 'command_mode'); | |
282 | }; |
|
282 | }; | |
283 |
|
283 | |||
284 | this.is_only_cell_on = function(i, on_class, off_class) { |
|
284 | this.is_only_cell_on = function(i, on_class, off_class) { | |
285 | var cells = this.get_cells(); |
|
285 | var cells = this.get_cells(); | |
286 | for (var j = 0; j < cells.length; j++) { |
|
286 | for (var j = 0; j < cells.length; j++) { | |
287 | if (j === i) { |
|
287 | if (j === i) { | |
288 | if (this._has_cell_class(j, off_class) || !this._has_cell_class(j, on_class)) { |
|
288 | if (this._has_cell_class(j, off_class) || !this._has_cell_class(j, on_class)) { | |
289 | return false; |
|
289 | return false; | |
290 | } |
|
290 | } | |
291 | } else { |
|
291 | } else { | |
292 | if (!this._has_cell_class(j, off_class) || this._has_cell_class(j, on_class)) { |
|
292 | if (!this._has_cell_class(j, off_class) || this._has_cell_class(j, on_class)) { | |
293 | return false; |
|
293 | return false; | |
294 | } |
|
294 | } | |
295 | } |
|
295 | } | |
296 | } |
|
296 | } | |
297 | return true; |
|
297 | return true; | |
298 | }; |
|
298 | }; | |
299 |
|
299 | |||
300 | this._has_cell_class = function(index, classes) { |
|
300 | this._has_cell_class = function(index, classes) { | |
301 | return this.evaluate(function(i, c) { |
|
301 | return this.evaluate(function(i, c) { | |
302 | var cell = IPython.notebook.get_cell(i); |
|
302 | var cell = IPython.notebook.get_cell(i); | |
303 | if (cell) { |
|
303 | if (cell) { | |
304 | return cell.element.hasClass(c); |
|
304 | return cell.element.hasClass(c); | |
305 | } |
|
305 | } | |
306 | return false; |
|
306 | return false; | |
307 | }, {i : index, c: classes}); |
|
307 | }, {i : index, c: classes}); | |
308 | }; |
|
308 | }; | |
309 | }); |
|
309 | }); |
General Comments 0
You need to be logged in to leave comments.
Login now