savewidget.js
148 lines
| 5.1 KiB
| application/javascript
|
JavascriptLexer
|
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. | ||||
//---------------------------------------------------------------------------- | ||||
|
r4369 | |||
//============================================================================ | ||||
|
r4609 | // SaveWidget | ||
|
r4369 | //============================================================================ | ||
var IPython = (function (IPython) { | ||||
var utils = IPython.utils; | ||||
var SaveWidget = function (selector) { | ||||
|
r4372 | this.selector = selector; | ||
if (this.selector !== undefined) { | ||||
this.element = $(selector); | ||||
this.style(); | ||||
|
r4369 | this.bind_events(); | ||
} | ||||
}; | ||||
|
r4372 | SaveWidget.prototype.style = function () { | ||
|
r5859 | this.element.find('span#save_widget').addClass('ui-widget'); | ||
this.element.find('span#notebook_name').addClass('ui-widget ui-widget-content'); | ||||
|
r5874 | this.element.find('span#save_status').addClass('ui-widget ui-widget-content') | ||
.css({border: 'none', 'margin-left': '20px'}); | ||||
|
r4372 | }; | ||
|
r4369 | SaveWidget.prototype.bind_events = function () { | ||
var that = this; | ||||
|
r5859 | this.element.find('span#notebook_name').click(function () { | ||
that.rename_notebook(); | ||||
|
r4372 | }); | ||
|
r5859 | this.element.find('span#notebook_name').hover(function () { | ||
$(this).addClass("ui-state-hover"); | ||||
}, function () { | ||||
$(this).removeClass("ui-state-hover"); | ||||
|
r4630 | }); | ||
|
r6047 | $([IPython.events]).on('notebook_loaded.Notebook', function () { | ||
that.set_last_saved(); | ||||
that.update_notebook_name(); | ||||
that.update_document_title(); | ||||
}); | ||||
$([IPython.events]).on('notebook_saved.Notebook', function () { | ||||
that.set_last_saved(); | ||||
that.update_notebook_name(); | ||||
that.update_document_title(); | ||||
}); | ||||
$([IPython.events]).on('notebook_save_failed.Notebook', function () { | ||||
that.set_save_status(''); | ||||
}); | ||||
|
r5006 | }; | ||
|
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( | ||||
|
r5871 | $('<input/>').attr('type','text').attr('size','25') | ||
|
r5869 | .addClass('ui-widget ui-widget-content') | ||
|
r6047 | .attr('value',IPython.notebook.get_notebook_name()) | ||
|
r5859 | ); | ||
|
r5872 | // $(document).append(dialog); | ||
|
r5859 | dialog.dialog({ | ||
resizable: false, | ||||
modal: true, | ||||
title: "Rename Notebook", | ||||
closeText: "", | ||||
|
r5873 | close: function(event, ui) {$(this).dialog('destroy').remove();}, | ||
|
r5859 | buttons : { | ||
"OK": function () { | ||||
var new_name = $(this).find('input').attr('value'); | ||||
|
r6047 | if (!IPython.notebook.test_notebook_name(new_name)) { | ||
|
r5859 | $(this).find('h3').html( | ||
|
r5955 | "Invalid notebook name. Notebook names must "+ | ||
"have 1 or more characters and can contain any characters " + | ||||
|
r7229 | "except :/\\. Please enter a new notebook name:" | ||
|
r5859 | ); | ||
} else { | ||||
|
r6047 | IPython.notebook.set_notebook_name(new_name); | ||
IPython.notebook.save_notebook(); | ||||
|
r5859 | $(this).dialog('close'); | ||
} | ||||
}, | ||||
"Cancel": function () { | ||||
$(this).dialog('close'); | ||||
} | ||||
|
r7246 | }, | ||
open : function (event, ui) { | ||||
var that = $(this); | ||||
// Upon ENTER, click the OK button. | ||||
|
r7247 | that.find('input[type="text"]').keydown(function (event, ui) { | ||
|
r7248 | if (event.which === utils.keycodes.ENTER) { | ||
|
r7246 | that.parent().find('button').first().click(); | ||
} | ||||
}); | ||||
|
r5859 | } | ||
}); | ||||
} | ||||
|
r4645 | |||
|
r6047 | SaveWidget.prototype.update_notebook_name = function () { | ||
var nbname = IPython.notebook.get_notebook_name(); | ||||
|
r5859 | this.element.find('span#notebook_name').html(nbname); | ||
|
r5479 | }; | ||
|
r4372 | |||
|
r4369 | |||
|
r6047 | SaveWidget.prototype.update_document_title = function () { | ||
var nbname = IPython.notebook.get_notebook_name(); | ||||
|
r5104 | document.title = nbname; | ||
|
r4549 | }; | ||
|
r4484 | |||
SaveWidget.prototype.update_url = function () { | ||||
|
r6047 | var notebook_id = IPython.notebook.get_notebook_id(); | ||
if (notebook_id !== null) { | ||||
|
r6003 | var new_url = $('body').data('baseProjectUrl') + notebook_id; | ||
|
r5861 | window.history.replaceState({}, '', new_url); | ||
|
r4484 | }; | ||
}; | ||||
|
r6047 | SaveWidget.prototype.set_save_status = function (msg) { | ||
this.element.find('span#save_status').html(msg); | ||||
} | ||||
|
r4484 | |||
|
r5874 | SaveWidget.prototype.set_last_saved = function () { | ||
var d = new Date(); | ||||
|
r6047 | this.set_save_status('Last saved: '+d.format('mmm dd h:MM TT')); | ||
|
r4630 | }; | ||
|
r4369 | IPython.SaveWidget = SaveWidget; | ||
return IPython; | ||||
}(IPython)); | ||||