Skip to content

Commit f365612

Browse files
committed
Backport PR #987: save before copy if notebook is dirty
in copy_notebook, rather than relying on async: false in event handlers, which is ignored. closes #986
1 parent eb31a3c commit f365612

File tree

3 files changed

+20
-17
lines changed

3 files changed

+20
-17
lines changed

notebook/static/notebook/js/actions.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -545,9 +545,6 @@ define(function(require){
545545
'duplicate-notebook':{
546546
help: "Create an open a copy of current notebook",
547547
handler : function (env, event) {
548-
if (env.notebook.dirty) {
549-
env.notebook.save_notebook({async : false});
550-
}
551548
env.notebook.copy_notebook();
552549
}
553550
},

notebook/static/notebook/js/menubar.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -112,9 +112,6 @@ define([
112112
), IPython._target);
113113
});
114114
this.element.find('#copy_notebook').click(function () {
115-
if (that.notebook.dirty) {
116-
that.notebook.save_notebook({async : false});
117-
}
118115
that.notebook.copy_notebook();
119116
return false;
120117
});

notebook/static/notebook/js/notebook.js

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2578,23 +2578,32 @@ define(function (require) {
25782578

25792579
/**
25802580
* Make a copy of the current notebook.
2581+
* If the notebook has unsaved changes, it is saved first.
25812582
*/
25822583
Notebook.prototype.copy_notebook = function () {
25832584
var that = this;
25842585
var base_url = this.base_url;
25852586
var w = window.open('', IPython._target);
25862587
var parent = utils.url_path_split(this.notebook_path)[0];
2587-
this.contents.copy(this.notebook_path, parent).then(
2588-
function (data) {
2589-
w.location = utils.url_path_join(
2590-
base_url, 'notebooks', utils.encode_uri_components(data.path)
2591-
);
2592-
},
2593-
function(error) {
2594-
w.close();
2595-
that.events.trigger('notebook_copy_failed', error);
2596-
}
2597-
);
2588+
var p;
2589+
if (this.dirty) {
2590+
p = this.save_notebook();
2591+
} else {
2592+
p = Promise.resolve();
2593+
}
2594+
return p.then(function () {
2595+
return that.contents.copy(that.notebook_path, parent).then(
2596+
function (data) {
2597+
w.location = utils.url_path_join(
2598+
base_url, 'notebooks', utils.encode_uri_components(data.path)
2599+
);
2600+
},
2601+
function(error) {
2602+
w.close();
2603+
that.events.trigger('notebook_copy_failed', error);
2604+
}
2605+
);
2606+
});
25982607
};
25992608

26002609
/**

0 commit comments

Comments
 (0)