From 22761eeb15f314044a7572e6664b55ab02e176da Mon Sep 17 00:00:00 2001 From: Min RK Date: Fri, 22 Jan 2016 11:04:34 +0100 Subject: [PATCH 1/2] save before copy if notebook is dirty in copy_notebook, rather than relying on async: false in event handlers, which is ignored. --- notebook/static/notebook/js/actions.js | 3 --- notebook/static/notebook/js/menubar.js | 3 --- notebook/static/notebook/js/notebook.js | 31 ++++++++++++++++--------- 3 files changed, 20 insertions(+), 17 deletions(-) diff --git a/notebook/static/notebook/js/actions.js b/notebook/static/notebook/js/actions.js index be9a2e40c..8296a0662 100644 --- a/notebook/static/notebook/js/actions.js +++ b/notebook/static/notebook/js/actions.js @@ -545,9 +545,6 @@ define(function(require){ 'duplicate-notebook':{ help: "Create an open a copy of current notebook", handler : function (env, event) { - if (env.notebook.dirty) { - env.notebook.save_notebook({async : false}); - } env.notebook.copy_notebook(); } }, diff --git a/notebook/static/notebook/js/menubar.js b/notebook/static/notebook/js/menubar.js index ae4a432af..61d8e4524 100644 --- a/notebook/static/notebook/js/menubar.js +++ b/notebook/static/notebook/js/menubar.js @@ -112,9 +112,6 @@ define([ ), IPython._target); }); this.element.find('#copy_notebook').click(function () { - if (that.notebook.dirty) { - that.notebook.save_notebook({async : false}); - } that.notebook.copy_notebook(); return false; }); diff --git a/notebook/static/notebook/js/notebook.js b/notebook/static/notebook/js/notebook.js index a367d9558..58b34f92d 100644 --- a/notebook/static/notebook/js/notebook.js +++ b/notebook/static/notebook/js/notebook.js @@ -2578,23 +2578,32 @@ define(function (require) { /** * Make a copy of the current notebook. + * If the notebook has unsaved changes, it is saved first. */ Notebook.prototype.copy_notebook = function () { var that = this; var base_url = this.base_url; var w = window.open('', IPython._target); var parent = utils.url_path_split(this.notebook_path)[0]; - this.contents.copy(this.notebook_path, parent).then( - function (data) { - w.location = utils.url_path_join( - base_url, 'notebooks', utils.encode_uri_components(data.path) - ); - }, - function(error) { - w.close(); - that.events.trigger('notebook_copy_failed', error); - } - ); + var p; + if (this.dirty) { + p = this.save_notebook(); + } else { + p = Promise.resolve(); + } + p.then(function () { + that.contents.copy(that.notebook_path, parent).then( + function (data) { + w.location = utils.url_path_join( + base_url, 'notebooks', utils.encode_uri_components(data.path) + ); + }, + function(error) { + w.close(); + that.events.trigger('notebook_copy_failed', error); + } + ); + }) }; /** From 55ce5083c893b88363f986c834f7e575def96e63 Mon Sep 17 00:00:00 2001 From: Min RK Date: Mon, 25 Jan 2016 10:45:03 +0100 Subject: [PATCH 2/2] return promises --- notebook/static/notebook/js/notebook.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/notebook/static/notebook/js/notebook.js b/notebook/static/notebook/js/notebook.js index 58b34f92d..ce77f4a36 100644 --- a/notebook/static/notebook/js/notebook.js +++ b/notebook/static/notebook/js/notebook.js @@ -2591,8 +2591,8 @@ define(function (require) { } else { p = Promise.resolve(); } - p.then(function () { - that.contents.copy(that.notebook_path, parent).then( + return p.then(function () { + return that.contents.copy(that.notebook_path, parent).then( function (data) { w.location = utils.url_path_join( base_url, 'notebooks', utils.encode_uri_components(data.path) @@ -2603,7 +2603,7 @@ define(function (require) { that.events.trigger('notebook_copy_failed', error); } ); - }) + }); }; /**