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