##// END OF EJS Templates
Fully removing .py file upload....
Brian E. Granger -
Show More
@@ -1,398 +1,397 b''
1 //----------------------------------------------------------------------------
1 //----------------------------------------------------------------------------
2 // Copyright (C) 2011 The IPython Development Team
2 // Copyright (C) 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 "use strict";
13 "use strict";
14
14
15 var utils = IPython.utils;
15 var utils = IPython.utils;
16
16
17 var NotebookList = function (selector) {
17 var NotebookList = function (selector) {
18 this.selector = selector;
18 this.selector = selector;
19 if (this.selector !== undefined) {
19 if (this.selector !== undefined) {
20 this.element = $(selector);
20 this.element = $(selector);
21 this.style();
21 this.style();
22 this.bind_events();
22 this.bind_events();
23 }
23 }
24 this.notebooks_list = [];
24 this.notebooks_list = [];
25 this.sessions = {};
25 this.sessions = {};
26 };
26 };
27
27
28 NotebookList.prototype.baseProjectUrl = function () {
28 NotebookList.prototype.baseProjectUrl = function () {
29 return $('body').data('baseProjectUrl');
29 return $('body').data('baseProjectUrl');
30 };
30 };
31
31
32 NotebookList.prototype.notebookPath = function() {
32 NotebookList.prototype.notebookPath = function() {
33 return $('body').data('notebookPath');
33 return $('body').data('notebookPath');
34 };
34 };
35
35
36 NotebookList.prototype.style = function () {
36 NotebookList.prototype.style = function () {
37 $('#notebook_toolbar').addClass('list_toolbar');
37 $('#notebook_toolbar').addClass('list_toolbar');
38 $('#drag_info').addClass('toolbar_info');
38 $('#drag_info').addClass('toolbar_info');
39 $('#notebook_buttons').addClass('toolbar_buttons');
39 $('#notebook_buttons').addClass('toolbar_buttons');
40 $('#notebook_list_header').addClass('list_header');
40 $('#notebook_list_header').addClass('list_header');
41 this.element.addClass("list_container");
41 this.element.addClass("list_container");
42 };
42 };
43
43
44
44
45 NotebookList.prototype.bind_events = function () {
45 NotebookList.prototype.bind_events = function () {
46 var that = this;
46 var that = this;
47 $('#refresh_notebook_list').click(function () {
47 $('#refresh_notebook_list').click(function () {
48 that.load_list();
48 that.load_list();
49 });
49 });
50 this.element.bind('dragover', function () {
50 this.element.bind('dragover', function () {
51 return false;
51 return false;
52 });
52 });
53 this.element.bind('drop', function(event){
53 this.element.bind('drop', function(event){
54 that.handelFilesUpload(event,'drop');
54 that.handelFilesUpload(event,'drop');
55 return false;
55 return false;
56 });
56 });
57 };
57 };
58
58
59 NotebookList.prototype.handelFilesUpload = function(event, dropOrForm) {
59 NotebookList.prototype.handelFilesUpload = function(event, dropOrForm) {
60 var that = this;
60 var that = this;
61 var files;
61 var files;
62 if(dropOrForm =='drop'){
62 if(dropOrForm =='drop'){
63 files = event.originalEvent.dataTransfer.files;
63 files = event.originalEvent.dataTransfer.files;
64 } else
64 } else
65 {
65 {
66 files = event.originalEvent.target.files;
66 files = event.originalEvent.target.files;
67 }
67 }
68 for (var i = 0; i < files.length; i++) {
68 for (var i = 0; i < files.length; i++) {
69 var f = files[i];
69 var f = files[i];
70 var reader = new FileReader();
70 var reader = new FileReader();
71 reader.readAsText(f);
71 reader.readAsText(f);
72 var fname = f.name.split('.');
72 var fname = f.name.split('.');
73 var nbname = fname.slice(0,-1).join('.');
73 var nbname = fname.slice(0,-1).join('.');
74 var nbformat = fname.slice(-1)[0];
74 var file_ext = fname.slice(-1)[0];
75 if (nbformat === 'ipynb') {nbformat = 'json';}
75 if (file_ext === 'ipynb') {
76 if (nbformat === 'py' || nbformat === 'json') {
77 var item = that.new_notebook_item(0);
76 var item = that.new_notebook_item(0);
78 that.add_name_input(nbname, item);
77 that.add_name_input(nbname, item);
79 item.data('nbformat', nbformat);
80 // Store the notebook item in the reader so we can use it later
78 // Store the notebook item in the reader so we can use it later
81 // to know which item it belongs to.
79 // to know which item it belongs to.
82 $(reader).data('item', item);
80 $(reader).data('item', item);
83 reader.onload = function (event) {
81 reader.onload = function (event) {
84 var nbitem = $(event.target).data('item');
82 var nbitem = $(event.target).data('item');
85 that.add_notebook_data(event.target.result, nbitem);
83 that.add_notebook_data(event.target.result, nbitem);
86 that.add_upload_button(nbitem);
84 that.add_upload_button(nbitem);
87 };
85 };
86 } else {
87 var dialog = 'Uploaded notebooks must be .ipynb files';
88 IPython.dialog.modal({
89 title : 'Invalid file type',
90 body : dialog,
91 buttons : {'OK' : {'class' : 'btn-primary'}}
92 });
88 }
93 }
89 }
94 }
90 return false;
95 return false;
91 };
96 };
92
97
93 NotebookList.prototype.clear_list = function () {
98 NotebookList.prototype.clear_list = function () {
94 this.element.children('.list_item').remove();
99 this.element.children('.list_item').remove();
95 };
100 };
96
101
97 NotebookList.prototype.load_sessions = function(){
102 NotebookList.prototype.load_sessions = function(){
98 var that = this;
103 var that = this;
99 var settings = {
104 var settings = {
100 processData : false,
105 processData : false,
101 cache : false,
106 cache : false,
102 type : "GET",
107 type : "GET",
103 dataType : "json",
108 dataType : "json",
104 success : $.proxy(that.sessions_loaded, this)
109 success : $.proxy(that.sessions_loaded, this)
105 };
110 };
106 var url = this.baseProjectUrl() + 'api/sessions';
111 var url = this.baseProjectUrl() + 'api/sessions';
107 $.ajax(url,settings);
112 $.ajax(url,settings);
108 };
113 };
109
114
110
115
111 NotebookList.prototype.sessions_loaded = function(data){
116 NotebookList.prototype.sessions_loaded = function(data){
112 this.sessions = {};
117 this.sessions = {};
113 var len = data.length;
118 var len = data.length;
114 if (len > 0) {
119 if (len > 0) {
115 for (var i=0; i<len; i++) {
120 for (var i=0; i<len; i++) {
116 var nb_path;
121 var nb_path;
117 if (!data[i].notebook.path) {
122 if (!data[i].notebook.path) {
118 nb_path = data[i].notebook.name;
123 nb_path = data[i].notebook.name;
119 }
124 }
120 else {
125 else {
121 nb_path = utils.url_path_join(
126 nb_path = utils.url_path_join(
122 data[i].notebook.path,
127 data[i].notebook.path,
123 data[i].notebook.name
128 data[i].notebook.name
124 );
129 );
125 }
130 }
126 this.sessions[nb_path] = data[i].id;
131 this.sessions[nb_path] = data[i].id;
127 }
132 }
128 }
133 }
129 this.load_list();
134 this.load_list();
130 };
135 };
131
136
132 NotebookList.prototype.load_list = function () {
137 NotebookList.prototype.load_list = function () {
133 var that = this;
138 var that = this;
134 var settings = {
139 var settings = {
135 processData : false,
140 processData : false,
136 cache : false,
141 cache : false,
137 type : "GET",
142 type : "GET",
138 dataType : "json",
143 dataType : "json",
139 success : $.proxy(this.list_loaded, this),
144 success : $.proxy(this.list_loaded, this),
140 error : $.proxy( function(){
145 error : $.proxy( function(){
141 that.list_loaded([], null, null, {msg:"Error connecting to server."});
146 that.list_loaded([], null, null, {msg:"Error connecting to server."});
142 },this)
147 },this)
143 };
148 };
144
149
145 var url = utils.url_path_join(
150 var url = utils.url_path_join(
146 this.baseProjectUrl(),
151 this.baseProjectUrl(),
147 'api',
152 'api',
148 'notebooks',
153 'notebooks',
149 this.notebookPath()
154 this.notebookPath()
150 );
155 );
151 $.ajax(url, settings);
156 $.ajax(url, settings);
152 };
157 };
153
158
154
159
155 NotebookList.prototype.list_loaded = function (data, status, xhr, param) {
160 NotebookList.prototype.list_loaded = function (data, status, xhr, param) {
156 var message = 'Notebook list empty.';
161 var message = 'Notebook list empty.';
157 if (param !== undefined && param.msg) {
162 if (param !== undefined && param.msg) {
158 message = param.msg;
163 message = param.msg;
159 }
164 }
160 var len = data.length;
165 var len = data.length;
161 this.clear_list();
166 this.clear_list();
162 if (len === 0) {
167 if (len === 0) {
163 $(this.new_notebook_item(0))
168 $(this.new_notebook_item(0))
164 .append(
169 .append(
165 $('<div style="margin:auto;text-align:center;color:grey"/>')
170 $('<div style="margin:auto;text-align:center;color:grey"/>')
166 .text(message)
171 .text(message)
167 );
172 );
168 }
173 }
169 for (var i=0; i<len; i++) {
174 for (var i=0; i<len; i++) {
170 var name = data[i].name;
175 var name = data[i].name;
171 var path = this.notebookPath();
176 var path = this.notebookPath();
172 var nbname = name.split(".")[0];
177 var nbname = name.split(".")[0];
173 var item = this.new_notebook_item(i);
178 var item = this.new_notebook_item(i);
174 this.add_link(path, nbname, item);
179 this.add_link(path, nbname, item);
175 name = utils.url_path_join(this.notebookPath(), name);
180 name = utils.url_path_join(this.notebookPath(), name);
176 if(this.sessions[name] === undefined){
181 if(this.sessions[name] === undefined){
177 this.add_delete_button(item);
182 this.add_delete_button(item);
178 } else {
183 } else {
179 this.add_shutdown_button(item,this.sessions[name]);
184 this.add_shutdown_button(item,this.sessions[name]);
180 }
185 }
181 }
186 }
182 };
187 };
183
188
184
189
185 NotebookList.prototype.new_notebook_item = function (index) {
190 NotebookList.prototype.new_notebook_item = function (index) {
186 var item = $('<div/>').addClass("list_item").addClass("row-fluid");
191 var item = $('<div/>').addClass("list_item").addClass("row-fluid");
187 // item.addClass('list_item ui-widget ui-widget-content ui-helper-clearfix');
192 // item.addClass('list_item ui-widget ui-widget-content ui-helper-clearfix');
188 // item.css('border-top-style','none');
193 // item.css('border-top-style','none');
189 item.append($("<div/>").addClass("span12").append(
194 item.append($("<div/>").addClass("span12").append(
190 $("<a/>").addClass("item_link").append(
195 $("<a/>").addClass("item_link").append(
191 $("<span/>").addClass("item_name")
196 $("<span/>").addClass("item_name")
192 )
197 )
193 ).append(
198 ).append(
194 $('<div/>').addClass("item_buttons btn-group pull-right")
199 $('<div/>').addClass("item_buttons btn-group pull-right")
195 ));
200 ));
196
201
197 if (index === -1) {
202 if (index === -1) {
198 this.element.append(item);
203 this.element.append(item);
199 } else {
204 } else {
200 this.element.children().eq(index).after(item);
205 this.element.children().eq(index).after(item);
201 }
206 }
202 return item;
207 return item;
203 };
208 };
204
209
205
210
206 NotebookList.prototype.add_link = function (path, nbname, item) {
211 NotebookList.prototype.add_link = function (path, nbname, item) {
207 item.data('nbname', nbname);
212 item.data('nbname', nbname);
208 item.data('path', path);
213 item.data('path', path);
209 item.find(".item_name").text(nbname);
214 item.find(".item_name").text(nbname);
210 item.find("a.item_link")
215 item.find("a.item_link")
211 .attr('href',
216 .attr('href',
212 utils.url_path_join(
217 utils.url_path_join(
213 this.baseProjectUrl(),
218 this.baseProjectUrl(),
214 "notebooks",
219 "notebooks",
215 this.notebookPath(),
220 this.notebookPath(),
216 nbname + ".ipynb"
221 nbname + ".ipynb"
217 )
222 )
218 ).attr('target','_blank');
223 ).attr('target','_blank');
219 };
224 };
220
225
221
226
222 NotebookList.prototype.add_name_input = function (nbname, item) {
227 NotebookList.prototype.add_name_input = function (nbname, item) {
223 item.data('nbname', nbname);
228 item.data('nbname', nbname);
224 item.find(".item_name").empty().append(
229 item.find(".item_name").empty().append(
225 $('<input/>')
230 $('<input/>')
226 .addClass("nbname_input")
231 .addClass("nbname_input")
227 .attr('value', nbname)
232 .attr('value', nbname)
228 .attr('size', '30')
233 .attr('size', '30')
229 .attr('type', 'text')
234 .attr('type', 'text')
230 );
235 );
231 };
236 };
232
237
233
238
234 NotebookList.prototype.add_notebook_data = function (data, item) {
239 NotebookList.prototype.add_notebook_data = function (data, item) {
235 item.data('nbdata', data);
240 item.data('nbdata', data);
236 };
241 };
237
242
238
243
239 NotebookList.prototype.add_shutdown_button = function (item, session) {
244 NotebookList.prototype.add_shutdown_button = function (item, session) {
240 var that = this;
245 var that = this;
241 var shutdown_button = $("<button/>").text("Shutdown").addClass("btn btn-mini").
246 var shutdown_button = $("<button/>").text("Shutdown").addClass("btn btn-mini").
242 click(function (e) {
247 click(function (e) {
243 var settings = {
248 var settings = {
244 processData : false,
249 processData : false,
245 cache : false,
250 cache : false,
246 type : "DELETE",
251 type : "DELETE",
247 dataType : "json",
252 dataType : "json",
248 success : function () {
253 success : function () {
249 that.load_sessions();
254 that.load_sessions();
250 }
255 }
251 };
256 };
252 var url = utils.url_path_join(
257 var url = utils.url_path_join(
253 that.baseProjectUrl(),
258 that.baseProjectUrl(),
254 'api/sessions',
259 'api/sessions',
255 session
260 session
256 );
261 );
257 $.ajax(url, settings);
262 $.ajax(url, settings);
258 return false;
263 return false;
259 });
264 });
260 // var new_buttons = item.find('a'); // shutdown_button;
265 // var new_buttons = item.find('a'); // shutdown_button;
261 item.find(".item_buttons").html("").append(shutdown_button);
266 item.find(".item_buttons").html("").append(shutdown_button);
262 };
267 };
263
268
264 NotebookList.prototype.add_delete_button = function (item) {
269 NotebookList.prototype.add_delete_button = function (item) {
265 var new_buttons = $('<span/>').addClass("btn-group pull-right");
270 var new_buttons = $('<span/>').addClass("btn-group pull-right");
266 var notebooklist = this;
271 var notebooklist = this;
267 var delete_button = $("<button/>").text("Delete").addClass("btn btn-mini").
272 var delete_button = $("<button/>").text("Delete").addClass("btn btn-mini").
268 click(function (e) {
273 click(function (e) {
269 // $(this) is the button that was clicked.
274 // $(this) is the button that was clicked.
270 var that = $(this);
275 var that = $(this);
271 // We use the nbname and notebook_id from the parent notebook_item element's
276 // We use the nbname and notebook_id from the parent notebook_item element's
272 // data because the outer scopes values change as we iterate through the loop.
277 // data because the outer scopes values change as we iterate through the loop.
273 var parent_item = that.parents('div.list_item');
278 var parent_item = that.parents('div.list_item');
274 var nbname = parent_item.data('nbname');
279 var nbname = parent_item.data('nbname');
275 var message = 'Are you sure you want to permanently delete the notebook: ' + nbname + '?';
280 var message = 'Are you sure you want to permanently delete the notebook: ' + nbname + '?';
276 IPython.dialog.modal({
281 IPython.dialog.modal({
277 title : "Delete notebook",
282 title : "Delete notebook",
278 body : message,
283 body : message,
279 buttons : {
284 buttons : {
280 Delete : {
285 Delete : {
281 class: "btn-danger",
286 class: "btn-danger",
282 click: function() {
287 click: function() {
283 var settings = {
288 var settings = {
284 processData : false,
289 processData : false,
285 cache : false,
290 cache : false,
286 type : "DELETE",
291 type : "DELETE",
287 dataType : "json",
292 dataType : "json",
288 success : function (data, status, xhr) {
293 success : function (data, status, xhr) {
289 parent_item.remove();
294 parent_item.remove();
290 }
295 }
291 };
296 };
292 var url = utils.url_path_join(
297 var url = utils.url_path_join(
293 notebooklist.baseProjectUrl(),
298 notebooklist.baseProjectUrl(),
294 'api/notebooks',
299 'api/notebooks',
295 notebooklist.notebookPath(),
300 notebooklist.notebookPath(),
296 nbname + '.ipynb'
301 nbname + '.ipynb'
297 );
302 );
298 $.ajax(url, settings);
303 $.ajax(url, settings);
299 }
304 }
300 },
305 },
301 Cancel : {}
306 Cancel : {}
302 }
307 }
303 });
308 });
304 return false;
309 return false;
305 });
310 });
306 item.find(".item_buttons").html("").append(delete_button);
311 item.find(".item_buttons").html("").append(delete_button);
307 };
312 };
308
313
309
314
310 NotebookList.prototype.add_upload_button = function (item) {
315 NotebookList.prototype.add_upload_button = function (item) {
311 var that = this;
316 var that = this;
312 var upload_button = $('<button/>').text("Upload")
317 var upload_button = $('<button/>').text("Upload")
313 .addClass('btn btn-primary btn-mini upload_button')
318 .addClass('btn btn-primary btn-mini upload_button')
314 .click(function (e) {
319 .click(function (e) {
315 var nbname = item.find('.item_name > input').val();
320 var nbname = item.find('.item_name > input').val();
316 var nbformat = item.data('nbformat');
317 var nbdata = item.data('nbdata');
321 var nbdata = item.data('nbdata');
318 var content_type = 'application/json';
322 var content_type = 'application/json';
319 if (nbformat === 'json') {
320 // pass
321 } else if (nbformat === 'py') {
322 // TODO: re-enable non-ipynb upload
323 }
324 var model = {
323 var model = {
325 content : JSON.parse(nbdata),
324 content : JSON.parse(nbdata),
326 };
325 };
327 var settings = {
326 var settings = {
328 processData : false,
327 processData : false,
329 cache : false,
328 cache : false,
330 type : 'POST',
329 type : 'POST',
331 dataType : 'json',
330 dataType : 'json',
332 data : JSON.stringify(model),
331 data : JSON.stringify(model),
333 headers : {'Content-Type': content_type},
332 headers : {'Content-Type': content_type},
334 success : function (data, status, xhr) {
333 success : function (data, status, xhr) {
335 that.add_link(data, nbname, item);
334 that.add_link(data, nbname, item);
336 that.add_delete_button(item);
335 that.add_delete_button(item);
337 },
336 },
338 error : function (data, status, xhr) {
337 error : function (data, status, xhr) {
339 console.log(data, status);
338 console.log(data, status);
340 }
339 }
341 };
340 };
342
341
343 var url = utils.url_path_join(
342 var url = utils.url_path_join(
344 that.baseProjectUrl(),
343 that.baseProjectUrl(),
345 'api/notebooks',
344 'api/notebooks',
346 that.notebookPath(),
345 that.notebookPath(),
347 nbname + '.ipynb'
346 nbname + '.ipynb'
348 );
347 );
349 $.ajax(url, settings);
348 $.ajax(url, settings);
350 return false;
349 return false;
351 });
350 });
352 var cancel_button = $('<button/>').text("Cancel")
351 var cancel_button = $('<button/>').text("Cancel")
353 .addClass("btn btn-mini")
352 .addClass("btn btn-mini")
354 .click(function (e) {
353 .click(function (e) {
355 console.log('cancel click');
354 console.log('cancel click');
356 item.remove();
355 item.remove();
357 return false;
356 return false;
358 });
357 });
359 item.find(".item_buttons").empty()
358 item.find(".item_buttons").empty()
360 .append(upload_button)
359 .append(upload_button)
361 .append(cancel_button);
360 .append(cancel_button);
362 };
361 };
363
362
364
363
365 NotebookList.prototype.new_notebook = function(){
364 NotebookList.prototype.new_notebook = function(){
366 var path = this.notebookPath();
365 var path = this.notebookPath();
367 var base_project_url = this.baseProjectUrl();
366 var base_project_url = this.baseProjectUrl();
368 var settings = {
367 var settings = {
369 processData : false,
368 processData : false,
370 cache : false,
369 cache : false,
371 type : "POST",
370 type : "POST",
372 dataType : "json",
371 dataType : "json",
373 async : false,
372 async : false,
374 success : function (data, status, xhr) {
373 success : function (data, status, xhr) {
375 var notebook_name = data.name;
374 var notebook_name = data.name;
376 window.open(
375 window.open(
377 utils.url_path_join(
376 utils.url_path_join(
378 base_project_url,
377 base_project_url,
379 'notebooks',
378 'notebooks',
380 path,
379 path,
381 notebook_name),
380 notebook_name),
382 '_blank'
381 '_blank'
383 );
382 );
384 }
383 }
385 };
384 };
386 var url = utils.url_path_join(
385 var url = utils.url_path_join(
387 base_project_url,
386 base_project_url,
388 'api/notebooks',
387 'api/notebooks',
389 path
388 path
390 );
389 );
391 $.ajax(url, settings);
390 $.ajax(url, settings);
392 };
391 };
393
392
394 IPython.NotebookList = NotebookList;
393 IPython.NotebookList = NotebookList;
395
394
396 return IPython;
395 return IPython;
397
396
398 }(IPython));
397 }(IPython));
General Comments 0
You need to be logged in to leave comments. Login now