contentmanager.js
290 lines
| 8.8 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) { | ||||
KesterTong
|
r18621 | var ContentManager = function(options) { | ||
jhemmelg
|
r18618 | // Constructor | ||
// | ||||
// A contentmanager handles passing file operations | ||||
// 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 | ||
jhemmelg
|
r18618 | this.version = 0.1; | ||
KesterTong
|
r18622 | this.events = options.events; | ||
KesterTong
|
r18621 | this.base_url = options.base_url; | ||
KesterTong
|
r18620 | }; | ||
KesterTong
|
r18621 | |||
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 | ||||
*/ | ||||
ContentManager.prototype.load_notebook = function (path, name, success_callback, | ||||
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'); | ||||
var url = utils.url_join_encode( | ||||
this.base_url, | ||||
Jeff Hemmelgarn
|
r18640 | 'api/contents', | ||
KesterTong
|
r18638 | path, | ||
name | ||||
); | ||||
$.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 | ||||
*/ | ||||
ContentManager.prototype.new_notebook = function(path) { | ||||
jhemmelg
|
r18619 | var base_url = this.base_url; | ||
var settings = { | ||||
processData : false, | ||||
cache : false, | ||||
type : "POST", | ||||
dataType : "json", | ||||
async : false, | ||||
success : function (data, status, xhr){ | ||||
var notebook_name = data.name; | ||||
window.open( | ||||
utils.url_join_encode( | ||||
base_url, | ||||
Jeff Hemmelgarn
|
r18640 | 'contents', | ||
jhemmelg
|
r18619 | path, | ||
notebook_name | ||||
), | ||||
'_blank' | ||||
); | ||||
}, | ||||
KesterTong
|
r18623 | error : function(xhr, status, error) { | ||
utils.log_ajax_error(xhr, status, error); | ||||
var msg; | ||||
if (xhr.responseJSON && xhr.responseJSON.message) { | ||||
msg = xhr.responseJSON.message; | ||||
} else { | ||||
msg = xhr.statusText; | ||||
} | ||||
dialog.modal({ | ||||
title : 'Creating Notebook Failed', | ||||
body : "The error was: " + msg, | ||||
buttons : {'OK' : {'class' : 'btn-primary'}} | ||||
}); | ||||
} | ||||
jhemmelg
|
r18619 | }; | ||
var url = utils.url_join_encode( | ||||
base_url, | ||||
Jeff Hemmelgarn
|
r18640 | 'api/contents', | ||
jhemmelg
|
r18619 | path | ||
); | ||||
$.ajax(url,settings); | ||||
KesterTong
|
r18620 | }; | ||
jhemmelg
|
r18618 | |||
KesterTong
|
r18629 | ContentManager.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, | ||
'notebook_deleted.ContentManager', | ||||
{ | ||||
KesterTong
|
r18630 | name: name, | ||
path: path | ||||
KesterTong
|
r18633 | }), | ||
KesterTong
|
r18630 | error : utils.log_ajax_error | ||
jhemmelg
|
r18619 | }; | ||
var url = utils.url_join_encode( | ||||
KesterTong
|
r18629 | this.base_url, | ||
Jeff Hemmelgarn
|
r18640 | 'api/contents', | ||
Jeff Hemmelgarn
|
r18625 | path, | ||
name | ||||
jhemmelg
|
r18619 | ); | ||
$.ajax(url, settings); | ||||
KesterTong
|
r18620 | }; | ||
jhemmelg
|
r18618 | |||
KesterTong
|
r18634 | ContentManager.prototype.rename_notebook = function(path, name, new_name) { | ||
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) { | ||
that.events.trigger('notebook_rename_success.ContentManager', | ||||
json); | ||||
}, | ||||
error : function (xhr, status, error) { | ||||
that.events.trigger('notebook_rename_error.ContentManager', | ||||
[xhr, status, error]); | ||||
} | ||||
jhemmelg
|
r18619 | }; | ||
var url = utils.url_join_encode( | ||||
KesterTong
|
r18634 | this.base_url, | ||
Jeff Hemmelgarn
|
r18640 | 'api/contents', | ||
KesterTong
|
r18634 | path, | ||
name | ||||
jhemmelg
|
r18619 | ); | ||
$.ajax(url, settings); | ||||
KesterTong
|
r18620 | }; | ||
jhemmelg
|
r18618 | |||
KesterTong
|
r18635 | ContentManager.prototype.save_notebook = function(path, name, content, | ||
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, | ||
'notebook_save_success.ContentManager', | ||||
$.extend(model, { start : start })), | ||||
error : function (xhr, status, error) { | ||||
that.events.trigger('notebook_save_error.ContentManager', | ||||
[xhr, status, error, model]); | ||||
} | ||||
jhemmelg
|
r18619 | }; | ||
if (extra_settings) { | ||||
for (var key in extra_settings) { | ||||
settings[key] = extra_settings[key]; | ||||
} | ||||
} | ||||
var url = utils.url_join_encode( | ||||
KesterTong
|
r18635 | this.base_url, | ||
Jeff Hemmelgarn
|
r18640 | 'api/contents', | ||
KesterTong
|
r18635 | path, | ||
name | ||||
jhemmelg
|
r18619 | ); | ||
$.ajax(url, settings); | ||||
}; | ||||
jhemmelg
|
r18618 | |||
KesterTong
|
r18636 | /** | ||
* Checkpointing Functions | ||||
*/ | ||||
jhemmelg
|
r18618 | ContentManager.prototype.save_checkpoint = function() { | ||
jhemmelg
|
r18619 | // This is not necessary - integrated into save | ||
KesterTong
|
r18620 | }; | ||
jhemmelg
|
r18618 | |||
jhemmelg
|
r18619 | ContentManager.prototype.restore_checkpoint = function(notebook, id) { | ||
that = notebook; | ||||
this.events.trigger('notebook_restoring.Notebook', checkpoint); | ||||
var url = utils.url_join_encode( | ||||
this.base_url, | ||||
Jeff Hemmelgarn
|
r18640 | 'api/contents', | ||
jhemmelg
|
r18619 | this.notebook_path, | ||
this.notebook_name, | ||||
'checkpoints', | ||||
checkpoint | ||||
); | ||||
$.post(url).done( | ||||
$.proxy(that.restore_checkpoint_success, that) | ||||
).fail( | ||||
$.proxy(that.restore_checkpoint_error, that) | ||||
); | ||||
KesterTong
|
r18620 | }; | ||
jhemmelg
|
r18618 | |||
jhemmelg
|
r18619 | ContentManager.prototype.list_checkpoints = function(notebook) { | ||
that = notebook; | ||||
var url = utils.url_join_encode( | ||||
that.base_url, | ||||
Jeff Hemmelgarn
|
r18640 | 'api/contents', | ||
jhemmelg
|
r18619 | that.notebook_path, | ||
that.notebook_name, | ||||
'checkpoints' | ||||
); | ||||
$.get(url).done( | ||||
$.proxy(that.list_checkpoints_success, that) | ||||
).fail( | ||||
$.proxy(that.list_checkpoints_error, that) | ||||
); | ||||
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 | ||||
*/ | ||||
ContentManager.prototype.list_contents = function(path, load_callback, | ||||
error_callback) { | ||||
var that = this; | ||||
var settings = { | ||||
processData : false, | ||||
cache : false, | ||||
type : "GET", | ||||
dataType : "json", | ||||
success : load_callback, | ||||
error : error_callback | ||||
}; | ||||
Jeff Hemmelgarn
|
r18640 | var url = utils.url_join_encode(this.base_url, 'api', 'contents', | ||
KesterTong
|
r18636 | path); | ||
$.ajax(url, settings); | ||||
} | ||||
KesterTong
|
r18620 | IPython.ContentManager = ContentManager; | ||
jhemmelg
|
r18618 | |||
KesterTong
|
r18620 | return {'ContentManager': ContentManager}; | ||
jhemmelg
|
r18618 | }); | ||