Skip to content

Commit

Permalink
[WIP] Add save as menu option
Browse files Browse the repository at this point in the history
  • Loading branch information
Madhu94 committed Feb 1, 2018
1 parent 3de5e50 commit f97e26e
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 8 deletions.
3 changes: 2 additions & 1 deletion notebook/services/contents/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,8 @@ def post(self, path=''):
ext = model.get('ext', '')
type = model.get('type', '')
if copy_from:
yield self._copy(copy_from, path)
copy_to = model.get('copy_to') or path
yield self._copy(copy_from, copy_to)
else:
yield self._new_untitled(path, type=type, ext=ext)
else:
Expand Down
6 changes: 6 additions & 0 deletions notebook/static/notebook/js/menubar.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,12 @@ define([
that.notebook.copy_notebook();
return false;
});

this.element.find('#save_as').click(function() {
that.notebook.save_notebook_as();
return false;
})

this.element.find('#download_ipynb').click(function () {
var base_url = that.notebook.base_url;
var notebook_path = utils.encode_uri_components(that.notebook.notebook_path);
Expand Down
63 changes: 60 additions & 3 deletions notebook/static/notebook/js/notebook.js
Original file line number Diff line number Diff line change
Expand Up @@ -2830,7 +2830,63 @@ define([
this._checkpoint_after_save = false;
}
};


Notebook.prototype.save_notebook_as = function(new_name) {
var that = this;
var dialog_body = $('<div/>').append(
$("<p/>").addClass("save-message")
.text(i18n.msg._('Path must be relative to the notebook root directory'))
).append(
$("<br/>")
).append(
$('<input/>').attr('type','text').attr('size','25')
.addClass('form-control').attr('placeholder', 'Enter notebook name here')
);
var d = dialog.modal({
title: 'Save As',
body: dialog_body,
keyboard_manager: this.keyboard_manager,
notebook: this,
buttons: {
Cancel: {},
Save: {
class: 'btn-primary',
click: function() {
var nb_name_or_path = d.find('input').val();
var nb_name = nb_name_or_path.split("/").slice(-1).pop();
var ext = utils.splitext(nb_name)[1];
if (ext && ext!== '.ipynb') {
d.find('.save-message').append('<br>').append(i18n.msg._(
"Notebook files should have extension .ipynb")
);
} else if (!that.test_notebook_name(nb_name)) {
d.find('.save-message').append('<br>').append(i18n.msg._(
"Notebook names must have 1 or more characters and can contain any characters except :/\\")
);
}
else if (ext === '') {
nb_name_or_path += '.ipynb';
}
that.copy_notebook(nb_name_or_path);
return false;
}
},
},
open : function () {
/**
* Upon ENTER, click the OK button.
*/
d.find('input[type="text"]').keydown(function (event) {
if (event.which === keyboard.keycodes.enter) {
d.find('.btn-primary').first().click();
return false;
}
});
d.find('input[type="text"]').focus().select();
}
});
}

/**
* Update the autosave interval based on the duration of the last save.
*
Expand Down Expand Up @@ -2911,19 +2967,20 @@ define([
* Make a copy of the current notebook.
* If the notebook has unsaved changes, it is saved first.
*/
Notebook.prototype.copy_notebook = function () {
Notebook.prototype.copy_notebook = function (new_name) {
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];
new_name = new_name || '';
var p;
if (this.dirty && this.writable) {
p = this.save_notebook(true);
} else {
p = Promise.resolve();
}
return p.then(function () {
return that.contents.copy(that.notebook_path, parent).then(
return that.contents.copy(that.notebook_path, parent, new_name).then(
function (data) {
w.location = utils.url_path_join(
base_url, 'notebooks', utils.encode_uri_components(data.path)
Expand Down
12 changes: 9 additions & 3 deletions notebook/static/services/contents.js
Original file line number Diff line number Diff line change
Expand Up @@ -186,17 +186,23 @@ define(function(requirejs) {
return utils.promising_ajax(url, settings);
};

Contents.prototype.copy = function(from_file, to_dir) {
Contents.prototype.copy = function(from_file, to_dir, name='') {
/**
* Copy a file into a given directory via POST
* The server will select the name of the copied file
* If name is not given, the server will select the name of the
* copied file
*/
var url = this.api_url(to_dir);

var body = {copy_from: from_file};

if (name) {
body.copy_to = name
}
var settings = {
processData : false,
type: "POST",
data: JSON.stringify({copy_from: from_file}),
data: JSON.stringify(body),
contentType: 'application/json',
dataType : "json",
};
Expand Down
5 changes: 4 additions & 1 deletion notebook/templates/notebook.html
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,10 @@
<li class="divider"></li>
<li id="copy_notebook"
title="{% trans %}Open a copy of this notebook's contents and start a new kernel{% endtrans %}">
<a href="#">{% trans %}Make a Copy...{% endtrans %}</a></li>
<a href="#">{% trans %}Make a Copy...{% endtrans %}</a></li>
<li id="save_as"
title="{% trans %}Open a copy of this notebook's contents and start a new kernel{% endtrans %}">
<a href="#">{% trans %}Save As{% endtrans %}</a></li>
<li id="rename_notebook"><a href="#">{% trans %}Rename...{% endtrans %}</a></li>
<li id="save_checkpoint"><a href="#">{% trans %}Save and Checkpoint{% endtrans %}</a></li>
<!-- <hr/> -->
Expand Down

0 comments on commit f97e26e

Please sign in to comment.