contents.js
264 lines
| 8.5 KiB
| application/javascript
|
JavascriptLexer
|
r18618 | // Copyright (c) IPython Development Team. | ||
// Distributed under the terms of the Modified BSD License. | ||||
define([ | ||||
'base/js/namespace', | ||||
'jquery', | ||||
|
r18620 | 'base/js/utils', | ||
|
r18717 | ], function(IPython, $, utils) { | ||
|
r18643 | var Contents = function(options) { | ||
|
r18618 | // Constructor | ||
// | ||||
|
r18643 | // A contents handles passing file operations | ||
|
r18618 | // to the back-end. This includes checkpointing | ||
// with the normal file operations. | ||||
// | ||||
// Parameters: | ||||
|
r18621 | // options: dictionary | ||
// Dictionary of keyword arguments. | ||||
// base_url: string | ||||
this.base_url = options.base_url; | ||||
|
r18620 | }; | ||
|
r18650 | |||
|
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.'; | ||||
|
r18752 | }; | ||
Contents.DirectoryNotEmptyError.prototype = Object.create(Error.prototype); | ||||
|
r18661 | Contents.DirectoryNotEmptyError.prototype.name = | ||
|
r18662 | Contents.DIRECTORY_NOT_EMPTY_ERROR; | ||
|
r18661 | |||
|
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); | ||||
}; | ||||
|
r18622 | /** | ||
|
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) { | ||||
|
r18752 | return utils.log_ajax_error; | ||
|
r18661 | } | ||
return function(xhr, status, error) { | ||||
callback(utils.wrap_ajax_error(xhr, status, error)); | ||||
}; | ||||
|
r18752 | }; | ||
|
r18661 | |||
/** | ||||
|
r18653 | * File Functions (including notebook operations) | ||
|
r18636 | */ | ||
/** | ||||
|
r18752 | * Get a file. | ||
|
r18638 | * | ||
|
r18658 | * Calls success with file JSON model, or error with error. | ||
|
r18638 | * | ||
|
r18752 | * @method get | ||
|
r18638 | * @param {String} path | ||
|
r18658 | * @param {Function} success | ||
* @param {Function} error | ||||
|
r18638 | */ | ||
|
r18752 | Contents.prototype.get = function (path, options) { | ||
|
r18638 | // We do the call with settings so we can set cache to false. | ||
var settings = { | ||||
processData : false, | ||||
cache : false, | ||||
type : "GET", | ||||
dataType : "json", | ||||
|
r18658 | success : options.success, | ||
|
r18661 | error : this.create_basic_error_handler(options.error) | ||
|
r18638 | }; | ||
|
r18752 | var url = this.api_url(path); | ||
|
r18638 | $.ajax(url, settings); | ||
}; | ||||
/** | ||||
|
r18760 | * Creates a new untitled file or directory in the specified directory path. | ||
|
r18622 | * | ||
|
r18752 | * @method new | ||
|
r18760 | * @param {String} path: the directory in which to create the new file/directory | ||
|
r18676 | * @param {Object} options: | ||
|
r18752 | * ext: file extension to use | ||
|
r18758 | * type: model type to create ('notebook', 'file', or 'directory') | ||
|
r18622 | */ | ||
|
r18760 | Contents.prototype.new_untitled = function(path, options) { | ||
|
r18758 | var data = JSON.stringify({ | ||
ext: options.ext, | ||||
type: options.type | ||||
}); | ||||
|
r18676 | |||
|
r18619 | var settings = { | ||
processData : false, | ||||
|
r18752 | type : "POST", | ||
|
r18676 | data: data, | ||
|
r18619 | dataType : "json", | ||
|
r18658 | success : options.success || function() {}, | ||
|
r18661 | error : this.create_basic_error_handler(options.error) | ||
|
r18619 | }; | ||
|
r18650 | $.ajax(this.api_url(path), settings); | ||
|
r18620 | }; | ||
|
r18618 | |||
|
r18752 | Contents.prototype.delete = function(path, options) { | ||
|
r18663 | var error_callback = options.error || function() {}; | ||
|
r18619 | var settings = { | ||
processData : false, | ||||
type : "DELETE", | ||||
|
r18629 | dataType : "json", | ||
|
r18658 | success : options.success || function() {}, | ||
|
r18653 | error : function(xhr, status, error) { | ||
|
r18661 | // TODO: update IPEP27 to specify errors more precisely, so | ||
// that error types can be detected here with certainty. | ||||
if (xhr.status === 400) { | ||||
|
r18663 | error_callback(new Contents.DirectoryNotEmptyError()); | ||
|
r18661 | } | ||
|
r18663 | error_callback(utils.wrap_ajax_error(xhr, status, error)); | ||
|
r18653 | } | ||
|
r18619 | }; | ||
|
r18752 | var url = this.api_url(path); | ||
|
r18619 | $.ajax(url, settings); | ||
|
r18620 | }; | ||
|
r18618 | |||
|
r18752 | Contents.prototype.rename = function(path, new_path, options) { | ||
var data = {path: new_path}; | ||||
|
r18619 | var settings = { | ||
processData : false, | ||||
type : "PATCH", | ||||
data : JSON.stringify(data), | ||||
dataType: "json", | ||||
|
r18627 | contentType: 'application/json', | ||
|
r18661 | success : options.success || function() {}, | ||
error : this.create_basic_error_handler(options.error) | ||||
|
r18619 | }; | ||
|
r18752 | var url = this.api_url(path); | ||
|
r18619 | $.ajax(url, settings); | ||
|
r18620 | }; | ||
|
r18618 | |||
|
r18752 | Contents.prototype.save = function(path, model, options) { | ||
|
r18619 | // We do the call with settings so we can set cache to false. | ||
var settings = { | ||||
processData : false, | ||||
type : "PUT", | ||||
data : JSON.stringify(model), | ||||
|
r18627 | contentType: 'application/json', | ||
|
r18658 | success : options.success || function() {}, | ||
|
r18661 | error : this.create_basic_error_handler(options.error) | ||
|
r18619 | }; | ||
|
r18752 | var url = this.api_url(path); | ||
|
r18619 | $.ajax(url, settings); | ||
}; | ||||
|
r18665 | |||
|
r18752 | Contents.prototype.copy = function(from_file, to_dir, options) { | ||
// 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); | ||||
|
r18665 | |||
var settings = { | ||||
processData : false, | ||||
|
r18752 | type: "POST", | ||
data: JSON.stringify({copy_from: from_file}), | ||||
|
r18665 | dataType : "json", | ||
success: options.success || function() {}, | ||||
error: this.create_basic_error_handler(options.error) | ||||
}; | ||||
$.ajax(url, settings); | ||||
}; | ||||
|
r18618 | |||
|
r18636 | /** | ||
* Checkpointing Functions | ||||
*/ | ||||
|
r18752 | Contents.prototype.create_checkpoint = function(path, options) { | ||
var url = this.api_url(path, 'checkpoints'); | ||||
|
r18652 | var settings = { | ||
type : "POST", | ||||
|
r18658 | success: options.success || function() {}, | ||
|
r18661 | error : this.create_basic_error_handler(options.error) | ||
|
r18652 | }; | ||
$.ajax(url, settings); | ||||
|
r18620 | }; | ||
|
r18618 | |||
|
r18752 | Contents.prototype.list_checkpoints = function(path, options) { | ||
var url = this.api_url(path, 'checkpoints'); | ||||
|
r18652 | var settings = { | ||
type : "GET", | ||||
|
r18658 | success: options.success, | ||
|
r18661 | error : this.create_basic_error_handler(options.error) | ||
|
r18652 | }; | ||
$.ajax(url, settings); | ||||
|
r18620 | }; | ||
|
r18618 | |||
|
r18752 | Contents.prototype.restore_checkpoint = function(path, checkpoint_id, options) { | ||
var url = this.api_url(path, 'checkpoints', checkpoint_id); | ||||
|
r18652 | var settings = { | ||
type : "POST", | ||||
|
r18658 | success: options.success || function() {}, | ||
|
r18661 | error : this.create_basic_error_handler(options.error) | ||
|
r18652 | }; | ||
$.ajax(url, settings); | ||||
}; | ||||
|
r18752 | Contents.prototype.delete_checkpoint = function(path, checkpoint_id, options) { | ||
var url = this.api_url(path, 'checkpoints', checkpoint_id); | ||||
|
r18652 | var settings = { | ||
type : "DELETE", | ||||
|
r18658 | success: options.success || function() {}, | ||
|
r18661 | error : this.create_basic_error_handler(options.error) | ||
|
r18652 | }; | ||
$.ajax(url, settings); | ||||
|
r18620 | }; | ||
|
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" | ||||
* created: created date | ||||
* last_modified: last modified dat | ||||
* @method list_notebooks | ||||
* @param {String} path The path to list notebooks in | ||||
* @param {Function} load_callback called with list of notebooks on success | ||||
|
r18658 | * @param {Function} error called with ajax results on error | ||
|
r18636 | */ | ||
|
r18653 | Contents.prototype.list_contents = function(path, options) { | ||
|
r18636 | var settings = { | ||
processData : false, | ||||
cache : false, | ||||
type : "GET", | ||||
dataType : "json", | ||||
|
r18658 | success : options.success, | ||
|
r18661 | error : this.create_basic_error_handler(options.error) | ||
|
r18636 | }; | ||
|
r18650 | $.ajax(this.api_url(path), settings); | ||
|
r18647 | }; | ||
|
r18636 | |||
|
r18643 | IPython.Contents = Contents; | ||
|
r18618 | |||
|
r18643 | return {'Contents': Contents}; | ||
|
r18661 | }); | ||