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