##// END OF EJS Templates
Standardise JS checkpointing API, use it for notebooks
Standardise JS checkpointing API, use it for notebooks

File last commit:

r18652:6b6d07fe
r18652:6b6d07fe
Show More
contents.js
250 lines | 8.2 KiB | application/javascript | JavascriptLexer
jhemmelg
Initial interface for javascript contentmanagers...
r18618 // Copyright (c) IPython Development Team.
// Distributed under the terms of the Modified BSD License.
define([
'base/js/namespace',
'jquery',
KesterTong
Style and bug fixes
r18620 'base/js/utils',
KesterTong
Adds dialog on new_notebook failure...
r18623 'base/js/dialog',
], function(IPython, $, utils, dialog) {
Jeff Hemmelgarn
Move contentmanager to contents
r18643 var Contents = function(options) {
jhemmelg
Initial interface for javascript contentmanagers...
r18618 // Constructor
//
Jeff Hemmelgarn
Move contentmanager to contents
r18643 // A contents handles passing file operations
jhemmelg
Initial interface for javascript contentmanagers...
r18618 // to the back-end. This includes checkpointing
// with the normal file operations.
//
// Parameters:
KesterTong
Use IPython style constructor
r18621 // options: dictionary
// Dictionary of keyword arguments.
KesterTong
Make ContentManager stateless...
r18622 // events: $(Events) instance
KesterTong
Use IPython style constructor
r18621 // base_url: string
KesterTong
Make ContentManager stateless...
r18622 this.events = options.events;
KesterTong
Use IPython style constructor
r18621 this.base_url = options.base_url;
KesterTong
Style and bug fixes
r18620 };
Thomas Kluyver
Add Contents.api_url convenience function
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
Make ContentManager stateless...
r18622 /**
KesterTong
Moves list_notebooks to ContentManager
r18636 * Notebook Functions
*/
/**
KesterTong
Moves load_notebook to ContentManager and adds new_notebook to Google Drive version
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
Move contentmanager to contents
r18643 Contents.prototype.load_notebook = function (path, name, success_callback,
KesterTong
Moves load_notebook to ContentManager and adds new_notebook to Google Drive version
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
Add Contents.api_url convenience function
r18650 var url = this.api_url(path, name);
KesterTong
Moves load_notebook to ContentManager and adds new_notebook to Google Drive version
r18638 $.ajax(url, settings);
};
/**
KesterTong
Make ContentManager stateless...
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
Don't do UI stuff in contents API module
r18649 Contents.prototype.new_notebook = function(path, options) {
jhemmelg
ContentManager function signatures updated
r18619 var base_url = this.base_url;
Thomas Kluyver
Don't do UI stuff in contents API module
r18649 var success_callback = options.success_callback || function(data, status, xhr) {};
var error_callback = options.error_callback || function(xhr, status, error) {};
jhemmelg
ContentManager function signatures updated
r18619 var settings = {
processData : false,
cache : false,
type : "POST",
dataType : "json",
async : false,
Thomas Kluyver
Don't do UI stuff in contents API module
r18649 success : success_callback,
KesterTong
Adds dialog on new_notebook failure...
r18623 error : function(xhr, status, error) {
utils.log_ajax_error(xhr, status, error);
Thomas Kluyver
Don't do UI stuff in contents API module
r18649 error_callback(xhr, status, error);
KesterTong
Adds dialog on new_notebook failure...
r18623 }
jhemmelg
ContentManager function signatures updated
r18619 };
Thomas Kluyver
Add Contents.api_url convenience function
r18650 $.ajax(this.api_url(path), settings);
KesterTong
Style and bug fixes
r18620 };
jhemmelg
Initial interface for javascript contentmanagers...
r18618
Jeff Hemmelgarn
Move contentmanager to contents
r18643 Contents.prototype.delete_notebook = function(name, path) {
jhemmelg
ContentManager function signatures updated
r18619 var settings = {
processData : false,
cache : false,
type : "DELETE",
KesterTong
Removes unnecessary parameter from ContentManager.delete_notebook
r18629 dataType : "json",
KesterTong
Use $.proxy instead of that
r18633 success : $.proxy(this.events.trigger, this.events,
Jeff Hemmelgarn
Move contentmanager to contents
r18643 'notebook_deleted.Contents',
KesterTong
Use $.proxy instead of that
r18633 {
KesterTong
Remove deleted notebook from notebook list...
r18630 name: name,
path: path
KesterTong
Use $.proxy instead of that
r18633 }),
KesterTong
Remove deleted notebook from notebook list...
r18630 error : utils.log_ajax_error
jhemmelg
ContentManager function signatures updated
r18619 };
Thomas Kluyver
Add Contents.api_url convenience function
r18650 var url = this.api_url(path, name);
jhemmelg
ContentManager function signatures updated
r18619 $.ajax(url, settings);
KesterTong
Style and bug fixes
r18620 };
jhemmelg
Initial interface for javascript contentmanagers...
r18618
Jeff Hemmelgarn
Move contentmanager to contents
r18643 Contents.prototype.rename_notebook = function(path, name, new_name) {
KesterTong
Use events for rename_notebook...
r18634 var that = this;
var data = {name: new_name};
jhemmelg
ContentManager function signatures updated
r18619 var settings = {
processData : false,
cache : false,
type : "PATCH",
data : JSON.stringify(data),
dataType: "json",
Jeff Hemmelgarn
Move notebook.rename to contentmanager...
r18627 contentType: 'application/json',
KesterTong
Use events for rename_notebook...
r18634 success : function (json, status, xhr) {
Jeff Hemmelgarn
Move contentmanager to contents
r18643 that.events.trigger('notebook_rename_success.Contents',
KesterTong
Use events for rename_notebook...
r18634 json);
},
error : function (xhr, status, error) {
Jeff Hemmelgarn
Move contentmanager to contents
r18643 that.events.trigger('notebook_rename_error.Contents',
KesterTong
Use events for rename_notebook...
r18634 [xhr, status, error]);
}
jhemmelg
ContentManager function signatures updated
r18619 };
Thomas Kluyver
Add Contents.api_url convenience function
r18650 var url = this.api_url(path, name);
jhemmelg
ContentManager function signatures updated
r18619 $.ajax(url, settings);
KesterTong
Style and bug fixes
r18620 };
jhemmelg
Initial interface for javascript contentmanagers...
r18618
Jeff Hemmelgarn
Move contentmanager to contents
r18643 Contents.prototype.save_notebook = function(path, name, content,
KesterTong
Uses events for ContentManager.save_notebook...
r18635 extra_settings) {
Jeff Hemmelgarn
Fix a few problems with cherry-picked commits...
r18640 var that = content;
jhemmelg
ContentManager function signatures updated
r18619 // Create a JSON model to be sent to the server.
KesterTong
Uses events for ContentManager.save_notebook...
r18635 var model = {
name : name,
path : path,
Jeff Hemmelgarn
Fix a few problems with cherry-picked commits...
r18640 type : "notebook",
KesterTong
Uses events for ContentManager.save_notebook...
r18635 content : content
};
jhemmelg
ContentManager function signatures updated
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
Move notebook.rename to contentmanager...
r18627 contentType: 'application/json',
KesterTong
Uses events for ContentManager.save_notebook...
r18635 success : $.proxy(this.events.trigger, this.events,
Jeff Hemmelgarn
Move contentmanager to contents
r18643 'notebook_save_success.Contents',
KesterTong
Uses events for ContentManager.save_notebook...
r18635 $.extend(model, { start : start })),
error : function (xhr, status, error) {
Jeff Hemmelgarn
Move contentmanager to contents
r18643 that.events.trigger('notebook_save_error.Contents',
KesterTong
Uses events for ContentManager.save_notebook...
r18635 [xhr, status, error, model]);
}
jhemmelg
ContentManager function signatures updated
r18619 };
if (extra_settings) {
for (var key in extra_settings) {
settings[key] = extra_settings[key];
}
}
Thomas Kluyver
Add Contents.api_url convenience function
r18650 var url = this.api_url(path, name);
jhemmelg
ContentManager function signatures updated
r18619 $.ajax(url, settings);
};
jhemmelg
Initial interface for javascript contentmanagers...
r18618
KesterTong
Moves list_notebooks to ContentManager
r18636 /**
* Checkpointing Functions
*/
Thomas Kluyver
Standardise JS checkpointing API, use it for notebooks
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
Style and bug fixes
r18620 };
jhemmelg
Initial interface for javascript contentmanagers...
r18618
Thomas Kluyver
Standardise JS checkpointing API, use it for notebooks
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
Style and bug fixes
r18620 };
jhemmelg
Initial interface for javascript contentmanagers...
r18618
Thomas Kluyver
Standardise JS checkpointing API, use it for notebooks
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
Style and bug fixes
r18620 };
KesterTong
Moves list_notebooks to ContentManager
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
Move contentmanager to contents
r18643 Contents.prototype.list_contents = function(path, load_callback,
KesterTong
Moves list_notebooks to ContentManager
r18636 error_callback) {
var settings = {
processData : false,
cache : false,
type : "GET",
dataType : "json",
success : load_callback,
error : error_callback
};
Thomas Kluyver
Add Contents.api_url convenience function
r18650 $.ajax(this.api_url(path), settings);
Thomas Kluyver
Add missing semicolon
r18647 };
KesterTong
Moves list_notebooks to ContentManager
r18636
Jeff Hemmelgarn
Move contentmanager to contents
r18643 IPython.Contents = Contents;
jhemmelg
Initial interface for javascript contentmanagers...
r18618
Jeff Hemmelgarn
Move contentmanager to contents
r18643 return {'Contents': Contents};
jhemmelg
Initial interface for javascript contentmanagers...
r18618 });