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