Commit 0b776b6b authored by Sebastian's avatar Sebastian

Add: Getting list from ERP5, getting notebook JSON loaded into page (buggy)

parent 3231315b
......@@ -5,60 +5,104 @@
(function (window, rJS) {
"use strict";
function toNotebookModel(obj) {
console.log(obj);
return { "name": obj.value.title,
// TODO: THIS PATH TO NOTEBOOK FILE
"path": "foo/bar/notebook1.ipynb",
//"path": "/localhost/Test-Notebook.ipynb",
"type": "notebook",
"format": "json",
"writable": true,
"last_modified": "2013-10-02T11:29:27.616675+00:00",
"created": "2013-10-01T12:21:20.123456+00:00",
"content": null
};
/*
Next two functions deal with a weird issue of unlin-ed strings, ie. list
of strings instead a single joined string. See
https://github.com/jupyter/jupyter-drive/blob/master/jupyterdrive/gdrive/notebook_model.js#L45
*/
function unsplit_lines(multiline_string) {
if (Array.isArray(multiline_string)) {
return multiline_string.join('');
} else {
return multiline_string;
}
}
function transform_notebook(notebook, transform_fn) {
if (!notebook['cells']) {
return null;
}
notebook['cells'].forEach(function (cell) {
if (cell['source']) {
cell['source'] = transform_fn(cell['source']);
}
if (cell['outputs']) {
cell['outputs'].forEach(function (output) {
if (output['data']) {
output['data'] = transform_fn(output['data']);
}
});
}
});
}
function toNotebookModel(id, obj, keepContent) {
//console.log(obj.value.text_content);
var cont = null;
if(keepContent) {
cont = JSON.parse(obj.text_content);
transform_notebook(cont, unsplit_lines);
cont.metadata = cont.metadata || {};
}
var nbobj = { "name": obj.title,
"path": id,
"type": "notebook",
"format": "json",
"writable": true,
"mimetype": "application/ipynb",
"content": cont,
//"content": keepContent ? obj.text_content : null
};
return nbobj;
}
function handleEvent(e) {
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]
})
if(e.detail.path === "") {
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]
})
.push(function(result) {
var nbs = [];
for(var i = 0; i < result.data.rows.length; i++) {
nbs.push(toNotebookModel(result.data.rows[i].id, result.data.rows[i].value, false));
}
var root_dir = {"name": "",
"path": "",
"last_modified": "2017-09-06T03:33:29.781159Z",
"created": "2017-09-06T03:33:29.781159Z",
"content": nbs,
};
e.detail.resolve(root_dir);
}, function(err) {
e.detail.reject();
console.log(err);
});
} else {
this.jio_get(e.detail.path)
.push(function(result) {
console.log("Querying Web JSON List");
console.log(result.data.rows);
var nbs = [];
for(var i = 0; i < result.data.rows.length; i++) {
nbs.push(toNotebookModel(result.data.rows[i]));
}
var root_dir = {"name": "",
"path": "",
"last_modified": "2017-09-06T03:33:29.781159Z",
"created": "2017-09-06T03:33:29.781159Z",
"content": nbs,
};
console.log(root_dir);
e.detail.resolve(root_dir);
e.detail.resolve(toNotebookModel(e.detail.path, result, true));
}, function(err) {
e.detail.reject();
console.log(err);
});
}
}
rJS(window)
.ready(function (gadget) {
console.log("Jio Contents Gadget yes yes.");
document.jiocontentsReady = true;
return gadget;
})
.declareMethod('render', function (options) {
})
.declareAcquiredMethod("jio_allDocs", "jio_allDocs")
.onEvent("custom_event", handleEvent, false, true);
.declareAcquiredMethod("jio_get", "jio_get")
.onEvent("custom_event", handleEvent, false, true)
.declareMethod('render', function () {
console.log("Rendering!");
document.jiocontentsReady = true;
return this;
});
}(window, rJS));
......@@ -99,7 +99,7 @@
return gadget.getDeclaredGadget("jiocontents_gadget");
})
.push(function(jiocontents_gadget) {
jiocontents_gadget.render({});
jiocontents_gadget.render();
});
})
.allowPublicAcquisition("getSetting", function (argument_list) {
......
......@@ -8,7 +8,7 @@ define(function(require) {
var utils = require('base/js/utils');
var baseUrl = "nbextensions/jiocontents";
var baseUrl = "/nbextensions/jiocontents";
var gadget_main_div = document.createElement("div");
gadget_main_div.setAttribute("data-gadget-url", baseUrl + "/gadget_jupyter_page.html");
......@@ -42,7 +42,7 @@ define(function(require) {
return new Promise(function(resolve, reject) {
var timerId = window.setInterval(function() {
if(document.jiocontentsReady) {
window.setTimeout(function() { resolve(); }, 200); // to allow for aquiredmethod to load
resolve();
clearInterval(timerId);
}
}, 100); // Check every 100ms to see if rjs-setup is complete and eventlistener available
......
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