savewidget.js
179 lines
| 5.4 KiB
| application/javascript
|
JavascriptLexer
Brian E. Granger
|
r4609 | //---------------------------------------------------------------------------- | ||
// Copyright (C) 2008-2011 The IPython Development Team | ||||
// | ||||
// Distributed under the terms of the BSD License. The full license is in | ||||
// the file COPYING, distributed as part of this software. | ||||
//---------------------------------------------------------------------------- | ||||
Brian E. Granger
|
r4369 | |||
//============================================================================ | ||||
Brian E. Granger
|
r4609 | // SaveWidget | ||
Brian E. Granger
|
r4369 | //============================================================================ | ||
var IPython = (function (IPython) { | ||||
var utils = IPython.utils; | ||||
var SaveWidget = function (selector) { | ||||
Brian E. Granger
|
r4372 | this.selector = selector; | ||
Stefan van der Walt
|
r5479 | this.notebook_name_blacklist_re = /[\/\\]/; | ||
Brian E. Granger
|
r4632 | this.last_saved_name = ''; | ||
Brian E. Granger
|
r4372 | if (this.selector !== undefined) { | ||
this.element = $(selector); | ||||
this.style(); | ||||
Brian E. Granger
|
r4369 | this.bind_events(); | ||
} | ||||
}; | ||||
Brian E. Granger
|
r4372 | SaveWidget.prototype.style = function () { | ||
Brian Granger
|
r5859 | this.element.find('span#save_widget').addClass('ui-widget'); | ||
this.element.find('span#notebook_name').addClass('ui-widget ui-widget-content'); | ||||
Brian Granger
|
r5874 | this.element.find('span#save_status').addClass('ui-widget ui-widget-content') | ||
.css({border: 'none', 'margin-left': '20px'}); | ||||
Brian E. Granger
|
r4372 | }; | ||
Brian E. Granger
|
r4369 | SaveWidget.prototype.bind_events = function () { | ||
var that = this; | ||||
Brian Granger
|
r5859 | this.element.find('span#notebook_name').click(function () { | ||
that.rename_notebook(); | ||||
Brian E. Granger
|
r4372 | }); | ||
Brian Granger
|
r5859 | this.element.find('span#notebook_name').hover(function () { | ||
$(this).addClass("ui-state-hover"); | ||||
}, function () { | ||||
$(this).removeClass("ui-state-hover"); | ||||
Brian E. Granger
|
r4630 | }); | ||
Brian E. Granger
|
r4369 | }; | ||
Brian E. Granger
|
r4645 | SaveWidget.prototype.save_notebook = function () { | ||
IPython.notebook.save_notebook(); | ||||
Felix Werner
|
r5006 | }; | ||
Brian Granger
|
r5859 | 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( | ||||
Brian Granger
|
r5871 | $('<input/>').attr('type','text').attr('size','25') | ||
Brian Granger
|
r5869 | .addClass('ui-widget ui-widget-content') | ||
.attr('value',that.get_notebook_name()) | ||||
Brian Granger
|
r5859 | ); | ||
Brian Granger
|
r5872 | // $(document).append(dialog); | ||
Brian Granger
|
r5859 | dialog.dialog({ | ||
resizable: false, | ||||
modal: true, | ||||
title: "Rename Notebook", | ||||
closeText: "", | ||||
Brian Granger
|
r5873 | close: function(event, ui) {$(this).dialog('destroy').remove();}, | ||
Brian Granger
|
r5859 | buttons : { | ||
"OK": function () { | ||||
var new_name = $(this).find('input').attr('value'); | ||||
if (!that.test_notebook_name(new_name)) { | ||||
$(this).find('h3').html( | ||||
Brian Granger
|
r5955 | "Invalid notebook name. Notebook names must "+ | ||
"have 1 or more characters and can contain any characters " + | ||||
Brian Granger
|
r5859 | "except / and \\. Please enter a new notebook name:" | ||
); | ||||
} else { | ||||
that.set_notebook_name(new_name); | ||||
that.save_notebook(); | ||||
$(this).dialog('close'); | ||||
} | ||||
}, | ||||
"Cancel": function () { | ||||
$(this).dialog('close'); | ||||
} | ||||
} | ||||
}); | ||||
} | ||||
Felix Werner
|
r5006 | SaveWidget.prototype.notebook_saved = function () { | ||
Brian E. Granger
|
r4645 | this.set_document_title(); | ||
this.last_saved_name = this.get_notebook_name(); | ||||
}; | ||||
Brian E. Granger
|
r4372 | SaveWidget.prototype.get_notebook_name = function () { | ||
Brian Granger
|
r5859 | return this.element.find('span#notebook_name').html(); | ||
Stefan van der Walt
|
r5479 | }; | ||
Brian E. Granger
|
r4372 | |||
Brian E. Granger
|
r4484 | SaveWidget.prototype.set_notebook_name = function (nbname) { | ||
Brian Granger
|
r5859 | this.element.find('span#notebook_name').html(nbname); | ||
Brian E. Granger
|
r4549 | this.set_document_title(); | ||
Brian E. Granger
|
r4632 | this.last_saved_name = nbname; | ||
Stefan van der Walt
|
r5479 | }; | ||
Brian E. Granger
|
r4372 | |||
Brian E. Granger
|
r4369 | |||
Brian E. Granger
|
r4549 | SaveWidget.prototype.set_document_title = function () { | ||
nbname = this.get_notebook_name(); | ||||
Brian E. Granger
|
r5104 | document.title = nbname; | ||
Brian E. Granger
|
r4549 | }; | ||
Brian E. Granger
|
r4484 | SaveWidget.prototype.get_notebook_id = function () { | ||
Brian E. Granger
|
r5100 | return $('body').data('notebookId'); | ||
Brian E. Granger
|
r4484 | }; | ||
SaveWidget.prototype.update_url = function () { | ||||
var notebook_id = this.get_notebook_id(); | ||||
if (notebook_id !== '') { | ||||
Andrew Straw
|
r6003 | var new_url = $('body').data('baseProjectUrl') + notebook_id; | ||
Brian Granger
|
r5861 | window.history.replaceState({}, '', new_url); | ||
Brian E. Granger
|
r4484 | }; | ||
}; | ||||
Brian Granger
|
r5859 | SaveWidget.prototype.test_notebook_name = function (nbname) { | ||
Brian Granger
|
r5955 | nbname = nbname || ''; | ||
if (this.notebook_name_blacklist_re.test(nbname) == false && nbname.length>0) { | ||||
Brian E. Granger
|
r4484 | return true; | ||
} else { | ||||
return false; | ||||
}; | ||||
}; | ||||
Brian Granger
|
r5874 | SaveWidget.prototype.set_last_saved = function () { | ||
var d = new Date(); | ||||
$('#save_status').html('Last saved: '+d.format('mmm dd h:MM TT')); | ||||
}; | ||||
Felix Werner
|
r5007 | SaveWidget.prototype.reset_status = function () { | ||
Brian Granger
|
r5874 | this.element.find('span#save_status').html(''); | ||
Felix Werner
|
r5007 | }; | ||
Brian Granger
|
r5874 | SaveWidget.prototype.status_last_saved = function () { | ||
this.set_last_saved(); | ||||
Stefan van der Walt
|
r4592 | }; | ||
Brian E. Granger
|
r4484 | |||
SaveWidget.prototype.status_saving = function () { | ||||
Brian Granger
|
r5874 | this.element.find('span#save_status').html('Saving...'); | ||
Stefan van der Walt
|
r4592 | }; | ||
Brian E. Granger
|
r4484 | |||
Brian Granger
|
r5874 | SaveWidget.prototype.status_save_failed = function () { | ||
this.element.find('span#save_status').html('Save failed'); | ||||
Brian Granger
|
r5859 | }; | ||
Brian E. Granger
|
r4484 | |||
Brian Granger
|
r5874 | SaveWidget.prototype.status_loading = function () { | ||
this.element.find('span#save_status').html('Loading...'); | ||||
Brian E. Granger
|
r4630 | }; | ||
Brian E. Granger
|
r4369 | IPython.SaveWidget = SaveWidget; | ||
return IPython; | ||||
}(IPython)); | ||||