contents.js
272 lines
| 8.9 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', | ||
KesterTong
|
r18623 | 'base/js/dialog', | ||
], function(IPython, $, utils, dialog) { | ||||
Jeff Hemmelgarn
|
r18643 | var Contents = function(options) { | ||
jhemmelg
|
r18618 | // Constructor | ||
// | ||||
Jeff Hemmelgarn
|
r18643 | // A contents handles passing file operations | ||
jhemmelg
|
r18618 | // to the back-end. This includes checkpointing | ||
// with the normal file operations. | ||||
// | ||||
// Parameters: | ||||
KesterTong
|
r18621 | // options: dictionary | ||
// Dictionary of keyword arguments. | ||||
// base_url: string | ||||
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.'; | ||||
} | ||||
Contents.DirectoryNotEmptyError.prototype = new Error; | ||||
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) { | ||||
return function(xhr, status, error) { }; | ||||
} | ||||
return function(xhr, status, error) { | ||||
callback(utils.wrap_ajax_error(xhr, status, error)); | ||||
}; | ||||
} | ||||
/** | ||||
Thomas Kluyver
|
r18653 | * File Functions (including notebook operations) | ||
KesterTong
|
r18636 | */ | ||
/** | ||||
Thomas Kluyver
|
r18653 | * Load a file. | ||
KesterTong
|
r18638 | * | ||
Thomas Kluyver
|
r18658 | * Calls success with file JSON model, or error with error. | ||
KesterTong
|
r18638 | * | ||
* @method load_notebook | ||||
* @param {String} path | ||||
* @param {String} name | ||||
Thomas Kluyver
|
r18658 | * @param {Function} success | ||
* @param {Function} error | ||||
KesterTong
|
r18638 | */ | ||
Thomas Kluyver
|
r18653 | Contents.prototype.load_file = function (path, name, options) { | ||
KesterTong
|
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
|
r18658 | success : options.success, | ||
Kester Tong
|
r18661 | error : this.create_basic_error_handler(options.error) | ||
KesterTong
|
r18638 | }; | ||
Thomas Kluyver
|
r18650 | var url = this.api_url(path, name); | ||
KesterTong
|
r18638 | $.ajax(url, settings); | ||
}; | ||||
/** | ||||
Thomas Kluyver
|
r18653 | * Creates a new notebook file at the specified directory path. | ||
KesterTong
|
r18622 | * | ||
* @method scroll_to_cell | ||||
* @param {String} path The path to create the new notebook at | ||||
*/ | ||||
Thomas Kluyver
|
r18649 | Contents.prototype.new_notebook = function(path, options) { | ||
Thomas Kluyver
|
r18658 | var error = options.error || function() {}; | ||
jhemmelg
|
r18619 | var settings = { | ||
processData : false, | ||||
type : "POST", | ||||
dataType : "json", | ||||
Thomas Kluyver
|
r18658 | success : options.success || function() {}, | ||
Kester Tong
|
r18661 | error : this.create_basic_error_handler(options.error) | ||
jhemmelg
|
r18619 | }; | ||
Thomas Kluyver
|
r18650 | $.ajax(this.api_url(path), settings); | ||
KesterTong
|
r18620 | }; | ||
jhemmelg
|
r18618 | |||
Thomas Kluyver
|
r18653 | Contents.prototype.delete_file = function(name, path, options) { | ||
Thomas Kluyver
|
r18663 | var error_callback = options.error || function() {}; | ||
Thomas Kluyver
|
r18653 | var that = this; | ||
jhemmelg
|
r18619 | var settings = { | ||
processData : false, | ||||
type : "DELETE", | ||||
KesterTong
|
r18629 | dataType : "json", | ||
Thomas Kluyver
|
r18658 | success : options.success || function() {}, | ||
Thomas Kluyver
|
r18653 | error : function(xhr, status, error) { | ||
Kester Tong
|
r18661 | // TODO: update IPEP27 to specify errors more precisely, so | ||
// that error types can be detected here with certainty. | ||||
if (xhr.status === 400) { | ||||
Thomas Kluyver
|
r18663 | error_callback(new Contents.DirectoryNotEmptyError()); | ||
Kester Tong
|
r18661 | } | ||
Thomas Kluyver
|
r18663 | error_callback(utils.wrap_ajax_error(xhr, status, error)); | ||
Thomas Kluyver
|
r18653 | } | ||
jhemmelg
|
r18619 | }; | ||
Thomas Kluyver
|
r18650 | var url = this.api_url(path, name); | ||
jhemmelg
|
r18619 | $.ajax(url, settings); | ||
KesterTong
|
r18620 | }; | ||
jhemmelg
|
r18618 | |||
Thomas Kluyver
|
r18653 | Contents.prototype.rename_file = function(path, name, new_path, new_name, options) { | ||
var data = {name: new_name, path: new_path}; | ||||
jhemmelg
|
r18619 | var settings = { | ||
processData : false, | ||||
cache : false, | ||||
type : "PATCH", | ||||
data : JSON.stringify(data), | ||||
dataType: "json", | ||||
Jeff Hemmelgarn
|
r18627 | contentType: 'application/json', | ||
Kester Tong
|
r18661 | success : options.success || function() {}, | ||
error : this.create_basic_error_handler(options.error) | ||||
jhemmelg
|
r18619 | }; | ||
Thomas Kluyver
|
r18650 | var url = this.api_url(path, name); | ||
jhemmelg
|
r18619 | $.ajax(url, settings); | ||
KesterTong
|
r18620 | }; | ||
jhemmelg
|
r18618 | |||
Thomas Kluyver
|
r18653 | Contents.prototype.save_file = function(path, name, model, options) { | ||
jhemmelg
|
r18619 | // We do the call with settings so we can set cache to false. | ||
var settings = { | ||||
processData : false, | ||||
type : "PUT", | ||||
data : JSON.stringify(model), | ||||
Jeff Hemmelgarn
|
r18627 | contentType: 'application/json', | ||
Thomas Kluyver
|
r18658 | success : options.success || function() {}, | ||
Kester Tong
|
r18661 | error : this.create_basic_error_handler(options.error) | ||
jhemmelg
|
r18619 | }; | ||
Thomas Kluyver
|
r18653 | if (options.extra_settings) { | ||
$.extend(settings, options.extra_settings); | ||||
jhemmelg
|
r18619 | } | ||
Thomas Kluyver
|
r18650 | var url = this.api_url(path, name); | ||
jhemmelg
|
r18619 | $.ajax(url, settings); | ||
}; | ||||
Thomas Kluyver
|
r18665 | |||
Contents.prototype.copy_file = function(to_path, to_name, from, options) { | ||||
var url, method; | ||||
if (to_name) { | ||||
url = this.api_url(to_path, to_name); | ||||
method = "PUT"; | ||||
} else { | ||||
url = this.api_url(to_path); | ||||
method = "POST"; | ||||
} | ||||
var settings = { | ||||
processData : false, | ||||
type: method, | ||||
data: JSON.stringify({copy_from: from}), | ||||
dataType : "json", | ||||
success: options.success || function() {}, | ||||
error: this.create_basic_error_handler(options.error) | ||||
}; | ||||
if (options.extra_settings) { | ||||
$.extend(settings, options.extra_settings); | ||||
} | ||||
$.ajax(url, settings); | ||||
}; | ||||
jhemmelg
|
r18618 | |||
KesterTong
|
r18636 | /** | ||
* Checkpointing Functions | ||||
*/ | ||||
Thomas Kluyver
|
r18652 | Contents.prototype.create_checkpoint = function(path, name, options) { | ||
var url = this.api_url(path, name, 'checkpoints'); | ||||
var settings = { | ||||
type : "POST", | ||||
Thomas Kluyver
|
r18658 | success: options.success || function() {}, | ||
Kester Tong
|
r18661 | error : this.create_basic_error_handler(options.error) | ||
Thomas Kluyver
|
r18652 | }; | ||
$.ajax(url, settings); | ||||
KesterTong
|
r18620 | }; | ||
jhemmelg
|
r18618 | |||
Thomas Kluyver
|
r18652 | Contents.prototype.list_checkpoints = function(path, name, options) { | ||
var url = this.api_url(path, name, 'checkpoints'); | ||||
var settings = { | ||||
type : "GET", | ||||
Thomas Kluyver
|
r18658 | success: options.success, | ||
Kester Tong
|
r18661 | error : this.create_basic_error_handler(options.error) | ||
Thomas Kluyver
|
r18652 | }; | ||
$.ajax(url, settings); | ||||
KesterTong
|
r18620 | }; | ||
jhemmelg
|
r18618 | |||
Thomas Kluyver
|
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
|
r18658 | success: options.success || function() {}, | ||
Kester Tong
|
r18661 | error : this.create_basic_error_handler(options.error) | ||
Thomas Kluyver
|
r18652 | }; | ||
$.ajax(url, settings); | ||||
}; | ||||
Kester Tong
|
r18661 | Contents.prototype.delete_checkpoint = function(path, name, checkpoint_id, options) { | ||
Thomas Kluyver
|
r18652 | var url = this.api_url(path, name, 'checkpoints', checkpoint_id); | ||
var settings = { | ||||
type : "DELETE", | ||||
Thomas Kluyver
|
r18658 | success: options.success || function() {}, | ||
Kester Tong
|
r18661 | error : this.create_basic_error_handler(options.error) | ||
Thomas Kluyver
|
r18652 | }; | ||
$.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" | ||||
* 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
|
r18658 | * @param {Function} error called with ajax results on error | ||
KesterTong
|
r18636 | */ | ||
Thomas Kluyver
|
r18653 | Contents.prototype.list_contents = function(path, options) { | ||
KesterTong
|
r18636 | var settings = { | ||
processData : false, | ||||
cache : false, | ||||
type : "GET", | ||||
dataType : "json", | ||||
Thomas Kluyver
|
r18658 | success : options.success, | ||
Kester Tong
|
r18661 | error : this.create_basic_error_handler(options.error) | ||
KesterTong
|
r18636 | }; | ||
Thomas Kluyver
|
r18650 | $.ajax(this.api_url(path), settings); | ||
Thomas Kluyver
|
r18647 | }; | ||
KesterTong
|
r18636 | |||
Jeff Hemmelgarn
|
r18643 | IPython.Contents = Contents; | ||
jhemmelg
|
r18618 | |||
Jeff Hemmelgarn
|
r18643 | return {'Contents': Contents}; | ||
Kester Tong
|
r18661 | }); | ||