##// END OF EJS Templates
Semi working version of basic dual mode UX....
Brian E. Granger -
Show More
@@ -1,469 +1,462 b''
1 1 //----------------------------------------------------------------------------
2 2 // Copyright (C) 2008-2011 The IPython Development Team
3 3 //
4 4 // Distributed under the terms of the BSD License. The full license is in
5 5 // the file COPYING, distributed as part of this software.
6 6 //----------------------------------------------------------------------------
7 7
8 8 //============================================================================
9 9 // Cell
10 10 //============================================================================
11 11 /**
12 12 * An extendable module that provide base functionnality to create cell for notebook.
13 13 * @module IPython
14 14 * @namespace IPython
15 15 * @submodule Cell
16 16 */
17 17
18 18 var IPython = (function (IPython) {
19 19 "use strict";
20 20
21 21 var utils = IPython.utils;
22 22
23 23 /**
24 24 * The Base `Cell` class from which to inherit
25 25 * @class Cell
26 26 **/
27 27
28 28 /*
29 29 * @constructor
30 30 *
31 31 * @param {object|undefined} [options]
32 32 * @param [options.cm_config] {object} config to pass to CodeMirror, will extend default parameters
33 33 */
34 34 var Cell = function (options) {
35 35
36 36 options = this.mergeopt(Cell, options);
37 37 // superclass default overwrite our default
38 38
39 39 this.placeholder = options.placeholder || '';
40 40 this.read_only = options.cm_config.readOnly;
41 41 this.selected = false;
42 42 this.rendered = false;
43 43 this.mode = 'command';
44 44 this.metadata = {};
45 45 // load this from metadata later ?
46 46 this.user_highlight = 'auto';
47 47 this.cm_config = options.cm_config;
48 48 this.cell_id = utils.uuid();
49 49 this._options = options;
50 50
51 51 // For JS VM engines optimisation, attributes should be all set (even
52 52 // to null) in the constructor, and if possible, if different subclass
53 53 // have new attributes with same name, they should be created in the
54 54 // same order. Easiest is to create and set to null in parent class.
55 55
56 56 this.element = null;
57 57 this.cell_type = this.cell_type || null;
58 58 this.code_mirror = null;
59 59
60 60
61 61 this.create_element();
62 62 if (this.element !== null) {
63 63 this.element.data("cell", this);
64 64 this.bind_events();
65 65 }
66 66 };
67 67
68 68 Cell.options_default = {
69 69 cm_config : {
70 70 indentUnit : 4,
71 71 readOnly: false,
72 72 theme: "default"
73 73 }
74 74 };
75 75
76 76 // FIXME: Workaround CM Bug #332 (Safari segfault on drag)
77 77 // by disabling drag/drop altogether on Safari
78 78 // https://github.com/marijnh/CodeMirror/issues/332
79 79
80 80 if (utils.browser[0] == "Safari") {
81 81 Cell.options_default.cm_config.dragDrop = false;
82 82 }
83 83
84 84 Cell.prototype.mergeopt = function(_class, options, overwrite){
85 85 options = options || {};
86 86 overwrite = overwrite || {};
87 87 return $.extend(true, {}, _class.options_default, options, overwrite)
88 88
89 89 }
90 90
91 91
92 92
93 93 /**
94 94 * Empty. Subclasses must implement create_element.
95 95 * This should contain all the code to create the DOM element in notebook
96 96 * and will be called by Base Class constructor.
97 97 * @method create_element
98 98 */
99 99 Cell.prototype.create_element = function () {
100 100 };
101 101
102 102
103 103 /**
104 104 * Subclasses can implement override bind_events.
105 105 * Be carefull to call the parent method when overwriting as it fires event.
106 106 * this will be triggerd after create_element in constructor.
107 107 * @method bind_events
108 108 */
109 109 Cell.prototype.bind_events = function () {
110 110 var that = this;
111 111 // We trigger events so that Cell doesn't have to depend on Notebook.
112 112 that.element.click(function (event) {
113 113 if (that.selected === false) {
114 114 $([IPython.events]).trigger('select.Cell', {'cell':that});
115 }
115 };
116 116 });
117 117 that.element.focusin(function (event) {
118 118 if (that.selected === false) {
119 119 $([IPython.events]).trigger('select.Cell', {'cell':that});
120 }
120 };
121 });
122 that.element.focusout(function (event) {
123 if (that.mode === 'edit') {
124 $([IPython.events]).trigger('command_mode.Cell', {'cell':that});
125 };
121 126 });
122 127 if (this.code_mirror) {
123 128 this.code_mirror.on("change", function(cm, change) {
124 129 $([IPython.events]).trigger("set_dirty.Notebook", {value: true});
125 130 });
126 131 };
127 132 if (this.code_mirror) {
128 133 this.code_mirror.on('focus', function(cm, change) {
129 134 $([IPython.events]).trigger('edit_mode.Cell', {cell: that});
130 135 });
131 136 };
132 137 };
133 138
134 139 /**
135 140 * Triger typsetting of math by mathjax on current cell element
136 141 * @method typeset
137 142 */
138 143 Cell.prototype.typeset = function () {
139 144 if (window.MathJax) {
140 145 var cell_math = this.element.get(0);
141 146 MathJax.Hub.Queue(["Typeset", MathJax.Hub, cell_math]);
142 147 };
143 148 };
144 149
145 150 /**
146 151 * handle cell level logic when a cell is selected
147 152 * @method select
148 153 * @return is the action being taken
149 154 */
150 155 Cell.prototype.select = function () {
151 console.log('Cell.select');
152 156 if (!this.selected) {
153 157 this.element.addClass('selected');
154 158 this.element.removeClass('unselected');
155 159 this.selected = true;
156 160 return true;
157 161 } else {
158 console.log('WARNING: select');
159 162 return false;
160 163 };
161 164 };
162 165
163 166 /**
164 167 * handle cell level logic when a cell is unselected
165 168 * @method unselect
166 169 * @return is the action being taken
167 170 */
168 171 Cell.prototype.unselect = function () {
169 console.log('Cell.unselect');
170 172 if (this.selected) {
171 173 this.element.addClass('unselected');
172 174 this.element.removeClass('selected');
173 175 this.selected = false;
174 176 return true;
175 177 } else {
176 console.log('WARNING: unselect');
177 178 return false;
178 179 };
179 180 };
180 181
181 182 /**
182 183 * handle cell level logic when a cell is rendered
183 184 * @method render
184 185 * @return is the action being taken
185 186 */
186 187 Cell.prototype.render = function () {
187 console.log('Cell.render');
188 188 if (!this.rendered) {
189 189 this.element.addClass('rendered');
190 190 this.element.removeClass('unrendered');
191 191 this.rendered = true;
192 192 return true;
193 193 } else {
194 console.log('WARNING: render');
195 194 return false;
196 195 };
197 196 };
198 197
199 198 /**
200 199 * handle cell level logic when a cell is unrendered
201 200 * @method unrender
202 201 * @return is the action being taken
203 202 */
204 203 Cell.prototype.unrender = function () {
205 console.log('Cell.unrender');
206 204 if (this.rendered) {
207 205 this.element.addClass('unrendered');
208 206 this.element.removeClass('rendered');
209 207 this.rendered = false;
210 208 return true;
211 209 } else {
212 console.log('WARNING: unrender');
213 210 return false;
214 211 };
215 212 };
216 213
217 214 /**
218 215 * enter the command mode for the cell
219 216 * @method command_mode
220 217 * @return is the action being taken
221 218 */
222 219 Cell.prototype.command_mode = function () {
223 console.log('Cell.command_mode:', this.mode);
224 220 if (this.mode !== 'command') {
225 221 this.element.addClass('command_mode');
226 222 this.element.removeClass('edit_mode');
227 223 this.mode = 'command';
228 224 return true;
229 225 } else {
230 console.log('WARNING: command_mode');
231 226 return false;
232 227 };
233 228 };
234 229
235 230 /**
236 231 * enter the edit mode for the cell
237 232 * @method command_mode
238 233 * @return is the action being taken
239 234 */
240 235 Cell.prototype.edit_mode = function () {
241 console.log('Cell.edit_mode:', this.mode);
242 236 if (this.mode !== 'edit') {
243 237 this.element.addClass('edit_mode');
244 238 this.element.removeClass('command_mode');
245 239 this.mode = 'edit';
246 240 return true;
247 241 } else {
248 console.log('WARNING: edit_mode');
249 242 return false;
250 243 };
251 244 }
252 245
253 246 /**
254 247 * Focus the cell in the DOM sense
255 248 * @method focus_cell
256 249 */
257 250 Cell.prototype.focus_cell = function () {
258 251 this.element.focus();
259 252 }
260 253
261 254 /**
262 255 * Focus the editor area so a user can type
263 256 * @method focus_editor
264 257 */
265 258 Cell.prototype.focus_editor = function () {
266 259 this.refresh();
267 260 this.code_mirror.focus();
268 261 }
269 262
270 263 /**
271 264 * Refresh codemirror instance
272 265 * @method refresh
273 266 */
274 267 Cell.prototype.refresh = function () {
275 268 this.code_mirror.refresh();
276 269 };
277 270
278 271 /**
279 272 * should be overritten by subclass
280 273 * @method get_text
281 274 */
282 275 Cell.prototype.get_text = function () {
283 276 };
284 277
285 278 /**
286 279 * should be overritten by subclass
287 280 * @method set_text
288 281 * @param {string} text
289 282 */
290 283 Cell.prototype.set_text = function (text) {
291 284 };
292 285
293 286 /**
294 287 * should be overritten by subclass
295 288 * serialise cell to json.
296 289 * @method toJSON
297 290 **/
298 291 Cell.prototype.toJSON = function () {
299 292 var data = {};
300 293 data.metadata = this.metadata;
301 294 data.cell_type = this.cell_type;
302 295 return data;
303 296 };
304 297
305 298
306 299 /**
307 300 * should be overritten by subclass
308 301 * @method fromJSON
309 302 **/
310 303 Cell.prototype.fromJSON = function (data) {
311 304 if (data.metadata !== undefined) {
312 305 this.metadata = data.metadata;
313 306 }
314 307 this.celltoolbar.rebuild();
315 308 };
316 309
317 310
318 311 /**
319 312 * can the cell be split into two cells
320 313 * @method is_splittable
321 314 **/
322 315 Cell.prototype.is_splittable = function () {
323 316 return true;
324 317 };
325 318
326 319
327 320 /**
328 321 * can the cell be merged with other cells
329 322 * @method is_mergeable
330 323 **/
331 324 Cell.prototype.is_mergeable = function () {
332 325 return true;
333 326 };
334 327
335 328
336 329 /**
337 330 * @return {String} - the text before the cursor
338 331 * @method get_pre_cursor
339 332 **/
340 333 Cell.prototype.get_pre_cursor = function () {
341 334 var cursor = this.code_mirror.getCursor();
342 335 var text = this.code_mirror.getRange({line:0, ch:0}, cursor);
343 336 text = text.replace(/^\n+/, '').replace(/\n+$/, '');
344 337 return text;
345 338 }
346 339
347 340
348 341 /**
349 342 * @return {String} - the text after the cursor
350 343 * @method get_post_cursor
351 344 **/
352 345 Cell.prototype.get_post_cursor = function () {
353 346 var cursor = this.code_mirror.getCursor();
354 347 var last_line_num = this.code_mirror.lineCount()-1;
355 348 var last_line_len = this.code_mirror.getLine(last_line_num).length;
356 349 var end = {line:last_line_num, ch:last_line_len}
357 350 var text = this.code_mirror.getRange(cursor, end);
358 351 text = text.replace(/^\n+/, '').replace(/\n+$/, '');
359 352 return text;
360 353 };
361 354
362 355 /**
363 356 * Show/Hide CodeMirror LineNumber
364 357 * @method show_line_numbers
365 358 *
366 359 * @param value {Bool} show (true), or hide (false) the line number in CodeMirror
367 360 **/
368 361 Cell.prototype.show_line_numbers = function (value) {
369 362 this.code_mirror.setOption('lineNumbers', value);
370 363 this.code_mirror.refresh();
371 364 };
372 365
373 366 /**
374 367 * Toggle CodeMirror LineNumber
375 368 * @method toggle_line_numbers
376 369 **/
377 370 Cell.prototype.toggle_line_numbers = function () {
378 371 var val = this.code_mirror.getOption('lineNumbers');
379 372 this.show_line_numbers(!val);
380 373 };
381 374
382 375 /**
383 376 * Force codemirror highlight mode
384 377 * @method force_highlight
385 378 * @param {object} - CodeMirror mode
386 379 **/
387 380 Cell.prototype.force_highlight = function(mode) {
388 381 this.user_highlight = mode;
389 382 this.auto_highlight();
390 383 };
391 384
392 385 /**
393 386 * Try to autodetect cell highlight mode, or use selected mode
394 387 * @methods _auto_highlight
395 388 * @private
396 389 * @param {String|object|undefined} - CodeMirror mode | 'auto'
397 390 **/
398 391 Cell.prototype._auto_highlight = function (modes) {
399 392 //Here we handle manually selected modes
400 393 if( this.user_highlight != undefined && this.user_highlight != 'auto' )
401 394 {
402 395 var mode = this.user_highlight;
403 396 CodeMirror.autoLoadMode(this.code_mirror, mode);
404 397 this.code_mirror.setOption('mode', mode);
405 398 return;
406 399 }
407 400 var current_mode = this.code_mirror.getOption('mode', mode);
408 401 var first_line = this.code_mirror.getLine(0);
409 402 // loop on every pairs
410 403 for( var mode in modes) {
411 404 var regs = modes[mode]['reg'];
412 405 // only one key every time but regexp can't be keys...
413 406 for(var reg in regs ) {
414 407 // here we handle non magic_modes
415 408 if(first_line.match(regs[reg]) != null) {
416 409 if(current_mode == mode){
417 410 return;
418 411 }
419 412 if (mode.search('magic_') != 0) {
420 413 this.code_mirror.setOption('mode', mode);
421 414 CodeMirror.autoLoadMode(this.code_mirror, mode);
422 415 return;
423 416 }
424 417 var open = modes[mode]['open']|| "%%";
425 418 var close = modes[mode]['close']|| "%%end";
426 419 var mmode = mode;
427 420 mode = mmode.substr(6);
428 421 if(current_mode == mode){
429 422 return;
430 423 }
431 424 CodeMirror.autoLoadMode(this.code_mirror, mode);
432 425 // create on the fly a mode that swhitch between
433 426 // plain/text and smth else otherwise `%%` is
434 427 // source of some highlight issues.
435 428 // we use patchedGetMode to circumvent a bug in CM
436 429 CodeMirror.defineMode(mmode , function(config) {
437 430 return CodeMirror.multiplexingMode(
438 431 CodeMirror.patchedGetMode(config, 'text/plain'),
439 432 // always set someting on close
440 433 {open: open, close: close,
441 434 mode: CodeMirror.patchedGetMode(config, mode),
442 435 delimStyle: "delimit"
443 436 }
444 437 );
445 438 });
446 439 this.code_mirror.setOption('mode', mmode);
447 440 return;
448 441 }
449 442 }
450 443 }
451 444 // fallback on default
452 445 var default_mode
453 446 try {
454 447 default_mode = this._options.cm_config.mode;
455 448 } catch(e) {
456 449 default_mode = 'text/plain';
457 450 }
458 451 if( current_mode === default_mode){
459 452 return
460 453 }
461 454 this.code_mirror.setOption('mode', default_mode);
462 455 };
463 456
464 457 IPython.Cell = Cell;
465 458
466 459 return IPython;
467 460
468 461 }(IPython));
469 462
@@ -1,506 +1,501 b''
1 1 //----------------------------------------------------------------------------
2 2 // Copyright (C) 2008-2011 The IPython Development Team
3 3 //
4 4 // Distributed under the terms of the BSD License. The full license is in
5 5 // the file COPYING, distributed as part of this software.
6 6 //----------------------------------------------------------------------------
7 7
8 8 //============================================================================
9 9 // CodeCell
10 10 //============================================================================
11 11 /**
12 12 * An extendable module that provide base functionnality to create cell for notebook.
13 13 * @module IPython
14 14 * @namespace IPython
15 15 * @submodule CodeCell
16 16 */
17 17
18 18
19 19 /* local util for codemirror */
20 20 var posEq = function(a, b) {return a.line == b.line && a.ch == b.ch;};
21 21
22 22 /**
23 23 *
24 24 * function to delete until previous non blanking space character
25 25 * or first multiple of 4 tabstop.
26 26 * @private
27 27 */
28 28 CodeMirror.commands.delSpaceToPrevTabStop = function(cm){
29 29 var from = cm.getCursor(true), to = cm.getCursor(false), sel = !posEq(from, to);
30 30 if (!posEq(from, to)) { cm.replaceRange("", from, to); return; }
31 31 var cur = cm.getCursor(), line = cm.getLine(cur.line);
32 32 var tabsize = cm.getOption('tabSize');
33 33 var chToPrevTabStop = cur.ch-(Math.ceil(cur.ch/tabsize)-1)*tabsize;
34 34 from = {ch:cur.ch-chToPrevTabStop,line:cur.line};
35 35 var select = cm.getRange(from,cur);
36 36 if( select.match(/^\ +$/) !== null){
37 37 cm.replaceRange("",from,cur);
38 38 } else {
39 39 cm.deleteH(-1,"char");
40 40 }
41 41 };
42 42
43 43
44 44 var IPython = (function (IPython) {
45 45 "use strict";
46 46
47 47 var utils = IPython.utils;
48 48 var key = IPython.utils.keycodes;
49 49
50 50 /**
51 51 * A Cell conceived to write code.
52 52 *
53 53 * The kernel doesn't have to be set at creation time, in that case
54 54 * it will be null and set_kernel has to be called later.
55 55 * @class CodeCell
56 56 * @extends IPython.Cell
57 57 *
58 58 * @constructor
59 59 * @param {Object|null} kernel
60 60 * @param {object|undefined} [options]
61 61 * @param [options.cm_config] {object} config to pass to CodeMirror
62 62 */
63 63 var CodeCell = function (kernel, options) {
64 64 this.kernel = kernel || null;
65 65 this.collapsed = false;
66 66
67 67 // create all attributed in constructor function
68 68 // even if null for V8 VM optimisation
69 69 this.input_prompt_number = null;
70 70 this.celltoolbar = null;
71 71 this.output_area = null;
72 72 this.last_msg_id = null;
73 73 this.completer = null;
74 74
75 75
76 76 var cm_overwrite_options = {
77 77 onKeyEvent: $.proxy(this.handle_codemirror_keyevent,this)
78 78 };
79 79
80 80 options = this.mergeopt(CodeCell, options, {cm_config:cm_overwrite_options});
81 81
82 82 IPython.Cell.apply(this,[options]);
83 83
84 84 // Attributes we want to override in this subclass.
85 85 this.cell_type = "code";
86 86
87 87 var that = this;
88 88 this.element.focusout(
89 89 function() { that.auto_highlight(); }
90 90 );
91 91 };
92 92
93 93 CodeCell.options_default = {
94 94 cm_config : {
95 95 extraKeys: {
96 96 "Tab" : "indentMore",
97 97 "Shift-Tab" : "indentLess",
98 98 "Backspace" : "delSpaceToPrevTabStop",
99 99 "Cmd-/" : "toggleComment",
100 100 "Ctrl-/" : "toggleComment"
101 101 },
102 102 mode: 'ipython',
103 103 theme: 'ipython',
104 104 matchBrackets: true
105 105 }
106 106 };
107 107
108 108
109 109 CodeCell.prototype = new IPython.Cell();
110 110
111 111 /**
112 112 * @method auto_highlight
113 113 */
114 114 CodeCell.prototype.auto_highlight = function () {
115 115 this._auto_highlight(IPython.config.cell_magic_highlight);
116 116 };
117 117
118 118 /** @method create_element */
119 119 CodeCell.prototype.create_element = function () {
120 120 IPython.Cell.prototype.create_element.apply(this, arguments);
121 121
122 122 var cell = $('<div></div>').addClass('cell border-box-sizing code_cell');
123 123 cell.attr('tabindex','2');
124 124
125 125 var input = $('<div></div>').addClass('input');
126 126 var prompt = $('<div/>').addClass('prompt input_prompt');
127 127 var inner_cell = $('<div/>').addClass('inner_cell');
128 128 this.celltoolbar = new IPython.CellToolbar(this);
129 129 inner_cell.append(this.celltoolbar.element);
130 130 var input_area = $('<div/>').addClass('input_area');
131 131 this.code_mirror = CodeMirror(input_area.get(0), this.cm_config);
132 132 $(this.code_mirror.getInputField()).attr("spellcheck", "false");
133 133 inner_cell.append(input_area);
134 134 input.append(prompt).append(inner_cell);
135 135 var output = $('<div></div>');
136 136 cell.append(input).append(output);
137 137 this.element = cell;
138 138 this.output_area = new IPython.OutputArea(output, true);
139 139 this.completer = new IPython.Completer(this);
140 140 };
141 141
142 142 /** @method bind_events */
143 143 CodeCell.prototype.bind_events = function () {
144 144 IPython.Cell.prototype.bind_events.apply(this);
145 145 var that = this;
146 146
147 147 this.element.focusout(
148 148 function() { that.auto_highlight(); }
149 149 );
150 150 };
151 151
152 152 /**
153 153 * This method gets called in CodeMirror's onKeyDown/onKeyPress
154 154 * handlers and is used to provide custom key handling. Its return
155 155 * value is used to determine if CodeMirror should ignore the event:
156 156 * true = ignore, false = don't ignore.
157 157 * @method handle_codemirror_keyevent
158 158 */
159 159 CodeCell.prototype.handle_codemirror_keyevent = function (editor, event) {
160
161 160 var that = this;
162 161
163 162 if (this.mode === 'command') {
164 163 return false
165 164 } else if (this.mode === 'edit') {
166 165 // whatever key is pressed, first, cancel the tooltip request before
167 166 // they are sent, and remove tooltip if any, except for tab again
168 167 if (event.type === 'keydown' && event.which != key.TAB ) {
169 168 IPython.tooltip.remove_and_cancel_tooltip();
170 169 };
171 170
172 171 var cur = editor.getCursor();
173 172 if (event.keyCode === key.ENTER){
174 173 this.auto_highlight();
175 174 }
176 175
177 if (event.keyCode === key.ENTER && (event.shiftKey || event.ctrlKey)) {
176 if (event.keyCode === key.ENTER && (event.shiftKey || event.ctrlKey || event.altKey)) {
178 177 // Always ignore shift-enter in CodeMirror as we handle it.
179 178 return true;
180 179
181 180 } else if (event.which === 40 && event.type === 'keypress' && IPython.tooltip.time_before_tooltip >= 0) {
182 181 // triger on keypress (!) otherwise inconsistent event.which depending on plateform
183 182 // browser and keyboard layout !
184 183 // Pressing '(' , request tooltip, don't forget to reappend it
185 184 // The second argument says to hide the tooltip if the docstring
186 185 // is actually empty
187 186 IPython.tooltip.pending(that, true);
188 187 } else if (event.which === key.UPARROW && event.type === 'keydown') {
189 188 // If we are not at the top, let CM handle the up arrow and
190 189 // prevent the global keydown handler from handling it.
191 190 if (!that.at_top()) {
192 191 event.stop();
193 192 return false;
194 193 } else {
195 194 return true;
196 195 };
197 196 } else if (event.which === key.ESC) {
198 197 IPython.tooltip.remove_and_cancel_tooltip(true);
199 198 return true;
200 199 } else if (event.which === key.DOWNARROW && event.type === 'keydown') {
201 200 // If we are not at the bottom, let CM handle the down arrow and
202 201 // prevent the global keydown handler from handling it.
203 202 if (!that.at_bottom()) {
204 203 event.stop();
205 204 return false;
206 205 } else {
207 206 return true;
208 207 };
209 208 } else if (event.keyCode === key.TAB && event.type == 'keydown' && event.shiftKey) {
210 209 if (editor.somethingSelected()){
211 210 var anchor = editor.getCursor("anchor");
212 211 var head = editor.getCursor("head");
213 212 if( anchor.line != head.line){
214 213 return false;
215 214 }
216 215 }
217 216 }
218 217 IPython.tooltip.request(that);
219 218 event.stop();
220 219 return true;
221 220 } else if (event.keyCode === key.TAB && event.type == 'keydown') {
222 221 // Tab completion.
223 222 IPython.tooltip.remove_and_cancel_tooltip();
224 223 if (editor.somethingSelected()) {
225 224 return false;
226 225 }
227 226 var pre_cursor = editor.getRange({line:cur.line,ch:0},cur);
228 227 if (pre_cursor.trim() === "") {
229 228 // Don't autocomplete if the part of the line before the cursor
230 229 // is empty. In this case, let CodeMirror handle indentation.
231 230 return false;
232 231 } else {
233 232 // keypress/keyup also trigger on TAB press, and we don't want to
234 233 // use those to disable tab completion.
235 234 return false;
236 235 };
237 236 return false;
238 237 }
239 238 return false;
240 239 };
241 240
242 241
243 242 // Kernel related calls.
244 243
245 244 CodeCell.prototype.set_kernel = function (kernel) {
246 245 this.kernel = kernel;
247 246 };
248 247
249 248 /**
250 249 * Execute current code cell to the kernel
251 250 * @method execute
252 251 */
253 252 CodeCell.prototype.execute = function () {
254 253 this.output_area.clear_output();
255 254 this.set_input_prompt('*');
256 255 this.element.addClass("running");
257 256 if (this.last_msg_id) {
258 257 this.kernel.clear_callbacks_for_msg(this.last_msg_id);
259 258 }
260 259 var callbacks = this.get_callbacks();
261 260
262 261 this.last_msg_id = this.kernel.execute(this.get_text(), callbacks, {silent: false, store_history: true});
263 262 };
264 263
265 264 /**
266 265 * Construct the default callbacks for
267 266 * @method get_callbacks
268 267 */
269 268 CodeCell.prototype.get_callbacks = function () {
270 269 return {
271 270 shell : {
272 271 reply : $.proxy(this._handle_execute_reply, this),
273 272 payload : {
274 273 set_next_input : $.proxy(this._handle_set_next_input, this),
275 274 page : $.proxy(this._open_with_pager, this)
276 275 }
277 276 },
278 277 iopub : {
279 278 output : $.proxy(this.output_area.handle_output, this.output_area),
280 279 clear_output : $.proxy(this.output_area.handle_clear_output, this.output_area),
281 280 },
282 281 input : $.proxy(this._handle_input_request, this)
283 282 };
284 283 };
285 284
286 285 CodeCell.prototype._open_with_pager = function (payload) {
287 286 $([IPython.events]).trigger('open_with_text.Pager', payload);
288 287 };
289 288
290 289 /**
291 290 * @method _handle_execute_reply
292 291 * @private
293 292 */
294 293 CodeCell.prototype._handle_execute_reply = function (msg) {
295 294 this.set_input_prompt(msg.content.execution_count);
296 295 this.element.removeClass("running");
297 296 $([IPython.events]).trigger('set_dirty.Notebook', {value: true});
298 297 };
299 298
300 299 /**
301 300 * @method _handle_set_next_input
302 301 * @private
303 302 */
304 303 CodeCell.prototype._handle_set_next_input = function (payload) {
305 304 var data = {'cell': this, 'text': payload.text};
306 305 $([IPython.events]).trigger('set_next_input.Notebook', data);
307 306 };
308 307
309 308 /**
310 309 * @method _handle_input_request
311 310 * @private
312 311 */
313 312 CodeCell.prototype._handle_input_request = function (msg) {
314 313 this.output_area.append_raw_input(msg);
315 314 };
316 315
317 316
318 317 // Basic cell manipulation.
319 318
320 319 CodeCell.prototype.select = function () {
321 320 var cont = IPython.Cell.prototype.select.apply(this);
322 console.log('CodeCell.select', cont);
323 321 if (cont) {
324 322 this.code_mirror.refresh();
325 323 this.auto_highlight();
326 324 };
327 325 return cont;
328 326 };
329 327
330 328 CodeCell.prototype.render = function () {
331 329 var cont = IPython.Cell.prototype.render.apply(this);
332 console.log('CodeCell.render');
333 330 // Always execute, even if we are already in the rendered state
334 331 return cont;
335 332 };
336 333
337 334 CodeCell.prototype.unrender = function () {
338 335 // CodeCell is always rendered
339 336 return false;
340 337 };
341 338
342 339 CodeCell.prototype.command_mode = function () {
343 340 var cont = IPython.Cell.prototype.command_mode.apply(this);
344 console.log('CodeCell.command_mode');
345 341 if (cont) {
346 342 this.focus_cell();
347 343 };
348 344 return cont;
349 345 }
350 346
351 347 CodeCell.prototype.edit_mode = function () {
352 348 var cont = IPython.Cell.prototype.edit_mode.apply(this);
353 console.log('CodeCell.edit_mode');
354 349 if (cont) {
355 350 this.focus_editor();
356 351 };
357 352 return cont;
358 353 }
359 354
360 355 CodeCell.prototype.select_all = function () {
361 356 var start = {line: 0, ch: 0};
362 357 var nlines = this.code_mirror.lineCount();
363 358 var last_line = this.code_mirror.getLine(nlines-1);
364 359 var end = {line: nlines-1, ch: last_line.length};
365 360 this.code_mirror.setSelection(start, end);
366 361 };
367 362
368 363
369 364 CodeCell.prototype.collapse = function () {
370 365 this.collapsed = true;
371 366 this.output_area.collapse();
372 367 };
373 368
374 369
375 370 CodeCell.prototype.expand = function () {
376 371 this.collapsed = false;
377 372 this.output_area.expand();
378 373 };
379 374
380 375
381 376 CodeCell.prototype.toggle_output = function () {
382 377 this.collapsed = Boolean(1 - this.collapsed);
383 378 this.output_area.toggle_output();
384 379 };
385 380
386 381
387 382 CodeCell.prototype.toggle_output_scroll = function () {
388 383 this.output_area.toggle_scroll();
389 384 };
390 385
391 386
392 387 CodeCell.input_prompt_classical = function (prompt_value, lines_number) {
393 388 var ns = prompt_value || "&nbsp;";
394 389 return 'In&nbsp;[' + ns + ']:';
395 390 };
396 391
397 392 CodeCell.input_prompt_continuation = function (prompt_value, lines_number) {
398 393 var html = [CodeCell.input_prompt_classical(prompt_value, lines_number)];
399 394 for(var i=1; i < lines_number; i++) {
400 395 html.push(['...:']);
401 396 }
402 397 return html.join('<br/>');
403 398 };
404 399
405 400 CodeCell.input_prompt_function = CodeCell.input_prompt_classical;
406 401
407 402
408 403 CodeCell.prototype.set_input_prompt = function (number) {
409 404 var nline = 1;
410 405 if (this.code_mirror !== undefined) {
411 406 nline = this.code_mirror.lineCount();
412 407 }
413 408 this.input_prompt_number = number;
414 409 var prompt_html = CodeCell.input_prompt_function(this.input_prompt_number, nline);
415 410 this.element.find('div.input_prompt').html(prompt_html);
416 411 };
417 412
418 413
419 414 CodeCell.prototype.clear_input = function () {
420 415 this.code_mirror.setValue('');
421 416 };
422 417
423 418
424 419 CodeCell.prototype.get_text = function () {
425 420 return this.code_mirror.getValue();
426 421 };
427 422
428 423
429 424 CodeCell.prototype.set_text = function (code) {
430 425 return this.code_mirror.setValue(code);
431 426 };
432 427
433 428
434 429 CodeCell.prototype.at_top = function () {
435 430 var cursor = this.code_mirror.getCursor();
436 431 if (cursor.line === 0 && cursor.ch === 0) {
437 432 return true;
438 433 } else {
439 434 return false;
440 435 }
441 436 };
442 437
443 438
444 439 CodeCell.prototype.at_bottom = function () {
445 440 var cursor = this.code_mirror.getCursor();
446 441 if (cursor.line === (this.code_mirror.lineCount()-1) && cursor.ch === this.code_mirror.getLine(cursor.line).length) {
447 442 return true;
448 443 } else {
449 444 return false;
450 445 }
451 446 };
452 447
453 448
454 449 CodeCell.prototype.clear_output = function (wait) {
455 450 this.output_area.clear_output(wait);
456 451 };
457 452
458 453
459 454 // JSON serialization
460 455
461 456 CodeCell.prototype.fromJSON = function (data) {
462 457 IPython.Cell.prototype.fromJSON.apply(this, arguments);
463 458 if (data.cell_type === 'code') {
464 459 if (data.input !== undefined) {
465 460 this.set_text(data.input);
466 461 // make this value the starting point, so that we can only undo
467 462 // to this state, instead of a blank cell
468 463 this.code_mirror.clearHistory();
469 464 this.auto_highlight();
470 465 }
471 466 if (data.prompt_number !== undefined) {
472 467 this.set_input_prompt(data.prompt_number);
473 468 } else {
474 469 this.set_input_prompt();
475 470 }
476 471 this.output_area.fromJSON(data.outputs);
477 472 if (data.collapsed !== undefined) {
478 473 if (data.collapsed) {
479 474 this.collapse();
480 475 } else {
481 476 this.expand();
482 477 }
483 478 }
484 479 }
485 480 };
486 481
487 482
488 483 CodeCell.prototype.toJSON = function () {
489 484 var data = IPython.Cell.prototype.toJSON.apply(this);
490 485 data.input = this.get_text();
491 486 // is finite protect against undefined and '*' value
492 487 if (isFinite(this.input_prompt_number)) {
493 488 data.prompt_number = this.input_prompt_number;
494 489 }
495 490 var outputs = this.output_area.toJSON();
496 491 data.outputs = outputs;
497 492 data.language = 'python';
498 493 data.collapsed = this.collapsed;
499 494 return data;
500 495 };
501 496
502 497
503 498 IPython.CodeCell = CodeCell;
504 499
505 500 return IPython;
506 501 }(IPython));
@@ -1,2338 +1,2342 b''
1 1 //----------------------------------------------------------------------------
2 2 // Copyright (C) 2011 The IPython Development Team
3 3 //
4 4 // Distributed under the terms of the BSD License. The full license is in
5 5 // the file COPYING, distributed as part of this software.
6 6 //----------------------------------------------------------------------------
7 7
8 8 //============================================================================
9 9 // Notebook
10 10 //============================================================================
11 11
12 12 var IPython = (function (IPython) {
13 13 "use strict";
14 14
15 15 var utils = IPython.utils;
16 16 var key = IPython.utils.keycodes;
17 17
18 18 /**
19 19 * A notebook contains and manages cells.
20 20 *
21 21 * @class Notebook
22 22 * @constructor
23 23 * @param {String} selector A jQuery selector for the notebook's DOM element
24 24 * @param {Object} [options] A config object
25 25 */
26 26 var Notebook = function (selector, options) {
27 27 var options = options || {};
28 28 this._baseProjectUrl = options.baseProjectUrl;
29 29 this.notebook_path = options.notebookPath;
30 30 this.notebook_name = options.notebookName;
31 31 this.element = $(selector);
32 32 this.element.scroll();
33 33 this.element.data("notebook", this);
34 34 this.next_prompt_number = 1;
35 35 this.session = null;
36 36 this.kernel = null;
37 37 this.clipboard = null;
38 38 this.undelete_backup = null;
39 39 this.undelete_index = null;
40 40 this.undelete_below = false;
41 41 this.paste_enabled = false;
42 42 this.mode = 'command';
43 this.edit_index = null;
44 43 this.set_dirty(false);
45 44 this.metadata = {};
46 45 this._checkpoint_after_save = false;
47 46 this.last_checkpoint = null;
48 47 this.checkpoints = [];
49 48 this.autosave_interval = 0;
50 49 this.autosave_timer = null;
51 50 // autosave *at most* every two minutes
52 51 this.minimum_autosave_interval = 120000;
53 52 // single worksheet for now
54 53 this.worksheet_metadata = {};
55 54 this.control_key_active = false;
56 55 this.notebook_name_blacklist_re = /[\/\\:]/;
57 56 this.nbformat = 3 // Increment this when changing the nbformat
58 57 this.nbformat_minor = 0 // Increment this when changing the nbformat
59 58 this.style();
60 59 this.create_elements();
61 60 this.bind_events();
62 61 };
63 62
64 63 /**
65 64 * Tweak the notebook's CSS style.
66 65 *
67 66 * @method style
68 67 */
69 68 Notebook.prototype.style = function () {
70 69 $('div#notebook').addClass('border-box-sizing');
71 70 };
72 71
73 72 /**
74 73 * Get the root URL of the notebook server.
75 74 *
76 75 * @method baseProjectUrl
77 76 * @return {String} The base project URL
78 77 */
79 78 Notebook.prototype.baseProjectUrl = function() {
80 79 return this._baseProjectUrl || $('body').data('baseProjectUrl');
81 80 };
82 81
83 82 Notebook.prototype.notebookName = function() {
84 83 return $('body').data('notebookName');
85 84 };
86 85
87 86 Notebook.prototype.notebookPath = function() {
88 87 return $('body').data('notebookPath');
89 88 };
90 89
91 90 /**
92 91 * Create an HTML and CSS representation of the notebook.
93 92 *
94 93 * @method create_elements
95 94 */
96 95 Notebook.prototype.create_elements = function () {
97 96 // We add this end_space div to the end of the notebook div to:
98 97 // i) provide a margin between the last cell and the end of the notebook
99 98 // ii) to prevent the div from scrolling up when the last cell is being
100 99 // edited, but is too low on the page, which browsers will do automatically.
101 100 var that = this;
102 101 this.container = $("<div/>").addClass("container").attr("id", "notebook-container");
103 102 var end_space = $('<div/>').addClass('end_space');
104 103 end_space.dblclick(function (e) {
105 104 var ncells = that.ncells();
106 105 that.insert_cell_below('code',ncells-1);
107 106 });
108 107 this.element.append(this.container);
109 108 this.container.append(end_space);
110 109 $('div#notebook').addClass('border-box-sizing');
111 110 };
112 111
113 112 /**
114 113 * Bind JavaScript events: key presses and custom IPython events.
115 114 *
116 115 * @method bind_events
117 116 */
118 117 Notebook.prototype.bind_events = function () {
119 118 var that = this;
120 119
121 120 $([IPython.events]).on('set_next_input.Notebook', function (event, data) {
122 121 var index = that.find_cell_index(data.cell);
123 122 var new_cell = that.insert_cell_below('code',index);
124 123 new_cell.set_text(data.text);
125 124 that.dirty = true;
126 125 });
127 126
128 127 $([IPython.events]).on('set_dirty.Notebook', function (event, data) {
129 128 that.dirty = data.value;
130 129 });
131 130
132 131 $([IPython.events]).on('select.Cell', function (event, data) {
133 132 var index = that.find_cell_index(data.cell);
134 133 that.select(index);
135 134 });
136 135
137 136 $([IPython.events]).on('edit_mode.Cell', function (event, data) {
138 137 var index = that.find_cell_index(data.cell);
139 138 that.select(index);
140 139 that.edit_mode();
141 140 });
142
141
142 $([IPython.events]).on('command_mode.Cell', function (event, data) {
143 that.command_mode();
144 });
145
143 146 $([IPython.events]).on('status_autorestarting.Kernel', function () {
144 147 IPython.dialog.modal({
145 148 title: "Kernel Restarting",
146 149 body: "The kernel appears to have died. It will restart automatically.",
147 150 buttons: {
148 151 OK : {
149 152 class : "btn-primary"
150 153 }
151 154 }
152 155 });
153 156 });
154 157
155 158 $(document).keydown(function (event) {
156 159
157 160 // Event handlers for both command and edit mode
158 161 if ((event.ctrlKey || event.metaKey) && event.keyCode==83) {
159 162 // Save (CTRL+S) or (Command+S on Mac)
160 163 that.save_checkpoint();
161 164 event.preventDefault();
162 165 return false;
163 166 } else if (event.which === key.ESC) {
164 167 // Intercept escape at highest level to avoid closing
165 168 // websocket connection with firefox
166 169 event.preventDefault();
167 170 // Don't return yet to allow edit/command modes to handle
168 171 } else if (event.which === key.SHIFT) {
169 172 // ignore shift keydown
170 173 return true;
171 174 } else if (event.which === key.ENTER && event.shiftKey) {
172 175 that.execute_selected_cell('shift');
173 176 return false;
174 177 } else if (event.which === key.ENTER && event.altKey) {
175 178 // Execute code cell, and insert new in place
176 179 that.execute_selected_cell('alt');
177 180 return false;
178 181 } else if (event.which === key.ENTER && event.ctrlKey) {
179 182 that.execute_selected_cell('ctrl');
180 183 return false;
181 184 }
182 185
183 186 // Event handlers for edit mode
184 187 if (that.mode === 'edit') {
185 188 if (event.which === key.ESC) {
186 189 // ESC
187 190 that.command_mode();
188 191 return false;
189 192 } else if (event.which === 77 && event.ctrlKey) {
190 193 // Ctrl-m
191 194 that.command_mode();
192 195 return false;
193 196 } else if (event.which === key.UPARROW && !event.shiftKey) {
194 197 var cell = that.get_selected_cell();
195 198 if (cell && cell.at_top()) {
196 199 event.preventDefault();
197 200 that.command_mode()
198 201 that.select_prev();
199 202 that.edit_mode();
200 203 return false;
201 204 };
202 205 } else if (event.which === key.DOWNARROW && !event.shiftKey) {
203 206 var cell = that.get_selected_cell();
204 207 if (cell && cell.at_bottom()) {
205 208 event.preventDefault();
206 209 that.command_mode()
207 210 that.select_next();
208 211 that.edit_mode();
209 212 return false;
210 213 };
211 214 };
212 215 // Event handlers for command mode
213 216 } else if (that.mode === 'command' && !(event.ctrlKey || event.altKey)) {
214 217 if (event.which === key.ENTER && !(event.ctrlKey || event.altKey || event.shiftKey)) {
215 218 // Enter edit mode = ENTER alone
216 219 that.edit_mode();
217 220 return false
218 221 } else if (event.which === key.UPARROW && !event.shiftKey) {
219 222 var index = that.get_selected_index();
220 223 if (index !== 0 && index !== null) {
221 224 that.select_prev();
222 225 var cell = that.get_selected_cell();
223 226 cell.focus_cell();
224 227 };
225 228 return false;
226 229 } else if (event.which === key.DOWNARROW && !event.shiftKey) {
227 230 var index = that.get_selected_index();
228 231 if (index !== (that.ncells()-1) && index !== null) {
229 232 that.select_next();
230 233 var cell = that.get_selected_cell();
231 234 cell.focus_cell();
232 235 };
233 236 return false;
234 237 } else if (event.which === 88) {
235 238 // Cut selected cell = x
236 239 that.cut_cell();
237 240 return false;
238 241 } else if (event.which === 67) {
239 242 // Copy selected cell = c
240 243 that.copy_cell();
241 244 return false;
242 245 } else if (event.which === 86) {
243 246 // Paste below selected cell = v
244 247 that.paste_cell_below();
245 248 return false;
246 249 } else if (event.which === 68) {
247 250 // Delete selected cell = d
248 251 that.delete_cell();
249 252 return false;
250 253 } else if (event.which === 65) {
251 254 // Insert code cell above selected = a
252 255 that.insert_cell_above('code');
253 256 that.select_prev();
254 257 return false;
255 258 } else if (event.which === 66) {
256 259 // Insert code cell below selected = b
257 260 that.insert_cell_below('code');
258 261 that.select_next();
259 262 return false;
260 263 } else if (event.which === 89) {
261 264 // To code = y
262 265 that.to_code();
263 266 return false;
264 267 } else if (event.which === 77) {
265 268 // To markdown = m
266 269 that.to_markdown();
267 270 return false;
268 271 } else if (event.which === 84) {
269 272 // To Raw = t
270 273 that.to_raw();
271 274 return false;
272 275 } else if (event.which === 49) {
273 276 // To Heading 1 = 1
274 277 that.to_heading(undefined, 1);
275 278 return false;
276 279 } else if (event.which === 50) {
277 280 // To Heading 2 = 2
278 281 that.to_heading(undefined, 2);
279 282 return false;
280 283 } else if (event.which === 51) {
281 284 // To Heading 3 = 3
282 285 that.to_heading(undefined, 3);
283 286 return false;
284 287 } else if (event.which === 52) {
285 288 // To Heading 4 = 4
286 289 that.to_heading(undefined, 4);
287 290 return false;
288 291 } else if (event.which === 53) {
289 292 // To Heading 5 = 5
290 293 that.to_heading(undefined, 5);
291 294 return false;
292 295 } else if (event.which === 54) {
293 296 // To Heading 6 = 6
294 297 that.to_heading(undefined, 6);
295 298 return false;
296 299 } else if (event.which === 79) {
297 300 // Toggle output = o
298 301 if (event.shiftKey) {
299 302 that.toggle_output_scroll();
300 303 } else {
301 304 that.toggle_output();
302 305 };
303 306 return false;
304 307 } else if (event.which === 83) {
305 308 // Save notebook = s
306 309 that.save_checkpoint();
307 310 that.control_key_active = false;
308 311 return false;
309 312 } else if (event.which === 74) {
310 313 // Move cell down = j
311 314 that.move_cell_down();
312 315 return false;
313 316 } else if (event.which === 75) {
314 317 // Move cell up = k
315 318 that.move_cell_up();
316 319 return false;
317 320 } else if (event.which === 80) {
318 321 // Select previous = p
319 322 that.select_prev();
320 323 return false;
321 324 } else if (event.which === 78) {
322 325 // Select next = n
323 326 that.select_next();
324 327 return false;
325 328 } else if (event.which === 76) {
326 329 // Toggle line numbers = l
327 330 that.cell_toggle_line_numbers();
328 331 return false;
329 332 } else if (event.which === 73) {
330 333 // Interrupt kernel = i
331 334 that.kernel.interrupt();
332 335 return false;
333 336 } else if (event.which === 190) {
334 337 // Restart kernel = . # matches qt console
335 338 that.restart_kernel();
336 339 return false;
337 340 } else if (event.which === 72) {
338 341 // Show keyboard shortcuts = h
339 342 IPython.quick_help.show_keyboard_shortcuts();
340 343 return false;
341 344 } else if (event.which === 90) {
342 345 // Undo last cell delete = z
343 346 that.undelete();
344 347 return false;
345 348 };
346 349 };
347 350
348 351 // If we havn't handled it, let someone else.
349 352 return true;
350 353 });
351 354
352 355 var collapse_time = function (time) {
353 356 var app_height = $('#ipython-main-app').height(); // content height
354 357 var splitter_height = $('div#pager_splitter').outerHeight(true);
355 358 var new_height = app_height - splitter_height;
356 359 that.element.animate({height : new_height + 'px'}, time);
357 360 };
358 361
359 362 this.element.bind('collapse_pager', function (event, extrap) {
360 363 var time = (extrap != undefined) ? ((extrap.duration != undefined ) ? extrap.duration : 'fast') : 'fast';
361 364 collapse_time(time);
362 365 });
363 366
364 367 var expand_time = function (time) {
365 368 var app_height = $('#ipython-main-app').height(); // content height
366 369 var splitter_height = $('div#pager_splitter').outerHeight(true);
367 370 var pager_height = $('div#pager').outerHeight(true);
368 371 var new_height = app_height - pager_height - splitter_height;
369 372 that.element.animate({height : new_height + 'px'}, time);
370 373 };
371 374
372 375 this.element.bind('expand_pager', function (event, extrap) {
373 376 var time = (extrap != undefined) ? ((extrap.duration != undefined ) ? extrap.duration : 'fast') : 'fast';
374 377 expand_time(time);
375 378 });
376 379
377 380 // Firefox 22 broke $(window).on("beforeunload")
378 381 // I'm not sure why or how.
379 382 window.onbeforeunload = function (e) {
380 383 // TODO: Make killing the kernel configurable.
381 384 var kill_kernel = false;
382 385 if (kill_kernel) {
383 386 that.session.kill_kernel();
384 387 }
385 388 // if we are autosaving, trigger an autosave on nav-away.
386 389 // still warn, because if we don't the autosave may fail.
387 390 if (that.dirty) {
388 391 if ( that.autosave_interval ) {
389 392 // schedule autosave in a timeout
390 393 // this gives you a chance to forcefully discard changes
391 394 // by reloading the page if you *really* want to.
392 395 // the timer doesn't start until you *dismiss* the dialog.
393 396 setTimeout(function () {
394 397 if (that.dirty) {
395 398 that.save_notebook();
396 399 }
397 400 }, 1000);
398 401 return "Autosave in progress, latest changes may be lost.";
399 402 } else {
400 403 return "Unsaved changes will be lost.";
401 404 }
402 405 };
403 406 // Null is the *only* return value that will make the browser not
404 407 // pop up the "don't leave" dialog.
405 408 return null;
406 409 };
407 410 };
408 411
409 412 /**
410 413 * Set the dirty flag, and trigger the set_dirty.Notebook event
411 414 *
412 415 * @method set_dirty
413 416 */
414 417 Notebook.prototype.set_dirty = function (value) {
415 418 if (value === undefined) {
416 419 value = true;
417 420 }
418 421 if (this.dirty == value) {
419 422 return;
420 423 }
421 424 $([IPython.events]).trigger('set_dirty.Notebook', {value: value});
422 425 };
423 426
424 427 /**
425 428 * Scroll the top of the page to a given cell.
426 429 *
427 430 * @method scroll_to_cell
428 431 * @param {Number} cell_number An index of the cell to view
429 432 * @param {Number} time Animation time in milliseconds
430 433 * @return {Number} Pixel offset from the top of the container
431 434 */
432 435 Notebook.prototype.scroll_to_cell = function (cell_number, time) {
433 436 var cells = this.get_cells();
434 437 var time = time || 0;
435 438 cell_number = Math.min(cells.length-1,cell_number);
436 439 cell_number = Math.max(0 ,cell_number);
437 440 var scroll_value = cells[cell_number].element.position().top-cells[0].element.position().top ;
438 441 this.element.animate({scrollTop:scroll_value}, time);
439 442 return scroll_value;
440 443 };
441 444
442 445 /**
443 446 * Scroll to the bottom of the page.
444 447 *
445 448 * @method scroll_to_bottom
446 449 */
447 450 Notebook.prototype.scroll_to_bottom = function () {
448 451 this.element.animate({scrollTop:this.element.get(0).scrollHeight}, 0);
449 452 };
450 453
451 454 /**
452 455 * Scroll to the top of the page.
453 456 *
454 457 * @method scroll_to_top
455 458 */
456 459 Notebook.prototype.scroll_to_top = function () {
457 460 this.element.animate({scrollTop:0}, 0);
458 461 };
459 462
460 463 // Edit Notebook metadata
461 464
462 465 Notebook.prototype.edit_metadata = function () {
463 466 var that = this;
464 467 IPython.dialog.edit_metadata(this.metadata, function (md) {
465 468 that.metadata = md;
466 469 }, 'Notebook');
467 470 };
468 471
469 472 // Cell indexing, retrieval, etc.
470 473
471 474 /**
472 475 * Get all cell elements in the notebook.
473 476 *
474 477 * @method get_cell_elements
475 478 * @return {jQuery} A selector of all cell elements
476 479 */
477 480 Notebook.prototype.get_cell_elements = function () {
478 481 return this.container.children("div.cell");
479 482 };
480 483
481 484 /**
482 485 * Get a particular cell element.
483 486 *
484 487 * @method get_cell_element
485 488 * @param {Number} index An index of a cell to select
486 489 * @return {jQuery} A selector of the given cell.
487 490 */
488 491 Notebook.prototype.get_cell_element = function (index) {
489 492 var result = null;
490 493 var e = this.get_cell_elements().eq(index);
491 494 if (e.length !== 0) {
492 495 result = e;
493 496 }
494 497 return result;
495 498 };
496 499
497 500 /**
498 501 * Count the cells in this notebook.
499 502 *
500 503 * @method ncells
501 504 * @return {Number} The number of cells in this notebook
502 505 */
503 506 Notebook.prototype.ncells = function () {
504 507 return this.get_cell_elements().length;
505 508 };
506 509
507 510 /**
508 511 * Get all Cell objects in this notebook.
509 512 *
510 513 * @method get_cells
511 514 * @return {Array} This notebook's Cell objects
512 515 */
513 516 // TODO: we are often calling cells as cells()[i], which we should optimize
514 517 // to cells(i) or a new method.
515 518 Notebook.prototype.get_cells = function () {
516 519 return this.get_cell_elements().toArray().map(function (e) {
517 520 return $(e).data("cell");
518 521 });
519 522 };
520 523
521 524 /**
522 525 * Get a Cell object from this notebook.
523 526 *
524 527 * @method get_cell
525 528 * @param {Number} index An index of a cell to retrieve
526 529 * @return {Cell} A particular cell
527 530 */
528 531 Notebook.prototype.get_cell = function (index) {
529 532 var result = null;
530 533 var ce = this.get_cell_element(index);
531 534 if (ce !== null) {
532 535 result = ce.data('cell');
533 536 }
534 537 return result;
535 538 }
536 539
537 540 /**
538 541 * Get the cell below a given cell.
539 542 *
540 543 * @method get_next_cell
541 544 * @param {Cell} cell The provided cell
542 545 * @return {Cell} The next cell
543 546 */
544 547 Notebook.prototype.get_next_cell = function (cell) {
545 548 var result = null;
546 549 var index = this.find_cell_index(cell);
547 550 if (this.is_valid_cell_index(index+1)) {
548 551 result = this.get_cell(index+1);
549 552 }
550 553 return result;
551 554 }
552 555
553 556 /**
554 557 * Get the cell above a given cell.
555 558 *
556 559 * @method get_prev_cell
557 560 * @param {Cell} cell The provided cell
558 561 * @return {Cell} The previous cell
559 562 */
560 563 Notebook.prototype.get_prev_cell = function (cell) {
561 564 // TODO: off-by-one
562 565 // nb.get_prev_cell(nb.get_cell(1)) is null
563 566 var result = null;
564 567 var index = this.find_cell_index(cell);
565 568 if (index !== null && index > 1) {
566 569 result = this.get_cell(index-1);
567 570 }
568 571 return result;
569 572 }
570 573
571 574 /**
572 575 * Get the numeric index of a given cell.
573 576 *
574 577 * @method find_cell_index
575 578 * @param {Cell} cell The provided cell
576 579 * @return {Number} The cell's numeric index
577 580 */
578 581 Notebook.prototype.find_cell_index = function (cell) {
579 582 var result = null;
580 583 this.get_cell_elements().filter(function (index) {
581 584 if ($(this).data("cell") === cell) {
582 585 result = index;
583 586 };
584 587 });
585 588 return result;
586 589 };
587 590
588 591 /**
589 592 * Get a given index , or the selected index if none is provided.
590 593 *
591 594 * @method index_or_selected
592 595 * @param {Number} index A cell's index
593 596 * @return {Number} The given index, or selected index if none is provided.
594 597 */
595 598 Notebook.prototype.index_or_selected = function (index) {
596 599 var i;
597 600 if (index === undefined || index === null) {
598 601 i = this.get_selected_index();
599 602 if (i === null) {
600 603 i = 0;
601 604 }
602 605 } else {
603 606 i = index;
604 607 }
605 608 return i;
606 609 };
607 610
608 611 /**
609 612 * Get the currently selected cell.
610 613 * @method get_selected_cell
611 614 * @return {Cell} The selected cell
612 615 */
613 616 Notebook.prototype.get_selected_cell = function () {
614 617 var index = this.get_selected_index();
615 618 return this.get_cell(index);
616 619 };
617 620
618 621 /**
619 622 * Check whether a cell index is valid.
620 623 *
621 624 * @method is_valid_cell_index
622 625 * @param {Number} index A cell index
623 626 * @return True if the index is valid, false otherwise
624 627 */
625 628 Notebook.prototype.is_valid_cell_index = function (index) {
626 629 if (index !== null && index >= 0 && index < this.ncells()) {
627 630 return true;
628 631 } else {
629 632 return false;
630 633 };
631 634 }
632 635
633 636 /**
634 637 * Get the index of the currently selected cell.
635 638
636 639 * @method get_selected_index
637 640 * @return {Number} The selected cell's numeric index
638 641 */
639 642 Notebook.prototype.get_selected_index = function () {
640 643 var result = null;
641 644 this.get_cell_elements().filter(function (index) {
642 645 if ($(this).data("cell").selected === true) {
643 646 result = index;
644 647 };
645 648 });
646 649 return result;
647 650 };
648 651
649 652
650 653 // Cell selection.
651 654
652 655 /**
653 656 * Programmatically select a cell.
654 657 *
655 658 * @method select
656 659 * @param {Number} index A cell's index
657 660 * @return {Notebook} This notebook
658 661 */
659 662 Notebook.prototype.select = function (index) {
660 663 if (this.is_valid_cell_index(index)) {
661 664 var sindex = this.get_selected_index()
662 665 if (sindex !== null && index !== sindex) {
666 this.command_mode();
663 667 this.get_cell(sindex).unselect();
664 668 };
665 669 var cell = this.get_cell(index);
666 console.log('Notebook.select', index);
667 670 cell.select();
668 671 if (cell.cell_type === 'heading') {
669 672 $([IPython.events]).trigger('selected_cell_type_changed.Notebook',
670 673 {'cell_type':cell.cell_type,level:cell.level}
671 674 );
672 675 } else {
673 676 $([IPython.events]).trigger('selected_cell_type_changed.Notebook',
674 677 {'cell_type':cell.cell_type}
675 678 );
676 679 };
677 680 };
678 681 return this;
679 682 };
680 683
681 684 /**
682 685 * Programmatically select the next cell.
683 686 *
684 687 * @method select_next
685 688 * @return {Notebook} This notebook
686 689 */
687 690 Notebook.prototype.select_next = function () {
688 691 var index = this.get_selected_index();
689 692 this.select(index+1);
690 693 return this;
691 694 };
692 695
693 696 /**
694 697 * Programmatically select the previous cell.
695 698 *
696 699 * @method select_prev
697 700 * @return {Notebook} This notebook
698 701 */
699 702 Notebook.prototype.select_prev = function () {
700 703 var index = this.get_selected_index();
701 704 this.select(index-1);
702 705 return this;
703 706 };
704 707
705 708
706 709 // Edit/Command mode
707 710
708 /**
709 * Enter command mode for the currently selected cell
710 *
711 * @method command_mode
712 */
711 Notebook.prototype.get_edit_index = function () {
712 var result = null;
713 this.get_cell_elements().filter(function (index) {
714 if ($(this).data("cell").mode === 'edit') {
715 result = index;
716 };
717 });
718 return result;
719 };
720
713 721 Notebook.prototype.command_mode = function () {
714 console.log('Notebook.command_mode', this.mode, this.edit_index);
715 722 if (this.mode !== 'command') {
716 var cell = this.get_cell(this.edit_index);
723 var index = this.get_edit_index();
724 var cell = this.get_cell(index);
717 725 if (cell) {
718 726 cell.command_mode();
719 727 this.mode = 'command';
720 this.edit_index = null;
721 728 };
722 729 };
723 730 };
724 731
725 /**
726 * Enter edit mode for the currently selected cell
727 *
728 * @method editmode
729 */
730 732 Notebook.prototype.edit_mode = function () {
731 var index = this.get_selected_index();
732 console.log('Notebook.edit_mode', this.mode, index);
733 if (index !== this.edit_index) {
734 if (this.edit_index !== null) {
735 var old_cell = this.get_cell(this.edit_index)
736 old_cell.command_mode();
737 }
733 if (this.mode !== 'edit') {
734 // We are in command mode so get_edit_index() is null!!!
735 var index = this.get_selected_index();
736 if (index === null) {return;} // No cell is selected
738 737 var cell = this.get_cell(index);
739 738 if (cell) {
740 739 cell.edit_mode();
741 740 this.mode = 'edit';
742 this.edit_index = index;
743 741 };
744 742 };
745 743 };
746 744
747 745
748 746 // Cell movement
749 747
750 748 /**
751 749 * Move given (or selected) cell up and select it.
752 750 *
753 751 * @method move_cell_up
754 752 * @param [index] {integer} cell index
755 753 * @return {Notebook} This notebook
756 754 **/
757 755 Notebook.prototype.move_cell_up = function (index) {
758 756 var i = this.index_or_selected(index);
759 757 if (this.is_valid_cell_index(i) && i > 0) {
760 758 var pivot = this.get_cell_element(i-1);
761 759 var tomove = this.get_cell_element(i);
762 760 if (pivot !== null && tomove !== null) {
763 761 tomove.detach();
764 762 pivot.before(tomove);
765 763 this.select(i-1);
764
766 765 };
767 766 this.set_dirty(true);
768 767 };
769 768 return this;
770 769 };
771 770
772 771
773 772 /**
774 773 * Move given (or selected) cell down and select it
775 774 *
776 775 * @method move_cell_down
777 776 * @param [index] {integer} cell index
778 777 * @return {Notebook} This notebook
779 778 **/
780 779 Notebook.prototype.move_cell_down = function (index) {
781 780 var i = this.index_or_selected(index);
782 if ( this.is_valid_cell_index(i) && this.is_valid_cell_index(i+1)) {
781 if (this.is_valid_cell_index(i) && this.is_valid_cell_index(i+1)) {
783 782 var pivot = this.get_cell_element(i+1);
784 783 var tomove = this.get_cell_element(i);
785 784 if (pivot !== null && tomove !== null) {
786 785 tomove.detach();
787 786 pivot.after(tomove);
788 787 this.select(i+1);
789 788 };
790 789 };
791 790 this.set_dirty();
792 791 return this;
793 792 };
794 793
795 794
796 795 // Insertion, deletion.
797 796
798 797 /**
799 798 * Delete a cell from the notebook.
800 799 *
801 800 * @method delete_cell
802 801 * @param [index] A cell's numeric index
803 802 * @return {Notebook} This notebook
804 803 */
805 804 Notebook.prototype.delete_cell = function (index) {
806 805 var i = this.index_or_selected(index);
807 806 var cell = this.get_selected_cell();
808 807 this.undelete_backup = cell.toJSON();
809 808 $('#undelete_cell').removeClass('disabled');
810 809 if (this.is_valid_cell_index(i)) {
811 810 var ce = this.get_cell_element(i);
812 811 ce.remove();
813 812 if (i === (this.ncells())) {
814 813 this.select(i-1);
815 814 this.undelete_index = i - 1;
816 815 this.undelete_below = true;
817 816 } else {
818 817 this.select(i);
819 818 this.undelete_index = i;
820 819 this.undelete_below = false;
821 820 };
822 821 $([IPython.events]).trigger('delete.Cell', {'cell': cell, 'index': i});
823 822 this.set_dirty(true);
824 823 };
825 824 return this;
826 825 };
827 826
828 827 /**
829 828 * Insert a cell so that after insertion the cell is at given index.
830 829 *
831 830 * Similar to insert_above, but index parameter is mandatory
832 831 *
833 832 * Index will be brought back into the accissible range [0,n]
834 833 *
835 834 * @method insert_cell_at_index
836 835 * @param type {string} in ['code','markdown','heading']
837 836 * @param [index] {int} a valid index where to inser cell
838 837 *
839 838 * @return cell {cell|null} created cell or null
840 839 **/
841 840 Notebook.prototype.insert_cell_at_index = function(type, index){
842 841
843 842 var ncells = this.ncells();
844 843 var index = Math.min(index,ncells);
845 844 index = Math.max(index,0);
846 845 var cell = null;
847 846
848 847 if (ncells === 0 || this.is_valid_cell_index(index) || index === ncells) {
849 848 if (type === 'code') {
850 849 cell = new IPython.CodeCell(this.kernel);
851 850 cell.set_input_prompt();
852 851 } else if (type === 'markdown') {
853 852 cell = new IPython.MarkdownCell();
854 853 } else if (type === 'raw') {
855 854 cell = new IPython.RawCell();
856 855 } else if (type === 'heading') {
857 856 cell = new IPython.HeadingCell();
858 857 }
859 858
860 859 if(this._insert_element_at_index(cell.element,index)) {
861 860 cell.render();
862 861 $([IPython.events]).trigger('create.Cell', {'cell': cell, 'index': index});
863 862 cell.refresh();
864 863 // TODO: should we really get rid of this?
865 864 //this.select(this.find_cell_index(cell));
866 865 this.set_dirty(true);
867 866 }
868 867 }
869 868 return cell;
870 869
871 870 };
872 871
873 872 /**
874 873 * Insert an element at given cell index.
875 874 *
876 875 * @method _insert_element_at_index
877 876 * @param element {dom element} a cell element
878 877 * @param [index] {int} a valid index where to inser cell
879 878 * @private
880 879 *
881 880 * return true if everything whent fine.
882 881 **/
883 882 Notebook.prototype._insert_element_at_index = function(element, index){
884 883 if (element === undefined){
885 884 return false;
886 885 }
887 886
888 887 var ncells = this.ncells();
889 888
890 889 if (ncells === 0) {
891 890 // special case append if empty
892 891 this.element.find('div.end_space').before(element);
893 892 } else if ( ncells === index ) {
894 893 // special case append it the end, but not empty
895 894 this.get_cell_element(index-1).after(element);
896 895 } else if (this.is_valid_cell_index(index)) {
897 896 // otherwise always somewhere to append to
898 897 this.get_cell_element(index).before(element);
899 898 } else {
900 899 return false;
901 900 }
902 901
903 902 if (this.undelete_index !== null && index <= this.undelete_index) {
904 903 this.undelete_index = this.undelete_index + 1;
905 904 this.set_dirty(true);
906 905 }
907 906 return true;
908 907 };
909 908
910 909 /**
911 910 * Insert a cell of given type above given index, or at top
912 911 * of notebook if index smaller than 0.
913 912 *
914 913 * default index value is the one of currently selected cell
915 914 *
916 915 * @method insert_cell_above
917 916 * @param type {string} cell type
918 917 * @param [index] {integer}
919 918 *
920 919 * @return handle to created cell or null
921 920 **/
922 921 Notebook.prototype.insert_cell_above = function (type, index) {
923 922 index = this.index_or_selected(index);
924 923 return this.insert_cell_at_index(type, index);
925 924 };
926 925
927 926 /**
928 927 * Insert a cell of given type below given index, or at bottom
929 928 * of notebook if index greater thatn number of cell
930 929 *
931 930 * default index value is the one of currently selected cell
932 931 *
933 932 * @method insert_cell_below
934 933 * @param type {string} cell type
935 934 * @param [index] {integer}
936 935 *
937 936 * @return handle to created cell or null
938 937 *
939 938 **/
940 939 Notebook.prototype.insert_cell_below = function (type, index) {
941 940 index = this.index_or_selected(index);
942 941 return this.insert_cell_at_index(type, index+1);
943 942 };
944 943
945 944
946 945 /**
947 946 * Insert cell at end of notebook
948 947 *
949 948 * @method insert_cell_at_bottom
950 949 * @param {String} type cell type
951 950 *
952 951 * @return the added cell; or null
953 952 **/
954 953 Notebook.prototype.insert_cell_at_bottom = function (type){
955 954 var len = this.ncells();
956 955 return this.insert_cell_below(type,len-1);
957 956 };
958 957
959 958 /**
960 959 * Turn a cell into a code cell.
961 960 *
962 961 * @method to_code
963 962 * @param {Number} [index] A cell's index
964 963 */
965 964 Notebook.prototype.to_code = function (index) {
966 965 var i = this.index_or_selected(index);
967 966 if (this.is_valid_cell_index(i)) {
968 967 var source_element = this.get_cell_element(i);
969 968 var source_cell = source_element.data("cell");
970 969 if (!(source_cell instanceof IPython.CodeCell)) {
971 970 var target_cell = this.insert_cell_below('code',i);
972 971 var text = source_cell.get_text();
973 972 if (text === source_cell.placeholder) {
974 973 text = '';
975 974 }
976 975 target_cell.set_text(text);
977 976 // make this value the starting point, so that we can only undo
978 977 // to this state, instead of a blank cell
979 978 target_cell.code_mirror.clearHistory();
980 979 source_element.remove();
981 980 this.set_dirty(true);
982 981 };
983 982 };
984 983 };
985 984
986 985 /**
987 986 * Turn a cell into a Markdown cell.
988 987 *
989 988 * @method to_markdown
990 989 * @param {Number} [index] A cell's index
991 990 */
992 991 Notebook.prototype.to_markdown = function (index) {
993 992 var i = this.index_or_selected(index);
994 993 if (this.is_valid_cell_index(i)) {
995 994 var source_element = this.get_cell_element(i);
996 995 var source_cell = source_element.data("cell");
997 996 if (!(source_cell instanceof IPython.MarkdownCell)) {
998 997 var target_cell = this.insert_cell_below('markdown',i);
999 998 var text = source_cell.get_text();
1000 999 if (text === source_cell.placeholder) {
1001 1000 text = '';
1002 1001 };
1003 // The edit must come before the set_text.
1002 // We must show the editor before setting its contents
1004 1003 target_cell.unrender();
1005 1004 target_cell.set_text(text);
1006 1005 // make this value the starting point, so that we can only undo
1007 1006 // to this state, instead of a blank cell
1008 1007 target_cell.code_mirror.clearHistory();
1009 1008 source_element.remove();
1010 1009 this.select(i);
1011 1010 this.edit_mode();
1012 1011 this.set_dirty(true);
1013 1012 };
1014 1013 };
1015 1014 };
1016 1015
1017 1016 /**
1018 1017 * Turn a cell into a raw text cell.
1019 1018 *
1020 1019 * @method to_raw
1021 1020 * @param {Number} [index] A cell's index
1022 1021 */
1023 1022 Notebook.prototype.to_raw = function (index) {
1024 1023 var i = this.index_or_selected(index);
1025 1024 if (this.is_valid_cell_index(i)) {
1026 1025 var source_element = this.get_cell_element(i);
1027 1026 var source_cell = source_element.data("cell");
1028 1027 var target_cell = null;
1029 1028 if (!(source_cell instanceof IPython.RawCell)) {
1030 1029 target_cell = this.insert_cell_below('raw',i);
1031 1030 var text = source_cell.get_text();
1032 1031 if (text === source_cell.placeholder) {
1033 1032 text = '';
1034 1033 };
1035 // The edit must come before the set_text.
1034 // We must show the editor before setting its contents
1036 1035 target_cell.unrender();
1037 1036 target_cell.set_text(text);
1038 1037 // make this value the starting point, so that we can only undo
1039 1038 // to this state, instead of a blank cell
1040 1039 target_cell.code_mirror.clearHistory();
1041 1040 source_element.remove();
1041 this.select(i);
1042 this.edit_mode();
1042 1043 this.set_dirty(true);
1043 1044 };
1044 1045 };
1045 1046 };
1046 1047
1047 1048 /**
1048 1049 * Turn a cell into a heading cell.
1049 1050 *
1050 1051 * @method to_heading
1051 1052 * @param {Number} [index] A cell's index
1052 1053 * @param {Number} [level] A heading level (e.g., 1 becomes &lt;h1&gt;)
1053 1054 */
1054 1055 Notebook.prototype.to_heading = function (index, level) {
1055 1056 level = level || 1;
1056 1057 var i = this.index_or_selected(index);
1057 1058 if (this.is_valid_cell_index(i)) {
1058 1059 var source_element = this.get_cell_element(i);
1059 1060 var source_cell = source_element.data("cell");
1060 1061 var target_cell = null;
1061 1062 if (source_cell instanceof IPython.HeadingCell) {
1062 1063 source_cell.set_level(level);
1063 1064 } else {
1064 1065 target_cell = this.insert_cell_below('heading',i);
1065 1066 var text = source_cell.get_text();
1066 1067 if (text === source_cell.placeholder) {
1067 1068 text = '';
1068 1069 };
1069 // The edit must come before the set_text.
1070 // We must show the editor before setting its contents
1070 1071 target_cell.set_level(level);
1071 1072 target_cell.unrender();
1072 1073 target_cell.set_text(text);
1073 1074 // make this value the starting point, so that we can only undo
1074 1075 // to this state, instead of a blank cell
1075 1076 target_cell.code_mirror.clearHistory();
1076 1077 source_element.remove();
1078 this.select(i);
1079 this.edit_mode();
1077 1080 this.set_dirty(true);
1078 1081 };
1079 1082 $([IPython.events]).trigger('selected_cell_type_changed.Notebook',
1080 1083 {'cell_type':'heading',level:level}
1081 1084 );
1082 1085 };
1083 1086 };
1084 1087
1085 1088
1086 1089 // Cut/Copy/Paste
1087 1090
1088 1091 /**
1089 1092 * Enable UI elements for pasting cells.
1090 1093 *
1091 1094 * @method enable_paste
1092 1095 */
1093 1096 Notebook.prototype.enable_paste = function () {
1094 1097 var that = this;
1095 1098 if (!this.paste_enabled) {
1096 1099 $('#paste_cell_replace').removeClass('disabled')
1097 1100 .on('click', function () {that.paste_cell_replace();});
1098 1101 $('#paste_cell_above').removeClass('disabled')
1099 1102 .on('click', function () {that.paste_cell_above();});
1100 1103 $('#paste_cell_below').removeClass('disabled')
1101 1104 .on('click', function () {that.paste_cell_below();});
1102 1105 this.paste_enabled = true;
1103 1106 };
1104 1107 };
1105 1108
1106 1109 /**
1107 1110 * Disable UI elements for pasting cells.
1108 1111 *
1109 1112 * @method disable_paste
1110 1113 */
1111 1114 Notebook.prototype.disable_paste = function () {
1112 1115 if (this.paste_enabled) {
1113 1116 $('#paste_cell_replace').addClass('disabled').off('click');
1114 1117 $('#paste_cell_above').addClass('disabled').off('click');
1115 1118 $('#paste_cell_below').addClass('disabled').off('click');
1116 1119 this.paste_enabled = false;
1117 1120 };
1118 1121 };
1119 1122
1120 1123 /**
1121 1124 * Cut a cell.
1122 1125 *
1123 1126 * @method cut_cell
1124 1127 */
1125 1128 Notebook.prototype.cut_cell = function () {
1126 1129 this.copy_cell();
1127 1130 this.delete_cell();
1128 1131 }
1129 1132
1130 1133 /**
1131 1134 * Copy a cell.
1132 1135 *
1133 1136 * @method copy_cell
1134 1137 */
1135 1138 Notebook.prototype.copy_cell = function () {
1136 1139 var cell = this.get_selected_cell();
1137 1140 this.clipboard = cell.toJSON();
1138 1141 this.enable_paste();
1139 1142 };
1140 1143
1141 1144 /**
1142 1145 * Replace the selected cell with a cell in the clipboard.
1143 1146 *
1144 1147 * @method paste_cell_replace
1145 1148 */
1146 1149 Notebook.prototype.paste_cell_replace = function () {
1147 1150 if (this.clipboard !== null && this.paste_enabled) {
1148 1151 var cell_data = this.clipboard;
1149 1152 var new_cell = this.insert_cell_above(cell_data.cell_type);
1150 1153 new_cell.fromJSON(cell_data);
1151 1154 var old_cell = this.get_next_cell(new_cell);
1152 1155 this.delete_cell(this.find_cell_index(old_cell));
1153 1156 this.select(this.find_cell_index(new_cell));
1154 1157 };
1155 1158 };
1156 1159
1157 1160 /**
1158 1161 * Paste a cell from the clipboard above the selected cell.
1159 1162 *
1160 1163 * @method paste_cell_above
1161 1164 */
1162 1165 Notebook.prototype.paste_cell_above = function () {
1163 1166 if (this.clipboard !== null && this.paste_enabled) {
1164 1167 var cell_data = this.clipboard;
1165 1168 var new_cell = this.insert_cell_above(cell_data.cell_type);
1166 1169 new_cell.fromJSON(cell_data);
1167 1170 };
1168 1171 };
1169 1172
1170 1173 /**
1171 1174 * Paste a cell from the clipboard below the selected cell.
1172 1175 *
1173 1176 * @method paste_cell_below
1174 1177 */
1175 1178 Notebook.prototype.paste_cell_below = function () {
1176 1179 if (this.clipboard !== null && this.paste_enabled) {
1177 1180 var cell_data = this.clipboard;
1178 1181 var new_cell = this.insert_cell_below(cell_data.cell_type);
1179 1182 new_cell.fromJSON(cell_data);
1180 1183 };
1181 1184 };
1182 1185
1183 1186 // Cell undelete
1184 1187
1185 1188 /**
1186 1189 * Restore the most recently deleted cell.
1187 1190 *
1188 1191 * @method undelete
1189 1192 */
1190 1193 Notebook.prototype.undelete = function() {
1191 1194 if (this.undelete_backup !== null && this.undelete_index !== null) {
1192 1195 var current_index = this.get_selected_index();
1193 1196 if (this.undelete_index < current_index) {
1194 1197 current_index = current_index + 1;
1195 1198 }
1196 1199 if (this.undelete_index >= this.ncells()) {
1197 1200 this.select(this.ncells() - 1);
1198 1201 }
1199 1202 else {
1200 1203 this.select(this.undelete_index);
1201 1204 }
1202 1205 var cell_data = this.undelete_backup;
1203 1206 var new_cell = null;
1204 1207 if (this.undelete_below) {
1205 1208 new_cell = this.insert_cell_below(cell_data.cell_type);
1206 1209 } else {
1207 1210 new_cell = this.insert_cell_above(cell_data.cell_type);
1208 1211 }
1209 1212 new_cell.fromJSON(cell_data);
1210 1213 this.select(current_index);
1211 1214 this.undelete_backup = null;
1212 1215 this.undelete_index = null;
1213 1216 }
1214 1217 $('#undelete_cell').addClass('disabled');
1215 1218 }
1216 1219
1217 1220 // Split/merge
1218 1221
1219 1222 /**
1220 1223 * Split the selected cell into two, at the cursor.
1221 1224 *
1222 1225 * @method split_cell
1223 1226 */
1224 1227 Notebook.prototype.split_cell = function () {
1225 1228 // Todo: implement spliting for other cell types.
1226 1229 var cell = this.get_selected_cell();
1227 1230 if (cell.is_splittable()) {
1228 1231 var texta = cell.get_pre_cursor();
1229 1232 var textb = cell.get_post_cursor();
1230 1233 if (cell instanceof IPython.CodeCell) {
1231 1234 cell.set_text(textb);
1232 1235 var new_cell = this.insert_cell_above('code');
1233 1236 new_cell.set_text(texta);
1234 1237 this.select_next();
1235 1238 } else if (cell instanceof IPython.MarkdownCell) {
1236 1239 cell.set_text(textb);
1237 1240 cell.render();
1238 1241 var new_cell = this.insert_cell_above('markdown');
1239 1242 new_cell.unrender(); // editor must be visible to call set_text
1240 1243 new_cell.set_text(texta);
1241 1244 new_cell.render();
1242 1245 this.select_next();
1243 1246 }
1244 1247 };
1245 1248 };
1246 1249
1247 1250 /**
1248 1251 * Combine the selected cell into the cell above it.
1249 1252 *
1250 1253 * @method merge_cell_above
1251 1254 */
1252 1255 Notebook.prototype.merge_cell_above = function () {
1253 1256 var index = this.get_selected_index();
1254 1257 var cell = this.get_cell(index);
1255 1258 if (!cell.is_mergeable()) {
1256 1259 return;
1257 1260 }
1258 1261 if (index > 0) {
1259 1262 var upper_cell = this.get_cell(index-1);
1260 1263 if (!upper_cell.is_mergeable()) {
1261 1264 return;
1262 1265 }
1263 1266 var upper_text = upper_cell.get_text();
1264 1267 var text = cell.get_text();
1265 1268 if (cell instanceof IPython.CodeCell) {
1266 1269 cell.set_text(upper_text+'\n'+text);
1267 1270 } else if (cell instanceof IPython.MarkdownCell) {
1268 1271 cell.unrender();
1269 1272 cell.set_text(upper_text+'\n'+text);
1270 1273 cell.render();
1271 1274 };
1272 1275 this.delete_cell(index-1);
1273 1276 this.select(this.find_cell_index(cell));
1274 1277 };
1275 1278 };
1276 1279
1277 1280 /**
1278 1281 * Combine the selected cell into the cell below it.
1279 1282 *
1280 1283 * @method merge_cell_below
1281 1284 */
1282 1285 Notebook.prototype.merge_cell_below = function () {
1283 1286 var index = this.get_selected_index();
1284 1287 var cell = this.get_cell(index);
1285 1288 if (!cell.is_mergeable()) {
1286 1289 return;
1287 1290 }
1288 1291 if (index < this.ncells()-1) {
1289 1292 var lower_cell = this.get_cell(index+1);
1290 1293 if (!lower_cell.is_mergeable()) {
1291 1294 return;
1292 1295 }
1293 1296 var lower_text = lower_cell.get_text();
1294 1297 var text = cell.get_text();
1295 1298 if (cell instanceof IPython.CodeCell) {
1296 1299 cell.set_text(text+'\n'+lower_text);
1297 1300 } else if (cell instanceof IPython.MarkdownCell) {
1298 1301 cell.unrender();
1299 1302 cell.set_text(text+'\n'+lower_text);
1300 1303 cell.render();
1301 1304 };
1302 1305 this.delete_cell(index+1);
1303 1306 this.select(this.find_cell_index(cell));
1304 1307 };
1305 1308 };
1306 1309
1307 1310
1308 1311 // Cell collapsing and output clearing
1309 1312
1310 1313 /**
1311 1314 * Hide a cell's output.
1312 1315 *
1313 1316 * @method collapse
1314 1317 * @param {Number} index A cell's numeric index
1315 1318 */
1316 1319 Notebook.prototype.collapse = function (index) {
1317 1320 var i = this.index_or_selected(index);
1318 1321 this.get_cell(i).collapse();
1319 1322 this.set_dirty(true);
1320 1323 };
1321 1324
1322 1325 /**
1323 1326 * Show a cell's output.
1324 1327 *
1325 1328 * @method expand
1326 1329 * @param {Number} index A cell's numeric index
1327 1330 */
1328 1331 Notebook.prototype.expand = function (index) {
1329 1332 var i = this.index_or_selected(index);
1330 1333 this.get_cell(i).expand();
1331 1334 this.set_dirty(true);
1332 1335 };
1333 1336
1334 1337 /** Toggle whether a cell's output is collapsed or expanded.
1335 1338 *
1336 1339 * @method toggle_output
1337 1340 * @param {Number} index A cell's numeric index
1338 1341 */
1339 1342 Notebook.prototype.toggle_output = function (index) {
1340 1343 var i = this.index_or_selected(index);
1341 1344 this.get_cell(i).toggle_output();
1342 1345 this.set_dirty(true);
1343 1346 };
1344 1347
1345 1348 /**
1346 1349 * Toggle a scrollbar for long cell outputs.
1347 1350 *
1348 1351 * @method toggle_output_scroll
1349 1352 * @param {Number} index A cell's numeric index
1350 1353 */
1351 1354 Notebook.prototype.toggle_output_scroll = function (index) {
1352 1355 var i = this.index_or_selected(index);
1353 1356 this.get_cell(i).toggle_output_scroll();
1354 1357 };
1355 1358
1356 1359 /**
1357 1360 * Hide each code cell's output area.
1358 1361 *
1359 1362 * @method collapse_all_output
1360 1363 */
1361 1364 Notebook.prototype.collapse_all_output = function () {
1362 1365 var ncells = this.ncells();
1363 1366 var cells = this.get_cells();
1364 1367 for (var i=0; i<ncells; i++) {
1365 1368 if (cells[i] instanceof IPython.CodeCell) {
1366 1369 cells[i].output_area.collapse();
1367 1370 }
1368 1371 };
1369 1372 // this should not be set if the `collapse` key is removed from nbformat
1370 1373 this.set_dirty(true);
1371 1374 };
1372 1375
1373 1376 /**
1374 1377 * Expand each code cell's output area, and add a scrollbar for long output.
1375 1378 *
1376 1379 * @method scroll_all_output
1377 1380 */
1378 1381 Notebook.prototype.scroll_all_output = function () {
1379 1382 var ncells = this.ncells();
1380 1383 var cells = this.get_cells();
1381 1384 for (var i=0; i<ncells; i++) {
1382 1385 if (cells[i] instanceof IPython.CodeCell) {
1383 1386 cells[i].output_area.expand();
1384 1387 cells[i].output_area.scroll_if_long();
1385 1388 }
1386 1389 };
1387 1390 // this should not be set if the `collapse` key is removed from nbformat
1388 1391 this.set_dirty(true);
1389 1392 };
1390 1393
1391 1394 /**
1392 1395 * Expand each code cell's output area, and remove scrollbars.
1393 1396 *
1394 1397 * @method expand_all_output
1395 1398 */
1396 1399 Notebook.prototype.expand_all_output = function () {
1397 1400 var ncells = this.ncells();
1398 1401 var cells = this.get_cells();
1399 1402 for (var i=0; i<ncells; i++) {
1400 1403 if (cells[i] instanceof IPython.CodeCell) {
1401 1404 cells[i].output_area.expand();
1402 1405 cells[i].output_area.unscroll_area();
1403 1406 }
1404 1407 };
1405 1408 // this should not be set if the `collapse` key is removed from nbformat
1406 1409 this.set_dirty(true);
1407 1410 };
1408 1411
1409 1412 /**
1410 1413 * Clear each code cell's output area.
1411 1414 *
1412 1415 * @method clear_all_output
1413 1416 */
1414 1417 Notebook.prototype.clear_all_output = function () {
1415 1418 var ncells = this.ncells();
1416 1419 var cells = this.get_cells();
1417 1420 for (var i=0; i<ncells; i++) {
1418 1421 if (cells[i] instanceof IPython.CodeCell) {
1419 1422 cells[i].clear_output();
1420 1423 // Make all In[] prompts blank, as well
1421 1424 // TODO: make this configurable (via checkbox?)
1422 1425 cells[i].set_input_prompt();
1423 1426 }
1424 1427 };
1425 1428 this.set_dirty(true);
1426 1429 };
1427 1430
1428 1431
1429 1432 // Other cell functions: line numbers, ...
1430 1433
1431 1434 /**
1432 1435 * Toggle line numbers in the selected cell's input area.
1433 1436 *
1434 1437 * @method cell_toggle_line_numbers
1435 1438 */
1436 1439 Notebook.prototype.cell_toggle_line_numbers = function() {
1437 1440 this.get_selected_cell().toggle_line_numbers();
1438 1441 };
1439 1442
1440 1443 // Session related things
1441 1444
1442 1445 /**
1443 1446 * Start a new session and set it on each code cell.
1444 1447 *
1445 1448 * @method start_session
1446 1449 */
1447 1450 Notebook.prototype.start_session = function () {
1448 1451 this.session = new IPython.Session(this.notebook_name, this.notebook_path, this);
1449 1452 this.session.start($.proxy(this._session_started, this));
1450 1453 };
1451 1454
1452 1455
1453 1456 /**
1454 1457 * Once a session is started, link the code cells to the kernel
1455 1458 *
1456 1459 */
1457 1460 Notebook.prototype._session_started = function(){
1458 1461 this.kernel = this.session.kernel;
1459 1462 var ncells = this.ncells();
1460 1463 for (var i=0; i<ncells; i++) {
1461 1464 var cell = this.get_cell(i);
1462 1465 if (cell instanceof IPython.CodeCell) {
1463 1466 cell.set_kernel(this.session.kernel);
1464 1467 };
1465 1468 };
1466 1469 };
1467 1470
1468 1471 /**
1469 1472 * Prompt the user to restart the IPython kernel.
1470 1473 *
1471 1474 * @method restart_kernel
1472 1475 */
1473 1476 Notebook.prototype.restart_kernel = function () {
1474 1477 var that = this;
1475 1478 IPython.dialog.modal({
1476 1479 title : "Restart kernel or continue running?",
1477 1480 body : $("<p/>").html(
1478 1481 'Do you want to restart the current kernel? You will lose all variables defined in it.'
1479 1482 ),
1480 1483 buttons : {
1481 1484 "Continue running" : {},
1482 1485 "Restart" : {
1483 1486 "class" : "btn-danger",
1484 1487 "click" : function() {
1485 1488 that.session.restart_kernel();
1486 1489 }
1487 1490 }
1488 1491 }
1489 1492 });
1490 1493 };
1491 1494
1492 1495 /**
1493 1496 * Run the selected cell.
1494 1497 *
1495 1498 * Execute or render cell outputs.
1496 1499 *
1497 1500 * @method execute_selected_cell
1498 1501 * @param {Object} options Customize post-execution behavior
1499 1502 */
1500 1503 Notebook.prototype.execute_selected_cell = function (mode) {
1501 1504 // mode = shift, ctrl, alt
1502 1505 mode = mode || 'shift'
1503 var that = this;
1504 var cell = that.get_selected_cell();
1505 var cell_index = that.find_cell_index(cell);
1506 var cell = this.get_selected_cell();
1507 var cell_index = this.find_cell_index(cell);
1506 1508
1507 1509 cell.execute();
1508 console.log('Notebook.execute_selected_cell', mode);
1510
1511 // If we are at the end always insert a new cell and return
1512 if (cell_index === (this.ncells()-1)) {
1513 this.insert_cell_below('code');
1514 this.select(cell_index+1);
1515 this.edit_mode();
1516 this.scroll_to_bottom();
1517 this.set_dirty(true);
1518 return;
1519 }
1520
1509 1521 if (mode === 'shift') {
1510 if (cell_index === (that.ncells()-1)) {
1511 that.insert_cell_below('code');
1512 that.select(cell_index+1);
1513 that.edit_mode();
1514 that.scroll_to_bottom();
1515 } else {
1516 that.command_mode();
1517 }
1522 this.command_mode();
1518 1523 } else if (mode === 'ctrl') {
1519 that.select(cell_index+1);
1520 that.get_cell(cell_index+1).focus_cell();
1524 this.select(cell_index+1);
1525 this.get_cell(cell_index+1).focus_cell();
1521 1526 } else if (mode === 'alt') {
1522 1527 // Only insert a new cell, if we ended up in an already populated cell
1523 if (/\S/.test(that.get_next_cell().get_text()) == true) {
1524 that.insert_cell_below('code');
1528 var next_text = this.get_cell(cell_index+1).get_text();
1529 if (/\S/.test(next_text) === true) {
1530 this.insert_cell_below('code');
1525 1531 }
1526 var next_index = cell_index+1;
1527 that.select(cell_index+1);
1528 that.edit_mode();
1532 this.select(cell_index+1);
1533 this.edit_mode();
1529 1534 }
1530
1531 1535 this.set_dirty(true);
1532 1536 };
1533 1537
1534 1538
1535 1539 /**
1536 1540 * Execute all cells below the selected cell.
1537 1541 *
1538 1542 * @method execute_cells_below
1539 1543 */
1540 1544 Notebook.prototype.execute_cells_below = function () {
1541 1545 this.execute_cell_range(this.get_selected_index(), this.ncells());
1542 1546 this.scroll_to_bottom();
1543 1547 };
1544 1548
1545 1549 /**
1546 1550 * Execute all cells above the selected cell.
1547 1551 *
1548 1552 * @method execute_cells_above
1549 1553 */
1550 1554 Notebook.prototype.execute_cells_above = function () {
1551 1555 this.execute_cell_range(0, this.get_selected_index());
1552 1556 };
1553 1557
1554 1558 /**
1555 1559 * Execute all cells.
1556 1560 *
1557 1561 * @method execute_all_cells
1558 1562 */
1559 1563 Notebook.prototype.execute_all_cells = function () {
1560 1564 this.execute_cell_range(0, this.ncells());
1561 1565 this.scroll_to_bottom();
1562 1566 };
1563 1567
1564 1568 /**
1565 1569 * Execute a contiguous range of cells.
1566 1570 *
1567 1571 * @method execute_cell_range
1568 1572 * @param {Number} start Index of the first cell to execute (inclusive)
1569 1573 * @param {Number} end Index of the last cell to execute (exclusive)
1570 1574 */
1571 1575 Notebook.prototype.execute_cell_range = function (start, end) {
1572 1576 for (var i=start; i<end; i++) {
1573 1577 this.select(i);
1574 1578 this.execute_selected_cell({add_new:false});
1575 1579 };
1576 1580 };
1577 1581
1578 1582 // Persistance and loading
1579 1583
1580 1584 /**
1581 1585 * Getter method for this notebook's name.
1582 1586 *
1583 1587 * @method get_notebook_name
1584 1588 * @return {String} This notebook's name
1585 1589 */
1586 1590 Notebook.prototype.get_notebook_name = function () {
1587 1591 var nbname = this.notebook_name.substring(0,this.notebook_name.length-6);
1588 1592 return nbname;
1589 1593 };
1590 1594
1591 1595 /**
1592 1596 * Setter method for this notebook's name.
1593 1597 *
1594 1598 * @method set_notebook_name
1595 1599 * @param {String} name A new name for this notebook
1596 1600 */
1597 1601 Notebook.prototype.set_notebook_name = function (name) {
1598 1602 this.notebook_name = name;
1599 1603 };
1600 1604
1601 1605 /**
1602 1606 * Check that a notebook's name is valid.
1603 1607 *
1604 1608 * @method test_notebook_name
1605 1609 * @param {String} nbname A name for this notebook
1606 1610 * @return {Boolean} True if the name is valid, false if invalid
1607 1611 */
1608 1612 Notebook.prototype.test_notebook_name = function (nbname) {
1609 1613 nbname = nbname || '';
1610 1614 if (this.notebook_name_blacklist_re.test(nbname) == false && nbname.length>0) {
1611 1615 return true;
1612 1616 } else {
1613 1617 return false;
1614 1618 };
1615 1619 };
1616 1620
1617 1621 /**
1618 1622 * Load a notebook from JSON (.ipynb).
1619 1623 *
1620 1624 * This currently handles one worksheet: others are deleted.
1621 1625 *
1622 1626 * @method fromJSON
1623 1627 * @param {Object} data JSON representation of a notebook
1624 1628 */
1625 1629 Notebook.prototype.fromJSON = function (data) {
1626 1630 var content = data.content;
1627 1631 var ncells = this.ncells();
1628 1632 var i;
1629 1633 for (i=0; i<ncells; i++) {
1630 1634 // Always delete cell 0 as they get renumbered as they are deleted.
1631 1635 this.delete_cell(0);
1632 1636 };
1633 1637 // Save the metadata and name.
1634 1638 this.metadata = content.metadata;
1635 1639 this.notebook_name = data.name;
1636 1640 // Only handle 1 worksheet for now.
1637 1641 var worksheet = content.worksheets[0];
1638 1642 if (worksheet !== undefined) {
1639 1643 if (worksheet.metadata) {
1640 1644 this.worksheet_metadata = worksheet.metadata;
1641 1645 }
1642 1646 var new_cells = worksheet.cells;
1643 1647 ncells = new_cells.length;
1644 1648 var cell_data = null;
1645 1649 var new_cell = null;
1646 1650 for (i=0; i<ncells; i++) {
1647 1651 cell_data = new_cells[i];
1648 1652 // VERSIONHACK: plaintext -> raw
1649 1653 // handle never-released plaintext name for raw cells
1650 1654 if (cell_data.cell_type === 'plaintext'){
1651 1655 cell_data.cell_type = 'raw';
1652 1656 }
1653 1657
1654 new_cell = this.insert_cell_at_bottom(cell_data.cell_type);
1658 new_cell = this.insert_cell_at_index(cell_data.cell_type, i);
1655 1659 new_cell.fromJSON(cell_data);
1656 1660 };
1657 1661 };
1658 1662 if (content.worksheets.length > 1) {
1659 1663 IPython.dialog.modal({
1660 1664 title : "Multiple worksheets",
1661 1665 body : "This notebook has " + data.worksheets.length + " worksheets, " +
1662 1666 "but this version of IPython can only handle the first. " +
1663 1667 "If you save this notebook, worksheets after the first will be lost.",
1664 1668 buttons : {
1665 1669 OK : {
1666 1670 class : "btn-danger"
1667 1671 }
1668 1672 }
1669 1673 });
1670 1674 }
1671 1675 };
1672 1676
1673 1677 /**
1674 1678 * Dump this notebook into a JSON-friendly object.
1675 1679 *
1676 1680 * @method toJSON
1677 1681 * @return {Object} A JSON-friendly representation of this notebook.
1678 1682 */
1679 1683 Notebook.prototype.toJSON = function () {
1680 1684 var cells = this.get_cells();
1681 1685 var ncells = cells.length;
1682 1686 var cell_array = new Array(ncells);
1683 1687 for (var i=0; i<ncells; i++) {
1684 1688 cell_array[i] = cells[i].toJSON();
1685 1689 };
1686 1690 var data = {
1687 1691 // Only handle 1 worksheet for now.
1688 1692 worksheets : [{
1689 1693 cells: cell_array,
1690 1694 metadata: this.worksheet_metadata
1691 1695 }],
1692 1696 metadata : this.metadata
1693 1697 };
1694 1698 return data;
1695 1699 };
1696 1700
1697 1701 /**
1698 1702 * Start an autosave timer, for periodically saving the notebook.
1699 1703 *
1700 1704 * @method set_autosave_interval
1701 1705 * @param {Integer} interval the autosave interval in milliseconds
1702 1706 */
1703 1707 Notebook.prototype.set_autosave_interval = function (interval) {
1704 1708 var that = this;
1705 1709 // clear previous interval, so we don't get simultaneous timers
1706 1710 if (this.autosave_timer) {
1707 1711 clearInterval(this.autosave_timer);
1708 1712 }
1709 1713
1710 1714 this.autosave_interval = this.minimum_autosave_interval = interval;
1711 1715 if (interval) {
1712 1716 this.autosave_timer = setInterval(function() {
1713 1717 if (that.dirty) {
1714 1718 that.save_notebook();
1715 1719 }
1716 1720 }, interval);
1717 1721 $([IPython.events]).trigger("autosave_enabled.Notebook", interval);
1718 1722 } else {
1719 1723 this.autosave_timer = null;
1720 1724 $([IPython.events]).trigger("autosave_disabled.Notebook");
1721 1725 };
1722 1726 };
1723 1727
1724 1728 /**
1725 1729 * Save this notebook on the server.
1726 1730 *
1727 1731 * @method save_notebook
1728 1732 */
1729 1733 Notebook.prototype.save_notebook = function (extra_settings) {
1730 1734 // Create a JSON model to be sent to the server.
1731 1735 var model = {};
1732 1736 model.name = this.notebook_name;
1733 1737 model.path = this.notebook_path;
1734 1738 model.content = this.toJSON();
1735 1739 model.content.nbformat = this.nbformat;
1736 1740 model.content.nbformat_minor = this.nbformat_minor;
1737 1741 // time the ajax call for autosave tuning purposes.
1738 1742 var start = new Date().getTime();
1739 1743 // We do the call with settings so we can set cache to false.
1740 1744 var settings = {
1741 1745 processData : false,
1742 1746 cache : false,
1743 1747 type : "PUT",
1744 1748 data : JSON.stringify(model),
1745 1749 headers : {'Content-Type': 'application/json'},
1746 1750 success : $.proxy(this.save_notebook_success, this, start),
1747 1751 error : $.proxy(this.save_notebook_error, this)
1748 1752 };
1749 1753 if (extra_settings) {
1750 1754 for (var key in extra_settings) {
1751 1755 settings[key] = extra_settings[key];
1752 1756 }
1753 1757 }
1754 1758 $([IPython.events]).trigger('notebook_saving.Notebook');
1755 1759 var url = utils.url_join_encode(
1756 1760 this._baseProjectUrl,
1757 1761 'api/notebooks',
1758 1762 this.notebook_path,
1759 1763 this.notebook_name
1760 1764 );
1761 1765 $.ajax(url, settings);
1762 1766 };
1763 1767
1764 1768 /**
1765 1769 * Success callback for saving a notebook.
1766 1770 *
1767 1771 * @method save_notebook_success
1768 1772 * @param {Integer} start the time when the save request started
1769 1773 * @param {Object} data JSON representation of a notebook
1770 1774 * @param {String} status Description of response status
1771 1775 * @param {jqXHR} xhr jQuery Ajax object
1772 1776 */
1773 1777 Notebook.prototype.save_notebook_success = function (start, data, status, xhr) {
1774 1778 this.set_dirty(false);
1775 1779 $([IPython.events]).trigger('notebook_saved.Notebook');
1776 1780 this._update_autosave_interval(start);
1777 1781 if (this._checkpoint_after_save) {
1778 1782 this.create_checkpoint();
1779 1783 this._checkpoint_after_save = false;
1780 1784 };
1781 1785 };
1782 1786
1783 1787 /**
1784 1788 * update the autosave interval based on how long the last save took
1785 1789 *
1786 1790 * @method _update_autosave_interval
1787 1791 * @param {Integer} timestamp when the save request started
1788 1792 */
1789 1793 Notebook.prototype._update_autosave_interval = function (start) {
1790 1794 var duration = (new Date().getTime() - start);
1791 1795 if (this.autosave_interval) {
1792 1796 // new save interval: higher of 10x save duration or parameter (default 30 seconds)
1793 1797 var interval = Math.max(10 * duration, this.minimum_autosave_interval);
1794 1798 // round to 10 seconds, otherwise we will be setting a new interval too often
1795 1799 interval = 10000 * Math.round(interval / 10000);
1796 1800 // set new interval, if it's changed
1797 1801 if (interval != this.autosave_interval) {
1798 1802 this.set_autosave_interval(interval);
1799 1803 }
1800 1804 }
1801 1805 };
1802 1806
1803 1807 /**
1804 1808 * Failure callback for saving a notebook.
1805 1809 *
1806 1810 * @method save_notebook_error
1807 1811 * @param {jqXHR} xhr jQuery Ajax object
1808 1812 * @param {String} status Description of response status
1809 1813 * @param {String} error HTTP error message
1810 1814 */
1811 1815 Notebook.prototype.save_notebook_error = function (xhr, status, error) {
1812 1816 $([IPython.events]).trigger('notebook_save_failed.Notebook', [xhr, status, error]);
1813 1817 };
1814 1818
1815 1819 Notebook.prototype.new_notebook = function(){
1816 1820 var path = this.notebook_path;
1817 1821 var base_project_url = this._baseProjectUrl;
1818 1822 var settings = {
1819 1823 processData : false,
1820 1824 cache : false,
1821 1825 type : "POST",
1822 1826 dataType : "json",
1823 1827 async : false,
1824 1828 success : function (data, status, xhr){
1825 1829 var notebook_name = data.name;
1826 1830 window.open(
1827 1831 utils.url_join_encode(
1828 1832 base_project_url,
1829 1833 'notebooks',
1830 1834 path,
1831 1835 notebook_name
1832 1836 ),
1833 1837 '_blank'
1834 1838 );
1835 1839 }
1836 1840 };
1837 1841 var url = utils.url_join_encode(
1838 1842 base_project_url,
1839 1843 'api/notebooks',
1840 1844 path
1841 1845 );
1842 1846 $.ajax(url,settings);
1843 1847 };
1844 1848
1845 1849
1846 1850 Notebook.prototype.copy_notebook = function(){
1847 1851 var path = this.notebook_path;
1848 1852 var base_project_url = this._baseProjectUrl;
1849 1853 var settings = {
1850 1854 processData : false,
1851 1855 cache : false,
1852 1856 type : "POST",
1853 1857 dataType : "json",
1854 1858 data : JSON.stringify({copy_from : this.notebook_name}),
1855 1859 async : false,
1856 1860 success : function (data, status, xhr) {
1857 1861 window.open(utils.url_join_encode(
1858 1862 base_project_url,
1859 1863 'notebooks',
1860 1864 data.path,
1861 1865 data.name
1862 1866 ), '_blank');
1863 1867 }
1864 1868 };
1865 1869 var url = utils.url_join_encode(
1866 1870 base_project_url,
1867 1871 'api/notebooks',
1868 1872 path
1869 1873 );
1870 1874 $.ajax(url,settings);
1871 1875 };
1872 1876
1873 1877 Notebook.prototype.rename = function (nbname) {
1874 1878 var that = this;
1875 1879 var data = {name: nbname + '.ipynb'};
1876 1880 var settings = {
1877 1881 processData : false,
1878 1882 cache : false,
1879 1883 type : "PATCH",
1880 1884 data : JSON.stringify(data),
1881 1885 dataType: "json",
1882 1886 headers : {'Content-Type': 'application/json'},
1883 1887 success : $.proxy(that.rename_success, this),
1884 1888 error : $.proxy(that.rename_error, this)
1885 1889 };
1886 1890 $([IPython.events]).trigger('rename_notebook.Notebook', data);
1887 1891 var url = utils.url_join_encode(
1888 1892 this._baseProjectUrl,
1889 1893 'api/notebooks',
1890 1894 this.notebook_path,
1891 1895 this.notebook_name
1892 1896 );
1893 1897 $.ajax(url, settings);
1894 1898 };
1895 1899
1896 1900
1897 1901 Notebook.prototype.rename_success = function (json, status, xhr) {
1898 1902 this.notebook_name = json.name;
1899 1903 var name = this.notebook_name;
1900 1904 var path = json.path;
1901 1905 this.session.rename_notebook(name, path);
1902 1906 $([IPython.events]).trigger('notebook_renamed.Notebook', json);
1903 1907 }
1904 1908
1905 1909 Notebook.prototype.rename_error = function (xhr, status, error) {
1906 1910 var that = this;
1907 1911 var dialog = $('<div/>').append(
1908 1912 $("<p/>").addClass("rename-message")
1909 1913 .html('This notebook name already exists.')
1910 1914 )
1911 1915 $([IPython.events]).trigger('notebook_rename_failed.Notebook', [xhr, status, error]);
1912 1916 IPython.dialog.modal({
1913 1917 title: "Notebook Rename Error!",
1914 1918 body: dialog,
1915 1919 buttons : {
1916 1920 "Cancel": {},
1917 1921 "OK": {
1918 1922 class: "btn-primary",
1919 1923 click: function () {
1920 1924 IPython.save_widget.rename_notebook();
1921 1925 }}
1922 1926 },
1923 1927 open : function (event, ui) {
1924 1928 var that = $(this);
1925 1929 // Upon ENTER, click the OK button.
1926 1930 that.find('input[type="text"]').keydown(function (event, ui) {
1927 1931 if (event.which === utils.keycodes.ENTER) {
1928 1932 that.find('.btn-primary').first().click();
1929 1933 }
1930 1934 });
1931 1935 that.find('input[type="text"]').focus();
1932 1936 }
1933 1937 });
1934 1938 }
1935 1939
1936 1940 /**
1937 1941 * Request a notebook's data from the server.
1938 1942 *
1939 1943 * @method load_notebook
1940 1944 * @param {String} notebook_name and path A notebook to load
1941 1945 */
1942 1946 Notebook.prototype.load_notebook = function (notebook_name, notebook_path) {
1943 1947 var that = this;
1944 1948 this.notebook_name = notebook_name;
1945 1949 this.notebook_path = notebook_path;
1946 1950 // We do the call with settings so we can set cache to false.
1947 1951 var settings = {
1948 1952 processData : false,
1949 1953 cache : false,
1950 1954 type : "GET",
1951 1955 dataType : "json",
1952 1956 success : $.proxy(this.load_notebook_success,this),
1953 1957 error : $.proxy(this.load_notebook_error,this),
1954 1958 };
1955 1959 $([IPython.events]).trigger('notebook_loading.Notebook');
1956 1960 var url = utils.url_join_encode(
1957 1961 this._baseProjectUrl,
1958 1962 'api/notebooks',
1959 1963 this.notebook_path,
1960 1964 this.notebook_name
1961 1965 );
1962 1966 $.ajax(url, settings);
1963 1967 };
1964 1968
1965 1969 /**
1966 1970 * Success callback for loading a notebook from the server.
1967 1971 *
1968 1972 * Load notebook data from the JSON response.
1969 1973 *
1970 1974 * @method load_notebook_success
1971 1975 * @param {Object} data JSON representation of a notebook
1972 1976 * @param {String} status Description of response status
1973 1977 * @param {jqXHR} xhr jQuery Ajax object
1974 1978 */
1975 1979 Notebook.prototype.load_notebook_success = function (data, status, xhr) {
1976 1980 this.fromJSON(data);
1977 1981 if (this.ncells() === 0) {
1978 1982 this.insert_cell_below('code');
1979 1983 this.select(0);
1980 1984 this.edit_mode();
1981 1985 } else {
1982 1986 this.select(0);
1983 1987 this.command_mode();
1984 1988 };
1985 1989 this.set_dirty(false);
1986 1990 this.scroll_to_top();
1987 1991 if (data.orig_nbformat !== undefined && data.nbformat !== data.orig_nbformat) {
1988 1992 var msg = "This notebook has been converted from an older " +
1989 1993 "notebook format (v"+data.orig_nbformat+") to the current notebook " +
1990 1994 "format (v"+data.nbformat+"). The next time you save this notebook, the " +
1991 1995 "newer notebook format will be used and older versions of IPython " +
1992 1996 "may not be able to read it. To keep the older version, close the " +
1993 1997 "notebook without saving it.";
1994 1998 IPython.dialog.modal({
1995 1999 title : "Notebook converted",
1996 2000 body : msg,
1997 2001 buttons : {
1998 2002 OK : {
1999 2003 class : "btn-primary"
2000 2004 }
2001 2005 }
2002 2006 });
2003 2007 } else if (data.orig_nbformat_minor !== undefined && data.nbformat_minor !== data.orig_nbformat_minor) {
2004 2008 var that = this;
2005 2009 var orig_vs = 'v' + data.nbformat + '.' + data.orig_nbformat_minor;
2006 2010 var this_vs = 'v' + data.nbformat + '.' + this.nbformat_minor;
2007 2011 var msg = "This notebook is version " + orig_vs + ", but we only fully support up to " +
2008 2012 this_vs + ". You can still work with this notebook, but some features " +
2009 2013 "introduced in later notebook versions may not be available."
2010 2014
2011 2015 IPython.dialog.modal({
2012 2016 title : "Newer Notebook",
2013 2017 body : msg,
2014 2018 buttons : {
2015 2019 OK : {
2016 2020 class : "btn-danger"
2017 2021 }
2018 2022 }
2019 2023 });
2020 2024
2021 2025 }
2022 2026
2023 2027 // Create the session after the notebook is completely loaded to prevent
2024 2028 // code execution upon loading, which is a security risk.
2025 2029 if (this.session == null) {
2026 2030 this.start_session();
2027 2031 }
2028 2032 // load our checkpoint list
2029 2033 this.list_checkpoints();
2030 2034
2031 2035 // load toolbar state
2032 2036 if (this.metadata.celltoolbar) {
2033 2037 IPython.CellToolbar.global_show();
2034 2038 IPython.CellToolbar.activate_preset(this.metadata.celltoolbar);
2035 2039 }
2036 2040
2037 2041 $([IPython.events]).trigger('notebook_loaded.Notebook');
2038 2042 };
2039 2043
2040 2044 /**
2041 2045 * Failure callback for loading a notebook from the server.
2042 2046 *
2043 2047 * @method load_notebook_error
2044 2048 * @param {jqXHR} xhr jQuery Ajax object
2045 2049 * @param {String} status Description of response status
2046 2050 * @param {String} error HTTP error message
2047 2051 */
2048 2052 Notebook.prototype.load_notebook_error = function (xhr, status, error) {
2049 2053 $([IPython.events]).trigger('notebook_load_failed.Notebook', [xhr, status, error]);
2050 2054 if (xhr.status === 400) {
2051 2055 var msg = error;
2052 2056 } else if (xhr.status === 500) {
2053 2057 var msg = "An unknown error occurred while loading this notebook. " +
2054 2058 "This version can load notebook formats " +
2055 2059 "v" + this.nbformat + " or earlier.";
2056 2060 }
2057 2061 IPython.dialog.modal({
2058 2062 title: "Error loading notebook",
2059 2063 body : msg,
2060 2064 buttons : {
2061 2065 "OK": {}
2062 2066 }
2063 2067 });
2064 2068 }
2065 2069
2066 2070 /********************* checkpoint-related *********************/
2067 2071
2068 2072 /**
2069 2073 * Save the notebook then immediately create a checkpoint.
2070 2074 *
2071 2075 * @method save_checkpoint
2072 2076 */
2073 2077 Notebook.prototype.save_checkpoint = function () {
2074 2078 this._checkpoint_after_save = true;
2075 2079 this.save_notebook();
2076 2080 };
2077 2081
2078 2082 /**
2079 2083 * Add a checkpoint for this notebook.
2080 2084 * for use as a callback from checkpoint creation.
2081 2085 *
2082 2086 * @method add_checkpoint
2083 2087 */
2084 2088 Notebook.prototype.add_checkpoint = function (checkpoint) {
2085 2089 var found = false;
2086 2090 for (var i = 0; i < this.checkpoints.length; i++) {
2087 2091 var existing = this.checkpoints[i];
2088 2092 if (existing.id == checkpoint.id) {
2089 2093 found = true;
2090 2094 this.checkpoints[i] = checkpoint;
2091 2095 break;
2092 2096 }
2093 2097 }
2094 2098 if (!found) {
2095 2099 this.checkpoints.push(checkpoint);
2096 2100 }
2097 2101 this.last_checkpoint = this.checkpoints[this.checkpoints.length - 1];
2098 2102 };
2099 2103
2100 2104 /**
2101 2105 * List checkpoints for this notebook.
2102 2106 *
2103 2107 * @method list_checkpoints
2104 2108 */
2105 2109 Notebook.prototype.list_checkpoints = function () {
2106 2110 var url = utils.url_join_encode(
2107 2111 this._baseProjectUrl,
2108 2112 'api/notebooks',
2109 2113 this.notebook_path,
2110 2114 this.notebook_name,
2111 2115 'checkpoints'
2112 2116 );
2113 2117 $.get(url).done(
2114 2118 $.proxy(this.list_checkpoints_success, this)
2115 2119 ).fail(
2116 2120 $.proxy(this.list_checkpoints_error, this)
2117 2121 );
2118 2122 };
2119 2123
2120 2124 /**
2121 2125 * Success callback for listing checkpoints.
2122 2126 *
2123 2127 * @method list_checkpoint_success
2124 2128 * @param {Object} data JSON representation of a checkpoint
2125 2129 * @param {String} status Description of response status
2126 2130 * @param {jqXHR} xhr jQuery Ajax object
2127 2131 */
2128 2132 Notebook.prototype.list_checkpoints_success = function (data, status, xhr) {
2129 2133 var data = $.parseJSON(data);
2130 2134 this.checkpoints = data;
2131 2135 if (data.length) {
2132 2136 this.last_checkpoint = data[data.length - 1];
2133 2137 } else {
2134 2138 this.last_checkpoint = null;
2135 2139 }
2136 2140 $([IPython.events]).trigger('checkpoints_listed.Notebook', [data]);
2137 2141 };
2138 2142
2139 2143 /**
2140 2144 * Failure callback for listing a checkpoint.
2141 2145 *
2142 2146 * @method list_checkpoint_error
2143 2147 * @param {jqXHR} xhr jQuery Ajax object
2144 2148 * @param {String} status Description of response status
2145 2149 * @param {String} error_msg HTTP error message
2146 2150 */
2147 2151 Notebook.prototype.list_checkpoints_error = function (xhr, status, error_msg) {
2148 2152 $([IPython.events]).trigger('list_checkpoints_failed.Notebook');
2149 2153 };
2150 2154
2151 2155 /**
2152 2156 * Create a checkpoint of this notebook on the server from the most recent save.
2153 2157 *
2154 2158 * @method create_checkpoint
2155 2159 */
2156 2160 Notebook.prototype.create_checkpoint = function () {
2157 2161 var url = utils.url_join_encode(
2158 2162 this._baseProjectUrl,
2159 2163 'api/notebooks',
2160 2164 this.notebookPath(),
2161 2165 this.notebook_name,
2162 2166 'checkpoints'
2163 2167 );
2164 2168 $.post(url).done(
2165 2169 $.proxy(this.create_checkpoint_success, this)
2166 2170 ).fail(
2167 2171 $.proxy(this.create_checkpoint_error, this)
2168 2172 );
2169 2173 };
2170 2174
2171 2175 /**
2172 2176 * Success callback for creating a checkpoint.
2173 2177 *
2174 2178 * @method create_checkpoint_success
2175 2179 * @param {Object} data JSON representation of a checkpoint
2176 2180 * @param {String} status Description of response status
2177 2181 * @param {jqXHR} xhr jQuery Ajax object
2178 2182 */
2179 2183 Notebook.prototype.create_checkpoint_success = function (data, status, xhr) {
2180 2184 var data = $.parseJSON(data);
2181 2185 this.add_checkpoint(data);
2182 2186 $([IPython.events]).trigger('checkpoint_created.Notebook', data);
2183 2187 };
2184 2188
2185 2189 /**
2186 2190 * Failure callback for creating a checkpoint.
2187 2191 *
2188 2192 * @method create_checkpoint_error
2189 2193 * @param {jqXHR} xhr jQuery Ajax object
2190 2194 * @param {String} status Description of response status
2191 2195 * @param {String} error_msg HTTP error message
2192 2196 */
2193 2197 Notebook.prototype.create_checkpoint_error = function (xhr, status, error_msg) {
2194 2198 $([IPython.events]).trigger('checkpoint_failed.Notebook');
2195 2199 };
2196 2200
2197 2201 Notebook.prototype.restore_checkpoint_dialog = function (checkpoint) {
2198 2202 var that = this;
2199 2203 var checkpoint = checkpoint || this.last_checkpoint;
2200 2204 if ( ! checkpoint ) {
2201 2205 console.log("restore dialog, but no checkpoint to restore to!");
2202 2206 return;
2203 2207 }
2204 2208 var body = $('<div/>').append(
2205 2209 $('<p/>').addClass("p-space").text(
2206 2210 "Are you sure you want to revert the notebook to " +
2207 2211 "the latest checkpoint?"
2208 2212 ).append(
2209 2213 $("<strong/>").text(
2210 2214 " This cannot be undone."
2211 2215 )
2212 2216 )
2213 2217 ).append(
2214 2218 $('<p/>').addClass("p-space").text("The checkpoint was last updated at:")
2215 2219 ).append(
2216 2220 $('<p/>').addClass("p-space").text(
2217 2221 Date(checkpoint.last_modified)
2218 2222 ).css("text-align", "center")
2219 2223 );
2220 2224
2221 2225 IPython.dialog.modal({
2222 2226 title : "Revert notebook to checkpoint",
2223 2227 body : body,
2224 2228 buttons : {
2225 2229 Revert : {
2226 2230 class : "btn-danger",
2227 2231 click : function () {
2228 2232 that.restore_checkpoint(checkpoint.id);
2229 2233 }
2230 2234 },
2231 2235 Cancel : {}
2232 2236 }
2233 2237 });
2234 2238 }
2235 2239
2236 2240 /**
2237 2241 * Restore the notebook to a checkpoint state.
2238 2242 *
2239 2243 * @method restore_checkpoint
2240 2244 * @param {String} checkpoint ID
2241 2245 */
2242 2246 Notebook.prototype.restore_checkpoint = function (checkpoint) {
2243 2247 $([IPython.events]).trigger('notebook_restoring.Notebook', checkpoint);
2244 2248 var url = utils.url_join_encode(
2245 2249 this._baseProjectUrl,
2246 2250 'api/notebooks',
2247 2251 this.notebookPath(),
2248 2252 this.notebook_name,
2249 2253 'checkpoints',
2250 2254 checkpoint
2251 2255 );
2252 2256 $.post(url).done(
2253 2257 $.proxy(this.restore_checkpoint_success, this)
2254 2258 ).fail(
2255 2259 $.proxy(this.restore_checkpoint_error, this)
2256 2260 );
2257 2261 };
2258 2262
2259 2263 /**
2260 2264 * Success callback for restoring a notebook to a checkpoint.
2261 2265 *
2262 2266 * @method restore_checkpoint_success
2263 2267 * @param {Object} data (ignored, should be empty)
2264 2268 * @param {String} status Description of response status
2265 2269 * @param {jqXHR} xhr jQuery Ajax object
2266 2270 */
2267 2271 Notebook.prototype.restore_checkpoint_success = function (data, status, xhr) {
2268 2272 $([IPython.events]).trigger('checkpoint_restored.Notebook');
2269 2273 this.load_notebook(this.notebook_name, this.notebook_path);
2270 2274 };
2271 2275
2272 2276 /**
2273 2277 * Failure callback for restoring a notebook to a checkpoint.
2274 2278 *
2275 2279 * @method restore_checkpoint_error
2276 2280 * @param {jqXHR} xhr jQuery Ajax object
2277 2281 * @param {String} status Description of response status
2278 2282 * @param {String} error_msg HTTP error message
2279 2283 */
2280 2284 Notebook.prototype.restore_checkpoint_error = function (xhr, status, error_msg) {
2281 2285 $([IPython.events]).trigger('checkpoint_restore_failed.Notebook');
2282 2286 };
2283 2287
2284 2288 /**
2285 2289 * Delete a notebook checkpoint.
2286 2290 *
2287 2291 * @method delete_checkpoint
2288 2292 * @param {String} checkpoint ID
2289 2293 */
2290 2294 Notebook.prototype.delete_checkpoint = function (checkpoint) {
2291 2295 $([IPython.events]).trigger('notebook_restoring.Notebook', checkpoint);
2292 2296 var url = utils.url_join_encode(
2293 2297 this._baseProjectUrl,
2294 2298 'api/notebooks',
2295 2299 this.notebookPath(),
2296 2300 this.notebook_name,
2297 2301 'checkpoints',
2298 2302 checkpoint
2299 2303 );
2300 2304 $.ajax(url, {
2301 2305 type: 'DELETE',
2302 2306 success: $.proxy(this.delete_checkpoint_success, this),
2303 2307 error: $.proxy(this.delete_notebook_error,this)
2304 2308 });
2305 2309 };
2306 2310
2307 2311 /**
2308 2312 * Success callback for deleting a notebook checkpoint
2309 2313 *
2310 2314 * @method delete_checkpoint_success
2311 2315 * @param {Object} data (ignored, should be empty)
2312 2316 * @param {String} status Description of response status
2313 2317 * @param {jqXHR} xhr jQuery Ajax object
2314 2318 */
2315 2319 Notebook.prototype.delete_checkpoint_success = function (data, status, xhr) {
2316 2320 $([IPython.events]).trigger('checkpoint_deleted.Notebook', data);
2317 2321 this.load_notebook(this.notebook_name, this.notebook_path);
2318 2322 };
2319 2323
2320 2324 /**
2321 2325 * Failure callback for deleting a notebook checkpoint.
2322 2326 *
2323 2327 * @method delete_checkpoint_error
2324 2328 * @param {jqXHR} xhr jQuery Ajax object
2325 2329 * @param {String} status Description of response status
2326 2330 * @param {String} error_msg HTTP error message
2327 2331 */
2328 2332 Notebook.prototype.delete_checkpoint_error = function (xhr, status, error_msg) {
2329 2333 $([IPython.events]).trigger('checkpoint_delete_failed.Notebook');
2330 2334 };
2331 2335
2332 2336
2333 2337 IPython.Notebook = Notebook;
2334 2338
2335 2339
2336 2340 return IPython;
2337 2341
2338 2342 }(IPython));
@@ -1,583 +1,599 b''
1 1 //----------------------------------------------------------------------------
2 2 // Copyright (C) 2008-2012 The IPython Development Team
3 3 //
4 4 // Distributed under the terms of the BSD License. The full license is in
5 5 // the file COPYING, distributed as part of this software.
6 6 //----------------------------------------------------------------------------
7 7
8 8 //============================================================================
9 9 // TextCell
10 10 //============================================================================
11 11
12 12
13 13
14 14 /**
15 15 A module that allow to create different type of Text Cell
16 16 @module IPython
17 17 @namespace IPython
18 18 */
19 19 var IPython = (function (IPython) {
20 20 "use strict";
21 21
22 22 // TextCell base class
23 23 var key = IPython.utils.keycodes;
24 24
25 25 /**
26 26 * Construct a new TextCell, codemirror mode is by default 'htmlmixed', and cell type is 'text'
27 27 * cell start as not redered.
28 28 *
29 29 * @class TextCell
30 30 * @constructor TextCell
31 31 * @extend IPython.Cell
32 32 * @param {object|undefined} [options]
33 33 * @param [options.cm_config] {object} config to pass to CodeMirror, will extend/overwrite default config
34 34 * @param [options.placeholder] {string} default string to use when souce in empty for rendering (only use in some TextCell subclass)
35 35 */
36 36 var TextCell = function (options) {
37 37 // in all TextCell/Cell subclasses
38 38 // do not assign most of members here, just pass it down
39 39 // in the options dict potentially overwriting what you wish.
40 40 // they will be assigned in the base class.
41 41
42 42 // we cannot put this as a class key as it has handle to "this".
43 43 var cm_overwrite_options = {
44 44 onKeyEvent: $.proxy(this.handle_codemirror_keyevent,this)
45 45 };
46 46
47 47 options = this.mergeopt(TextCell,options,{cm_config:cm_overwrite_options});
48 48
49 49 this.cell_type = this.cell_type || 'text';
50 50
51 51 IPython.Cell.apply(this, [options]);
52 52
53 53 this.rendered = false;
54 54 };
55 55
56 56 TextCell.prototype = new IPython.Cell();
57 57
58 58 TextCell.options_default = {
59 59 cm_config : {
60 60 extraKeys: {"Tab": "indentMore","Shift-Tab" : "indentLess"},
61 61 mode: 'htmlmixed',
62 62 lineWrapping : true,
63 63 }
64 64 };
65 65
66 66
67 67 /**
68 68 * Create the DOM element of the TextCell
69 69 * @method create_element
70 70 * @private
71 71 */
72 72 TextCell.prototype.create_element = function () {
73 73 IPython.Cell.prototype.create_element.apply(this, arguments);
74 74
75 75 var cell = $("<div>").addClass('cell text_cell border-box-sizing');
76 76 cell.attr('tabindex','2');
77 77
78 78 var prompt = $('<div/>').addClass('prompt input_prompt');
79 79 cell.append(prompt);
80 80 var inner_cell = $('<div/>').addClass('inner_cell');
81 81 this.celltoolbar = new IPython.CellToolbar(this);
82 82 inner_cell.append(this.celltoolbar.element);
83 83 var input_area = $('<div/>').addClass('text_cell_input border-box-sizing');
84 84 this.code_mirror = CodeMirror(input_area.get(0), this.cm_config);
85 85 // The tabindex=-1 makes this div focusable.
86 86 var render_area = $('<div/>').addClass('text_cell_render border-box-sizing').
87 87 addClass('rendered_html').attr('tabindex','-1');
88 88 inner_cell.append(input_area).append(render_area);
89 89 cell.append(inner_cell);
90 90 this.element = cell;
91 91 };
92 92
93 93
94 94 /**
95 95 * Bind the DOM evet to cell actions
96 96 * Need to be called after TextCell.create_element
97 97 * @private
98 98 * @method bind_event
99 99 */
100 100 TextCell.prototype.bind_events = function () {
101 101 IPython.Cell.prototype.bind_events.apply(this);
102 102 var that = this;
103 103
104 // TODO: move this to the notebook event handler
105 this.element.keydown(function (event) {
106 if (event.which === 13 && !event.shiftKey) {
107 if (that.rendered) {
108 that.unrender();
109 return false;
110 };
111 };
112 });
113
114 104 this.element.dblclick(function () {
115 that.unrender();
105 if (that.selected === false) {
106 $([IPython.events]).trigger('select.Cell', {'cell':that});
107 };
108 $([IPython.events]).trigger('edit_mode.Cell', {cell: that});
116 109 });
117 110 };
118 111
119 112
120 113 /**
121 114 * This method gets called in CodeMirror's onKeyDown/onKeyPress
122 115 * handlers and is used to provide custom key handling.
123 116 *
124 117 * Subclass should override this method to have custom handeling
125 118 *
126 119 * @method handle_codemirror_keyevent
127 120 * @param {CodeMirror} editor - The codemirror instance bound to the cell
128 121 * @param {event} event -
129 122 * @return {Boolean} `true` if CodeMirror should ignore the event, `false` Otherwise
130 123 */
131 124 TextCell.prototype.handle_codemirror_keyevent = function (editor, event) {
125 var that = this;
132 126 if (this.mode === 'command') {
133 127 return false
134 128 } else if (this.mode === 'edit') {
135 if (event.keyCode === 13 && (event.shiftKey || event.ctrlKey)) {
129 if (event.keyCode === 13 && (event.shiftKey || event.ctrlKey || event.altKey)) {
136 130 // Always ignore shift-enter in CodeMirror as we handle it.
137 131 return true;
138 };
132 } else if (event.which === key.UPARROW && event.type === 'keydown') {
133 // If we are not at the top, let CM handle the up arrow and
134 // prevent the global keydown handler from handling it.
135 if (!that.at_top()) {
136 event.stop();
137 return false;
138 } else {
139 return true;
140 };
141 } else if (event.which === key.DOWNARROW && event.type === 'keydown') {
142 // If we are not at the bottom, let CM handle the down arrow and
143 // prevent the global keydown handler from handling it.
144 if (!that.at_bottom()) {
145 event.stop();
146 return false;
147 } else {
148 return true;
149 };
150 }
139 151 return false;
140 152 };
141 153 return false;
142 154 };
143 155
144 156 // Cell level actions
145 157
146 158 TextCell.prototype.select = function () {
147 159 var cont = IPython.Cell.prototype.select.apply(this);
148 160 if (cont) {
149 161 if (this.mode === 'edit') {
150 162 this.code_mirror.refresh();
151 163 }
152 164 };
153 165 return cont;
154 166 };
155 167
156 168 TextCell.prototype.unrender = function () {
157 169 if (this.read_only) return;
158 170 var cont = IPython.Cell.prototype.unrender.apply(this);
159 171 if (cont) {
160 172 var text_cell = this.element;
161 173 var output = text_cell.find("div.text_cell_render");
162 174 output.hide();
163 175 text_cell.find('div.text_cell_input').show();
164 176 this.focus_editor();
165 177 if (this.get_text() === this.placeholder) {
166 178 this.set_text('');
167 179 this.refresh();
168 180 }
169 181
170 182 };
171 183 return cont;
172 184 };
173 185
174 186 TextCell.prototype.execute = function () {
175 187 this.render();
176 188 };
177 189
178 190 TextCell.prototype.command_mode = function () {
179 191 var cont = IPython.Cell.prototype.command_mode.apply(this);
180 192 if (cont) {
181 193 this.focus_cell();
182 194 };
183 195 return cont;
184 196 }
185 197
186 198 TextCell.prototype.edit_mode = function () {
187 199 var cont = IPython.Cell.prototype.edit_mode.apply(this);
188 200 if (cont) {
201 this.unrender();
189 202 this.focus_editor();
190 203 };
191 204 return cont;
192 205 }
193 206
194 207 /**
195 208 * setter: {{#crossLink "TextCell/set_text"}}{{/crossLink}}
196 209 * @method get_text
197 210 * @retrun {string} CodeMirror current text value
198 211 */
199 212 TextCell.prototype.get_text = function() {
200 213 return this.code_mirror.getValue();
201 214 };
202 215
203 216 /**
204 217 * @param {string} text - Codemiror text value
205 218 * @see TextCell#get_text
206 219 * @method set_text
207 220 * */
208 221 TextCell.prototype.set_text = function(text) {
209 222 this.code_mirror.setValue(text);
210 223 this.code_mirror.refresh();
211 224 };
212 225
213 226 /**
214 227 * setter :{{#crossLink "TextCell/set_rendered"}}{{/crossLink}}
215 228 * @method get_rendered
216 229 * @return {html} html of rendered element
217 230 * */
218 231 TextCell.prototype.get_rendered = function() {
219 232 return this.element.find('div.text_cell_render').html();
220 233 };
221 234
222 235 /**
223 236 * @method set_rendered
224 237 */
225 238 TextCell.prototype.set_rendered = function(text) {
226 239 this.element.find('div.text_cell_render').html(text);
227 240 };
228 241
229 242 /**
230 243 * @method at_top
231 244 * @return {Boolean}
232 245 */
233 246 TextCell.prototype.at_top = function () {
234 247 if (this.rendered) {
235 248 return true;
236 249 } else {
250 var cursor = this.code_mirror.getCursor();
237 251 if (cursor.line === 0 && cursor.ch === 0) {
238 252 return true;
239 253 } else {
240 254 return false;
241 255 };
242 256 };
243 257 };
244 258
245 259
246 260 /** @method at_bottom **/
247 261 TextCell.prototype.at_bottom = function () {
248 262 if (this.rendered) {
249 263 return true
250 264 } else {
251 265 var cursor = this.code_mirror.getCursor();
252 266 if (cursor.line === (this.code_mirror.lineCount()-1) && cursor.ch === this.code_mirror.getLine(cursor.line).length) {
253 267 return true;
254 268 } else {
255 269 return false;
256 270 };
257 271 };
258 272 };
259 273
260 274 /**
261 275 * @method at_bottom
262 276 * @return {Boolean}
263 277 * */
264 278 TextCell.prototype.at_bottom = function () {
265 279 if (this.rendered) {
266 280 return true;
267 281 } else {
268 282 var cursor = this.code_mirror.getCursor();
269 283 if (cursor.line === (this.code_mirror.lineCount()-1) && cursor.ch === this.code_mirror.getLine(cursor.line).length) {
270 284 return true;
271 285 } else {
272 286 return false;
273 287 };
274 288 };
275 289 };
276 290
277 291 /**
278 292 * Create Text cell from JSON
279 293 * @param {json} data - JSON serialized text-cell
280 294 * @method fromJSON
281 295 */
282 296 TextCell.prototype.fromJSON = function (data) {
283 297 IPython.Cell.prototype.fromJSON.apply(this, arguments);
284 298 if (data.cell_type === this.cell_type) {
285 299 if (data.source !== undefined) {
286 300 this.set_text(data.source);
287 301 // make this value the starting point, so that we can only undo
288 302 // to this state, instead of a blank cell
289 303 this.code_mirror.clearHistory();
290 304 this.set_rendered(data.rendered || '');
291 305 this.rendered = false;
292 306 this.render();
293 307 }
294 308 }
295 309 };
296 310
297 311 /** Generate JSON from cell
298 312 * @return {object} cell data serialised to json
299 313 */
300 314 TextCell.prototype.toJSON = function () {
301 315 var data = IPython.Cell.prototype.toJSON.apply(this);
302 316 data.source = this.get_text();
303 317 if (data.source == this.placeholder) {
304 318 data.source = "";
305 319 }
306 320 return data;
307 321 };
308 322
309 323
310 324 /**
311 325 * @class MarkdownCell
312 326 * @constructor MarkdownCell
313 327 * @extends IPython.HTMLCell
314 328 */
315 329 var MarkdownCell = function (options) {
316 330 options = this.mergeopt(MarkdownCell, options);
317 331
318 332 this.cell_type = 'markdown';
319 333 TextCell.apply(this, [options]);
320 334 };
321 335
322 336 MarkdownCell.options_default = {
323 337 cm_config: {
324 338 mode: 'gfm'
325 339 },
326 340 placeholder: "Type *Markdown* and LaTeX: $\\alpha^2$"
327 341 }
328 342
329 343 MarkdownCell.prototype = new TextCell();
330 344
331 345 /**
332 346 * @method render
333 347 */
334 348 MarkdownCell.prototype.render = function () {
335 349 var cont = IPython.TextCell.prototype.render.apply(this);
336 350 if (cont) {
337 351 var text = this.get_text();
338 352 var math = null;
339 353 if (text === "") { text = this.placeholder; }
340 354 var text_and_math = IPython.mathjaxutils.remove_math(text);
341 355 text = text_and_math[0];
342 356 math = text_and_math[1];
343 357 var html = marked.parser(marked.lexer(text));
344 358 html = $(IPython.mathjaxutils.replace_math(html, math));
345 359 // links in markdown cells should open in new tabs
346 360 html.find("a[href]").not('[href^="#"]').attr("target", "_blank");
347 361 try {
348 362 this.set_rendered(html);
349 363 } catch (e) {
350 364 console.log("Error running Javascript in Markdown:");
351 365 console.log(e);
352 366 this.set_rendered($("<div/>").addClass("js-error").html(
353 367 "Error rendering Markdown!<br/>" + e.toString())
354 368 );
355 369 }
356 370 this.element.find('div.text_cell_input').hide();
357 371 this.element.find("div.text_cell_render").show();
358 372 this.typeset()
359 373 };
360 374 return cont;
361 375 };
362 376
363 377
364 378 // RawCell
365 379
366 380 /**
367 381 * @class RawCell
368 382 * @constructor RawCell
369 383 * @extends IPython.TextCell
370 384 */
371 385 var RawCell = function (options) {
372 options = this.mergeopt(RawCell, options);
373
374 this.cell_type = 'raw';
386
387 options = this.mergeopt(RawCell,options)
375 388 TextCell.apply(this, [options]);
389 this.cell_type = 'raw';
390 // RawCell should always hide its rendered div
391 this.element.find('div.text_cell_render').hide();
376 392 };
377 393
378 394 RawCell.options_default = {
379 395 placeholder : "Write raw LaTeX or other formats here, for use with nbconvert.\n" +
380 396 "It will not be rendered in the notebook.\n" +
381 397 "When passing through nbconvert, a Raw Cell's content is added to the output unmodified."
382 398 };
383 399
384 400 RawCell.prototype = new TextCell();
385 401
386 402 /** @method bind_events **/
387 403 RawCell.prototype.bind_events = function () {
388 404 TextCell.prototype.bind_events.apply(this);
389 405 var that = this
390 406 this.element.focusout(function() {
391 407 that.auto_highlight();
392 408 });
393 409 };
394 410
395 411 /**
396 412 * Trigger autodetection of highlight scheme for current cell
397 413 * @method auto_highlight
398 414 */
399 415 RawCell.prototype.auto_highlight = function () {
400 416 this._auto_highlight(IPython.config.raw_cell_highlight);
401 417 };
402 418
403 419 /** @method render **/
404 420 RawCell.prototype.render = function () {
405 421 // Make sure that this cell type can never be rendered
406 422 if (this.rendered) {
407 423 this.unrender();
408 424 }
409 425 var text = this.get_text();
410 426 if (text === "") { text = this.placeholder; }
411 427 this.set_text(text);
412 428 };
413 429
414 430
415 431 /** @method handle_codemirror_keyevent **/
416 432 RawCell.prototype.handle_codemirror_keyevent = function (editor, event) {
417 433
418 434 var that = this;
419 435 if (this.mode === 'command') {
420 436 return false
421 437 } else if (this.mode === 'edit') {
422 438 // TODO: review these handlers...
423 439 if (event.which === key.UPARROW && event.type === 'keydown') {
424 440 // If we are not at the top, let CM handle the up arrow and
425 441 // prevent the global keydown handler from handling it.
426 442 if (!that.at_top()) {
427 443 event.stop();
428 444 return false;
429 445 } else {
430 446 return true;
431 447 };
432 448 } else if (event.which === key.DOWNARROW && event.type === 'keydown') {
433 449 // If we are not at the bottom, let CM handle the down arrow and
434 450 // prevent the global keydown handler from handling it.
435 451 if (!that.at_bottom()) {
436 452 event.stop();
437 453 return false;
438 454 } else {
439 455 return true;
440 456 };
441 457 };
442 458 return false;
443 459 };
444 460 return false;
445 461 };
446 462
447 463
448 464 /**
449 465 * @class HeadingCell
450 466 * @extends IPython.TextCell
451 467 */
452 468
453 469 /**
454 470 * @constructor HeadingCell
455 471 * @extends IPython.TextCell
456 472 */
457 473 var HeadingCell = function (options) {
458 474 options = this.mergeopt(HeadingCell, options);
459 475
460 476 this.level = 1;
461 477 this.cell_type = 'heading';
462 478 TextCell.apply(this, [options]);
463 479
464 480 /**
465 481 * heading level of the cell, use getter and setter to access
466 482 * @property level
467 483 */
468 484 };
469 485
470 486 HeadingCell.options_default = {
471 487 placeholder: "Type Heading Here"
472 488 };
473 489
474 490 HeadingCell.prototype = new TextCell();
475 491
476 492 /** @method fromJSON */
477 493 HeadingCell.prototype.fromJSON = function (data) {
478 494 if (data.level != undefined){
479 495 this.level = data.level;
480 496 }
481 497 TextCell.prototype.fromJSON.apply(this, arguments);
482 498 };
483 499
484 500
485 501 /** @method toJSON */
486 502 HeadingCell.prototype.toJSON = function () {
487 503 var data = TextCell.prototype.toJSON.apply(this);
488 504 data.level = this.get_level();
489 505 return data;
490 506 };
491 507
492 508 /**
493 509 * can the cell be split into two cells
494 510 * @method is_splittable
495 511 **/
496 512 HeadingCell.prototype.is_splittable = function () {
497 513 return false;
498 514 };
499 515
500 516
501 517 /**
502 518 * can the cell be merged with other cells
503 519 * @method is_mergeable
504 520 **/
505 521 HeadingCell.prototype.is_mergeable = function () {
506 522 return false;
507 523 };
508 524
509 525 /**
510 526 * Change heading level of cell, and re-render
511 527 * @method set_level
512 528 */
513 529 HeadingCell.prototype.set_level = function (level) {
514 530 this.level = level;
515 531 if (this.rendered) {
516 532 this.rendered = false;
517 533 this.render();
518 534 };
519 535 };
520 536
521 537 /** The depth of header cell, based on html (h1 to h6)
522 538 * @method get_level
523 539 * @return {integer} level - for 1 to 6
524 540 */
525 541 HeadingCell.prototype.get_level = function () {
526 542 return this.level;
527 543 };
528 544
529 545
530 546 HeadingCell.prototype.set_rendered = function (html) {
531 547 this.element.find("div.text_cell_render").html(html);
532 548 };
533 549
534 550
535 551 HeadingCell.prototype.get_rendered = function () {
536 552 var r = this.element.find("div.text_cell_render");
537 553 return r.children().first().html();
538 554 };
539 555
540 556
541 557 HeadingCell.prototype.render = function () {
542 558 var cont = IPython.TextCell.prototype.render.apply(this);
543 559 if (cont) {
544 560 var text = this.get_text();
545 561 var math = null;
546 562 // Markdown headings must be a single line
547 563 text = text.replace(/\n/g, ' ');
548 564 if (text === "") { text = this.placeholder; }
549 565 text = Array(this.level + 1).join("#") + " " + text;
550 566 var text_and_math = IPython.mathjaxutils.remove_math(text);
551 567 text = text_and_math[0];
552 568 math = text_and_math[1];
553 569 var html = marked.parser(marked.lexer(text));
554 570 var h = $(IPython.mathjaxutils.replace_math(html, math));
555 571 // add id and linkback anchor
556 572 var hash = h.text().replace(/ /g, '-');
557 573 h.attr('id', hash);
558 574 h.append(
559 575 $('<a/>')
560 576 .addClass('anchor-link')
561 577 .attr('href', '#' + hash)
562 578 .text('¶')
563 579 );
564 580
565 581 this.set_rendered(h);
566 582 this.typeset();
567 583 this.element.find('div.text_cell_input').hide();
568 584 this.element.find("div.text_cell_render").show();
569 585
570 586 };
571 587 return cont;
572 588 };
573 589
574 590 IPython.TextCell = TextCell;
575 591 IPython.MarkdownCell = MarkdownCell;
576 592 IPython.RawCell = RawCell;
577 593 IPython.HeadingCell = HeadingCell;
578 594
579 595
580 596 return IPython;
581 597
582 598 }(IPython));
583 599
General Comments 0
You need to be logged in to leave comments. Login now