##// END OF EJS Templates
fix kill_and_exit button in notebook
Zachary Sailer -
Show More
@@ -1,285 +1,285
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 // MenuBar
9 // MenuBar
10 //============================================================================
10 //============================================================================
11
11
12 /**
12 /**
13 * @module IPython
13 * @module IPython
14 * @namespace IPython
14 * @namespace IPython
15 * @submodule MenuBar
15 * @submodule MenuBar
16 */
16 */
17
17
18
18
19 var IPython = (function (IPython) {
19 var IPython = (function (IPython) {
20 "use strict";
20 "use strict";
21
21
22 /**
22 /**
23 * A MenuBar Class to generate the menubar of IPython noteboko
23 * A MenuBar Class to generate the menubar of IPython notebook
24 * @Class MenuBar
24 * @Class MenuBar
25 *
25 *
26 * @constructor
26 * @constructor
27 *
27 *
28 *
28 *
29 * @param selector {string} selector for the menubar element in DOM
29 * @param selector {string} selector for the menubar element in DOM
30 * @param {object} [options]
30 * @param {object} [options]
31 * @param [options.baseProjectUrl] {String} String to use for the
31 * @param [options.baseProjectUrl] {String} String to use for the
32 * Base Project url, default would be to inspect
32 * Base Project url, default would be to inspect
33 * $('body').data('baseProjectUrl');
33 * $('body').data('baseProjectUrl');
34 * does not support change for now is set through this option
34 * does not support change for now is set through this option
35 */
35 */
36 var MenuBar = function (selector, options) {
36 var MenuBar = function (selector, options) {
37 var options = options || {};
37 var options = options || {};
38 if(options.baseProjectUrl!= undefined){
38 if(options.baseProjectUrl!= undefined){
39 this._baseProjectUrl = options.baseProjectUrl;
39 this._baseProjectUrl = options.baseProjectUrl;
40 }
40 }
41 this.selector = selector;
41 this.selector = selector;
42 if (this.selector !== undefined) {
42 if (this.selector !== undefined) {
43 this.element = $(selector);
43 this.element = $(selector);
44 this.style();
44 this.style();
45 this.bind_events();
45 this.bind_events();
46 }
46 }
47 };
47 };
48
48
49 MenuBar.prototype.baseProjectUrl = function(){
49 MenuBar.prototype.baseProjectUrl = function(){
50 return this._baseProjectUrl || $('body').data('baseProjectUrl');
50 return this._baseProjectUrl || $('body').data('baseProjectUrl');
51 };
51 };
52
52
53 MenuBar.prototype.notebookPath = function() {
53 MenuBar.prototype.notebookPath = function() {
54 var path = $('body').data('notebookPath');
54 var path = $('body').data('notebookPath');
55 if (path != 'None') {
55 if (path != 'None') {
56 if (path[path.length-1] != '/') {
56 if (path[path.length-1] != '/') {
57 path = path.substring(0,path.length);
57 path = path.substring(0,path.length);
58 };
58 };
59 return path;
59 return path;
60 } else {
60 } else {
61 return '';
61 return '';
62 }
62 }
63 };
63 };
64
64
65 MenuBar.prototype.style = function () {
65 MenuBar.prototype.style = function () {
66 this.element.addClass('border-box-sizing');
66 this.element.addClass('border-box-sizing');
67 this.element.find("li").click(function (event, ui) {
67 this.element.find("li").click(function (event, ui) {
68 // The selected cell loses focus when the menu is entered, so we
68 // The selected cell loses focus when the menu is entered, so we
69 // re-select it upon selection.
69 // re-select it upon selection.
70 var i = IPython.notebook.get_selected_index();
70 var i = IPython.notebook.get_selected_index();
71 IPython.notebook.select(i);
71 IPython.notebook.select(i);
72 }
72 }
73 );
73 );
74 };
74 };
75
75
76
76
77 MenuBar.prototype.bind_events = function () {
77 MenuBar.prototype.bind_events = function () {
78 // File
78 // File
79 var that = this;
79 var that = this;
80 this.element.find('#new_notebook').click(function () {
80 this.element.find('#new_notebook').click(function () {
81 window.open(that.baseProjectUrl() + 'notebooks/' + that.notebookPath() +'new');
81 window.open(that.baseProjectUrl() + 'notebooks/' + that.notebookPath() +'new');
82 });
82 });
83 this.element.find('#open_notebook').click(function () {
83 this.element.find('#open_notebook').click(function () {
84 window.open(that.baseProjectUrl() + 'tree/' + that.notebookPath());
84 window.open(that.baseProjectUrl() + 'tree/' + that.notebookPath());
85 });
85 });
86 this.element.find('#copy_notebook').click(function () {
86 this.element.find('#copy_notebook').click(function () {
87 var notebook_name = IPython.notebook.get_notebook_name();
87 var notebook_name = IPython.notebook.get_notebook_name();
88 var url = that.baseProjectUrl() + 'notebooks/' + that.notebookPath() + notebook_name + '/copy';
88 var url = that.baseProjectUrl() + 'notebooks/' + that.notebookPath() + notebook_name + '/copy';
89 window.open(url,'_blank');
89 window.open(url,'_blank');
90 return false;
90 return false;
91 });
91 });
92 this.element.find('#download_ipynb').click(function () {
92 this.element.find('#download_ipynb').click(function () {
93 var notebook_name = IPython.notebook.get_notebook_name();
93 var notebook_name = IPython.notebook.get_notebook_name();
94 var url = that.baseProjectUrl() + 'api/notebooks/' +
94 var url = that.baseProjectUrl() + 'api/notebooks/' +
95 notebook_name + '?format=json';
95 notebook_name + '?format=json';
96 window.location.assign(url);
96 window.location.assign(url);
97 });
97 });
98 this.element.find('#download_py').click(function () {
98 this.element.find('#download_py').click(function () {
99 var notebook_name = IPython.notebook.get_notebook_name();
99 var notebook_name = IPython.notebook.get_notebook_name();
100 var url = that.baseProjectUrl() + 'api/notebooks/' +
100 var url = that.baseProjectUrl() + 'api/notebooks/' +
101 notebook_name + '?format=py';
101 notebook_name + '?format=py';
102 window.location.assign(url);
102 window.location.assign(url);
103 });
103 });
104 this.element.find('#rename_notebook').click(function () {
104 this.element.find('#rename_notebook').click(function () {
105 IPython.save_widget.rename_notebook();
105 IPython.save_widget.rename_notebook();
106 });
106 });
107 this.element.find('#save_checkpoint').click(function () {
107 this.element.find('#save_checkpoint').click(function () {
108 IPython.notebook.save_checkpoint();
108 IPython.notebook.save_checkpoint();
109 });
109 });
110 this.element.find('#restore_checkpoint').click(function () {
110 this.element.find('#restore_checkpoint').click(function () {
111 });
111 });
112 this.element.find('#kill_and_exit').click(function () {
112 this.element.find('#kill_and_exit').click(function () {
113 IPython.notebook.kernel.kill();
113 IPython.notebook.session.delete_session();
114 setTimeout(function(){window.close();}, 200);
114 setTimeout(function(){window.close();}, 500);
115 });
115 });
116 // Edit
116 // Edit
117 this.element.find('#cut_cell').click(function () {
117 this.element.find('#cut_cell').click(function () {
118 IPython.notebook.cut_cell();
118 IPython.notebook.cut_cell();
119 });
119 });
120 this.element.find('#copy_cell').click(function () {
120 this.element.find('#copy_cell').click(function () {
121 IPython.notebook.copy_cell();
121 IPython.notebook.copy_cell();
122 });
122 });
123 this.element.find('#delete_cell').click(function () {
123 this.element.find('#delete_cell').click(function () {
124 IPython.notebook.delete_cell();
124 IPython.notebook.delete_cell();
125 });
125 });
126 this.element.find('#undelete_cell').click(function () {
126 this.element.find('#undelete_cell').click(function () {
127 IPython.notebook.undelete();
127 IPython.notebook.undelete();
128 });
128 });
129 this.element.find('#split_cell').click(function () {
129 this.element.find('#split_cell').click(function () {
130 IPython.notebook.split_cell();
130 IPython.notebook.split_cell();
131 });
131 });
132 this.element.find('#merge_cell_above').click(function () {
132 this.element.find('#merge_cell_above').click(function () {
133 IPython.notebook.merge_cell_above();
133 IPython.notebook.merge_cell_above();
134 });
134 });
135 this.element.find('#merge_cell_below').click(function () {
135 this.element.find('#merge_cell_below').click(function () {
136 IPython.notebook.merge_cell_below();
136 IPython.notebook.merge_cell_below();
137 });
137 });
138 this.element.find('#move_cell_up').click(function () {
138 this.element.find('#move_cell_up').click(function () {
139 IPython.notebook.move_cell_up();
139 IPython.notebook.move_cell_up();
140 });
140 });
141 this.element.find('#move_cell_down').click(function () {
141 this.element.find('#move_cell_down').click(function () {
142 IPython.notebook.move_cell_down();
142 IPython.notebook.move_cell_down();
143 });
143 });
144 this.element.find('#select_previous').click(function () {
144 this.element.find('#select_previous').click(function () {
145 IPython.notebook.select_prev();
145 IPython.notebook.select_prev();
146 });
146 });
147 this.element.find('#select_next').click(function () {
147 this.element.find('#select_next').click(function () {
148 IPython.notebook.select_next();
148 IPython.notebook.select_next();
149 });
149 });
150 this.element.find('#edit_nb_metadata').click(function () {
150 this.element.find('#edit_nb_metadata').click(function () {
151 IPython.notebook.edit_metadata();
151 IPython.notebook.edit_metadata();
152 });
152 });
153
153
154 // View
154 // View
155 this.element.find('#toggle_header').click(function () {
155 this.element.find('#toggle_header').click(function () {
156 $('div#header').toggle();
156 $('div#header').toggle();
157 IPython.layout_manager.do_resize();
157 IPython.layout_manager.do_resize();
158 });
158 });
159 this.element.find('#toggle_toolbar').click(function () {
159 this.element.find('#toggle_toolbar').click(function () {
160 $('div#maintoolbar').toggle();
160 $('div#maintoolbar').toggle();
161 IPython.layout_manager.do_resize();
161 IPython.layout_manager.do_resize();
162 });
162 });
163 // Insert
163 // Insert
164 this.element.find('#insert_cell_above').click(function () {
164 this.element.find('#insert_cell_above').click(function () {
165 IPython.notebook.insert_cell_above('code');
165 IPython.notebook.insert_cell_above('code');
166 });
166 });
167 this.element.find('#insert_cell_below').click(function () {
167 this.element.find('#insert_cell_below').click(function () {
168 IPython.notebook.insert_cell_below('code');
168 IPython.notebook.insert_cell_below('code');
169 });
169 });
170 // Cell
170 // Cell
171 this.element.find('#run_cell').click(function () {
171 this.element.find('#run_cell').click(function () {
172 IPython.notebook.execute_selected_cell();
172 IPython.notebook.execute_selected_cell();
173 });
173 });
174 this.element.find('#run_cell_in_place').click(function () {
174 this.element.find('#run_cell_in_place').click(function () {
175 IPython.notebook.execute_selected_cell({terminal:true});
175 IPython.notebook.execute_selected_cell({terminal:true});
176 });
176 });
177 this.element.find('#run_all_cells').click(function () {
177 this.element.find('#run_all_cells').click(function () {
178 IPython.notebook.execute_all_cells();
178 IPython.notebook.execute_all_cells();
179 }).attr('title', 'Run all cells in the notebook');
179 }).attr('title', 'Run all cells in the notebook');
180 this.element.find('#run_all_cells_above').click(function () {
180 this.element.find('#run_all_cells_above').click(function () {
181 IPython.notebook.execute_cells_above();
181 IPython.notebook.execute_cells_above();
182 }).attr('title', 'Run all cells above (but not including) this cell');
182 }).attr('title', 'Run all cells above (but not including) this cell');
183 this.element.find('#run_all_cells_below').click(function () {
183 this.element.find('#run_all_cells_below').click(function () {
184 IPython.notebook.execute_cells_below();
184 IPython.notebook.execute_cells_below();
185 }).attr('title', 'Run this cell and all cells below it');
185 }).attr('title', 'Run this cell and all cells below it');
186 this.element.find('#to_code').click(function () {
186 this.element.find('#to_code').click(function () {
187 IPython.notebook.to_code();
187 IPython.notebook.to_code();
188 });
188 });
189 this.element.find('#to_markdown').click(function () {
189 this.element.find('#to_markdown').click(function () {
190 IPython.notebook.to_markdown();
190 IPython.notebook.to_markdown();
191 });
191 });
192 this.element.find('#to_raw').click(function () {
192 this.element.find('#to_raw').click(function () {
193 IPython.notebook.to_raw();
193 IPython.notebook.to_raw();
194 });
194 });
195 this.element.find('#to_heading1').click(function () {
195 this.element.find('#to_heading1').click(function () {
196 IPython.notebook.to_heading(undefined, 1);
196 IPython.notebook.to_heading(undefined, 1);
197 });
197 });
198 this.element.find('#to_heading2').click(function () {
198 this.element.find('#to_heading2').click(function () {
199 IPython.notebook.to_heading(undefined, 2);
199 IPython.notebook.to_heading(undefined, 2);
200 });
200 });
201 this.element.find('#to_heading3').click(function () {
201 this.element.find('#to_heading3').click(function () {
202 IPython.notebook.to_heading(undefined, 3);
202 IPython.notebook.to_heading(undefined, 3);
203 });
203 });
204 this.element.find('#to_heading4').click(function () {
204 this.element.find('#to_heading4').click(function () {
205 IPython.notebook.to_heading(undefined, 4);
205 IPython.notebook.to_heading(undefined, 4);
206 });
206 });
207 this.element.find('#to_heading5').click(function () {
207 this.element.find('#to_heading5').click(function () {
208 IPython.notebook.to_heading(undefined, 5);
208 IPython.notebook.to_heading(undefined, 5);
209 });
209 });
210 this.element.find('#to_heading6').click(function () {
210 this.element.find('#to_heading6').click(function () {
211 IPython.notebook.to_heading(undefined, 6);
211 IPython.notebook.to_heading(undefined, 6);
212 });
212 });
213 this.element.find('#toggle_output').click(function () {
213 this.element.find('#toggle_output').click(function () {
214 IPython.notebook.toggle_output();
214 IPython.notebook.toggle_output();
215 });
215 });
216 this.element.find('#collapse_all_output').click(function () {
216 this.element.find('#collapse_all_output').click(function () {
217 IPython.notebook.collapse_all_output();
217 IPython.notebook.collapse_all_output();
218 });
218 });
219 this.element.find('#scroll_all_output').click(function () {
219 this.element.find('#scroll_all_output').click(function () {
220 IPython.notebook.scroll_all_output();
220 IPython.notebook.scroll_all_output();
221 });
221 });
222 this.element.find('#expand_all_output').click(function () {
222 this.element.find('#expand_all_output').click(function () {
223 IPython.notebook.expand_all_output();
223 IPython.notebook.expand_all_output();
224 });
224 });
225 this.element.find('#clear_all_output').click(function () {
225 this.element.find('#clear_all_output').click(function () {
226 IPython.notebook.clear_all_output();
226 IPython.notebook.clear_all_output();
227 });
227 });
228 // Kernel
228 // Kernel
229 this.element.find('#int_kernel').click(function () {
229 this.element.find('#int_kernel').click(function () {
230 IPython.notebook.session.interrupt_kernel();
230 IPython.notebook.session.interrupt_kernel();
231 });
231 });
232 this.element.find('#restart_kernel').click(function () {
232 this.element.find('#restart_kernel').click(function () {
233 IPython.notebook.restart_kernel();
233 IPython.notebook.restart_kernel();
234 });
234 });
235 // Help
235 // Help
236 this.element.find('#keyboard_shortcuts').click(function () {
236 this.element.find('#keyboard_shortcuts').click(function () {
237 IPython.quick_help.show_keyboard_shortcuts();
237 IPython.quick_help.show_keyboard_shortcuts();
238 });
238 });
239
239
240 this.update_restore_checkpoint(null);
240 this.update_restore_checkpoint(null);
241
241
242 $([IPython.events]).on('checkpoints_listed.Notebook', function (event, data) {
242 $([IPython.events]).on('checkpoints_listed.Notebook', function (event, data) {
243 that.update_restore_checkpoint(IPython.notebook.checkpoints);
243 that.update_restore_checkpoint(IPython.notebook.checkpoints);
244 });
244 });
245
245
246 $([IPython.events]).on('checkpoint_created.Notebook', function (event, data) {
246 $([IPython.events]).on('checkpoint_created.Notebook', function (event, data) {
247 that.update_restore_checkpoint(IPython.notebook.checkpoints);
247 that.update_restore_checkpoint(IPython.notebook.checkpoints);
248 });
248 });
249 };
249 };
250
250
251 MenuBar.prototype.update_restore_checkpoint = function(checkpoints) {
251 MenuBar.prototype.update_restore_checkpoint = function(checkpoints) {
252 var ul = this.element.find("#restore_checkpoint").find("ul");
252 var ul = this.element.find("#restore_checkpoint").find("ul");
253 ul.empty();
253 ul.empty();
254 if (! checkpoints || checkpoints.length == 0) {
254 if (! checkpoints || checkpoints.length == 0) {
255 ul.append(
255 ul.append(
256 $("<li/>")
256 $("<li/>")
257 .addClass("disabled")
257 .addClass("disabled")
258 .append(
258 .append(
259 $("<a/>")
259 $("<a/>")
260 .text("No checkpoints")
260 .text("No checkpoints")
261 )
261 )
262 );
262 );
263 return;
263 return;
264 };
264 };
265
265
266 checkpoints.map(function (checkpoint) {
266 checkpoints.map(function (checkpoint) {
267 var d = new Date(checkpoint.last_modified);
267 var d = new Date(checkpoint.last_modified);
268 ul.append(
268 ul.append(
269 $("<li/>").append(
269 $("<li/>").append(
270 $("<a/>")
270 $("<a/>")
271 .attr("href", "#")
271 .attr("href", "#")
272 .text(d.format("mmm dd HH:MM:ss"))
272 .text(d.format("mmm dd HH:MM:ss"))
273 .click(function () {
273 .click(function () {
274 IPython.notebook.restore_checkpoint_dialog(checkpoint);
274 IPython.notebook.restore_checkpoint_dialog(checkpoint);
275 })
275 })
276 )
276 )
277 );
277 );
278 });
278 });
279 };
279 };
280
280
281 IPython.MenuBar = MenuBar;
281 IPython.MenuBar = MenuBar;
282
282
283 return IPython;
283 return IPython;
284
284
285 }(IPython));
285 }(IPython));
@@ -1,74 +1,90
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 Session = function(notebook_path, Notebook){
14 var Session = function(notebook_path, Notebook){
15 this.kernel = null;
15 this.kernel = null;
16 this.kernel_id = null;
16 this.kernel_id = null;
17 this.session_id = null;
17 this.session_id = null;
18 this.notebook_path = notebook_path;
18 this.notebook_path = notebook_path;
19 this.notebook = Notebook;
19 this.notebook = Notebook;
20 this._baseProjectUrl = Notebook.baseProjectUrl()
20 };
21 };
21
22
22 Session.prototype.start = function(){
23 Session.prototype.start = function(){
23 var that = this
24 var that = this
24 var qs = $.param({notebook_path:this.notebook_path});
25 var qs = $.param({notebook_path:this.notebook_path});
25 var url = '/api/sessions' + '?' + qs;
26 var url = '/api/sessions' + '?' + qs;
26 $.post(url,
27 $.post(url,
27 $.proxy(this.start_kernel, that),
28 $.proxy(this.start_kernel, that),
28 'json'
29 'json'
29 );
30 );
30 };
31 };
31
32
32 // Kernel related things
33 // Kernel related things
33
34
34 /**
35 /**
35 * Start a new kernel and set it on each code cell.
36 * Start a new kernel and set it on each code cell.
36 *
37 *
37 * @method start_kernel
38 * @method start_kernel
38 */
39 */
39 Session.prototype.start_kernel = function (json) {
40 Session.prototype.start_kernel = function (json) {
40 this.session_id = json.session_id;
41 this.session_id = json.session_id;
41 this.kernel_content = json.kernel;
42 this.kernel_content = json.kernel;
42 var base_url = $('body').data('baseKernelUrl') + "api/kernels";
43 var base_url = $('body').data('baseKernelUrl') + "api/kernels";
43 this.kernel = new IPython.Kernel(base_url, this.session_id);
44 this.kernel = new IPython.Kernel(base_url, this.session_id);
44 // Now that the kernel has been created, tell the CodeCells about it.
45 // Now that the kernel has been created, tell the CodeCells about it.
45 this.kernel._kernel_started(this.kernel_content)
46 this.kernel._kernel_started(this.kernel_content)
46 var ncells = this.notebook.ncells();
47 var ncells = this.notebook.ncells();
47 for (var i=0; i<ncells; i++) {
48 for (var i=0; i<ncells; i++) {
48 var cell = this.notebook.get_cell(i);
49 var cell = this.notebook.get_cell(i);
49 if (cell instanceof IPython.CodeCell) {
50 if (cell instanceof IPython.CodeCell) {
50 cell.set_kernel(this.kernel)
51 cell.set_kernel(this.kernel)
51 };
52 };
52 };
53 };
53
54
54 };
55 };
55
56
56 /**
57 /**
57 * Prompt the user to restart the IPython kernel.
58 * Prompt the user to restart the IPython kernel.
58 *
59 *
59 * @method restart_kernel
60 * @method restart_kernel
60 */
61 */
61 Session.prototype.restart_kernel = function () {
62 Session.prototype.restart_kernel = function () {
62 this.kernel.restart();
63 this.kernel.restart();
63 };
64 };
64
65
65 Session.prototype.interrupt_kernel = function() {
66 Session.prototype.interrupt_kernel = function() {
66 this.kernel.interrupt();
67 this.kernel.interrupt();
67 }
68 };
69
70 Session.prototype.delete_session = function() {
71 var settings = {
72 processData : false,
73 cache : false,
74 type : "DELETE",
75 dataType : "json",
76 };
77 var url = this._baseProjectUrl + 'api/sessions/' + this.session_id;
78 $.ajax(url, settings);
79 };
80
81 Session.prototype.kill_kernel = function() {
82 this.kernel.kill();
83 };
68
84
69 IPython.Session = Session;
85 IPython.Session = Session;
70
86
71
87
72 return IPython;
88 return IPython;
73
89
74 }(IPython));
90 }(IPython));
@@ -1,350 +1,349
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 // NotebookList
9 // NotebookList
10 //============================================================================
10 //============================================================================
11
11
12 var IPython = (function (IPython) {
12 var IPython = (function (IPython) {
13
13
14 var NotebookList = function (selector) {
14 var NotebookList = function (selector) {
15 this.selector = selector;
15 this.selector = selector;
16 if (this.selector !== undefined) {
16 if (this.selector !== undefined) {
17 this.element = $(selector);
17 this.element = $(selector);
18 this.style();
18 this.style();
19 this.bind_events();
19 this.bind_events();
20 }
20 }
21 this.notebooks_list = new Array();
21 this.notebooks_list = new Array();
22 this.sessions = new Object();
22 this.sessions = new Object();
23 };
23 };
24
24
25 NotebookList.prototype.baseProjectUrl = function () {
25 NotebookList.prototype.baseProjectUrl = function () {
26 return $('body').data('baseProjectUrl');
26 return $('body').data('baseProjectUrl');
27 };
27 };
28
28
29 NotebookList.prototype.notebookPath = function() {
29 NotebookList.prototype.notebookPath = function() {
30 var path = $('body').data('notebookPath');
30 var path = $('body').data('notebookPath');
31 if (path != "") {
31 if (path != "") {
32 if (path[path.length-1] != '/') {
32 if (path[path.length-1] != '/') {
33 path = path.substring(0,path.length);
33 path = path.substring(0,path.length);
34 };
34 };
35 return path;
35 return path;
36 } else {
36 } else {
37 return path;
37 return path;
38 };
38 };
39 };
39 };
40
40
41 NotebookList.prototype.url_name = function(name){
41 NotebookList.prototype.url_name = function(name){
42 return encodeURIComponent(name);
42 return encodeURIComponent(name);
43 };
43 };
44
44
45 NotebookList.prototype.style = function () {
45 NotebookList.prototype.style = function () {
46 $('#notebook_toolbar').addClass('list_toolbar');
46 $('#notebook_toolbar').addClass('list_toolbar');
47 $('#drag_info').addClass('toolbar_info');
47 $('#drag_info').addClass('toolbar_info');
48 $('#notebook_buttons').addClass('toolbar_buttons');
48 $('#notebook_buttons').addClass('toolbar_buttons');
49 $('#notebook_list_header').addClass('list_header');
49 $('#notebook_list_header').addClass('list_header');
50 this.element.addClass("list_container");
50 this.element.addClass("list_container");
51 };
51 };
52
52
53
53
54 NotebookList.prototype.bind_events = function () {
54 NotebookList.prototype.bind_events = function () {
55 var that = this;
55 var that = this;
56 $('#refresh_notebook_list').click(function () {
56 $('#refresh_notebook_list').click(function () {
57 that.load_list();
57 that.load_list();
58 });
58 });
59 this.element.bind('dragover', function () {
59 this.element.bind('dragover', function () {
60 return false;
60 return false;
61 });
61 });
62 this.element.bind('drop', function(event){
62 this.element.bind('drop', function(event){
63 that.handelFilesUpload(event,'drop');
63 that.handelFilesUpload(event,'drop');
64 return false;
64 return false;
65 });
65 });
66 };
66 };
67
67
68 NotebookList.prototype.handelFilesUpload = function(event, dropOrForm) {
68 NotebookList.prototype.handelFilesUpload = function(event, dropOrForm) {
69 var that = this;
69 var that = this;
70 var files;
70 var files;
71 if(dropOrForm =='drop'){
71 if(dropOrForm =='drop'){
72 files = event.originalEvent.dataTransfer.files;
72 files = event.originalEvent.dataTransfer.files;
73 } else
73 } else
74 {
74 {
75 files = event.originalEvent.target.files
75 files = event.originalEvent.target.files
76 }
76 }
77 for (var i = 0, f; f = files[i]; i++) {
77 for (var i = 0, f; f = files[i]; i++) {
78 var reader = new FileReader();
78 var reader = new FileReader();
79 reader.readAsText(f);
79 reader.readAsText(f);
80 var fname = f.name.split('.');
80 var fname = f.name.split('.');
81 var nbname = fname.slice(0,-1).join('.');
81 var nbname = fname.slice(0,-1).join('.');
82 var nbformat = fname.slice(-1)[0];
82 var nbformat = fname.slice(-1)[0];
83 if (nbformat === 'ipynb') {nbformat = 'json';};
83 if (nbformat === 'ipynb') {nbformat = 'json';};
84 if (nbformat === 'py' || nbformat === 'json') {
84 if (nbformat === 'py' || nbformat === 'json') {
85 var item = that.new_notebook_item(0);
85 var item = that.new_notebook_item(0);
86 that.add_name_input(nbname, item);
86 that.add_name_input(nbname, item);
87 item.data('nbformat', nbformat);
87 item.data('nbformat', nbformat);
88 // Store the notebook item in the reader so we can use it later
88 // Store the notebook item in the reader so we can use it later
89 // to know which item it belongs to.
89 // to know which item it belongs to.
90 $(reader).data('item', item);
90 $(reader).data('item', item);
91 reader.onload = function (event) {
91 reader.onload = function (event) {
92 var nbitem = $(event.target).data('item');
92 var nbitem = $(event.target).data('item');
93 that.add_notebook_data(event.target.result, nbitem);
93 that.add_notebook_data(event.target.result, nbitem);
94 that.add_upload_button(nbitem);
94 that.add_upload_button(nbitem);
95 };
95 };
96 };
96 };
97 }
97 }
98 return false;
98 return false;
99 };
99 };
100
100
101 NotebookList.prototype.clear_list = function () {
101 NotebookList.prototype.clear_list = function () {
102 this.element.children('.list_item').remove();
102 this.element.children('.list_item').remove();
103 };
103 };
104
104
105 NotebookList.prototype.load_sessions = function(){
105 NotebookList.prototype.load_sessions = function(){
106 var settings = {
106 var settings = {
107 processData : false,
107 processData : false,
108 cache : false,
108 cache : false,
109 type : "GET",
109 type : "GET",
110 dataType : "json",
110 dataType : "json",
111 success : $.proxy(this.sessions_loaded, this)
111 success : $.proxy(this.sessions_loaded, this)
112 };
112 };
113 var url = this.baseProjectUrl() + 'api/sessions';
113 var url = this.baseProjectUrl() + 'api/sessions';
114 console.log("THIS IS A TEST");
115 $.ajax(url,settings);
114 $.ajax(url,settings);
116 };
115 };
117
116
118
117
119 NotebookList.prototype.sessions_loaded = function(data){
118 NotebookList.prototype.sessions_loaded = function(data){
120 this.sessions=new Object();
119 this.sessions=new Object();
121 var len = data.length;
120 var len = data.length;
122 if (len != 0) {
121 if (len != 0) {
123 for (var i=0; i<len; i++) {
122 for (var i=0; i<len; i++) {
124 if (data[i]['notebook_path']==null) {
123 if (data[i]['notebook_path']==null) {
125 nb_path = data[i]['notebook_name'];
124 nb_path = data[i]['notebook_name'];
126 }
125 }
127 else {
126 else {
128 nb_path = data[i]['notebook_path'] + data[i]['notebook_name'];
127 nb_path = data[i]['notebook_path'] + data[i]['notebook_name'];
129 }
128 }
130 this.sessions[nb_path]= data[i]['session_id'];
129 this.sessions[nb_path]= data[i]['session_id'];
131 }
130 }
132 };
131 };
133 this.load_list();
132 this.load_list();
134 };
133 };
135
134
136 NotebookList.prototype.load_list = function () {
135 NotebookList.prototype.load_list = function () {
137 var that = this;
136 var that = this;
138 var settings = {
137 var settings = {
139 processData : false,
138 processData : false,
140 cache : false,
139 cache : false,
141 type : "GET",
140 type : "GET",
142 dataType : "json",
141 dataType : "json",
143 success : $.proxy(this.list_loaded, this),
142 success : $.proxy(this.list_loaded, this),
144 error : $.proxy( function(){
143 error : $.proxy( function(){
145 that.list_loaded([], null, null, {msg:"Error connecting to server."});
144 that.list_loaded([], null, null, {msg:"Error connecting to server."});
146 },this)
145 },this)
147 };
146 };
148
147
149 var url = this.baseProjectUrl() + 'api/notebooks/' + this.notebookPath();
148 var url = this.baseProjectUrl() + 'api/notebooks/' + this.notebookPath();
150 $.ajax(url, settings);
149 $.ajax(url, settings);
151 };
150 };
152
151
153
152
154 NotebookList.prototype.list_loaded = function (data, status, xhr, param) {
153 NotebookList.prototype.list_loaded = function (data, status, xhr, param) {
155 var message = 'Notebook list empty.';
154 var message = 'Notebook list empty.';
156 if (param !== undefined && param.msg) {
155 if (param !== undefined && param.msg) {
157 var message = param.msg;
156 var message = param.msg;
158 }
157 }
159 var len = data.length;
158 var len = data.length;
160 this.clear_list();
159 this.clear_list();
161 if(len == 0)
160 if(len == 0)
162 {
161 {
163 $(this.new_notebook_item(0))
162 $(this.new_notebook_item(0))
164 .append(
163 .append(
165 $('<div style="margin:auto;text-align:center;color:grey"/>')
164 $('<div style="margin:auto;text-align:center;color:grey"/>')
166 .text(message)
165 .text(message)
167 )
166 )
168 }
167 }
169 for (var i=0; i<len; i++) {
168 for (var i=0; i<len; i++) {
170 var name = data[i].notebook_name;
169 var name = data[i].notebook_name;
171 var path = this.notebookPath();
170 var path = this.notebookPath();
172 var nbname = name.split(".")[0];
171 var nbname = name.split(".")[0];
173 var item = this.new_notebook_item(i);
172 var item = this.new_notebook_item(i);
174 this.add_link(path, nbname, item);
173 this.add_link(path, nbname, item);
175 name = this.notebookPath() + name;
174 name = this.notebookPath() + name;
176 if(this.sessions[name] == undefined){
175 if(this.sessions[name] == undefined){
177 this.add_delete_button(item);
176 this.add_delete_button(item);
178 } else {
177 } else {
179 this.add_shutdown_button(item,this.sessions[name]);
178 this.add_shutdown_button(item,this.sessions[name]);
180 }
179 }
181 };
180 };
182 };
181 };
183
182
184
183
185 NotebookList.prototype.new_notebook_item = function (index) {
184 NotebookList.prototype.new_notebook_item = function (index) {
186 var item = $('<div/>').addClass("list_item").addClass("row-fluid");
185 var item = $('<div/>').addClass("list_item").addClass("row-fluid");
187 // item.addClass('list_item ui-widget ui-widget-content ui-helper-clearfix');
186 // item.addClass('list_item ui-widget ui-widget-content ui-helper-clearfix');
188 // item.css('border-top-style','none');
187 // item.css('border-top-style','none');
189 item.append($("<div/>").addClass("span12").append(
188 item.append($("<div/>").addClass("span12").append(
190 $("<a/>").addClass("item_link").append(
189 $("<a/>").addClass("item_link").append(
191 $("<span/>").addClass("item_name")
190 $("<span/>").addClass("item_name")
192 )
191 )
193 ).append(
192 ).append(
194 $('<div/>').addClass("item_buttons btn-group pull-right")
193 $('<div/>').addClass("item_buttons btn-group pull-right")
195 ));
194 ));
196
195
197 if (index === -1) {
196 if (index === -1) {
198 this.element.append(item);
197 this.element.append(item);
199 } else {
198 } else {
200 this.element.children().eq(index).after(item);
199 this.element.children().eq(index).after(item);
201 }
200 }
202 return item;
201 return item;
203 };
202 };
204
203
205
204
206 NotebookList.prototype.add_link = function (path, nbname, item) {
205 NotebookList.prototype.add_link = function (path, nbname, item) {
207 item.data('nbname', nbname);
206 item.data('nbname', nbname);
208 item.data('path', path);
207 item.data('path', path);
209 item.find(".item_name").text(nbname);
208 item.find(".item_name").text(nbname);
210 item.find("a.item_link")
209 item.find("a.item_link")
211 .attr('href', this.baseProjectUrl() + "notebooks/" + this.notebookPath() + nbname + ".ipynb")
210 .attr('href', this.baseProjectUrl() + "notebooks/" + this.notebookPath() + nbname + ".ipynb")
212 .attr('target','_blank');
211 .attr('target','_blank');
213 };
212 };
214
213
215
214
216 NotebookList.prototype.add_name_input = function (nbname, item) {
215 NotebookList.prototype.add_name_input = function (nbname, item) {
217 item.data('nbname', nbname);
216 item.data('nbname', nbname);
218 item.find(".item_name").empty().append(
217 item.find(".item_name").empty().append(
219 $('<input/>')
218 $('<input/>')
220 .addClass("nbname_input")
219 .addClass("nbname_input")
221 .attr('value', nbname)
220 .attr('value', nbname)
222 .attr('size', '30')
221 .attr('size', '30')
223 .attr('type', 'text')
222 .attr('type', 'text')
224 );
223 );
225 };
224 };
226
225
227
226
228 NotebookList.prototype.add_notebook_data = function (data, item) {
227 NotebookList.prototype.add_notebook_data = function (data, item) {
229 item.data('nbdata',data);
228 item.data('nbdata',data);
230 };
229 };
231
230
232
231
233 NotebookList.prototype.add_shutdown_button = function (item, session) {
232 NotebookList.prototype.add_shutdown_button = function (item, session) {
234 var that = this;
233 var that = this;
235 var shutdown_button = $("<button/>").text("Shutdown").addClass("btn btn-mini").
234 var shutdown_button = $("<button/>").text("Shutdown").addClass("btn btn-mini").
236 click(function (e) {
235 click(function (e) {
237 var settings = {
236 var settings = {
238 processData : false,
237 processData : false,
239 cache : false,
238 cache : false,
240 type : "DELETE",
239 type : "DELETE",
241 dataType : "json",
240 dataType : "json",
242 success : function (data, status, xhr) {
241 success : function (data, status, xhr) {
243 that.load_sessions();
242 that.load_sessions();
244 }
243 }
245 };
244 };
246 var url = that.baseProjectUrl() + 'api/sessions/' + session;
245 var url = that.baseProjectUrl() + 'api/sessions/' + session;
247 $.ajax(url, settings);
246 $.ajax(url, settings);
248 return false;
247 return false;
249 });
248 });
250 // var new_buttons = item.find('a'); // shutdown_button;
249 // var new_buttons = item.find('a'); // shutdown_button;
251 item.find(".item_buttons").html("").append(shutdown_button);
250 item.find(".item_buttons").html("").append(shutdown_button);
252 };
251 };
253
252
254 NotebookList.prototype.add_delete_button = function (item) {
253 NotebookList.prototype.add_delete_button = function (item) {
255 var new_buttons = $('<span/>').addClass("btn-group pull-right");
254 var new_buttons = $('<span/>').addClass("btn-group pull-right");
256 var notebooklist = this;
255 var notebooklist = this;
257 var delete_button = $("<button/>").text("Delete").addClass("btn btn-mini").
256 var delete_button = $("<button/>").text("Delete").addClass("btn btn-mini").
258 click(function (e) {
257 click(function (e) {
259 // $(this) is the button that was clicked.
258 // $(this) is the button that was clicked.
260 var that = $(this);
259 var that = $(this);
261 // We use the nbname and notebook_id from the parent notebook_item element's
260 // We use the nbname and notebook_id from the parent notebook_item element's
262 // data because the outer scopes values change as we iterate through the loop.
261 // data because the outer scopes values change as we iterate through the loop.
263 var parent_item = that.parents('div.list_item');
262 var parent_item = that.parents('div.list_item');
264 var nbname = parent_item.data('nbname');
263 var nbname = parent_item.data('nbname');
265 var message = 'Are you sure you want to permanently delete the notebook: ' + nbname + '?';
264 var message = 'Are you sure you want to permanently delete the notebook: ' + nbname + '?';
266 IPython.dialog.modal({
265 IPython.dialog.modal({
267 title : "Delete notebook",
266 title : "Delete notebook",
268 body : message,
267 body : message,
269 buttons : {
268 buttons : {
270 Delete : {
269 Delete : {
271 class: "btn-danger",
270 class: "btn-danger",
272 click: function() {
271 click: function() {
273 var settings = {
272 var settings = {
274 processData : false,
273 processData : false,
275 cache : false,
274 cache : false,
276 type : "DELETE",
275 type : "DELETE",
277 dataType : "json",
276 dataType : "json",
278 success : function (data, status, xhr) {
277 success : function (data, status, xhr) {
279 parent_item.remove();
278 parent_item.remove();
280 }
279 }
281 };
280 };
282 if (notebooklist.notebookPath() == "") {
281 if (notebooklist.notebookPath() == "") {
283 var url = notebooklist.baseProjectUrl() + 'api/notebooks/' + nbname +'.ipynb';
282 var url = notebooklist.baseProjectUrl() + 'api/notebooks/' + nbname +'.ipynb';
284 }
283 }
285 else {
284 else {
286 var url = notebooklist.baseProjectUrl() + 'api/notebooks/' + notebooklist.notebookPath() + nbname + '.ipynb';
285 var url = notebooklist.baseProjectUrl() + 'api/notebooks/' + notebooklist.notebookPath() + nbname + '.ipynb';
287 }
286 }
288 $.ajax(url, settings);
287 $.ajax(url, settings);
289 }
288 }
290 },
289 },
291 Cancel : {}
290 Cancel : {}
292 }
291 }
293 });
292 });
294 return false;
293 return false;
295 });
294 });
296 item.find(".item_buttons").html("").append(delete_button);
295 item.find(".item_buttons").html("").append(delete_button);
297 };
296 };
298
297
299
298
300 NotebookList.prototype.add_upload_button = function (item) {
299 NotebookList.prototype.add_upload_button = function (item) {
301 var that = this;
300 var that = this;
302 var upload_button = $('<button/>').text("Upload")
301 var upload_button = $('<button/>').text("Upload")
303 .addClass('btn btn-primary btn-mini upload_button')
302 .addClass('btn btn-primary btn-mini upload_button')
304 .click(function (e) {
303 .click(function (e) {
305 var nbname = item.find('.item_name > input').attr('value');
304 var nbname = item.find('.item_name > input').attr('value');
306 var nbformat = item.data('nbformat');
305 var nbformat = item.data('nbformat');
307 var nbdata = item.data('nbdata');
306 var nbdata = item.data('nbdata');
308 var content_type = 'text/plain';
307 var content_type = 'text/plain';
309 if (nbformat === 'json') {
308 if (nbformat === 'json') {
310 content_type = 'application/json';
309 content_type = 'application/json';
311 } else if (nbformat === 'py') {
310 } else if (nbformat === 'py') {
312 content_type = 'application/x-python';
311 content_type = 'application/x-python';
313 };
312 };
314 var settings = {
313 var settings = {
315 processData : false,
314 processData : false,
316 cache : false,
315 cache : false,
317 type : 'POST',
316 type : 'POST',
318 dataType : 'json',
317 dataType : 'json',
319 data : nbdata,
318 data : nbdata,
320 headers : {'Content-Type': content_type},
319 headers : {'Content-Type': content_type},
321 success : function (data, status, xhr) {
320 success : function (data, status, xhr) {
322 that.add_link(data, nbname, item);
321 that.add_link(data, nbname, item);
323 that.add_delete_button(item);
322 that.add_delete_button(item);
324 }
323 }
325 };
324 };
326
325
327 var qs = $.param({name:nbname, format:nbformat});
326 var qs = $.param({name:nbname, format:nbformat});
328 var url = that.baseProjectUrl() + 'notebooks?' + qs;
327 var url = that.baseProjectUrl() + 'notebooks?' + qs;
329 $.ajax(url, settings);
328 $.ajax(url, settings);
330 return false;
329 return false;
331 });
330 });
332 var cancel_button = $('<button/>').text("Cancel")
331 var cancel_button = $('<button/>').text("Cancel")
333 .addClass("btn btn-mini")
332 .addClass("btn btn-mini")
334 .click(function (e) {
333 .click(function (e) {
335 console.log('cancel click');
334 console.log('cancel click');
336 item.remove();
335 item.remove();
337 return false;
336 return false;
338 });
337 });
339 item.find(".item_buttons").empty()
338 item.find(".item_buttons").empty()
340 .append(upload_button)
339 .append(upload_button)
341 .append(cancel_button);
340 .append(cancel_button);
342 };
341 };
343
342
344
343
345 IPython.NotebookList = NotebookList;
344 IPython.NotebookList = NotebookList;
346
345
347 return IPython;
346 return IPython;
348
347
349 }(IPython));
348 }(IPython));
350
349
General Comments 0
You need to be logged in to leave comments. Login now