Commit af0452bc authored by Sebastian's avatar Sebastian

Add: save functionality and slight modifications to get

parent b8b3a8ac
...@@ -7,8 +7,9 @@ ...@@ -7,8 +7,9 @@
/* /*
Next two functions deal with a weird issue of unlin-ed strings, ie. list 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 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) { function unsplit_lines(multiline_string) {
if (Array.isArray(multiline_string)) { if (Array.isArray(multiline_string)) {
...@@ -29,7 +30,10 @@ ...@@ -29,7 +30,10 @@
if (cell['outputs']) { if (cell['outputs']) {
cell['outputs'].forEach(function (output) { cell['outputs'].forEach(function (output) {
if (output['data']) { 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 @@ ...@@ -51,18 +55,28 @@
"writable": true, "writable": true,
"mimetype": "application/ipynb", "mimetype": "application/ipynb",
"content": cont, "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; return nbobj;
} }
/*
*
* Event handlers API <-> JIO connection points
*
*/
function handleEvent(e) { function handleGet(e) {
if(e.detail.path === "") { 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({ this.jio_allDocs({
query: 'portal_type: "Web JSON"', query: 'portal_type: "Web JSON"',
select_list : ['text_content', 'title', 'reference'], //, 'modification_date'], NOT WORKING select_list : ["text_content", "title", "reference", "creation_date", "modification_date"],
// sort_on: [["modification_date", "descending"]], // NOT WORKING sort_on: [["modification_date", "descending"]],
limit: [0, 5] limit: [0, 20]
}) })
.push(function(result) { .push(function(result) {
var nbs = []; var nbs = [];
...@@ -71,24 +85,51 @@ ...@@ -71,24 +85,51 @@
} }
var root_dir = {"name": "", var root_dir = {"name": "",
"path": "", "path": "",
"last_modified": "2017-09-06T03:33:29.781159Z", "content": nbs };
"created": "2017-09-06T03:33:29.781159Z",
"content": nbs,
};
e.detail.resolve(root_dir); e.detail.resolve(root_dir);
}, function(err) { }, function(err) {
e.detail.reject();
console.log(err); console.log(err);
e.detail.reject(err);
}); });
} else { } else {
// Get the notebook file
this.jio_get(e.detail.path) this.jio_get(e.detail.path)
.push(function(result) { .push(function(result) {
e.detail.resolve(toNotebookModel(e.detail.path, result, true)); e.detail.resolve(toNotebookModel(e.detail.path, result, true));
}, function(err) { }, function(err) {
console.log(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) rJS(window)
.ready(function (gadget) { .ready(function (gadget) {
...@@ -97,7 +138,9 @@ ...@@ -97,7 +138,9 @@
}) })
.declareAcquiredMethod("jio_allDocs", "jio_allDocs") .declareAcquiredMethod("jio_allDocs", "jio_allDocs")
.declareAcquiredMethod("jio_get", "jio_get") .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 () { .declareMethod('render', function () {
console.log("Rendering!"); console.log("Rendering!");
document.jiocontentsReady = true; document.jiocontentsReady = true;
......
...@@ -147,7 +147,7 @@ define(function(require) { ...@@ -147,7 +147,7 @@ define(function(require) {
return waitForReadyPromise() return waitForReadyPromise()
.then(function() { .then(function() {
return new Promise(function(resolve, reject) { return new Promise(function(resolve, reject) {
var ev = new CustomEvent("custom_event", { var ev = new CustomEvent("get_event", {
detail: { detail: {
path: path, path: path,
resolve: resolve, resolve: resolve,
...@@ -217,20 +217,22 @@ define(function(require) { ...@@ -217,20 +217,22 @@ define(function(require) {
}; };
Contents.prototype.save = function(path, model) { Contents.prototype.save = function(path, model) {
/** return waitForReadyPromise()
* We do the call with settings so we can set cache to false. .then(function() {
*/ return new Promise(function(resolve, reject) {
var settings = { var ev = new CustomEvent("save_event", {
processData : false, detail: {
type : "PUT", path: path,
dataType: "json", model: JSON.stringify(model.content),
data : JSON.stringify(model), resolve: resolve,
contentType: 'application/json', reject: reject
}; }
var url = this.api_url(path); });
return utils.promising_ajax(url, settings); getJiocontentsDiv().dispatchEvent(ev);
});
});
}; };
Contents.prototype.copy = function(from_file, to_dir) { Contents.prototype.copy = function(from_file, to_dir) {
/** /**
* Copy a file into a given directory via POST * 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