Show More
@@ -1,422 +1,420 | |||||
1 |
|
1 | |||
2 | //============================================================================ |
|
2 | //============================================================================ | |
3 | // CodeCell |
|
3 | // CodeCell | |
4 | //============================================================================ |
|
4 | //============================================================================ | |
5 |
|
5 | |||
6 | var IPython = (function (IPython) { |
|
6 | var IPython = (function (IPython) { | |
7 |
|
7 | |||
8 | var utils = IPython.utils; |
|
8 | var utils = IPython.utils; | |
9 |
|
9 | |||
10 | var CodeCell = function (notebook) { |
|
10 | var CodeCell = function (notebook) { | |
11 | this.code_mirror = null; |
|
11 | this.code_mirror = null; | |
12 | this.input_prompt_number = ' '; |
|
12 | this.input_prompt_number = ' '; | |
13 | this.is_completing = false; |
|
13 | this.is_completing = false; | |
14 | this.completion_cursor = null; |
|
14 | this.completion_cursor = null; | |
15 | this.outputs = []; |
|
15 | this.outputs = []; | |
16 | this.collapsed = false; |
|
16 | this.collapsed = false; | |
17 | IPython.Cell.apply(this, arguments); |
|
17 | IPython.Cell.apply(this, arguments); | |
18 | }; |
|
18 | }; | |
19 |
|
19 | |||
20 |
|
20 | |||
21 | CodeCell.prototype = new IPython.Cell(); |
|
21 | CodeCell.prototype = new IPython.Cell(); | |
22 |
|
22 | |||
23 |
|
23 | |||
24 | CodeCell.prototype.create_element = function () { |
|
24 | CodeCell.prototype.create_element = function () { | |
25 | var cell = $('<div></div>').addClass('cell border-box-sizing code_cell vbox'); |
|
25 | var cell = $('<div></div>').addClass('cell border-box-sizing code_cell vbox'); | |
26 | var input = $('<div></div>').addClass('input hbox'); |
|
26 | var input = $('<div></div>').addClass('input hbox'); | |
27 | input.append($('<div/>').addClass('prompt input_prompt')); |
|
27 | input.append($('<div/>').addClass('prompt input_prompt')); | |
28 | var input_area = $('<div/>').addClass('input_area box-flex1'); |
|
28 | var input_area = $('<div/>').addClass('input_area box-flex1'); | |
29 | this.code_mirror = CodeMirror(input_area.get(0), { |
|
29 | this.code_mirror = CodeMirror(input_area.get(0), { | |
30 | indentUnit : 4, |
|
30 | indentUnit : 4, | |
31 | mode: 'python', |
|
31 | mode: 'python', | |
32 | theme: 'ipython', |
|
32 | theme: 'ipython', | |
33 | onKeyEvent: $.proxy(this.handle_codemirror_keyevent,this) |
|
33 | onKeyEvent: $.proxy(this.handle_codemirror_keyevent,this) | |
34 | }); |
|
34 | }); | |
35 | input.append(input_area); |
|
35 | input.append(input_area); | |
36 | var output = $('<div></div>').addClass('output vbox'); |
|
36 | var output = $('<div></div>').addClass('output vbox'); | |
37 | cell.append(input).append(output); |
|
37 | cell.append(input).append(output); | |
38 | this.element = cell; |
|
38 | this.element = cell; | |
39 | this.collapse() |
|
39 | this.collapse() | |
40 | }; |
|
40 | }; | |
41 |
|
41 | |||
42 |
|
42 | |||
43 | CodeCell.prototype.handle_codemirror_keyevent = function (editor, event) { |
|
43 | CodeCell.prototype.handle_codemirror_keyevent = function (editor, event) { | |
44 | // This method gets called in CodeMirror's onKeyDown/onKeyPress handlers and |
|
44 | // This method gets called in CodeMirror's onKeyDown/onKeyPress handlers and | |
45 | // is used to provide custom key handling. Its return value is used to determine |
|
45 | // is used to provide custom key handling. Its return value is used to determine | |
46 | // if CodeMirror should ignore the event: true = ignore, false = don't ignore. |
|
46 | // if CodeMirror should ignore the event: true = ignore, false = don't ignore. | |
47 | if (event.keyCode === 13 && event.shiftKey) { |
|
47 | if (event.keyCode === 13 && event.shiftKey) { | |
48 | // Always ignore shift-enter in CodeMirror as we handle it. |
|
48 | // Always ignore shift-enter in CodeMirror as we handle it. | |
49 | return true; |
|
49 | return true; | |
50 | } else if (event.keyCode === 9) { |
|
50 | } else if (event.keyCode === 9) { | |
51 | var cur = editor.getCursor(); |
|
51 | var cur = editor.getCursor(); | |
52 | var pre_cursor = editor.getRange({line:cur.line,ch:0},cur).trim(); |
|
52 | var pre_cursor = editor.getRange({line:cur.line,ch:0},cur).trim(); | |
53 | if (pre_cursor === "") { |
|
53 | if (pre_cursor === "") { | |
54 | // Don't autocomplete if the part of the line before the cursor is empty. |
|
54 | // Don't autocomplete if the part of the line before the cursor is empty. | |
55 | // In this case, let CodeMirror handle indentation. |
|
55 | // In this case, let CodeMirror handle indentation. | |
56 | return false; |
|
56 | return false; | |
57 | } else { |
|
57 | } else { | |
58 | // Autocomplete the current line. |
|
58 | // Autocomplete the current line. | |
59 | event.stop(); |
|
59 | event.stop(); | |
60 | var line = editor.getLine(cur.line); |
|
60 | var line = editor.getLine(cur.line); | |
61 | this.is_completing = true; |
|
61 | this.is_completing = true; | |
62 | this.completion_cursor = cur; |
|
62 | this.completion_cursor = cur; | |
63 | IPython.notebook.complete_cell(this, line, cur.ch); |
|
63 | IPython.notebook.complete_cell(this, line, cur.ch); | |
64 | return true; |
|
64 | return true; | |
65 | } |
|
65 | } | |
66 | } else if (event.keyCode === 8 && event.type == 'keydown') { |
|
66 | } else if (event.keyCode === 8 && event.type == 'keydown') { | |
67 | // If backspace and the line ends with 4 spaces, remove them. |
|
67 | // If backspace and the line ends with 4 spaces, remove them. | |
68 | var cur = editor.getCursor(); |
|
68 | var cur = editor.getCursor(); | |
69 | var line = editor.getLine(cur.line); |
|
69 | var line = editor.getLine(cur.line); | |
70 | var ending = line.slice(-4); |
|
70 | var ending = line.slice(-4); | |
71 | if (ending === ' ') { |
|
71 | if (ending === ' ') { | |
72 | editor.replaceRange('', |
|
72 | editor.replaceRange('', | |
73 | {line: cur.line, ch: cur.ch-4}, |
|
73 | {line: cur.line, ch: cur.ch-4}, | |
74 | {line: cur.line, ch: cur.ch} |
|
74 | {line: cur.line, ch: cur.ch} | |
75 | ); |
|
75 | ); | |
76 | event.stop(); |
|
76 | event.stop(); | |
77 | return true; |
|
77 | return true; | |
78 | } else { |
|
78 | } else { | |
79 | return false; |
|
79 | return false; | |
80 | }; |
|
80 | }; | |
81 | } else { |
|
81 | } else { | |
82 | if (this.is_completing && this.completion_cursor !== editor.getCursor()) { |
|
82 | if (this.is_completing && this.completion_cursor !== editor.getCursor()) { | |
83 | this.is_completing = false; |
|
83 | this.is_completing = false; | |
84 | this.completion_cursor = null; |
|
84 | this.completion_cursor = null; | |
85 | } |
|
85 | } | |
86 | return false; |
|
86 | return false; | |
87 | }; |
|
87 | }; | |
88 | }; |
|
88 | }; | |
89 |
|
89 | |||
90 |
|
90 | |||
91 | CodeCell.prototype.finish_completing = function (matched_text, matches) { |
|
91 | CodeCell.prototype.finish_completing = function (matched_text, matches) { | |
92 | if (!this.is_completing || matches.length === 0) {return;} |
|
92 | if (!this.is_completing || matches.length === 0) {return;} | |
93 | // console.log("Got matches", matched_text, matches); |
|
93 | // console.log("Got matches", matched_text, matches); | |
94 |
|
94 | |||
95 | var that = this; |
|
95 | var that = this; | |
96 | var cur = this.completion_cursor; |
|
96 | var cur = this.completion_cursor; | |
97 | var complete = $('<div/>').addClass('completions'); |
|
97 | var complete = $('<div/>').addClass('completions'); | |
98 | var select = $('<select/>').attr('multiple','true'); |
|
98 | var select = $('<select/>').attr('multiple','true'); | |
99 | for (var i=0; i<matches.length; ++i) { |
|
99 | for (var i=0; i<matches.length; ++i) { | |
100 | select.append($('<option/>').text(matches[i])); |
|
100 | select.append($('<option/>').text(matches[i])); | |
101 | } |
|
101 | } | |
102 | select.children().first().attr('selected','true'); |
|
102 | select.children().first().attr('selected','true'); | |
103 | select.attr('size',Math.min(10,matches.length)); |
|
103 | select.attr('size',Math.min(10,matches.length)); | |
104 | var pos = this.code_mirror.cursorCoords(); |
|
104 | var pos = this.code_mirror.cursorCoords(); | |
105 | complete.css('left',pos.x+'px'); |
|
105 | complete.css('left',pos.x+'px'); | |
106 | complete.css('top',pos.yBot+'px'); |
|
106 | complete.css('top',pos.yBot+'px'); | |
107 | complete.append(select); |
|
107 | complete.append(select); | |
108 |
|
108 | |||
109 | $('body').append(complete); |
|
109 | $('body').append(complete); | |
110 | var done = false; |
|
110 | var done = false; | |
111 |
|
111 | |||
112 | var insert = function (selected_text) { |
|
112 | var insert = function (selected_text) { | |
113 | that.code_mirror.replaceRange( |
|
113 | that.code_mirror.replaceRange( | |
114 | selected_text, |
|
114 | selected_text, | |
115 | {line: cur.line, ch: (cur.ch-matched_text.length)}, |
|
115 | {line: cur.line, ch: (cur.ch-matched_text.length)}, | |
116 | {line: cur.line, ch: cur.ch} |
|
116 | {line: cur.line, ch: cur.ch} | |
117 | ); |
|
117 | ); | |
118 | }; |
|
118 | }; | |
119 |
|
119 | |||
120 | var close = function () { |
|
120 | var close = function () { | |
121 | if (done) return; |
|
121 | if (done) return; | |
122 | done = true; |
|
122 | done = true; | |
123 | complete.remove(); |
|
123 | complete.remove(); | |
124 | that.is_completing = false; |
|
124 | that.is_completing = false; | |
125 | that.completion_cursor = null; |
|
125 | that.completion_cursor = null; | |
126 | }; |
|
126 | }; | |
127 |
|
127 | |||
128 | var pick = function () { |
|
128 | var pick = function () { | |
129 | insert(select.val()[0]); |
|
129 | insert(select.val()[0]); | |
130 | close(); |
|
130 | close(); | |
131 | setTimeout(function(){that.code_mirror.focus();}, 50); |
|
131 | setTimeout(function(){that.code_mirror.focus();}, 50); | |
132 | }; |
|
132 | }; | |
133 |
|
133 | |||
134 | select.blur(close); |
|
134 | select.blur(close); | |
135 | select.keydown(function (event) { |
|
135 | select.keydown(function (event) { | |
136 | var code = event.which; |
|
136 | var code = event.which; | |
137 | if (code === 13 || code === 32) { |
|
137 | if (code === 13 || code === 32) { | |
138 | // Pressing SPACE or ENTER will cause a pick |
|
138 | // Pressing SPACE or ENTER will cause a pick | |
139 | event.stopPropagation(); |
|
139 | event.stopPropagation(); | |
140 | event.preventDefault(); |
|
140 | event.preventDefault(); | |
141 | pick(); |
|
141 | pick(); | |
142 | } else if (code === 38 || code === 40) { |
|
142 | } else if (code === 38 || code === 40) { | |
143 | // We don't want the document keydown handler to handle UP/DOWN, |
|
143 | // We don't want the document keydown handler to handle UP/DOWN, | |
144 | // but we want the default action. |
|
144 | // but we want the default action. | |
145 | event.stopPropagation(); |
|
145 | event.stopPropagation(); | |
146 | } else { |
|
146 | } else { | |
147 | // All other key presses simple exit completion. |
|
147 | // All other key presses simple exit completion. | |
148 | event.stopPropagation(); |
|
148 | event.stopPropagation(); | |
149 | event.preventDefault(); |
|
149 | event.preventDefault(); | |
150 | close(); |
|
150 | close(); | |
151 | that.code_mirror.focus(); |
|
151 | that.code_mirror.focus(); | |
152 | } |
|
152 | } | |
153 | }); |
|
153 | }); | |
154 | // Double click also causes a pick. |
|
154 | // Double click also causes a pick. | |
155 | select.dblclick(pick); |
|
155 | select.dblclick(pick); | |
156 | select.focus(); |
|
156 | select.focus(); | |
157 | }; |
|
157 | }; | |
158 |
|
158 | |||
159 |
|
159 | |||
160 | CodeCell.prototype.select = function () { |
|
160 | CodeCell.prototype.select = function () { | |
161 | IPython.Cell.prototype.select.apply(this); |
|
161 | IPython.Cell.prototype.select.apply(this); | |
162 | // Todo: this dance is needed because as of CodeMirror 2.12, focus is |
|
162 | // Todo: this dance is needed because as of CodeMirror 2.12, focus is | |
163 | // not causing the cursor to blink if the editor is empty initially. |
|
163 | // not causing the cursor to blink if the editor is empty initially. | |
164 | // While this seems to fix the issue, this should be fixed |
|
164 | // While this seems to fix the issue, this should be fixed | |
165 | // in CodeMirror proper. |
|
165 | // in CodeMirror proper. | |
166 | var s = this.code_mirror.getValue(); |
|
166 | var s = this.code_mirror.getValue(); | |
167 | if (s === '') this.code_mirror.setValue('.'); |
|
167 | if (s === '') this.code_mirror.setValue('.'); | |
168 | this.code_mirror.focus(); |
|
168 | this.code_mirror.focus(); | |
169 | if (s === '') this.code_mirror.setValue(''); |
|
169 | if (s === '') this.code_mirror.setValue(''); | |
170 | }; |
|
170 | }; | |
171 |
|
171 | |||
172 |
|
172 | |||
173 | CodeCell.prototype.append_output = function (json) { |
|
173 | CodeCell.prototype.append_output = function (json) { | |
174 | this.expand(); |
|
174 | this.expand(); | |
175 | if (json.output_type === 'pyout') { |
|
175 | if (json.output_type === 'pyout') { | |
176 | this.append_pyout(json); |
|
176 | this.append_pyout(json); | |
177 | } else if (json.output_type === 'pyerr') { |
|
177 | } else if (json.output_type === 'pyerr') { | |
178 | this.append_pyerr(json); |
|
178 | this.append_pyerr(json); | |
179 | } else if (json.output_type === 'display_data') { |
|
179 | } else if (json.output_type === 'display_data') { | |
180 | this.append_display_data(json); |
|
180 | this.append_display_data(json); | |
181 | } else if (json.output_type === 'stream') { |
|
181 | } else if (json.output_type === 'stream') { | |
182 | this.append_stream(json); |
|
182 | this.append_stream(json); | |
183 | }; |
|
183 | }; | |
184 | this.outputs.push(json); |
|
184 | this.outputs.push(json); | |
185 | }; |
|
185 | }; | |
186 |
|
186 | |||
187 |
|
187 | |||
188 | CodeCell.prototype.append_pyout = function (json) { |
|
188 | CodeCell.prototype.append_pyout = function (json) { | |
189 | n = json.prompt_number || ' '; |
|
189 | n = json.prompt_number || ' '; | |
190 | var toinsert = $("<div/>").addClass("output_pyout hbox"); |
|
190 | var toinsert = $("<div/>").addClass("output_pyout hbox"); | |
191 | toinsert.append($('<div/>'). |
|
191 | toinsert.append($('<div/>'). | |
192 | addClass('prompt output_prompt'). |
|
192 | addClass('prompt output_prompt'). | |
193 | html('Out[' + n + ']:') |
|
193 | html('Out[' + n + ']:') | |
194 | ); |
|
194 | ); | |
195 | this.append_mime_type(json, toinsert).addClass('output_area'); |
|
195 | this.append_mime_type(json, toinsert).addClass('output_area'); | |
196 | toinsert.children().last().addClass("box_flex1 pyout_area"); |
|
196 | toinsert.children().last().addClass("box_flex1 pyout_area"); | |
197 | this.element.find("div.output").append(toinsert); |
|
197 | this.element.find("div.output").append(toinsert); | |
198 | // If we just output latex, typeset it. |
|
198 | // If we just output latex, typeset it. | |
199 | if (json.latex !== undefined) { |
|
199 | if (json.latex !== undefined) { | |
200 | MathJax.Hub.Queue(["Typeset",MathJax.Hub]); |
|
200 | MathJax.Hub.Queue(["Typeset",MathJax.Hub]); | |
201 | }; |
|
201 | }; | |
202 | }; |
|
202 | }; | |
203 |
|
203 | |||
204 |
|
204 | |||
205 | CodeCell.prototype.append_pyerr = function (json) { |
|
205 | CodeCell.prototype.append_pyerr = function (json) { | |
206 | var tb = json.traceback; |
|
206 | var tb = json.traceback; | |
207 | if (tb !== undefined && tb.length > 0) { |
|
207 | if (tb !== undefined && tb.length > 0) { | |
208 | var s = ''; |
|
208 | var s = ''; | |
209 | var len = tb.length; |
|
209 | var len = tb.length; | |
210 | for (var i=0; i<len; i++) { |
|
210 | for (var i=0; i<len; i++) { | |
211 | s = s + tb[i] + '\n'; |
|
211 | s = s + tb[i] + '\n'; | |
212 | } |
|
212 | } | |
213 | s = s + '\n'; |
|
213 | s = s + '\n'; | |
214 | this.append_text(s).addClass('output_area'); |
|
214 | this.append_text(s).addClass('output_area'); | |
215 | }; |
|
215 | }; | |
216 | }; |
|
216 | }; | |
217 |
|
217 | |||
218 |
|
218 | |||
219 | CodeCell.prototype.append_stream = function (json) { |
|
219 | CodeCell.prototype.append_stream = function (json) { | |
220 | this.append_text(json.text).addClass('output_area'); |
|
220 | this.append_text(json.text).addClass('output_area'); | |
221 | }; |
|
221 | }; | |
222 |
|
222 | |||
223 |
|
223 | |||
224 | CodeCell.prototype.append_display_data = function (json) { |
|
224 | CodeCell.prototype.append_display_data = function (json) { | |
225 | this.append_mime_type(json).addClass('output_area'); |
|
225 | this.append_mime_type(json).addClass('output_area'); | |
|
226 | // If we just output latex, typeset it. | |||
|
227 | if (json.latex !== undefined) { | |||
|
228 | MathJax.Hub.Queue(["Typeset",MathJax.Hub]); | |||
|
229 | }; | |||
226 | }; |
|
230 | }; | |
227 |
|
231 | |||
228 |
|
232 | |||
229 | CodeCell.prototype.append_mime_type = function (json, element) { |
|
233 | CodeCell.prototype.append_mime_type = function (json, element) { | |
230 | element = element || this.element.find("div.output"); |
|
234 | element = element || this.element.find("div.output"); | |
231 | if (json.html !== undefined) { |
|
235 | if (json.html !== undefined) { | |
232 | this.append_html(json.html, element); |
|
236 | this.append_html(json.html, element); | |
233 | } else if (json.latex !== undefined) { |
|
237 | } else if (json.latex !== undefined) { | |
234 | this.append_latex(json.latex, element); |
|
238 | this.append_latex(json.latex, element); | |
235 | // If it is undefined, then we just appended to div.output, which |
|
|||
236 | // makes the latex visible and we can typeset it. The typesetting |
|
|||
237 | // has to be done after the latex is on the page. |
|
|||
238 | if (element === undefined) { |
|
|||
239 | MathJax.Hub.Queue(["Typeset",MathJax.Hub]); |
|
|||
240 | }; |
|
|||
241 | } else if (json.svg !== undefined) { |
|
239 | } else if (json.svg !== undefined) { | |
242 | this.append_svg(json.svg, element); |
|
240 | this.append_svg(json.svg, element); | |
243 | } else if (json.png !== undefined) { |
|
241 | } else if (json.png !== undefined) { | |
244 | this.append_png(json.png, element); |
|
242 | this.append_png(json.png, element); | |
245 | } else if (json.jpeg !== undefined) { |
|
243 | } else if (json.jpeg !== undefined) { | |
246 | this.append_jpeg(json.jpeg, element); |
|
244 | this.append_jpeg(json.jpeg, element); | |
247 | } else if (json.text !== undefined) { |
|
245 | } else if (json.text !== undefined) { | |
248 | this.append_text(json.text, element); |
|
246 | this.append_text(json.text, element); | |
249 | }; |
|
247 | }; | |
250 | return element; |
|
248 | return element; | |
251 | }; |
|
249 | }; | |
252 |
|
250 | |||
253 |
|
251 | |||
254 | CodeCell.prototype.append_html = function (html, element) { |
|
252 | CodeCell.prototype.append_html = function (html, element) { | |
255 | element = element || this.element.find("div.output"); |
|
253 | element = element || this.element.find("div.output"); | |
256 | var toinsert = $("<div/>").addClass("output_html rendered_html"); |
|
254 | var toinsert = $("<div/>").addClass("output_html rendered_html"); | |
257 | toinsert.append(html); |
|
255 | toinsert.append(html); | |
258 | element.append(toinsert); |
|
256 | element.append(toinsert); | |
259 | return element; |
|
257 | return element; | |
260 | } |
|
258 | } | |
261 |
|
259 | |||
262 |
|
260 | |||
263 | CodeCell.prototype.append_text = function (data, element) { |
|
261 | CodeCell.prototype.append_text = function (data, element) { | |
264 | element = element || this.element.find("div.output"); |
|
262 | element = element || this.element.find("div.output"); | |
265 | var toinsert = $("<div/>").addClass("output_stream"); |
|
263 | var toinsert = $("<div/>").addClass("output_stream"); | |
266 | toinsert.append($("<pre/>").html(data)); |
|
264 | toinsert.append($("<pre/>").html(data)); | |
267 | element.append(toinsert); |
|
265 | element.append(toinsert); | |
268 | return element; |
|
266 | return element; | |
269 | }; |
|
267 | }; | |
270 |
|
268 | |||
271 |
|
269 | |||
272 | CodeCell.prototype.append_svg = function (svg, element) { |
|
270 | CodeCell.prototype.append_svg = function (svg, element) { | |
273 | element = element || this.element.find("div.output"); |
|
271 | element = element || this.element.find("div.output"); | |
274 | var toinsert = $("<div/>").addClass("output_svg"); |
|
272 | var toinsert = $("<div/>").addClass("output_svg"); | |
275 | toinsert.append(svg); |
|
273 | toinsert.append(svg); | |
276 | element.append(toinsert); |
|
274 | element.append(toinsert); | |
277 | return element; |
|
275 | return element; | |
278 | }; |
|
276 | }; | |
279 |
|
277 | |||
280 |
|
278 | |||
281 | CodeCell.prototype.append_png = function (png, element) { |
|
279 | CodeCell.prototype.append_png = function (png, element) { | |
282 | element = element || this.element.find("div.output"); |
|
280 | element = element || this.element.find("div.output"); | |
283 | var toinsert = $("<div/>").addClass("output_png"); |
|
281 | var toinsert = $("<div/>").addClass("output_png"); | |
284 | toinsert.append($("<img/>").attr('src','data:image/png;base64,'+png)); |
|
282 | toinsert.append($("<img/>").attr('src','data:image/png;base64,'+png)); | |
285 | element.append(toinsert); |
|
283 | element.append(toinsert); | |
286 | return element; |
|
284 | return element; | |
287 | }; |
|
285 | }; | |
288 |
|
286 | |||
289 |
|
287 | |||
290 | CodeCell.prototype.append_jpeg = function (jpeg, element) { |
|
288 | CodeCell.prototype.append_jpeg = function (jpeg, element) { | |
291 | element = element || this.element.find("div.output"); |
|
289 | element = element || this.element.find("div.output"); | |
292 | var toinsert = $("<div/>").addClass("output_jpeg"); |
|
290 | var toinsert = $("<div/>").addClass("output_jpeg"); | |
293 | toinsert.append($("<img/>").attr('src','data:image/jpeg;base64,'+jpeg)); |
|
291 | toinsert.append($("<img/>").attr('src','data:image/jpeg;base64,'+jpeg)); | |
294 | element.append(toinsert); |
|
292 | element.append(toinsert); | |
295 | return element; |
|
293 | return element; | |
296 | }; |
|
294 | }; | |
297 |
|
295 | |||
298 |
|
296 | |||
299 | CodeCell.prototype.append_latex = function (latex, element) { |
|
297 | CodeCell.prototype.append_latex = function (latex, element) { | |
300 | // This method cannot do the typesetting because the latex first has to |
|
298 | // This method cannot do the typesetting because the latex first has to | |
301 | // be on the page. |
|
299 | // be on the page. | |
302 | element = element || this.element.find("div.output"); |
|
300 | element = element || this.element.find("div.output"); | |
303 | var toinsert = $("<div/>").addClass("output_latex"); |
|
301 | var toinsert = $("<div/>").addClass("output_latex"); | |
304 | toinsert.append(latex); |
|
302 | toinsert.append(latex); | |
305 | element.append(toinsert); |
|
303 | element.append(toinsert); | |
306 | return element; |
|
304 | return element; | |
307 | } |
|
305 | } | |
308 |
|
306 | |||
309 |
|
307 | |||
310 | CodeCell.prototype.clear_output = function () { |
|
308 | CodeCell.prototype.clear_output = function () { | |
311 | this.element.find("div.output").html(""); |
|
309 | this.element.find("div.output").html(""); | |
312 | this.outputs = []; |
|
310 | this.outputs = []; | |
313 | }; |
|
311 | }; | |
314 |
|
312 | |||
315 |
|
313 | |||
316 | CodeCell.prototype.clear_input = function () { |
|
314 | CodeCell.prototype.clear_input = function () { | |
317 | this.code_mirror.setValue(''); |
|
315 | this.code_mirror.setValue(''); | |
318 | }; |
|
316 | }; | |
319 |
|
317 | |||
320 |
|
318 | |||
321 | CodeCell.prototype.collapse = function () { |
|
319 | CodeCell.prototype.collapse = function () { | |
322 | if (!this.collapsed) { |
|
320 | if (!this.collapsed) { | |
323 | this.element.find('div.output').hide(); |
|
321 | this.element.find('div.output').hide(); | |
324 | this.collapsed = true; |
|
322 | this.collapsed = true; | |
325 | }; |
|
323 | }; | |
326 | }; |
|
324 | }; | |
327 |
|
325 | |||
328 |
|
326 | |||
329 | CodeCell.prototype.expand = function () { |
|
327 | CodeCell.prototype.expand = function () { | |
330 | if (this.collapsed) { |
|
328 | if (this.collapsed) { | |
331 | this.element.find('div.output').show(); |
|
329 | this.element.find('div.output').show(); | |
332 | this.collapsed = false; |
|
330 | this.collapsed = false; | |
333 | }; |
|
331 | }; | |
334 | }; |
|
332 | }; | |
335 |
|
333 | |||
336 |
|
334 | |||
337 | CodeCell.prototype.set_input_prompt = function (number) { |
|
335 | CodeCell.prototype.set_input_prompt = function (number) { | |
338 | var n = number || ' '; |
|
336 | var n = number || ' '; | |
339 | this.input_prompt_number = n |
|
337 | this.input_prompt_number = n | |
340 | this.element.find('div.input_prompt').html('In [' + n + ']:'); |
|
338 | this.element.find('div.input_prompt').html('In [' + n + ']:'); | |
341 | }; |
|
339 | }; | |
342 |
|
340 | |||
343 |
|
341 | |||
344 | CodeCell.prototype.get_code = function () { |
|
342 | CodeCell.prototype.get_code = function () { | |
345 | return this.code_mirror.getValue(); |
|
343 | return this.code_mirror.getValue(); | |
346 | }; |
|
344 | }; | |
347 |
|
345 | |||
348 |
|
346 | |||
349 | CodeCell.prototype.set_code = function (code) { |
|
347 | CodeCell.prototype.set_code = function (code) { | |
350 | return this.code_mirror.setValue(code); |
|
348 | return this.code_mirror.setValue(code); | |
351 | }; |
|
349 | }; | |
352 |
|
350 | |||
353 |
|
351 | |||
354 | CodeCell.prototype.at_top = function () { |
|
352 | CodeCell.prototype.at_top = function () { | |
355 | var cursor = this.code_mirror.getCursor(); |
|
353 | var cursor = this.code_mirror.getCursor(); | |
356 | if (cursor.line === 0) { |
|
354 | if (cursor.line === 0) { | |
357 | return true; |
|
355 | return true; | |
358 | } else { |
|
356 | } else { | |
359 | return false; |
|
357 | return false; | |
360 | } |
|
358 | } | |
361 | }; |
|
359 | }; | |
362 |
|
360 | |||
363 |
|
361 | |||
364 | CodeCell.prototype.at_bottom = function () { |
|
362 | CodeCell.prototype.at_bottom = function () { | |
365 | var cursor = this.code_mirror.getCursor(); |
|
363 | var cursor = this.code_mirror.getCursor(); | |
366 | if (cursor.line === (this.code_mirror.lineCount()-1)) { |
|
364 | if (cursor.line === (this.code_mirror.lineCount()-1)) { | |
367 | return true; |
|
365 | return true; | |
368 | } else { |
|
366 | } else { | |
369 | return false; |
|
367 | return false; | |
370 | } |
|
368 | } | |
371 | }; |
|
369 | }; | |
372 |
|
370 | |||
373 |
|
371 | |||
374 | CodeCell.prototype.fromJSON = function (data) { |
|
372 | CodeCell.prototype.fromJSON = function (data) { | |
375 | // console.log('Import from JSON:', data); |
|
373 | // console.log('Import from JSON:', data); | |
376 | if (data.cell_type === 'code') { |
|
374 | if (data.cell_type === 'code') { | |
377 | if (data.input !== undefined) { |
|
375 | if (data.input !== undefined) { | |
378 | this.set_code(data.input); |
|
376 | this.set_code(data.input); | |
379 | } |
|
377 | } | |
380 | if (data.prompt_number !== undefined) { |
|
378 | if (data.prompt_number !== undefined) { | |
381 | this.set_input_prompt(data.prompt_number); |
|
379 | this.set_input_prompt(data.prompt_number); | |
382 | } else { |
|
380 | } else { | |
383 | this.set_input_prompt(); |
|
381 | this.set_input_prompt(); | |
384 | }; |
|
382 | }; | |
385 | var len = data.outputs.length; |
|
383 | var len = data.outputs.length; | |
386 | for (var i=0; i<len; i++) { |
|
384 | for (var i=0; i<len; i++) { | |
387 | this.append_output(data.outputs[i]); |
|
385 | this.append_output(data.outputs[i]); | |
388 | }; |
|
386 | }; | |
389 | if (data.collapsed !== undefined) { |
|
387 | if (data.collapsed !== undefined) { | |
390 | if (data.collapsed) { |
|
388 | if (data.collapsed) { | |
391 | this.collapse(); |
|
389 | this.collapse(); | |
392 | }; |
|
390 | }; | |
393 | }; |
|
391 | }; | |
394 | }; |
|
392 | }; | |
395 | }; |
|
393 | }; | |
396 |
|
394 | |||
397 |
|
395 | |||
398 | CodeCell.prototype.toJSON = function () { |
|
396 | CodeCell.prototype.toJSON = function () { | |
399 | var data = {}; |
|
397 | var data = {}; | |
400 | data.input = this.get_code(); |
|
398 | data.input = this.get_code(); | |
401 | data.cell_type = 'code'; |
|
399 | data.cell_type = 'code'; | |
402 | if (this.input_prompt_number !== ' ') { |
|
400 | if (this.input_prompt_number !== ' ') { | |
403 | data.prompt_number = this.input_prompt_number |
|
401 | data.prompt_number = this.input_prompt_number | |
404 | }; |
|
402 | }; | |
405 | var outputs = []; |
|
403 | var outputs = []; | |
406 | var len = this.outputs.length; |
|
404 | var len = this.outputs.length; | |
407 | for (var i=0; i<len; i++) { |
|
405 | for (var i=0; i<len; i++) { | |
408 | outputs[i] = this.outputs[i]; |
|
406 | outputs[i] = this.outputs[i]; | |
409 | }; |
|
407 | }; | |
410 | data.outputs = outputs; |
|
408 | data.outputs = outputs; | |
411 | data.language = 'python'; |
|
409 | data.language = 'python'; | |
412 | data.collapsed = this.collapsed; |
|
410 | data.collapsed = this.collapsed; | |
413 | // console.log('Export to JSON:',data); |
|
411 | // console.log('Export to JSON:',data); | |
414 | return data; |
|
412 | return data; | |
415 | }; |
|
413 | }; | |
416 |
|
414 | |||
417 |
|
415 | |||
418 | IPython.CodeCell = CodeCell; |
|
416 | IPython.CodeCell = CodeCell; | |
419 |
|
417 | |||
420 | return IPython; |
|
418 | return IPython; | |
421 | }(IPython)); |
|
419 | }(IPython)); | |
422 |
|
420 |
General Comments 0
You need to be logged in to leave comments.
Login now