##// END OF EJS Templates
More accuract height calculations for the pager collapse/expand.
Brian E. Granger -
Show More
@@ -1,557 +1,556 b''
1
1
2 //============================================================================
2 //============================================================================
3 // Notebook
3 // Notebook
4 //============================================================================
4 //============================================================================
5
5
6 var IPython = (function (IPython) {
6 var IPython = (function (IPython) {
7
7
8 var utils = IPython.utils;
8 var utils = IPython.utils;
9
9
10 var Notebook = function (selector) {
10 var Notebook = function (selector) {
11 this.element = $(selector);
11 this.element = $(selector);
12 this.element.scroll();
12 this.element.scroll();
13 this.element.data("notebook", this);
13 this.element.data("notebook", this);
14 this.next_prompt_number = 1;
14 this.next_prompt_number = 1;
15 this.kernel = null;
15 this.kernel = null;
16 this.msg_cell_map = {};
16 this.msg_cell_map = {};
17 this.filename = null;
17 this.filename = null;
18 this.notebook_load_re = /%notebook load/
18 this.notebook_load_re = /%notebook load/
19 this.notebook_save_re = /%notebook save/
19 this.notebook_save_re = /%notebook save/
20 this.notebook_filename_re = /(\w)+.ipynb/
20 this.notebook_filename_re = /(\w)+.ipynb/
21 this.style();
21 this.style();
22 this.bind_events();
22 this.bind_events();
23 this.start_kernel();
23 this.start_kernel();
24 };
24 };
25
25
26
26
27 Notebook.prototype.style = function () {
27 Notebook.prototype.style = function () {
28 };
28 };
29
29
30
30
31 Notebook.prototype.bind_events = function () {
31 Notebook.prototype.bind_events = function () {
32 var that = this;
32 var that = this;
33 $(document).keydown(function (event) {
33 $(document).keydown(function (event) {
34 // console.log(event);
34 // console.log(event);
35 if (event.which === 38) {
35 if (event.which === 38) {
36 var cell = that.selected_cell();
36 var cell = that.selected_cell();
37 if (cell.at_top()) {
37 if (cell.at_top()) {
38 event.preventDefault();
38 event.preventDefault();
39 that.select_prev();
39 that.select_prev();
40 };
40 };
41 } else if (event.which === 40) {
41 } else if (event.which === 40) {
42 var cell = that.selected_cell();
42 var cell = that.selected_cell();
43 if (cell.at_bottom()) {
43 if (cell.at_bottom()) {
44 event.preventDefault();
44 event.preventDefault();
45 that.select_next();
45 that.select_next();
46 };
46 };
47 } else if (event.which === 13 && event.shiftKey) {
47 } else if (event.which === 13 && event.shiftKey) {
48 // The focus is not quite working here.
48 // The focus is not quite working here.
49 var cell = that.selected_cell();
49 var cell = that.selected_cell();
50 var cell_index = that.find_cell_index(cell);
50 var cell_index = that.find_cell_index(cell);
51 // TODO: the logic here needs to be moved into appropriate
51 // TODO: the logic here needs to be moved into appropriate
52 // methods of Notebook.
52 // methods of Notebook.
53 if (cell instanceof IPython.CodeCell) {
53 if (cell instanceof IPython.CodeCell) {
54 event.preventDefault();
54 event.preventDefault();
55 cell.clear_output();
55 cell.clear_output();
56 var code = cell.get_code();
56 var code = cell.get_code();
57 if (that.notebook_load_re.test(code)) {
57 if (that.notebook_load_re.test(code)) {
58 var code_parts = code.split(' ');
58 var code_parts = code.split(' ');
59 if (code_parts.length === 3) {
59 if (code_parts.length === 3) {
60 that.load_notebook(code_parts[2]);
60 that.load_notebook(code_parts[2]);
61 };
61 };
62 } else if (that.notebook_save_re.test(code)) {
62 } else if (that.notebook_save_re.test(code)) {
63 var code_parts = code.split(' ');
63 var code_parts = code.split(' ');
64 if (code_parts.length === 3) {
64 if (code_parts.length === 3) {
65 that.save_notebook(code_parts[2]);
65 that.save_notebook(code_parts[2]);
66 } else {
66 } else {
67 that.save_notebook()
67 that.save_notebook()
68 };
68 };
69 } else {
69 } else {
70 var msg_id = that.kernel.execute(cell.get_code());
70 var msg_id = that.kernel.execute(cell.get_code());
71 that.msg_cell_map[msg_id] = cell.cell_id;
71 that.msg_cell_map[msg_id] = cell.cell_id;
72 };
72 };
73 } else if (cell instanceof IPython.TextCell) {
73 } else if (cell instanceof IPython.TextCell) {
74 event.preventDefault();
74 event.preventDefault();
75 cell.render();
75 cell.render();
76 }
76 }
77 if (cell_index === (that.ncells()-1)) {
77 if (cell_index === (that.ncells()-1)) {
78 that.insert_code_cell_after();
78 that.insert_code_cell_after();
79 } else {
79 } else {
80 that.select(cell_index+1);
80 that.select(cell_index+1);
81 };
81 };
82 };
82 };
83 });
83 });
84
84
85 this.element.bind('collapse_pager', function () {
85 this.element.bind('collapse_pager', function () {
86 var that_height = that.element.outerHeight(true);
86 var app_height = $('div#notebook_app').height(); // content height
87 var pager_height = $('div#pager').outerHeight(true);
87 var splitter_height = $('div#pager_splitter').outerHeight(true);
88 var new_height = that_height + pager_height;
88 var new_height = app_height - splitter_height;
89 console.log('collapse', that_height, pager_height, new_height);
90 that.element.animate({height : new_height + 'px'}, 'fast');
89 that.element.animate({height : new_height + 'px'}, 'fast');
91 });
90 });
92
91
93 this.element.bind('expand_pager', function () {
92 this.element.bind('expand_pager', function () {
94 var that_height = that.element.outerHeight(true);
93 var app_height = $('div#notebook_app').height(); // content height
94 var splitter_height = $('div#pager_splitter').outerHeight(true);
95 var pager_height = $('div#pager').outerHeight(true);
95 var pager_height = $('div#pager').outerHeight(true);
96 var new_height = that_height - pager_height;
96 var new_height = app_height - pager_height - splitter_height;
97 console.log('expand', that_height, pager_height, new_height);
98 that.element.animate({height : new_height + 'px'}, 'fast');
97 that.element.animate({height : new_height + 'px'}, 'fast');
99 });
98 });
100 };
99 };
101
100
102
101
103 // Cell indexing, retrieval, etc.
102 // Cell indexing, retrieval, etc.
104
103
105
104
106 Notebook.prototype.cell_elements = function () {
105 Notebook.prototype.cell_elements = function () {
107 return this.element.children("div.cell");
106 return this.element.children("div.cell");
108 }
107 }
109
108
110
109
111 Notebook.prototype.ncells = function (cell) {
110 Notebook.prototype.ncells = function (cell) {
112 return this.cell_elements().length;
111 return this.cell_elements().length;
113 }
112 }
114
113
115
114
116 // TODO: we are often calling cells as cells()[i], which we should optimize
115 // TODO: we are often calling cells as cells()[i], which we should optimize
117 // to cells(i) or a new method.
116 // to cells(i) or a new method.
118 Notebook.prototype.cells = function () {
117 Notebook.prototype.cells = function () {
119 return this.cell_elements().toArray().map(function (e) {
118 return this.cell_elements().toArray().map(function (e) {
120 return $(e).data("cell");
119 return $(e).data("cell");
121 });
120 });
122 }
121 }
123
122
124
123
125 Notebook.prototype.find_cell_index = function (cell) {
124 Notebook.prototype.find_cell_index = function (cell) {
126 var result = null;
125 var result = null;
127 this.cell_elements().filter(function (index) {
126 this.cell_elements().filter(function (index) {
128 if ($(this).data("cell") === cell) {
127 if ($(this).data("cell") === cell) {
129 result = index;
128 result = index;
130 };
129 };
131 });
130 });
132 return result;
131 return result;
133 };
132 };
134
133
135
134
136 Notebook.prototype.index_or_selected = function (index) {
135 Notebook.prototype.index_or_selected = function (index) {
137 return index || this.selected_index() || 0;
136 return index || this.selected_index() || 0;
138 }
137 }
139
138
140
139
141 Notebook.prototype.select = function (index) {
140 Notebook.prototype.select = function (index) {
142 if (index !== undefined && index >= 0 && index < this.ncells()) {
141 if (index !== undefined && index >= 0 && index < this.ncells()) {
143 if (this.selected_index() !== null) {
142 if (this.selected_index() !== null) {
144 this.selected_cell().unselect();
143 this.selected_cell().unselect();
145 };
144 };
146 this.cells()[index].select();
145 this.cells()[index].select();
147 };
146 };
148 return this;
147 return this;
149 };
148 };
150
149
151
150
152 Notebook.prototype.select_next = function () {
151 Notebook.prototype.select_next = function () {
153 var index = this.selected_index();
152 var index = this.selected_index();
154 if (index !== null && index >= 0 && (index+1) < this.ncells()) {
153 if (index !== null && index >= 0 && (index+1) < this.ncells()) {
155 this.select(index+1);
154 this.select(index+1);
156 };
155 };
157 return this;
156 return this;
158 };
157 };
159
158
160
159
161 Notebook.prototype.select_prev = function () {
160 Notebook.prototype.select_prev = function () {
162 var index = this.selected_index();
161 var index = this.selected_index();
163 if (index !== null && index >= 0 && (index-1) < this.ncells()) {
162 if (index !== null && index >= 0 && (index-1) < this.ncells()) {
164 this.select(index-1);
163 this.select(index-1);
165 };
164 };
166 return this;
165 return this;
167 };
166 };
168
167
169
168
170 Notebook.prototype.selected_index = function () {
169 Notebook.prototype.selected_index = function () {
171 var result = null;
170 var result = null;
172 this.cell_elements().filter(function (index) {
171 this.cell_elements().filter(function (index) {
173 if ($(this).data("cell").selected === true) {
172 if ($(this).data("cell").selected === true) {
174 result = index;
173 result = index;
175 };
174 };
176 });
175 });
177 return result;
176 return result;
178 };
177 };
179
178
180
179
181 Notebook.prototype.cell_for_msg = function (msg_id) {
180 Notebook.prototype.cell_for_msg = function (msg_id) {
182 var cell_id = this.msg_cell_map[msg_id];
181 var cell_id = this.msg_cell_map[msg_id];
183 var result = null;
182 var result = null;
184 this.cell_elements().filter(function (index) {
183 this.cell_elements().filter(function (index) {
185 cell = $(this).data("cell");
184 cell = $(this).data("cell");
186 if (cell.cell_id === cell_id) {
185 if (cell.cell_id === cell_id) {
187 result = cell;
186 result = cell;
188 };
187 };
189 });
188 });
190 return result;
189 return result;
191 };
190 };
192
191
193
192
194 Notebook.prototype.selected_cell = function () {
193 Notebook.prototype.selected_cell = function () {
195 return this.cell_elements().eq(this.selected_index()).data("cell");
194 return this.cell_elements().eq(this.selected_index()).data("cell");
196 }
195 }
197
196
198
197
199 // Cell insertion, deletion and moving.
198 // Cell insertion, deletion and moving.
200
199
201
200
202 Notebook.prototype.delete_cell = function (index) {
201 Notebook.prototype.delete_cell = function (index) {
203 var i = index || this.selected_index();
202 var i = index || this.selected_index();
204 if (i !== null && i >= 0 && i < this.ncells()) {
203 if (i !== null && i >= 0 && i < this.ncells()) {
205 this.cell_elements().eq(i).remove();
204 this.cell_elements().eq(i).remove();
206 if (i === (this.ncells())) {
205 if (i === (this.ncells())) {
207 this.select(i-1);
206 this.select(i-1);
208 } else {
207 } else {
209 this.select(i);
208 this.select(i);
210 };
209 };
211 };
210 };
212 return this;
211 return this;
213 };
212 };
214
213
215
214
216 Notebook.prototype.append_cell = function (cell) {
215 Notebook.prototype.append_cell = function (cell) {
217 this.element.append(cell.element);
216 this.element.append(cell.element);
218 return this;
217 return this;
219 };
218 };
220
219
221
220
222 Notebook.prototype.insert_cell_after = function (cell, index) {
221 Notebook.prototype.insert_cell_after = function (cell, index) {
223 var ncells = this.ncells();
222 var ncells = this.ncells();
224 if (ncells === 0) {
223 if (ncells === 0) {
225 this.append_cell(cell);
224 this.append_cell(cell);
226 return this;
225 return this;
227 };
226 };
228 if (index >= 0 && index < ncells) {
227 if (index >= 0 && index < ncells) {
229 this.cell_elements().eq(index).after(cell.element);
228 this.cell_elements().eq(index).after(cell.element);
230 };
229 };
231 return this
230 return this
232 };
231 };
233
232
234
233
235 Notebook.prototype.insert_cell_before = function (cell, index) {
234 Notebook.prototype.insert_cell_before = function (cell, index) {
236 var ncells = this.ncells();
235 var ncells = this.ncells();
237 if (ncells === 0) {
236 if (ncells === 0) {
238 this.append_cell(cell);
237 this.append_cell(cell);
239 return this;
238 return this;
240 };
239 };
241 if (index >= 0 && index < ncells) {
240 if (index >= 0 && index < ncells) {
242 this.cell_elements().eq(index).before(cell.element);
241 this.cell_elements().eq(index).before(cell.element);
243 };
242 };
244 return this;
243 return this;
245 };
244 };
246
245
247
246
248 Notebook.prototype.move_cell_up = function (index) {
247 Notebook.prototype.move_cell_up = function (index) {
249 var i = index || this.selected_index();
248 var i = index || this.selected_index();
250 if (i !== null && i < this.ncells() && i > 0) {
249 if (i !== null && i < this.ncells() && i > 0) {
251 var pivot = this.cell_elements().eq(i-1);
250 var pivot = this.cell_elements().eq(i-1);
252 var tomove = this.cell_elements().eq(i);
251 var tomove = this.cell_elements().eq(i);
253 if (pivot !== null && tomove !== null) {
252 if (pivot !== null && tomove !== null) {
254 tomove.detach();
253 tomove.detach();
255 pivot.before(tomove);
254 pivot.before(tomove);
256 this.select(i-1);
255 this.select(i-1);
257 };
256 };
258 };
257 };
259 return this;
258 return this;
260 }
259 }
261
260
262
261
263 Notebook.prototype.move_cell_down = function (index) {
262 Notebook.prototype.move_cell_down = function (index) {
264 var i = index || this.selected_index();
263 var i = index || this.selected_index();
265 if (i !== null && i < (this.ncells()-1) && i >= 0) {
264 if (i !== null && i < (this.ncells()-1) && i >= 0) {
266 var pivot = this.cell_elements().eq(i+1)
265 var pivot = this.cell_elements().eq(i+1)
267 var tomove = this.cell_elements().eq(i)
266 var tomove = this.cell_elements().eq(i)
268 if (pivot !== null && tomove !== null) {
267 if (pivot !== null && tomove !== null) {
269 tomove.detach();
268 tomove.detach();
270 pivot.after(tomove);
269 pivot.after(tomove);
271 this.select(i+1);
270 this.select(i+1);
272 };
271 };
273 };
272 };
274 return this;
273 return this;
275 }
274 }
276
275
277
276
278 Notebook.prototype.sort_cells = function () {
277 Notebook.prototype.sort_cells = function () {
279 var ncells = this.ncells();
278 var ncells = this.ncells();
280 var sindex = this.selected_index();
279 var sindex = this.selected_index();
281 var swapped;
280 var swapped;
282 do {
281 do {
283 swapped = false
282 swapped = false
284 for (var i=1; i<ncells; i++) {
283 for (var i=1; i<ncells; i++) {
285 current = this.cell_elements().eq(i).data("cell");
284 current = this.cell_elements().eq(i).data("cell");
286 previous = this.cell_elements().eq(i-1).data("cell");
285 previous = this.cell_elements().eq(i-1).data("cell");
287 if (previous.input_prompt_number > current.input_prompt_number) {
286 if (previous.input_prompt_number > current.input_prompt_number) {
288 this.move_cell_up(i);
287 this.move_cell_up(i);
289 swapped = true;
288 swapped = true;
290 };
289 };
291 };
290 };
292 } while (swapped);
291 } while (swapped);
293 this.select(sindex);
292 this.select(sindex);
294 return this;
293 return this;
295 };
294 };
296
295
297
296
298 Notebook.prototype.insert_code_cell_before = function (index) {
297 Notebook.prototype.insert_code_cell_before = function (index) {
299 // TODO: Bounds check for i
298 // TODO: Bounds check for i
300 var i = this.index_or_selected(index);
299 var i = this.index_or_selected(index);
301 var cell = new IPython.CodeCell(this);
300 var cell = new IPython.CodeCell(this);
302 cell.set_input_prompt(this.next_prompt_number);
301 cell.set_input_prompt(this.next_prompt_number);
303 this.next_prompt_number = this.next_prompt_number + 1;
302 this.next_prompt_number = this.next_prompt_number + 1;
304 this.insert_cell_before(cell, i);
303 this.insert_cell_before(cell, i);
305 this.select(this.find_cell_index(cell));
304 this.select(this.find_cell_index(cell));
306 return this;
305 return this;
307 }
306 }
308
307
309
308
310 Notebook.prototype.insert_code_cell_after = function (index) {
309 Notebook.prototype.insert_code_cell_after = function (index) {
311 // TODO: Bounds check for i
310 // TODO: Bounds check for i
312 var i = this.index_or_selected(index);
311 var i = this.index_or_selected(index);
313 var cell = new IPython.CodeCell(this);
312 var cell = new IPython.CodeCell(this);
314 cell.set_input_prompt(this.next_prompt_number);
313 cell.set_input_prompt(this.next_prompt_number);
315 this.next_prompt_number = this.next_prompt_number + 1;
314 this.next_prompt_number = this.next_prompt_number + 1;
316 this.insert_cell_after(cell, i);
315 this.insert_cell_after(cell, i);
317 this.select(this.find_cell_index(cell));
316 this.select(this.find_cell_index(cell));
318 return this;
317 return this;
319 }
318 }
320
319
321
320
322 Notebook.prototype.insert_text_cell_before = function (index) {
321 Notebook.prototype.insert_text_cell_before = function (index) {
323 // TODO: Bounds check for i
322 // TODO: Bounds check for i
324 var i = this.index_or_selected(index);
323 var i = this.index_or_selected(index);
325 var cell = new IPython.TextCell(this);
324 var cell = new IPython.TextCell(this);
326 cell.config_mathjax();
325 cell.config_mathjax();
327 this.insert_cell_before(cell, i);
326 this.insert_cell_before(cell, i);
328 this.select(this.find_cell_index(cell));
327 this.select(this.find_cell_index(cell));
329 return this;
328 return this;
330 }
329 }
331
330
332
331
333 Notebook.prototype.insert_text_cell_after = function (index) {
332 Notebook.prototype.insert_text_cell_after = function (index) {
334 // TODO: Bounds check for i
333 // TODO: Bounds check for i
335 var i = this.index_or_selected(index);
334 var i = this.index_or_selected(index);
336 var cell = new IPython.TextCell(this);
335 var cell = new IPython.TextCell(this);
337 cell.config_mathjax();
336 cell.config_mathjax();
338 this.insert_cell_after(cell, i);
337 this.insert_cell_after(cell, i);
339 this.select(this.find_cell_index(cell));
338 this.select(this.find_cell_index(cell));
340 return this;
339 return this;
341 }
340 }
342
341
343
342
344 Notebook.prototype.text_to_code = function (index) {
343 Notebook.prototype.text_to_code = function (index) {
345 // TODO: Bounds check for i
344 // TODO: Bounds check for i
346 var i = this.index_or_selected(index);
345 var i = this.index_or_selected(index);
347 var source_element = this.cell_elements().eq(i);
346 var source_element = this.cell_elements().eq(i);
348 var source_cell = source_element.data("cell");
347 var source_cell = source_element.data("cell");
349 if (source_cell instanceof IPython.TextCell) {
348 if (source_cell instanceof IPython.TextCell) {
350 this.insert_code_cell_after(i);
349 this.insert_code_cell_after(i);
351 var target_cell = this.cells()[i+1];
350 var target_cell = this.cells()[i+1];
352 target_cell.set_code(source_cell.get_text());
351 target_cell.set_code(source_cell.get_text());
353 source_element.remove();
352 source_element.remove();
354 };
353 };
355 };
354 };
356
355
357
356
358 Notebook.prototype.code_to_text = function (index) {
357 Notebook.prototype.code_to_text = function (index) {
359 // TODO: Bounds check for i
358 // TODO: Bounds check for i
360 var i = this.index_or_selected(index);
359 var i = this.index_or_selected(index);
361 var source_element = this.cell_elements().eq(i);
360 var source_element = this.cell_elements().eq(i);
362 var source_cell = source_element.data("cell");
361 var source_cell = source_element.data("cell");
363 if (source_cell instanceof IPython.CodeCell) {
362 if (source_cell instanceof IPython.CodeCell) {
364 this.insert_text_cell_after(i);
363 this.insert_text_cell_after(i);
365 var target_cell = this.cells()[i+1];
364 var target_cell = this.cells()[i+1];
366 var text = source_cell.get_code();
365 var text = source_cell.get_code();
367 if (text === "") {text = target_cell.placeholder;};
366 if (text === "") {text = target_cell.placeholder;};
368 target_cell.set_text(text);
367 target_cell.set_text(text);
369 source_element.remove();
368 source_element.remove();
370 target_cell.edit();
369 target_cell.edit();
371 };
370 };
372 };
371 };
373
372
374
373
375 // Cell collapsing
374 // Cell collapsing
376
375
377 Notebook.prototype.collapse = function (index) {
376 Notebook.prototype.collapse = function (index) {
378 var i = this.index_or_selected(index);
377 var i = this.index_or_selected(index);
379 this.cells()[i].collapse();
378 this.cells()[i].collapse();
380 };
379 };
381
380
382
381
383 Notebook.prototype.expand = function (index) {
382 Notebook.prototype.expand = function (index) {
384 var i = this.index_or_selected(index);
383 var i = this.index_or_selected(index);
385 this.cells()[i].expand();
384 this.cells()[i].expand();
386 };
385 };
387
386
388
387
389 // Kernel related things
388 // Kernel related things
390
389
391 Notebook.prototype.start_kernel = function () {
390 Notebook.prototype.start_kernel = function () {
392 this.kernel = new IPython.Kernel();
391 this.kernel = new IPython.Kernel();
393 this.kernel.start_kernel($.proxy(this.kernel_started, this));
392 this.kernel.start_kernel($.proxy(this.kernel_started, this));
394 };
393 };
395
394
396
395
397 Notebook.prototype.handle_shell_reply = function (e) {
396 Notebook.prototype.handle_shell_reply = function (e) {
398 reply = $.parseJSON(e.data);
397 reply = $.parseJSON(e.data);
399 var header = reply.header;
398 var header = reply.header;
400 var content = reply.content;
399 var content = reply.content;
401 var msg_type = header.msg_type;
400 var msg_type = header.msg_type;
402 console.log(reply);
401 console.log(reply);
403 var cell = this.cell_for_msg(reply.parent_header.msg_id);
402 var cell = this.cell_for_msg(reply.parent_header.msg_id);
404 if (msg_type === "execute_reply") {
403 if (msg_type === "execute_reply") {
405 cell.set_input_prompt(content.execution_count);
404 cell.set_input_prompt(content.execution_count);
406 };
405 };
407 var payload = content.payload || [];
406 var payload = content.payload || [];
408 this.handle_payload(content.payload);
407 this.handle_payload(content.payload);
409 };
408 };
410
409
411
410
412 Notebook.prototype.handle_payload = function (payload) {
411 Notebook.prototype.handle_payload = function (payload) {
413 var l = payload.length;
412 var l = payload.length;
414 if (l > 0) {
413 if (l > 0) {
415 IPython.pager.clear();
414 IPython.pager.clear();
416 IPython.pager.expand();
415 IPython.pager.expand();
417 };
416 };
418 for (var i=0; i<l; i++) {
417 for (var i=0; i<l; i++) {
419 IPython.pager.append_text(payload[i].text);
418 IPython.pager.append_text(payload[i].text);
420 };
419 };
421 };
420 };
422
421
423
422
424 Notebook.prototype.handle_iopub_reply = function (e) {
423 Notebook.prototype.handle_iopub_reply = function (e) {
425 reply = $.parseJSON(e.data);
424 reply = $.parseJSON(e.data);
426 var content = reply.content;
425 var content = reply.content;
427 // console.log(reply);
426 // console.log(reply);
428 var msg_type = reply.header.msg_type;
427 var msg_type = reply.header.msg_type;
429 var cell = this.cell_for_msg(reply.parent_header.msg_id);
428 var cell = this.cell_for_msg(reply.parent_header.msg_id);
430 if (msg_type === "stream") {
429 if (msg_type === "stream") {
431 cell.expand();
430 cell.expand();
432 cell.append_stream(content.data + "\n");
431 cell.append_stream(content.data + "\n");
433 } else if (msg_type === "display_data") {
432 } else if (msg_type === "display_data") {
434 cell.expand();
433 cell.expand();
435 cell.append_display_data(content.data);
434 cell.append_display_data(content.data);
436 } else if (msg_type === "pyout") {
435 } else if (msg_type === "pyout") {
437 cell.expand();
436 cell.expand();
438 cell.append_pyout(content.data, content.execution_count)
437 cell.append_pyout(content.data, content.execution_count)
439 } else if (msg_type === "pyerr") {
438 } else if (msg_type === "pyerr") {
440 cell.expand();
439 cell.expand();
441 cell.append_pyerr(content.ename, content.evalue, content.traceback);
440 cell.append_pyerr(content.ename, content.evalue, content.traceback);
442 } else if (msg_type === "status") {
441 } else if (msg_type === "status") {
443 if (content.execution_state === "busy") {
442 if (content.execution_state === "busy") {
444 this.kernel.status_busy();
443 this.kernel.status_busy();
445 } else if (content.execution_state === "idle") {
444 } else if (content.execution_state === "idle") {
446 this.kernel.status_idle();
445 this.kernel.status_idle();
447 };
446 };
448 }
447 }
449 };
448 };
450
449
451
450
452 Notebook.prototype.kernel_started = function () {
451 Notebook.prototype.kernel_started = function () {
453 console.log("Kernel started: ", this.kernel.kernel_id);
452 console.log("Kernel started: ", this.kernel.kernel_id);
454 this.kernel.shell_channel.onmessage = $.proxy(this.handle_shell_reply,this);
453 this.kernel.shell_channel.onmessage = $.proxy(this.handle_shell_reply,this);
455 this.kernel.iopub_channel.onmessage = $.proxy(this.handle_iopub_reply,this);
454 this.kernel.iopub_channel.onmessage = $.proxy(this.handle_iopub_reply,this);
456 };
455 };
457
456
458
457
459 // Persistance and loading
458 // Persistance and loading
460
459
461
460
462 Notebook.prototype.fromJSON = function (data) {
461 Notebook.prototype.fromJSON = function (data) {
463 var ncells = this.ncells();
462 var ncells = this.ncells();
464 for (var i=0; i<ncells; i++) {
463 for (var i=0; i<ncells; i++) {
465 // Always delete cell 0 as they get renumbered as they are deleted.
464 // Always delete cell 0 as they get renumbered as they are deleted.
466 this.delete_cell(0);
465 this.delete_cell(0);
467 };
466 };
468 var new_cells = data.cells;
467 var new_cells = data.cells;
469 ncells = new_cells.length;
468 ncells = new_cells.length;
470 var cell_data = null;
469 var cell_data = null;
471 for (var i=0; i<ncells; i++) {
470 for (var i=0; i<ncells; i++) {
472 cell_data = new_cells[i];
471 cell_data = new_cells[i];
473 if (cell_data.cell_type == 'code') {
472 if (cell_data.cell_type == 'code') {
474 this.insert_code_cell_after();
473 this.insert_code_cell_after();
475 this.selected_cell().fromJSON(cell_data);
474 this.selected_cell().fromJSON(cell_data);
476 } else if (cell_data.cell_type === 'text') {
475 } else if (cell_data.cell_type === 'text') {
477 this.insert_text_cell_after();
476 this.insert_text_cell_after();
478 this.selected_cell().fromJSON(cell_data);
477 this.selected_cell().fromJSON(cell_data);
479 };
478 };
480 };
479 };
481 };
480 };
482
481
483
482
484 Notebook.prototype.toJSON = function () {
483 Notebook.prototype.toJSON = function () {
485 var cells = this.cells();
484 var cells = this.cells();
486 var ncells = cells.length;
485 var ncells = cells.length;
487 cell_array = new Array(ncells);
486 cell_array = new Array(ncells);
488 for (var i=0; i<ncells; i++) {
487 for (var i=0; i<ncells; i++) {
489 cell_array[i] = cells[i].toJSON();
488 cell_array[i] = cells[i].toJSON();
490 };
489 };
491 json = {
490 json = {
492 cells : cell_array
491 cells : cell_array
493 };
492 };
494 return json
493 return json
495 };
494 };
496
495
497
496
498 Notebook.prototype.test_filename = function (filename) {
497 Notebook.prototype.test_filename = function (filename) {
499 if (this.notebook_filename_re.test(filename)) {
498 if (this.notebook_filename_re.test(filename)) {
500 return true;
499 return true;
501 } else {
500 } else {
502 var bad_filename = $('<div/>');
501 var bad_filename = $('<div/>');
503 bad_filename.html(
502 bad_filename.html(
504 "The filename you entered (" + filename + ") is not valid. Notebook filenames must have the following form: foo.ipynb"
503 "The filename you entered (" + filename + ") is not valid. Notebook filenames must have the following form: foo.ipynb"
505 );
504 );
506 bad_filename.dialog({title: 'Invalid filename', modal: true});
505 bad_filename.dialog({title: 'Invalid filename', modal: true});
507 return false;
506 return false;
508 };
507 };
509 };
508 };
510
509
511 Notebook.prototype.save_notebook = function (filename) {
510 Notebook.prototype.save_notebook = function (filename) {
512 this.filename = filename || this.filename || '';
511 this.filename = filename || this.filename || '';
513 if (this.filename === '') {
512 if (this.filename === '') {
514 var no_filename = $('<div/>');
513 var no_filename = $('<div/>');
515 no_filename.html(
514 no_filename.html(
516 "This notebook has no filename, please specify a filename of the form: foo.ipynb"
515 "This notebook has no filename, please specify a filename of the form: foo.ipynb"
517 );
516 );
518 no_filename.dialog({title: 'Missing filename', modal: true});
517 no_filename.dialog({title: 'Missing filename', modal: true});
519 return;
518 return;
520 }
519 }
521 if (!this.test_filename(this.filename)) {return;}
520 if (!this.test_filename(this.filename)) {return;}
522 var thedata = this.toJSON();
521 var thedata = this.toJSON();
523 var settings = {
522 var settings = {
524 processData : false,
523 processData : false,
525 cache : false,
524 cache : false,
526 type : "PUT",
525 type : "PUT",
527 data : JSON.stringify(thedata),
526 data : JSON.stringify(thedata),
528 success : function (data, status, xhr) {console.log(data);}
527 success : function (data, status, xhr) {console.log(data);}
529 };
528 };
530 $.ajax("/notebooks/" + this.filename, settings);
529 $.ajax("/notebooks/" + this.filename, settings);
531 };
530 };
532
531
533
532
534 Notebook.prototype.load_notebook = function (filename) {
533 Notebook.prototype.load_notebook = function (filename) {
535 if (!this.test_filename(filename)) {return;}
534 if (!this.test_filename(filename)) {return;}
536 var that = this;
535 var that = this;
537 // We do the call with settings so we can set cache to false.
536 // We do the call with settings so we can set cache to false.
538 var settings = {
537 var settings = {
539 processData : false,
538 processData : false,
540 cache : false,
539 cache : false,
541 type : "GET",
540 type : "GET",
542 dataType : "json",
541 dataType : "json",
543 success : function (data, status, xhr) {
542 success : function (data, status, xhr) {
544 that.fromJSON(data);
543 that.fromJSON(data);
545 that.filename = filename;
544 that.filename = filename;
546 that.kernel.restart();
545 that.kernel.restart();
547 }
546 }
548 };
547 };
549 $.ajax("/notebooks/" + filename, settings);
548 $.ajax("/notebooks/" + filename, settings);
550 }
549 }
551
550
552 IPython.Notebook = Notebook;
551 IPython.Notebook = Notebook;
553
552
554 return IPython;
553 return IPython;
555
554
556 }(IPython));
555 }(IPython));
557
556
@@ -1,101 +1,105 b''
1
1
2 //============================================================================
2 //============================================================================
3 // On document ready
3 // On document ready
4 //============================================================================
4 //============================================================================
5
5
6
6
7 $(document).ready(function () {
7 $(document).ready(function () {
8
8
9
9
10 $('div#notebook_app').addClass('border-box-sizing ui-widget ui-widget-content');
10 $('div#notebook_app').addClass('border-box-sizing ui-widget ui-widget-content');
11 $('div#left_panel').addClass('border-box-sizing ui-widget');
11 $('div#left_panel').addClass('border-box-sizing ui-widget');
12 $('div#left_panel_splitter').addClass('border-box-sizing ui-widget ui-state-default');
12 $('div#left_panel_splitter').addClass('border-box-sizing ui-widget ui-state-default');
13 $('div#notebook_panel').addClass('border-box-sizing ui-widget');
13 $('div#notebook_panel').addClass('border-box-sizing ui-widget');
14 $('div#notebook').addClass('border-box-sizing');
14 $('div#notebook').addClass('border-box-sizing');
15
15
16 $('div#left_panel_splitter').click(function () {
16 $('div#left_panel_splitter').click(function () {
17 $('div#left_panel').toggle('fast');
17 $('div#left_panel').toggle('fast');
18 });
18 });
19
19
20 $('div#left_panel_splitter').hover(
20 $('div#left_panel_splitter').hover(
21 function () {
21 function () {
22 $('div#left_panel_splitter').addClass('ui-state-hover');
22 $('div#left_panel_splitter').addClass('ui-state-hover');
23 },
23 },
24 function () {
24 function () {
25 $('div#left_panel_splitter').removeClass('ui-state-hover');
25 $('div#left_panel_splitter').removeClass('ui-state-hover');
26 }
26 }
27 );
27 );
28
28
29 MathJax.Hub.Config({
29 MathJax.Hub.Config({
30 tex2jax: {
30 tex2jax: {
31 inlineMath: [ ['$','$'], ["\\(","\\)"] ],
31 inlineMath: [ ['$','$'], ["\\(","\\)"] ],
32 displayMath: [ ['$$','$$'], ["\\[","\\]"] ],
32 displayMath: [ ['$$','$$'], ["\\[","\\]"] ],
33 },
33 },
34 displayAlign: 'left', // Change this to 'center' to center equations.
34 displayAlign: 'left', // Change this to 'center' to center equations.
35 "HTML-CSS": {
35 "HTML-CSS": {
36 styles: {'.MathJax_Display': {"margin": 0}}
36 styles: {'.MathJax_Display': {"margin": 0}}
37 }
37 }
38 });
38 });
39
39
40 var do_resize = function () {
40 var do_resize = function () {
41 var win = $(window);
41 var win = $(window);
42 var w = win.width();
42 var w = win.width();
43 var h = win.height();
43 var h = win.height();
44 var header_height = $('div#header').outerHeight(true);
44 var header_height = $('div#header').outerHeight(true);
45 var app_height = h - header_height - 2;
45 var app_height = h - header_height - 2; // content height
46 var pager_height = $('div#pager').outerHeight(true);
46 var pager_height = $('div#pager').outerHeight(true);
47 var pager_splitter_height = $('div#pager_splitter').outerHeight(true);
47 var pager_splitter_height = $('div#pager_splitter').outerHeight(true);
48 $('div#notebook_app').height(app_height + 2);
48 $('div#notebook_app').height(app_height + 2); // content+padding+border height
49 $('div#left_panel').height(app_height);
49 $('div#left_panel').height(app_height);
50 $('div#left_panel_splitter').height(app_height);
50 $('div#left_panel_splitter').height(app_height);
51 $('div#notebook_panel').height(app_height);
51 $('div#notebook_panel').height(app_height);
52 $('div#notebook').height(app_height-pager_height-pager_splitter_height);
52 if (IPython.pager.expanded) {
53 $('div#notebook').height(app_height-pager_height-pager_splitter_height);
54 } else {
55 $('div#notebook').height(app_height-pager_splitter_height);
56 }
53 console.log('resize: ', app_height);
57 console.log('resize: ', app_height);
54 };
58 };
55
59
56 $(window).resize(do_resize);
60 $(window).resize(do_resize);
57
61
58 IPython.notebook = new IPython.Notebook('div#notebook');
62 IPython.notebook = new IPython.Notebook('div#notebook');
59 IPython.notebook.insert_code_cell_after();
63 IPython.notebook.insert_code_cell_after();
60
64
61 IPython.pager = new IPython.Pager('div#pager', 'div#pager_splitter');
65 IPython.pager = new IPython.Pager('div#pager', 'div#pager_splitter');
62
66
63 do_resize();
67 do_resize();
64
68
65 // $("#menu_tabs").tabs();
69 // $("#menu_tabs").tabs();
66
70
67 // $("#help_toolbar").buttonset();
71 // $("#help_toolbar").buttonset();
68
72
69 // $("#kernel_toolbar").buttonset();
73 // $("#kernel_toolbar").buttonset();
70 // $("#interrupt_kernel").click(function () {IPython.notebook.kernel.interrupt();});
74 // $("#interrupt_kernel").click(function () {IPython.notebook.kernel.interrupt();});
71 // $("#restart_kernel").click(function () {IPython.notebook.kernel.restart();});
75 // $("#restart_kernel").click(function () {IPython.notebook.kernel.restart();});
72 // $("#kernel_status").addClass("status_idle");
76 // $("#kernel_status").addClass("status_idle");
73
77
74 // $("#move_cell").buttonset();
78 // $("#move_cell").buttonset();
75 // $("#move_up").button("option", "icons", {primary:"ui-icon-arrowthick-1-n"});
79 // $("#move_up").button("option", "icons", {primary:"ui-icon-arrowthick-1-n"});
76 // $("#move_up").button("option", "text", false);
80 // $("#move_up").button("option", "text", false);
77 // $("#move_up").click(function () {IPython.notebook.move_cell_up();});
81 // $("#move_up").click(function () {IPython.notebook.move_cell_up();});
78 // $("#move_down").button("option", "icons", {primary:"ui-icon-arrowthick-1-s"});
82 // $("#move_down").button("option", "icons", {primary:"ui-icon-arrowthick-1-s"});
79 // $("#move_down").button("option", "text", false);
83 // $("#move_down").button("option", "text", false);
80 // $("#move_down").click(function () {IPython.notebook.move_cell_down();});
84 // $("#move_down").click(function () {IPython.notebook.move_cell_down();});
81
85
82 // $("#insert_delete").buttonset();
86 // $("#insert_delete").buttonset();
83 // $("#insert_cell_before").click(function () {IPython.notebook.insert_code_cell_before();});
87 // $("#insert_cell_before").click(function () {IPython.notebook.insert_code_cell_before();});
84 // $("#insert_cell_after").click(function () {IPython.notebook.insert_code_cell_after();});
88 // $("#insert_cell_after").click(function () {IPython.notebook.insert_code_cell_after();});
85 // $("#delete_cell").button("option", "icons", {primary:"ui-icon-closethick"});
89 // $("#delete_cell").button("option", "icons", {primary:"ui-icon-closethick"});
86 // $("#delete_cell").button("option", "text", false);
90 // $("#delete_cell").button("option", "text", false);
87 // $("#delete_cell").click(function () {IPython.notebook.delete_cell();});
91 // $("#delete_cell").click(function () {IPython.notebook.delete_cell();});
88
92
89 // $("#cell_type").buttonset();
93 // $("#cell_type").buttonset();
90 // $("#to_code").click(function () {IPython.notebook.text_to_code();});
94 // $("#to_code").click(function () {IPython.notebook.text_to_code();});
91 // $("#to_text").click(function () {IPython.notebook.code_to_text();});
95 // $("#to_text").click(function () {IPython.notebook.code_to_text();});
92
96
93 // $("#sort").buttonset();
97 // $("#sort").buttonset();
94 // $("#sort_cells").click(function () {IPython.notebook.sort_cells();});
98 // $("#sort_cells").click(function () {IPython.notebook.sort_cells();});
95
99
96 // $("#toggle").buttonset();
100 // $("#toggle").buttonset();
97 // $("#collapse").click(function () {IPython.notebook.collapse();});
101 // $("#collapse").click(function () {IPython.notebook.collapse();});
98 // $("#expand").click(function () {IPython.notebook.expand();});
102 // $("#expand").click(function () {IPython.notebook.expand();});
99
103
100 });
104 });
101
105
General Comments 0
You need to be logged in to leave comments. Login now