Show More
@@ -75,30 +75,27 b' define([' | |||
|
75 | 75 | } |
|
76 | 76 | for (var i = 0; i < files.length; i++) { |
|
77 | 77 | var f = files[i]; |
|
78 | var reader = new FileReader(); | |
|
79 | reader.readAsText(f); | |
|
80 | 78 | var name_and_ext = utils.splitext(f.name); |
|
81 | 79 | var file_ext = name_and_ext[1]; |
|
80 | ||
|
81 | var reader = new FileReader(); | |
|
82 | 82 | if (file_ext === '.ipynb') { |
|
83 | var item = that.new_item(0); | |
|
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 | }; | |
|
83 | reader.readAsText(f); | |
|
94 | 84 | } else { |
|
95 | var dialog_body = 'Uploaded notebooks must be .ipynb files'; | |
|
96 | dialog.modal({ | |
|
97 | title : 'Invalid file type', | |
|
98 | body : dialog_body, | |
|
99 | buttons : {'OK' : {'class' : 'btn-primary'}} | |
|
100 | }); | |
|
85 | // read non-notebook files as binary | |
|
86 | reader.readAsArrayBuffer(f); | |
|
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 | 100 | // Replace the file input form wth a clone of itself. This is required to |
|
104 | 101 | // reset the form. Otherwise, if you upload a file, delete it and try to |
@@ -268,16 +265,16 b' define([' | |||
|
268 | 265 | item.find(".item_icon").addClass('notebook_icon').addClass('icon-fixed-width'); |
|
269 | 266 | item.find(".item_name").empty().append( |
|
270 | 267 | $('<input/>') |
|
271 |
.addClass(" |
|
|
272 |
.attr('value', |
|
|
268 | .addClass("filename_input") | |
|
269 | .attr('value', name) | |
|
273 | 270 | .attr('size', '30') |
|
274 | 271 | .attr('type', 'text') |
|
275 | 272 | ); |
|
276 | 273 | }; |
|
277 | 274 | |
|
278 | 275 | |
|
279 |
NotebookList.prototype.add_ |
|
|
280 |
item.data(' |
|
|
276 | NotebookList.prototype.add_file_data = function (data, item) { | |
|
277 | item.data('filedata', data); | |
|
281 | 278 | }; |
|
282 | 279 | |
|
283 | 280 | |
@@ -314,8 +311,8 b' define([' | |||
|
314 | 311 | click(function (e) { |
|
315 | 312 | // $(this) is the button that was clicked. |
|
316 | 313 | var that = $(this); |
|
317 |
// We use the |
|
|
318 | // data because the outer scopes values change as we iterate through the loop. | |
|
314 | // We use the filename from the parent list_item element's | |
|
315 | // data because the outer scope's values change as we iterate through the loop. | |
|
319 | 316 | var parent_item = that.parents('div.list_item'); |
|
320 | 317 | var name = parent_item.data('name'); |
|
321 | 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 | 355 | var that = this; |
|
359 | 356 | var upload_button = $('<button/>').text("Upload") |
|
360 | 357 | .addClass('btn btn-primary btn-xs upload_button') |
|
361 | 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 | 359 | var path = that.notebook_path; |
|
367 |
var |
|
|
368 | var content_type = 'application/json'; | |
|
360 | var filename = item.find('.item_name > input').val(); | |
|
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 | 374 | var model = { |
|
370 | 375 | path: path, |
|
371 |
name: |
|
|
372 | content : JSON.parse(nbdata), | |
|
373 | type : 'notebook' | |
|
376 | name: filename | |
|
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 | 395 | var settings = { |
|
376 | 396 | processData : false, |
|
377 | 397 | cache : false, |
|
378 | 398 | type : 'PUT', |
|
379 | dataType : 'json', | |
|
380 | 399 | data : JSON.stringify(model), |
|
381 | 400 | headers : {'Content-Type': content_type}, |
|
382 | 401 | success : function (data, status, xhr) { |
|
402 | item.removeClass('new-file'); | |
|
383 | 403 | that.add_link(model, item); |
|
384 | 404 | that.add_delete_button(item); |
|
385 | 405 | }, |
@@ -390,7 +410,7 b' define([' | |||
|
390 | 410 | that.base_url, |
|
391 | 411 | 'api/contents', |
|
392 | 412 | that.notebook_path, |
|
393 |
|
|
|
413 | filename | |
|
394 | 414 | ); |
|
395 | 415 | $.ajax(url, settings); |
|
396 | 416 | return false; |
@@ -398,7 +418,6 b' define([' | |||
|
398 | 418 | var cancel_button = $('<button/>').text("Cancel") |
|
399 | 419 | .addClass("btn btn-default btn-xs") |
|
400 | 420 | .click(function (e) { |
|
401 | console.log('cancel click'); | |
|
402 | 421 | item.remove(); |
|
403 | 422 | return false; |
|
404 | 423 | }); |
General Comments 0
You need to be logged in to leave comments.
Login now