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