##// END OF EJS Templates
Modifies Contents API to return Error objects...
Modifies Contents API to return Error objects Modfies the Contents class to return JavaScript Error objects instead of passing on the return values from $.ajax(). This has two advantages. First, it allows the content manager to parse errors and give more informative messages than the ajax response. Second, it makes the Contents interface more general, since other kinds of backends might generate client-side errors.

File last commit:

r18661:d632dcb6
r18661:d632dcb6
Show More
contents.js
251 lines | 8.2 KiB | application/javascript | JavascriptLexer
jhemmelg
Initial interface for javascript contentmanagers...
r18618 // Copyright (c) IPython Development Team.
// Distributed under the terms of the Modified BSD License.
define([
'base/js/namespace',
'jquery',
KesterTong
Style and bug fixes
r18620 'base/js/utils',
KesterTong
Adds dialog on new_notebook failure...
r18623 'base/js/dialog',
], function(IPython, $, utils, dialog) {
Jeff Hemmelgarn
Move contentmanager to contents
r18643 var Contents = function(options) {
jhemmelg
Initial interface for javascript contentmanagers...
r18618 // Constructor
//
Jeff Hemmelgarn
Move contentmanager to contents
r18643 // A contents handles passing file operations
jhemmelg
Initial interface for javascript contentmanagers...
r18618 // to the back-end. This includes checkpointing
// with the normal file operations.
//
// Parameters:
KesterTong
Use IPython style constructor
r18621 // options: dictionary
// Dictionary of keyword arguments.
// base_url: string
this.base_url = options.base_url;
KesterTong
Style and bug fixes
r18620 };
Thomas Kluyver
Add Contents.api_url convenience function
r18650
Kester Tong
Modifies Contents API to return Error objects...
r18661 /** Error type */
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.';
}
Contents.DirectoryNotEmptyError.prototype = new Error;
Contents.DirectoryNotEmptyError.prototype.name =
Contents.DIRECTORY_NOTE_EMPTY_ERROR;
Thomas Kluyver
Add Contents.api_url convenience function
r18650 Contents.prototype.api_url = function() {
var url_parts = [this.base_url, 'api/contents'].concat(
Array.prototype.slice.apply(arguments));
return utils.url_join_encode.apply(null, url_parts);
};
KesterTong
Make ContentManager stateless...
r18622 /**
Kester Tong
Modifies Contents API to return Error objects...
r18661 * 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 function(xhr, status, error) { };
}
return function(xhr, status, error) {
callback(utils.wrap_ajax_error(xhr, status, error));
};
}
/**
Thomas Kluyver
Make contents JS API consistent
r18653 * File Functions (including notebook operations)
KesterTong
Moves list_notebooks to ContentManager
r18636 */
/**
Thomas Kluyver
Make contents JS API consistent
r18653 * Load a file.
KesterTong
Moves load_notebook to ContentManager and adds new_notebook to Google Drive version
r18638 *
Thomas Kluyver
Rename callback parameters to success/error
r18658 * Calls success with file JSON model, or error with error.
KesterTong
Moves load_notebook to ContentManager and adds new_notebook to Google Drive version
r18638 *
* @method load_notebook
* @param {String} path
* @param {String} name
Thomas Kluyver
Rename callback parameters to success/error
r18658 * @param {Function} success
* @param {Function} error
KesterTong
Moves load_notebook to ContentManager and adds new_notebook to Google Drive version
r18638 */
Thomas Kluyver
Make contents JS API consistent
r18653 Contents.prototype.load_file = function (path, name, options) {
KesterTong
Moves load_notebook to ContentManager and adds new_notebook to Google Drive version
r18638 // We do the call with settings so we can set cache to false.
var settings = {
processData : false,
cache : false,
type : "GET",
dataType : "json",
Thomas Kluyver
Rename callback parameters to success/error
r18658 success : options.success,
Kester Tong
Modifies Contents API to return Error objects...
r18661 error : this.create_basic_error_handler(options.error)
KesterTong
Moves load_notebook to ContentManager and adds new_notebook to Google Drive version
r18638 };
Thomas Kluyver
Add Contents.api_url convenience function
r18650 var url = this.api_url(path, name);
KesterTong
Moves load_notebook to ContentManager and adds new_notebook to Google Drive version
r18638 $.ajax(url, settings);
};
/**
Thomas Kluyver
Make contents JS API consistent
r18653 * Creates a new notebook file at the specified directory path.
KesterTong
Make ContentManager stateless...
r18622 *
* @method scroll_to_cell
* @param {String} path The path to create the new notebook at
*/
Thomas Kluyver
Don't do UI stuff in contents API module
r18649 Contents.prototype.new_notebook = function(path, options) {
Thomas Kluyver
Rename callback parameters to success/error
r18658 var error = options.error || function() {};
jhemmelg
ContentManager function signatures updated
r18619 var settings = {
processData : false,
cache : false,
type : "POST",
dataType : "json",
Thomas Kluyver
Rename callback parameters to success/error
r18658 success : options.success || function() {},
Kester Tong
Modifies Contents API to return Error objects...
r18661 error : this.create_basic_error_handler(options.error)
jhemmelg
ContentManager function signatures updated
r18619 };
Thomas Kluyver
Add Contents.api_url convenience function
r18650 $.ajax(this.api_url(path), settings);
KesterTong
Style and bug fixes
r18620 };
jhemmelg
Initial interface for javascript contentmanagers...
r18618
Thomas Kluyver
Make contents JS API consistent
r18653 Contents.prototype.delete_file = function(name, path, options) {
Thomas Kluyver
Rename callback parameters to success/error
r18658 var error = options.error || function() {};
Thomas Kluyver
Make contents JS API consistent
r18653 var that = this;
jhemmelg
ContentManager function signatures updated
r18619 var settings = {
processData : false,
cache : false,
type : "DELETE",
KesterTong
Removes unnecessary parameter from ContentManager.delete_notebook
r18629 dataType : "json",
Thomas Kluyver
Rename callback parameters to success/error
r18658 success : options.success || function() {},
Thomas Kluyver
Make contents JS API consistent
r18653 error : function(xhr, status, error) {
Kester Tong
Modifies Contents API to return Error objects...
r18661 // TODO: update IPEP27 to specify errors more precisely, so
// that error types can be detected here with certainty.
if (xhr.status === 400) {
error(new Contents.DirectoryNotEmptyError());
}
error(utils.wrap_ajax_error(xhr, status, error));
Thomas Kluyver
Make contents JS API consistent
r18653 }
jhemmelg
ContentManager function signatures updated
r18619 };
Thomas Kluyver
Add Contents.api_url convenience function
r18650 var url = this.api_url(path, name);
jhemmelg
ContentManager function signatures updated
r18619 $.ajax(url, settings);
KesterTong
Style and bug fixes
r18620 };
jhemmelg
Initial interface for javascript contentmanagers...
r18618
Thomas Kluyver
Make contents JS API consistent
r18653 Contents.prototype.rename_file = function(path, name, new_path, new_name, options) {
var data = {name: new_name, path: new_path};
jhemmelg
ContentManager function signatures updated
r18619 var settings = {
processData : false,
cache : false,
type : "PATCH",
data : JSON.stringify(data),
dataType: "json",
Jeff Hemmelgarn
Move notebook.rename to contentmanager...
r18627 contentType: 'application/json',
Kester Tong
Modifies Contents API to return Error objects...
r18661 success : options.success || function() {},
error : this.create_basic_error_handler(options.error)
jhemmelg
ContentManager function signatures updated
r18619 };
Thomas Kluyver
Add Contents.api_url convenience function
r18650 var url = this.api_url(path, name);
jhemmelg
ContentManager function signatures updated
r18619 $.ajax(url, settings);
KesterTong
Style and bug fixes
r18620 };
jhemmelg
Initial interface for javascript contentmanagers...
r18618
Thomas Kluyver
Make contents JS API consistent
r18653 Contents.prototype.save_file = function(path, name, model, options) {
jhemmelg
ContentManager function signatures updated
r18619 // We do the call with settings so we can set cache to false.
var settings = {
processData : false,
cache : false,
type : "PUT",
data : JSON.stringify(model),
Jeff Hemmelgarn
Move notebook.rename to contentmanager...
r18627 contentType: 'application/json',
Thomas Kluyver
Rename callback parameters to success/error
r18658 success : options.success || function() {},
Kester Tong
Modifies Contents API to return Error objects...
r18661 error : this.create_basic_error_handler(options.error)
jhemmelg
ContentManager function signatures updated
r18619 };
Thomas Kluyver
Make contents JS API consistent
r18653 if (options.extra_settings) {
$.extend(settings, options.extra_settings);
jhemmelg
ContentManager function signatures updated
r18619 }
Thomas Kluyver
Add Contents.api_url convenience function
r18650 var url = this.api_url(path, name);
jhemmelg
ContentManager function signatures updated
r18619 $.ajax(url, settings);
};
jhemmelg
Initial interface for javascript contentmanagers...
r18618
KesterTong
Moves list_notebooks to ContentManager
r18636 /**
* Checkpointing Functions
*/
Thomas Kluyver
Standardise JS checkpointing API, use it for notebooks
r18652 Contents.prototype.create_checkpoint = function(path, name, options) {
var url = this.api_url(path, name, 'checkpoints');
var settings = {
type : "POST",
Thomas Kluyver
Rename callback parameters to success/error
r18658 success: options.success || function() {},
Kester Tong
Modifies Contents API to return Error objects...
r18661 error : this.create_basic_error_handler(options.error)
Thomas Kluyver
Standardise JS checkpointing API, use it for notebooks
r18652 };
$.ajax(url, settings);
KesterTong
Style and bug fixes
r18620 };
jhemmelg
Initial interface for javascript contentmanagers...
r18618
Thomas Kluyver
Standardise JS checkpointing API, use it for notebooks
r18652 Contents.prototype.list_checkpoints = function(path, name, options) {
var url = this.api_url(path, name, 'checkpoints');
var settings = {
type : "GET",
Thomas Kluyver
Rename callback parameters to success/error
r18658 success: options.success,
Kester Tong
Modifies Contents API to return Error objects...
r18661 error : this.create_basic_error_handler(options.error)
Thomas Kluyver
Standardise JS checkpointing API, use it for notebooks
r18652 };
$.ajax(url, settings);
KesterTong
Style and bug fixes
r18620 };
jhemmelg
Initial interface for javascript contentmanagers...
r18618
Thomas Kluyver
Standardise JS checkpointing API, use it for notebooks
r18652 Contents.prototype.restore_checkpoint = function(path, name, checkpoint_id, options) {
var url = this.api_url(path, name, 'checkpoints', checkpoint_id);
var settings = {
type : "POST",
Thomas Kluyver
Rename callback parameters to success/error
r18658 success: options.success || function() {},
Kester Tong
Modifies Contents API to return Error objects...
r18661 error : this.create_basic_error_handler(options.error)
Thomas Kluyver
Standardise JS checkpointing API, use it for notebooks
r18652 };
$.ajax(url, settings);
};
Kester Tong
Modifies Contents API to return Error objects...
r18661 Contents.prototype.delete_checkpoint = function(path, name, checkpoint_id, options) {
Thomas Kluyver
Standardise JS checkpointing API, use it for notebooks
r18652 var url = this.api_url(path, name, 'checkpoints', checkpoint_id);
var settings = {
type : "DELETE",
Thomas Kluyver
Rename callback parameters to success/error
r18658 success: options.success || function() {},
Kester Tong
Modifies Contents API to return Error objects...
r18661 error : this.create_basic_error_handler(options.error)
Thomas Kluyver
Standardise JS checkpointing API, use it for notebooks
r18652 };
$.ajax(url, settings);
KesterTong
Style and bug fixes
r18620 };
KesterTong
Moves list_notebooks to ContentManager
r18636 /**
* File management functions
*/
/**
* List notebooks and directories at a given path
*
* On success, load_callback is called with an array of dictionaries
* representing individual files or directories. Each dictionary has
* the keys:
* type: "notebook" or "directory"
* name: the name of the file or directory
* created: created date
* last_modified: last modified dat
* path: the path
* @method list_notebooks
* @param {String} path The path to list notebooks in
* @param {Function} load_callback called with list of notebooks on success
Thomas Kluyver
Rename callback parameters to success/error
r18658 * @param {Function} error called with ajax results on error
KesterTong
Moves list_notebooks to ContentManager
r18636 */
Thomas Kluyver
Make contents JS API consistent
r18653 Contents.prototype.list_contents = function(path, options) {
KesterTong
Moves list_notebooks to ContentManager
r18636 var settings = {
processData : false,
cache : false,
type : "GET",
dataType : "json",
Thomas Kluyver
Rename callback parameters to success/error
r18658 success : options.success,
Kester Tong
Modifies Contents API to return Error objects...
r18661 error : this.create_basic_error_handler(options.error)
KesterTong
Moves list_notebooks to ContentManager
r18636 };
Thomas Kluyver
Add Contents.api_url convenience function
r18650 $.ajax(this.api_url(path), settings);
Thomas Kluyver
Add missing semicolon
r18647 };
KesterTong
Moves list_notebooks to ContentManager
r18636
Jeff Hemmelgarn
Move contentmanager to contents
r18643 IPython.Contents = Contents;
jhemmelg
Initial interface for javascript contentmanagers...
r18618
Jeff Hemmelgarn
Move contentmanager to contents
r18643 return {'Contents': Contents};
Kester Tong
Modifies Contents API to return Error objects...
r18661 });