Commit af0452bc authored by Sebastian's avatar Sebastian

Add: save functionality and slight modifications to get

parent b8b3a8ac
......@@ -7,8 +7,9 @@
/*
Next two functions deal with a weird issue of unlin-ed strings, ie. list
of strings instead a single joined string. See
of strings instead a single joined string. Adpoted from
https://github.com/jupyter/jupyter-drive/blob/master/jupyterdrive/gdrive/notebook_model.js#L45
with adjustment to match updates in jupyter's data model.
*/
function unsplit_lines(multiline_string) {
if (Array.isArray(multiline_string)) {
......@@ -29,7 +30,10 @@
if (cell['outputs']) {
cell['outputs'].forEach(function (output) {
if (output['data']) {
output['data'] = transform_fn(output['data']);
for(var key in output['data']) {
var t = transform_fn(output['data'][key]);
output['data'][key] = t;
}
}
});
}
......@@ -51,18 +55,28 @@
"writable": true,
"mimetype": "application/ipynb",
"content": cont,
//"content": keepContent ? obj.text_content : null
// TODO: Fix date representation. Might cause problems!
"created": obj.creation_date,
"last_modified": obj.modification_date,
};
return nbobj;
}
/*
*
* Event handlers API <-> JIO connection points
*
*/
function handleEvent(e) {
function handleGet(e) {
if(e.detail.path === "") {
// We a querying for the root directory. Should return a list of contents which themselves have no
// content
this.jio_allDocs({
query: 'portal_type: "Web JSON"',
select_list : ['text_content', 'title', 'reference'], //, 'modification_date'], NOT WORKING
// sort_on: [["modification_date", "descending"]], // NOT WORKING
limit: [0, 5]
select_list : ["text_content", "title", "reference", "creation_date", "modification_date"],
sort_on: [["modification_date", "descending"]],
limit: [0, 20]
})
.push(function(result) {
var nbs = [];
......@@ -71,24 +85,51 @@
}
var root_dir = {"name": "",
"path": "",
"last_modified": "2017-09-06T03:33:29.781159Z",
"created": "2017-09-06T03:33:29.781159Z",
"content": nbs,
};
"content": nbs };
e.detail.resolve(root_dir);
}, function(err) {
e.detail.reject();
console.log(err);
e.detail.reject(err);
});
} else {
// Get the notebook file
this.jio_get(e.detail.path)
.push(function(result) {
e.detail.resolve(toNotebookModel(e.detail.path, result, true));
}, function(err) {
console.log(err);
e.detail.reject(err);
});
}
}
function handleSave(e) {
var gadget = this;
gadget.jio_get(e.detail.path)
.push(function(result) {
gadget.jio_put(e.detail.path, {
title: result.title,
reference: result.reference,
text_content: e.detail.model
})
.push(function(result_put) {
console.log("Notebook saved in ERP5", result_put);
e.detail.resolve(toNotebookModel(e.detail.path, result, true));
}, function(err) {
console.log(err);
e.detail.reject(err);
});
}, function (err) {
console.log(err);
e.detail.reject(err);
});
}
/*
*
* RJS Stuffs
*
*/
rJS(window)
.ready(function (gadget) {
......@@ -97,7 +138,9 @@
})
.declareAcquiredMethod("jio_allDocs", "jio_allDocs")
.declareAcquiredMethod("jio_get", "jio_get")
.onEvent("custom_event", handleEvent, false, true)
.declareAcquiredMethod("jio_put", "jio_put")
.onEvent("get_event", handleGet, false, true)
.onEvent("save_event", handleSave, false, true)
.declareMethod('render', function () {
console.log("Rendering!");
document.jiocontentsReady = true;
......
......@@ -147,7 +147,7 @@ define(function(require) {
return waitForReadyPromise()
.then(function() {
return new Promise(function(resolve, reject) {
var ev = new CustomEvent("custom_event", {
var ev = new CustomEvent("get_event", {
detail: {
path: path,
resolve: resolve,
......@@ -217,20 +217,22 @@ define(function(require) {
};
Contents.prototype.save = function(path, model) {
/**
* We do the call with settings so we can set cache to false.
*/
var settings = {
processData : false,
type : "PUT",
dataType: "json",
data : JSON.stringify(model),
contentType: 'application/json',
};
var url = this.api_url(path);
return utils.promising_ajax(url, settings);
return waitForReadyPromise()
.then(function() {
return new Promise(function(resolve, reject) {
var ev = new CustomEvent("save_event", {
detail: {
path: path,
model: JSON.stringify(model.content),
resolve: resolve,
reject: reject
}
});
getJiocontentsDiv().dispatchEvent(ev);
});
});
};
Contents.prototype.copy = function(from_file, to_dir) {
/**
* Copy a file into a given directory via POST
......
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