Show More
@@ -1,1782 +1,1740 | |||||
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 | // single worksheet for now |
|
42 | // single worksheet for now | |
43 | this.worksheet_metadata = {}; |
|
43 | this.worksheet_metadata = {}; | |
44 | this.control_key_active = false; |
|
44 | this.control_key_active = false; | |
45 | this.notebook_id = null; |
|
45 | this.notebook_id = null; | |
46 | this.notebook_name = null; |
|
46 | this.notebook_name = null; | |
47 | this.notebook_name_blacklist_re = /[\/\\:]/; |
|
47 | this.notebook_name_blacklist_re = /[\/\\:]/; | |
48 | this.nbformat = 3 // Increment this when changing the nbformat |
|
48 | this.nbformat = 3 // Increment this when changing the nbformat | |
49 | this.nbformat_minor = 0 // Increment this when changing the nbformat |
|
49 | this.nbformat_minor = 0 // Increment this when changing the nbformat | |
50 | this.style(); |
|
50 | this.style(); | |
51 | this.create_elements(); |
|
51 | this.create_elements(); | |
52 | this.bind_events(); |
|
52 | this.bind_events(); | |
53 | }; |
|
53 | }; | |
54 |
|
54 | |||
55 | /** |
|
55 | /** | |
56 | * Tweak the notebook's CSS style. |
|
56 | * Tweak the notebook's CSS style. | |
57 | * |
|
57 | * | |
58 | * @method style |
|
58 | * @method style | |
59 | */ |
|
59 | */ | |
60 | Notebook.prototype.style = function () { |
|
60 | Notebook.prototype.style = function () { | |
61 | $('div#notebook').addClass('border-box-sizing'); |
|
61 | $('div#notebook').addClass('border-box-sizing'); | |
62 | }; |
|
62 | }; | |
63 |
|
63 | |||
64 | /** |
|
64 | /** | |
65 | * Get the root URL of the notebook server. |
|
65 | * Get the root URL of the notebook server. | |
66 | * |
|
66 | * | |
67 | * @method baseProjectUrl |
|
67 | * @method baseProjectUrl | |
68 | * @return {String} The base project URL |
|
68 | * @return {String} The base project URL | |
69 | */ |
|
69 | */ | |
70 | Notebook.prototype.baseProjectUrl = function(){ |
|
70 | Notebook.prototype.baseProjectUrl = function(){ | |
71 | return this._baseProjectUrl || $('body').data('baseProjectUrl'); |
|
71 | return this._baseProjectUrl || $('body').data('baseProjectUrl'); | |
72 | }; |
|
72 | }; | |
73 |
|
73 | |||
74 | /** |
|
74 | /** | |
75 | * Create an HTML and CSS representation of the notebook. |
|
75 | * Create an HTML and CSS representation of the notebook. | |
76 | * |
|
76 | * | |
77 | * @method create_elements |
|
77 | * @method create_elements | |
78 | */ |
|
78 | */ | |
79 | Notebook.prototype.create_elements = function () { |
|
79 | Notebook.prototype.create_elements = function () { | |
80 | // We add this end_space div to the end of the notebook div to: |
|
80 | // We add this end_space div to the end of the notebook div to: | |
81 | // i) provide a margin between the last cell and the end of the notebook |
|
81 | // i) provide a margin between the last cell and the end of the notebook | |
82 | // ii) to prevent the div from scrolling up when the last cell is being |
|
82 | // ii) to prevent the div from scrolling up when the last cell is being | |
83 | // edited, but is too low on the page, which browsers will do automatically. |
|
83 | // edited, but is too low on the page, which browsers will do automatically. | |
84 | var that = this; |
|
84 | var that = this; | |
85 | var end_space = $('<div/>').addClass('end_space').height("30%"); |
|
85 | var end_space = $('<div/>').addClass('end_space').height("30%"); | |
86 | end_space.dblclick(function (e) { |
|
86 | end_space.dblclick(function (e) { | |
87 | if (that.read_only) return; |
|
87 | if (that.read_only) return; | |
88 | var ncells = that.ncells(); |
|
88 | var ncells = that.ncells(); | |
89 | that.insert_cell_below('code',ncells-1); |
|
89 | that.insert_cell_below('code',ncells-1); | |
90 | }); |
|
90 | }); | |
91 | this.element.append(end_space); |
|
91 | this.element.append(end_space); | |
92 | $('div#notebook').addClass('border-box-sizing'); |
|
92 | $('div#notebook').addClass('border-box-sizing'); | |
93 | }; |
|
93 | }; | |
94 |
|
94 | |||
95 | /** |
|
95 | /** | |
96 | * Bind JavaScript events: key presses and custom IPython events. |
|
96 | * Bind JavaScript events: key presses and custom IPython events. | |
97 | * |
|
97 | * | |
98 | * @method bind_events |
|
98 | * @method bind_events | |
99 | */ |
|
99 | */ | |
100 | Notebook.prototype.bind_events = function () { |
|
100 | Notebook.prototype.bind_events = function () { | |
101 | var that = this; |
|
101 | var that = this; | |
102 |
|
102 | |||
103 | $([IPython.events]).on('set_next_input.Notebook', function (event, data) { |
|
103 | $([IPython.events]).on('set_next_input.Notebook', function (event, data) { | |
104 | var index = that.find_cell_index(data.cell); |
|
104 | var index = that.find_cell_index(data.cell); | |
105 | var new_cell = that.insert_cell_below('code',index); |
|
105 | var new_cell = that.insert_cell_below('code',index); | |
106 | new_cell.set_text(data.text); |
|
106 | new_cell.set_text(data.text); | |
107 | that.dirty = true; |
|
107 | that.dirty = true; | |
108 | }); |
|
108 | }); | |
109 |
|
109 | |||
110 | $([IPython.events]).on('set_dirty.Notebook', function (event, data) { |
|
110 | $([IPython.events]).on('set_dirty.Notebook', function (event, data) { | |
111 | that.dirty = data.value; |
|
111 | that.dirty = data.value; | |
112 | }); |
|
112 | }); | |
113 |
|
113 | |||
114 | $([IPython.events]).on('select.Cell', function (event, data) { |
|
114 | $([IPython.events]).on('select.Cell', function (event, data) { | |
115 | var index = that.find_cell_index(data.cell); |
|
115 | var index = that.find_cell_index(data.cell); | |
116 | that.select(index); |
|
116 | that.select(index); | |
117 | }); |
|
117 | }); | |
118 |
|
118 | |||
119 |
|
119 | |||
120 | $(document).keydown(function (event) { |
|
120 | $(document).keydown(function (event) { | |
121 | // console.log(event); |
|
121 | // console.log(event); | |
122 | if (that.read_only) return true; |
|
122 | if (that.read_only) return true; | |
123 |
|
123 | |||
124 | // Save (CTRL+S) or (AppleKey+S) |
|
124 | // Save (CTRL+S) or (AppleKey+S) | |
125 | //metaKey = applekey on mac |
|
125 | //metaKey = applekey on mac | |
126 | if ((event.ctrlKey || event.metaKey) && event.keyCode==83) { |
|
126 | if ((event.ctrlKey || event.metaKey) && event.keyCode==83) { | |
127 | that.save_notebook(); |
|
127 | that.save_notebook(); | |
128 | event.preventDefault(); |
|
128 | event.preventDefault(); | |
129 | return false; |
|
129 | return false; | |
130 | } else if (event.which === key.ESC) { |
|
130 | } else if (event.which === key.ESC) { | |
131 | // Intercept escape at highest level to avoid closing |
|
131 | // Intercept escape at highest level to avoid closing | |
132 | // websocket connection with firefox |
|
132 | // websocket connection with firefox | |
133 | event.preventDefault(); |
|
133 | event.preventDefault(); | |
134 | } else if (event.which === key.SHIFT) { |
|
134 | } else if (event.which === key.SHIFT) { | |
135 | // ignore shift keydown |
|
135 | // ignore shift keydown | |
136 | return true; |
|
136 | return true; | |
137 | } |
|
137 | } | |
138 | if (event.which === key.UPARROW && !event.shiftKey) { |
|
138 | if (event.which === key.UPARROW && !event.shiftKey) { | |
139 | var cell = that.get_selected_cell(); |
|
139 | var cell = that.get_selected_cell(); | |
140 | if (cell && cell.at_top()) { |
|
140 | if (cell && cell.at_top()) { | |
141 | event.preventDefault(); |
|
141 | event.preventDefault(); | |
142 | that.select_prev(); |
|
142 | that.select_prev(); | |
143 | }; |
|
143 | }; | |
144 | } else if (event.which === key.DOWNARROW && !event.shiftKey) { |
|
144 | } else if (event.which === key.DOWNARROW && !event.shiftKey) { | |
145 | var cell = that.get_selected_cell(); |
|
145 | var cell = that.get_selected_cell(); | |
146 | if (cell && cell.at_bottom()) { |
|
146 | if (cell && cell.at_bottom()) { | |
147 | event.preventDefault(); |
|
147 | event.preventDefault(); | |
148 | that.select_next(); |
|
148 | that.select_next(); | |
149 | }; |
|
149 | }; | |
150 | } else if (event.which === key.ENTER && event.shiftKey) { |
|
150 | } else if (event.which === key.ENTER && event.shiftKey) { | |
151 | that.execute_selected_cell(); |
|
151 | that.execute_selected_cell(); | |
152 | return false; |
|
152 | return false; | |
153 | } else if (event.which === key.ENTER && event.altKey) { |
|
153 | } else if (event.which === key.ENTER && event.altKey) { | |
154 | // Execute code cell, and insert new in place |
|
154 | // Execute code cell, and insert new in place | |
155 | that.execute_selected_cell(); |
|
155 | that.execute_selected_cell(); | |
156 | // Only insert a new cell, if we ended up in an already populated cell |
|
156 | // Only insert a new cell, if we ended up in an already populated cell | |
157 | if (/\S/.test(that.get_selected_cell().get_text()) == true) { |
|
157 | if (/\S/.test(that.get_selected_cell().get_text()) == true) { | |
158 | that.insert_cell_above('code'); |
|
158 | that.insert_cell_above('code'); | |
159 | } |
|
159 | } | |
160 | return false; |
|
160 | return false; | |
161 | } else if (event.which === key.ENTER && event.ctrlKey) { |
|
161 | } else if (event.which === key.ENTER && event.ctrlKey) { | |
162 | that.execute_selected_cell({terminal:true}); |
|
162 | that.execute_selected_cell({terminal:true}); | |
163 | return false; |
|
163 | return false; | |
164 | } else if (event.which === 77 && event.ctrlKey && that.control_key_active == false) { |
|
164 | } else if (event.which === 77 && event.ctrlKey && that.control_key_active == false) { | |
165 | that.control_key_active = true; |
|
165 | that.control_key_active = true; | |
166 | return false; |
|
166 | return false; | |
167 | } else if (event.which === 88 && that.control_key_active) { |
|
167 | } else if (event.which === 88 && that.control_key_active) { | |
168 | // Cut selected cell = x |
|
168 | // Cut selected cell = x | |
169 | that.cut_cell(); |
|
169 | that.cut_cell(); | |
170 | that.control_key_active = false; |
|
170 | that.control_key_active = false; | |
171 | return false; |
|
171 | return false; | |
172 | } else if (event.which === 67 && that.control_key_active) { |
|
172 | } else if (event.which === 67 && that.control_key_active) { | |
173 | // Copy selected cell = c |
|
173 | // Copy selected cell = c | |
174 | that.copy_cell(); |
|
174 | that.copy_cell(); | |
175 | that.control_key_active = false; |
|
175 | that.control_key_active = false; | |
176 | return false; |
|
176 | return false; | |
177 | } else if (event.which === 86 && that.control_key_active) { |
|
177 | } else if (event.which === 86 && that.control_key_active) { | |
178 | // Paste below selected cell = v |
|
178 | // Paste below selected cell = v | |
179 | that.paste_cell_below(); |
|
179 | that.paste_cell_below(); | |
180 | that.control_key_active = false; |
|
180 | that.control_key_active = false; | |
181 | return false; |
|
181 | return false; | |
182 | } else if (event.which === 68 && that.control_key_active) { |
|
182 | } else if (event.which === 68 && that.control_key_active) { | |
183 | // Delete selected cell = d |
|
183 | // Delete selected cell = d | |
184 | that.delete_cell(); |
|
184 | that.delete_cell(); | |
185 | that.control_key_active = false; |
|
185 | that.control_key_active = false; | |
186 | return false; |
|
186 | return false; | |
187 | } else if (event.which === 65 && that.control_key_active) { |
|
187 | } else if (event.which === 65 && that.control_key_active) { | |
188 | // Insert code cell above selected = a |
|
188 | // Insert code cell above selected = a | |
189 | that.insert_cell_above('code'); |
|
189 | that.insert_cell_above('code'); | |
190 | that.control_key_active = false; |
|
190 | that.control_key_active = false; | |
191 | return false; |
|
191 | return false; | |
192 | } else if (event.which === 66 && that.control_key_active) { |
|
192 | } else if (event.which === 66 && that.control_key_active) { | |
193 | // Insert code cell below selected = b |
|
193 | // Insert code cell below selected = b | |
194 | that.insert_cell_below('code'); |
|
194 | that.insert_cell_below('code'); | |
195 | that.control_key_active = false; |
|
195 | that.control_key_active = false; | |
196 | return false; |
|
196 | return false; | |
197 | } else if (event.which === 89 && that.control_key_active) { |
|
197 | } else if (event.which === 89 && that.control_key_active) { | |
198 | // To code = y |
|
198 | // To code = y | |
199 | that.to_code(); |
|
199 | that.to_code(); | |
200 | that.control_key_active = false; |
|
200 | that.control_key_active = false; | |
201 | return false; |
|
201 | return false; | |
202 | } else if (event.which === 77 && that.control_key_active) { |
|
202 | } else if (event.which === 77 && that.control_key_active) { | |
203 | // To markdown = m |
|
203 | // To markdown = m | |
204 | that.to_markdown(); |
|
204 | that.to_markdown(); | |
205 | that.control_key_active = false; |
|
205 | that.control_key_active = false; | |
206 | return false; |
|
206 | return false; | |
207 | } else if (event.which === 84 && that.control_key_active) { |
|
207 | } else if (event.which === 84 && that.control_key_active) { | |
208 | // To Raw = t |
|
208 | // To Raw = t | |
209 | that.to_raw(); |
|
209 | that.to_raw(); | |
210 | that.control_key_active = false; |
|
210 | that.control_key_active = false; | |
211 | return false; |
|
211 | return false; | |
212 | } else if (event.which === 49 && that.control_key_active) { |
|
212 | } else if (event.which === 49 && that.control_key_active) { | |
213 | // To Heading 1 = 1 |
|
213 | // To Heading 1 = 1 | |
214 | that.to_heading(undefined, 1); |
|
214 | that.to_heading(undefined, 1); | |
215 | that.control_key_active = false; |
|
215 | that.control_key_active = false; | |
216 | return false; |
|
216 | return false; | |
217 | } else if (event.which === 50 && that.control_key_active) { |
|
217 | } else if (event.which === 50 && that.control_key_active) { | |
218 | // To Heading 2 = 2 |
|
218 | // To Heading 2 = 2 | |
219 | that.to_heading(undefined, 2); |
|
219 | that.to_heading(undefined, 2); | |
220 | that.control_key_active = false; |
|
220 | that.control_key_active = false; | |
221 | return false; |
|
221 | return false; | |
222 | } else if (event.which === 51 && that.control_key_active) { |
|
222 | } else if (event.which === 51 && that.control_key_active) { | |
223 | // To Heading 3 = 3 |
|
223 | // To Heading 3 = 3 | |
224 | that.to_heading(undefined, 3); |
|
224 | that.to_heading(undefined, 3); | |
225 | that.control_key_active = false; |
|
225 | that.control_key_active = false; | |
226 | return false; |
|
226 | return false; | |
227 | } else if (event.which === 52 && that.control_key_active) { |
|
227 | } else if (event.which === 52 && that.control_key_active) { | |
228 | // To Heading 4 = 4 |
|
228 | // To Heading 4 = 4 | |
229 | that.to_heading(undefined, 4); |
|
229 | that.to_heading(undefined, 4); | |
230 | that.control_key_active = false; |
|
230 | that.control_key_active = false; | |
231 | return false; |
|
231 | return false; | |
232 | } else if (event.which === 53 && that.control_key_active) { |
|
232 | } else if (event.which === 53 && that.control_key_active) { | |
233 | // To Heading 5 = 5 |
|
233 | // To Heading 5 = 5 | |
234 | that.to_heading(undefined, 5); |
|
234 | that.to_heading(undefined, 5); | |
235 | that.control_key_active = false; |
|
235 | that.control_key_active = false; | |
236 | return false; |
|
236 | return false; | |
237 | } else if (event.which === 54 && that.control_key_active) { |
|
237 | } else if (event.which === 54 && that.control_key_active) { | |
238 | // To Heading 6 = 6 |
|
238 | // To Heading 6 = 6 | |
239 | that.to_heading(undefined, 6); |
|
239 | that.to_heading(undefined, 6); | |
240 | that.control_key_active = false; |
|
240 | that.control_key_active = false; | |
241 | return false; |
|
241 | return false; | |
242 | } else if (event.which === 79 && that.control_key_active) { |
|
242 | } else if (event.which === 79 && that.control_key_active) { | |
243 | // Toggle output = o |
|
243 | // Toggle output = o | |
244 | if (event.shiftKey){ |
|
244 | if (event.shiftKey){ | |
245 | that.toggle_output_scroll(); |
|
245 | that.toggle_output_scroll(); | |
246 | } else { |
|
246 | } else { | |
247 | that.toggle_output(); |
|
247 | that.toggle_output(); | |
248 | } |
|
248 | } | |
249 | that.control_key_active = false; |
|
249 | that.control_key_active = false; | |
250 | return false; |
|
250 | return false; | |
251 | } else if (event.which === 83 && that.control_key_active) { |
|
251 | } else if (event.which === 83 && that.control_key_active) { | |
252 | // Save notebook = s |
|
252 | // Save notebook = s | |
253 | that.save_notebook(); |
|
253 | that.save_notebook(); | |
254 | that.control_key_active = false; |
|
254 | that.control_key_active = false; | |
255 | return false; |
|
255 | return false; | |
256 | } else if (event.which === 74 && that.control_key_active) { |
|
256 | } else if (event.which === 74 && that.control_key_active) { | |
257 | // Move cell down = j |
|
257 | // Move cell down = j | |
258 | that.move_cell_down(); |
|
258 | that.move_cell_down(); | |
259 | that.control_key_active = false; |
|
259 | that.control_key_active = false; | |
260 | return false; |
|
260 | return false; | |
261 | } else if (event.which === 75 && that.control_key_active) { |
|
261 | } else if (event.which === 75 && that.control_key_active) { | |
262 | // Move cell up = k |
|
262 | // Move cell up = k | |
263 | that.move_cell_up(); |
|
263 | that.move_cell_up(); | |
264 | that.control_key_active = false; |
|
264 | that.control_key_active = false; | |
265 | return false; |
|
265 | return false; | |
266 | } else if (event.which === 80 && that.control_key_active) { |
|
266 | } else if (event.which === 80 && that.control_key_active) { | |
267 | // Select previous = p |
|
267 | // Select previous = p | |
268 | that.select_prev(); |
|
268 | that.select_prev(); | |
269 | that.control_key_active = false; |
|
269 | that.control_key_active = false; | |
270 | return false; |
|
270 | return false; | |
271 | } else if (event.which === 78 && that.control_key_active) { |
|
271 | } else if (event.which === 78 && that.control_key_active) { | |
272 | // Select next = n |
|
272 | // Select next = n | |
273 | that.select_next(); |
|
273 | that.select_next(); | |
274 | that.control_key_active = false; |
|
274 | that.control_key_active = false; | |
275 | return false; |
|
275 | return false; | |
276 | } else if (event.which === 76 && that.control_key_active) { |
|
276 | } else if (event.which === 76 && that.control_key_active) { | |
277 | // Toggle line numbers = l |
|
277 | // Toggle line numbers = l | |
278 | that.cell_toggle_line_numbers(); |
|
278 | that.cell_toggle_line_numbers(); | |
279 | that.control_key_active = false; |
|
279 | that.control_key_active = false; | |
280 | return false; |
|
280 | return false; | |
281 | } else if (event.which === 73 && that.control_key_active) { |
|
281 | } else if (event.which === 73 && that.control_key_active) { | |
282 | // Interrupt kernel = i |
|
282 | // Interrupt kernel = i | |
283 | that.kernel.interrupt(); |
|
283 | that.kernel.interrupt(); | |
284 | that.control_key_active = false; |
|
284 | that.control_key_active = false; | |
285 | return false; |
|
285 | return false; | |
286 | } else if (event.which === 190 && that.control_key_active) { |
|
286 | } else if (event.which === 190 && that.control_key_active) { | |
287 | // Restart kernel = . # matches qt console |
|
287 | // Restart kernel = . # matches qt console | |
288 | that.restart_kernel(); |
|
288 | that.restart_kernel(); | |
289 | that.control_key_active = false; |
|
289 | that.control_key_active = false; | |
290 | return false; |
|
290 | return false; | |
291 | } else if (event.which === 72 && that.control_key_active) { |
|
291 | } else if (event.which === 72 && that.control_key_active) { | |
292 | // Show keyboard shortcuts = h |
|
292 | // Show keyboard shortcuts = h | |
293 | IPython.quick_help.show_keyboard_shortcuts(); |
|
293 | IPython.quick_help.show_keyboard_shortcuts(); | |
294 | that.control_key_active = false; |
|
294 | that.control_key_active = false; | |
295 | return false; |
|
295 | return false; | |
296 | } else if (event.which === 90 && that.control_key_active) { |
|
296 | } else if (event.which === 90 && that.control_key_active) { | |
297 | // Undo last cell delete = z |
|
297 | // Undo last cell delete = z | |
298 | that.undelete(); |
|
298 | that.undelete(); | |
299 | that.control_key_active = false; |
|
299 | that.control_key_active = false; | |
300 | return false; |
|
300 | return false; | |
301 | } else if (that.control_key_active) { |
|
301 | } else if (that.control_key_active) { | |
302 | that.control_key_active = false; |
|
302 | that.control_key_active = false; | |
303 | return true; |
|
303 | return true; | |
304 | }; |
|
304 | }; | |
305 | return true; |
|
305 | return true; | |
306 | }); |
|
306 | }); | |
307 |
|
307 | |||
308 | var collapse_time = function(time){ |
|
308 | var collapse_time = function(time){ | |
309 | var app_height = $('#ipython-main-app').height(); // content height |
|
309 | var app_height = $('#ipython-main-app').height(); // content height | |
310 | var splitter_height = $('div#pager_splitter').outerHeight(true); |
|
310 | var splitter_height = $('div#pager_splitter').outerHeight(true); | |
311 | var new_height = app_height - splitter_height; |
|
311 | var new_height = app_height - splitter_height; | |
312 | that.element.animate({height : new_height + 'px'}, time); |
|
312 | that.element.animate({height : new_height + 'px'}, time); | |
313 | } |
|
313 | } | |
314 |
|
314 | |||
315 | this.element.bind('collapse_pager', function (event,extrap) { |
|
315 | this.element.bind('collapse_pager', function (event,extrap) { | |
316 | var time = (extrap != undefined) ? ((extrap.duration != undefined ) ? extrap.duration : 'fast') : 'fast'; |
|
316 | var time = (extrap != undefined) ? ((extrap.duration != undefined ) ? extrap.duration : 'fast') : 'fast'; | |
317 | collapse_time(time); |
|
317 | collapse_time(time); | |
318 | }); |
|
318 | }); | |
319 |
|
319 | |||
320 | var expand_time = function(time) { |
|
320 | var expand_time = function(time) { | |
321 | var app_height = $('#ipython-main-app').height(); // content height |
|
321 | var app_height = $('#ipython-main-app').height(); // content height | |
322 | var splitter_height = $('div#pager_splitter').outerHeight(true); |
|
322 | var splitter_height = $('div#pager_splitter').outerHeight(true); | |
323 | var pager_height = $('div#pager').outerHeight(true); |
|
323 | var pager_height = $('div#pager').outerHeight(true); | |
324 | var new_height = app_height - pager_height - splitter_height; |
|
324 | var new_height = app_height - pager_height - splitter_height; | |
325 | that.element.animate({height : new_height + 'px'}, time); |
|
325 | that.element.animate({height : new_height + 'px'}, time); | |
326 | } |
|
326 | } | |
327 |
|
327 | |||
328 | this.element.bind('expand_pager', function (event, extrap) { |
|
328 | this.element.bind('expand_pager', function (event, extrap) { | |
329 | var time = (extrap != undefined) ? ((extrap.duration != undefined ) ? extrap.duration : 'fast') : 'fast'; |
|
329 | var time = (extrap != undefined) ? ((extrap.duration != undefined ) ? extrap.duration : 'fast') : 'fast'; | |
330 | expand_time(time); |
|
330 | expand_time(time); | |
331 | }); |
|
331 | }); | |
332 |
|
332 | |||
333 | $(window).bind('beforeunload', function () { |
|
333 | $(window).bind('beforeunload', function () { | |
334 | // TODO: Make killing the kernel configurable. |
|
334 | // TODO: Make killing the kernel configurable. | |
335 | var kill_kernel = false; |
|
335 | var kill_kernel = false; | |
336 | if (kill_kernel) { |
|
336 | if (kill_kernel) { | |
337 | that.kernel.kill(); |
|
337 | that.kernel.kill(); | |
338 | } |
|
338 | } | |
339 | if (that.dirty && ! that.read_only) { |
|
339 | if (that.dirty && ! that.read_only) { | |
340 | return "You have unsaved changes that will be lost if you leave this page."; |
|
340 | return "You have unsaved changes that will be lost if you leave this page."; | |
341 | }; |
|
341 | }; | |
342 | // Null is the *only* return value that will make the browser not |
|
342 | // Null is the *only* return value that will make the browser not | |
343 | // pop up the "don't leave" dialog. |
|
343 | // pop up the "don't leave" dialog. | |
344 | return null; |
|
344 | return null; | |
345 | }); |
|
345 | }); | |
346 | }; |
|
346 | }; | |
347 |
|
347 | |||
348 | /** |
|
348 | /** | |
349 | * Scroll the top of the page to a given cell. |
|
349 | * Scroll the top of the page to a given cell. | |
350 | * |
|
350 | * | |
351 | * @method scroll_to_cell |
|
351 | * @method scroll_to_cell | |
352 | * @param {Number} cell_number An index of the cell to view |
|
352 | * @param {Number} cell_number An index of the cell to view | |
353 | * @param {Number} time Animation time in milliseconds |
|
353 | * @param {Number} time Animation time in milliseconds | |
354 | * @return {Number} Pixel offset from the top of the container |
|
354 | * @return {Number} Pixel offset from the top of the container | |
355 | */ |
|
355 | */ | |
356 | Notebook.prototype.scroll_to_cell = function (cell_number, time) { |
|
356 | Notebook.prototype.scroll_to_cell = function (cell_number, time) { | |
357 | var cells = this.get_cells(); |
|
357 | var cells = this.get_cells(); | |
358 | var time = time || 0; |
|
358 | var time = time || 0; | |
359 | cell_number = Math.min(cells.length-1,cell_number); |
|
359 | cell_number = Math.min(cells.length-1,cell_number); | |
360 | cell_number = Math.max(0 ,cell_number); |
|
360 | cell_number = Math.max(0 ,cell_number); | |
361 | var scroll_value = cells[cell_number].element.position().top-cells[0].element.position().top ; |
|
361 | var scroll_value = cells[cell_number].element.position().top-cells[0].element.position().top ; | |
362 | this.element.animate({scrollTop:scroll_value}, time); |
|
362 | this.element.animate({scrollTop:scroll_value}, time); | |
363 | return scroll_value; |
|
363 | return scroll_value; | |
364 | }; |
|
364 | }; | |
365 |
|
365 | |||
366 | /** |
|
366 | /** | |
367 | * Scroll to the bottom of the page. |
|
367 | * Scroll to the bottom of the page. | |
368 | * |
|
368 | * | |
369 | * @method scroll_to_bottom |
|
369 | * @method scroll_to_bottom | |
370 | */ |
|
370 | */ | |
371 | Notebook.prototype.scroll_to_bottom = function () { |
|
371 | Notebook.prototype.scroll_to_bottom = function () { | |
372 | this.element.animate({scrollTop:this.element.get(0).scrollHeight}, 0); |
|
372 | this.element.animate({scrollTop:this.element.get(0).scrollHeight}, 0); | |
373 | }; |
|
373 | }; | |
374 |
|
374 | |||
375 | /** |
|
375 | /** | |
376 | * Scroll to the top of the page. |
|
376 | * Scroll to the top of the page. | |
377 | * |
|
377 | * | |
378 | * @method scroll_to_top |
|
378 | * @method scroll_to_top | |
379 | */ |
|
379 | */ | |
380 | Notebook.prototype.scroll_to_top = function () { |
|
380 | Notebook.prototype.scroll_to_top = function () { | |
381 | this.element.animate({scrollTop:0}, 0); |
|
381 | this.element.animate({scrollTop:0}, 0); | |
382 | }; |
|
382 | }; | |
383 |
|
383 | |||
384 |
|
384 | |||
385 | // Cell indexing, retrieval, etc. |
|
385 | // Cell indexing, retrieval, etc. | |
386 |
|
386 | |||
387 | /** |
|
387 | /** | |
388 | * Get all cell elements in the notebook. |
|
388 | * Get all cell elements in the notebook. | |
389 | * |
|
389 | * | |
390 | * @method get_cell_elements |
|
390 | * @method get_cell_elements | |
391 | * @return {jQuery} A selector of all cell elements |
|
391 | * @return {jQuery} A selector of all cell elements | |
392 | */ |
|
392 | */ | |
393 | Notebook.prototype.get_cell_elements = function () { |
|
393 | Notebook.prototype.get_cell_elements = function () { | |
394 | return this.element.children("div.cell"); |
|
394 | return this.element.children("div.cell"); | |
395 | }; |
|
395 | }; | |
396 |
|
396 | |||
397 | /** |
|
397 | /** | |
398 | * Get a particular cell element. |
|
398 | * Get a particular cell element. | |
399 | * |
|
399 | * | |
400 | * @method get_cell_element |
|
400 | * @method get_cell_element | |
401 | * @param {Number} index An index of a cell to select |
|
401 | * @param {Number} index An index of a cell to select | |
402 | * @return {jQuery} A selector of the given cell. |
|
402 | * @return {jQuery} A selector of the given cell. | |
403 | */ |
|
403 | */ | |
404 | Notebook.prototype.get_cell_element = function (index) { |
|
404 | Notebook.prototype.get_cell_element = function (index) { | |
405 | var result = null; |
|
405 | var result = null; | |
406 | var e = this.get_cell_elements().eq(index); |
|
406 | var e = this.get_cell_elements().eq(index); | |
407 | if (e.length !== 0) { |
|
407 | if (e.length !== 0) { | |
408 | result = e; |
|
408 | result = e; | |
409 | } |
|
409 | } | |
410 | return result; |
|
410 | return result; | |
411 | }; |
|
411 | }; | |
412 |
|
412 | |||
413 | /** |
|
413 | /** | |
414 | * Count the cells in this notebook. |
|
414 | * Count the cells in this notebook. | |
415 | * |
|
415 | * | |
416 | * @method ncells |
|
416 | * @method ncells | |
417 | * @return {Number} The number of cells in this notebook |
|
417 | * @return {Number} The number of cells in this notebook | |
418 | */ |
|
418 | */ | |
419 | Notebook.prototype.ncells = function () { |
|
419 | Notebook.prototype.ncells = function () { | |
420 | return this.get_cell_elements().length; |
|
420 | return this.get_cell_elements().length; | |
421 | }; |
|
421 | }; | |
422 |
|
422 | |||
423 | /** |
|
423 | /** | |
424 | * Get all Cell objects in this notebook. |
|
424 | * Get all Cell objects in this notebook. | |
425 | * |
|
425 | * | |
426 | * @method get_cells |
|
426 | * @method get_cells | |
427 | * @return {Array} This notebook's Cell objects |
|
427 | * @return {Array} This notebook's Cell objects | |
428 | */ |
|
428 | */ | |
429 | // TODO: we are often calling cells as cells()[i], which we should optimize |
|
429 | // TODO: we are often calling cells as cells()[i], which we should optimize | |
430 | // to cells(i) or a new method. |
|
430 | // to cells(i) or a new method. | |
431 | Notebook.prototype.get_cells = function () { |
|
431 | Notebook.prototype.get_cells = function () { | |
432 | return this.get_cell_elements().toArray().map(function (e) { |
|
432 | return this.get_cell_elements().toArray().map(function (e) { | |
433 | return $(e).data("cell"); |
|
433 | return $(e).data("cell"); | |
434 | }); |
|
434 | }); | |
435 | }; |
|
435 | }; | |
436 |
|
436 | |||
437 | /** |
|
437 | /** | |
438 | * Get a Cell object from this notebook. |
|
438 | * Get a Cell object from this notebook. | |
439 | * |
|
439 | * | |
440 | * @method get_cell |
|
440 | * @method get_cell | |
441 | * @param {Number} index An index of a cell to retrieve |
|
441 | * @param {Number} index An index of a cell to retrieve | |
442 | * @return {Cell} A particular cell |
|
442 | * @return {Cell} A particular cell | |
443 | */ |
|
443 | */ | |
444 | Notebook.prototype.get_cell = function (index) { |
|
444 | Notebook.prototype.get_cell = function (index) { | |
445 | var result = null; |
|
445 | var result = null; | |
446 | var ce = this.get_cell_element(index); |
|
446 | var ce = this.get_cell_element(index); | |
447 | if (ce !== null) { |
|
447 | if (ce !== null) { | |
448 | result = ce.data('cell'); |
|
448 | result = ce.data('cell'); | |
449 | } |
|
449 | } | |
450 | return result; |
|
450 | return result; | |
451 | } |
|
451 | } | |
452 |
|
452 | |||
453 | /** |
|
453 | /** | |
454 | * Get the cell below a given cell. |
|
454 | * Get the cell below a given cell. | |
455 | * |
|
455 | * | |
456 | * @method get_next_cell |
|
456 | * @method get_next_cell | |
457 | * @param {Cell} cell The provided cell |
|
457 | * @param {Cell} cell The provided cell | |
458 | * @return {Cell} The next cell |
|
458 | * @return {Cell} The next cell | |
459 | */ |
|
459 | */ | |
460 | Notebook.prototype.get_next_cell = function (cell) { |
|
460 | Notebook.prototype.get_next_cell = function (cell) { | |
461 | var result = null; |
|
461 | var result = null; | |
462 | var index = this.find_cell_index(cell); |
|
462 | var index = this.find_cell_index(cell); | |
463 | if (this.is_valid_cell_index(index+1)) { |
|
463 | if (this.is_valid_cell_index(index+1)) { | |
464 | result = this.get_cell(index+1); |
|
464 | result = this.get_cell(index+1); | |
465 | } |
|
465 | } | |
466 | return result; |
|
466 | return result; | |
467 | } |
|
467 | } | |
468 |
|
468 | |||
469 | /** |
|
469 | /** | |
470 | * Get the cell above a given cell. |
|
470 | * Get the cell above a given cell. | |
471 | * |
|
471 | * | |
472 | * @method get_prev_cell |
|
472 | * @method get_prev_cell | |
473 | * @param {Cell} cell The provided cell |
|
473 | * @param {Cell} cell The provided cell | |
474 | * @return {Cell} The previous cell |
|
474 | * @return {Cell} The previous cell | |
475 | */ |
|
475 | */ | |
476 | Notebook.prototype.get_prev_cell = function (cell) { |
|
476 | Notebook.prototype.get_prev_cell = function (cell) { | |
477 | // TODO: off-by-one |
|
477 | // TODO: off-by-one | |
478 | // nb.get_prev_cell(nb.get_cell(1)) is null |
|
478 | // nb.get_prev_cell(nb.get_cell(1)) is null | |
479 | var result = null; |
|
479 | var result = null; | |
480 | var index = this.find_cell_index(cell); |
|
480 | var index = this.find_cell_index(cell); | |
481 | if (index !== null && index > 1) { |
|
481 | if (index !== null && index > 1) { | |
482 | result = this.get_cell(index-1); |
|
482 | result = this.get_cell(index-1); | |
483 | } |
|
483 | } | |
484 | return result; |
|
484 | return result; | |
485 | } |
|
485 | } | |
486 |
|
486 | |||
487 | /** |
|
487 | /** | |
488 | * Get the numeric index of a given cell. |
|
488 | * Get the numeric index of a given cell. | |
489 | * |
|
489 | * | |
490 | * @method find_cell_index |
|
490 | * @method find_cell_index | |
491 | * @param {Cell} cell The provided cell |
|
491 | * @param {Cell} cell The provided cell | |
492 | * @return {Number} The cell's numeric index |
|
492 | * @return {Number} The cell's numeric index | |
493 | */ |
|
493 | */ | |
494 | Notebook.prototype.find_cell_index = function (cell) { |
|
494 | Notebook.prototype.find_cell_index = function (cell) { | |
495 | var result = null; |
|
495 | var result = null; | |
496 | this.get_cell_elements().filter(function (index) { |
|
496 | this.get_cell_elements().filter(function (index) { | |
497 | if ($(this).data("cell") === cell) { |
|
497 | if ($(this).data("cell") === cell) { | |
498 | result = index; |
|
498 | result = index; | |
499 | }; |
|
499 | }; | |
500 | }); |
|
500 | }); | |
501 | return result; |
|
501 | return result; | |
502 | }; |
|
502 | }; | |
503 |
|
503 | |||
504 | /** |
|
504 | /** | |
505 | * Get a given index , or the selected index if none is provided. |
|
505 | * Get a given index , or the selected index if none is provided. | |
506 | * |
|
506 | * | |
507 | * @method index_or_selected |
|
507 | * @method index_or_selected | |
508 | * @param {Number} index A cell's index |
|
508 | * @param {Number} index A cell's index | |
509 | * @return {Number} The given index, or selected index if none is provided. |
|
509 | * @return {Number} The given index, or selected index if none is provided. | |
510 | */ |
|
510 | */ | |
511 | Notebook.prototype.index_or_selected = function (index) { |
|
511 | Notebook.prototype.index_or_selected = function (index) { | |
512 | var i; |
|
512 | var i; | |
513 | if (index === undefined || index === null) { |
|
513 | if (index === undefined || index === null) { | |
514 | i = this.get_selected_index(); |
|
514 | i = this.get_selected_index(); | |
515 | if (i === null) { |
|
515 | if (i === null) { | |
516 | i = 0; |
|
516 | i = 0; | |
517 | } |
|
517 | } | |
518 | } else { |
|
518 | } else { | |
519 | i = index; |
|
519 | i = index; | |
520 | } |
|
520 | } | |
521 | return i; |
|
521 | return i; | |
522 | }; |
|
522 | }; | |
523 |
|
523 | |||
524 | /** |
|
524 | /** | |
525 | * Get the currently selected cell. |
|
525 | * Get the currently selected cell. | |
526 | * @method get_selected_cell |
|
526 | * @method get_selected_cell | |
527 | * @return {Cell} The selected cell |
|
527 | * @return {Cell} The selected cell | |
528 | */ |
|
528 | */ | |
529 | Notebook.prototype.get_selected_cell = function () { |
|
529 | Notebook.prototype.get_selected_cell = function () { | |
530 | var index = this.get_selected_index(); |
|
530 | var index = this.get_selected_index(); | |
531 | return this.get_cell(index); |
|
531 | return this.get_cell(index); | |
532 | }; |
|
532 | }; | |
533 |
|
533 | |||
534 | /** |
|
534 | /** | |
535 | * Check whether a cell index is valid. |
|
535 | * Check whether a cell index is valid. | |
536 | * |
|
536 | * | |
537 | * @method is_valid_cell_index |
|
537 | * @method is_valid_cell_index | |
538 | * @param {Number} index A cell index |
|
538 | * @param {Number} index A cell index | |
539 | * @return True if the index is valid, false otherwise |
|
539 | * @return True if the index is valid, false otherwise | |
540 | */ |
|
540 | */ | |
541 | Notebook.prototype.is_valid_cell_index = function (index) { |
|
541 | Notebook.prototype.is_valid_cell_index = function (index) { | |
542 | if (index !== null && index >= 0 && index < this.ncells()) { |
|
542 | if (index !== null && index >= 0 && index < this.ncells()) { | |
543 | return true; |
|
543 | return true; | |
544 | } else { |
|
544 | } else { | |
545 | return false; |
|
545 | return false; | |
546 | }; |
|
546 | }; | |
547 | } |
|
547 | } | |
548 |
|
548 | |||
549 | /** |
|
549 | /** | |
550 | * Get the index of the currently selected cell. |
|
550 | * Get the index of the currently selected cell. | |
551 |
|
551 | |||
552 | * @method get_selected_index |
|
552 | * @method get_selected_index | |
553 | * @return {Number} The selected cell's numeric index |
|
553 | * @return {Number} The selected cell's numeric index | |
554 | */ |
|
554 | */ | |
555 | Notebook.prototype.get_selected_index = function () { |
|
555 | Notebook.prototype.get_selected_index = function () { | |
556 | var result = null; |
|
556 | var result = null; | |
557 | this.get_cell_elements().filter(function (index) { |
|
557 | this.get_cell_elements().filter(function (index) { | |
558 | if ($(this).data("cell").selected === true) { |
|
558 | if ($(this).data("cell").selected === true) { | |
559 | result = index; |
|
559 | result = index; | |
560 | }; |
|
560 | }; | |
561 | }); |
|
561 | }); | |
562 | return result; |
|
562 | return result; | |
563 | }; |
|
563 | }; | |
564 |
|
564 | |||
565 |
|
565 | |||
566 | // Cell selection. |
|
566 | // Cell selection. | |
567 |
|
567 | |||
568 | /** |
|
568 | /** | |
569 | * Programmatically select a cell. |
|
569 | * Programmatically select a cell. | |
570 | * |
|
570 | * | |
571 | * @method select |
|
571 | * @method select | |
572 | * @param {Number} index A cell's index |
|
572 | * @param {Number} index A cell's index | |
573 | * @return {Notebook} This notebook |
|
573 | * @return {Notebook} This notebook | |
574 | */ |
|
574 | */ | |
575 | Notebook.prototype.select = function (index) { |
|
575 | Notebook.prototype.select = function (index) { | |
576 | if (this.is_valid_cell_index(index)) { |
|
576 | if (this.is_valid_cell_index(index)) { | |
577 | var sindex = this.get_selected_index() |
|
577 | var sindex = this.get_selected_index() | |
578 | if (sindex !== null && index !== sindex) { |
|
578 | if (sindex !== null && index !== sindex) { | |
579 | this.get_cell(sindex).unselect(); |
|
579 | this.get_cell(sindex).unselect(); | |
580 | }; |
|
580 | }; | |
581 | var cell = this.get_cell(index); |
|
581 | var cell = this.get_cell(index); | |
582 | cell.select(); |
|
582 | cell.select(); | |
583 | if (cell.cell_type === 'heading') { |
|
583 | if (cell.cell_type === 'heading') { | |
584 | $([IPython.events]).trigger('selected_cell_type_changed.Notebook', |
|
584 | $([IPython.events]).trigger('selected_cell_type_changed.Notebook', | |
585 | {'cell_type':cell.cell_type,level:cell.level} |
|
585 | {'cell_type':cell.cell_type,level:cell.level} | |
586 | ); |
|
586 | ); | |
587 | } else { |
|
587 | } else { | |
588 | $([IPython.events]).trigger('selected_cell_type_changed.Notebook', |
|
588 | $([IPython.events]).trigger('selected_cell_type_changed.Notebook', | |
589 | {'cell_type':cell.cell_type} |
|
589 | {'cell_type':cell.cell_type} | |
590 | ); |
|
590 | ); | |
591 | }; |
|
591 | }; | |
592 | }; |
|
592 | }; | |
593 | return this; |
|
593 | return this; | |
594 | }; |
|
594 | }; | |
595 |
|
595 | |||
596 | /** |
|
596 | /** | |
597 | * Programmatically select the next cell. |
|
597 | * Programmatically select the next cell. | |
598 | * |
|
598 | * | |
599 | * @method select_next |
|
599 | * @method select_next | |
600 | * @return {Notebook} This notebook |
|
600 | * @return {Notebook} This notebook | |
601 | */ |
|
601 | */ | |
602 | Notebook.prototype.select_next = function () { |
|
602 | Notebook.prototype.select_next = function () { | |
603 | var index = this.get_selected_index(); |
|
603 | var index = this.get_selected_index(); | |
604 | this.select(index+1); |
|
604 | this.select(index+1); | |
605 | return this; |
|
605 | return this; | |
606 | }; |
|
606 | }; | |
607 |
|
607 | |||
608 | /** |
|
608 | /** | |
609 | * Programmatically select the previous cell. |
|
609 | * Programmatically select the previous cell. | |
610 | * |
|
610 | * | |
611 | * @method select_prev |
|
611 | * @method select_prev | |
612 | * @return {Notebook} This notebook |
|
612 | * @return {Notebook} This notebook | |
613 | */ |
|
613 | */ | |
614 | Notebook.prototype.select_prev = function () { |
|
614 | Notebook.prototype.select_prev = function () { | |
615 | var index = this.get_selected_index(); |
|
615 | var index = this.get_selected_index(); | |
616 | this.select(index-1); |
|
616 | this.select(index-1); | |
617 | return this; |
|
617 | return this; | |
618 | }; |
|
618 | }; | |
619 |
|
619 | |||
620 |
|
620 | |||
621 | // Cell movement |
|
621 | // Cell movement | |
622 |
|
622 | |||
623 | /** |
|
623 | /** | |
624 | * Move given (or selected) cell up and select it. |
|
624 | * Move given (or selected) cell up and select it. | |
625 | * |
|
625 | * | |
626 | * @method move_cell_up |
|
626 | * @method move_cell_up | |
627 | * @param [index] {integer} cell index |
|
627 | * @param [index] {integer} cell index | |
628 | * @return {Notebook} This notebook |
|
628 | * @return {Notebook} This notebook | |
629 | **/ |
|
629 | **/ | |
630 | Notebook.prototype.move_cell_up = function (index) { |
|
630 | Notebook.prototype.move_cell_up = function (index) { | |
631 | var i = this.index_or_selected(index); |
|
631 | var i = this.index_or_selected(index); | |
632 | if (this.is_valid_cell_index(i) && i > 0) { |
|
632 | if (this.is_valid_cell_index(i) && i > 0) { | |
633 | var pivot = this.get_cell_element(i-1); |
|
633 | var pivot = this.get_cell_element(i-1); | |
634 | var tomove = this.get_cell_element(i); |
|
634 | var tomove = this.get_cell_element(i); | |
635 | if (pivot !== null && tomove !== null) { |
|
635 | if (pivot !== null && tomove !== null) { | |
636 | tomove.detach(); |
|
636 | tomove.detach(); | |
637 | pivot.before(tomove); |
|
637 | pivot.before(tomove); | |
638 | this.select(i-1); |
|
638 | this.select(i-1); | |
639 | }; |
|
639 | }; | |
640 | this.dirty = true; |
|
640 | this.dirty = true; | |
641 | }; |
|
641 | }; | |
642 | return this; |
|
642 | return this; | |
643 | }; |
|
643 | }; | |
644 |
|
644 | |||
645 |
|
645 | |||
646 | /** |
|
646 | /** | |
647 | * Move given (or selected) cell down and select it |
|
647 | * Move given (or selected) cell down and select it | |
648 | * |
|
648 | * | |
649 | * @method move_cell_down |
|
649 | * @method move_cell_down | |
650 | * @param [index] {integer} cell index |
|
650 | * @param [index] {integer} cell index | |
651 | * @return {Notebook} This notebook |
|
651 | * @return {Notebook} This notebook | |
652 | **/ |
|
652 | **/ | |
653 | Notebook.prototype.move_cell_down = function (index) { |
|
653 | Notebook.prototype.move_cell_down = function (index) { | |
654 | var i = this.index_or_selected(index); |
|
654 | var i = this.index_or_selected(index); | |
655 | if ( this.is_valid_cell_index(i) && this.is_valid_cell_index(i+1)) { |
|
655 | if ( this.is_valid_cell_index(i) && this.is_valid_cell_index(i+1)) { | |
656 | var pivot = this.get_cell_element(i+1); |
|
656 | var pivot = this.get_cell_element(i+1); | |
657 | var tomove = this.get_cell_element(i); |
|
657 | var tomove = this.get_cell_element(i); | |
658 | if (pivot !== null && tomove !== null) { |
|
658 | if (pivot !== null && tomove !== null) { | |
659 | tomove.detach(); |
|
659 | tomove.detach(); | |
660 | pivot.after(tomove); |
|
660 | pivot.after(tomove); | |
661 | this.select(i+1); |
|
661 | this.select(i+1); | |
662 | }; |
|
662 | }; | |
663 | }; |
|
663 | }; | |
664 | this.dirty = true; |
|
664 | this.dirty = true; | |
665 | return this; |
|
665 | return this; | |
666 | }; |
|
666 | }; | |
667 |
|
667 | |||
668 |
|
668 | |||
669 | // Insertion, deletion. |
|
669 | // Insertion, deletion. | |
670 |
|
670 | |||
671 | /** |
|
671 | /** | |
672 | * Delete a cell from the notebook. |
|
672 | * Delete a cell from the notebook. | |
673 | * |
|
673 | * | |
674 | * @method delete_cell |
|
674 | * @method delete_cell | |
675 | * @param [index] A cell's numeric index |
|
675 | * @param [index] A cell's numeric index | |
676 | * @return {Notebook} This notebook |
|
676 | * @return {Notebook} This notebook | |
677 | */ |
|
677 | */ | |
678 | Notebook.prototype.delete_cell = function (index) { |
|
678 | Notebook.prototype.delete_cell = function (index) { | |
679 | var i = this.index_or_selected(index); |
|
679 | var i = this.index_or_selected(index); | |
680 | var cell = this.get_selected_cell(); |
|
680 | var cell = this.get_selected_cell(); | |
681 | this.undelete_backup = cell.toJSON(); |
|
681 | this.undelete_backup = cell.toJSON(); | |
682 | $('#undelete_cell').removeClass('ui-state-disabled'); |
|
682 | $('#undelete_cell').removeClass('ui-state-disabled'); | |
683 | if (this.is_valid_cell_index(i)) { |
|
683 | if (this.is_valid_cell_index(i)) { | |
684 | var ce = this.get_cell_element(i); |
|
684 | var ce = this.get_cell_element(i); | |
685 | ce.remove(); |
|
685 | ce.remove(); | |
686 | if (i === (this.ncells())) { |
|
686 | if (i === (this.ncells())) { | |
687 | this.select(i-1); |
|
687 | this.select(i-1); | |
688 | this.undelete_index = i - 1; |
|
688 | this.undelete_index = i - 1; | |
689 | this.undelete_below = true; |
|
689 | this.undelete_below = true; | |
690 | } else { |
|
690 | } else { | |
691 | this.select(i); |
|
691 | this.select(i); | |
692 | this.undelete_index = i; |
|
692 | this.undelete_index = i; | |
693 | this.undelete_below = false; |
|
693 | this.undelete_below = false; | |
694 | }; |
|
694 | }; | |
695 | this.dirty = true; |
|
695 | this.dirty = true; | |
696 | }; |
|
696 | }; | |
697 | return this; |
|
697 | return this; | |
698 | }; |
|
698 | }; | |
699 |
|
699 | |||
700 | /** |
|
700 | /** | |
701 | * Insert a cell so that after insertion the cell is at given index. |
|
701 | * Insert a cell so that after insertion the cell is at given index. | |
702 | * |
|
702 | * | |
703 | * Similar to insert_above, but index parameter is mandatory |
|
703 | * Similar to insert_above, but index parameter is mandatory | |
704 | * |
|
704 | * | |
705 | * Index will be brought back into the accissible range [0,n] |
|
705 | * Index will be brought back into the accissible range [0,n] | |
706 | * |
|
706 | * | |
707 | * @method insert_cell_at_index |
|
707 | * @method insert_cell_at_index | |
708 |
* @param type {string} in ['code',' |
|
708 | * @param type {string} in ['code','markdown','heading'] | |
709 | * @param [index] {int} a valid index where to inser cell |
|
709 | * @param [index] {int} a valid index where to inser cell | |
710 | * |
|
710 | * | |
711 | * @return cell {cell|null} created cell or null |
|
711 | * @return cell {cell|null} created cell or null | |
712 | **/ |
|
712 | **/ | |
713 | Notebook.prototype.insert_cell_at_index = function(type, index){ |
|
713 | Notebook.prototype.insert_cell_at_index = function(type, index){ | |
714 |
|
714 | |||
715 | var ncells = this.ncells(); |
|
715 | var ncells = this.ncells(); | |
716 | var index = Math.min(index,ncells); |
|
716 | var index = Math.min(index,ncells); | |
717 | index = Math.max(index,0); |
|
717 | index = Math.max(index,0); | |
718 | var cell = null; |
|
718 | var cell = null; | |
719 |
|
719 | |||
720 | if (ncells === 0 || this.is_valid_cell_index(index) || index === ncells) { |
|
720 | if (ncells === 0 || this.is_valid_cell_index(index) || index === ncells) { | |
721 | if (type === 'code') { |
|
721 | if (type === 'code') { | |
722 | cell = new IPython.CodeCell(this.kernel); |
|
722 | cell = new IPython.CodeCell(this.kernel); | |
723 | cell.set_input_prompt(); |
|
723 | cell.set_input_prompt(); | |
724 | } else if (type === 'markdown') { |
|
724 | } else if (type === 'markdown') { | |
725 | cell = new IPython.MarkdownCell(); |
|
725 | cell = new IPython.MarkdownCell(); | |
726 | } else if (type === 'html') { |
|
|||
727 | cell = new IPython.HTMLCell(); |
|
|||
728 | } else if (type === 'raw') { |
|
726 | } else if (type === 'raw') { | |
729 | cell = new IPython.RawCell(); |
|
727 | cell = new IPython.RawCell(); | |
730 | } else if (type === 'heading') { |
|
728 | } else if (type === 'heading') { | |
731 | cell = new IPython.HeadingCell(); |
|
729 | cell = new IPython.HeadingCell(); | |
732 | } |
|
730 | } | |
733 |
|
731 | |||
734 | if(this._insert_element_at_index(cell.element,index)){ |
|
732 | if(this._insert_element_at_index(cell.element,index)){ | |
735 | cell.render(); |
|
733 | cell.render(); | |
736 | this.select(this.find_cell_index(cell)); |
|
734 | this.select(this.find_cell_index(cell)); | |
737 | this.dirty = true; |
|
735 | this.dirty = true; | |
738 | } |
|
736 | } | |
739 | } |
|
737 | } | |
740 | return cell; |
|
738 | return cell; | |
741 |
|
739 | |||
742 | }; |
|
740 | }; | |
743 |
|
741 | |||
744 | /** |
|
742 | /** | |
745 | * Insert an element at given cell index. |
|
743 | * Insert an element at given cell index. | |
746 | * |
|
744 | * | |
747 | * @method _insert_element_at_index |
|
745 | * @method _insert_element_at_index | |
748 | * @param element {dom element} a cell element |
|
746 | * @param element {dom element} a cell element | |
749 | * @param [index] {int} a valid index where to inser cell |
|
747 | * @param [index] {int} a valid index where to inser cell | |
750 | * @private |
|
748 | * @private | |
751 | * |
|
749 | * | |
752 | * return true if everything whent fine. |
|
750 | * return true if everything whent fine. | |
753 | **/ |
|
751 | **/ | |
754 | Notebook.prototype._insert_element_at_index = function(element, index){ |
|
752 | Notebook.prototype._insert_element_at_index = function(element, index){ | |
755 | if (element === undefined){ |
|
753 | if (element === undefined){ | |
756 | return false; |
|
754 | return false; | |
757 | } |
|
755 | } | |
758 |
|
756 | |||
759 | var ncells = this.ncells(); |
|
757 | var ncells = this.ncells(); | |
760 |
|
758 | |||
761 | if (ncells === 0) { |
|
759 | if (ncells === 0) { | |
762 | // special case append if empty |
|
760 | // special case append if empty | |
763 | this.element.find('div.end_space').before(element); |
|
761 | this.element.find('div.end_space').before(element); | |
764 | } else if ( ncells === index ) { |
|
762 | } else if ( ncells === index ) { | |
765 | // special case append it the end, but not empty |
|
763 | // special case append it the end, but not empty | |
766 | this.get_cell_element(index-1).after(element); |
|
764 | this.get_cell_element(index-1).after(element); | |
767 | } else if (this.is_valid_cell_index(index)) { |
|
765 | } else if (this.is_valid_cell_index(index)) { | |
768 | // otherwise always somewhere to append to |
|
766 | // otherwise always somewhere to append to | |
769 | this.get_cell_element(index).before(element); |
|
767 | this.get_cell_element(index).before(element); | |
770 | } else { |
|
768 | } else { | |
771 | return false; |
|
769 | return false; | |
772 | } |
|
770 | } | |
773 |
|
771 | |||
774 | if (this.undelete_index !== null && index <= this.undelete_index) { |
|
772 | if (this.undelete_index !== null && index <= this.undelete_index) { | |
775 | this.undelete_index = this.undelete_index + 1; |
|
773 | this.undelete_index = this.undelete_index + 1; | |
776 | this.dirty = true; |
|
774 | this.dirty = true; | |
777 | } |
|
775 | } | |
778 | return true; |
|
776 | return true; | |
779 | }; |
|
777 | }; | |
780 |
|
778 | |||
781 | /** |
|
779 | /** | |
782 | * Insert a cell of given type above given index, or at top |
|
780 | * Insert a cell of given type above given index, or at top | |
783 | * of notebook if index smaller than 0. |
|
781 | * of notebook if index smaller than 0. | |
784 | * |
|
782 | * | |
785 | * default index value is the one of currently selected cell |
|
783 | * default index value is the one of currently selected cell | |
786 | * |
|
784 | * | |
787 | * @method insert_cell_above |
|
785 | * @method insert_cell_above | |
788 | * @param type {string} cell type |
|
786 | * @param type {string} cell type | |
789 | * @param [index] {integer} |
|
787 | * @param [index] {integer} | |
790 | * |
|
788 | * | |
791 | * @return handle to created cell or null |
|
789 | * @return handle to created cell or null | |
792 | **/ |
|
790 | **/ | |
793 | Notebook.prototype.insert_cell_above = function (type, index) { |
|
791 | Notebook.prototype.insert_cell_above = function (type, index) { | |
794 | index = this.index_or_selected(index); |
|
792 | index = this.index_or_selected(index); | |
795 | return this.insert_cell_at_index(type, index); |
|
793 | return this.insert_cell_at_index(type, index); | |
796 | }; |
|
794 | }; | |
797 |
|
795 | |||
798 | /** |
|
796 | /** | |
799 | * Insert a cell of given type below given index, or at bottom |
|
797 | * Insert a cell of given type below given index, or at bottom | |
800 | * of notebook if index greater thatn number of cell |
|
798 | * of notebook if index greater thatn number of cell | |
801 | * |
|
799 | * | |
802 | * default index value is the one of currently selected cell |
|
800 | * default index value is the one of currently selected cell | |
803 | * |
|
801 | * | |
804 | * @method insert_cell_below |
|
802 | * @method insert_cell_below | |
805 | * @param type {string} cell type |
|
803 | * @param type {string} cell type | |
806 | * @param [index] {integer} |
|
804 | * @param [index] {integer} | |
807 | * |
|
805 | * | |
808 | * @return handle to created cell or null |
|
806 | * @return handle to created cell or null | |
809 | * |
|
807 | * | |
810 | **/ |
|
808 | **/ | |
811 | Notebook.prototype.insert_cell_below = function (type, index) { |
|
809 | Notebook.prototype.insert_cell_below = function (type, index) { | |
812 | index = this.index_or_selected(index); |
|
810 | index = this.index_or_selected(index); | |
813 | return this.insert_cell_at_index(type, index+1); |
|
811 | return this.insert_cell_at_index(type, index+1); | |
814 | }; |
|
812 | }; | |
815 |
|
813 | |||
816 |
|
814 | |||
817 | /** |
|
815 | /** | |
818 | * Insert cell at end of notebook |
|
816 | * Insert cell at end of notebook | |
819 | * |
|
817 | * | |
820 | * @method insert_cell_at_bottom |
|
818 | * @method insert_cell_at_bottom | |
821 | * @param {String} type cell type |
|
819 | * @param {String} type cell type | |
822 | * |
|
820 | * | |
823 | * @return the added cell; or null |
|
821 | * @return the added cell; or null | |
824 | **/ |
|
822 | **/ | |
825 | Notebook.prototype.insert_cell_at_bottom = function (type){ |
|
823 | Notebook.prototype.insert_cell_at_bottom = function (type){ | |
826 | var len = this.ncells(); |
|
824 | var len = this.ncells(); | |
827 | return this.insert_cell_below(type,len-1); |
|
825 | return this.insert_cell_below(type,len-1); | |
828 | }; |
|
826 | }; | |
829 |
|
827 | |||
830 | /** |
|
828 | /** | |
831 | * Turn a cell into a code cell. |
|
829 | * Turn a cell into a code cell. | |
832 | * |
|
830 | * | |
833 | * @method to_code |
|
831 | * @method to_code | |
834 | * @param {Number} [index] A cell's index |
|
832 | * @param {Number} [index] A cell's index | |
835 | */ |
|
833 | */ | |
836 | Notebook.prototype.to_code = function (index) { |
|
834 | Notebook.prototype.to_code = function (index) { | |
837 | var i = this.index_or_selected(index); |
|
835 | var i = this.index_or_selected(index); | |
838 | if (this.is_valid_cell_index(i)) { |
|
836 | if (this.is_valid_cell_index(i)) { | |
839 | var source_element = this.get_cell_element(i); |
|
837 | var source_element = this.get_cell_element(i); | |
840 | var source_cell = source_element.data("cell"); |
|
838 | var source_cell = source_element.data("cell"); | |
841 | if (!(source_cell instanceof IPython.CodeCell)) { |
|
839 | if (!(source_cell instanceof IPython.CodeCell)) { | |
842 | var target_cell = this.insert_cell_below('code',i); |
|
840 | var target_cell = this.insert_cell_below('code',i); | |
843 | var text = source_cell.get_text(); |
|
841 | var text = source_cell.get_text(); | |
844 | if (text === source_cell.placeholder) { |
|
842 | if (text === source_cell.placeholder) { | |
845 | text = ''; |
|
843 | text = ''; | |
846 | } |
|
844 | } | |
847 | target_cell.set_text(text); |
|
845 | target_cell.set_text(text); | |
848 | // make this value the starting point, so that we can only undo |
|
846 | // make this value the starting point, so that we can only undo | |
849 | // to this state, instead of a blank cell |
|
847 | // to this state, instead of a blank cell | |
850 | target_cell.code_mirror.clearHistory(); |
|
848 | target_cell.code_mirror.clearHistory(); | |
851 | source_element.remove(); |
|
849 | source_element.remove(); | |
852 | this.dirty = true; |
|
850 | this.dirty = true; | |
853 | }; |
|
851 | }; | |
854 | }; |
|
852 | }; | |
855 | }; |
|
853 | }; | |
856 |
|
854 | |||
857 | /** |
|
855 | /** | |
858 | * Turn a cell into a Markdown cell. |
|
856 | * Turn a cell into a Markdown cell. | |
859 | * |
|
857 | * | |
860 | * @method to_markdown |
|
858 | * @method to_markdown | |
861 | * @param {Number} [index] A cell's index |
|
859 | * @param {Number} [index] A cell's index | |
862 | */ |
|
860 | */ | |
863 | Notebook.prototype.to_markdown = function (index) { |
|
861 | Notebook.prototype.to_markdown = function (index) { | |
864 | var i = this.index_or_selected(index); |
|
862 | var i = this.index_or_selected(index); | |
865 | if (this.is_valid_cell_index(i)) { |
|
863 | if (this.is_valid_cell_index(i)) { | |
866 | var source_element = this.get_cell_element(i); |
|
864 | var source_element = this.get_cell_element(i); | |
867 | var source_cell = source_element.data("cell"); |
|
865 | var source_cell = source_element.data("cell"); | |
868 | if (!(source_cell instanceof IPython.MarkdownCell)) { |
|
866 | if (!(source_cell instanceof IPython.MarkdownCell)) { | |
869 | var target_cell = this.insert_cell_below('markdown',i); |
|
867 | var target_cell = this.insert_cell_below('markdown',i); | |
870 | var text = source_cell.get_text(); |
|
868 | var text = source_cell.get_text(); | |
871 | if (text === source_cell.placeholder) { |
|
869 | if (text === source_cell.placeholder) { | |
872 | text = ''; |
|
870 | text = ''; | |
873 | }; |
|
871 | }; | |
874 | // The edit must come before the set_text. |
|
872 | // The edit must come before the set_text. | |
875 | target_cell.edit(); |
|
873 | target_cell.edit(); | |
876 | target_cell.set_text(text); |
|
874 | target_cell.set_text(text); | |
877 | // make this value the starting point, so that we can only undo |
|
875 | // make this value the starting point, so that we can only undo | |
878 | // to this state, instead of a blank cell |
|
876 | // to this state, instead of a blank cell | |
879 | target_cell.code_mirror.clearHistory(); |
|
877 | target_cell.code_mirror.clearHistory(); | |
880 | source_element.remove(); |
|
878 | source_element.remove(); | |
881 | this.dirty = true; |
|
879 | this.dirty = true; | |
882 | }; |
|
880 | }; | |
883 | }; |
|
881 | }; | |
884 | }; |
|
882 | }; | |
885 |
|
883 | |||
886 | /** |
|
884 | /** | |
887 | * Turn a cell into an HTML cell. |
|
|||
888 | * |
|
|||
889 | * @method to_html |
|
|||
890 | * @param {Number} [index] A cell's index |
|
|||
891 | */ |
|
|||
892 | Notebook.prototype.to_html = function (index) { |
|
|||
893 | // TODO: remove? This is never called |
|
|||
894 | var i = this.index_or_selected(index); |
|
|||
895 | if (this.is_valid_cell_index(i)) { |
|
|||
896 | var source_element = this.get_cell_element(i); |
|
|||
897 | var source_cell = source_element.data("cell"); |
|
|||
898 | var target_cell = null; |
|
|||
899 | if (!(source_cell instanceof IPython.HTMLCell)) { |
|
|||
900 | target_cell = this.insert_cell_below('html',i); |
|
|||
901 | var text = source_cell.get_text(); |
|
|||
902 | if (text === source_cell.placeholder) { |
|
|||
903 | text = ''; |
|
|||
904 | }; |
|
|||
905 | // The edit must come before the set_text. |
|
|||
906 | target_cell.edit(); |
|
|||
907 | target_cell.set_text(text); |
|
|||
908 | // make this value the starting point, so that we can only undo |
|
|||
909 | // to this state, instead of a blank cell |
|
|||
910 | target_cell.code_mirror.clearHistory(); |
|
|||
911 | source_element.remove(); |
|
|||
912 | this.dirty = true; |
|
|||
913 | }; |
|
|||
914 | }; |
|
|||
915 | }; |
|
|||
916 |
|
||||
917 | /** |
|
|||
918 | * Turn a cell into a raw text cell. |
|
885 | * Turn a cell into a raw text cell. | |
919 | * |
|
886 | * | |
920 | * @method to_raw |
|
887 | * @method to_raw | |
921 | * @param {Number} [index] A cell's index |
|
888 | * @param {Number} [index] A cell's index | |
922 | */ |
|
889 | */ | |
923 | Notebook.prototype.to_raw = function (index) { |
|
890 | Notebook.prototype.to_raw = function (index) { | |
924 | var i = this.index_or_selected(index); |
|
891 | var i = this.index_or_selected(index); | |
925 | if (this.is_valid_cell_index(i)) { |
|
892 | if (this.is_valid_cell_index(i)) { | |
926 | var source_element = this.get_cell_element(i); |
|
893 | var source_element = this.get_cell_element(i); | |
927 | var source_cell = source_element.data("cell"); |
|
894 | var source_cell = source_element.data("cell"); | |
928 | var target_cell = null; |
|
895 | var target_cell = null; | |
929 | if (!(source_cell instanceof IPython.RawCell)) { |
|
896 | if (!(source_cell instanceof IPython.RawCell)) { | |
930 | target_cell = this.insert_cell_below('raw',i); |
|
897 | target_cell = this.insert_cell_below('raw',i); | |
931 | var text = source_cell.get_text(); |
|
898 | var text = source_cell.get_text(); | |
932 | if (text === source_cell.placeholder) { |
|
899 | if (text === source_cell.placeholder) { | |
933 | text = ''; |
|
900 | text = ''; | |
934 | }; |
|
901 | }; | |
935 | // The edit must come before the set_text. |
|
902 | // The edit must come before the set_text. | |
936 | target_cell.edit(); |
|
903 | target_cell.edit(); | |
937 | target_cell.set_text(text); |
|
904 | target_cell.set_text(text); | |
938 | // make this value the starting point, so that we can only undo |
|
905 | // make this value the starting point, so that we can only undo | |
939 | // to this state, instead of a blank cell |
|
906 | // to this state, instead of a blank cell | |
940 | target_cell.code_mirror.clearHistory(); |
|
907 | target_cell.code_mirror.clearHistory(); | |
941 | source_element.remove(); |
|
908 | source_element.remove(); | |
942 | this.dirty = true; |
|
909 | this.dirty = true; | |
943 | }; |
|
910 | }; | |
944 | }; |
|
911 | }; | |
945 | }; |
|
912 | }; | |
946 |
|
913 | |||
947 | /** |
|
914 | /** | |
948 | * Turn a cell into a heading cell. |
|
915 | * Turn a cell into a heading cell. | |
949 | * |
|
916 | * | |
950 | * @method to_heading |
|
917 | * @method to_heading | |
951 | * @param {Number} [index] A cell's index |
|
918 | * @param {Number} [index] A cell's index | |
952 | * @param {Number} [level] A heading level (e.g., 1 becomes <h1>) |
|
919 | * @param {Number} [level] A heading level (e.g., 1 becomes <h1>) | |
953 | */ |
|
920 | */ | |
954 | Notebook.prototype.to_heading = function (index, level) { |
|
921 | Notebook.prototype.to_heading = function (index, level) { | |
955 | level = level || 1; |
|
922 | level = level || 1; | |
956 | var i = this.index_or_selected(index); |
|
923 | var i = this.index_or_selected(index); | |
957 | if (this.is_valid_cell_index(i)) { |
|
924 | if (this.is_valid_cell_index(i)) { | |
958 | var source_element = this.get_cell_element(i); |
|
925 | var source_element = this.get_cell_element(i); | |
959 | var source_cell = source_element.data("cell"); |
|
926 | var source_cell = source_element.data("cell"); | |
960 | var target_cell = null; |
|
927 | var target_cell = null; | |
961 | if (source_cell instanceof IPython.HeadingCell) { |
|
928 | if (source_cell instanceof IPython.HeadingCell) { | |
962 | source_cell.set_level(level); |
|
929 | source_cell.set_level(level); | |
963 | } else { |
|
930 | } else { | |
964 | target_cell = this.insert_cell_below('heading',i); |
|
931 | target_cell = this.insert_cell_below('heading',i); | |
965 | var text = source_cell.get_text(); |
|
932 | var text = source_cell.get_text(); | |
966 | if (text === source_cell.placeholder) { |
|
933 | if (text === source_cell.placeholder) { | |
967 | text = ''; |
|
934 | text = ''; | |
968 | }; |
|
935 | }; | |
969 | // The edit must come before the set_text. |
|
936 | // The edit must come before the set_text. | |
970 | target_cell.set_level(level); |
|
937 | target_cell.set_level(level); | |
971 | target_cell.edit(); |
|
938 | target_cell.edit(); | |
972 | target_cell.set_text(text); |
|
939 | target_cell.set_text(text); | |
973 | // make this value the starting point, so that we can only undo |
|
940 | // make this value the starting point, so that we can only undo | |
974 | // to this state, instead of a blank cell |
|
941 | // to this state, instead of a blank cell | |
975 | target_cell.code_mirror.clearHistory(); |
|
942 | target_cell.code_mirror.clearHistory(); | |
976 | source_element.remove(); |
|
943 | source_element.remove(); | |
977 | this.dirty = true; |
|
944 | this.dirty = true; | |
978 | }; |
|
945 | }; | |
979 | $([IPython.events]).trigger('selected_cell_type_changed.Notebook', |
|
946 | $([IPython.events]).trigger('selected_cell_type_changed.Notebook', | |
980 | {'cell_type':'heading',level:level} |
|
947 | {'cell_type':'heading',level:level} | |
981 | ); |
|
948 | ); | |
982 | }; |
|
949 | }; | |
983 | }; |
|
950 | }; | |
984 |
|
951 | |||
985 |
|
952 | |||
986 | // Cut/Copy/Paste |
|
953 | // Cut/Copy/Paste | |
987 |
|
954 | |||
988 | /** |
|
955 | /** | |
989 | * Enable UI elements for pasting cells. |
|
956 | * Enable UI elements for pasting cells. | |
990 | * |
|
957 | * | |
991 | * @method enable_paste |
|
958 | * @method enable_paste | |
992 | */ |
|
959 | */ | |
993 | Notebook.prototype.enable_paste = function () { |
|
960 | Notebook.prototype.enable_paste = function () { | |
994 | var that = this; |
|
961 | var that = this; | |
995 | if (!this.paste_enabled) { |
|
962 | if (!this.paste_enabled) { | |
996 | $('#paste_cell_replace').removeClass('ui-state-disabled') |
|
963 | $('#paste_cell_replace').removeClass('ui-state-disabled') | |
997 | .on('click', function () {that.paste_cell_replace();}); |
|
964 | .on('click', function () {that.paste_cell_replace();}); | |
998 | $('#paste_cell_above').removeClass('ui-state-disabled') |
|
965 | $('#paste_cell_above').removeClass('ui-state-disabled') | |
999 | .on('click', function () {that.paste_cell_above();}); |
|
966 | .on('click', function () {that.paste_cell_above();}); | |
1000 | $('#paste_cell_below').removeClass('ui-state-disabled') |
|
967 | $('#paste_cell_below').removeClass('ui-state-disabled') | |
1001 | .on('click', function () {that.paste_cell_below();}); |
|
968 | .on('click', function () {that.paste_cell_below();}); | |
1002 | this.paste_enabled = true; |
|
969 | this.paste_enabled = true; | |
1003 | }; |
|
970 | }; | |
1004 | }; |
|
971 | }; | |
1005 |
|
972 | |||
1006 | /** |
|
973 | /** | |
1007 | * Disable UI elements for pasting cells. |
|
974 | * Disable UI elements for pasting cells. | |
1008 | * |
|
975 | * | |
1009 | * @method disable_paste |
|
976 | * @method disable_paste | |
1010 | */ |
|
977 | */ | |
1011 | Notebook.prototype.disable_paste = function () { |
|
978 | Notebook.prototype.disable_paste = function () { | |
1012 | if (this.paste_enabled) { |
|
979 | if (this.paste_enabled) { | |
1013 | $('#paste_cell_replace').addClass('ui-state-disabled').off('click'); |
|
980 | $('#paste_cell_replace').addClass('ui-state-disabled').off('click'); | |
1014 | $('#paste_cell_above').addClass('ui-state-disabled').off('click'); |
|
981 | $('#paste_cell_above').addClass('ui-state-disabled').off('click'); | |
1015 | $('#paste_cell_below').addClass('ui-state-disabled').off('click'); |
|
982 | $('#paste_cell_below').addClass('ui-state-disabled').off('click'); | |
1016 | this.paste_enabled = false; |
|
983 | this.paste_enabled = false; | |
1017 | }; |
|
984 | }; | |
1018 | }; |
|
985 | }; | |
1019 |
|
986 | |||
1020 | /** |
|
987 | /** | |
1021 | * Cut a cell. |
|
988 | * Cut a cell. | |
1022 | * |
|
989 | * | |
1023 | * @method cut_cell |
|
990 | * @method cut_cell | |
1024 | */ |
|
991 | */ | |
1025 | Notebook.prototype.cut_cell = function () { |
|
992 | Notebook.prototype.cut_cell = function () { | |
1026 | this.copy_cell(); |
|
993 | this.copy_cell(); | |
1027 | this.delete_cell(); |
|
994 | this.delete_cell(); | |
1028 | } |
|
995 | } | |
1029 |
|
996 | |||
1030 | /** |
|
997 | /** | |
1031 | * Copy a cell. |
|
998 | * Copy a cell. | |
1032 | * |
|
999 | * | |
1033 | * @method copy_cell |
|
1000 | * @method copy_cell | |
1034 | */ |
|
1001 | */ | |
1035 | Notebook.prototype.copy_cell = function () { |
|
1002 | Notebook.prototype.copy_cell = function () { | |
1036 | var cell = this.get_selected_cell(); |
|
1003 | var cell = this.get_selected_cell(); | |
1037 | this.clipboard = cell.toJSON(); |
|
1004 | this.clipboard = cell.toJSON(); | |
1038 | this.enable_paste(); |
|
1005 | this.enable_paste(); | |
1039 | }; |
|
1006 | }; | |
1040 |
|
1007 | |||
1041 | /** |
|
1008 | /** | |
1042 | * Replace the selected cell with a cell in the clipboard. |
|
1009 | * Replace the selected cell with a cell in the clipboard. | |
1043 | * |
|
1010 | * | |
1044 | * @method paste_cell_replace |
|
1011 | * @method paste_cell_replace | |
1045 | */ |
|
1012 | */ | |
1046 | Notebook.prototype.paste_cell_replace = function () { |
|
1013 | Notebook.prototype.paste_cell_replace = function () { | |
1047 | if (this.clipboard !== null && this.paste_enabled) { |
|
1014 | if (this.clipboard !== null && this.paste_enabled) { | |
1048 | var cell_data = this.clipboard; |
|
1015 | var cell_data = this.clipboard; | |
1049 | var new_cell = this.insert_cell_above(cell_data.cell_type); |
|
1016 | var new_cell = this.insert_cell_above(cell_data.cell_type); | |
1050 | new_cell.fromJSON(cell_data); |
|
1017 | new_cell.fromJSON(cell_data); | |
1051 | var old_cell = this.get_next_cell(new_cell); |
|
1018 | var old_cell = this.get_next_cell(new_cell); | |
1052 | this.delete_cell(this.find_cell_index(old_cell)); |
|
1019 | this.delete_cell(this.find_cell_index(old_cell)); | |
1053 | this.select(this.find_cell_index(new_cell)); |
|
1020 | this.select(this.find_cell_index(new_cell)); | |
1054 | }; |
|
1021 | }; | |
1055 | }; |
|
1022 | }; | |
1056 |
|
1023 | |||
1057 | /** |
|
1024 | /** | |
1058 | * Paste a cell from the clipboard above the selected cell. |
|
1025 | * Paste a cell from the clipboard above the selected cell. | |
1059 | * |
|
1026 | * | |
1060 | * @method paste_cell_above |
|
1027 | * @method paste_cell_above | |
1061 | */ |
|
1028 | */ | |
1062 | Notebook.prototype.paste_cell_above = function () { |
|
1029 | Notebook.prototype.paste_cell_above = function () { | |
1063 | if (this.clipboard !== null && this.paste_enabled) { |
|
1030 | if (this.clipboard !== null && this.paste_enabled) { | |
1064 | var cell_data = this.clipboard; |
|
1031 | var cell_data = this.clipboard; | |
1065 | var new_cell = this.insert_cell_above(cell_data.cell_type); |
|
1032 | var new_cell = this.insert_cell_above(cell_data.cell_type); | |
1066 | new_cell.fromJSON(cell_data); |
|
1033 | new_cell.fromJSON(cell_data); | |
1067 | }; |
|
1034 | }; | |
1068 | }; |
|
1035 | }; | |
1069 |
|
1036 | |||
1070 | /** |
|
1037 | /** | |
1071 | * Paste a cell from the clipboard below the selected cell. |
|
1038 | * Paste a cell from the clipboard below the selected cell. | |
1072 | * |
|
1039 | * | |
1073 | * @method paste_cell_below |
|
1040 | * @method paste_cell_below | |
1074 | */ |
|
1041 | */ | |
1075 | Notebook.prototype.paste_cell_below = function () { |
|
1042 | Notebook.prototype.paste_cell_below = function () { | |
1076 | if (this.clipboard !== null && this.paste_enabled) { |
|
1043 | if (this.clipboard !== null && this.paste_enabled) { | |
1077 | var cell_data = this.clipboard; |
|
1044 | var cell_data = this.clipboard; | |
1078 | var new_cell = this.insert_cell_below(cell_data.cell_type); |
|
1045 | var new_cell = this.insert_cell_below(cell_data.cell_type); | |
1079 | new_cell.fromJSON(cell_data); |
|
1046 | new_cell.fromJSON(cell_data); | |
1080 | }; |
|
1047 | }; | |
1081 | }; |
|
1048 | }; | |
1082 |
|
1049 | |||
1083 | // Cell undelete |
|
1050 | // Cell undelete | |
1084 |
|
1051 | |||
1085 | /** |
|
1052 | /** | |
1086 | * Restore the most recently deleted cell. |
|
1053 | * Restore the most recently deleted cell. | |
1087 | * |
|
1054 | * | |
1088 | * @method undelete |
|
1055 | * @method undelete | |
1089 | */ |
|
1056 | */ | |
1090 | Notebook.prototype.undelete = function() { |
|
1057 | Notebook.prototype.undelete = function() { | |
1091 | if (this.undelete_backup !== null && this.undelete_index !== null) { |
|
1058 | if (this.undelete_backup !== null && this.undelete_index !== null) { | |
1092 | var current_index = this.get_selected_index(); |
|
1059 | var current_index = this.get_selected_index(); | |
1093 | if (this.undelete_index < current_index) { |
|
1060 | if (this.undelete_index < current_index) { | |
1094 | current_index = current_index + 1; |
|
1061 | current_index = current_index + 1; | |
1095 | } |
|
1062 | } | |
1096 | if (this.undelete_index >= this.ncells()) { |
|
1063 | if (this.undelete_index >= this.ncells()) { | |
1097 | this.select(this.ncells() - 1); |
|
1064 | this.select(this.ncells() - 1); | |
1098 | } |
|
1065 | } | |
1099 | else { |
|
1066 | else { | |
1100 | this.select(this.undelete_index); |
|
1067 | this.select(this.undelete_index); | |
1101 | } |
|
1068 | } | |
1102 | var cell_data = this.undelete_backup; |
|
1069 | var cell_data = this.undelete_backup; | |
1103 | var new_cell = null; |
|
1070 | var new_cell = null; | |
1104 | if (this.undelete_below) { |
|
1071 | if (this.undelete_below) { | |
1105 | new_cell = this.insert_cell_below(cell_data.cell_type); |
|
1072 | new_cell = this.insert_cell_below(cell_data.cell_type); | |
1106 | } else { |
|
1073 | } else { | |
1107 | new_cell = this.insert_cell_above(cell_data.cell_type); |
|
1074 | new_cell = this.insert_cell_above(cell_data.cell_type); | |
1108 | } |
|
1075 | } | |
1109 | new_cell.fromJSON(cell_data); |
|
1076 | new_cell.fromJSON(cell_data); | |
1110 | this.select(current_index); |
|
1077 | this.select(current_index); | |
1111 | this.undelete_backup = null; |
|
1078 | this.undelete_backup = null; | |
1112 | this.undelete_index = null; |
|
1079 | this.undelete_index = null; | |
1113 | } |
|
1080 | } | |
1114 | $('#undelete_cell').addClass('ui-state-disabled'); |
|
1081 | $('#undelete_cell').addClass('ui-state-disabled'); | |
1115 | } |
|
1082 | } | |
1116 |
|
1083 | |||
1117 | // Split/merge |
|
1084 | // Split/merge | |
1118 |
|
1085 | |||
1119 | /** |
|
1086 | /** | |
1120 | * Split the selected cell into two, at the cursor. |
|
1087 | * Split the selected cell into two, at the cursor. | |
1121 | * |
|
1088 | * | |
1122 | * @method split_cell |
|
1089 | * @method split_cell | |
1123 | */ |
|
1090 | */ | |
1124 | Notebook.prototype.split_cell = function () { |
|
1091 | Notebook.prototype.split_cell = function () { | |
1125 | // Todo: implement spliting for other cell types. |
|
1092 | // Todo: implement spliting for other cell types. | |
1126 | var cell = this.get_selected_cell(); |
|
1093 | var cell = this.get_selected_cell(); | |
1127 | if (cell.is_splittable()) { |
|
1094 | if (cell.is_splittable()) { | |
1128 | var texta = cell.get_pre_cursor(); |
|
1095 | var texta = cell.get_pre_cursor(); | |
1129 | var textb = cell.get_post_cursor(); |
|
1096 | var textb = cell.get_post_cursor(); | |
1130 | if (cell instanceof IPython.CodeCell) { |
|
1097 | if (cell instanceof IPython.CodeCell) { | |
1131 | cell.set_text(texta); |
|
1098 | cell.set_text(texta); | |
1132 | var new_cell = this.insert_cell_below('code'); |
|
1099 | var new_cell = this.insert_cell_below('code'); | |
1133 | new_cell.set_text(textb); |
|
1100 | new_cell.set_text(textb); | |
1134 | } else if (cell instanceof IPython.MarkdownCell) { |
|
1101 | } else if (cell instanceof IPython.MarkdownCell) { | |
1135 | cell.set_text(texta); |
|
1102 | cell.set_text(texta); | |
1136 | cell.render(); |
|
1103 | cell.render(); | |
1137 | var new_cell = this.insert_cell_below('markdown'); |
|
1104 | var new_cell = this.insert_cell_below('markdown'); | |
1138 | new_cell.edit(); // editor must be visible to call set_text |
|
1105 | new_cell.edit(); // editor must be visible to call set_text | |
1139 | new_cell.set_text(textb); |
|
1106 | new_cell.set_text(textb); | |
1140 | new_cell.render(); |
|
1107 | new_cell.render(); | |
1141 | } else if (cell instanceof IPython.HTMLCell) { |
|
1108 | } | |
1142 | cell.set_text(texta); |
|
|||
1143 | cell.render(); |
|
|||
1144 | var new_cell = this.insert_cell_below('html'); |
|
|||
1145 | new_cell.edit(); // editor must be visible to call set_text |
|
|||
1146 | new_cell.set_text(textb); |
|
|||
1147 | new_cell.render(); |
|
|||
1148 | }; |
|
|||
1149 | }; |
|
1109 | }; | |
1150 | }; |
|
1110 | }; | |
1151 |
|
1111 | |||
1152 | /** |
|
1112 | /** | |
1153 | * Combine the selected cell into the cell above it. |
|
1113 | * Combine the selected cell into the cell above it. | |
1154 | * |
|
1114 | * | |
1155 | * @method merge_cell_above |
|
1115 | * @method merge_cell_above | |
1156 | */ |
|
1116 | */ | |
1157 | Notebook.prototype.merge_cell_above = function () { |
|
1117 | Notebook.prototype.merge_cell_above = function () { | |
1158 | var index = this.get_selected_index(); |
|
1118 | var index = this.get_selected_index(); | |
1159 | var cell = this.get_cell(index); |
|
1119 | var cell = this.get_cell(index); | |
1160 | if (index > 0) { |
|
1120 | if (index > 0) { | |
1161 | var upper_cell = this.get_cell(index-1); |
|
1121 | var upper_cell = this.get_cell(index-1); | |
1162 | var upper_text = upper_cell.get_text(); |
|
1122 | var upper_text = upper_cell.get_text(); | |
1163 | var text = cell.get_text(); |
|
1123 | var text = cell.get_text(); | |
1164 | if (cell instanceof IPython.CodeCell) { |
|
1124 | if (cell instanceof IPython.CodeCell) { | |
1165 | cell.set_text(upper_text+'\n'+text); |
|
1125 | cell.set_text(upper_text+'\n'+text); | |
1166 |
} else if (cell instanceof IPython.MarkdownCell |
|
1126 | } else if (cell instanceof IPython.MarkdownCell) { | |
1167 | cell.edit(); |
|
1127 | cell.edit(); | |
1168 | cell.set_text(upper_text+'\n'+text); |
|
1128 | cell.set_text(upper_text+'\n'+text); | |
1169 | cell.render(); |
|
1129 | cell.render(); | |
1170 | }; |
|
1130 | }; | |
1171 | this.delete_cell(index-1); |
|
1131 | this.delete_cell(index-1); | |
1172 | this.select(this.find_cell_index(cell)); |
|
1132 | this.select(this.find_cell_index(cell)); | |
1173 | }; |
|
1133 | }; | |
1174 | }; |
|
1134 | }; | |
1175 |
|
1135 | |||
1176 | /** |
|
1136 | /** | |
1177 | * Combine the selected cell into the cell below it. |
|
1137 | * Combine the selected cell into the cell below it. | |
1178 | * |
|
1138 | * | |
1179 | * @method merge_cell_below |
|
1139 | * @method merge_cell_below | |
1180 | */ |
|
1140 | */ | |
1181 | Notebook.prototype.merge_cell_below = function () { |
|
1141 | Notebook.prototype.merge_cell_below = function () { | |
1182 | var index = this.get_selected_index(); |
|
1142 | var index = this.get_selected_index(); | |
1183 | var cell = this.get_cell(index); |
|
1143 | var cell = this.get_cell(index); | |
1184 | if (index < this.ncells()-1) { |
|
1144 | if (index < this.ncells()-1) { | |
1185 | var lower_cell = this.get_cell(index+1); |
|
1145 | var lower_cell = this.get_cell(index+1); | |
1186 | var lower_text = lower_cell.get_text(); |
|
1146 | var lower_text = lower_cell.get_text(); | |
1187 | var text = cell.get_text(); |
|
1147 | var text = cell.get_text(); | |
1188 | if (cell instanceof IPython.CodeCell) { |
|
1148 | if (cell instanceof IPython.CodeCell) { | |
1189 | cell.set_text(text+'\n'+lower_text); |
|
1149 | cell.set_text(text+'\n'+lower_text); | |
1190 |
} else if (cell instanceof IPython.MarkdownCell |
|
1150 | } else if (cell instanceof IPython.MarkdownCell) { | |
1191 | cell.edit(); |
|
1151 | cell.edit(); | |
1192 | cell.set_text(text+'\n'+lower_text); |
|
1152 | cell.set_text(text+'\n'+lower_text); | |
1193 | cell.render(); |
|
1153 | cell.render(); | |
1194 | }; |
|
1154 | }; | |
1195 | this.delete_cell(index+1); |
|
1155 | this.delete_cell(index+1); | |
1196 | this.select(this.find_cell_index(cell)); |
|
1156 | this.select(this.find_cell_index(cell)); | |
1197 | }; |
|
1157 | }; | |
1198 | }; |
|
1158 | }; | |
1199 |
|
1159 | |||
1200 |
|
1160 | |||
1201 | // Cell collapsing and output clearing |
|
1161 | // Cell collapsing and output clearing | |
1202 |
|
1162 | |||
1203 | /** |
|
1163 | /** | |
1204 | * Hide a cell's output. |
|
1164 | * Hide a cell's output. | |
1205 | * |
|
1165 | * | |
1206 | * @method collapse |
|
1166 | * @method collapse | |
1207 | * @param {Number} index A cell's numeric index |
|
1167 | * @param {Number} index A cell's numeric index | |
1208 | */ |
|
1168 | */ | |
1209 | Notebook.prototype.collapse = function (index) { |
|
1169 | Notebook.prototype.collapse = function (index) { | |
1210 | var i = this.index_or_selected(index); |
|
1170 | var i = this.index_or_selected(index); | |
1211 | this.get_cell(i).collapse(); |
|
1171 | this.get_cell(i).collapse(); | |
1212 | this.dirty = true; |
|
1172 | this.dirty = true; | |
1213 | }; |
|
1173 | }; | |
1214 |
|
1174 | |||
1215 | /** |
|
1175 | /** | |
1216 | * Show a cell's output. |
|
1176 | * Show a cell's output. | |
1217 | * |
|
1177 | * | |
1218 | * @method expand |
|
1178 | * @method expand | |
1219 | * @param {Number} index A cell's numeric index |
|
1179 | * @param {Number} index A cell's numeric index | |
1220 | */ |
|
1180 | */ | |
1221 | Notebook.prototype.expand = function (index) { |
|
1181 | Notebook.prototype.expand = function (index) { | |
1222 | var i = this.index_or_selected(index); |
|
1182 | var i = this.index_or_selected(index); | |
1223 | this.get_cell(i).expand(); |
|
1183 | this.get_cell(i).expand(); | |
1224 | this.dirty = true; |
|
1184 | this.dirty = true; | |
1225 | }; |
|
1185 | }; | |
1226 |
|
1186 | |||
1227 | /** Toggle whether a cell's output is collapsed or expanded. |
|
1187 | /** Toggle whether a cell's output is collapsed or expanded. | |
1228 | * |
|
1188 | * | |
1229 | * @method toggle_output |
|
1189 | * @method toggle_output | |
1230 | * @param {Number} index A cell's numeric index |
|
1190 | * @param {Number} index A cell's numeric index | |
1231 | */ |
|
1191 | */ | |
1232 | Notebook.prototype.toggle_output = function (index) { |
|
1192 | Notebook.prototype.toggle_output = function (index) { | |
1233 | var i = this.index_or_selected(index); |
|
1193 | var i = this.index_or_selected(index); | |
1234 | this.get_cell(i).toggle_output(); |
|
1194 | this.get_cell(i).toggle_output(); | |
1235 | this.dirty = true; |
|
1195 | this.dirty = true; | |
1236 | }; |
|
1196 | }; | |
1237 |
|
1197 | |||
1238 | /** |
|
1198 | /** | |
1239 | * Toggle a scrollbar for long cell outputs. |
|
1199 | * Toggle a scrollbar for long cell outputs. | |
1240 | * |
|
1200 | * | |
1241 | * @method toggle_output_scroll |
|
1201 | * @method toggle_output_scroll | |
1242 | * @param {Number} index A cell's numeric index |
|
1202 | * @param {Number} index A cell's numeric index | |
1243 | */ |
|
1203 | */ | |
1244 | Notebook.prototype.toggle_output_scroll = function (index) { |
|
1204 | Notebook.prototype.toggle_output_scroll = function (index) { | |
1245 | var i = this.index_or_selected(index); |
|
1205 | var i = this.index_or_selected(index); | |
1246 | this.get_cell(i).toggle_output_scroll(); |
|
1206 | this.get_cell(i).toggle_output_scroll(); | |
1247 | }; |
|
1207 | }; | |
1248 |
|
1208 | |||
1249 | /** |
|
1209 | /** | |
1250 | * Hide each code cell's output area. |
|
1210 | * Hide each code cell's output area. | |
1251 | * |
|
1211 | * | |
1252 | * @method collapse_all_output |
|
1212 | * @method collapse_all_output | |
1253 | */ |
|
1213 | */ | |
1254 | Notebook.prototype.collapse_all_output = function () { |
|
1214 | Notebook.prototype.collapse_all_output = function () { | |
1255 | var ncells = this.ncells(); |
|
1215 | var ncells = this.ncells(); | |
1256 | var cells = this.get_cells(); |
|
1216 | var cells = this.get_cells(); | |
1257 | for (var i=0; i<ncells; i++) { |
|
1217 | for (var i=0; i<ncells; i++) { | |
1258 | if (cells[i] instanceof IPython.CodeCell) { |
|
1218 | if (cells[i] instanceof IPython.CodeCell) { | |
1259 | cells[i].output_area.collapse(); |
|
1219 | cells[i].output_area.collapse(); | |
1260 | } |
|
1220 | } | |
1261 | }; |
|
1221 | }; | |
1262 | // this should not be set if the `collapse` key is removed from nbformat |
|
1222 | // this should not be set if the `collapse` key is removed from nbformat | |
1263 | this.dirty = true; |
|
1223 | this.dirty = true; | |
1264 | }; |
|
1224 | }; | |
1265 |
|
1225 | |||
1266 | /** |
|
1226 | /** | |
1267 | * Expand each code cell's output area, and add a scrollbar for long output. |
|
1227 | * Expand each code cell's output area, and add a scrollbar for long output. | |
1268 | * |
|
1228 | * | |
1269 | * @method scroll_all_output |
|
1229 | * @method scroll_all_output | |
1270 | */ |
|
1230 | */ | |
1271 | Notebook.prototype.scroll_all_output = function () { |
|
1231 | Notebook.prototype.scroll_all_output = function () { | |
1272 | var ncells = this.ncells(); |
|
1232 | var ncells = this.ncells(); | |
1273 | var cells = this.get_cells(); |
|
1233 | var cells = this.get_cells(); | |
1274 | for (var i=0; i<ncells; i++) { |
|
1234 | for (var i=0; i<ncells; i++) { | |
1275 | if (cells[i] instanceof IPython.CodeCell) { |
|
1235 | if (cells[i] instanceof IPython.CodeCell) { | |
1276 | cells[i].output_area.expand(); |
|
1236 | cells[i].output_area.expand(); | |
1277 | cells[i].output_area.scroll_if_long(20); |
|
1237 | cells[i].output_area.scroll_if_long(20); | |
1278 | } |
|
1238 | } | |
1279 | }; |
|
1239 | }; | |
1280 | // this should not be set if the `collapse` key is removed from nbformat |
|
1240 | // this should not be set if the `collapse` key is removed from nbformat | |
1281 | this.dirty = true; |
|
1241 | this.dirty = true; | |
1282 | }; |
|
1242 | }; | |
1283 |
|
1243 | |||
1284 | /** |
|
1244 | /** | |
1285 | * Expand each code cell's output area, and remove scrollbars. |
|
1245 | * Expand each code cell's output area, and remove scrollbars. | |
1286 | * |
|
1246 | * | |
1287 | * @method expand_all_output |
|
1247 | * @method expand_all_output | |
1288 | */ |
|
1248 | */ | |
1289 | Notebook.prototype.expand_all_output = function () { |
|
1249 | Notebook.prototype.expand_all_output = function () { | |
1290 | var ncells = this.ncells(); |
|
1250 | var ncells = this.ncells(); | |
1291 | var cells = this.get_cells(); |
|
1251 | var cells = this.get_cells(); | |
1292 | for (var i=0; i<ncells; i++) { |
|
1252 | for (var i=0; i<ncells; i++) { | |
1293 | if (cells[i] instanceof IPython.CodeCell) { |
|
1253 | if (cells[i] instanceof IPython.CodeCell) { | |
1294 | cells[i].output_area.expand(); |
|
1254 | cells[i].output_area.expand(); | |
1295 | cells[i].output_area.unscroll_area(); |
|
1255 | cells[i].output_area.unscroll_area(); | |
1296 | } |
|
1256 | } | |
1297 | }; |
|
1257 | }; | |
1298 | // this should not be set if the `collapse` key is removed from nbformat |
|
1258 | // this should not be set if the `collapse` key is removed from nbformat | |
1299 | this.dirty = true; |
|
1259 | this.dirty = true; | |
1300 | }; |
|
1260 | }; | |
1301 |
|
1261 | |||
1302 | /** |
|
1262 | /** | |
1303 | * Clear each code cell's output area. |
|
1263 | * Clear each code cell's output area. | |
1304 | * |
|
1264 | * | |
1305 | * @method clear_all_output |
|
1265 | * @method clear_all_output | |
1306 | */ |
|
1266 | */ | |
1307 | Notebook.prototype.clear_all_output = function () { |
|
1267 | Notebook.prototype.clear_all_output = function () { | |
1308 | var ncells = this.ncells(); |
|
1268 | var ncells = this.ncells(); | |
1309 | var cells = this.get_cells(); |
|
1269 | var cells = this.get_cells(); | |
1310 | for (var i=0; i<ncells; i++) { |
|
1270 | for (var i=0; i<ncells; i++) { | |
1311 | if (cells[i] instanceof IPython.CodeCell) { |
|
1271 | if (cells[i] instanceof IPython.CodeCell) { | |
1312 | cells[i].clear_output(true,true,true); |
|
1272 | cells[i].clear_output(true,true,true); | |
1313 | // Make all In[] prompts blank, as well |
|
1273 | // Make all In[] prompts blank, as well | |
1314 | // TODO: make this configurable (via checkbox?) |
|
1274 | // TODO: make this configurable (via checkbox?) | |
1315 | cells[i].set_input_prompt(); |
|
1275 | cells[i].set_input_prompt(); | |
1316 | } |
|
1276 | } | |
1317 | }; |
|
1277 | }; | |
1318 | this.dirty = true; |
|
1278 | this.dirty = true; | |
1319 | }; |
|
1279 | }; | |
1320 |
|
1280 | |||
1321 |
|
1281 | |||
1322 | // Other cell functions: line numbers, ... |
|
1282 | // Other cell functions: line numbers, ... | |
1323 |
|
1283 | |||
1324 | /** |
|
1284 | /** | |
1325 | * Toggle line numbers in the selected cell's input area. |
|
1285 | * Toggle line numbers in the selected cell's input area. | |
1326 | * |
|
1286 | * | |
1327 | * @method cell_toggle_line_numbers |
|
1287 | * @method cell_toggle_line_numbers | |
1328 | */ |
|
1288 | */ | |
1329 | Notebook.prototype.cell_toggle_line_numbers = function() { |
|
1289 | Notebook.prototype.cell_toggle_line_numbers = function() { | |
1330 | this.get_selected_cell().toggle_line_numbers(); |
|
1290 | this.get_selected_cell().toggle_line_numbers(); | |
1331 | }; |
|
1291 | }; | |
1332 |
|
1292 | |||
1333 | // Kernel related things |
|
1293 | // Kernel related things | |
1334 |
|
1294 | |||
1335 | /** |
|
1295 | /** | |
1336 | * Start a new kernel and set it on each code cell. |
|
1296 | * Start a new kernel and set it on each code cell. | |
1337 | * |
|
1297 | * | |
1338 | * @method start_kernel |
|
1298 | * @method start_kernel | |
1339 | */ |
|
1299 | */ | |
1340 | Notebook.prototype.start_kernel = function () { |
|
1300 | Notebook.prototype.start_kernel = function () { | |
1341 | var base_url = $('body').data('baseKernelUrl') + "kernels"; |
|
1301 | var base_url = $('body').data('baseKernelUrl') + "kernels"; | |
1342 | this.kernel = new IPython.Kernel(base_url); |
|
1302 | this.kernel = new IPython.Kernel(base_url); | |
1343 | this.kernel.start(this.notebook_id); |
|
1303 | this.kernel.start(this.notebook_id); | |
1344 | // Now that the kernel has been created, tell the CodeCells about it. |
|
1304 | // Now that the kernel has been created, tell the CodeCells about it. | |
1345 | var ncells = this.ncells(); |
|
1305 | var ncells = this.ncells(); | |
1346 | for (var i=0; i<ncells; i++) { |
|
1306 | for (var i=0; i<ncells; i++) { | |
1347 | var cell = this.get_cell(i); |
|
1307 | var cell = this.get_cell(i); | |
1348 | if (cell instanceof IPython.CodeCell) { |
|
1308 | if (cell instanceof IPython.CodeCell) { | |
1349 | cell.set_kernel(this.kernel) |
|
1309 | cell.set_kernel(this.kernel) | |
1350 | }; |
|
1310 | }; | |
1351 | }; |
|
1311 | }; | |
1352 | }; |
|
1312 | }; | |
1353 |
|
1313 | |||
1354 | /** |
|
1314 | /** | |
1355 | * Prompt the user to restart the IPython kernel. |
|
1315 | * Prompt the user to restart the IPython kernel. | |
1356 | * |
|
1316 | * | |
1357 | * @method restart_kernel |
|
1317 | * @method restart_kernel | |
1358 | */ |
|
1318 | */ | |
1359 | Notebook.prototype.restart_kernel = function () { |
|
1319 | Notebook.prototype.restart_kernel = function () { | |
1360 | var that = this; |
|
1320 | var that = this; | |
1361 | var dialog = $('<div/>'); |
|
1321 | var dialog = $('<div/>'); | |
1362 | dialog.html('Do you want to restart the current kernel? You will lose all variables defined in it.'); |
|
1322 | dialog.html('Do you want to restart the current kernel? You will lose all variables defined in it.'); | |
1363 | $(document).append(dialog); |
|
1323 | $(document).append(dialog); | |
1364 | dialog.dialog({ |
|
1324 | dialog.dialog({ | |
1365 | resizable: false, |
|
1325 | resizable: false, | |
1366 | modal: true, |
|
1326 | modal: true, | |
1367 | title: "Restart kernel or continue running?", |
|
1327 | title: "Restart kernel or continue running?", | |
1368 | closeText: '', |
|
1328 | closeText: '', | |
1369 | buttons : { |
|
1329 | buttons : { | |
1370 | "Restart": function () { |
|
1330 | "Restart": function () { | |
1371 | that.kernel.restart(); |
|
1331 | that.kernel.restart(); | |
1372 | $(this).dialog('close'); |
|
1332 | $(this).dialog('close'); | |
1373 | }, |
|
1333 | }, | |
1374 | "Continue running": function () { |
|
1334 | "Continue running": function () { | |
1375 | $(this).dialog('close'); |
|
1335 | $(this).dialog('close'); | |
1376 | } |
|
1336 | } | |
1377 | } |
|
1337 | } | |
1378 | }); |
|
1338 | }); | |
1379 | }; |
|
1339 | }; | |
1380 |
|
1340 | |||
1381 | /** |
|
1341 | /** | |
1382 | * Run the selected cell. |
|
1342 | * Run the selected cell. | |
1383 | * |
|
1343 | * | |
1384 | * Execute or render cell outputs. |
|
1344 | * Execute or render cell outputs. | |
1385 | * |
|
1345 | * | |
1386 | * @method execute_selected_cell |
|
1346 | * @method execute_selected_cell | |
1387 | * @param {Object} options Customize post-execution behavior |
|
1347 | * @param {Object} options Customize post-execution behavior | |
1388 | */ |
|
1348 | */ | |
1389 | Notebook.prototype.execute_selected_cell = function (options) { |
|
1349 | Notebook.prototype.execute_selected_cell = function (options) { | |
1390 | // add_new: should a new cell be added if we are at the end of the nb |
|
1350 | // add_new: should a new cell be added if we are at the end of the nb | |
1391 | // terminal: execute in terminal mode, which stays in the current cell |
|
1351 | // terminal: execute in terminal mode, which stays in the current cell | |
1392 | var default_options = {terminal: false, add_new: true}; |
|
1352 | var default_options = {terminal: false, add_new: true}; | |
1393 | $.extend(default_options, options); |
|
1353 | $.extend(default_options, options); | |
1394 | var that = this; |
|
1354 | var that = this; | |
1395 | var cell = that.get_selected_cell(); |
|
1355 | var cell = that.get_selected_cell(); | |
1396 | var cell_index = that.find_cell_index(cell); |
|
1356 | var cell_index = that.find_cell_index(cell); | |
1397 | if (cell instanceof IPython.CodeCell) { |
|
1357 | if (cell instanceof IPython.CodeCell) { | |
1398 | cell.execute(); |
|
1358 | cell.execute(); | |
1399 | } else if (cell instanceof IPython.HTMLCell) { |
|
|||
1400 | cell.render(); |
|
|||
1401 | } |
|
1359 | } | |
1402 | if (default_options.terminal) { |
|
1360 | if (default_options.terminal) { | |
1403 | cell.select_all(); |
|
1361 | cell.select_all(); | |
1404 | } else { |
|
1362 | } else { | |
1405 | if ((cell_index === (that.ncells()-1)) && default_options.add_new) { |
|
1363 | if ((cell_index === (that.ncells()-1)) && default_options.add_new) { | |
1406 | that.insert_cell_below('code'); |
|
1364 | that.insert_cell_below('code'); | |
1407 | // If we are adding a new cell at the end, scroll down to show it. |
|
1365 | // If we are adding a new cell at the end, scroll down to show it. | |
1408 | that.scroll_to_bottom(); |
|
1366 | that.scroll_to_bottom(); | |
1409 | } else { |
|
1367 | } else { | |
1410 | that.select(cell_index+1); |
|
1368 | that.select(cell_index+1); | |
1411 | }; |
|
1369 | }; | |
1412 | }; |
|
1370 | }; | |
1413 | this.dirty = true; |
|
1371 | this.dirty = true; | |
1414 | }; |
|
1372 | }; | |
1415 |
|
1373 | |||
1416 | /** |
|
1374 | /** | |
1417 | * Execute all cells below the selected cell. |
|
1375 | * Execute all cells below the selected cell. | |
1418 | * |
|
1376 | * | |
1419 | * @method execute_cells_below |
|
1377 | * @method execute_cells_below | |
1420 | */ |
|
1378 | */ | |
1421 | Notebook.prototype.execute_cells_below = function () { |
|
1379 | Notebook.prototype.execute_cells_below = function () { | |
1422 | this.execute_cell_range(this.get_selected_index(), this.ncells()); |
|
1380 | this.execute_cell_range(this.get_selected_index(), this.ncells()); | |
1423 | this.scroll_to_bottom(); |
|
1381 | this.scroll_to_bottom(); | |
1424 | }; |
|
1382 | }; | |
1425 |
|
1383 | |||
1426 | /** |
|
1384 | /** | |
1427 | * Execute all cells above the selected cell. |
|
1385 | * Execute all cells above the selected cell. | |
1428 | * |
|
1386 | * | |
1429 | * @method execute_cells_above |
|
1387 | * @method execute_cells_above | |
1430 | */ |
|
1388 | */ | |
1431 | Notebook.prototype.execute_cells_above = function () { |
|
1389 | Notebook.prototype.execute_cells_above = function () { | |
1432 | this.execute_cell_range(0, this.get_selected_index()); |
|
1390 | this.execute_cell_range(0, this.get_selected_index()); | |
1433 | }; |
|
1391 | }; | |
1434 |
|
1392 | |||
1435 | /** |
|
1393 | /** | |
1436 | * Execute all cells. |
|
1394 | * Execute all cells. | |
1437 | * |
|
1395 | * | |
1438 | * @method execute_all_cells |
|
1396 | * @method execute_all_cells | |
1439 | */ |
|
1397 | */ | |
1440 | Notebook.prototype.execute_all_cells = function () { |
|
1398 | Notebook.prototype.execute_all_cells = function () { | |
1441 | this.execute_cell_range(0, this.ncells()); |
|
1399 | this.execute_cell_range(0, this.ncells()); | |
1442 | this.scroll_to_bottom(); |
|
1400 | this.scroll_to_bottom(); | |
1443 | }; |
|
1401 | }; | |
1444 |
|
1402 | |||
1445 | /** |
|
1403 | /** | |
1446 | * Execute a contiguous range of cells. |
|
1404 | * Execute a contiguous range of cells. | |
1447 | * |
|
1405 | * | |
1448 | * @method execute_cell_range |
|
1406 | * @method execute_cell_range | |
1449 | * @param {Number} start Index of the first cell to execute (inclusive) |
|
1407 | * @param {Number} start Index of the first cell to execute (inclusive) | |
1450 | * @param {Number} end Index of the last cell to execute (exclusive) |
|
1408 | * @param {Number} end Index of the last cell to execute (exclusive) | |
1451 | */ |
|
1409 | */ | |
1452 | Notebook.prototype.execute_cell_range = function (start, end) { |
|
1410 | Notebook.prototype.execute_cell_range = function (start, end) { | |
1453 | for (var i=start; i<end; i++) { |
|
1411 | for (var i=start; i<end; i++) { | |
1454 | this.select(i); |
|
1412 | this.select(i); | |
1455 | this.execute_selected_cell({add_new:false}); |
|
1413 | this.execute_selected_cell({add_new:false}); | |
1456 | }; |
|
1414 | }; | |
1457 | }; |
|
1415 | }; | |
1458 |
|
1416 | |||
1459 | // Persistance and loading |
|
1417 | // Persistance and loading | |
1460 |
|
1418 | |||
1461 | /** |
|
1419 | /** | |
1462 | * Getter method for this notebook's ID. |
|
1420 | * Getter method for this notebook's ID. | |
1463 | * |
|
1421 | * | |
1464 | * @method get_notebook_id |
|
1422 | * @method get_notebook_id | |
1465 | * @return {String} This notebook's ID |
|
1423 | * @return {String} This notebook's ID | |
1466 | */ |
|
1424 | */ | |
1467 | Notebook.prototype.get_notebook_id = function () { |
|
1425 | Notebook.prototype.get_notebook_id = function () { | |
1468 | return this.notebook_id; |
|
1426 | return this.notebook_id; | |
1469 | }; |
|
1427 | }; | |
1470 |
|
1428 | |||
1471 | /** |
|
1429 | /** | |
1472 | * Getter method for this notebook's name. |
|
1430 | * Getter method for this notebook's name. | |
1473 | * |
|
1431 | * | |
1474 | * @method get_notebook_name |
|
1432 | * @method get_notebook_name | |
1475 | * @return {String} This notebook's name |
|
1433 | * @return {String} This notebook's name | |
1476 | */ |
|
1434 | */ | |
1477 | Notebook.prototype.get_notebook_name = function () { |
|
1435 | Notebook.prototype.get_notebook_name = function () { | |
1478 | return this.notebook_name; |
|
1436 | return this.notebook_name; | |
1479 | }; |
|
1437 | }; | |
1480 |
|
1438 | |||
1481 | /** |
|
1439 | /** | |
1482 | * Setter method for this notebook's name. |
|
1440 | * Setter method for this notebook's name. | |
1483 | * |
|
1441 | * | |
1484 | * @method set_notebook_name |
|
1442 | * @method set_notebook_name | |
1485 | * @param {String} name A new name for this notebook |
|
1443 | * @param {String} name A new name for this notebook | |
1486 | */ |
|
1444 | */ | |
1487 | Notebook.prototype.set_notebook_name = function (name) { |
|
1445 | Notebook.prototype.set_notebook_name = function (name) { | |
1488 | this.notebook_name = name; |
|
1446 | this.notebook_name = name; | |
1489 | }; |
|
1447 | }; | |
1490 |
|
1448 | |||
1491 | /** |
|
1449 | /** | |
1492 | * Check that a notebook's name is valid. |
|
1450 | * Check that a notebook's name is valid. | |
1493 | * |
|
1451 | * | |
1494 | * @method test_notebook_name |
|
1452 | * @method test_notebook_name | |
1495 | * @param {String} nbname A name for this notebook |
|
1453 | * @param {String} nbname A name for this notebook | |
1496 | * @return {Boolean} True if the name is valid, false if invalid |
|
1454 | * @return {Boolean} True if the name is valid, false if invalid | |
1497 | */ |
|
1455 | */ | |
1498 | Notebook.prototype.test_notebook_name = function (nbname) { |
|
1456 | Notebook.prototype.test_notebook_name = function (nbname) { | |
1499 | nbname = nbname || ''; |
|
1457 | nbname = nbname || ''; | |
1500 | if (this.notebook_name_blacklist_re.test(nbname) == false && nbname.length>0) { |
|
1458 | if (this.notebook_name_blacklist_re.test(nbname) == false && nbname.length>0) { | |
1501 | return true; |
|
1459 | return true; | |
1502 | } else { |
|
1460 | } else { | |
1503 | return false; |
|
1461 | return false; | |
1504 | }; |
|
1462 | }; | |
1505 | }; |
|
1463 | }; | |
1506 |
|
1464 | |||
1507 | /** |
|
1465 | /** | |
1508 | * Load a notebook from JSON (.ipynb). |
|
1466 | * Load a notebook from JSON (.ipynb). | |
1509 | * |
|
1467 | * | |
1510 | * This currently handles one worksheet: others are deleted. |
|
1468 | * This currently handles one worksheet: others are deleted. | |
1511 | * |
|
1469 | * | |
1512 | * @method fromJSON |
|
1470 | * @method fromJSON | |
1513 | * @param {Object} data JSON representation of a notebook |
|
1471 | * @param {Object} data JSON representation of a notebook | |
1514 | */ |
|
1472 | */ | |
1515 | Notebook.prototype.fromJSON = function (data) { |
|
1473 | Notebook.prototype.fromJSON = function (data) { | |
1516 | var ncells = this.ncells(); |
|
1474 | var ncells = this.ncells(); | |
1517 | var i; |
|
1475 | var i; | |
1518 | for (i=0; i<ncells; i++) { |
|
1476 | for (i=0; i<ncells; i++) { | |
1519 | // Always delete cell 0 as they get renumbered as they are deleted. |
|
1477 | // Always delete cell 0 as they get renumbered as they are deleted. | |
1520 | this.delete_cell(0); |
|
1478 | this.delete_cell(0); | |
1521 | }; |
|
1479 | }; | |
1522 | // Save the metadata and name. |
|
1480 | // Save the metadata and name. | |
1523 | this.metadata = data.metadata; |
|
1481 | this.metadata = data.metadata; | |
1524 | this.notebook_name = data.metadata.name; |
|
1482 | this.notebook_name = data.metadata.name; | |
1525 | // Only handle 1 worksheet for now. |
|
1483 | // Only handle 1 worksheet for now. | |
1526 | var worksheet = data.worksheets[0]; |
|
1484 | var worksheet = data.worksheets[0]; | |
1527 | if (worksheet !== undefined) { |
|
1485 | if (worksheet !== undefined) { | |
1528 | if (worksheet.metadata) { |
|
1486 | if (worksheet.metadata) { | |
1529 | this.worksheet_metadata = worksheet.metadata; |
|
1487 | this.worksheet_metadata = worksheet.metadata; | |
1530 | } |
|
1488 | } | |
1531 | var new_cells = worksheet.cells; |
|
1489 | var new_cells = worksheet.cells; | |
1532 | ncells = new_cells.length; |
|
1490 | ncells = new_cells.length; | |
1533 | var cell_data = null; |
|
1491 | var cell_data = null; | |
1534 | var new_cell = null; |
|
1492 | var new_cell = null; | |
1535 | for (i=0; i<ncells; i++) { |
|
1493 | for (i=0; i<ncells; i++) { | |
1536 | cell_data = new_cells[i]; |
|
1494 | cell_data = new_cells[i]; | |
1537 | // VERSIONHACK: plaintext -> raw |
|
1495 | // VERSIONHACK: plaintext -> raw | |
1538 | // handle never-released plaintext name for raw cells |
|
1496 | // handle never-released plaintext name for raw cells | |
1539 | if (cell_data.cell_type === 'plaintext'){ |
|
1497 | if (cell_data.cell_type === 'plaintext'){ | |
1540 | cell_data.cell_type = 'raw'; |
|
1498 | cell_data.cell_type = 'raw'; | |
1541 | } |
|
1499 | } | |
1542 |
|
1500 | |||
1543 | new_cell = this.insert_cell_below(cell_data.cell_type); |
|
1501 | new_cell = this.insert_cell_below(cell_data.cell_type); | |
1544 | new_cell.fromJSON(cell_data); |
|
1502 | new_cell.fromJSON(cell_data); | |
1545 | }; |
|
1503 | }; | |
1546 | }; |
|
1504 | }; | |
1547 | if (data.worksheets.length > 1) { |
|
1505 | if (data.worksheets.length > 1) { | |
1548 | var dialog = $('<div/>'); |
|
1506 | var dialog = $('<div/>'); | |
1549 | dialog.html("This notebook has " + data.worksheets.length + " worksheets, " + |
|
1507 | dialog.html("This notebook has " + data.worksheets.length + " worksheets, " + | |
1550 | "but this version of IPython can only handle the first. " + |
|
1508 | "but this version of IPython can only handle the first. " + | |
1551 | "If you save this notebook, worksheets after the first will be lost." |
|
1509 | "If you save this notebook, worksheets after the first will be lost." | |
1552 | ); |
|
1510 | ); | |
1553 | this.element.append(dialog); |
|
1511 | this.element.append(dialog); | |
1554 | dialog.dialog({ |
|
1512 | dialog.dialog({ | |
1555 | resizable: false, |
|
1513 | resizable: false, | |
1556 | modal: true, |
|
1514 | modal: true, | |
1557 | title: "Multiple worksheets", |
|
1515 | title: "Multiple worksheets", | |
1558 | closeText: "", |
|
1516 | closeText: "", | |
1559 | close: function(event, ui) {$(this).dialog('destroy').remove();}, |
|
1517 | close: function(event, ui) {$(this).dialog('destroy').remove();}, | |
1560 | buttons : { |
|
1518 | buttons : { | |
1561 | "OK": function () { |
|
1519 | "OK": function () { | |
1562 | $(this).dialog('close'); |
|
1520 | $(this).dialog('close'); | |
1563 | } |
|
1521 | } | |
1564 | }, |
|
1522 | }, | |
1565 | width: 400 |
|
1523 | width: 400 | |
1566 | }); |
|
1524 | }); | |
1567 | } |
|
1525 | } | |
1568 | }; |
|
1526 | }; | |
1569 |
|
1527 | |||
1570 | /** |
|
1528 | /** | |
1571 | * Dump this notebook into a JSON-friendly object. |
|
1529 | * Dump this notebook into a JSON-friendly object. | |
1572 | * |
|
1530 | * | |
1573 | * @method toJSON |
|
1531 | * @method toJSON | |
1574 | * @return {Object} A JSON-friendly representation of this notebook. |
|
1532 | * @return {Object} A JSON-friendly representation of this notebook. | |
1575 | */ |
|
1533 | */ | |
1576 | Notebook.prototype.toJSON = function () { |
|
1534 | Notebook.prototype.toJSON = function () { | |
1577 | var cells = this.get_cells(); |
|
1535 | var cells = this.get_cells(); | |
1578 | var ncells = cells.length; |
|
1536 | var ncells = cells.length; | |
1579 | var cell_array = new Array(ncells); |
|
1537 | var cell_array = new Array(ncells); | |
1580 | for (var i=0; i<ncells; i++) { |
|
1538 | for (var i=0; i<ncells; i++) { | |
1581 | cell_array[i] = cells[i].toJSON(); |
|
1539 | cell_array[i] = cells[i].toJSON(); | |
1582 | }; |
|
1540 | }; | |
1583 | var data = { |
|
1541 | var data = { | |
1584 | // Only handle 1 worksheet for now. |
|
1542 | // Only handle 1 worksheet for now. | |
1585 | worksheets : [{ |
|
1543 | worksheets : [{ | |
1586 | cells: cell_array, |
|
1544 | cells: cell_array, | |
1587 | metadata: this.worksheet_metadata |
|
1545 | metadata: this.worksheet_metadata | |
1588 | }], |
|
1546 | }], | |
1589 | metadata : this.metadata |
|
1547 | metadata : this.metadata | |
1590 | }; |
|
1548 | }; | |
1591 | return data; |
|
1549 | return data; | |
1592 | }; |
|
1550 | }; | |
1593 |
|
1551 | |||
1594 | /** |
|
1552 | /** | |
1595 | * Save this notebook on the server. |
|
1553 | * Save this notebook on the server. | |
1596 | * |
|
1554 | * | |
1597 | * @method save_notebook |
|
1555 | * @method save_notebook | |
1598 | */ |
|
1556 | */ | |
1599 | Notebook.prototype.save_notebook = function () { |
|
1557 | Notebook.prototype.save_notebook = function () { | |
1600 | // We may want to move the name/id/nbformat logic inside toJSON? |
|
1558 | // We may want to move the name/id/nbformat logic inside toJSON? | |
1601 | var data = this.toJSON(); |
|
1559 | var data = this.toJSON(); | |
1602 | data.metadata.name = this.notebook_name; |
|
1560 | data.metadata.name = this.notebook_name; | |
1603 | data.nbformat = this.nbformat; |
|
1561 | data.nbformat = this.nbformat; | |
1604 | data.nbformat_minor = this.nbformat_minor; |
|
1562 | data.nbformat_minor = this.nbformat_minor; | |
1605 | // We do the call with settings so we can set cache to false. |
|
1563 | // We do the call with settings so we can set cache to false. | |
1606 | var settings = { |
|
1564 | var settings = { | |
1607 | processData : false, |
|
1565 | processData : false, | |
1608 | cache : false, |
|
1566 | cache : false, | |
1609 | type : "PUT", |
|
1567 | type : "PUT", | |
1610 | data : JSON.stringify(data), |
|
1568 | data : JSON.stringify(data), | |
1611 | headers : {'Content-Type': 'application/json'}, |
|
1569 | headers : {'Content-Type': 'application/json'}, | |
1612 | success : $.proxy(this.save_notebook_success,this), |
|
1570 | success : $.proxy(this.save_notebook_success,this), | |
1613 | error : $.proxy(this.save_notebook_error,this) |
|
1571 | error : $.proxy(this.save_notebook_error,this) | |
1614 | }; |
|
1572 | }; | |
1615 | $([IPython.events]).trigger('notebook_saving.Notebook'); |
|
1573 | $([IPython.events]).trigger('notebook_saving.Notebook'); | |
1616 | var url = this.baseProjectUrl() + 'notebooks/' + this.notebook_id; |
|
1574 | var url = this.baseProjectUrl() + 'notebooks/' + this.notebook_id; | |
1617 | $.ajax(url, settings); |
|
1575 | $.ajax(url, settings); | |
1618 | }; |
|
1576 | }; | |
1619 |
|
1577 | |||
1620 | /** |
|
1578 | /** | |
1621 | * Success callback for saving a notebook. |
|
1579 | * Success callback for saving a notebook. | |
1622 | * |
|
1580 | * | |
1623 | * @method save_notebook_success |
|
1581 | * @method save_notebook_success | |
1624 | * @param {Object} data JSON representation of a notebook |
|
1582 | * @param {Object} data JSON representation of a notebook | |
1625 | * @param {String} status Description of response status |
|
1583 | * @param {String} status Description of response status | |
1626 | * @param {jqXHR} xhr jQuery Ajax object |
|
1584 | * @param {jqXHR} xhr jQuery Ajax object | |
1627 | */ |
|
1585 | */ | |
1628 | Notebook.prototype.save_notebook_success = function (data, status, xhr) { |
|
1586 | Notebook.prototype.save_notebook_success = function (data, status, xhr) { | |
1629 | this.dirty = false; |
|
1587 | this.dirty = false; | |
1630 | $([IPython.events]).trigger('notebook_saved.Notebook'); |
|
1588 | $([IPython.events]).trigger('notebook_saved.Notebook'); | |
1631 | }; |
|
1589 | }; | |
1632 |
|
1590 | |||
1633 | /** |
|
1591 | /** | |
1634 | * Failure callback for saving a notebook. |
|
1592 | * Failure callback for saving a notebook. | |
1635 | * |
|
1593 | * | |
1636 | * @method save_notebook_error |
|
1594 | * @method save_notebook_error | |
1637 | * @param {jqXHR} xhr jQuery Ajax object |
|
1595 | * @param {jqXHR} xhr jQuery Ajax object | |
1638 | * @param {String} status Description of response status |
|
1596 | * @param {String} status Description of response status | |
1639 | * @param {String} error_msg HTTP error message |
|
1597 | * @param {String} error_msg HTTP error message | |
1640 | */ |
|
1598 | */ | |
1641 | Notebook.prototype.save_notebook_error = function (xhr, status, error_msg) { |
|
1599 | Notebook.prototype.save_notebook_error = function (xhr, status, error_msg) { | |
1642 | $([IPython.events]).trigger('notebook_save_failed.Notebook'); |
|
1600 | $([IPython.events]).trigger('notebook_save_failed.Notebook'); | |
1643 | }; |
|
1601 | }; | |
1644 |
|
1602 | |||
1645 | /** |
|
1603 | /** | |
1646 | * Request a notebook's data from the server. |
|
1604 | * Request a notebook's data from the server. | |
1647 | * |
|
1605 | * | |
1648 | * @method load_notebook |
|
1606 | * @method load_notebook | |
1649 | * @param {String} notebook_id A notebook to load |
|
1607 | * @param {String} notebook_id A notebook to load | |
1650 | */ |
|
1608 | */ | |
1651 | Notebook.prototype.load_notebook = function (notebook_id) { |
|
1609 | Notebook.prototype.load_notebook = function (notebook_id) { | |
1652 | var that = this; |
|
1610 | var that = this; | |
1653 | this.notebook_id = notebook_id; |
|
1611 | this.notebook_id = notebook_id; | |
1654 | // We do the call with settings so we can set cache to false. |
|
1612 | // We do the call with settings so we can set cache to false. | |
1655 | var settings = { |
|
1613 | var settings = { | |
1656 | processData : false, |
|
1614 | processData : false, | |
1657 | cache : false, |
|
1615 | cache : false, | |
1658 | type : "GET", |
|
1616 | type : "GET", | |
1659 | dataType : "json", |
|
1617 | dataType : "json", | |
1660 | success : $.proxy(this.load_notebook_success,this), |
|
1618 | success : $.proxy(this.load_notebook_success,this), | |
1661 | error : $.proxy(this.load_notebook_error,this), |
|
1619 | error : $.proxy(this.load_notebook_error,this), | |
1662 | }; |
|
1620 | }; | |
1663 | $([IPython.events]).trigger('notebook_loading.Notebook'); |
|
1621 | $([IPython.events]).trigger('notebook_loading.Notebook'); | |
1664 | var url = this.baseProjectUrl() + 'notebooks/' + this.notebook_id; |
|
1622 | var url = this.baseProjectUrl() + 'notebooks/' + this.notebook_id; | |
1665 | $.ajax(url, settings); |
|
1623 | $.ajax(url, settings); | |
1666 | }; |
|
1624 | }; | |
1667 |
|
1625 | |||
1668 | /** |
|
1626 | /** | |
1669 | * Success callback for loading a notebook from the server. |
|
1627 | * Success callback for loading a notebook from the server. | |
1670 | * |
|
1628 | * | |
1671 | * Load notebook data from the JSON response. |
|
1629 | * Load notebook data from the JSON response. | |
1672 | * |
|
1630 | * | |
1673 | * @method load_notebook_success |
|
1631 | * @method load_notebook_success | |
1674 | * @param {Object} data JSON representation of a notebook |
|
1632 | * @param {Object} data JSON representation of a notebook | |
1675 | * @param {String} status Description of response status |
|
1633 | * @param {String} status Description of response status | |
1676 | * @param {jqXHR} xhr jQuery Ajax object |
|
1634 | * @param {jqXHR} xhr jQuery Ajax object | |
1677 | */ |
|
1635 | */ | |
1678 | Notebook.prototype.load_notebook_success = function (data, status, xhr) { |
|
1636 | Notebook.prototype.load_notebook_success = function (data, status, xhr) { | |
1679 | this.fromJSON(data); |
|
1637 | this.fromJSON(data); | |
1680 | if (this.ncells() === 0) { |
|
1638 | if (this.ncells() === 0) { | |
1681 | this.insert_cell_below('code'); |
|
1639 | this.insert_cell_below('code'); | |
1682 | }; |
|
1640 | }; | |
1683 | this.dirty = false; |
|
1641 | this.dirty = false; | |
1684 | this.select(0); |
|
1642 | this.select(0); | |
1685 | this.scroll_to_top(); |
|
1643 | this.scroll_to_top(); | |
1686 | if (data.orig_nbformat !== undefined && data.nbformat !== data.orig_nbformat) { |
|
1644 | if (data.orig_nbformat !== undefined && data.nbformat !== data.orig_nbformat) { | |
1687 | msg = "This notebook has been converted from an older " + |
|
1645 | msg = "This notebook has been converted from an older " + | |
1688 | "notebook format (v"+data.orig_nbformat+") to the current notebook " + |
|
1646 | "notebook format (v"+data.orig_nbformat+") to the current notebook " + | |
1689 | "format (v"+data.nbformat+"). The next time you save this notebook, the " + |
|
1647 | "format (v"+data.nbformat+"). The next time you save this notebook, the " + | |
1690 | "newer notebook format will be used and older verions of IPython " + |
|
1648 | "newer notebook format will be used and older verions of IPython " + | |
1691 | "may not be able to read it. To keep the older version, close the " + |
|
1649 | "may not be able to read it. To keep the older version, close the " + | |
1692 | "notebook without saving it."; |
|
1650 | "notebook without saving it."; | |
1693 | var dialog = $('<div/>'); |
|
1651 | var dialog = $('<div/>'); | |
1694 | dialog.html(msg); |
|
1652 | dialog.html(msg); | |
1695 | this.element.append(dialog); |
|
1653 | this.element.append(dialog); | |
1696 | dialog.dialog({ |
|
1654 | dialog.dialog({ | |
1697 | resizable: false, |
|
1655 | resizable: false, | |
1698 | modal: true, |
|
1656 | modal: true, | |
1699 | title: "Notebook converted", |
|
1657 | title: "Notebook converted", | |
1700 | closeText: "", |
|
1658 | closeText: "", | |
1701 | close: function(event, ui) {$(this).dialog('destroy').remove();}, |
|
1659 | close: function(event, ui) {$(this).dialog('destroy').remove();}, | |
1702 | buttons : { |
|
1660 | buttons : { | |
1703 | "OK": function () { |
|
1661 | "OK": function () { | |
1704 | $(this).dialog('close'); |
|
1662 | $(this).dialog('close'); | |
1705 | } |
|
1663 | } | |
1706 | }, |
|
1664 | }, | |
1707 | width: 400 |
|
1665 | width: 400 | |
1708 | }); |
|
1666 | }); | |
1709 | } else if (data.orig_nbformat_minor !== undefined && data.nbformat_minor !== data.orig_nbformat_minor) { |
|
1667 | } else if (data.orig_nbformat_minor !== undefined && data.nbformat_minor !== data.orig_nbformat_minor) { | |
1710 | var that = this; |
|
1668 | var that = this; | |
1711 | var orig_vs = 'v' + data.nbformat + '.' + data.orig_nbformat_minor; |
|
1669 | var orig_vs = 'v' + data.nbformat + '.' + data.orig_nbformat_minor; | |
1712 | var this_vs = 'v' + data.nbformat + '.' + this.nbformat_minor; |
|
1670 | var this_vs = 'v' + data.nbformat + '.' + this.nbformat_minor; | |
1713 | var msg = "This notebook is version " + orig_vs + ", but we only fully support up to " + |
|
1671 | var msg = "This notebook is version " + orig_vs + ", but we only fully support up to " + | |
1714 | this_vs + ". You can still work with this notebook, but some features " + |
|
1672 | this_vs + ". You can still work with this notebook, but some features " + | |
1715 | "introduced in later notebook versions may not be available." |
|
1673 | "introduced in later notebook versions may not be available." | |
1716 |
|
1674 | |||
1717 | var dialog = $('<div/>'); |
|
1675 | var dialog = $('<div/>'); | |
1718 | dialog.html(msg); |
|
1676 | dialog.html(msg); | |
1719 | this.element.append(dialog); |
|
1677 | this.element.append(dialog); | |
1720 | dialog.dialog({ |
|
1678 | dialog.dialog({ | |
1721 | resizable: false, |
|
1679 | resizable: false, | |
1722 | modal: true, |
|
1680 | modal: true, | |
1723 | title: "Newer Notebook", |
|
1681 | title: "Newer Notebook", | |
1724 | closeText: "", |
|
1682 | closeText: "", | |
1725 | close: function(event, ui) {$(this).dialog('destroy').remove();}, |
|
1683 | close: function(event, ui) {$(this).dialog('destroy').remove();}, | |
1726 | buttons : { |
|
1684 | buttons : { | |
1727 | "OK": function () { |
|
1685 | "OK": function () { | |
1728 | $(this).dialog('close'); |
|
1686 | $(this).dialog('close'); | |
1729 | } |
|
1687 | } | |
1730 | }, |
|
1688 | }, | |
1731 | width: 400 |
|
1689 | width: 400 | |
1732 | }); |
|
1690 | }); | |
1733 |
|
1691 | |||
1734 | } |
|
1692 | } | |
1735 | // Create the kernel after the notebook is completely loaded to prevent |
|
1693 | // Create the kernel after the notebook is completely loaded to prevent | |
1736 | // code execution upon loading, which is a security risk. |
|
1694 | // code execution upon loading, which is a security risk. | |
1737 | if (! this.read_only) { |
|
1695 | if (! this.read_only) { | |
1738 | this.start_kernel(); |
|
1696 | this.start_kernel(); | |
1739 | } |
|
1697 | } | |
1740 | $([IPython.events]).trigger('notebook_loaded.Notebook'); |
|
1698 | $([IPython.events]).trigger('notebook_loaded.Notebook'); | |
1741 | }; |
|
1699 | }; | |
1742 |
|
1700 | |||
1743 | /** |
|
1701 | /** | |
1744 | * Failure callback for loading a notebook from the server. |
|
1702 | * Failure callback for loading a notebook from the server. | |
1745 | * |
|
1703 | * | |
1746 | * @method load_notebook_error |
|
1704 | * @method load_notebook_error | |
1747 | * @param {jqXHR} xhr jQuery Ajax object |
|
1705 | * @param {jqXHR} xhr jQuery Ajax object | |
1748 | * @param {String} textStatus Description of response status |
|
1706 | * @param {String} textStatus Description of response status | |
1749 | * @param {String} errorThrow HTTP error message |
|
1707 | * @param {String} errorThrow HTTP error message | |
1750 | */ |
|
1708 | */ | |
1751 | Notebook.prototype.load_notebook_error = function (xhr, textStatus, errorThrow) { |
|
1709 | Notebook.prototype.load_notebook_error = function (xhr, textStatus, errorThrow) { | |
1752 | if (xhr.status === 500) { |
|
1710 | if (xhr.status === 500) { | |
1753 | var msg = "An error occurred while loading this notebook. Most likely " + |
|
1711 | var msg = "An error occurred while loading this notebook. Most likely " + | |
1754 | "this notebook is in a newer format than is supported by this " + |
|
1712 | "this notebook is in a newer format than is supported by this " + | |
1755 | "version of IPython. This version can load notebook formats " + |
|
1713 | "version of IPython. This version can load notebook formats " + | |
1756 | "v"+this.nbformat+" or earlier."; |
|
1714 | "v"+this.nbformat+" or earlier."; | |
1757 | var dialog = $('<div/>'); |
|
1715 | var dialog = $('<div/>'); | |
1758 | dialog.html(msg); |
|
1716 | dialog.html(msg); | |
1759 | this.element.append(dialog); |
|
1717 | this.element.append(dialog); | |
1760 | dialog.dialog({ |
|
1718 | dialog.dialog({ | |
1761 | resizable: false, |
|
1719 | resizable: false, | |
1762 | modal: true, |
|
1720 | modal: true, | |
1763 | title: "Error loading notebook", |
|
1721 | title: "Error loading notebook", | |
1764 | closeText: "", |
|
1722 | closeText: "", | |
1765 | close: function(event, ui) {$(this).dialog('destroy').remove();}, |
|
1723 | close: function(event, ui) {$(this).dialog('destroy').remove();}, | |
1766 | buttons : { |
|
1724 | buttons : { | |
1767 | "OK": function () { |
|
1725 | "OK": function () { | |
1768 | $(this).dialog('close'); |
|
1726 | $(this).dialog('close'); | |
1769 | } |
|
1727 | } | |
1770 | }, |
|
1728 | }, | |
1771 | width: 400 |
|
1729 | width: 400 | |
1772 | }); |
|
1730 | }); | |
1773 | } |
|
1731 | } | |
1774 | } |
|
1732 | } | |
1775 |
|
1733 | |||
1776 | IPython.Notebook = Notebook; |
|
1734 | IPython.Notebook = Notebook; | |
1777 |
|
1735 | |||
1778 |
|
1736 | |||
1779 | return IPython; |
|
1737 | return IPython; | |
1780 |
|
1738 | |||
1781 | }(IPython)); |
|
1739 | }(IPython)); | |
1782 |
|
1740 |
@@ -1,557 +1,557 | |||||
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 | // OutputArea |
|
9 | // OutputArea | |
10 | //============================================================================ |
|
10 | //============================================================================ | |
11 |
|
11 | |||
12 | var IPython = (function (IPython) { |
|
12 | var IPython = (function (IPython) { | |
13 | "use strict"; |
|
13 | "use strict"; | |
14 |
|
14 | |||
15 | var utils = IPython.utils; |
|
15 | var utils = IPython.utils; | |
16 |
|
16 | |||
17 | var OutputArea = function (selector, prompt_area) { |
|
17 | var OutputArea = function (selector, prompt_area) { | |
18 | this.selector = selector; |
|
18 | this.selector = selector; | |
19 | this.wrapper = $(selector); |
|
19 | this.wrapper = $(selector); | |
20 | this.outputs = []; |
|
20 | this.outputs = []; | |
21 | this.collapsed = false; |
|
21 | this.collapsed = false; | |
22 | this.scrolled = false; |
|
22 | this.scrolled = false; | |
23 | this.clear_out_timeout = null; |
|
23 | this.clear_out_timeout = null; | |
24 | if (prompt_area === undefined) { |
|
24 | if (prompt_area === undefined) { | |
25 | this.prompt_area = true; |
|
25 | this.prompt_area = true; | |
26 | } else { |
|
26 | } else { | |
27 | this.prompt_area = prompt_area; |
|
27 | this.prompt_area = prompt_area; | |
28 | } |
|
28 | } | |
29 | this.create_elements(); |
|
29 | this.create_elements(); | |
30 | this.style(); |
|
30 | this.style(); | |
31 | this.bind_events(); |
|
31 | this.bind_events(); | |
32 | }; |
|
32 | }; | |
33 |
|
33 | |||
34 | OutputArea.prototype.create_elements = function () { |
|
34 | OutputArea.prototype.create_elements = function () { | |
35 | this.element = $("<div/>"); |
|
35 | this.element = $("<div/>"); | |
36 | this.collapse_button = $("<div/>"); |
|
36 | this.collapse_button = $("<div/>"); | |
37 | this.prompt_overlay = $("<div/>"); |
|
37 | this.prompt_overlay = $("<div/>"); | |
38 | this.wrapper.append(this.prompt_overlay); |
|
38 | this.wrapper.append(this.prompt_overlay); | |
39 | this.wrapper.append(this.element); |
|
39 | this.wrapper.append(this.element); | |
40 | this.wrapper.append(this.collapse_button); |
|
40 | this.wrapper.append(this.collapse_button); | |
41 | }; |
|
41 | }; | |
42 |
|
42 | |||
43 |
|
43 | |||
44 | OutputArea.prototype.style = function () { |
|
44 | OutputArea.prototype.style = function () { | |
45 | this.collapse_button.hide(); |
|
45 | this.collapse_button.hide(); | |
46 | this.prompt_overlay.hide(); |
|
46 | this.prompt_overlay.hide(); | |
47 |
|
47 | |||
48 | this.wrapper.addClass('output_wrapper'); |
|
48 | this.wrapper.addClass('output_wrapper'); | |
49 | this.element.addClass('output vbox'); |
|
49 | this.element.addClass('output vbox'); | |
50 |
|
50 | |||
51 | this.collapse_button.button(); |
|
51 | this.collapse_button.button(); | |
52 | this.collapse_button.addClass('output_collapsed vbox'); |
|
52 | this.collapse_button.addClass('output_collapsed vbox'); | |
53 | this.collapse_button.attr('title', 'click to expand output'); |
|
53 | this.collapse_button.attr('title', 'click to expand output'); | |
54 | this.collapse_button.html('. . .'); |
|
54 | this.collapse_button.html('. . .'); | |
55 |
|
55 | |||
56 | this.prompt_overlay.addClass('out_prompt_overlay prompt'); |
|
56 | this.prompt_overlay.addClass('out_prompt_overlay prompt'); | |
57 | this.prompt_overlay.attr('title', 'click to expand output; double click to hide output'); |
|
57 | this.prompt_overlay.attr('title', 'click to expand output; double click to hide output'); | |
58 |
|
58 | |||
59 | this.collapse(); |
|
59 | this.collapse(); | |
60 | }; |
|
60 | }; | |
61 |
|
61 | |||
62 |
|
62 | |||
63 | OutputArea.prototype._should_scroll = function (lines) { |
|
63 | OutputArea.prototype._should_scroll = function (lines) { | |
64 | if (!lines) { |
|
64 | if (!lines) { | |
65 | lines = 100; |
|
65 | lines = 100; | |
66 | } |
|
66 | } | |
67 | // line-height from http://stackoverflow.com/questions/1185151 |
|
67 | // line-height from http://stackoverflow.com/questions/1185151 | |
68 | var fontSize = this.element.css('font-size'); |
|
68 | var fontSize = this.element.css('font-size'); | |
69 | var lineHeight = Math.floor(parseInt(fontSize.replace('px','')) * 1.5); |
|
69 | var lineHeight = Math.floor(parseInt(fontSize.replace('px','')) * 1.5); | |
70 |
|
70 | |||
71 | return (this.element.height() > lines * lineHeight); |
|
71 | return (this.element.height() > lines * lineHeight); | |
72 | }; |
|
72 | }; | |
73 |
|
73 | |||
74 |
|
74 | |||
75 | OutputArea.prototype.bind_events = function () { |
|
75 | OutputArea.prototype.bind_events = function () { | |
76 | var that = this; |
|
76 | var that = this; | |
77 | this.prompt_overlay.dblclick(function () { that.toggle_output(); }); |
|
77 | this.prompt_overlay.dblclick(function () { that.toggle_output(); }); | |
78 | this.prompt_overlay.click(function () { that.toggle_scroll(); }); |
|
78 | this.prompt_overlay.click(function () { that.toggle_scroll(); }); | |
79 |
|
79 | |||
80 | this.element.resize(function () { |
|
80 | this.element.resize(function () { | |
81 | // FIXME: Firefox on Linux misbehaves, so automatic scrolling is disabled |
|
81 | // FIXME: Firefox on Linux misbehaves, so automatic scrolling is disabled | |
82 | if ( IPython.utils.browser[0] === "Firefox" ) { |
|
82 | if ( IPython.utils.browser[0] === "Firefox" ) { | |
83 | return; |
|
83 | return; | |
84 | } |
|
84 | } | |
85 | // maybe scroll output, |
|
85 | // maybe scroll output, | |
86 | // if it's grown large enough and hasn't already been scrolled. |
|
86 | // if it's grown large enough and hasn't already been scrolled. | |
87 | if ( !that.scrolled && that._should_scroll()) { |
|
87 | if ( !that.scrolled && that._should_scroll()) { | |
88 | that.scroll_area(); |
|
88 | that.scroll_area(); | |
89 | } |
|
89 | } | |
90 | }); |
|
90 | }); | |
91 | this.collapse_button.click(function () { |
|
91 | this.collapse_button.click(function () { | |
92 | that.expand(); |
|
92 | that.expand(); | |
93 | }); |
|
93 | }); | |
94 | this.collapse_button.hover(function () { |
|
94 | this.collapse_button.hover(function () { | |
95 | $(this).addClass("ui-state-hover"); |
|
95 | $(this).addClass("ui-state-hover"); | |
96 | }, function () { |
|
96 | }, function () { | |
97 | $(this).removeClass("ui-state-hover"); |
|
97 | $(this).removeClass("ui-state-hover"); | |
98 | }); |
|
98 | }); | |
99 | }; |
|
99 | }; | |
100 |
|
100 | |||
101 |
|
101 | |||
102 | OutputArea.prototype.collapse = function () { |
|
102 | OutputArea.prototype.collapse = function () { | |
103 | if (!this.collapsed) { |
|
103 | if (!this.collapsed) { | |
104 | this.element.hide(); |
|
104 | this.element.hide(); | |
105 | this.prompt_overlay.hide(); |
|
105 | this.prompt_overlay.hide(); | |
106 | if (this.element.html()){ |
|
106 | if (this.element.html()){ | |
107 | this.collapse_button.show(); |
|
107 | this.collapse_button.show(); | |
108 | } |
|
108 | } | |
109 | this.collapsed = true; |
|
109 | this.collapsed = true; | |
110 | } |
|
110 | } | |
111 | }; |
|
111 | }; | |
112 |
|
112 | |||
113 |
|
113 | |||
114 | OutputArea.prototype.expand = function () { |
|
114 | OutputArea.prototype.expand = function () { | |
115 | if (this.collapsed) { |
|
115 | if (this.collapsed) { | |
116 | this.collapse_button.hide(); |
|
116 | this.collapse_button.hide(); | |
117 | this.element.show(); |
|
117 | this.element.show(); | |
118 | this.prompt_overlay.show(); |
|
118 | this.prompt_overlay.show(); | |
119 | this.collapsed = false; |
|
119 | this.collapsed = false; | |
120 | } |
|
120 | } | |
121 | }; |
|
121 | }; | |
122 |
|
122 | |||
123 |
|
123 | |||
124 | OutputArea.prototype.toggle_output = function () { |
|
124 | OutputArea.prototype.toggle_output = function () { | |
125 | if (this.collapsed) { |
|
125 | if (this.collapsed) { | |
126 | this.expand(); |
|
126 | this.expand(); | |
127 | } else { |
|
127 | } else { | |
128 | this.collapse(); |
|
128 | this.collapse(); | |
129 | } |
|
129 | } | |
130 | }; |
|
130 | }; | |
131 |
|
131 | |||
132 |
|
132 | |||
133 | OutputArea.prototype.scroll_area = function () { |
|
133 | OutputArea.prototype.scroll_area = function () { | |
134 | this.element.addClass('output_scroll'); |
|
134 | this.element.addClass('output_scroll'); | |
135 | this.prompt_overlay.attr('title', 'click to unscroll output; double click to hide'); |
|
135 | this.prompt_overlay.attr('title', 'click to unscroll output; double click to hide'); | |
136 | this.scrolled = true; |
|
136 | this.scrolled = true; | |
137 | }; |
|
137 | }; | |
138 |
|
138 | |||
139 |
|
139 | |||
140 | OutputArea.prototype.unscroll_area = function () { |
|
140 | OutputArea.prototype.unscroll_area = function () { | |
141 | this.element.removeClass('output_scroll'); |
|
141 | this.element.removeClass('output_scroll'); | |
142 | this.prompt_overlay.attr('title', 'click to scroll output; double click to hide'); |
|
142 | this.prompt_overlay.attr('title', 'click to scroll output; double click to hide'); | |
143 | this.scrolled = false; |
|
143 | this.scrolled = false; | |
144 | }; |
|
144 | }; | |
145 |
|
145 | |||
146 |
|
146 | |||
147 | OutputArea.prototype.scroll_if_long = function (lines) { |
|
147 | OutputArea.prototype.scroll_if_long = function (lines) { | |
148 | if (this._should_scroll(lines)) { |
|
148 | if (this._should_scroll(lines)) { | |
149 | // only allow scrolling long-enough output |
|
149 | // only allow scrolling long-enough output | |
150 | this.scroll_area(); |
|
150 | this.scroll_area(); | |
151 | } |
|
151 | } | |
152 | }; |
|
152 | }; | |
153 |
|
153 | |||
154 |
|
154 | |||
155 | OutputArea.prototype.toggle_scroll = function () { |
|
155 | OutputArea.prototype.toggle_scroll = function () { | |
156 | if (this.scrolled) { |
|
156 | if (this.scrolled) { | |
157 | this.unscroll_area(); |
|
157 | this.unscroll_area(); | |
158 | } else { |
|
158 | } else { | |
159 | // only allow scrolling long-enough output |
|
159 | // only allow scrolling long-enough output | |
160 | this.scroll_if_long(20); |
|
160 | this.scroll_if_long(20); | |
161 | } |
|
161 | } | |
162 | }; |
|
162 | }; | |
163 |
|
163 | |||
164 |
|
164 | |||
165 | // typeset with MathJax if MathJax is available |
|
165 | // typeset with MathJax if MathJax is available | |
166 | OutputArea.prototype.typeset = function () { |
|
166 | OutputArea.prototype.typeset = function () { | |
167 | if (window.MathJax){ |
|
167 | if (window.MathJax){ | |
168 | MathJax.Hub.Queue(["Typeset",MathJax.Hub]); |
|
168 | MathJax.Hub.Queue(["Typeset",MathJax.Hub]); | |
169 | } |
|
169 | } | |
170 | }; |
|
170 | }; | |
171 |
|
171 | |||
172 |
|
172 | |||
173 | OutputArea.prototype.handle_output = function (msg_type, content) { |
|
173 | OutputArea.prototype.handle_output = function (msg_type, content) { | |
174 | var json = {}; |
|
174 | var json = {}; | |
175 | json.output_type = msg_type; |
|
175 | json.output_type = msg_type; | |
176 | if (msg_type === "stream") { |
|
176 | if (msg_type === "stream") { | |
177 | json.text = content.data; |
|
177 | json.text = content.data; | |
178 | json.stream = content.name; |
|
178 | json.stream = content.name; | |
179 | } else if (msg_type === "display_data") { |
|
179 | } else if (msg_type === "display_data") { | |
180 | json = this.convert_mime_types(json, content.data); |
|
180 | json = this.convert_mime_types(json, content.data); | |
181 | } else if (msg_type === "pyout") { |
|
181 | } else if (msg_type === "pyout") { | |
182 | json.prompt_number = content.execution_count; |
|
182 | json.prompt_number = content.execution_count; | |
183 | json = this.convert_mime_types(json, content.data); |
|
183 | json = this.convert_mime_types(json, content.data); | |
184 | } else if (msg_type === "pyerr") { |
|
184 | } else if (msg_type === "pyerr") { | |
185 | json.ename = content.ename; |
|
185 | json.ename = content.ename; | |
186 | json.evalue = content.evalue; |
|
186 | json.evalue = content.evalue; | |
187 | json.traceback = content.traceback; |
|
187 | json.traceback = content.traceback; | |
188 | } |
|
188 | } | |
189 | // append with dynamic=true |
|
189 | // append with dynamic=true | |
190 | this.append_output(json, true); |
|
190 | this.append_output(json, true); | |
191 | }; |
|
191 | }; | |
192 |
|
192 | |||
193 |
|
193 | |||
194 | OutputArea.prototype.convert_mime_types = function (json, data) { |
|
194 | OutputArea.prototype.convert_mime_types = function (json, data) { | |
195 | if (data['text/plain'] !== undefined) { |
|
195 | if (data['text/plain'] !== undefined) { | |
196 | json.text = data['text/plain']; |
|
196 | json.text = data['text/plain']; | |
197 | } |
|
197 | } | |
198 | if (data['text/html'] !== undefined) { |
|
198 | if (data['text/html'] !== undefined) { | |
199 | json.html = data['text/html']; |
|
199 | json.html = data['text/html']; | |
200 | } |
|
200 | } | |
201 | if (data['image/svg+xml'] !== undefined) { |
|
201 | if (data['image/svg+xml'] !== undefined) { | |
202 | json.svg = data['image/svg+xml']; |
|
202 | json.svg = data['image/svg+xml']; | |
203 | } |
|
203 | } | |
204 | if (data['image/png'] !== undefined) { |
|
204 | if (data['image/png'] !== undefined) { | |
205 | json.png = data['image/png']; |
|
205 | json.png = data['image/png']; | |
206 | } |
|
206 | } | |
207 | if (data['image/jpeg'] !== undefined) { |
|
207 | if (data['image/jpeg'] !== undefined) { | |
208 | json.jpeg = data['image/jpeg']; |
|
208 | json.jpeg = data['image/jpeg']; | |
209 | } |
|
209 | } | |
210 | if (data['text/latex'] !== undefined) { |
|
210 | if (data['text/latex'] !== undefined) { | |
211 | json.latex = data['text/latex']; |
|
211 | json.latex = data['text/latex']; | |
212 | } |
|
212 | } | |
213 | if (data['application/json'] !== undefined) { |
|
213 | if (data['application/json'] !== undefined) { | |
214 | json.json = data['application/json']; |
|
214 | json.json = data['application/json']; | |
215 | } |
|
215 | } | |
216 | if (data['application/javascript'] !== undefined) { |
|
216 | if (data['application/javascript'] !== undefined) { | |
217 | json.javascript = data['application/javascript']; |
|
217 | json.javascript = data['application/javascript']; | |
218 | } |
|
218 | } | |
219 | return json; |
|
219 | return json; | |
220 | }; |
|
220 | }; | |
221 |
|
221 | |||
222 |
|
222 | |||
223 | OutputArea.prototype.append_output = function (json, dynamic) { |
|
223 | OutputArea.prototype.append_output = function (json, dynamic) { | |
224 | // If dynamic is true, javascript output will be eval'd. |
|
224 | // If dynamic is true, javascript output will be eval'd. | |
225 | this.expand(); |
|
225 | this.expand(); | |
226 | this.flush_clear_timeout(); |
|
226 | this.flush_clear_timeout(); | |
227 | if (json.output_type === 'pyout') { |
|
227 | if (json.output_type === 'pyout') { | |
228 | this.append_pyout(json, dynamic); |
|
228 | this.append_pyout(json, dynamic); | |
229 | } else if (json.output_type === 'pyerr') { |
|
229 | } else if (json.output_type === 'pyerr') { | |
230 | this.append_pyerr(json); |
|
230 | this.append_pyerr(json); | |
231 | } else if (json.output_type === 'display_data') { |
|
231 | } else if (json.output_type === 'display_data') { | |
232 | this.append_display_data(json, dynamic); |
|
232 | this.append_display_data(json, dynamic); | |
233 | } else if (json.output_type === 'stream') { |
|
233 | } else if (json.output_type === 'stream') { | |
234 | this.append_stream(json); |
|
234 | this.append_stream(json); | |
235 | } |
|
235 | } | |
236 | this.outputs.push(json); |
|
236 | this.outputs.push(json); | |
237 | var that = this; |
|
237 | var that = this; | |
238 | setTimeout(function(){that.element.trigger('resize');}, 100); |
|
238 | setTimeout(function(){that.element.trigger('resize');}, 100); | |
239 | }; |
|
239 | }; | |
240 |
|
240 | |||
241 |
|
241 | |||
242 | OutputArea.prototype.create_output_area = function () { |
|
242 | OutputArea.prototype.create_output_area = function () { | |
243 | var oa = $("<div/>").addClass("output_area"); |
|
243 | var oa = $("<div/>").addClass("output_area"); | |
244 | if (this.prompt_area) { |
|
244 | if (this.prompt_area) { | |
245 | oa.append($('<div/>').addClass('prompt')); |
|
245 | oa.append($('<div/>').addClass('prompt')); | |
246 | } |
|
246 | } | |
247 | return oa; |
|
247 | return oa; | |
248 | }; |
|
248 | }; | |
249 |
|
249 | |||
250 |
|
250 | |||
251 | OutputArea.prototype.append_pyout = function (json, dynamic) { |
|
251 | OutputArea.prototype.append_pyout = function (json, dynamic) { | |
252 | var n = json.prompt_number || ' '; |
|
252 | var n = json.prompt_number || ' '; | |
253 | var toinsert = this.create_output_area(); |
|
253 | var toinsert = this.create_output_area(); | |
254 | if (this.prompt_area) { |
|
254 | if (this.prompt_area) { | |
255 | toinsert.find('div.prompt').addClass('output_prompt').html('Out[' + n + ']:'); |
|
255 | toinsert.find('div.prompt').addClass('output_prompt').html('Out[' + n + ']:'); | |
256 | } |
|
256 | } | |
257 | this.append_mime_type(json, toinsert, dynamic); |
|
257 | this.append_mime_type(json, toinsert, dynamic); | |
258 | this.element.append(toinsert); |
|
258 | this.element.append(toinsert); | |
259 | // If we just output latex, typeset it. |
|
259 | // If we just output latex, typeset it. | |
260 | if ((json.latex !== undefined) || (json.html !== undefined)) { |
|
260 | if ((json.latex !== undefined) || (json.html !== undefined)) { | |
261 | this.typeset(); |
|
261 | this.typeset(); | |
262 | } |
|
262 | } | |
263 | }; |
|
263 | }; | |
264 |
|
264 | |||
265 |
|
265 | |||
266 | OutputArea.prototype.append_pyerr = function (json) { |
|
266 | OutputArea.prototype.append_pyerr = function (json) { | |
267 | var tb = json.traceback; |
|
267 | var tb = json.traceback; | |
268 | if (tb !== undefined && tb.length > 0) { |
|
268 | if (tb !== undefined && tb.length > 0) { | |
269 | var s = ''; |
|
269 | var s = ''; | |
270 | var len = tb.length; |
|
270 | var len = tb.length; | |
271 | for (var i=0; i<len; i++) { |
|
271 | for (var i=0; i<len; i++) { | |
272 | s = s + tb[i] + '\n'; |
|
272 | s = s + tb[i] + '\n'; | |
273 | } |
|
273 | } | |
274 | s = s + '\n'; |
|
274 | s = s + '\n'; | |
275 | var toinsert = this.create_output_area(); |
|
275 | var toinsert = this.create_output_area(); | |
276 | this.append_text(s, toinsert); |
|
276 | this.append_text(s, toinsert); | |
277 | this.element.append(toinsert); |
|
277 | this.element.append(toinsert); | |
278 | } |
|
278 | } | |
279 | }; |
|
279 | }; | |
280 |
|
280 | |||
281 |
|
281 | |||
282 | OutputArea.prototype.append_stream = function (json) { |
|
282 | OutputArea.prototype.append_stream = function (json) { | |
283 | // temporary fix: if stream undefined (json file written prior to this patch), |
|
283 | // temporary fix: if stream undefined (json file written prior to this patch), | |
284 | // default to most likely stdout: |
|
284 | // default to most likely stdout: | |
285 | if (json.stream == undefined){ |
|
285 | if (json.stream == undefined){ | |
286 | json.stream = 'stdout'; |
|
286 | json.stream = 'stdout'; | |
287 | } |
|
287 | } | |
288 | var text = json.text; |
|
288 | var text = json.text; | |
289 | var subclass = "output_"+json.stream; |
|
289 | var subclass = "output_"+json.stream; | |
290 | if (this.outputs.length > 0){ |
|
290 | if (this.outputs.length > 0){ | |
291 | // have at least one output to consider |
|
291 | // have at least one output to consider | |
292 | var last = this.outputs[this.outputs.length-1]; |
|
292 | var last = this.outputs[this.outputs.length-1]; | |
293 | if (last.output_type == 'stream' && json.stream == last.stream){ |
|
293 | if (last.output_type == 'stream' && json.stream == last.stream){ | |
294 | // latest output was in the same stream, |
|
294 | // latest output was in the same stream, | |
295 | // so append directly into its pre tag |
|
295 | // so append directly into its pre tag | |
296 | // escape ANSI & HTML specials: |
|
296 | // escape ANSI & HTML specials: | |
297 | var pre = this.element.find('div.'+subclass).last().find('pre'); |
|
297 | var pre = this.element.find('div.'+subclass).last().find('pre'); | |
298 | var html = utils.fixCarriageReturn( |
|
298 | var html = utils.fixCarriageReturn( | |
299 | pre.html() + utils.fixConsole(text)); |
|
299 | pre.html() + utils.fixConsole(text)); | |
300 | pre.html(html); |
|
300 | pre.html(html); | |
301 | return; |
|
301 | return; | |
302 | } |
|
302 | } | |
303 | } |
|
303 | } | |
304 |
|
304 | |||
305 | if (!text.replace("\r", "")) { |
|
305 | if (!text.replace("\r", "")) { | |
306 | // text is nothing (empty string, \r, etc.) |
|
306 | // text is nothing (empty string, \r, etc.) | |
307 | // so don't append any elements, which might add undesirable space |
|
307 | // so don't append any elements, which might add undesirable space | |
308 | return; |
|
308 | return; | |
309 | } |
|
309 | } | |
310 |
|
310 | |||
311 | // If we got here, attach a new div |
|
311 | // If we got here, attach a new div | |
312 | var toinsert = this.create_output_area(); |
|
312 | var toinsert = this.create_output_area(); | |
313 | this.append_text(text, toinsert, "output_stream "+subclass); |
|
313 | this.append_text(text, toinsert, "output_stream "+subclass); | |
314 | this.element.append(toinsert); |
|
314 | this.element.append(toinsert); | |
315 | }; |
|
315 | }; | |
316 |
|
316 | |||
317 |
|
317 | |||
318 | OutputArea.prototype.append_display_data = function (json, dynamic) { |
|
318 | OutputArea.prototype.append_display_data = function (json, dynamic) { | |
319 | var toinsert = this.create_output_area(); |
|
319 | var toinsert = this.create_output_area(); | |
320 | this.append_mime_type(json, toinsert, dynamic); |
|
320 | this.append_mime_type(json, toinsert, dynamic); | |
321 | this.element.append(toinsert); |
|
321 | this.element.append(toinsert); | |
322 | // If we just output latex, typeset it. |
|
322 | // If we just output latex, typeset it. | |
323 | if ( (json.latex !== undefined) || (json.html !== undefined) ) { |
|
323 | if ( (json.latex !== undefined) || (json.html !== undefined) ) { | |
324 | this.typeset(); |
|
324 | this.typeset(); | |
325 | } |
|
325 | } | |
326 | }; |
|
326 | }; | |
327 |
|
327 | |||
328 |
OutputArea.display_order = ['javascript' |
|
328 | OutputArea.display_order = ['javascript','latex','svg','png','jpeg','text']; | |
329 |
|
329 | |||
330 | OutputArea.prototype.append_mime_type = function (json, element, dynamic) { |
|
330 | OutputArea.prototype.append_mime_type = function (json, element, dynamic) { | |
331 | for(var type_i in OutputArea.display_order){ |
|
331 | for(var type_i in OutputArea.display_order){ | |
332 | var type = OutputArea.display_order[type_i]; |
|
332 | var type = OutputArea.display_order[type_i]; | |
333 | if(json[type] != undefined ){ |
|
333 | if(json[type] != undefined ){ | |
334 | if(type == 'javascript'){ |
|
334 | if(type == 'javascript'){ | |
335 | if (dynamic) { |
|
335 | if (dynamic) { | |
336 | this.append_javascript(json.javascript, element, dynamic); |
|
336 | this.append_javascript(json.javascript, element, dynamic); | |
337 | } |
|
337 | } | |
338 | } else { |
|
338 | } else { | |
339 | this['append_'+type](json[type], element); |
|
339 | this['append_'+type](json[type], element); | |
340 | } |
|
340 | } | |
341 | return; |
|
341 | return; | |
342 | } |
|
342 | } | |
343 | } |
|
343 | } | |
344 | }; |
|
344 | }; | |
345 |
|
345 | |||
346 |
|
346 | |||
347 | OutputArea.prototype.append_html = function (html, element) { |
|
347 | OutputArea.prototype.append_html = function (html, element) { | |
348 | var toinsert = $("<div/>").addClass("output_subarea output_html rendered_html"); |
|
348 | var toinsert = $("<div/>").addClass("output_subarea output_html rendered_html"); | |
349 | toinsert.append(html); |
|
349 | toinsert.append(html); | |
350 | element.append(toinsert); |
|
350 | element.append(toinsert); | |
351 | }; |
|
351 | }; | |
352 |
|
352 | |||
353 |
|
353 | |||
354 | OutputArea.prototype.append_javascript = function (js, container) { |
|
354 | OutputArea.prototype.append_javascript = function (js, container) { | |
355 | // We just eval the JS code, element appears in the local scope. |
|
355 | // We just eval the JS code, element appears in the local scope. | |
356 | var element = $("<div/>").addClass("output_subarea"); |
|
356 | var element = $("<div/>").addClass("output_subarea"); | |
357 | container.append(element); |
|
357 | container.append(element); | |
358 | // Div for js shouldn't be drawn, as it will add empty height to the area. |
|
358 | // Div for js shouldn't be drawn, as it will add empty height to the area. | |
359 | container.hide(); |
|
359 | container.hide(); | |
360 | // If the Javascript appends content to `element` that should be drawn, then |
|
360 | // If the Javascript appends content to `element` that should be drawn, then | |
361 | // it must also call `container.show()`. |
|
361 | // it must also call `container.show()`. | |
362 | try { |
|
362 | try { | |
363 | eval(js); |
|
363 | eval(js); | |
364 | } catch(err) { |
|
364 | } catch(err) { | |
365 | console.log('Error in Javascript!'); |
|
365 | console.log('Error in Javascript!'); | |
366 | console.log(err); |
|
366 | console.log(err); | |
367 | container.show(); |
|
367 | container.show(); | |
368 | element.append($('<div/>') |
|
368 | element.append($('<div/>') | |
369 | .html("Error in Javascript !<br/>"+ |
|
369 | .html("Error in Javascript !<br/>"+ | |
370 | err.toString()+ |
|
370 | err.toString()+ | |
371 | '<br/>See your browser Javascript console for more details.') |
|
371 | '<br/>See your browser Javascript console for more details.') | |
372 | .addClass('js-error') |
|
372 | .addClass('js-error') | |
373 | ); |
|
373 | ); | |
374 | } |
|
374 | } | |
375 | }; |
|
375 | }; | |
376 |
|
376 | |||
377 |
|
377 | |||
378 | OutputArea.prototype.append_text = function (data, element, extra_class) { |
|
378 | OutputArea.prototype.append_text = function (data, element, extra_class) { | |
379 | var toinsert = $("<div/>").addClass("output_subarea output_text"); |
|
379 | var toinsert = $("<div/>").addClass("output_subarea output_text"); | |
380 | // escape ANSI & HTML specials in plaintext: |
|
380 | // escape ANSI & HTML specials in plaintext: | |
381 | data = utils.fixConsole(data); |
|
381 | data = utils.fixConsole(data); | |
382 | data = utils.fixCarriageReturn(data); |
|
382 | data = utils.fixCarriageReturn(data); | |
383 | data = utils.autoLinkUrls(data); |
|
383 | data = utils.autoLinkUrls(data); | |
384 | if (extra_class){ |
|
384 | if (extra_class){ | |
385 | toinsert.addClass(extra_class); |
|
385 | toinsert.addClass(extra_class); | |
386 | } |
|
386 | } | |
387 | toinsert.append($("<pre/>").html(data)); |
|
387 | toinsert.append($("<pre/>").html(data)); | |
388 | element.append(toinsert); |
|
388 | element.append(toinsert); | |
389 | }; |
|
389 | }; | |
390 |
|
390 | |||
391 |
|
391 | |||
392 | OutputArea.prototype.append_svg = function (svg, element) { |
|
392 | OutputArea.prototype.append_svg = function (svg, element) { | |
393 | var toinsert = $("<div/>").addClass("output_subarea output_svg"); |
|
393 | var toinsert = $("<div/>").addClass("output_subarea output_svg"); | |
394 | toinsert.append(svg); |
|
394 | toinsert.append(svg); | |
395 | element.append(toinsert); |
|
395 | element.append(toinsert); | |
396 | }; |
|
396 | }; | |
397 |
|
397 | |||
398 |
|
398 | |||
399 | OutputArea.prototype._dblclick_to_reset_size = function (img) { |
|
399 | OutputArea.prototype._dblclick_to_reset_size = function (img) { | |
400 | // schedule wrapping image in resizable after a delay, |
|
400 | // schedule wrapping image in resizable after a delay, | |
401 | // so we don't end up calling resize on a zero-size object |
|
401 | // so we don't end up calling resize on a zero-size object | |
402 | var that = this; |
|
402 | var that = this; | |
403 | setTimeout(function () { |
|
403 | setTimeout(function () { | |
404 | var h0 = img.height(); |
|
404 | var h0 = img.height(); | |
405 | var w0 = img.width(); |
|
405 | var w0 = img.width(); | |
406 | if (!(h0 && w0)) { |
|
406 | if (!(h0 && w0)) { | |
407 | // zero size, schedule another timeout |
|
407 | // zero size, schedule another timeout | |
408 | that._dblclick_to_reset_size(img); |
|
408 | that._dblclick_to_reset_size(img); | |
409 | return; |
|
409 | return; | |
410 | } |
|
410 | } | |
411 | img.resizable({ |
|
411 | img.resizable({ | |
412 | aspectRatio: true, |
|
412 | aspectRatio: true, | |
413 | autoHide: true |
|
413 | autoHide: true | |
414 | }); |
|
414 | }); | |
415 | img.dblclick(function () { |
|
415 | img.dblclick(function () { | |
416 | // resize wrapper & image together for some reason: |
|
416 | // resize wrapper & image together for some reason: | |
417 | img.parent().height(h0); |
|
417 | img.parent().height(h0); | |
418 | img.height(h0); |
|
418 | img.height(h0); | |
419 | img.parent().width(w0); |
|
419 | img.parent().width(w0); | |
420 | img.width(w0); |
|
420 | img.width(w0); | |
421 | }); |
|
421 | }); | |
422 | }, 250); |
|
422 | }, 250); | |
423 | }; |
|
423 | }; | |
424 |
|
424 | |||
425 |
|
425 | |||
426 | OutputArea.prototype.append_png = function (png, element) { |
|
426 | OutputArea.prototype.append_png = function (png, element) { | |
427 | var toinsert = $("<div/>").addClass("output_subarea output_png"); |
|
427 | var toinsert = $("<div/>").addClass("output_subarea output_png"); | |
428 | var img = $("<img/>").attr('src','data:image/png;base64,'+png); |
|
428 | var img = $("<img/>").attr('src','data:image/png;base64,'+png); | |
429 | this._dblclick_to_reset_size(img); |
|
429 | this._dblclick_to_reset_size(img); | |
430 | toinsert.append(img); |
|
430 | toinsert.append(img); | |
431 | element.append(toinsert); |
|
431 | element.append(toinsert); | |
432 | }; |
|
432 | }; | |
433 |
|
433 | |||
434 |
|
434 | |||
435 | OutputArea.prototype.append_jpeg = function (jpeg, element) { |
|
435 | OutputArea.prototype.append_jpeg = function (jpeg, element) { | |
436 | var toinsert = $("<div/>").addClass("output_subarea output_jpeg"); |
|
436 | var toinsert = $("<div/>").addClass("output_subarea output_jpeg"); | |
437 | var img = $("<img/>").attr('src','data:image/jpeg;base64,'+jpeg); |
|
437 | var img = $("<img/>").attr('src','data:image/jpeg;base64,'+jpeg); | |
438 | this._dblclick_to_reset_size(img); |
|
438 | this._dblclick_to_reset_size(img); | |
439 | toinsert.append(img); |
|
439 | toinsert.append(img); | |
440 | element.append(toinsert); |
|
440 | element.append(toinsert); | |
441 | }; |
|
441 | }; | |
442 |
|
442 | |||
443 |
|
443 | |||
444 | OutputArea.prototype.append_latex = function (latex, element) { |
|
444 | OutputArea.prototype.append_latex = function (latex, element) { | |
445 | // This method cannot do the typesetting because the latex first has to |
|
445 | // This method cannot do the typesetting because the latex first has to | |
446 | // be on the page. |
|
446 | // be on the page. | |
447 | var toinsert = $("<div/>").addClass("output_subarea output_latex"); |
|
447 | var toinsert = $("<div/>").addClass("output_subarea output_latex"); | |
448 | toinsert.append(latex); |
|
448 | toinsert.append(latex); | |
449 | element.append(toinsert); |
|
449 | element.append(toinsert); | |
450 | }; |
|
450 | }; | |
451 |
|
451 | |||
452 |
|
452 | |||
453 | OutputArea.prototype.handle_clear_output = function (content) { |
|
453 | OutputArea.prototype.handle_clear_output = function (content) { | |
454 | this.clear_output(content.stdout, content.stderr, content.other); |
|
454 | this.clear_output(content.stdout, content.stderr, content.other); | |
455 | }; |
|
455 | }; | |
456 |
|
456 | |||
457 |
|
457 | |||
458 | OutputArea.prototype.clear_output = function (stdout, stderr, other) { |
|
458 | OutputArea.prototype.clear_output = function (stdout, stderr, other) { | |
459 | var that = this; |
|
459 | var that = this; | |
460 | if (this.clear_out_timeout != null){ |
|
460 | if (this.clear_out_timeout != null){ | |
461 | // fire previous pending clear *immediately* |
|
461 | // fire previous pending clear *immediately* | |
462 | clearTimeout(this.clear_out_timeout); |
|
462 | clearTimeout(this.clear_out_timeout); | |
463 | this.clear_out_timeout = null; |
|
463 | this.clear_out_timeout = null; | |
464 | this.clear_output_callback(this._clear_stdout, this._clear_stderr, this._clear_other); |
|
464 | this.clear_output_callback(this._clear_stdout, this._clear_stderr, this._clear_other); | |
465 | } |
|
465 | } | |
466 | // store flags for flushing the timeout |
|
466 | // store flags for flushing the timeout | |
467 | this._clear_stdout = stdout; |
|
467 | this._clear_stdout = stdout; | |
468 | this._clear_stderr = stderr; |
|
468 | this._clear_stderr = stderr; | |
469 | this._clear_other = other; |
|
469 | this._clear_other = other; | |
470 | this.clear_out_timeout = setTimeout(function() { |
|
470 | this.clear_out_timeout = setTimeout(function() { | |
471 | // really clear timeout only after a short delay |
|
471 | // really clear timeout only after a short delay | |
472 | // this reduces flicker in 'clear_output; print' cases |
|
472 | // this reduces flicker in 'clear_output; print' cases | |
473 | that.clear_out_timeout = null; |
|
473 | that.clear_out_timeout = null; | |
474 | that._clear_stdout = that._clear_stderr = that._clear_other = null; |
|
474 | that._clear_stdout = that._clear_stderr = that._clear_other = null; | |
475 | that.clear_output_callback(stdout, stderr, other); |
|
475 | that.clear_output_callback(stdout, stderr, other); | |
476 | }, 500 |
|
476 | }, 500 | |
477 | ); |
|
477 | ); | |
478 | }; |
|
478 | }; | |
479 |
|
479 | |||
480 |
|
480 | |||
481 | OutputArea.prototype.clear_output_callback = function (stdout, stderr, other) { |
|
481 | OutputArea.prototype.clear_output_callback = function (stdout, stderr, other) { | |
482 | var output_div = this.element; |
|
482 | var output_div = this.element; | |
483 |
|
483 | |||
484 | if (stdout && stderr && other){ |
|
484 | if (stdout && stderr && other){ | |
485 | // clear all, no need for logic |
|
485 | // clear all, no need for logic | |
486 | output_div.html(""); |
|
486 | output_div.html(""); | |
487 | this.outputs = []; |
|
487 | this.outputs = []; | |
488 | this.unscroll_area(); |
|
488 | this.unscroll_area(); | |
489 | return; |
|
489 | return; | |
490 | } |
|
490 | } | |
491 | // remove html output |
|
491 | // remove html output | |
492 | // each output_subarea that has an identifying class is in an output_area |
|
492 | // each output_subarea that has an identifying class is in an output_area | |
493 | // which is the element to be removed. |
|
493 | // which is the element to be removed. | |
494 | if (stdout) { |
|
494 | if (stdout) { | |
495 | output_div.find("div.output_stdout").parent().remove(); |
|
495 | output_div.find("div.output_stdout").parent().remove(); | |
496 | } |
|
496 | } | |
497 | if (stderr) { |
|
497 | if (stderr) { | |
498 | output_div.find("div.output_stderr").parent().remove(); |
|
498 | output_div.find("div.output_stderr").parent().remove(); | |
499 | } |
|
499 | } | |
500 | if (other) { |
|
500 | if (other) { | |
501 | output_div.find("div.output_subarea").not("div.output_stderr").not("div.output_stdout").parent().remove(); |
|
501 | output_div.find("div.output_subarea").not("div.output_stderr").not("div.output_stdout").parent().remove(); | |
502 | } |
|
502 | } | |
503 | this.unscroll_area(); |
|
503 | this.unscroll_area(); | |
504 |
|
504 | |||
505 | // remove cleared outputs from JSON list: |
|
505 | // remove cleared outputs from JSON list: | |
506 | for (var i = this.outputs.length - 1; i >= 0; i--) { |
|
506 | for (var i = this.outputs.length - 1; i >= 0; i--) { | |
507 | var out = this.outputs[i]; |
|
507 | var out = this.outputs[i]; | |
508 | var output_type = out.output_type; |
|
508 | var output_type = out.output_type; | |
509 | if (output_type == "display_data" && other) { |
|
509 | if (output_type == "display_data" && other) { | |
510 | this.outputs.splice(i,1); |
|
510 | this.outputs.splice(i,1); | |
511 | } else if (output_type == "stream") { |
|
511 | } else if (output_type == "stream") { | |
512 | if (stdout && out.stream == "stdout") { |
|
512 | if (stdout && out.stream == "stdout") { | |
513 | this.outputs.splice(i,1); |
|
513 | this.outputs.splice(i,1); | |
514 | } else if (stderr && out.stream == "stderr") { |
|
514 | } else if (stderr && out.stream == "stderr") { | |
515 | this.outputs.splice(i,1); |
|
515 | this.outputs.splice(i,1); | |
516 | } |
|
516 | } | |
517 | } |
|
517 | } | |
518 | } |
|
518 | } | |
519 | }; |
|
519 | }; | |
520 |
|
520 | |||
521 |
|
521 | |||
522 | OutputArea.prototype.flush_clear_timeout = function() { |
|
522 | OutputArea.prototype.flush_clear_timeout = function() { | |
523 | var output_div = this.element; |
|
523 | var output_div = this.element; | |
524 | if (this.clear_out_timeout){ |
|
524 | if (this.clear_out_timeout){ | |
525 | clearTimeout(this.clear_out_timeout); |
|
525 | clearTimeout(this.clear_out_timeout); | |
526 | this.clear_out_timeout = null; |
|
526 | this.clear_out_timeout = null; | |
527 | this.clear_output_callback(this._clear_stdout, this._clear_stderr, this._clear_other); |
|
527 | this.clear_output_callback(this._clear_stdout, this._clear_stderr, this._clear_other); | |
528 | } |
|
528 | } | |
529 | }; |
|
529 | }; | |
530 |
|
530 | |||
531 |
|
531 | |||
532 | // JSON serialization |
|
532 | // JSON serialization | |
533 |
|
533 | |||
534 | OutputArea.prototype.fromJSON = function (outputs) { |
|
534 | OutputArea.prototype.fromJSON = function (outputs) { | |
535 | var len = outputs.length; |
|
535 | var len = outputs.length; | |
536 | for (var i=0; i<len; i++) { |
|
536 | for (var i=0; i<len; i++) { | |
537 | // append with dynamic=false. |
|
537 | // append with dynamic=false. | |
538 | this.append_output(outputs[i], false); |
|
538 | this.append_output(outputs[i], false); | |
539 | } |
|
539 | } | |
540 | }; |
|
540 | }; | |
541 |
|
541 | |||
542 |
|
542 | |||
543 | OutputArea.prototype.toJSON = function () { |
|
543 | OutputArea.prototype.toJSON = function () { | |
544 | var outputs = []; |
|
544 | var outputs = []; | |
545 | var len = this.outputs.length; |
|
545 | var len = this.outputs.length; | |
546 | for (var i=0; i<len; i++) { |
|
546 | for (var i=0; i<len; i++) { | |
547 | outputs[i] = this.outputs[i]; |
|
547 | outputs[i] = this.outputs[i]; | |
548 | } |
|
548 | } | |
549 | return outputs; |
|
549 | return outputs; | |
550 | }; |
|
550 | }; | |
551 |
|
551 | |||
552 |
|
552 | |||
553 | IPython.OutputArea = OutputArea; |
|
553 | IPython.OutputArea = OutputArea; | |
554 |
|
554 | |||
555 | return IPython; |
|
555 | return IPython; | |
556 |
|
556 | |||
557 | }(IPython)); |
|
557 | }(IPython)); |
@@ -1,598 +1,558 | |||||
1 | //---------------------------------------------------------------------------- |
|
1 | //---------------------------------------------------------------------------- | |
2 | // Copyright (C) 2008-2012 The IPython Development Team |
|
2 | // Copyright (C) 2008-2012 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 | // TextCell |
|
9 | // TextCell | |
10 | //============================================================================ |
|
10 | //============================================================================ | |
11 |
|
11 | |||
12 |
|
12 | |||
13 |
|
13 | |||
14 | /** |
|
14 | /** | |
15 | A module that allow to create different type of Text Cell |
|
15 | A module that allow to create different type of Text Cell | |
16 | @module IPython |
|
16 | @module IPython | |
17 | @namespace IPython |
|
17 | @namespace IPython | |
18 | */ |
|
18 | */ | |
19 | var IPython = (function (IPython) { |
|
19 | var IPython = (function (IPython) { | |
20 |
|
20 | |||
21 | // TextCell base class |
|
21 | // TextCell base class | |
22 | var key = IPython.utils.keycodes; |
|
22 | var key = IPython.utils.keycodes; | |
23 |
|
23 | |||
24 | /** |
|
24 | /** | |
25 | * Construct a new TextCell, codemirror mode is by default 'htmlmixed', and cell type is 'text' |
|
25 | * Construct a new TextCell, codemirror mode is by default 'htmlmixed', and cell type is 'text' | |
26 | * cell start as not redered. |
|
26 | * cell start as not redered. | |
27 | * |
|
27 | * | |
28 | * @class TextCell |
|
28 | * @class TextCell | |
29 | * @constructor TextCell |
|
29 | * @constructor TextCell | |
30 | * @extend Ipython.Cell |
|
30 | * @extend Ipython.Cell | |
31 | * @param {object|undefined} [options] |
|
31 | * @param {object|undefined} [options] | |
32 | * @param [options.cm_config] {object} config to pass to CodeMirror, will extend/overwrite default config |
|
32 | * @param [options.cm_config] {object} config to pass to CodeMirror, will extend/overwrite default config | |
33 | * @param [options.placeholder] {string} default string to use when souce in empty for rendering (only use in some TextCell subclass) |
|
33 | * @param [options.placeholder] {string} default string to use when souce in empty for rendering (only use in some TextCell subclass) | |
34 | */ |
|
34 | */ | |
35 | var TextCell = function (options) { |
|
35 | var TextCell = function (options) { | |
36 | // in all TextCell/Cell subclasses |
|
36 | // in all TextCell/Cell subclasses | |
37 | // do not assign most of members here, just pass it down |
|
37 | // do not assign most of members here, just pass it down | |
38 | // in the options dict potentially overwriting what you wish. |
|
38 | // in the options dict potentially overwriting what you wish. | |
39 | // they will be assigned in the base class. |
|
39 | // they will be assigned in the base class. | |
40 |
|
40 | |||
41 | // we cannot put this as a class key as it has handle to "this". |
|
41 | // we cannot put this as a class key as it has handle to "this". | |
42 | var cm_overwrite_options = { |
|
42 | var cm_overwrite_options = { | |
43 | onKeyEvent: $.proxy(this.handle_codemirror_keyevent,this) |
|
43 | onKeyEvent: $.proxy(this.handle_codemirror_keyevent,this) | |
44 | }; |
|
44 | }; | |
45 |
|
45 | |||
46 | options = this.mergeopt(TextCell,options,{cm_config:cm_overwrite_options}); |
|
46 | options = this.mergeopt(TextCell,options,{cm_config:cm_overwrite_options}); | |
47 |
|
47 | |||
48 | IPython.Cell.apply(this, [options]); |
|
48 | IPython.Cell.apply(this, [options]); | |
49 |
|
49 | |||
50 |
|
50 | |||
51 | this.rendered = false; |
|
51 | this.rendered = false; | |
52 | this.cell_type = this.cell_type || 'text'; |
|
52 | this.cell_type = this.cell_type || 'text'; | |
53 | }; |
|
53 | }; | |
54 |
|
54 | |||
55 | TextCell.prototype = new IPython.Cell(); |
|
55 | TextCell.prototype = new IPython.Cell(); | |
56 |
|
56 | |||
57 | TextCell.options_default = { |
|
57 | TextCell.options_default = { | |
58 | cm_config : { |
|
58 | cm_config : { | |
59 | extraKeys: {"Tab": "indentMore","Shift-Tab" : "indentLess"}, |
|
59 | extraKeys: {"Tab": "indentMore","Shift-Tab" : "indentLess"}, | |
60 | mode: 'htmlmixed', |
|
60 | mode: 'htmlmixed', | |
61 | lineWrapping : true, |
|
61 | lineWrapping : true, | |
62 | } |
|
62 | } | |
63 | }; |
|
63 | }; | |
64 |
|
64 | |||
65 |
|
65 | |||
66 |
|
66 | |||
67 | /** |
|
67 | /** | |
68 | * Create the DOM element of the TextCell |
|
68 | * Create the DOM element of the TextCell | |
69 | * @method create_element |
|
69 | * @method create_element | |
70 | * @private |
|
70 | * @private | |
71 | */ |
|
71 | */ | |
72 | TextCell.prototype.create_element = function () { |
|
72 | TextCell.prototype.create_element = function () { | |
73 | IPython.Cell.prototype.create_element.apply(this, arguments); |
|
73 | IPython.Cell.prototype.create_element.apply(this, arguments); | |
74 | var cell = $("<div>").addClass('cell text_cell border-box-sizing'); |
|
74 | var cell = $("<div>").addClass('cell text_cell border-box-sizing'); | |
75 | cell.attr('tabindex','2'); |
|
75 | cell.attr('tabindex','2'); | |
76 |
|
76 | |||
77 | this.celltoolbar = new IPython.CellToolbar(this); |
|
77 | this.celltoolbar = new IPython.CellToolbar(this); | |
78 | cell.append(this.celltoolbar.element); |
|
78 | cell.append(this.celltoolbar.element); | |
79 |
|
79 | |||
80 | var input_area = $('<div/>').addClass('text_cell_input border-box-sizing'); |
|
80 | var input_area = $('<div/>').addClass('text_cell_input border-box-sizing'); | |
81 | this.code_mirror = CodeMirror(input_area.get(0), this.cm_config); |
|
81 | this.code_mirror = CodeMirror(input_area.get(0), this.cm_config); | |
82 |
|
82 | |||
83 | // The tabindex=-1 makes this div focusable. |
|
83 | // The tabindex=-1 makes this div focusable. | |
84 | var render_area = $('<div/>').addClass('text_cell_render border-box-sizing'). |
|
84 | var render_area = $('<div/>').addClass('text_cell_render border-box-sizing'). | |
85 | addClass('rendered_html').attr('tabindex','-1'); |
|
85 | addClass('rendered_html').attr('tabindex','-1'); | |
86 | cell.append(input_area).append(render_area); |
|
86 | cell.append(input_area).append(render_area); | |
87 | this.element = cell; |
|
87 | this.element = cell; | |
88 | }; |
|
88 | }; | |
89 |
|
89 | |||
90 |
|
90 | |||
91 | /** |
|
91 | /** | |
92 | * Bind the DOM evet to cell actions |
|
92 | * Bind the DOM evet to cell actions | |
93 | * Need to be called after TextCell.create_element |
|
93 | * Need to be called after TextCell.create_element | |
94 | * @private |
|
94 | * @private | |
95 | * @method bind_event |
|
95 | * @method bind_event | |
96 | */ |
|
96 | */ | |
97 | TextCell.prototype.bind_events = function () { |
|
97 | TextCell.prototype.bind_events = function () { | |
98 | IPython.Cell.prototype.bind_events.apply(this); |
|
98 | IPython.Cell.prototype.bind_events.apply(this); | |
99 | var that = this; |
|
99 | var that = this; | |
100 | this.element.keydown(function (event) { |
|
100 | this.element.keydown(function (event) { | |
101 | if (event.which === 13 && !event.shiftKey) { |
|
101 | if (event.which === 13 && !event.shiftKey) { | |
102 | if (that.rendered) { |
|
102 | if (that.rendered) { | |
103 | that.edit(); |
|
103 | that.edit(); | |
104 | return false; |
|
104 | return false; | |
105 | }; |
|
105 | }; | |
106 | }; |
|
106 | }; | |
107 | }); |
|
107 | }); | |
108 | this.element.dblclick(function () { |
|
108 | this.element.dblclick(function () { | |
109 | that.edit(); |
|
109 | that.edit(); | |
110 | }); |
|
110 | }); | |
111 | }; |
|
111 | }; | |
112 |
|
112 | |||
113 | /** |
|
113 | /** | |
114 | * This method gets called in CodeMirror's onKeyDown/onKeyPress |
|
114 | * This method gets called in CodeMirror's onKeyDown/onKeyPress | |
115 | * handlers and is used to provide custom key handling. |
|
115 | * handlers and is used to provide custom key handling. | |
116 | * |
|
116 | * | |
117 | * Subclass should override this method to have custom handeling |
|
117 | * Subclass should override this method to have custom handeling | |
118 | * |
|
118 | * | |
119 | * @method handle_codemirror_keyevent |
|
119 | * @method handle_codemirror_keyevent | |
120 | * @param {CodeMirror} editor - The codemirror instance bound to the cell |
|
120 | * @param {CodeMirror} editor - The codemirror instance bound to the cell | |
121 | * @param {event} event - |
|
121 | * @param {event} event - | |
122 | * @return {Boolean} `true` if CodeMirror should ignore the event, `false` Otherwise |
|
122 | * @return {Boolean} `true` if CodeMirror should ignore the event, `false` Otherwise | |
123 | */ |
|
123 | */ | |
124 | TextCell.prototype.handle_codemirror_keyevent = function (editor, event) { |
|
124 | TextCell.prototype.handle_codemirror_keyevent = function (editor, event) { | |
125 |
|
125 | |||
126 | if (event.keyCode === 13 && (event.shiftKey || event.ctrlKey)) { |
|
126 | if (event.keyCode === 13 && (event.shiftKey || event.ctrlKey)) { | |
127 | // Always ignore shift-enter in CodeMirror as we handle it. |
|
127 | // Always ignore shift-enter in CodeMirror as we handle it. | |
128 | return true; |
|
128 | return true; | |
129 | } |
|
129 | } | |
130 | return false; |
|
130 | return false; | |
131 | }; |
|
131 | }; | |
132 |
|
132 | |||
133 | /** |
|
133 | /** | |
134 | * Select the current cell and trigger 'focus' |
|
134 | * Select the current cell and trigger 'focus' | |
135 | * @method select |
|
135 | * @method select | |
136 | */ |
|
136 | */ | |
137 | TextCell.prototype.select = function () { |
|
137 | TextCell.prototype.select = function () { | |
138 | IPython.Cell.prototype.select.apply(this); |
|
138 | IPython.Cell.prototype.select.apply(this); | |
139 | var output = this.element.find("div.text_cell_render"); |
|
139 | var output = this.element.find("div.text_cell_render"); | |
140 | output.trigger('focus'); |
|
140 | output.trigger('focus'); | |
141 | }; |
|
141 | }; | |
142 |
|
142 | |||
143 | /** |
|
143 | /** | |
144 | * unselect the current cell and `render` it |
|
144 | * unselect the current cell and `render` it | |
145 | * @method unselect |
|
145 | * @method unselect | |
146 | */ |
|
146 | */ | |
147 | TextCell.prototype.unselect = function() { |
|
147 | TextCell.prototype.unselect = function() { | |
148 | // render on selection of another cell |
|
148 | // render on selection of another cell | |
149 | this.render(); |
|
149 | this.render(); | |
150 | IPython.Cell.prototype.unselect.apply(this); |
|
150 | IPython.Cell.prototype.unselect.apply(this); | |
151 | }; |
|
151 | }; | |
152 |
|
152 | |||
153 | /** |
|
153 | /** | |
154 | * |
|
154 | * | |
155 | * put the current cell in edition mode |
|
155 | * put the current cell in edition mode | |
156 | * @method edit |
|
156 | * @method edit | |
157 | */ |
|
157 | */ | |
158 | TextCell.prototype.edit = function () { |
|
158 | TextCell.prototype.edit = function () { | |
159 | if ( this.read_only ) return; |
|
159 | if ( this.read_only ) return; | |
160 | if (this.rendered === true) { |
|
160 | if (this.rendered === true) { | |
161 | var text_cell = this.element; |
|
161 | var text_cell = this.element; | |
162 | var output = text_cell.find("div.text_cell_render"); |
|
162 | var output = text_cell.find("div.text_cell_render"); | |
163 | output.hide(); |
|
163 | output.hide(); | |
164 | text_cell.find('div.text_cell_input').show(); |
|
164 | text_cell.find('div.text_cell_input').show(); | |
165 | this.code_mirror.refresh(); |
|
165 | this.code_mirror.refresh(); | |
166 | this.code_mirror.focus(); |
|
166 | this.code_mirror.focus(); | |
167 | // We used to need an additional refresh() after the focus, but |
|
167 | // We used to need an additional refresh() after the focus, but | |
168 | // it appears that this has been fixed in CM. This bug would show |
|
168 | // it appears that this has been fixed in CM. This bug would show | |
169 | // up on FF when a newly loaded markdown cell was edited. |
|
169 | // up on FF when a newly loaded markdown cell was edited. | |
170 | this.rendered = false; |
|
170 | this.rendered = false; | |
171 | if (this.get_text() === this.placeholder) { |
|
171 | if (this.get_text() === this.placeholder) { | |
172 | this.set_text(''); |
|
172 | this.set_text(''); | |
173 | this.refresh(); |
|
173 | this.refresh(); | |
174 | } |
|
174 | } | |
175 | } |
|
175 | } | |
176 | }; |
|
176 | }; | |
177 |
|
177 | |||
178 |
|
178 | |||
179 | /** |
|
179 | /** | |
180 | * Empty, Subclasses must define render. |
|
180 | * Empty, Subclasses must define render. | |
181 | * @method render |
|
181 | * @method render | |
182 | */ |
|
182 | */ | |
183 | TextCell.prototype.render = function () {}; |
|
183 | TextCell.prototype.render = function () {}; | |
184 |
|
184 | |||
185 |
|
185 | |||
186 | /** |
|
186 | /** | |
187 | * setter: {{#crossLink "TextCell/set_text"}}{{/crossLink}} |
|
187 | * setter: {{#crossLink "TextCell/set_text"}}{{/crossLink}} | |
188 | * @method get_text |
|
188 | * @method get_text | |
189 | * @retrun {string} CodeMirror current text value |
|
189 | * @retrun {string} CodeMirror current text value | |
190 | */ |
|
190 | */ | |
191 | TextCell.prototype.get_text = function() { |
|
191 | TextCell.prototype.get_text = function() { | |
192 | return this.code_mirror.getValue(); |
|
192 | return this.code_mirror.getValue(); | |
193 | }; |
|
193 | }; | |
194 |
|
194 | |||
195 | /** |
|
195 | /** | |
196 | * @param {string} text - Codemiror text value |
|
196 | * @param {string} text - Codemiror text value | |
197 | * @see TextCell#get_text |
|
197 | * @see TextCell#get_text | |
198 | * @method set_text |
|
198 | * @method set_text | |
199 | * */ |
|
199 | * */ | |
200 | TextCell.prototype.set_text = function(text) { |
|
200 | TextCell.prototype.set_text = function(text) { | |
201 | this.code_mirror.setValue(text); |
|
201 | this.code_mirror.setValue(text); | |
202 | this.code_mirror.refresh(); |
|
202 | this.code_mirror.refresh(); | |
203 | }; |
|
203 | }; | |
204 |
|
204 | |||
205 | /** |
|
205 | /** | |
206 | * setter :{{#crossLink "TextCell/set_rendered"}}{{/crossLink}} |
|
206 | * setter :{{#crossLink "TextCell/set_rendered"}}{{/crossLink}} | |
207 | * @method get_rendered |
|
207 | * @method get_rendered | |
208 | * @return {html} html of rendered element |
|
208 | * @return {html} html of rendered element | |
209 | * */ |
|
209 | * */ | |
210 | TextCell.prototype.get_rendered = function() { |
|
210 | TextCell.prototype.get_rendered = function() { | |
211 | return this.element.find('div.text_cell_render').html(); |
|
211 | return this.element.find('div.text_cell_render').html(); | |
212 | }; |
|
212 | }; | |
213 |
|
213 | |||
214 | /** |
|
214 | /** | |
215 | * @method set_rendered |
|
215 | * @method set_rendered | |
216 | */ |
|
216 | */ | |
217 | TextCell.prototype.set_rendered = function(text) { |
|
217 | TextCell.prototype.set_rendered = function(text) { | |
218 | this.element.find('div.text_cell_render').html(text); |
|
218 | this.element.find('div.text_cell_render').html(text); | |
219 | }; |
|
219 | }; | |
220 |
|
220 | |||
221 | /** |
|
221 | /** | |
222 | * not deprecated, but implementation wrong |
|
222 | * not deprecated, but implementation wrong | |
223 | * @method at_top |
|
223 | * @method at_top | |
224 | * @deprecated |
|
224 | * @deprecated | |
225 | * @return {Boolean} true is cell rendered, false otherwise |
|
225 | * @return {Boolean} true is cell rendered, false otherwise | |
226 | * I doubt this is what it is supposed to do |
|
226 | * I doubt this is what it is supposed to do | |
227 | * this implementation is completly false |
|
227 | * this implementation is completly false | |
228 | */ |
|
228 | */ | |
229 | TextCell.prototype.at_top = function () { |
|
229 | TextCell.prototype.at_top = function () { | |
230 | if (this.rendered) { |
|
230 | if (this.rendered) { | |
231 | return true; |
|
231 | return true; | |
232 | } else { |
|
232 | } else { | |
233 | return false; |
|
233 | return false; | |
234 | } |
|
234 | } | |
235 | }; |
|
235 | }; | |
236 |
|
236 | |||
237 |
|
237 | |||
238 | /** |
|
238 | /** | |
239 | * not deprecated, but implementation wrong |
|
239 | * not deprecated, but implementation wrong | |
240 | * @method at_bottom |
|
240 | * @method at_bottom | |
241 | * @deprecated |
|
241 | * @deprecated | |
242 | * @return {Boolean} true is cell rendered, false otherwise |
|
242 | * @return {Boolean} true is cell rendered, false otherwise | |
243 | * I doubt this is what it is supposed to do |
|
243 | * I doubt this is what it is supposed to do | |
244 | * this implementation is completly false |
|
244 | * this implementation is completly false | |
245 | * */ |
|
245 | * */ | |
246 | TextCell.prototype.at_bottom = function () { |
|
246 | TextCell.prototype.at_bottom = function () { | |
247 | if (this.rendered) { |
|
247 | if (this.rendered) { | |
248 | return true; |
|
248 | return true; | |
249 | } else { |
|
249 | } else { | |
250 | return false; |
|
250 | return false; | |
251 | } |
|
251 | } | |
252 | }; |
|
252 | }; | |
253 |
|
253 | |||
254 | /** |
|
254 | /** | |
255 | * Create Text cell from JSON |
|
255 | * Create Text cell from JSON | |
256 | * @param {json} data - JSON serialized text-cell |
|
256 | * @param {json} data - JSON serialized text-cell | |
257 | * @method fromJSON |
|
257 | * @method fromJSON | |
258 | */ |
|
258 | */ | |
259 | TextCell.prototype.fromJSON = function (data) { |
|
259 | TextCell.prototype.fromJSON = function (data) { | |
260 | IPython.Cell.prototype.fromJSON.apply(this, arguments); |
|
260 | IPython.Cell.prototype.fromJSON.apply(this, arguments); | |
261 | if (data.cell_type === this.cell_type) { |
|
261 | if (data.cell_type === this.cell_type) { | |
262 | if (data.source !== undefined) { |
|
262 | if (data.source !== undefined) { | |
263 | this.set_text(data.source); |
|
263 | this.set_text(data.source); | |
264 | // make this value the starting point, so that we can only undo |
|
264 | // make this value the starting point, so that we can only undo | |
265 | // to this state, instead of a blank cell |
|
265 | // to this state, instead of a blank cell | |
266 | this.code_mirror.clearHistory(); |
|
266 | this.code_mirror.clearHistory(); | |
267 | this.set_rendered(data.rendered || ''); |
|
267 | this.set_rendered(data.rendered || ''); | |
268 | this.rendered = false; |
|
268 | this.rendered = false; | |
269 | this.render(); |
|
269 | this.render(); | |
270 | } |
|
270 | } | |
271 | } |
|
271 | } | |
272 | }; |
|
272 | }; | |
273 |
|
273 | |||
274 | /** Generate JSON from cell |
|
274 | /** Generate JSON from cell | |
275 | * @return {object} cell data serialised to json |
|
275 | * @return {object} cell data serialised to json | |
276 | */ |
|
276 | */ | |
277 | TextCell.prototype.toJSON = function () { |
|
277 | TextCell.prototype.toJSON = function () { | |
278 | var data = IPython.Cell.prototype.toJSON.apply(this); |
|
278 | var data = IPython.Cell.prototype.toJSON.apply(this); | |
279 | data.cell_type = this.cell_type; |
|
279 | data.cell_type = this.cell_type; | |
280 | data.source = this.get_text(); |
|
280 | data.source = this.get_text(); | |
281 | return data; |
|
281 | return data; | |
282 | }; |
|
282 | }; | |
283 |
|
283 | |||
284 |
|
284 | |||
285 | /** |
|
285 | /** | |
286 | * @constructor HtmlCell |
|
|||
287 | * @class HtmlCell |
|
|||
288 | * @extends Ipython.TextCell |
|
|||
289 | */ |
|
|||
290 | var HTMLCell = function (options) { |
|
|||
291 |
|
||||
292 | options = this.mergeopt(HTMLCell,options); |
|
|||
293 | TextCell.apply(this, [options]); |
|
|||
294 |
|
||||
295 | this.cell_type = 'html'; |
|
|||
296 | }; |
|
|||
297 |
|
||||
298 | HTMLCell.options_default = { |
|
|||
299 | cm_config : { |
|
|||
300 | mode: 'htmlmixed', |
|
|||
301 | }, |
|
|||
302 | placeholder: "Type <strong>HTML</strong> and LaTeX: $\\alpha^2$" |
|
|||
303 | }; |
|
|||
304 |
|
||||
305 |
|
||||
306 | HTMLCell.prototype = new TextCell(); |
|
|||
307 |
|
||||
308 | /** |
|
|||
309 | * @method render |
|
|||
310 | */ |
|
|||
311 | HTMLCell.prototype.render = function () { |
|
|||
312 | if (this.rendered === false) { |
|
|||
313 | var text = this.get_text(); |
|
|||
314 | if (text === "") { text = this.placeholder; } |
|
|||
315 | this.set_rendered(text); |
|
|||
316 | this.typeset(); |
|
|||
317 | this.element.find('div.text_cell_input').hide(); |
|
|||
318 | this.element.find("div.text_cell_render").show(); |
|
|||
319 | this.rendered = true; |
|
|||
320 | } |
|
|||
321 | }; |
|
|||
322 |
|
||||
323 |
|
||||
324 | /** |
|
|||
325 | * @class MarkdownCell |
|
286 | * @class MarkdownCell | |
326 | * @constructor MarkdownCell |
|
287 | * @constructor MarkdownCell | |
327 | * @extends Ipython.HtmlCell |
|
288 | * @extends Ipython.HtmlCell | |
328 | */ |
|
289 | */ | |
329 | var MarkdownCell = function (options) { |
|
290 | var MarkdownCell = function (options) { | |
330 | var options = options || {}; |
|
291 | var options = options || {}; | |
331 |
|
292 | |||
332 | options = this.mergeopt(MarkdownCell,options); |
|
293 | options = this.mergeopt(MarkdownCell,options); | |
333 | TextCell.apply(this, [options]); |
|
294 | TextCell.apply(this, [options]); | |
334 |
|
295 | |||
335 | this.cell_type = 'markdown'; |
|
296 | this.cell_type = 'markdown'; | |
336 | }; |
|
297 | }; | |
337 |
|
298 | |||
338 | MarkdownCell.options_default = { |
|
299 | MarkdownCell.options_default = { | |
339 | cm_config: { |
|
300 | cm_config: { | |
340 | mode: 'markdown' |
|
301 | mode: 'markdown' | |
341 | }, |
|
302 | }, | |
342 | placeholder: "Type *Markdown* and LaTeX: $\\alpha^2$" |
|
303 | placeholder: "Type *Markdown* and LaTeX: $\\alpha^2$" | |
343 | } |
|
304 | } | |
344 |
|
305 | |||
345 |
|
306 | |||
346 |
|
307 | |||
347 |
|
308 | |||
348 | MarkdownCell.prototype = new TextCell(); |
|
309 | MarkdownCell.prototype = new TextCell(); | |
349 |
|
310 | |||
350 | /** |
|
311 | /** | |
351 | * @method render |
|
312 | * @method render | |
352 | */ |
|
313 | */ | |
353 | MarkdownCell.prototype.render = function () { |
|
314 | MarkdownCell.prototype.render = function () { | |
354 | if (this.rendered === false) { |
|
315 | if (this.rendered === false) { | |
355 | var text = this.get_text(); |
|
316 | var text = this.get_text(); | |
356 | if (text === "") { text = this.placeholder; } |
|
317 | if (text === "") { text = this.placeholder; } | |
357 | text = IPython.mathjaxutils.remove_math(text) |
|
318 | text = IPython.mathjaxutils.remove_math(text) | |
358 | var html = IPython.markdown_converter.makeHtml(text); |
|
319 | var html = IPython.markdown_converter.makeHtml(text); | |
359 | html = IPython.mathjaxutils.replace_math(html) |
|
320 | html = IPython.mathjaxutils.replace_math(html) | |
360 | try { |
|
321 | try { | |
361 | this.set_rendered(html); |
|
322 | this.set_rendered(html); | |
362 | } catch (e) { |
|
323 | } catch (e) { | |
363 | console.log("Error running Javascript in Markdown:"); |
|
324 | console.log("Error running Javascript in Markdown:"); | |
364 | console.log(e); |
|
325 | console.log(e); | |
365 | this.set_rendered($("<div/>").addClass("js-error").html( |
|
326 | this.set_rendered($("<div/>").addClass("js-error").html( | |
366 | "Error rendering Markdown!<br/>" + e.toString()) |
|
327 | "Error rendering Markdown!<br/>" + e.toString()) | |
367 | ); |
|
328 | ); | |
368 | } |
|
329 | } | |
369 | this.element.find('div.text_cell_input').hide(); |
|
330 | this.element.find('div.text_cell_input').hide(); | |
370 | this.element.find("div.text_cell_render").show(); |
|
331 | this.element.find("div.text_cell_render").show(); | |
371 | var code_snippets = this.element.find("pre > code"); |
|
332 | var code_snippets = this.element.find("pre > code"); | |
372 | code_snippets.replaceWith(function () { |
|
333 | code_snippets.replaceWith(function () { | |
373 | var code = $(this).html(); |
|
334 | var code = $(this).html(); | |
374 | /* Substitute br for newlines and for spaces |
|
335 | /* Substitute br for newlines and for spaces | |
375 | before highlighting, since prettify doesn't |
|
336 | before highlighting, since prettify doesn't | |
376 | preserve those on all browsers */ |
|
337 | preserve those on all browsers */ | |
377 | code = code.replace(/(\r\n|\n|\r)/gm, "<br/>"); |
|
338 | code = code.replace(/(\r\n|\n|\r)/gm, "<br/>"); | |
378 | code = code.replace(/ /gm, ' '); |
|
339 | code = code.replace(/ /gm, ' '); | |
379 | code = prettyPrintOne(code); |
|
340 | code = prettyPrintOne(code); | |
380 |
|
341 | |||
381 | return '<code class="prettyprint">' + code + '</code>'; |
|
342 | return '<code class="prettyprint">' + code + '</code>'; | |
382 | }); |
|
343 | }); | |
383 | this.typeset() |
|
344 | this.typeset() | |
384 | this.rendered = true; |
|
345 | this.rendered = true; | |
385 | } |
|
346 | } | |
386 | }; |
|
347 | }; | |
387 |
|
348 | |||
388 |
|
349 | |||
389 | // RawCell |
|
350 | // RawCell | |
390 |
|
351 | |||
391 | /** |
|
352 | /** | |
392 | * @class RawCell |
|
353 | * @class RawCell | |
393 | * @constructor RawCell |
|
354 | * @constructor RawCell | |
394 | * @extends Ipython.TextCell |
|
355 | * @extends Ipython.TextCell | |
395 | */ |
|
356 | */ | |
396 | var RawCell = function (options) { |
|
357 | var RawCell = function (options) { | |
397 |
|
358 | |||
398 | options = this.mergeopt(RawCell,options) |
|
359 | options = this.mergeopt(RawCell,options) | |
399 | TextCell.apply(this, [options]); |
|
360 | TextCell.apply(this, [options]); | |
400 |
|
361 | |||
401 | this.cell_type = 'raw'; |
|
362 | this.cell_type = 'raw'; | |
402 |
|
363 | |||
403 | var that = this |
|
364 | var that = this | |
404 | this.element.focusout( |
|
365 | this.element.focusout( | |
405 | function() { that.auto_highlight(); } |
|
366 | function() { that.auto_highlight(); } | |
406 | ); |
|
367 | ); | |
407 | }; |
|
368 | }; | |
408 |
|
369 | |||
409 | RawCell.options_default = { |
|
370 | RawCell.options_default = { | |
410 | placeholder : "Type plain text and LaTeX: $\\alpha^2$" |
|
371 | placeholder : "Type plain text and LaTeX: $\\alpha^2$" | |
411 | }; |
|
372 | }; | |
412 |
|
373 | |||
413 |
|
374 | |||
414 |
|
375 | |||
415 | RawCell.prototype = new TextCell(); |
|
376 | RawCell.prototype = new TextCell(); | |
416 |
|
377 | |||
417 | /** |
|
378 | /** | |
418 | * Trigger autodetection of highlight scheme for current cell |
|
379 | * Trigger autodetection of highlight scheme for current cell | |
419 | * @method auto_highlight |
|
380 | * @method auto_highlight | |
420 | */ |
|
381 | */ | |
421 | RawCell.prototype.auto_highlight = function () { |
|
382 | RawCell.prototype.auto_highlight = function () { | |
422 | this._auto_highlight(IPython.config.raw_cell_highlight); |
|
383 | this._auto_highlight(IPython.config.raw_cell_highlight); | |
423 | }; |
|
384 | }; | |
424 |
|
385 | |||
425 | /** @method render **/ |
|
386 | /** @method render **/ | |
426 | RawCell.prototype.render = function () { |
|
387 | RawCell.prototype.render = function () { | |
427 | this.rendered = true; |
|
388 | this.rendered = true; | |
428 | this.edit(); |
|
389 | this.edit(); | |
429 | }; |
|
390 | }; | |
430 |
|
391 | |||
431 |
|
392 | |||
432 | /** @method handle_codemirror_keyevent **/ |
|
393 | /** @method handle_codemirror_keyevent **/ | |
433 | RawCell.prototype.handle_codemirror_keyevent = function (editor, event) { |
|
394 | RawCell.prototype.handle_codemirror_keyevent = function (editor, event) { | |
434 |
|
395 | |||
435 | var that = this; |
|
396 | var that = this; | |
436 | if (event.which === key.UPARROW && event.type === 'keydown') { |
|
397 | if (event.which === key.UPARROW && event.type === 'keydown') { | |
437 | // If we are not at the top, let CM handle the up arrow and |
|
398 | // If we are not at the top, let CM handle the up arrow and | |
438 | // prevent the global keydown handler from handling it. |
|
399 | // prevent the global keydown handler from handling it. | |
439 | if (!that.at_top()) { |
|
400 | if (!that.at_top()) { | |
440 | event.stop(); |
|
401 | event.stop(); | |
441 | return false; |
|
402 | return false; | |
442 | } else { |
|
403 | } else { | |
443 | return true; |
|
404 | return true; | |
444 | }; |
|
405 | }; | |
445 | } else if (event.which === key.DOWNARROW && event.type === 'keydown') { |
|
406 | } else if (event.which === key.DOWNARROW && event.type === 'keydown') { | |
446 | // If we are not at the bottom, let CM handle the down arrow and |
|
407 | // If we are not at the bottom, let CM handle the down arrow and | |
447 | // prevent the global keydown handler from handling it. |
|
408 | // prevent the global keydown handler from handling it. | |
448 | if (!that.at_bottom()) { |
|
409 | if (!that.at_bottom()) { | |
449 | event.stop(); |
|
410 | event.stop(); | |
450 | return false; |
|
411 | return false; | |
451 | } else { |
|
412 | } else { | |
452 | return true; |
|
413 | return true; | |
453 | }; |
|
414 | }; | |
454 | }; |
|
415 | }; | |
455 | return false; |
|
416 | return false; | |
456 | }; |
|
417 | }; | |
457 |
|
418 | |||
458 | /** @method select **/ |
|
419 | /** @method select **/ | |
459 | RawCell.prototype.select = function () { |
|
420 | RawCell.prototype.select = function () { | |
460 | IPython.Cell.prototype.select.apply(this); |
|
421 | IPython.Cell.prototype.select.apply(this); | |
461 | this.code_mirror.refresh(); |
|
422 | this.code_mirror.refresh(); | |
462 | this.code_mirror.focus(); |
|
423 | this.code_mirror.focus(); | |
463 | }; |
|
424 | }; | |
464 |
|
425 | |||
465 | /** @method at_top **/ |
|
426 | /** @method at_top **/ | |
466 | RawCell.prototype.at_top = function () { |
|
427 | RawCell.prototype.at_top = function () { | |
467 | var cursor = this.code_mirror.getCursor(); |
|
428 | var cursor = this.code_mirror.getCursor(); | |
468 | if (cursor.line === 0 && cursor.ch === 0) { |
|
429 | if (cursor.line === 0 && cursor.ch === 0) { | |
469 | return true; |
|
430 | return true; | |
470 | } else { |
|
431 | } else { | |
471 | return false; |
|
432 | return false; | |
472 | } |
|
433 | } | |
473 | }; |
|
434 | }; | |
474 |
|
435 | |||
475 |
|
436 | |||
476 | /** @method at_bottom **/ |
|
437 | /** @method at_bottom **/ | |
477 | RawCell.prototype.at_bottom = function () { |
|
438 | RawCell.prototype.at_bottom = function () { | |
478 | var cursor = this.code_mirror.getCursor(); |
|
439 | var cursor = this.code_mirror.getCursor(); | |
479 | if (cursor.line === (this.code_mirror.lineCount()-1) && cursor.ch === this.code_mirror.getLine(cursor.line).length) { |
|
440 | if (cursor.line === (this.code_mirror.lineCount()-1) && cursor.ch === this.code_mirror.getLine(cursor.line).length) { | |
480 | return true; |
|
441 | return true; | |
481 | } else { |
|
442 | } else { | |
482 | return false; |
|
443 | return false; | |
483 | } |
|
444 | } | |
484 | }; |
|
445 | }; | |
485 |
|
446 | |||
486 |
|
447 | |||
487 | /** |
|
448 | /** | |
488 | * @class HeadingCell |
|
449 | * @class HeadingCell | |
489 | * @extends Ipython.TextCell |
|
450 | * @extends Ipython.TextCell | |
490 | */ |
|
451 | */ | |
491 |
|
452 | |||
492 | /** |
|
453 | /** | |
493 | * @constructor HeadingCell |
|
454 | * @constructor HeadingCell | |
494 | * @extends Ipython.TextCell |
|
455 | * @extends Ipython.TextCell | |
495 | */ |
|
456 | */ | |
496 | var HeadingCell = function (options) { |
|
457 | var HeadingCell = function (options) { | |
497 |
|
458 | |||
498 | options = this.mergeopt(HeadingCell,options) |
|
459 | options = this.mergeopt(HeadingCell,options) | |
499 | TextCell.apply(this, [options]); |
|
460 | TextCell.apply(this, [options]); | |
500 |
|
461 | |||
501 | /** |
|
462 | /** | |
502 | * heading level of the cell, use getter and setter to access |
|
463 | * heading level of the cell, use getter and setter to access | |
503 | * @property level |
|
464 | * @property level | |
504 | */ |
|
465 | */ | |
505 | this.level = 1; |
|
466 | this.level = 1; | |
506 | this.cell_type = 'heading'; |
|
467 | this.cell_type = 'heading'; | |
507 | }; |
|
468 | }; | |
508 |
|
469 | |||
509 | HeadingCell.options_default = { |
|
470 | HeadingCell.options_default = { | |
510 | placeholder: "Type Heading Here" |
|
471 | placeholder: "Type Heading Here" | |
511 | }; |
|
472 | }; | |
512 |
|
473 | |||
513 | HeadingCell.prototype = new TextCell(); |
|
474 | HeadingCell.prototype = new TextCell(); | |
514 |
|
475 | |||
515 | /** @method fromJSON */ |
|
476 | /** @method fromJSON */ | |
516 | HeadingCell.prototype.fromJSON = function (data) { |
|
477 | HeadingCell.prototype.fromJSON = function (data) { | |
517 | if (data.level != undefined){ |
|
478 | if (data.level != undefined){ | |
518 | this.level = data.level; |
|
479 | this.level = data.level; | |
519 | } |
|
480 | } | |
520 | TextCell.prototype.fromJSON.apply(this, arguments); |
|
481 | TextCell.prototype.fromJSON.apply(this, arguments); | |
521 | }; |
|
482 | }; | |
522 |
|
483 | |||
523 |
|
484 | |||
524 | /** @method toJSON */ |
|
485 | /** @method toJSON */ | |
525 | HeadingCell.prototype.toJSON = function () { |
|
486 | HeadingCell.prototype.toJSON = function () { | |
526 | var data = TextCell.prototype.toJSON.apply(this); |
|
487 | var data = TextCell.prototype.toJSON.apply(this); | |
527 | data.level = this.get_level(); |
|
488 | data.level = this.get_level(); | |
528 | return data; |
|
489 | return data; | |
529 | }; |
|
490 | }; | |
530 |
|
491 | |||
531 |
|
492 | |||
532 | /** |
|
493 | /** | |
533 | * Change heading level of cell, and re-render |
|
494 | * Change heading level of cell, and re-render | |
534 | * @method set_level |
|
495 | * @method set_level | |
535 | */ |
|
496 | */ | |
536 | HeadingCell.prototype.set_level = function (level) { |
|
497 | HeadingCell.prototype.set_level = function (level) { | |
537 | this.level = level; |
|
498 | this.level = level; | |
538 | if (this.rendered) { |
|
499 | if (this.rendered) { | |
539 | this.rendered = false; |
|
500 | this.rendered = false; | |
540 | this.render(); |
|
501 | this.render(); | |
541 | }; |
|
502 | }; | |
542 | }; |
|
503 | }; | |
543 |
|
504 | |||
544 | /** The depth of header cell, based on html (h1 to h6) |
|
505 | /** The depth of header cell, based on html (h1 to h6) | |
545 | * @method get_level |
|
506 | * @method get_level | |
546 | * @return {integer} level - for 1 to 6 |
|
507 | * @return {integer} level - for 1 to 6 | |
547 | */ |
|
508 | */ | |
548 | HeadingCell.prototype.get_level = function () { |
|
509 | HeadingCell.prototype.get_level = function () { | |
549 | return this.level; |
|
510 | return this.level; | |
550 | }; |
|
511 | }; | |
551 |
|
512 | |||
552 |
|
513 | |||
553 | HeadingCell.prototype.set_rendered = function (text) { |
|
514 | HeadingCell.prototype.set_rendered = function (text) { | |
554 | var r = this.element.find("div.text_cell_render"); |
|
515 | var r = this.element.find("div.text_cell_render"); | |
555 | r.empty(); |
|
516 | r.empty(); | |
556 | var link = text.replace(/ /g, '_'); |
|
517 | var link = text.replace(/ /g, '_'); | |
557 | r.append( |
|
518 | r.append( | |
558 | $('<h'+this.level+'/>') |
|
519 | $('<h'+this.level+'/>') | |
559 | .append( |
|
520 | .append( | |
560 | $('<a/>') |
|
521 | $('<a/>') | |
561 | .addClass('heading-anchor') |
|
522 | .addClass('heading-anchor') | |
562 | .attr('href', '#' + link) |
|
523 | .attr('href', '#' + link) | |
563 | .attr('id', link) |
|
524 | .attr('id', link) | |
564 | .html(text) |
|
525 | .html(text) | |
565 | ) |
|
526 | ) | |
566 | ); |
|
527 | ); | |
567 | }; |
|
528 | }; | |
568 |
|
529 | |||
569 |
|
530 | |||
570 | HeadingCell.prototype.get_rendered = function () { |
|
531 | HeadingCell.prototype.get_rendered = function () { | |
571 | var r = this.element.find("div.text_cell_render"); |
|
532 | var r = this.element.find("div.text_cell_render"); | |
572 | return r.children().first().html(); |
|
533 | return r.children().first().html(); | |
573 | }; |
|
534 | }; | |
574 |
|
535 | |||
575 |
|
536 | |||
576 | HeadingCell.prototype.render = function () { |
|
537 | HeadingCell.prototype.render = function () { | |
577 | if (this.rendered === false) { |
|
538 | if (this.rendered === false) { | |
578 | var text = this.get_text(); |
|
539 | var text = this.get_text(); | |
579 | if (text === "") { text = this.placeholder; } |
|
540 | if (text === "") { text = this.placeholder; } | |
580 | this.set_rendered(text); |
|
541 | this.set_rendered(text); | |
581 | this.typeset(); |
|
542 | this.typeset(); | |
582 | this.element.find('div.text_cell_input').hide(); |
|
543 | this.element.find('div.text_cell_input').hide(); | |
583 | this.element.find("div.text_cell_render").show(); |
|
544 | this.element.find("div.text_cell_render").show(); | |
584 | this.rendered = true; |
|
545 | this.rendered = true; | |
585 | }; |
|
546 | }; | |
586 | }; |
|
547 | }; | |
587 |
|
548 | |||
588 | IPython.TextCell = TextCell; |
|
549 | IPython.TextCell = TextCell; | |
589 | IPython.HTMLCell = HTMLCell; |
|
|||
590 | IPython.MarkdownCell = MarkdownCell; |
|
550 | IPython.MarkdownCell = MarkdownCell; | |
591 | IPython.RawCell = RawCell; |
|
551 | IPython.RawCell = RawCell; | |
592 | IPython.HeadingCell = HeadingCell; |
|
552 | IPython.HeadingCell = HeadingCell; | |
593 |
|
553 | |||
594 |
|
554 | |||
595 | return IPython; |
|
555 | return IPython; | |
596 |
|
556 | |||
597 | }(IPython)); |
|
557 | }(IPython)); | |
598 |
|
558 |
General Comments 0
You need to be logged in to leave comments.
Login now