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