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