savewidget.js
175 lines
| 5.8 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) { | ||||
Matthias BUSSONNIER
|
r12103 | "use strict"; | ||
Brian E. Granger
|
r4369 | |||
var utils = IPython.utils; | ||||
var SaveWidget = function (selector) { | ||||
Brian E. Granger
|
r4372 | this.selector = selector; | ||
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 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 Granger
|
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(); | ||||
}); | ||||
Zachary Sailer
|
r12997 | $([IPython.events]).on('notebook_renamed.Notebook', function () { | ||
Zachary Sailer
|
r13012 | that.update_notebook_name(); | ||
that.update_document_title(); | ||||
Zachary Sailer
|
r13010 | that.update_address_bar(); | ||
Zachary Sailer
|
r12997 | }); | ||
Brian Granger
|
r6047 | $([IPython.events]).on('notebook_save_failed.Notebook', function () { | ||
MinRK
|
r10829 | that.set_save_status('Autosave Failed!'); | ||
Brian Granger
|
r6047 | }); | ||
MinRK
|
r10516 | $([IPython.events]).on('checkpoints_listed.Notebook', function (event, data) { | ||
MinRK
|
r10520 | that.set_last_checkpoint(data[0]); | ||
MinRK
|
r10516 | }); | ||
Kevin Burke
|
r12368 | |||
MinRK
|
r10516 | $([IPython.events]).on('checkpoint_created.Notebook', function (event, data) { | ||
that.set_last_checkpoint(data); | ||||
}); | ||||
MinRK
|
r10829 | $([IPython.events]).on('set_dirty.Notebook', function (event, data) { | ||
that.set_autosaved(data.value); | ||||
}); | ||||
Felix Werner
|
r5006 | }; | ||
Brian Granger
|
r5859 | SaveWidget.prototype.rename_notebook = function () { | ||
var that = this; | ||||
MinRK
|
r10956 | var dialog = $('<div/>').append( | ||
$("<p/>").addClass("rename-message") | ||||
Matthias BUSSONNIER
|
r14634 | .text('Enter a new notebook name:') | ||
MinRK
|
r10956 | ).append( | ||
$("<br/>") | ||||
).append( | ||||
Brian Granger
|
r5871 | $('<input/>').attr('type','text').attr('size','25') | ||
MinRK
|
r10895 | .val(IPython.notebook.get_notebook_name()) | ||
Brian Granger
|
r5859 | ); | ||
MinRK
|
r10895 | IPython.dialog.modal({ | ||
Brian Granger
|
r5859 | title: "Rename Notebook", | ||
MinRK
|
r10895 | body: dialog, | ||
Brian Granger
|
r5859 | buttons : { | ||
MinRK
|
r10895 | "Cancel": {}, | ||
"OK": { | ||||
class: "btn-primary", | ||||
click: function () { | ||||
var new_name = $(this).find('input').val(); | ||||
Brian Granger
|
r6047 | if (!IPython.notebook.test_notebook_name(new_name)) { | ||
Matthias BUSSONNIER
|
r14634 | $(this).find('.rename-message').text( | ||
Brian Granger
|
r5955 | "Invalid notebook name. Notebook names must "+ | ||
"have 1 or more characters and can contain any characters " + | ||||
Brian Granger
|
r7229 | "except :/\\. Please enter a new notebook name:" | ||
Brian Granger
|
r5859 | ); | ||
Rick Lupton
|
r11551 | return false; | ||
Brian Granger
|
r5859 | } else { | ||
MinRK
|
r13133 | IPython.notebook.rename(new_name); | ||
Brian Granger
|
r5859 | } | ||
MinRK
|
r10895 | }} | ||
Brian Granger
|
r5859 | }, | ||
Brian Granger
|
r7246 | open : function (event, ui) { | ||
var that = $(this); | ||||
// Upon ENTER, click the OK button. | ||||
Brian Granger
|
r7247 | that.find('input[type="text"]').keydown(function (event, ui) { | ||
Brian E. Granger
|
r15619 | if (event.which === IPython.keyboard.keycodes.enter) { | ||
MinRK
|
r10895 | that.find('.btn-primary').first().click(); | ||
MinRK
|
r12019 | return false; | ||
Brian Granger
|
r7246 | } | ||
}); | ||||
Kevin Burke
|
r12368 | that.find('input[type="text"]').focus().select(); | ||
Brian Granger
|
r5859 | } | ||
}); | ||||
} | ||||
Brian E. Granger
|
r4645 | |||
Brian Granger
|
r6047 | SaveWidget.prototype.update_notebook_name = function () { | ||
var nbname = IPython.notebook.get_notebook_name(); | ||||
Matthias BUSSONNIER
|
r14634 | this.element.find('span#notebook_name').text(nbname); | ||
Stefan van der Walt
|
r5479 | }; | ||
Brian E. Granger
|
r4372 | |||
Brian E. Granger
|
r4369 | |||
Brian Granger
|
r6047 | SaveWidget.prototype.update_document_title = function () { | ||
var nbname = IPython.notebook.get_notebook_name(); | ||||
Brian E. Granger
|
r5104 | document.title = nbname; | ||
Brian E. Granger
|
r4549 | }; | ||
Zachary Sailer
|
r13010 | |||
SaveWidget.prototype.update_address_bar = function(){ | ||||
MinRK
|
r15483 | var base_url = IPython.notebook.base_url; | ||
Zachary Sailer
|
r13010 | var nbname = IPython.notebook.notebook_name; | ||
MinRK
|
r15234 | var path = IPython.notebook.notebook_path; | ||
MinRK
|
r15483 | var state = {path : path, name: nbname}; | ||
MinRK
|
r13693 | window.history.replaceState(state, "", utils.url_join_encode( | ||
MinRK
|
r15483 | base_url, | ||
"notebooks", | ||||
MinRK
|
r13103 | path, | ||
nbname) | ||||
); | ||||
MinRK
|
r15483 | }; | ||
Brian E. Granger
|
r4484 | |||
Brian Granger
|
r6047 | SaveWidget.prototype.set_save_status = function (msg) { | ||
Matthias BUSSONNIER
|
r14634 | this.element.find('span#autosave_status').text(msg); | ||
MinRK
|
r10516 | } | ||
SaveWidget.prototype.set_checkpoint_status = function (msg) { | ||||
Matthias BUSSONNIER
|
r14634 | this.element.find('span#checkpoint_status').text(msg); | ||
Brian Granger
|
r6047 | } | ||
Brian E. Granger
|
r4484 | |||
MinRK
|
r10516 | SaveWidget.prototype.set_last_checkpoint = function (checkpoint) { | ||
MinRK
|
r10532 | if (!checkpoint) { | ||
this.set_checkpoint_status(""); | ||||
MinRK
|
r10547 | return; | ||
MinRK
|
r10532 | } | ||
MinRK
|
r10516 | var d = new Date(checkpoint.last_modified); | ||
this.set_checkpoint_status( | ||||
"Last Checkpoint: " + d.format('mmm dd HH:MM') | ||||
); | ||||
} | ||||
Brian E. Granger
|
r4484 | |||
MinRK
|
r10829 | SaveWidget.prototype.set_autosaved = function (dirty) { | ||
if (dirty) { | ||||
this.set_save_status("(unsaved changes)"); | ||||
} else { | ||||
this.set_save_status("(autosaved)"); | ||||
} | ||||
Brian E. Granger
|
r4630 | }; | ||
Brian E. Granger
|
r4369 | IPython.SaveWidget = SaveWidget; | ||
return IPython; | ||||
}(IPython)); | ||||