contents.js
252 lines
| 7.6 KiB
| application/javascript
|
JavascriptLexer
jhemmelg
|
r18618 | // Copyright (c) IPython Development Team. | ||
// Distributed under the terms of the Modified BSD License. | ||||
define([ | ||||
'base/js/namespace', | ||||
'jquery', | ||||
KesterTong
|
r18620 | 'base/js/utils', | ||
Thomas Kluyver
|
r18717 | ], function(IPython, $, utils) { | ||
Scott Sanderson
|
r19349 | "use strict"; | ||
Jeff Hemmelgarn
|
r18643 | var Contents = function(options) { | ||
Jonathan Frederic
|
r19176 | /** | ||
* Constructor | ||||
* | ||||
* A contents handles passing file operations | ||||
* to the back-end. This includes checkpointing | ||||
* with the normal file operations. | ||||
* | ||||
* Parameters: | ||||
* options: dictionary | ||||
* Dictionary of keyword arguments. | ||||
* base_url: string | ||||
*/ | ||||
KesterTong
|
r18621 | this.base_url = options.base_url; | ||
KesterTong
|
r18620 | }; | ||
Thomas Kluyver
|
r18650 | |||
Kester Tong
|
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.'; | ||||
Min RK
|
r18752 | }; | ||
Contents.DirectoryNotEmptyError.prototype = Object.create(Error.prototype); | ||||
Kester Tong
|
r18661 | Contents.DirectoryNotEmptyError.prototype.name = | ||
Kester Tong
|
r18662 | Contents.DIRECTORY_NOT_EMPTY_ERROR; | ||
Kester Tong
|
r18661 | |||
Thomas Kluyver
|
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
|
r18622 | /** | ||
Kester Tong
|
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) { | ||||
Min RK
|
r18752 | return utils.log_ajax_error; | ||
Kester Tong
|
r18661 | } | ||
return function(xhr, status, error) { | ||||
callback(utils.wrap_ajax_error(xhr, status, error)); | ||||
}; | ||||
Min RK
|
r18752 | }; | ||
Kester Tong
|
r18661 | |||
/** | ||||
Thomas Kluyver
|
r18653 | * File Functions (including notebook operations) | ||
KesterTong
|
r18636 | */ | ||
/** | ||||
Min RK
|
r18752 | * Get a file. | ||
KesterTong
|
r18638 | * | ||
Thomas Kluyver
|
r18658 | * Calls success with file JSON model, or error with error. | ||
KesterTong
|
r18638 | * | ||
Min RK
|
r18752 | * @method get | ||
KesterTong
|
r18638 | * @param {String} path | ||
Thomas Kluyver
|
r18840 | * @param {Object} options | ||
* type : 'notebook', 'file', or 'directory' | ||||
* format: 'text' or 'base64'; only relevant for type: 'file' | ||||
KesterTong
|
r18638 | */ | ||
Min RK
|
r18752 | Contents.prototype.get = function (path, options) { | ||
Jonathan Frederic
|
r19176 | /** | ||
* We do the call with settings so we can set cache to false. | ||||
*/ | ||||
KesterTong
|
r18638 | var settings = { | ||
processData : false, | ||||
cache : false, | ||||
type : "GET", | ||||
dataType : "json", | ||||
}; | ||||
Min RK
|
r18752 | var url = this.api_url(path); | ||
Scott Sanderson
|
r19349 | var params = {}; | ||
Thomas Kluyver
|
r18790 | if (options.type) { params.type = options.type; } | ||
if (options.format) { params.format = options.format; } | ||||
Thomas Kluyver
|
r18827 | return utils.promising_ajax(url + '?' + $.param(params), settings); | ||
KesterTong
|
r18638 | }; | ||
/** | ||||
Min RK
|
r18760 | * Creates a new untitled file or directory in the specified directory path. | ||
KesterTong
|
r18622 | * | ||
Min RK
|
r18752 | * @method new | ||
Min RK
|
r18760 | * @param {String} path: the directory in which to create the new file/directory | ||
Thomas Kluyver
|
r18676 | * @param {Object} options: | ||
Min RK
|
r18752 | * ext: file extension to use | ||
Min RK
|
r18758 | * type: model type to create ('notebook', 'file', or 'directory') | ||
KesterTong
|
r18622 | */ | ||
Min RK
|
r18760 | Contents.prototype.new_untitled = function(path, options) { | ||
Min RK
|
r18758 | var data = JSON.stringify({ | ||
ext: options.ext, | ||||
type: options.type | ||||
}); | ||||
Thomas Kluyver
|
r18676 | |||
jhemmelg
|
r18619 | var settings = { | ||
processData : false, | ||||
Min RK
|
r18752 | type : "POST", | ||
Thomas Kluyver
|
r18676 | data: data, | ||
jhemmelg
|
r18619 | dataType : "json", | ||
}; | ||||
Thomas Kluyver
|
r18829 | return utils.promising_ajax(this.api_url(path), settings); | ||
KesterTong
|
r18620 | }; | ||
jhemmelg
|
r18618 | |||
Thomas Kluyver
|
r18829 | Contents.prototype.delete = function(path) { | ||
jhemmelg
|
r18619 | var settings = { | ||
processData : false, | ||||
type : "DELETE", | ||||
KesterTong
|
r18629 | dataType : "json", | ||
Thomas Kluyver
|
r18829 | }; | ||
var url = this.api_url(path); | ||||
return utils.promising_ajax(url, settings).catch( | ||||
// Translate certain errors to more specific ones. | ||||
function(error) { | ||||
Kester Tong
|
r18661 | // TODO: update IPEP27 to specify errors more precisely, so | ||
// that error types can be detected here with certainty. | ||||
Thomas Kluyver
|
r18829 | if (error.xhr.status === 400) { | ||
Thomas Kluyver
|
r18837 | throw new Contents.DirectoryNotEmptyError(); | ||
Kester Tong
|
r18661 | } | ||
Thomas Kluyver
|
r18837 | throw error; | ||
Thomas Kluyver
|
r18653 | } | ||
Thomas Kluyver
|
r18829 | ); | ||
KesterTong
|
r18620 | }; | ||
jhemmelg
|
r18618 | |||
Thomas Kluyver
|
r18829 | Contents.prototype.rename = function(path, new_path) { | ||
Min RK
|
r18752 | var data = {path: new_path}; | ||
jhemmelg
|
r18619 | var settings = { | ||
processData : false, | ||||
type : "PATCH", | ||||
data : JSON.stringify(data), | ||||
dataType: "json", | ||||
Jeff Hemmelgarn
|
r18627 | contentType: 'application/json', | ||
jhemmelg
|
r18619 | }; | ||
Min RK
|
r18752 | var url = this.api_url(path); | ||
Thomas Kluyver
|
r18829 | return utils.promising_ajax(url, settings); | ||
KesterTong
|
r18620 | }; | ||
jhemmelg
|
r18618 | |||
Thomas Kluyver
|
r18829 | Contents.prototype.save = function(path, model) { | ||
Jonathan Frederic
|
r19176 | /** | ||
* We do the call with settings so we can set cache to false. | ||||
*/ | ||||
jhemmelg
|
r18619 | var settings = { | ||
processData : false, | ||||
type : "PUT", | ||||
Min RK
|
r19314 | dataType: "json", | ||
jhemmelg
|
r18619 | data : JSON.stringify(model), | ||
Jeff Hemmelgarn
|
r18627 | contentType: 'application/json', | ||
jhemmelg
|
r18619 | }; | ||
Min RK
|
r18752 | var url = this.api_url(path); | ||
Thomas Kluyver
|
r18829 | return utils.promising_ajax(url, settings); | ||
jhemmelg
|
r18619 | }; | ||
Thomas Kluyver
|
r18665 | |||
Thomas Kluyver
|
r18829 | Contents.prototype.copy = function(from_file, to_dir) { | ||
Jonathan Frederic
|
r19176 | /** | ||
* Copy a file into a given directory via POST | ||||
* The server will select the name of the copied file | ||||
*/ | ||||
Min RK
|
r18752 | var url = this.api_url(to_dir); | ||
Thomas Kluyver
|
r18665 | |||
var settings = { | ||||
processData : false, | ||||
Min RK
|
r18752 | type: "POST", | ||
data: JSON.stringify({copy_from: from_file}), | ||||
Thomas Kluyver
|
r18665 | dataType : "json", | ||
}; | ||||
Thomas Kluyver
|
r18829 | return utils.promising_ajax(url, settings); | ||
Thomas Kluyver
|
r18665 | }; | ||
jhemmelg
|
r18618 | |||
KesterTong
|
r18636 | /** | ||
* Checkpointing Functions | ||||
*/ | ||||
Thomas Kluyver
|
r18829 | Contents.prototype.create_checkpoint = function(path) { | ||
Min RK
|
r18752 | var url = this.api_url(path, 'checkpoints'); | ||
Thomas Kluyver
|
r18652 | var settings = { | ||
type : "POST", | ||||
Thomas Kluyver
|
r18830 | dataType : "json", | ||
Thomas Kluyver
|
r18652 | }; | ||
Thomas Kluyver
|
r18829 | return utils.promising_ajax(url, settings); | ||
KesterTong
|
r18620 | }; | ||
jhemmelg
|
r18618 | |||
Thomas Kluyver
|
r18829 | Contents.prototype.list_checkpoints = function(path) { | ||
Min RK
|
r18752 | var url = this.api_url(path, 'checkpoints'); | ||
Thomas Kluyver
|
r18652 | var settings = { | ||
type : "GET", | ||||
Thomas Kluyver
|
r18830 | cache: false, | ||
dataType: "json", | ||||
Thomas Kluyver
|
r18652 | }; | ||
Thomas Kluyver
|
r18829 | return utils.promising_ajax(url, settings); | ||
KesterTong
|
r18620 | }; | ||
jhemmelg
|
r18618 | |||
Thomas Kluyver
|
r18829 | Contents.prototype.restore_checkpoint = function(path, checkpoint_id) { | ||
Min RK
|
r18752 | var url = this.api_url(path, 'checkpoints', checkpoint_id); | ||
Thomas Kluyver
|
r18652 | var settings = { | ||
type : "POST", | ||||
}; | ||||
Thomas Kluyver
|
r18829 | return utils.promising_ajax(url, settings); | ||
Thomas Kluyver
|
r18652 | }; | ||
Thomas Kluyver
|
r18829 | Contents.prototype.delete_checkpoint = function(path, checkpoint_id) { | ||
Min RK
|
r18752 | var url = this.api_url(path, 'checkpoints', checkpoint_id); | ||
Thomas Kluyver
|
r18652 | var settings = { | ||
type : "DELETE", | ||||
}; | ||||
Thomas Kluyver
|
r18829 | return utils.promising_ajax(url, settings); | ||
KesterTong
|
r18620 | }; | ||
KesterTong
|
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 | ||||
*/ | ||||
Thomas Kluyver
|
r18827 | Contents.prototype.list_contents = function(path) { | ||
return this.get(path, {type: 'directory'}); | ||||
Thomas Kluyver
|
r18647 | }; | ||
KesterTong
|
r18636 | |||
Jeff Hemmelgarn
|
r18643 | IPython.Contents = Contents; | ||
jhemmelg
|
r18618 | |||
Jeff Hemmelgarn
|
r18643 | return {'Contents': Contents}; | ||
Kester Tong
|
r18661 | }); | ||