##// END OF EJS Templates
various upload fixes...
MinRK -
Show More
@@ -75,30 +75,27 b' define(['
75 }
75 }
76 for (var i = 0; i < files.length; i++) {
76 for (var i = 0; i < files.length; i++) {
77 var f = files[i];
77 var f = files[i];
78 var reader = new FileReader();
79 reader.readAsText(f);
80 var name_and_ext = utils.splitext(f.name);
78 var name_and_ext = utils.splitext(f.name);
81 var file_ext = name_and_ext[1];
79 var file_ext = name_and_ext[1];
80
81 var reader = new FileReader();
82 if (file_ext === '.ipynb') {
82 if (file_ext === '.ipynb') {
83 var item = that.new_item(0);
83 reader.readAsText(f);
84 item.addClass('new-file');
85 that.add_name_input(f.name, item);
86 // Store the notebook item in the reader so we can use it later
87 // to know which item it belongs to.
88 $(reader).data('item', item);
89 reader.onload = function (event) {
90 var nbitem = $(event.target).data('item');
91 that.add_notebook_data(event.target.result, nbitem);
92 that.add_upload_button(nbitem);
93 };
94 } else {
84 } else {
95 var dialog_body = 'Uploaded notebooks must be .ipynb files';
85 // read non-notebook files as binary
96 dialog.modal({
86 reader.readAsArrayBuffer(f);
97 title : 'Invalid file type',
98 body : dialog_body,
99 buttons : {'OK' : {'class' : 'btn-primary'}}
100 });
101 }
87 }
88 var item = that.new_item(0);
89 item.addClass('new-file');
90 that.add_name_input(f.name, item);
91 // Store the list item in the reader so we can use it later
92 // to know which item it belongs to.
93 $(reader).data('item', item);
94 reader.onload = function (event) {
95 var item = $(event.target).data('item');
96 that.add_file_data(event.target.result, item);
97 that.add_upload_button(item);
98 };
102 }
99 }
103 // Replace the file input form wth a clone of itself. This is required to
100 // Replace the file input form wth a clone of itself. This is required to
104 // reset the form. Otherwise, if you upload a file, delete it and try to
101 // reset the form. Otherwise, if you upload a file, delete it and try to
@@ -268,16 +265,16 b' define(['
268 item.find(".item_icon").addClass('notebook_icon').addClass('icon-fixed-width');
265 item.find(".item_icon").addClass('notebook_icon').addClass('icon-fixed-width');
269 item.find(".item_name").empty().append(
266 item.find(".item_name").empty().append(
270 $('<input/>')
267 $('<input/>')
271 .addClass("nbname_input")
268 .addClass("filename_input")
272 .attr('value', utils.splitext(name)[0])
269 .attr('value', name)
273 .attr('size', '30')
270 .attr('size', '30')
274 .attr('type', 'text')
271 .attr('type', 'text')
275 );
272 );
276 };
273 };
277
274
278
275
279 NotebookList.prototype.add_notebook_data = function (data, item) {
276 NotebookList.prototype.add_file_data = function (data, item) {
280 item.data('nbdata', data);
277 item.data('filedata', data);
281 };
278 };
282
279
283
280
@@ -314,8 +311,8 b' define(['
314 click(function (e) {
311 click(function (e) {
315 // $(this) is the button that was clicked.
312 // $(this) is the button that was clicked.
316 var that = $(this);
313 var that = $(this);
317 // We use the nbname and notebook_id from the parent notebook_item element's
314 // We use the filename from the parent list_item element's
318 // data because the outer scopes values change as we iterate through the loop.
315 // data because the outer scope's values change as we iterate through the loop.
319 var parent_item = that.parents('div.list_item');
316 var parent_item = that.parents('div.list_item');
320 var name = parent_item.data('name');
317 var name = parent_item.data('name');
321 var message = 'Are you sure you want to permanently delete the file: ' + name + '?';
318 var message = 'Are you sure you want to permanently delete the file: ' + name + '?';
@@ -354,32 +351,55 b' define(['
354 };
351 };
355
352
356
353
357 NotebookList.prototype.add_upload_button = function (item) {
354 NotebookList.prototype.add_upload_button = function (item, type) {
358 var that = this;
355 var that = this;
359 var upload_button = $('<button/>').text("Upload")
356 var upload_button = $('<button/>').text("Upload")
360 .addClass('btn btn-primary btn-xs upload_button')
357 .addClass('btn btn-primary btn-xs upload_button')
361 .click(function (e) {
358 .click(function (e) {
362 var nbname = item.find('.item_name > input').val();
363 if (nbname.slice(nbname.length-6, nbname.length) != ".ipynb") {
364 nbname = nbname + ".ipynb";
365 }
366 var path = that.notebook_path;
359 var path = that.notebook_path;
367 var nbdata = item.data('nbdata');
360 var filename = item.find('.item_name > input').val();
368 var content_type = 'application/json';
361 var filedata = item.data('filedata');
362 var format = 'text';
363 if (filedata instanceof ArrayBuffer) {
364 // base64-encode binary file data
365 var bytes = '';
366 var buf = new Uint8Array(filedata);
367 var nbytes = buf.byteLength;
368 for (var i=0; i<nbytes; i++) {
369 bytes += String.fromCharCode(buf[i]);
370 }
371 filedata = btoa(bytes);
372 format = 'base64';
373 }
369 var model = {
374 var model = {
370 path: path,
375 path: path,
371 name: nbname,
376 name: filename
372 content : JSON.parse(nbdata),
373 type : 'notebook'
374 };
377 };
378
379 var name_and_ext = utils.splitext(filename);
380 var file_ext = name_and_ext[1];
381 var content_type;
382 if (file_ext === '.ipynb') {
383 model.type = 'notebook';
384 model.format = 'json';
385 model.content = JSON.parse(filedata);
386 content_type = 'application/json';
387 } else {
388 model.type = 'file';
389 model.format = format;
390 model.content = filedata;
391 content_type = 'application/octet-stream';
392 }
393 var filedata = item.data('filedata');
394
375 var settings = {
395 var settings = {
376 processData : false,
396 processData : false,
377 cache : false,
397 cache : false,
378 type : 'PUT',
398 type : 'PUT',
379 dataType : 'json',
380 data : JSON.stringify(model),
399 data : JSON.stringify(model),
381 headers : {'Content-Type': content_type},
400 headers : {'Content-Type': content_type},
382 success : function (data, status, xhr) {
401 success : function (data, status, xhr) {
402 item.removeClass('new-file');
383 that.add_link(model, item);
403 that.add_link(model, item);
384 that.add_delete_button(item);
404 that.add_delete_button(item);
385 },
405 },
@@ -390,7 +410,7 b' define(['
390 that.base_url,
410 that.base_url,
391 'api/contents',
411 'api/contents',
392 that.notebook_path,
412 that.notebook_path,
393 nbname
413 filename
394 );
414 );
395 $.ajax(url, settings);
415 $.ajax(url, settings);
396 return false;
416 return false;
@@ -398,7 +418,6 b' define(['
398 var cancel_button = $('<button/>').text("Cancel")
418 var cancel_button = $('<button/>').text("Cancel")
399 .addClass("btn btn-default btn-xs")
419 .addClass("btn btn-default btn-xs")
400 .click(function (e) {
420 .click(function (e) {
401 console.log('cancel click');
402 item.remove();
421 item.remove();
403 return false;
422 return false;
404 });
423 });
General Comments 0
You need to be logged in to leave comments. Login now