savewidget.js
177 lines
| 5.8 KiB
| application/javascript
|
JavascriptLexer
|
r17198 | // Copyright (c) IPython Development Team. | ||
// Distributed under the terms of the Modified BSD License. | ||||
define([ | ||||
'base/js/namespace', | ||||
|
r17200 | 'jquery', | ||
|
r17198 | 'base/js/utils', | ||
'base/js/dialog', | ||||
'base/js/keyboard', | ||||
|
r17214 | 'dateformat', | ||
|
r17202 | ], function(IPython, $, utils, dialog, keyboard) { | ||
|
r12103 | "use strict"; | ||
|
r4369 | |||
|
r17213 | var SaveWidget = function (selector, options) { | ||
|
r17211 | // TODO: Remove circulat ref. | ||
|
r17198 | this.notebook = undefined; | ||
|
r4372 | this.selector = selector; | ||
|
r17213 | this.events = options.events; | ||
this.keyboard_manager = options.keyboard_manager; | ||||
|
r4372 | 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 | }); | ||
|
r17198 | this.events.on('notebook_loaded.Notebook', function () { | ||
|
r6047 | that.update_notebook_name(); | ||
that.update_document_title(); | ||||
}); | ||||
|
r17198 | this.events.on('notebook_saved.Notebook', function () { | ||
|
r6047 | that.update_notebook_name(); | ||
that.update_document_title(); | ||||
}); | ||||
|
r17198 | this.events.on('notebook_renamed.Notebook', function () { | ||
|
r13012 | that.update_notebook_name(); | ||
that.update_document_title(); | ||||
|
r13010 | that.update_address_bar(); | ||
|
r12997 | }); | ||
|
r17198 | this.events.on('notebook_save_failed.Notebook', function () { | ||
|
r10829 | that.set_save_status('Autosave Failed!'); | ||
|
r6047 | }); | ||
|
r17198 | this.events.on('checkpoints_listed.Notebook', function (event, data) { | ||
|
r10520 | that.set_last_checkpoint(data[0]); | ||
|
r10516 | }); | ||
|
r12368 | |||
|
r17198 | this.events.on('checkpoint_created.Notebook', function (event, data) { | ||
|
r10516 | that.set_last_checkpoint(data); | ||
}); | ||||
|
r17198 | this.events.on('set_dirty.Notebook', function (event, data) { | ||
|
r10829 | that.set_autosaved(data.value); | ||
}); | ||||
|
r5006 | }; | ||
|
r17213 | SaveWidget.prototype.rename_notebook = function (options) { | ||
options = options || {}; | ||||
|
r5859 | var that = this; | ||
|
r17203 | var dialog_body = $('<div/>').append( | ||
|
r10956 | $("<p/>").addClass("rename-message") | ||
|
r14634 | .text('Enter a new notebook name:') | ||
|
r10956 | ).append( | ||
$("<br/>") | ||||
).append( | ||||
|
r16957 | $('<input/>').attr('type','text').attr('size','25').addClass('form-control') | ||
|
r17198 | .val(that.notebook.get_notebook_name()) | ||
|
r5859 | ); | ||
|
r17202 | dialog.modal({ | ||
|
r5859 | title: "Rename Notebook", | ||
|
r17203 | body: dialog_body, | ||
|
r17213 | notebook: options.notebook, | ||
keyboard_manager: this.keyboard_manager, | ||||
|
r5859 | buttons : { | ||
|
r10895 | "Cancel": {}, | ||
"OK": { | ||||
class: "btn-primary", | ||||
click: function () { | ||||
var new_name = $(this).find('input').val(); | ||||
|
r17198 | if (!that.notebook.test_notebook_name(new_name)) { | ||
|
r14634 | $(this).find('.rename-message').text( | ||
|
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 | ); | ||
|
r11551 | return false; | ||
|
r5859 | } else { | ||
|
r17198 | that.notebook.rename(new_name); | ||
|
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) { | ||
|
r17213 | if (event.which === keyboard.keycodes.enter) { | ||
|
r10895 | that.find('.btn-primary').first().click(); | ||
|
r12019 | return false; | ||
|
r7246 | } | ||
}); | ||||
|
r12368 | that.find('input[type="text"]').focus().select(); | ||
|
r5859 | } | ||
}); | ||||
|
r17198 | }; | ||
|
r5859 | |||
|
r4645 | |||
|
r6047 | SaveWidget.prototype.update_notebook_name = function () { | ||
|
r17198 | var nbname = this.notebook.get_notebook_name(); | ||
|
r14634 | this.element.find('span#notebook_name').text(nbname); | ||
|
r5479 | }; | ||
|
r4372 | |||
|
r4369 | |||
|
r6047 | SaveWidget.prototype.update_document_title = function () { | ||
|
r17198 | var nbname = this.notebook.get_notebook_name(); | ||
|
r5104 | document.title = nbname; | ||
|
r4549 | }; | ||
|
r13010 | |||
SaveWidget.prototype.update_address_bar = function(){ | ||||
|
r17198 | var base_url = this.notebook.base_url; | ||
var nbname = this.notebook.notebook_name; | ||||
var path = this.notebook.notebook_path; | ||||
|
r15483 | var state = {path : path, name: nbname}; | ||
|
r13693 | window.history.replaceState(state, "", utils.url_join_encode( | ||
|
r15483 | base_url, | ||
"notebooks", | ||||
|
r13103 | path, | ||
nbname) | ||||
); | ||||
|
r15483 | }; | ||
|
r4484 | |||
|
r6047 | SaveWidget.prototype.set_save_status = function (msg) { | ||
|
r14634 | this.element.find('span#autosave_status').text(msg); | ||
|
r17198 | }; | ||
|
r10516 | |||
SaveWidget.prototype.set_checkpoint_status = function (msg) { | ||||
|
r14634 | this.element.find('span#checkpoint_status').text(msg); | ||
|
r17198 | }; | ||
|
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') | ||||
); | ||||
|
r17198 | }; | ||
|
r4484 | |||
|
r10829 | SaveWidget.prototype.set_autosaved = function (dirty) { | ||
if (dirty) { | ||||
this.set_save_status("(unsaved changes)"); | ||||
} else { | ||||
this.set_save_status("(autosaved)"); | ||||
} | ||||
|
r4630 | }; | ||
|
r17198 | // Backwards compatability. | ||
|
r4369 | IPython.SaveWidget = SaveWidget; | ||
|
r17201 | return {'SaveWidget': SaveWidget}; | ||
|
r4369 | |||
|
r17198 | }); | ||