Skip to content

Commit 21b7710

Browse files
committed
Add "Save as" menu option
1 parent e321c80 commit 21b7710

File tree

3 files changed

+94
-1
lines changed

3 files changed

+94
-1
lines changed

notebook/static/notebook/js/menubar.js

+5
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,11 @@ define([
170170
that.notebook.copy_notebook();
171171
return false;
172172
});
173+
this.element.find('#save_notebook_as').click(function() {
174+
that.notebook.save_notebook_as();
175+
return false;
176+
177+
});
173178
this.element.find('#download_ipynb').click(function () {
174179
var base_url = that.notebook.base_url;
175180
var notebook_path = utils.encode_uri_components(that.notebook.notebook_path);

notebook/static/notebook/js/notebook.js

+86-1
Original file line numberDiff line numberDiff line change
@@ -2755,7 +2755,7 @@ define([
27552755
// In some cases the filesystem reports an inconsistent time,
27562756
// so we allow 0.5 seconds difference before complaining.
27572757
// This is configurable in nbconfig/notebook.json as `last_modified_check_margin`.
2758-
if ((last_modified.getTime() - that.last_modified.getTime()) > last_modified_check_margin) {
2758+
if ((last_modified.getTime() - that.last_modified.getTime()) > last_modified_check_margin) {
27592759
console.warn("Last saving was done on `"+that.last_modified+"`("+that._last_modified+"), "+
27602760
"while the current file seem to have been saved on `"+data.last_modified+"`");
27612761
if (that._changed_on_disk_dialog !== null) {
@@ -2845,6 +2845,91 @@ define([
28452845
}
28462846
};
28472847

2848+
2849+
Notebook.prototype.save_notebook_as = function() {
2850+
// If current notebook has some changes, save them
2851+
if (this.writable && this.dirty) {
2852+
this.save_notebook();
2853+
}
2854+
var that = this;
2855+
var dialog_body = $('<div/>').append(
2856+
$("<p/>").addClass("save-message")
2857+
.text(i18n.msg._('Enter a notebook path relative to notebook dir'))
2858+
).append(
2859+
$("<br/>")
2860+
).append(
2861+
$('<input/>').attr('type','text').attr('size','25')
2862+
.addClass('form-control').attr('placeholder', that.notebook_name)
2863+
);
2864+
2865+
var d = dialog.modal({
2866+
title: 'Save As',
2867+
body: dialog_body,
2868+
keyboard_manager: this.keyboard_manager,
2869+
notebook: this,
2870+
buttons: {
2871+
Cancel: {},
2872+
Save: {
2873+
class: 'btn-primary',
2874+
click: function() {
2875+
var nb_path = d.find('input').val();
2876+
var nb_name = nb_path.split('/').slice(-1).pop();
2877+
// If notebook name does not contain extension '.ipynb' add it
2878+
var ext = utils.splitext(nb_name);
2879+
if (ext === '') {
2880+
nb_name = nb_name + '.ipynb';
2881+
}
2882+
var model = {
2883+
'type': 'notebook',
2884+
'content': that.toJSON(),
2885+
'name': nb_name
2886+
};
2887+
2888+
var save_thunk = function() {
2889+
return that.contents.save(nb_path, model)
2890+
.then(function(data) {
2891+
window.open(data.path, '_self');
2892+
},function(error) {
2893+
console.error(i18n.msg._(error.message || 'Unknown error saving notebook'));
2894+
});
2895+
}
2896+
that.contents.get(nb_path, {type: 'notebook'}).then(function(data) {
2897+
var warning_body = $('<div/>').append(
2898+
$("<p/>").text(i18n.msg._('Notebook with that name exists.')));
2899+
dialog.modal({
2900+
title: 'Save As',
2901+
body: warning_body,
2902+
buttons: {Cancel: {},
2903+
Overwrite: {
2904+
class: 'btn-warning',
2905+
click: function() {
2906+
return save_thunk();
2907+
}
2908+
}
2909+
}
2910+
});
2911+
}, function(err) {
2912+
return save_thunk();
2913+
})
2914+
return false;
2915+
}
2916+
},
2917+
},
2918+
open : function () {
2919+
/**
2920+
* Upon ENTER, click the OK button.
2921+
*/
2922+
d.find('input[type="text"]').keydown(function (event) {
2923+
if (event.which === keyboard.keycodes.enter) {
2924+
d.find('.btn-primary').first().click();
2925+
return false;
2926+
}
2927+
});
2928+
d.find('input[type="text"]').focus().select();
2929+
}
2930+
});
2931+
}
2932+
28482933
/**
28492934
* Update the autosave interval based on the duration of the last save.
28502935
*

notebook/templates/notebook.html

+3
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,9 @@
8989
<li id="copy_notebook"
9090
title="{% trans %}Open a copy of this notebook's contents and start a new kernel{% endtrans %}">
9191
<a href="#">{% trans %}Make a Copy...{% endtrans %}</a></li>
92+
<li id="save_notebook_as"
93+
title="{% trans %}Save a copy of the notebook's contents and start a new kernel{% endtrans %}">
94+
<a href="#">{% trans %}Save as{% endtrans %}</a></li>
9295
<li id="rename_notebook"><a href="#">{% trans %}Rename...{% endtrans %}</a></li>
9396
<li id="save_checkpoint"><a href="#">{% trans %}Save and Checkpoint{% endtrans %}</a></li>
9497
<!-- <hr/> -->

0 commit comments

Comments
 (0)