Commit 9048229a authored by Sebastian's avatar Sebastian

Add: remaining functionality, updated title/reference handling

parent 3a3978b5
......@@ -40,15 +40,18 @@
});
}
function toNotebookModel(id, obj, keepContent) {
function toNotebookModel(id, obj, isRootDir) {
//console.log(obj.value.text_content);
var cont = null;
if(keepContent) {
var title = obj.title;
if(isRootDir) {
title += " [ref: " + obj.reference + "]";
} else {
cont = JSON.parse(obj.text_content);
transform_notebook(cont, unsplit_lines);
cont.metadata = cont.metadata || {};
cont.metadata = cont.metadata || {};
}
var nbobj = { "name": obj.title,
var nbobj = { "name": title,
"path": id,
"type": "notebook",
"format": "json",
......@@ -81,7 +84,7 @@
.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));
nbs.push(toNotebookModel(result.data.rows[i].id, result.data.rows[i].value, true));
}
var root_dir = {"name": "",
"path": "",
......@@ -95,7 +98,7 @@
// Get the notebook file
this.jio_get(e.detail.path)
.push(function(result) {
e.detail.resolve(toNotebookModel(e.detail.path, result, true));
e.detail.resolve(toNotebookModel(e.detail.path, result, false));
}, function(err) {
console.log(err);
e.detail.reject(err);
......@@ -114,7 +117,7 @@
})
.push(function(result_put) {
console.log("Notebook saved in ERP5", result_put);
e.detail.resolve(toNotebookModel(e.detail.path, result, true));
e.detail.resolve(toNotebookModel(e.detail.path, result, false));
}, function(err) {
console.log(err);
e.detail.reject(err);
......@@ -126,12 +129,12 @@
}
function handleCreate(e) {
var now = new Date().toUTCString();
var now = new Date();
var jioObj = {
portal_type: "Web JSON",
parent_relative_url: "web_page_module",
title: "untitled_notebook.ipynb",
reference: "untitled notebook from " + now,
title: "untitled_notebook",
reference: "ut_nb_from_" + now.getTime(),
text_content: JSON.stringify(createBlankNotebook())
};
var gadget = this;
......@@ -139,7 +142,7 @@
.push(function(newId) {
gadget.jio_get(newId)
.push(function(result) {
e.detail.resolve(toNotebookModel(newId, result, true));
e.detail.resolve(toNotebookModel(newId, result, false));
}, function(err) {
console.log(err);
e.detail.reject(err);
......@@ -150,6 +153,25 @@
});
}
function handleRename(e) {
var gadget = this;
gadget.jio_put(e.detail.path, {
title: e.detail.new_title,
})
.push(function(id) {
gadget.jio_get(id)
.push(function(result) {
e.detail.resolve(toNotebookModel(id, result, false));
}, function(err) {
console.log(err);
e.detail.reject(err);
})
}, function(err) {
console.log(err);
e.detail.reject(err);
});
}
/*
*
* RJS Stuffs
......@@ -168,13 +190,14 @@
.onEvent("get_event", handleGet, false, true)
.onEvent("save_event", handleSave, false, true)
.onEvent("create_event", handleCreate, false, true)
.onEvent("rename_event", handleRename, false, true)
.declareMethod('render', function () {
console.log("Rendering!");
document.jiocontentsReady = true;
return this;
});
function createBlankNotebook() {
function createBlankNotebook(kernel_type) {
return {
"cells": [
{ "cell_type": "code",
......
......@@ -88,10 +88,6 @@ define(function(require) {
Contents.DIRECTORY_NOT_EMPTY_ERROR = 'DirectoryNotEmptyError';
Contents.DirectoryNotEmptyError = function() {
// Constructor
//
// An error representing the result of attempting to delete a
// non-empty directory.
this.message = 'A directory must be empty before being deleted.';
};
......@@ -100,7 +96,6 @@ define(function(require) {
Contents.DirectoryNotEmptyError.prototype.name =
Contents.DIRECTORY_NOT_EMPTY_ERROR;
Contents.prototype.api_url = function() {
var url_parts = [
this.base_url, 'api/contents',
......@@ -109,17 +104,6 @@ define(function(require) {
return utils.url_path_join.apply(null, url_parts);
};
/**
* Creates a basic error handler that wraps a jqXHR error as an Error.
*
* Takes a callback that accepts an Error, and returns a callback that can
* be passed directly to $.ajax, which will wrap the error from jQuery
* as an Error, and pass that to the original callback.
*
* @method create_basic_error_handler
* @param{Function} callback
* @return{Function}
*/
Contents.prototype.create_basic_error_handler = function(callback) {
if (!callback) {
return utils.log_ajax_error;
......@@ -133,16 +117,6 @@ define(function(require) {
* File Functions (including notebook operations)
*/
/**
* Get a file.
*
* @method get
* @param {String} path
* @param {Object} options
* type : 'notebook', 'file', or 'directory'
* format: 'text' or 'base64'; only relevant for type: 'file'
* content: true or false; // whether to include the content
*/
Contents.prototype.get = function (path, options) {
return waitForReadyPromise()
.then(function() {
......@@ -159,22 +133,10 @@ define(function(require) {
});
};
/**
* Creates a new untitled file or directory in the specified directory
* path.
*
* @method new
* @param {String} path: the directory in which to create the new
* file/directory
* @param {Object} options:
* ext: file extension to use
* type: model type to create ('notebook', 'file', or 'directory')
*/
Contents.prototype.new_untitled = function(path, options) {
if(options.type !== "notebook") {
console.log("Creation of non-notebooks is not supported!");
return Promise.reject();
return Promise.reject("Creation of non-notebooks is not supported!");
}
return waitForReadyPromise()
.then(function() {
......@@ -192,36 +154,25 @@ define(function(require) {
};
Contents.prototype.delete = function(path) {
var settings = {
processData : false,
type : "DELETE",
dataType : "json",
};
var url = this.api_url(path);
return utils.promising_ajax(url, settings).catch(
// Translate certain errors to more specific ones.
function(error) {
// TODO: update IPEP27 to specify errors more precisely, so
// that error types can be detected here with certainty.
if (error.xhr.status === 400) {
throw new Contents.DirectoryNotEmptyError();
}
throw error;
}
);
return Promise.reject("Delete is currently not supported!");
};
Contents.prototype.rename = function(path, new_path) {
var data = {path: new_path};
var settings = {
processData : false,
type : "PATCH",
data : JSON.stringify(data),
dataType: "json",
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("rename_event", {
detail: {
path: path,
// get rid of path-prefix web_page_module. Not used in titles!
new_title: new_path.split("/")[1],
resolve: resolve,
reject: reject
}
});
getJiocontentsDiv().dispatchEvent(ev);
});
});
};
Contents.prototype.save = function(path, model) {
......@@ -242,20 +193,7 @@ define(function(require) {
};
Contents.prototype.copy = function(from_file, to_dir) {
/**
* Copy a file into a given directory via POST
* The server will select the name of the copied file
*/
var url = this.api_url(to_dir);
var settings = {
processData : false,
type: "POST",
data: JSON.stringify({copy_from: from_file}),
contentType: 'application/json',
dataType : "json",
};
return utils.promising_ajax(url, settings);
return Promise.reject("Copy is currently not supported!");
};
/**
......@@ -263,40 +201,22 @@ define(function(require) {
*/
Contents.prototype.create_checkpoint = function(path) {
var url = this.api_url(path, 'checkpoints');
var settings = {
type : "POST",
contentType: false, // no data
dataType : "json",
};
return utils.promising_ajax(url, settings);
return Promise.resolve({
id: "dummy-id",
last_modified: "2013-10-18T23:54:40+00:00"//dummy
});
};
Contents.prototype.list_checkpoints = function(path) {
var url = this.api_url(path, 'checkpoints');
var settings = {
type : "GET",
cache: false,
dataType: "json",
};
return utils.promising_ajax(url, settings);
return Promise.resolve([]);
};
Contents.prototype.restore_checkpoint = function(path, checkpoint_id) {
var url = this.api_url(path, 'checkpoints', checkpoint_id);
var settings = {
type : "POST",
contentType: false, // no data
};
return utils.promising_ajax(url, settings);
return Promise.resolve();
};
Contents.prototype.delete_checkpoint = function(path, checkpoint_id) {
var url = this.api_url(path, 'checkpoints', checkpoint_id);
var settings = {
type : "DELETE",
};
return utils.promising_ajax(url, settings);
return Promise.resolve();
};
/**
......
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