contents.js
250 lines
| 8.2 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. | ||||
KesterTong
|
r18622 | // events: $(Events) instance | ||
KesterTong
|
r18621 | // base_url: string | ||
KesterTong
|
r18622 | this.events = options.events; | ||
KesterTong
|
r18621 | this.base_url = options.base_url; | ||
KesterTong
|
r18620 | }; | ||
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 | /** | ||
KesterTong
|
r18636 | * Notebook Functions | ||
*/ | ||||
/** | ||||
KesterTong
|
r18638 | * Load a notebook. | ||
* | ||||
* Calls success_callback with notebook JSON object (as string), or | ||||
* error_callback with error. | ||||
* | ||||
* @method load_notebook | ||||
* @param {String} path | ||||
* @param {String} name | ||||
* @param {Function} success_callback | ||||
* @param {Function} error_callback | ||||
*/ | ||||
Jeff Hemmelgarn
|
r18643 | Contents.prototype.load_notebook = function (path, name, success_callback, | ||
KesterTong
|
r18638 | error_callback) { | ||
// We do the call with settings so we can set cache to false. | ||||
var settings = { | ||||
processData : false, | ||||
cache : false, | ||||
type : "GET", | ||||
dataType : "json", | ||||
success : success_callback, | ||||
error : error_callback, | ||||
}; | ||||
this.events.trigger('notebook_loading.Notebook'); | ||||
Thomas Kluyver
|
r18650 | var url = this.api_url(path, name); | ||
KesterTong
|
r18638 | $.ajax(url, settings); | ||
}; | ||||
/** | ||||
KesterTong
|
r18622 | * Creates a new notebook file at the specified path, and | ||
* opens that notebook in a new window. | ||||
* | ||||
* @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) { | ||
jhemmelg
|
r18619 | var base_url = this.base_url; | ||
Thomas Kluyver
|
r18649 | var success_callback = options.success_callback || function(data, status, xhr) {}; | ||
var error_callback = options.error_callback || function(xhr, status, error) {}; | ||||
jhemmelg
|
r18619 | var settings = { | ||
processData : false, | ||||
cache : false, | ||||
type : "POST", | ||||
dataType : "json", | ||||
async : false, | ||||
Thomas Kluyver
|
r18649 | success : success_callback, | ||
KesterTong
|
r18623 | error : function(xhr, status, error) { | ||
utils.log_ajax_error(xhr, status, error); | ||||
Thomas Kluyver
|
r18649 | error_callback(xhr, status, error); | ||
KesterTong
|
r18623 | } | ||
jhemmelg
|
r18619 | }; | ||
Thomas Kluyver
|
r18650 | $.ajax(this.api_url(path), settings); | ||
KesterTong
|
r18620 | }; | ||
jhemmelg
|
r18618 | |||
Jeff Hemmelgarn
|
r18643 | Contents.prototype.delete_notebook = function(name, path) { | ||
jhemmelg
|
r18619 | var settings = { | ||
processData : false, | ||||
cache : false, | ||||
type : "DELETE", | ||||
KesterTong
|
r18629 | dataType : "json", | ||
KesterTong
|
r18633 | success : $.proxy(this.events.trigger, this.events, | ||
Jeff Hemmelgarn
|
r18643 | 'notebook_deleted.Contents', | ||
KesterTong
|
r18633 | { | ||
KesterTong
|
r18630 | name: name, | ||
path: path | ||||
KesterTong
|
r18633 | }), | ||
KesterTong
|
r18630 | error : utils.log_ajax_error | ||
jhemmelg
|
r18619 | }; | ||
Thomas Kluyver
|
r18650 | var url = this.api_url(path, name); | ||
jhemmelg
|
r18619 | $.ajax(url, settings); | ||
KesterTong
|
r18620 | }; | ||
jhemmelg
|
r18618 | |||
Jeff Hemmelgarn
|
r18643 | Contents.prototype.rename_notebook = function(path, name, new_name) { | ||
KesterTong
|
r18634 | var that = this; | ||
var data = {name: new_name}; | ||||
jhemmelg
|
r18619 | var settings = { | ||
processData : false, | ||||
cache : false, | ||||
type : "PATCH", | ||||
data : JSON.stringify(data), | ||||
dataType: "json", | ||||
Jeff Hemmelgarn
|
r18627 | contentType: 'application/json', | ||
KesterTong
|
r18634 | success : function (json, status, xhr) { | ||
Jeff Hemmelgarn
|
r18643 | that.events.trigger('notebook_rename_success.Contents', | ||
KesterTong
|
r18634 | json); | ||
}, | ||||
error : function (xhr, status, error) { | ||||
Jeff Hemmelgarn
|
r18643 | that.events.trigger('notebook_rename_error.Contents', | ||
KesterTong
|
r18634 | [xhr, status, error]); | ||
} | ||||
jhemmelg
|
r18619 | }; | ||
Thomas Kluyver
|
r18650 | var url = this.api_url(path, name); | ||
jhemmelg
|
r18619 | $.ajax(url, settings); | ||
KesterTong
|
r18620 | }; | ||
jhemmelg
|
r18618 | |||
Jeff Hemmelgarn
|
r18643 | Contents.prototype.save_notebook = function(path, name, content, | ||
KesterTong
|
r18635 | extra_settings) { | ||
Jeff Hemmelgarn
|
r18640 | var that = content; | ||
jhemmelg
|
r18619 | // Create a JSON model to be sent to the server. | ||
KesterTong
|
r18635 | var model = { | ||
name : name, | ||||
path : path, | ||||
Jeff Hemmelgarn
|
r18640 | type : "notebook", | ||
KesterTong
|
r18635 | content : content | ||
}; | ||||
jhemmelg
|
r18619 | // time the ajax call for autosave tuning purposes. | ||
var start = new Date().getTime(); | ||||
// 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
|
r18627 | contentType: 'application/json', | ||
KesterTong
|
r18635 | success : $.proxy(this.events.trigger, this.events, | ||
Jeff Hemmelgarn
|
r18643 | 'notebook_save_success.Contents', | ||
KesterTong
|
r18635 | $.extend(model, { start : start })), | ||
error : function (xhr, status, error) { | ||||
Jeff Hemmelgarn
|
r18643 | that.events.trigger('notebook_save_error.Contents', | ||
KesterTong
|
r18635 | [xhr, status, error, model]); | ||
} | ||||
jhemmelg
|
r18619 | }; | ||
if (extra_settings) { | ||||
for (var key in extra_settings) { | ||||
settings[key] = extra_settings[key]; | ||||
} | ||||
} | ||||
Thomas Kluyver
|
r18650 | var url = this.api_url(path, name); | ||
jhemmelg
|
r18619 | $.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", | ||||
success: options.success_callback || function(data, status, xhr) {}, | ||||
error: options.error_callback || function(xhr, status, error_msg) {} | ||||
}; | ||||
$.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", | ||||
success: options.success_callback || function(data, status, xhr) {}, | ||||
error: options.error_callback || function(xhr, status, error_msg) {} | ||||
}; | ||||
$.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", | ||||
success: options.success_callback || function(data, status, xhr) {}, | ||||
error: options.error_callback || function(xhr, status, error_msg) {} | ||||
}; | ||||
$.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", | ||||
success: options.success_callback || function(data, status, xhr) {}, | ||||
error: options.error_callback || function(xhr, status, error_msg) {} | ||||
}; | ||||
$.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 | ||||
* @param {Function} error_callback called with ajax results on error | ||||
*/ | ||||
Jeff Hemmelgarn
|
r18643 | Contents.prototype.list_contents = function(path, load_callback, | ||
KesterTong
|
r18636 | error_callback) { | ||
var settings = { | ||||
processData : false, | ||||
cache : false, | ||||
type : "GET", | ||||
dataType : "json", | ||||
success : load_callback, | ||||
error : error_callback | ||||
}; | ||||
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}; | ||
jhemmelg
|
r18618 | }); | ||