##// END OF EJS Templates
remove rogue codemirror onUpdate
Matthias BUSSONNIER -
Show More
@@ -1,330 +1,329
1 //----------------------------------------------------------------------------
1 //----------------------------------------------------------------------------
2 // Copyright (C) 2008-2011 The IPython Development Team
2 // Copyright (C) 2008-2011 The IPython Development Team
3 //
3 //
4 // Distributed under the terms of the BSD License. The full license is in
4 // Distributed under the terms of the BSD License. The full license is in
5 // the file COPYING, distributed as part of this software.
5 // the file COPYING, distributed as part of this software.
6 //----------------------------------------------------------------------------
6 //----------------------------------------------------------------------------
7
7
8 //============================================================================
8 //============================================================================
9 // CodeCell
9 // CodeCell
10 //============================================================================
10 //============================================================================
11
11
12 var IPython = (function (IPython) {
12 var IPython = (function (IPython) {
13 "use strict";
13 "use strict";
14
14
15 var utils = IPython.utils;
15 var utils = IPython.utils;
16 var key = IPython.utils.keycodes;
16 var key = IPython.utils.keycodes;
17
17
18 var CodeCell = function (kernel) {
18 var CodeCell = function (kernel) {
19 // The kernel doesn't have to be set at creation time, in that case
19 // The kernel doesn't have to be set at creation time, in that case
20 // it will be null and set_kernel has to be called later.
20 // it will be null and set_kernel has to be called later.
21 this.kernel = kernel || null;
21 this.kernel = kernel || null;
22 this.code_mirror = null;
22 this.code_mirror = null;
23 this.input_prompt_number = null;
23 this.input_prompt_number = null;
24 this.tooltip_on_tab = true;
24 this.tooltip_on_tab = true;
25 this.collapsed = false;
25 this.collapsed = false;
26 IPython.Cell.apply(this, arguments);
26 IPython.Cell.apply(this, arguments);
27 };
27 };
28
28
29
29
30 CodeCell.prototype = new IPython.Cell();
30 CodeCell.prototype = new IPython.Cell();
31
31
32
32
33 CodeCell.prototype.create_element = function () {
33 CodeCell.prototype.create_element = function () {
34 var cell = $('<div></div>').addClass('cell border-box-sizing code_cell vbox');
34 var cell = $('<div></div>').addClass('cell border-box-sizing code_cell vbox');
35 cell.attr('tabindex','2');
35 cell.attr('tabindex','2');
36 var input = $('<div></div>').addClass('input hbox');
36 var input = $('<div></div>').addClass('input hbox');
37 input.append($('<div/>').addClass('prompt input_prompt'));
37 input.append($('<div/>').addClass('prompt input_prompt'));
38 var input_area = $('<div/>').addClass('input_area box-flex1');
38 var input_area = $('<div/>').addClass('input_area box-flex1');
39 this.code_mirror = CodeMirror(input_area.get(0), {
39 this.code_mirror = CodeMirror(input_area.get(0), {
40 indentUnit : 4,
40 indentUnit : 4,
41 mode: 'python',
41 mode: 'python',
42 theme: 'ipython',
42 theme: 'ipython',
43 readOnly: this.read_only,
43 readOnly: this.read_only,
44 onUpdate: $.proxy(function(){this.set_input_prompt()},this),
45 extraKeys: {"Tab": "indentMore","Shift-Tab" : "indentLess",'Backspace':"delSpaceToPrevTabStop"},
44 extraKeys: {"Tab": "indentMore","Shift-Tab" : "indentLess",'Backspace':"delSpaceToPrevTabStop"},
46 onKeyEvent: $.proxy(this.handle_codemirror_keyevent,this)
45 onKeyEvent: $.proxy(this.handle_codemirror_keyevent,this)
47 });
46 });
48 input.append(input_area);
47 input.append(input_area);
49 var output = $('<div></div>');
48 var output = $('<div></div>');
50 cell.append(input).append(output);
49 cell.append(input).append(output);
51 this.element = cell;
50 this.element = cell;
52 this.output_area = new IPython.OutputArea(output, true);
51 this.output_area = new IPython.OutputArea(output, true);
53
52
54 // construct a completer only if class exist
53 // construct a completer only if class exist
55 // otherwise no print view
54 // otherwise no print view
56 if (IPython.Completer !== undefined)
55 if (IPython.Completer !== undefined)
57 {
56 {
58 this.completer = new IPython.Completer(this);
57 this.completer = new IPython.Completer(this);
59 }
58 }
60 };
59 };
61
60
62 CodeCell.prototype.handle_codemirror_keyevent = function (editor, event) {
61 CodeCell.prototype.handle_codemirror_keyevent = function (editor, event) {
63 // This method gets called in CodeMirror's onKeyDown/onKeyPress
62 // This method gets called in CodeMirror's onKeyDown/onKeyPress
64 // handlers and is used to provide custom key handling. Its return
63 // handlers and is used to provide custom key handling. Its return
65 // value is used to determine if CodeMirror should ignore the event:
64 // value is used to determine if CodeMirror should ignore the event:
66 // true = ignore, false = don't ignore.
65 // true = ignore, false = don't ignore.
67
66
68 if (this.read_only){
67 if (this.read_only){
69 return false;
68 return false;
70 }
69 }
71
70
72 var that = this;
71 var that = this;
73 // whatever key is pressed, first, cancel the tooltip request before
72 // whatever key is pressed, first, cancel the tooltip request before
74 // they are sent, and remove tooltip if any, except for tab again
73 // they are sent, and remove tooltip if any, except for tab again
75 if (event.type === 'keydown' && event.which != key.TAB ) {
74 if (event.type === 'keydown' && event.which != key.TAB ) {
76 IPython.tooltip.remove_and_cancel_tooltip();
75 IPython.tooltip.remove_and_cancel_tooltip();
77 };
76 };
78
77
79 var cur = editor.getCursor();
78 var cur = editor.getCursor();
80
79
81 if (event.keyCode === key.ENTER && (event.shiftKey || event.ctrlKey)) {
80 if (event.keyCode === key.ENTER && (event.shiftKey || event.ctrlKey)) {
82 // Always ignore shift-enter in CodeMirror as we handle it.
81 // Always ignore shift-enter in CodeMirror as we handle it.
83 return true;
82 return true;
84 } else if (event.which === 40 && event.type === 'keypress' && IPython.tooltip.time_before_tooltip >= 0) {
83 } else if (event.which === 40 && event.type === 'keypress' && IPython.tooltip.time_before_tooltip >= 0) {
85 // triger on keypress (!) otherwise inconsistent event.which depending on plateform
84 // triger on keypress (!) otherwise inconsistent event.which depending on plateform
86 // browser and keyboard layout !
85 // browser and keyboard layout !
87 // Pressing '(' , request tooltip, don't forget to reappend it
86 // Pressing '(' , request tooltip, don't forget to reappend it
88 IPython.tooltip.pending(that);
87 IPython.tooltip.pending(that);
89 } else if (event.which === key.UPARROW && event.type === 'keydown') {
88 } else if (event.which === key.UPARROW && event.type === 'keydown') {
90 // If we are not at the top, let CM handle the up arrow and
89 // If we are not at the top, let CM handle the up arrow and
91 // prevent the global keydown handler from handling it.
90 // prevent the global keydown handler from handling it.
92 if (!that.at_top()) {
91 if (!that.at_top()) {
93 event.stop();
92 event.stop();
94 return false;
93 return false;
95 } else {
94 } else {
96 return true;
95 return true;
97 };
96 };
98 } else if (event.which === key.ESC) {
97 } else if (event.which === key.ESC) {
99 IPython.tooltip.remove_and_cancel_tooltip(true);
98 IPython.tooltip.remove_and_cancel_tooltip(true);
100 return true;
99 return true;
101 } else if (event.which === key.DOWNARROW && event.type === 'keydown') {
100 } else if (event.which === key.DOWNARROW && event.type === 'keydown') {
102 // If we are not at the bottom, let CM handle the down arrow and
101 // If we are not at the bottom, let CM handle the down arrow and
103 // prevent the global keydown handler from handling it.
102 // prevent the global keydown handler from handling it.
104 if (!that.at_bottom()) {
103 if (!that.at_bottom()) {
105 event.stop();
104 event.stop();
106 return false;
105 return false;
107 } else {
106 } else {
108 return true;
107 return true;
109 };
108 };
110 } else if (event.keyCode === key.TAB && event.type == 'keydown') {
109 } else if (event.keyCode === key.TAB && event.type == 'keydown') {
111 // Tab completion.
110 // Tab completion.
112 //Do not trim here because of tooltip
111 //Do not trim here because of tooltip
113 var pre_cursor = editor.getRange({line:cur.line,ch:0},cur);
112 var pre_cursor = editor.getRange({line:cur.line,ch:0},cur);
114 if (pre_cursor.trim() === "") {
113 if (pre_cursor.trim() === "") {
115 // Don't autocomplete if the part of the line before the cursor
114 // Don't autocomplete if the part of the line before the cursor
116 // is empty. In this case, let CodeMirror handle indentation.
115 // is empty. In this case, let CodeMirror handle indentation.
117 return false;
116 return false;
118 } else if ((pre_cursor.substr(-1) === "("|| pre_cursor.substr(-1) === " ") && that.tooltip_on_tab ) {
117 } else if ((pre_cursor.substr(-1) === "("|| pre_cursor.substr(-1) === " ") && that.tooltip_on_tab ) {
119 IPython.tooltip.request(that);
118 IPython.tooltip.request(that);
120 // Prevent the event from bubbling up.
119 // Prevent the event from bubbling up.
121 event.stop();
120 event.stop();
122 // Prevent CodeMirror from handling the tab.
121 // Prevent CodeMirror from handling the tab.
123 return true;
122 return true;
124 } else {
123 } else {
125 event.stop();
124 event.stop();
126 this.completer.startCompletion();
125 this.completer.startCompletion();
127 return true;
126 return true;
128 };
127 };
129 } else {
128 } else {
130 // keypress/keyup also trigger on TAB press, and we don't want to
129 // keypress/keyup also trigger on TAB press, and we don't want to
131 // use those to disable tab completion.
130 // use those to disable tab completion.
132 return false;
131 return false;
133 };
132 };
134 return false;
133 return false;
135 };
134 };
136
135
137
136
138 // Kernel related calls.
137 // Kernel related calls.
139
138
140 CodeCell.prototype.set_kernel = function (kernel) {
139 CodeCell.prototype.set_kernel = function (kernel) {
141 this.kernel = kernel;
140 this.kernel = kernel;
142 }
141 }
143
142
144
143
145 CodeCell.prototype.execute = function () {
144 CodeCell.prototype.execute = function () {
146 this.output_area.clear_output(true, true, true);
145 this.output_area.clear_output(true, true, true);
147 this.set_input_prompt('*');
146 this.set_input_prompt('*');
148 this.element.addClass("running");
147 this.element.addClass("running");
149 var callbacks = {
148 var callbacks = {
150 'execute_reply': $.proxy(this._handle_execute_reply, this),
149 'execute_reply': $.proxy(this._handle_execute_reply, this),
151 'output': $.proxy(this.output_area.handle_output, this.output_area),
150 'output': $.proxy(this.output_area.handle_output, this.output_area),
152 'clear_output': $.proxy(this.output_area.handle_clear_output, this.output_area),
151 'clear_output': $.proxy(this.output_area.handle_clear_output, this.output_area),
153 'set_next_input': $.proxy(this._handle_set_next_input, this)
152 'set_next_input': $.proxy(this._handle_set_next_input, this)
154 };
153 };
155 var msg_id = this.kernel.execute(this.get_text(), callbacks, {silent: false});
154 var msg_id = this.kernel.execute(this.get_text(), callbacks, {silent: false});
156 };
155 };
157
156
158
157
159 CodeCell.prototype._handle_execute_reply = function (content) {
158 CodeCell.prototype._handle_execute_reply = function (content) {
160 this.set_input_prompt(content.execution_count);
159 this.set_input_prompt(content.execution_count);
161 this.element.removeClass("running");
160 this.element.removeClass("running");
162 $([IPython.events]).trigger('set_dirty.Notebook', {'value': true});
161 $([IPython.events]).trigger('set_dirty.Notebook', {'value': true});
163 }
162 }
164
163
165 CodeCell.prototype._handle_set_next_input = function (text) {
164 CodeCell.prototype._handle_set_next_input = function (text) {
166 var data = {'cell': this, 'text': text}
165 var data = {'cell': this, 'text': text}
167 $([IPython.events]).trigger('set_next_input.Notebook', data);
166 $([IPython.events]).trigger('set_next_input.Notebook', data);
168 }
167 }
169
168
170 // Basic cell manipulation.
169 // Basic cell manipulation.
171
170
172 CodeCell.prototype.select = function () {
171 CodeCell.prototype.select = function () {
173 IPython.Cell.prototype.select.apply(this);
172 IPython.Cell.prototype.select.apply(this);
174 this.code_mirror.refresh();
173 this.code_mirror.refresh();
175 this.code_mirror.focus();
174 this.code_mirror.focus();
176 // We used to need an additional refresh() after the focus, but
175 // We used to need an additional refresh() after the focus, but
177 // it appears that this has been fixed in CM. This bug would show
176 // it appears that this has been fixed in CM. This bug would show
178 // up on FF when a newly loaded markdown cell was edited.
177 // up on FF when a newly loaded markdown cell was edited.
179 };
178 };
180
179
181
180
182 CodeCell.prototype.select_all = function () {
181 CodeCell.prototype.select_all = function () {
183 var start = {line: 0, ch: 0};
182 var start = {line: 0, ch: 0};
184 var nlines = this.code_mirror.lineCount();
183 var nlines = this.code_mirror.lineCount();
185 var last_line = this.code_mirror.getLine(nlines-1);
184 var last_line = this.code_mirror.getLine(nlines-1);
186 var end = {line: nlines-1, ch: last_line.length};
185 var end = {line: nlines-1, ch: last_line.length};
187 this.code_mirror.setSelection(start, end);
186 this.code_mirror.setSelection(start, end);
188 };
187 };
189
188
190
189
191 CodeCell.prototype.collapse = function () {
190 CodeCell.prototype.collapse = function () {
192 this.collapsed = true;
191 this.collapsed = true;
193 this.output_area.collapse();
192 this.output_area.collapse();
194 };
193 };
195
194
196
195
197 CodeCell.prototype.expand = function () {
196 CodeCell.prototype.expand = function () {
198 this.collapsed = false;
197 this.collapsed = false;
199 this.output_area.expand();
198 this.output_area.expand();
200 };
199 };
201
200
202
201
203 CodeCell.prototype.toggle_output = function () {
202 CodeCell.prototype.toggle_output = function () {
204 this.collapsed = Boolean(1 - this.collapsed);
203 this.collapsed = Boolean(1 - this.collapsed);
205 this.output_area.toggle_output();
204 this.output_area.toggle_output();
206 };
205 };
207
206
208
207
209 CodeCell.prototype.toggle_output_scroll = function () {
208 CodeCell.prototype.toggle_output_scroll = function () {
210 this.output_area.toggle_scroll();
209 this.output_area.toggle_scroll();
211 };
210 };
212
211
213
212
214
213
215
214
216
215
217 CodeCell.input_prompt_classical = function (prompt_value, lines_number) {
216 CodeCell.input_prompt_classical = function (prompt_value, lines_number) {
218 var ns = prompt_value || "&nbsp;";
217 var ns = prompt_value || "&nbsp;";
219 return 'In&nbsp;[' + ns + ']:'
218 return 'In&nbsp;[' + ns + ']:'
220 };
219 };
221
220
222 CodeCell.input_prompt_continuation = function (prompt_value, lines_number) {
221 CodeCell.input_prompt_continuation = function (prompt_value, lines_number) {
223 var html = [CodeCell.input_prompt_classical(prompt_value, lines_number)];
222 var html = [CodeCell.input_prompt_classical(prompt_value, lines_number)];
224 for(var i=1; i < lines_number; i++){html.push(['...:'])};
223 for(var i=1; i < lines_number; i++){html.push(['...:'])};
225 return html.join('</br>')
224 return html.join('</br>')
226 };
225 };
227
226
228 CodeCell.input_prompt_function = CodeCell.input_prompt_classical;
227 CodeCell.input_prompt_function = CodeCell.input_prompt_classical;
229
228
230
229
231 CodeCell.prototype.set_input_prompt = function (number) {
230 CodeCell.prototype.set_input_prompt = function (number) {
232 var nline = 1
231 var nline = 1
233 if( this.code_mirror != undefined) {
232 if( this.code_mirror != undefined) {
234 nline = this.code_mirror.lineCount();
233 nline = this.code_mirror.lineCount();
235 }
234 }
236 if (number){
235 if (number){
237 this.input_prompt_number = number;
236 this.input_prompt_number = number;
238 }
237 }
239 var prompt_html = CodeCell.input_prompt_function(this.input_prompt_number, nline);
238 var prompt_html = CodeCell.input_prompt_function(this.input_prompt_number, nline);
240 this.element.find('div.input_prompt').html(prompt_html);
239 this.element.find('div.input_prompt').html(prompt_html);
241 };
240 };
242
241
243
242
244 CodeCell.prototype.clear_input = function () {
243 CodeCell.prototype.clear_input = function () {
245 this.code_mirror.setValue('');
244 this.code_mirror.setValue('');
246 };
245 };
247
246
248
247
249 CodeCell.prototype.get_text = function () {
248 CodeCell.prototype.get_text = function () {
250 return this.code_mirror.getValue();
249 return this.code_mirror.getValue();
251 };
250 };
252
251
253
252
254 CodeCell.prototype.set_text = function (code) {
253 CodeCell.prototype.set_text = function (code) {
255 return this.code_mirror.setValue(code);
254 return this.code_mirror.setValue(code);
256 };
255 };
257
256
258
257
259 CodeCell.prototype.at_top = function () {
258 CodeCell.prototype.at_top = function () {
260 var cursor = this.code_mirror.getCursor();
259 var cursor = this.code_mirror.getCursor();
261 if (cursor.line === 0 && cursor.ch === 0) {
260 if (cursor.line === 0 && cursor.ch === 0) {
262 return true;
261 return true;
263 } else {
262 } else {
264 return false;
263 return false;
265 }
264 }
266 };
265 };
267
266
268
267
269 CodeCell.prototype.at_bottom = function () {
268 CodeCell.prototype.at_bottom = function () {
270 var cursor = this.code_mirror.getCursor();
269 var cursor = this.code_mirror.getCursor();
271 if (cursor.line === (this.code_mirror.lineCount()-1) && cursor.ch === this.code_mirror.getLine(cursor.line).length) {
270 if (cursor.line === (this.code_mirror.lineCount()-1) && cursor.ch === this.code_mirror.getLine(cursor.line).length) {
272 return true;
271 return true;
273 } else {
272 } else {
274 return false;
273 return false;
275 }
274 }
276 };
275 };
277
276
278
277
279 CodeCell.prototype.clear_output = function (stdout, stderr, other) {
278 CodeCell.prototype.clear_output = function (stdout, stderr, other) {
280 this.output_area.clear_output(stdout, stderr, other);
279 this.output_area.clear_output(stdout, stderr, other);
281 };
280 };
282
281
283
282
284 // JSON serialization
283 // JSON serialization
285
284
286 CodeCell.prototype.fromJSON = function (data) {
285 CodeCell.prototype.fromJSON = function (data) {
287 IPython.Cell.prototype.fromJSON.apply(this, arguments);
286 IPython.Cell.prototype.fromJSON.apply(this, arguments);
288 if (data.cell_type === 'code') {
287 if (data.cell_type === 'code') {
289 if (data.input !== undefined) {
288 if (data.input !== undefined) {
290 this.set_text(data.input);
289 this.set_text(data.input);
291 // make this value the starting point, so that we can only undo
290 // make this value the starting point, so that we can only undo
292 // to this state, instead of a blank cell
291 // to this state, instead of a blank cell
293 this.code_mirror.clearHistory();
292 this.code_mirror.clearHistory();
294 }
293 }
295 if (data.prompt_number !== undefined) {
294 if (data.prompt_number !== undefined) {
296 this.set_input_prompt(data.prompt_number);
295 this.set_input_prompt(data.prompt_number);
297 } else {
296 } else {
298 this.set_input_prompt();
297 this.set_input_prompt();
299 };
298 };
300 this.output_area.fromJSON(data.outputs);
299 this.output_area.fromJSON(data.outputs);
301 if (data.collapsed !== undefined) {
300 if (data.collapsed !== undefined) {
302 if (data.collapsed) {
301 if (data.collapsed) {
303 this.collapse();
302 this.collapse();
304 } else {
303 } else {
305 this.expand();
304 this.expand();
306 };
305 };
307 };
306 };
308 };
307 };
309 };
308 };
310
309
311
310
312 CodeCell.prototype.toJSON = function () {
311 CodeCell.prototype.toJSON = function () {
313 var data = IPython.Cell.prototype.toJSON.apply(this);
312 var data = IPython.Cell.prototype.toJSON.apply(this);
314 data.input = this.get_text();
313 data.input = this.get_text();
315 data.cell_type = 'code';
314 data.cell_type = 'code';
316 if (this.input_prompt_number) {
315 if (this.input_prompt_number) {
317 data.prompt_number = this.input_prompt_number;
316 data.prompt_number = this.input_prompt_number;
318 };
317 };
319 var outputs = this.output_area.toJSON();
318 var outputs = this.output_area.toJSON();
320 data.outputs = outputs;
319 data.outputs = outputs;
321 data.language = 'python';
320 data.language = 'python';
322 data.collapsed = this.collapsed;
321 data.collapsed = this.collapsed;
323 return data;
322 return data;
324 };
323 };
325
324
326
325
327 IPython.CodeCell = CodeCell;
326 IPython.CodeCell = CodeCell;
328
327
329 return IPython;
328 return IPython;
330 }(IPython));
329 }(IPython));
General Comments 0
You need to be logged in to leave comments. Login now