Commit f42f5b42 authored by Romain Casati's avatar Romain Casati
Browse files

Better way of opening file.

parent a520c74a
......@@ -196,6 +196,54 @@ Un lien vers la page de Basthon avec le contenu actuel du script a été créé.
* Load a notebook.
*/
that.openNotebook = function (file) {
return new Promise(function (resolve, reject) {
const reader = new FileReader();
reader.readAsText(file);
reader.onload = async function (event) {
/* TODO: connect filename to notebook name */
await that.notebook.load(JSON.parse(event.target.result));
// notification seems useless here.
resolve();
};
reader.onerror = reject;
});
};
/**
* Loading file in the (emulated) local filesystem (async).
*/
that.putFSRessource = function (file) {
return new Promise(function (resolve, reject) {
const reader = new FileReader();
reader.readAsArrayBuffer(file);
reader.onload = async function (event) {
await Basthon.putRessource(file.name, event.target.result);
const msg = $("<div>").html(
file.name + " est maintenant utilisable depuis Python");
that.dialog.modal({
notebook: that.notebook,
keyboard_manager: that.notebook.keyboard_manager,
title : "Fichier utilisable depuis Python",
body : msg,
buttons : {
OK: {
"class": "btn-primary",
},
}
});
resolve();
};
reader.onerror = reject;
});
};
/**
* Opening file: If it has .ipynb extension, load the notebok,
* if it has .py extension, loading it in the first cell
* or put on (emulated) local filesystem (user is asked to),
* otherwise, loading it in the local filesystem.
*/
that.openFile = function () {
return new Promise(function (resolve, reject) {
var input = document.createElement('input');
input.type = 'file';
......@@ -203,22 +251,13 @@ Un lien vers la page de Basthon avec le contenu actuel du script a été créé.
input.onchange = async function (event) {
for( var file of event.target.files ) {
const ext = file.name.split('.').pop();
var reader = new FileReader();
if(ext === 'ipynb') {
reader.readAsText(file);
reader.onload = function (event) {
/* TODO: connect filename to notebook name */
await that.notebook.load(JSON.parse(event.target.result));
resolve();
};
await that.openNotebook(file);
} else {
reader.readAsArrayBuffer(file);
reader.onload = function (event) {
await Basthon.putFile(file.name, event.target.result);
resolve();
};
await that.putFSRessource(file);
}
}
resolve();
}
document.body.appendChild(input);
input.click();
......
......@@ -891,7 +891,7 @@ define([
help_index: 'fd',
icon: 'fa-folder-open-o',
handler : function (env, event) {
basthonGUI.openNotebook();
basthonGUI.openFile();
if(event){
event.preventDefault();
}
......
......@@ -159,7 +159,7 @@ define([
var that = this;
this.element.find('#open_notebook').click(function () {
basthonGUI.openNotebook();
basthonGUI.openFile();
});
this.element.find('#copy_notebook').click(function () {
that.notebook.copy_notebook();
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment