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