Show More
@@ -1,310 +1,311 | |||
|
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 | }; |
|
22 | 22 | |
|
23 | 23 | NotebookList.prototype.style = function () { |
|
24 | 24 | $('#notebook_toolbar').addClass('list_toolbar'); |
|
25 | 25 | $('#drag_info').addClass('toolbar_info'); |
|
26 | 26 | $('#notebook_buttons').addClass('toolbar_buttons'); |
|
27 | 27 | $('div#project_name').addClass('list_header ui-widget ui-widget-header'); |
|
28 | 28 | $('#refresh_notebook_list').button({ |
|
29 | 29 | icons : {primary: 'ui-icon-arrowrefresh-1-s'}, |
|
30 | 30 | text : false |
|
31 | 31 | }); |
|
32 | 32 | }; |
|
33 | 33 | |
|
34 | 34 | |
|
35 | 35 | NotebookList.prototype.bind_events = function () { |
|
36 | 36 | if (IPython.read_only){ |
|
37 | 37 | return; |
|
38 | 38 | } |
|
39 | 39 | var that = this; |
|
40 | 40 | $('#refresh_notebook_list').click(function () { |
|
41 | 41 | that.load_list(); |
|
42 | 42 | }); |
|
43 | 43 | this.element.bind('dragover', function () { |
|
44 | 44 | return false; |
|
45 | 45 | }); |
|
46 | 46 | this.element.bind('drop', function(event){ |
|
47 | 47 | console.log('bound to drop'); |
|
48 | 48 | that.handelFilesUpload(event,'drop'); |
|
49 | 49 | return false; |
|
50 | 50 | }); |
|
51 | 51 | }; |
|
52 | 52 | |
|
53 | 53 | NotebookList.prototype.handelFilesUpload = function(event, dropOrForm) { |
|
54 | 54 | var that = this; |
|
55 | 55 | var files; |
|
56 | 56 | if(dropOrForm =='drop'){ |
|
57 | 57 | files = event.originalEvent.dataTransfer.files; |
|
58 | 58 | } else |
|
59 | 59 | { |
|
60 | 60 | files = event.originalEvent.target.files |
|
61 | 61 | } |
|
62 | 62 | for (var i = 0, f; f = files[i]; i++) { |
|
63 | 63 | var reader = new FileReader(); |
|
64 | 64 | reader.readAsText(f); |
|
65 | 65 | var fname = f.name.split('.'); |
|
66 | 66 | var nbname = fname.slice(0,-1).join('.'); |
|
67 | 67 | var nbformat = fname.slice(-1)[0]; |
|
68 | 68 | if (nbformat === 'ipynb') {nbformat = 'json';}; |
|
69 | 69 | if (nbformat === 'py' || nbformat === 'json') { |
|
70 | 70 | var item = that.new_notebook_item(0); |
|
71 | 71 | that.add_name_input(nbname, item); |
|
72 | 72 | item.data('nbformat', nbformat); |
|
73 | 73 | // Store the notebook item in the reader so we can use it later |
|
74 | 74 | // to know which item it belongs to. |
|
75 | 75 | $(reader).data('item', item); |
|
76 | 76 | reader.onload = function (event) { |
|
77 | 77 | var nbitem = $(event.target).data('item'); |
|
78 | 78 | that.add_notebook_data(event.target.result, nbitem); |
|
79 | 79 | that.add_upload_button(nbitem); |
|
80 | 80 | }; |
|
81 | 81 | }; |
|
82 | 82 | } |
|
83 | 83 | return false; |
|
84 | 84 | }; |
|
85 | 85 | |
|
86 | 86 | NotebookList.prototype.clear_list = function () { |
|
87 | 87 | this.element.children('.list_item').remove(); |
|
88 | 88 | } |
|
89 | 89 | |
|
90 | 90 | |
|
91 | 91 | NotebookList.prototype.load_list = function () { |
|
92 | 92 | var settings = { |
|
93 | 93 | processData : false, |
|
94 | 94 | cache : false, |
|
95 | 95 | type : "GET", |
|
96 | 96 | dataType : "json", |
|
97 | 97 | success : $.proxy(this.list_loaded, this) |
|
98 | 98 | }; |
|
99 | 99 | var url = $('body').data('baseProjectUrl') + 'notebooks'; |
|
100 | 100 | $.ajax(url, settings); |
|
101 | 101 | }; |
|
102 | 102 | |
|
103 | 103 | |
|
104 | 104 | NotebookList.prototype.list_loaded = function (data, status, xhr) { |
|
105 | 105 | var len = data.length; |
|
106 | 106 | this.clear_list(); |
|
107 | 107 | // Todo: remove old children |
|
108 | 108 | for (var i=0; i<len; i++) { |
|
109 | 109 | var notebook_id = data[i].notebook_id; |
|
110 | 110 | var nbname = data[i].name; |
|
111 | 111 | var kernel = data[i].kernel_id; |
|
112 | 112 | var item = this.new_notebook_item(i); |
|
113 | 113 | this.add_link(notebook_id, nbname, item); |
|
114 | 114 | if (!IPython.read_only){ |
|
115 | 115 | // hide delete buttons when readonly |
|
116 | 116 | if(kernel == null){ |
|
117 | 117 | this.add_delete_button(item); |
|
118 | 118 | } else { |
|
119 | 119 | this.add_shutdown_button(item,kernel); |
|
120 | 120 | } |
|
121 | 121 | } |
|
122 | 122 | }; |
|
123 | 123 | }; |
|
124 | 124 | |
|
125 | 125 | |
|
126 | 126 | NotebookList.prototype.new_notebook_item = function (index) { |
|
127 | 127 | var item = $('<div/>'); |
|
128 | 128 | item.addClass('list_item ui-widget ui-widget-content ui-helper-clearfix'); |
|
129 | 129 | item.css('border-top-style','none'); |
|
130 | 130 | var item_name = $('<span/>').addClass('item_name'); |
|
131 | 131 | |
|
132 | 132 | item.append(item_name); |
|
133 | 133 | if (index === -1) { |
|
134 | 134 | this.element.append(item); |
|
135 | 135 | } else { |
|
136 | 136 | this.element.children().eq(index).after(item); |
|
137 | 137 | } |
|
138 | 138 | return item; |
|
139 | 139 | }; |
|
140 | 140 | |
|
141 | 141 | |
|
142 | 142 | NotebookList.prototype.add_link = function (notebook_id, nbname, item) { |
|
143 | 143 | item.data('nbname', nbname); |
|
144 | 144 | item.data('notebook_id', notebook_id); |
|
145 | 145 | var new_item_name = $('<span/>').addClass('item_name'); |
|
146 | 146 | new_item_name.append( |
|
147 | 147 | $('<a/>'). |
|
148 | 148 | attr('href', $('body').data('baseProjectUrl')+notebook_id). |
|
149 | 149 | attr('target','_blank'). |
|
150 | 150 | text(nbname) |
|
151 | 151 | ); |
|
152 | 152 | var e = item.find('.item_name'); |
|
153 | 153 | if (e.length === 0) { |
|
154 | 154 | item.append(new_item_name); |
|
155 | 155 | } else { |
|
156 | 156 | e.replaceWith(new_item_name); |
|
157 | 157 | }; |
|
158 | 158 | }; |
|
159 | 159 | |
|
160 | 160 | |
|
161 | 161 | NotebookList.prototype.add_name_input = function (nbname, item) { |
|
162 | 162 | item.data('nbname', nbname); |
|
163 | 163 | var new_item_name = $('<span/>').addClass('item_name'); |
|
164 | 164 | new_item_name.append( |
|
165 | 165 | $('<input/>').addClass('ui-widget ui-widget-content'). |
|
166 | 166 | attr('value', nbname). |
|
167 | 167 | attr('size', '30'). |
|
168 | 168 | attr('type', 'text') |
|
169 | 169 | ); |
|
170 | 170 | var e = item.find('.item_name'); |
|
171 | 171 | if (e.length === 0) { |
|
172 | 172 | item.append(new_item_name); |
|
173 | 173 | } else { |
|
174 | 174 | e.replaceWith(new_item_name); |
|
175 | 175 | }; |
|
176 | 176 | }; |
|
177 | 177 | |
|
178 | 178 | |
|
179 | 179 | NotebookList.prototype.add_notebook_data = function (data, item) { |
|
180 | 180 | item.data('nbdata',data); |
|
181 | 181 | }; |
|
182 | 182 | |
|
183 | 183 | |
|
184 | 184 | NotebookList.prototype.add_shutdown_button = function (item,kernel) { |
|
185 | 185 | var new_buttons = $('<span/>').addClass('item_buttons'); |
|
186 | 186 | var that = this; |
|
187 | 187 | var shutdown_button = $('<button>Shutdown</button>').button(). |
|
188 | 188 | click(function (e) { |
|
189 | 189 | var settings = { |
|
190 | 190 | processData : false, |
|
191 | 191 | cache : false, |
|
192 | 192 | type : "DELETE", |
|
193 | 193 | dataType : "json", |
|
194 | 194 | success : function (data, status, xhr) { |
|
195 | 195 | that.load_list(); |
|
196 | 196 | } |
|
197 | 197 | }; |
|
198 | 198 | var url = $('body').data('baseProjectUrl') + 'kernels/'+kernel; |
|
199 | 199 | $.ajax(url, settings); |
|
200 | 200 | }); |
|
201 | 201 | new_buttons.append(shutdown_button); |
|
202 | 202 | var e = item.find('.item_buttons'); |
|
203 | 203 | if (e.length === 0) { |
|
204 | 204 | item.append(new_buttons); |
|
205 | 205 | } else { |
|
206 | 206 | e.replaceWith(new_buttons); |
|
207 | 207 | }; |
|
208 | 208 | }; |
|
209 | 209 | |
|
210 | 210 | NotebookList.prototype.add_delete_button = function (item) { |
|
211 | 211 | var new_buttons = $('<span/>').addClass('item_buttons'); |
|
212 | 212 | var delete_button = $('<button>Delete</button>').button(). |
|
213 | 213 | click(function (e) { |
|
214 | 214 | // $(this) is the button that was clicked. |
|
215 | 215 | var that = $(this); |
|
216 | 216 | // We use the nbname and notebook_id from the parent notebook_item element's |
|
217 | 217 | // data because the outer scopes values change as we iterate through the loop. |
|
218 | 218 | var parent_item = that.parents('div.list_item'); |
|
219 | 219 | var nbname = parent_item.data('nbname'); |
|
220 | 220 | var notebook_id = parent_item.data('notebook_id'); |
|
221 | 221 | var dialog = $('<div/>'); |
|
222 | 222 | dialog.html('Are you sure you want to permanently delete the notebook: ' + nbname + '?'); |
|
223 | 223 | parent_item.append(dialog); |
|
224 | 224 | dialog.dialog({ |
|
225 | 225 | resizable: false, |
|
226 | 226 | modal: true, |
|
227 | 227 | title: "Delete notebook", |
|
228 | 228 | buttons : { |
|
229 | 229 | "Delete": function () { |
|
230 | 230 | var settings = { |
|
231 | 231 | processData : false, |
|
232 | 232 | cache : false, |
|
233 | 233 | type : "DELETE", |
|
234 | 234 | dataType : "json", |
|
235 | 235 | success : function (data, status, xhr) { |
|
236 | 236 | parent_item.remove(); |
|
237 | 237 | } |
|
238 | 238 | }; |
|
239 | 239 | var url = $('body').data('baseProjectUrl') + 'notebooks/' + notebook_id; |
|
240 | 240 | $.ajax(url, settings); |
|
241 | 241 | $(this).dialog('close'); |
|
242 | 242 | }, |
|
243 | 243 | "Cancel": function () { |
|
244 | 244 | $(this).dialog('close'); |
|
245 | 245 | } |
|
246 | 246 | } |
|
247 | 247 | }); |
|
248 | 248 | }); |
|
249 | 249 | new_buttons.append(delete_button); |
|
250 | 250 | var e = item.find('.item_buttons'); |
|
251 | 251 | if (e.length === 0) { |
|
252 | 252 | item.append(new_buttons); |
|
253 | 253 | } else { |
|
254 | 254 | e.replaceWith(new_buttons); |
|
255 | 255 | }; |
|
256 | 256 | }; |
|
257 | 257 | |
|
258 | 258 | |
|
259 | 259 | NotebookList.prototype.add_upload_button = function (item) { |
|
260 | 260 | var that = this; |
|
261 | 261 | var new_buttons = $('<span/>').addClass('item_buttons'); |
|
262 | 262 | var upload_button = $('<button>Upload</button>').button(). |
|
263 | addClass('upload-button'). | |
|
263 | 264 | click(function (e) { |
|
264 | 265 | var nbname = item.find('.item_name > input').attr('value'); |
|
265 | 266 | var nbformat = item.data('nbformat'); |
|
266 | 267 | var nbdata = item.data('nbdata'); |
|
267 | 268 | var content_type = 'text/plain'; |
|
268 | 269 | if (nbformat === 'json') { |
|
269 | 270 | content_type = 'application/json'; |
|
270 | 271 | } else if (nbformat === 'py') { |
|
271 | 272 | content_type = 'application/x-python'; |
|
272 | 273 | }; |
|
273 | 274 | var settings = { |
|
274 | 275 | processData : false, |
|
275 | 276 | cache : false, |
|
276 | 277 | type : 'POST', |
|
277 | 278 | dataType : 'json', |
|
278 | 279 | data : nbdata, |
|
279 | 280 | headers : {'Content-Type': content_type}, |
|
280 | 281 | success : function (data, status, xhr) { |
|
281 | 282 | that.add_link(data, nbname, item); |
|
282 | 283 | that.add_delete_button(item); |
|
283 | 284 | } |
|
284 | 285 | }; |
|
285 | 286 | |
|
286 | 287 | var qs = $.param({name:nbname, format:nbformat}); |
|
287 | 288 | var url = $('body').data('baseProjectUrl') + 'notebooks?' + qs; |
|
288 | 289 | $.ajax(url, settings); |
|
289 | 290 | }); |
|
290 | 291 | var cancel_button = $('<button>Cancel</button>').button(). |
|
291 | 292 | click(function (e) { |
|
292 | 293 | item.remove(); |
|
293 | 294 | }); |
|
294 | 295 | upload_button.addClass('upload_button'); |
|
295 | 296 | new_buttons.append(upload_button).append(cancel_button); |
|
296 | 297 | var e = item.find('.item_buttons'); |
|
297 | 298 | if (e.length === 0) { |
|
298 | 299 | item.append(new_buttons); |
|
299 | 300 | } else { |
|
300 | 301 | e.replaceWith(new_buttons); |
|
301 | 302 | }; |
|
302 | 303 | }; |
|
303 | 304 | |
|
304 | 305 | |
|
305 | 306 | IPython.NotebookList = NotebookList; |
|
306 | 307 | |
|
307 | 308 | return IPython; |
|
308 | 309 | |
|
309 | 310 | }(IPython)); |
|
310 | 311 |
@@ -1,75 +1,84 | |||
|
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 | // On document ready |
|
10 | 10 | //============================================================================ |
|
11 | 11 | |
|
12 | 12 | |
|
13 | 13 | $(document).ready(function () { |
|
14 | 14 | |
|
15 | 15 | IPython.page = new IPython.Page(); |
|
16 | 16 | |
|
17 | 17 | $('div#tabs').tabs(); |
|
18 | 18 | $('div#tabs').on('tabsselect', function (event, ui) { |
|
19 | 19 | var new_url = $('body').data('baseProjectUrl') + '#' + ui.panel.id; |
|
20 | 20 | window.history.replaceState({}, '', new_url); |
|
21 | 21 | }); |
|
22 | 22 | $('div#main_app').addClass('border-box-sizing ui-widget'); |
|
23 | 23 | $('div#notebooks_toolbar').addClass('ui-widget ui-helper-clearfix'); |
|
24 | 24 | $('#new_notebook').button().click(function (e) { |
|
25 | 25 | window.open($('body').data('baseProjectUrl')+'new'); |
|
26 | 26 | }); |
|
27 | 27 | |
|
28 | 28 | IPython.read_only = $('body').data('readOnly') === 'True'; |
|
29 | 29 | IPython.notebook_list = new IPython.NotebookList('div#notebook_list'); |
|
30 | 30 | IPython.cluster_list = new IPython.ClusterList('div#cluster_list'); |
|
31 | 31 | IPython.login_widget = new IPython.LoginWidget('span#login_widget'); |
|
32 | 32 | |
|
33 | 33 | var interval_id=0; |
|
34 | 34 | // auto refresh every xx secondes, no need to be fast, |
|
35 | 35 | // update is done at least when page get focus |
|
36 | 36 | var time_refresh = 60; // in sec |
|
37 | 37 | |
|
38 | 38 | var enable_autorefresh = function(){ |
|
39 | 39 | //refresh immediately , then start interval |
|
40 | if($('upload_button').length == 0) | |
|
41 | { | |
|
40 | 42 | IPython.notebook_list.load_list(); |
|
41 | 43 | IPython.cluster_list.load_list(); |
|
44 | } | |
|
42 | 45 | if (!interval_id){ |
|
43 | 46 | interval_id = setInterval(function(){ |
|
47 | if($('upload_button').length == 0) | |
|
48 | { | |
|
44 | 49 | IPython.notebook_list.load_list(); |
|
45 | 50 | IPython.cluster_list.load_list(); |
|
51 | } | |
|
46 | 52 | }, time_refresh*1000); |
|
47 | 53 | } |
|
48 | 54 | } |
|
49 | 55 | |
|
50 | 56 | var disable_autorefresh = function(){ |
|
51 | 57 | clearInterval(interval_id); |
|
52 | 58 | interval_id = 0; |
|
53 | 59 | } |
|
54 | 60 | |
|
55 | 61 | // stop autorefresh when page lose focus |
|
56 | 62 | $(window).blur(function() { |
|
57 | 63 | disable_autorefresh(); |
|
58 | 64 | }) |
|
59 | 65 | |
|
60 | 66 | //re-enable when page get focus back |
|
61 | 67 | $(window).focus(function() { |
|
62 | 68 | enable_autorefresh(); |
|
63 | 69 | }); |
|
64 | 70 | |
|
65 | 71 | // finally start it, it will refresh immediately |
|
66 | 72 | enable_autorefresh(); |
|
67 | 73 | |
|
74 | IPython.enable_autorefresh = enable_autorefresh; | |
|
75 | IPython.disable_autorefresh = disable_autorefresh; | |
|
76 | ||
|
68 | 77 | IPython.page.show(); |
|
69 | 78 | |
|
70 | 79 | // bound the upload method to the on change of the file select list |
|
71 | 80 | $("#alternate_upload").change(function (event){ |
|
72 | 81 | IPython.notebook_list.handelFilesUpload(event,'form'); |
|
73 | 82 | }); |
|
74 | 83 | }); |
|
75 | 84 |
General Comments 0
You need to be logged in to leave comments.
Login now