savewidget.js
155 lines
| 5.2 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 () { | ||
}; | ||||
|
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.update_notebook_name(); | ||||
that.update_document_title(); | ||||
}); | ||||
$([IPython.events]).on('notebook_saved.Notebook', function () { | ||||
that.update_notebook_name(); | ||||
that.update_document_title(); | ||||
}); | ||||
$([IPython.events]).on('notebook_save_failed.Notebook', function () { | ||||
|
r10829 | that.set_save_status('Autosave Failed!'); | ||
|
r6047 | }); | ||
|
r10516 | $([IPython.events]).on('checkpoints_listed.Notebook', function (event, data) { | ||
|
r10520 | that.set_last_checkpoint(data[0]); | ||
|
r10516 | }); | ||
$([IPython.events]).on('checkpoint_created.Notebook', function (event, data) { | ||||
that.set_last_checkpoint(data); | ||||
}); | ||||
|
r10829 | $([IPython.events]).on('set_dirty.Notebook', function (event, data) { | ||
that.set_autosaved(data.value); | ||||
}); | ||||
|
r5006 | }; | ||
|
r5859 | SaveWidget.prototype.rename_notebook = function () { | ||
var that = this; | ||||
|
r10956 | var dialog = $('<div/>').append( | ||
$("<p/>").addClass("rename-message") | ||||
.html('Enter a new notebook name:') | ||||
).append( | ||||
$("<br/>") | ||||
).append( | ||||
|
r5871 | $('<input/>').attr('type','text').attr('size','25') | ||
|
r10895 | .val(IPython.notebook.get_notebook_name()) | ||
|
r5859 | ); | ||
|
r10895 | IPython.dialog.modal({ | ||
|
r5859 | title: "Rename Notebook", | ||
|
r10895 | body: dialog, | ||
|
r5859 | buttons : { | ||
|
r10895 | "Cancel": {}, | ||
"OK": { | ||||
class: "btn-primary", | ||||
click: function () { | ||||
var new_name = $(this).find('input').val(); | ||||
|
r6047 | if (!IPython.notebook.test_notebook_name(new_name)) { | ||
|
r10895 | $(this).find('.rename-message').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); | ||
|
r10519 | IPython.notebook.save_notebook(); | ||
|
r5859 | } | ||
|
r10895 | }} | ||
|
r5859 | }, | ||
|
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) { | ||
|
r10895 | that.find('.btn-primary').first().click(); | ||
|
r7246 | } | ||
}); | ||||
|
r10895 | that.find('input[type="text"]').focus(); | ||
|
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 | |||
|
r6047 | SaveWidget.prototype.set_save_status = function (msg) { | ||
|
r10516 | this.element.find('span#autosave_status').html(msg); | ||
} | ||||
SaveWidget.prototype.set_checkpoint_status = function (msg) { | ||||
this.element.find('span#checkpoint_status').html(msg); | ||||
|
r6047 | } | ||
|
r4484 | |||
|
r10516 | SaveWidget.prototype.set_last_checkpoint = function (checkpoint) { | ||
|
r10532 | if (!checkpoint) { | ||
this.set_checkpoint_status(""); | ||||
|
r10547 | return; | ||
|
r10532 | } | ||
|
r10516 | var d = new Date(checkpoint.last_modified); | ||
this.set_checkpoint_status( | ||||
"Last Checkpoint: " + d.format('mmm dd HH:MM') | ||||
); | ||||
} | ||||
|
r4484 | |||
|
r10829 | SaveWidget.prototype.set_autosaved = function (dirty) { | ||
if (dirty) { | ||||
this.set_save_status("(unsaved changes)"); | ||||
} else { | ||||
this.set_save_status("(autosaved)"); | ||||
} | ||||
|
r4630 | }; | ||
|
r4369 | IPython.SaveWidget = SaveWidget; | ||
return IPython; | ||||
}(IPython)); | ||||