Permalink
Browse files

add central 'notebook name' dialog to utils

moved from rename notebook, reused in:

- new notebook (dashboard and menu bar)
- rename notebook
- copy notebook

blacklist_re and test_notebook_name moved to utils as well,
as they are only ever used in this one dialog.
  • Loading branch information...
1 parent 19782f8 commit 0bea4300a4dd5d616ef32b0ddb3676847edc2a87 @minrk committed Apr 6, 2013
@@ -67,7 +67,14 @@ var IPython = (function (IPython) {
// File
var that = this;
this.element.find('#new_notebook').click(function () {
- window.open(that.baseProjectUrl()+'new');
+ IPython.utils.notebook_name_dialog(
+ "New Notebook",
+ "Enter new notebook name:",
+ "Untitled",
+ function(new_name) {
+ window.open(that.baseProjectUrl()+'new?name=' + new_name);
+ }
+ );
});
this.element.find('#open_notebook').click(function () {
window.open(that.baseProjectUrl());
@@ -76,9 +83,16 @@ var IPython = (function (IPython) {
IPython.save_widget.rename_notebook();
});
this.element.find('#copy_notebook').click(function () {
- var notebook_id = IPython.notebook.get_notebook_id();
- var url = that.baseProjectUrl() + notebook_id + '/copy';
- window.open(url,'_blank');
+ IPython.utils.notebook_name_dialog(
+ "Copy Notebook",
+ "Enter new notebook name:",
+ IPython.notebook.notebook_name + '-Copy',
+ function(new_name) {
+ var notebook_id = IPython.notebook.get_notebook_id();
+ var url = that.baseProjectUrl()+ notebook_id + '/copy?name=' + new_name;
+ window.open(url, '_blank');
+ }
+ );
return false;
});
this.element.find('#save_notebook').click(function () {
@@ -44,7 +44,6 @@ var IPython = (function (IPython) {
this.control_key_active = false;
this.notebook_id = null;
this.notebook_name = null;
- this.notebook_name_blacklist_re = /[\/\\:]/;
this.nbformat = 3 // Increment this when changing the nbformat
this.nbformat_minor = 0 // Increment this when changing the nbformat
this.style();
@@ -1489,22 +1488,6 @@ var IPython = (function (IPython) {
};
/**
- * Check that a notebook's name is valid.
- *
- * @method test_notebook_name
- * @param {String} nbname A name for this notebook
- * @return {Boolean} True if the name is valid, false if invalid
- */
- Notebook.prototype.test_notebook_name = function (nbname) {
- nbname = nbname || '';
- if (this.notebook_name_blacklist_re.test(nbname) == false && nbname.length>0) {
- return true;
- } else {
- return false;
- };
- };
-
- /**
* Load a notebook from JSON (.ipynb).
*
* This currently handles one worksheet: others are deleted.
@@ -22,7 +22,14 @@ $(document).ready(function () {
$('#ipython-main-app').addClass('border-box-sizing ui-widget');
$('div#notebooks_toolbar').addClass('ui-widget ui-helper-clearfix');
$('#new_notebook').button().click(function (e) {
- window.open($('body').data('baseProjectUrl')+'new');
+ IPython.utils.notebook_name_dialog(
+ "New Notebook",
+ "Enter new notebook name:",
+ "Untitled",
+ function(new_name) {
+ window.open($('body').data('baseProjectUrl')+'new?name=' + new_name);
+ }
+ );
});
IPython.read_only = $('body').data('readOnly') === 'True';
@@ -59,52 +59,14 @@ var IPython = (function (IPython) {
SaveWidget.prototype.rename_notebook = function () {
var that = this;
- var dialog = $('<div/>');
- dialog.append(
- $('<h3/>').html('Enter a new notebook name:')
- .css({'margin-bottom': '10px'})
- );
- dialog.append(
- $('<input/>').attr('type','text').attr('size','25')
- .addClass('ui-widget ui-widget-content')
- .attr('value',IPython.notebook.get_notebook_name())
- );
- // $(document).append(dialog);
- dialog.dialog({
- resizable: false,
- modal: true,
- title: "Rename Notebook",
- closeText: "",
- close: function(event, ui) {$(this).dialog('destroy').remove();},
- buttons : {
- "OK": function () {
- var new_name = $(this).find('input').attr('value');
- if (!IPython.notebook.test_notebook_name(new_name)) {
- $(this).find('h3').html(
- "Invalid notebook name. Notebook names must "+
- "have 1 or more characters and can contain any characters " +
- "except :/\\. Please enter a new notebook name:"
- );
- } else {
- IPython.notebook.set_notebook_name(new_name);
- IPython.notebook.save_notebook();
- $(this).dialog('close');
- }
- },
- "Cancel": function () {
- $(this).dialog('close');
- }
- },
- open : function (event, ui) {
- var that = $(this);
- // Upon ENTER, click the OK button.
- that.find('input[type="text"]').keydown(function (event, ui) {
- if (event.which === utils.keycodes.ENTER) {
- that.parent().find('button').first().click();
- }
- });
+ IPython.utils.notebook_name_dialog(
+ "Rename Notebook",
+ "Enter a new notebook name:",
+ function (new_name) {
+ IPython.notebook.set_notebook_name(new_name);
+ IPython.notebook.save_notebook();
}
- });
+ );
}
@@ -269,6 +269,75 @@ IPython.utils = (function (IPython) {
return M;
})();
+ var notebook_name_blacklist_re= /[\/\\:]/;
+
+ /**
+ * Check that a notebook's name is valid.
+ *
+ * @function test_notebook_name
+ * @param {String} nbname A name for a notebook
+ * @return {Boolean} True if the name is valid, false if invalid
+ */
+ var test_notebook_name = function (nbname) {
+ nbname = nbname || '';
+ if (notebook_name_blacklist_re.test(nbname) == false && nbname.length>0) {
+ return true;
+ } else {
+ return false;
+ };
+ };
+
+
+ var notebook_name_dialog = function(title, prompt, initial_value, action) {
+ // a dialog for entering a notebook name
+ // used in rename, new notebook, duplicate
+ var dialog = $('<div/>');
+ dialog.append(
+ $('<h3/>').html(prompt)
+ .css({'margin-bottom': '10px'})
+ );
+ dialog.append(
+ $('<input/>').attr('type','text').attr('size','25')
+ .addClass('ui-widget ui-widget-content')
+ .attr('value', initial_value)
+ );
+ dialog.dialog({
+ resizable: false,
+ modal: true,
+ title: title,
+ closeText: "",
+ close: function(event, ui) {$(this).dialog('destroy').remove();},
+ buttons : {
+ "OK": function () {
+ var new_name = $(this).find('input').attr('value');
+ if (!test_notebook_name(new_name)) {
+ $(this).find('h3').html(
+ "Invalid notebook name. Notebook names must "+
+ "have 1 or more characters and can contain any characters " +
+ "except :/\\. Please enter a new notebook name:"
+ );
+ } else {
+ action(new_name);
+ $(this).dialog('close');
+ }
+ },
+ "Cancel": function () {
+ $(this).dialog('close');
+ }
+ },
+ open : function (event, ui) {
+ var that = $(this);
+ // Upon ENTER, click the OK button.
+ that.find('input[type="text"]').keydown(function (event, ui) {
+ if (event.which === keycodes.ENTER) {
+ that.parent().find('button').first().click();
+ }
+ });
+ }
+ });
+
+ }
+
return {
regex_split : regex_split,
@@ -279,7 +348,9 @@ IPython.utils = (function (IPython) {
fixCarriageReturn : fixCarriageReturn,
autoLinkUrls : autoLinkUrls,
points_to_pixels : points_to_pixels,
- browser : browser
+ browser : browser,
+ test_notebook_name : test_notebook_name,
+ notebook_name_dialog : notebook_name_dialog
};
}(IPython));
@@ -87,4 +87,5 @@
<script src="{{static_url("js/notebooklist.js") }}" type="text/javascript" charset="utf-8"></script>
<script src="{{static_url("js/clusterlist.js") }}" type="text/javascript" charset="utf-8"></script>
<script src="{{static_url("js/projectdashboardmain.js") }}" type="text/javascript" charset="utf-8"></script>
+ <script src="{{ static_url("js/utils.js") }}" type="text/javascript" charset="utf-8"></script>
{% endblock %}

0 comments on commit 0bea430

Please sign in to comment.