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