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