contents.js
215 lines
| 7.0 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', | ||
|
r18623 | 'base/js/dialog', | ||
], function(IPython, $, utils, dialog) { | ||||
|
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. | ||||
|
r18622 | // events: $(Events) instance | ||
|
r18621 | // base_url: string | ||
|
r18622 | this.events = options.events; | ||
|
r18621 | this.base_url = options.base_url; | ||
|
r18620 | }; | ||
|
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 | /** | ||
|
r18653 | * File Functions (including notebook operations) | ||
|
r18636 | */ | ||
/** | ||||
|
r18653 | * Load a file. | ||
|
r18638 | * | ||
|
r18653 | * Calls success_callback with file JSON model, or error_callback with error. | ||
|
r18638 | * | ||
* @method load_notebook | ||||
* @param {String} path | ||||
* @param {String} name | ||||
* @param {Function} success_callback | ||||
* @param {Function} error_callback | ||||
*/ | ||||
|
r18653 | Contents.prototype.load_file = function (path, name, 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", | ||||
|
r18653 | success : options.success_callback, | ||
error : options.error_callback || function() {} | ||||
|
r18638 | }; | ||
this.events.trigger('notebook_loading.Notebook'); | ||||
|
r18650 | var url = this.api_url(path, name); | ||
|
r18638 | $.ajax(url, settings); | ||
}; | ||||
/** | ||||
|
r18653 | * Creates a new notebook file at the specified directory path. | ||
|
r18622 | * | ||
* @method scroll_to_cell | ||||
* @param {String} path The path to create the new notebook at | ||||
*/ | ||||
|
r18649 | Contents.prototype.new_notebook = function(path, options) { | ||
|
r18653 | var error_callback = options.error_callback || function() {}; | ||
|
r18619 | var settings = { | ||
processData : false, | ||||
cache : false, | ||||
type : "POST", | ||||
dataType : "json", | ||||
|
r18653 | success : options.success_callback || function() {}, | ||
error : options.error_callback || function() {} | ||||
|
r18619 | }; | ||
|
r18650 | $.ajax(this.api_url(path), settings); | ||
|
r18620 | }; | ||
|
r18618 | |||
|
r18653 | Contents.prototype.delete_file = function(name, path, options) { | ||
var error_callback = options.error_callback || function() {}; | ||||
var that = this; | ||||
|
r18619 | var settings = { | ||
processData : false, | ||||
cache : false, | ||||
type : "DELETE", | ||||
|
r18629 | dataType : "json", | ||
|
r18653 | success : options.success_callback || function() {}, | ||
error : function(xhr, status, error) { | ||||
utils.log_ajax_error(xhr, status, error); | ||||
error_callback(xhr, status, error); | ||||
} | ||||
|
r18619 | }; | ||
|
r18650 | var url = this.api_url(path, name); | ||
|
r18619 | $.ajax(url, settings); | ||
|
r18620 | }; | ||
|
r18618 | |||
|
r18653 | Contents.prototype.rename_file = function(path, name, new_path, new_name, options) { | ||
var data = {name: new_name, path: new_path}; | ||||
|
r18619 | var settings = { | ||
processData : false, | ||||
cache : false, | ||||
type : "PATCH", | ||||
data : JSON.stringify(data), | ||||
dataType: "json", | ||||
|
r18627 | contentType: 'application/json', | ||
|
r18653 | success : options.success_callback || function() {}, | ||
error : options.error_callback || function() {} | ||||
|
r18619 | }; | ||
|
r18650 | var url = this.api_url(path, name); | ||
|
r18619 | $.ajax(url, settings); | ||
|
r18620 | }; | ||
|
r18618 | |||
|
r18653 | Contents.prototype.save_file = function(path, name, model, options) { | ||
|
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), | ||||
|
r18627 | contentType: 'application/json', | ||
|
r18653 | success : options.success_callback || function() {}, | ||
error : options.error_callback || function() {} | ||||
|
r18619 | }; | ||
|
r18653 | if (options.extra_settings) { | ||
$.extend(settings, options.extra_settings); | ||||
|
r18619 | } | ||
|
r18650 | var url = this.api_url(path, name); | ||
|
r18619 | $.ajax(url, settings); | ||
}; | ||||
|
r18618 | |||
|
r18636 | /** | ||
* Checkpointing Functions | ||||
*/ | ||||
|
r18652 | Contents.prototype.create_checkpoint = function(path, name, options) { | ||
var url = this.api_url(path, name, 'checkpoints'); | ||||
var settings = { | ||||
type : "POST", | ||||
|
r18653 | success: options.success_callback || function() {}, | ||
error: options.error_callback || function() {} | ||||
|
r18652 | }; | ||
$.ajax(url, settings); | ||||
|
r18620 | }; | ||
|
r18618 | |||
|
r18652 | Contents.prototype.list_checkpoints = function(path, name, options) { | ||
var url = this.api_url(path, name, 'checkpoints'); | ||||
var settings = { | ||||
type : "GET", | ||||
|
r18653 | success: options.success_callback, | ||
error: options.error_callback || function() {} | ||||
|
r18652 | }; | ||
$.ajax(url, settings); | ||||
|
r18620 | }; | ||
|
r18618 | |||
|
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", | ||||
|
r18653 | success: options.success_callback || function() {}, | ||
error: options.error_callback || function() {} | ||||
|
r18652 | }; | ||
$.ajax(url, settings); | ||||
}; | ||||
Contents.prototype.delete_checkpoint = function(path, name, checkpoint_id, options) { | ||||
var url = this.api_url(path, name, 'checkpoints', checkpoint_id); | ||||
var settings = { | ||||
type : "DELETE", | ||||
|
r18653 | success: options.success_callback || function() {}, | ||
error: options.error_callback || function() {} | ||||
|
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" | ||||
* 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 | ||||
* @param {Function} error_callback called with ajax results on error | ||||
*/ | ||||
|
r18653 | Contents.prototype.list_contents = function(path, options) { | ||
|
r18636 | var settings = { | ||
processData : false, | ||||
cache : false, | ||||
type : "GET", | ||||
dataType : "json", | ||||
|
r18653 | success : options.success_callback, | ||
error : options.error_callback || function() {} | ||||
|
r18636 | }; | ||
|
r18650 | $.ajax(this.api_url(path), settings); | ||
|
r18647 | }; | ||
|
r18636 | |||
|
r18643 | IPython.Contents = Contents; | ||
|
r18618 | |||
|
r18643 | return {'Contents': Contents}; | ||
|
r18618 | }); | ||