From 01ccf42b876cb03181ee9b4f00bc4088d2aa7f5f 2014-11-03 18:18:28 From: Thomas Kluyver Date: 2014-11-03 18:18:28 Subject: [PATCH] Make contents JS API consistent --- diff --git a/IPython/html/static/notebook/js/notebook.js b/IPython/html/static/notebook/js/notebook.js index 6671c0d..dfd01fa 100644 --- a/IPython/html/static/notebook/js/notebook.js +++ b/IPython/html/static/notebook/js/notebook.js @@ -1929,10 +1929,27 @@ define([ nbformat : this.nbformat, nbformat_minor : this.nbformat_minor }) - this.contents.save_notebook(this.notebook_path, - this.notebook_name, - content, - extra_settings); + // Create a JSON model to be sent to the server. + var model = { + name : this.notebook_name, + path : this.notebook_path, + type : "notebook", + content : content + }; + // time the ajax call for autosave tuning purposes. + var start = new Date().getTime(); + + var that = this; + this.contents.save_file(this.notebook_path, this.notebook_name, model, { + extra_settings: extra_settings, + success_callback: $.proxy(this.events.trigger, this.events, + 'notebook_save_success.Contents', + $.extend(model, { start : start })), + error_callback: function (xhr, status, error) { + that.events.trigger('notebook_save_error.Contents', + [xhr, status, error, model]); + } + }); }; /** @@ -2087,18 +2104,26 @@ define([ $.ajax(url,settings); }; - Notebook.prototype.rename = function (nbname) { - if (!nbname.match(/\.ipynb$/)) { - nbname = nbname + ".ipynb"; + Notebook.prototype.rename = function (new_name) { + if (!new_name.match(/\.ipynb$/)) { + new_name = new_name + ".ipynb"; } - this.contents.rename_notebook(this.notebook_path, - this.notebook_name, nbname); + var that = this; + this.contents.rename_file(this.notebook_path, this.notebook_name, + this.notebook_path, new_name, { + success_callback: function (json, status, xhr) { + that.events.trigger('notebook_rename_success.Contents', json); + }, + error_callback: function (xhr, status, error) { + that.events.trigger('notebook_rename_error.Contents', + [xhr, status, error]); + } + }); }; Notebook.prototype.delete = function () { - this.contents.delete_notebook(this.notebook_name, - this.notebook_path); + this.contents.delete_file(this.notebook_name, this.notebook_path); }; Notebook.prototype.rename_error = function (xhr, status, error) { @@ -2142,11 +2167,10 @@ define([ Notebook.prototype.load_notebook = function (notebook_name, notebook_path) { this.notebook_name = notebook_name; this.notebook_path = notebook_path; - this.contents.load_notebook( - notebook_path, - notebook_name, - $.proxy(this.load_notebook_success,this), - $.proxy(this.load_notebook_error,this)); + this.contents.load_file(notebook_path, notebook_name, { + success_callback: $.proxy(this.load_notebook_success, this), + error_callback: $.proxy(this.load_notebook_error, this) + }); }; /** diff --git a/IPython/html/static/services/contents.js b/IPython/html/static/services/contents.js index 85f3e93..60bf5c3 100644 --- a/IPython/html/static/services/contents.js +++ b/IPython/html/static/services/contents.js @@ -30,14 +30,13 @@ define([ }; /** - * Notebook Functions + * File Functions (including notebook operations) */ /** - * Load a notebook. + * Load a file. * - * Calls success_callback with notebook JSON object (as string), or - * error_callback with error. + * Calls success_callback with file JSON model, or error_callback with error. * * @method load_notebook * @param {String} path @@ -45,16 +44,15 @@ define([ * @param {Function} success_callback * @param {Function} error_callback */ - Contents.prototype.load_notebook = function (path, name, success_callback, - error_callback) { + Contents.prototype.load_file = function (path, name, options) { // 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, + success : options.success_callback, + error : options.error_callback || function() {} }; this.events.trigger('notebook_loading.Notebook'); var url = this.api_url(path, name); @@ -63,52 +61,44 @@ define([ /** - * Creates a new notebook file at the specified path, and - * opens that notebook in a new window. + * Creates a new notebook file at the specified directory path. * * @method scroll_to_cell * @param {String} path The path to create the new notebook at */ Contents.prototype.new_notebook = function(path, options) { - var base_url = this.base_url; - var success_callback = options.success_callback || function(data, status, xhr) {}; - var error_callback = options.error_callback || function(xhr, status, error) {}; + var error_callback = options.error_callback || function() {}; var settings = { processData : false, cache : false, type : "POST", dataType : "json", - async : false, - success : success_callback, - error : function(xhr, status, error) { - utils.log_ajax_error(xhr, status, error); - error_callback(xhr, status, error); - } + success : options.success_callback || function() {}, + error : options.error_callback || function() {} }; $.ajax(this.api_url(path), settings); }; - Contents.prototype.delete_notebook = function(name, path) { + Contents.prototype.delete_file = function(name, path, options) { + var error_callback = options.error_callback || function() {}; + var that = this; var settings = { processData : false, cache : false, type : "DELETE", dataType : "json", - success : $.proxy(this.events.trigger, this.events, - 'notebook_deleted.Contents', - { - name: name, - path: path - }), - error : utils.log_ajax_error + success : options.success_callback || function() {}, + error : function(xhr, status, error) { + utils.log_ajax_error(xhr, status, error); + error_callback(xhr, status, error); + } }; var url = this.api_url(path, name); $.ajax(url, settings); }; - Contents.prototype.rename_notebook = function(path, name, new_name) { - var that = this; - var data = {name: new_name}; + Contents.prototype.rename_file = function(path, name, new_path, new_name, options) { + var data = {name: new_name, path: new_path}; var settings = { processData : false, cache : false, @@ -116,31 +106,14 @@ define([ data : JSON.stringify(data), dataType: "json", contentType: 'application/json', - success : function (json, status, xhr) { - that.events.trigger('notebook_rename_success.Contents', - json); - }, - error : function (xhr, status, error) { - that.events.trigger('notebook_rename_error.Contents', - [xhr, status, error]); - } + success : options.success_callback || function() {}, + error : options.error_callback || function() {} }; var url = this.api_url(path, name); $.ajax(url, settings); }; - Contents.prototype.save_notebook = function(path, name, content, - extra_settings) { - var that = content; - // Create a JSON model to be sent to the server. - var model = { - name : name, - path : path, - type : "notebook", - content : content - }; - // time the ajax call for autosave tuning purposes. - var start = new Date().getTime(); + Contents.prototype.save_file = function(path, name, model, options) { // We do the call with settings so we can set cache to false. var settings = { processData : false, @@ -148,18 +121,11 @@ define([ type : "PUT", data : JSON.stringify(model), contentType: 'application/json', - success : $.proxy(this.events.trigger, this.events, - 'notebook_save_success.Contents', - $.extend(model, { start : start })), - error : function (xhr, status, error) { - that.events.trigger('notebook_save_error.Contents', - [xhr, status, error, model]); - } + success : options.success_callback || function() {}, + error : options.error_callback || function() {} }; - if (extra_settings) { - for (var key in extra_settings) { - settings[key] = extra_settings[key]; - } + if (options.extra_settings) { + $.extend(settings, options.extra_settings); } var url = this.api_url(path, name); $.ajax(url, settings); @@ -173,8 +139,8 @@ define([ 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) {} + success: options.success_callback || function() {}, + error: options.error_callback || function() {} }; $.ajax(url, settings); }; @@ -183,8 +149,8 @@ define([ 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) {} + success: options.success_callback, + error: options.error_callback || function() {} }; $.ajax(url, settings); }; @@ -193,8 +159,8 @@ define([ 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) {} + success: options.success_callback || function() {}, + error: options.error_callback || function() {} }; $.ajax(url, settings); }; @@ -203,8 +169,8 @@ define([ 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) {} + success: options.success_callback || function() {}, + error: options.error_callback || function() {} }; $.ajax(url, settings); }; @@ -229,15 +195,14 @@ define([ * @param {Function} load_callback called with list of notebooks on success * @param {Function} error_callback called with ajax results on error */ - Contents.prototype.list_contents = function(path, load_callback, - error_callback) { + Contents.prototype.list_contents = function(path, options) { var settings = { processData : false, cache : false, type : "GET", dataType : "json", - success : load_callback, - error : error_callback + success : options.success_callback, + error : options.error_callback || function() {} }; $.ajax(this.api_url(path), settings); diff --git a/IPython/html/static/tree/js/notebooklist.js b/IPython/html/static/tree/js/notebooklist.js index a37aac7..cc7e88c 100644 --- a/IPython/html/static/tree/js/notebooklist.js +++ b/IPython/html/static/tree/js/notebooklist.js @@ -157,14 +157,13 @@ define([ NotebookList.prototype.load_list = function () { var that = this - this.contents.list_contents( - that.notebook_path, - $.proxy(that.draw_notebook_list, that), - $.proxy( function(xhr, status, error) { + this.contents.list_contents(that.notebook_path, { + success_callback: $.proxy(this.draw_notebook_list, this), + error_callback: function(xhr, status, error) { utils.log_ajax_error(xhr, status, error); that.draw_notebook_list([], "Error connecting to server."); - }, that) - ); + } + }); }; /** @@ -347,7 +346,12 @@ define([ Delete : { class: "btn-danger", click: function() { - notebooklist.contents.delete_notebook(nbname, path); + notebooklist.contents.delete_file(nbname, path, { + success_callback: function() { + that.events.trigger('notebook_deleted.Contents', + {name: name, path: path}); + } + }); } }, Cancel : {}