##// END OF EJS Templates
Merge pull request #6258 from patricktokeeffe/patch-1...
Merge pull request #6258 from patricktokeeffe/patch-1 Update custom.js

File last commit:

r17759:830c1a7f
r17762:0214dd73 merge
Show More
savewidget.js
241 lines | 8.0 KiB | application/javascript | JavascriptLexer
Jonathan Frederic
Almost done!...
r17198 // Copyright (c) IPython Development Team.
// Distributed under the terms of the Modified BSD License.
define([
'base/js/namespace',
Jonathan Frederic
MWE,...
r17200 'jquery',
Jonathan Frederic
Almost done!...
r17198 'base/js/utils',
'base/js/dialog',
'base/js/keyboard',
Matthias BUSSONNIER
use momentjs for nice dates
r17474 'moment',
], function(IPython, $, utils, dialog, keyboard, moment) {
Matthias BUSSONNIER
"use strict" in most (if not all) our javascript...
r12103 "use strict";
Brian E. Granger
Minors fixes and initial work on save widget....
r4369
Jonathan Frederic
Fix some dialog keyboard_manager problems
r17213 var SaveWidget = function (selector, options) {
Matthias BUSSONNIER
remove unused dependecy and css class
r17442 // TODO: Remove circular ref.
Jonathan Frederic
Almost done!...
r17198 this.notebook = undefined;
Brian E. Granger
Work on save widget, kernel status widget and notebook section.
r4372 this.selector = selector;
Jonathan Frederic
Fix some dialog keyboard_manager problems
r17213 this.events = options.events;
Matthias BUSSONNIER
use momentjs for nice dates
r17474 this._checkpoint_date = undefined;
Jonathan Frederic
Fix some dialog keyboard_manager problems
r17213 this.keyboard_manager = options.keyboard_manager;
Brian E. Granger
Work on save widget, kernel status widget and notebook section.
r4372 if (this.selector !== undefined) {
this.element = $(selector);
Brian E. Granger
Minors fixes and initial work on save widget....
r4369 this.bind_events();
}
};
Brian E. Granger
Work on save widget, kernel status widget and notebook section.
r4372
Brian E. Granger
Minors fixes and initial work on save widget....
r4369 SaveWidget.prototype.bind_events = function () {
var that = this;
Brian Granger
Improved notebook renaming....
r5859 this.element.find('span#notebook_name').click(function () {
that.rename_notebook();
Brian E. Granger
Work on save widget, kernel status widget and notebook section.
r4372 });
Jonathan Frederic
Almost done!...
r17198 this.events.on('notebook_loaded.Notebook', function () {
Brian Granger
Major refactoring of saving, notification....
r6047 that.update_notebook_name();
that.update_document_title();
});
Jonathan Frederic
Almost done!...
r17198 this.events.on('notebook_saved.Notebook', function () {
Brian Granger
Major refactoring of saving, notification....
r6047 that.update_notebook_name();
that.update_document_title();
});
Jonathan Frederic
Almost done!...
r17198 this.events.on('notebook_renamed.Notebook', function () {
Zachary Sailer
allow spaces in notebook path
r13012 that.update_notebook_name();
that.update_document_title();
Zachary Sailer
redirect url after notebook rename
r13010 that.update_address_bar();
Zachary Sailer
Add 'patch' to session & notebook, rename working
r12997 });
Jonathan Frederic
Almost done!...
r17198 this.events.on('notebook_save_failed.Notebook', function () {
MinRK
use dirty event to set autosaved/unsaved changes...
r10829 that.set_save_status('Autosave Failed!');
Brian Granger
Major refactoring of saving, notification....
r6047 });
Jonathan Frederic
Almost done!...
r17198 this.events.on('checkpoints_listed.Notebook', function (event, data) {
Matthias BUSSONNIER
use momentjs for nice dates
r17474 that._set_last_checkpoint(data[0]);
MinRK
add checkpoint status to notebook header...
r10516 });
Kevin Burke
Select default title when renaming a notebook...
r12368
Jonathan Frederic
Almost done!...
r17198 this.events.on('checkpoint_created.Notebook', function (event, data) {
Matthias BUSSONNIER
use momentjs for nice dates
r17474 that._set_last_checkpoint(data);
MinRK
add checkpoint status to notebook header...
r10516 });
Jonathan Frederic
Almost done!...
r17198 this.events.on('set_dirty.Notebook', function (event, data) {
MinRK
use dirty event to set autosaved/unsaved changes...
r10829 that.set_autosaved(data.value);
});
Felix Werner
Update document title and last_saved_name only after a successful save.
r5006 };
Jonathan Frederic
Fix some dialog keyboard_manager problems
r17213 SaveWidget.prototype.rename_notebook = function (options) {
options = options || {};
Brian Granger
Improved notebook renaming....
r5859 var that = this;
Jonathan Frederic
Fix all the bugs!
r17203 var dialog_body = $('<div/>').append(
MinRK
add break between prompt and input in Rename dialog
r10956 $("<p/>").addClass("rename-message")
Matthias BUSSONNIER
some $.html( -> $.text(...
r14634 .text('Enter a new notebook name:')
MinRK
add break between prompt and input in Rename dialog
r10956 ).append(
$("<br/>")
).append(
Jonathan Frederic
Review comments
r16957 $('<input/>').attr('type','text').attr('size','25').addClass('form-control')
Jonathan Frederic
Almost done!...
r17198 .val(that.notebook.get_notebook_name())
Brian Granger
Improved notebook renaming....
r5859 );
Jonathan Frederic
Fix imports of "modules",...
r17202 dialog.modal({
Brian Granger
Improved notebook renaming....
r5859 title: "Rename Notebook",
Jonathan Frederic
Fix all the bugs!
r17203 body: dialog_body,
Jonathan Frederic
Fix some dialog keyboard_manager problems
r17213 notebook: options.notebook,
keyboard_manager: this.keyboard_manager,
Brian Granger
Improved notebook renaming....
r5859 buttons : {
MinRK
bootstrap dialogs
r10895 "Cancel": {},
"OK": {
class: "btn-primary",
click: function () {
var new_name = $(this).find('input').val();
Jonathan Frederic
Almost done!...
r17198 if (!that.notebook.test_notebook_name(new_name)) {
Matthias BUSSONNIER
some $.html( -> $.text(...
r14634 $(this).find('.rename-message').text(
Brian Granger
Disallow empty notebook names.
r5955 "Invalid notebook name. Notebook names must "+
"have 1 or more characters and can contain any characters " +
Brian Granger
Make : invalid in filenames in the Notebook JS code.
r7229 "except :/\\. Please enter a new notebook name:"
Brian Granger
Improved notebook renaming....
r5859 );
Rick Lupton
Fix rename notebook - show error with invalid name...
r11551 return false;
Brian Granger
Improved notebook renaming....
r5859 } else {
Jonathan Frederic
Almost done!...
r17198 that.notebook.rename(new_name);
Brian Granger
Improved notebook renaming....
r5859 }
MinRK
bootstrap dialogs
r10895 }}
Brian Granger
Improved notebook renaming....
r5859 },
Brian Granger
ENTER submits the rename notebook dialog.
r7246 open : function (event, ui) {
var that = $(this);
// Upon ENTER, click the OK button.
Brian Granger
Making the input text area watch for `ENTER` in nb renames.
r7247 that.find('input[type="text"]').keydown(function (event, ui) {
Jonathan Frederic
Fix some dialog keyboard_manager problems
r17213 if (event.which === keyboard.keycodes.enter) {
MinRK
bootstrap dialogs
r10895 that.find('.btn-primary').first().click();
MinRK
swallow enter event in rename dialog...
r12019 return false;
Brian Granger
ENTER submits the rename notebook dialog.
r7246 }
});
Kevin Burke
Select default title when renaming a notebook...
r12368 that.find('input[type="text"]').focus().select();
Brian Granger
Improved notebook renaming....
r5859 }
});
Jonathan Frederic
Almost done!...
r17198 };
Brian Granger
Improved notebook renaming....
r5859
Brian E. Granger
Adding keyboard shortcuts.
r4645
Brian Granger
Major refactoring of saving, notification....
r6047 SaveWidget.prototype.update_notebook_name = function () {
Jonathan Frederic
Almost done!...
r17198 var nbname = this.notebook.get_notebook_name();
Matthias BUSSONNIER
some $.html( -> $.text(...
r14634 this.element.find('span#notebook_name').text(nbname);
Stefan van der Walt
Clean up javascript based on js2-mode feedback.
r5479 };
Brian E. Granger
Work on save widget, kernel status widget and notebook section.
r4372
Brian E. Granger
Minors fixes and initial work on save widget....
r4369
Brian Granger
Major refactoring of saving, notification....
r6047 SaveWidget.prototype.update_document_title = function () {
Jonathan Frederic
Almost done!...
r17198 var nbname = this.notebook.get_notebook_name();
Brian E. Granger
Misc changes to the notebook....
r5104 document.title = nbname;
Brian E. Granger
Browser window title follows the name of the notebook.
r4549 };
Matthias BUSSONNIER
use momentjs for nice dates
r17474
Zachary Sailer
redirect url after notebook rename
r13010 SaveWidget.prototype.update_address_bar = function(){
Jonathan Frederic
Almost done!...
r17198 var base_url = this.notebook.base_url;
var nbname = this.notebook.notebook_name;
var path = this.notebook.notebook_path;
MinRK
don't forget base_url when updating address bar in rename
r15483 var state = {path : path, name: nbname};
MinRK
make sure to encode URL components for API requests...
r13693 window.history.replaceState(state, "", utils.url_join_encode(
MinRK
don't forget base_url when updating address bar in rename
r15483 base_url,
"notebooks",
MinRK
review pass on multidir js
r13103 path,
nbname)
);
MinRK
don't forget base_url when updating address bar in rename
r15483 };
Brian E. Granger
Massive work on the notebook document format....
r4484
Brian Granger
Major refactoring of saving, notification....
r6047 SaveWidget.prototype.set_save_status = function (msg) {
Matthias BUSSONNIER
some $.html( -> $.text(...
r14634 this.element.find('span#autosave_status').text(msg);
Jonathan Frederic
Almost done!...
r17198 };
MinRK
add checkpoint status to notebook header...
r10516
Matthias BUSSONNIER
use momentjs for nice dates
r17474 SaveWidget.prototype._set_checkpoint_status = function (human_date, iso_date) {
MinRK
fix handling of no-checkpoint-defined...
r17715 var el = this.element.find('span#checkpoint_status');
Matthias BUSSONNIER
use momentjs for nice dates
r17474 if(human_date){
el.text("Last Checkpoint: "+human_date).attr('title',iso_date);
} else {
MinRK
fix handling of no-checkpoint-defined...
r17715 el.text('').attr('title', 'no-checkpoint');
Matthias BUSSONNIER
use momentjs for nice dates
r17474 }
Jonathan Frederic
Almost done!...
r17198 };
Brian E. Granger
Massive work on the notebook document format....
r4484
Matthias BUSSONNIER
use momentjs for nice dates
r17474 // compute (roughly) the remaining time in millisecond until the next
// moment.js relative time update of the string, which by default
// happend at
// (a few seconds ago)
// - 45sec,
// (a minute ago)
// - 90sec,
// ( x minutes ago)
// - then every minutes until
// - 45 min,
// (an hour ago)
// - 1h45,
// (x hours ago )
// - then every hours
// - 22 hours ago
var _next_timeago_update = function(deltatime_ms){
var s = 1000;
var m = 60*s;
var h = 60*m;
var mtt = moment.relativeTimeThreshold;
if(deltatime_ms < mtt.s*s){
return mtt.s*s-deltatime_ms;
} else if (deltatime_ms < (mtt.s*s+m)) {
return (mtt.s*s+m)-deltatime_ms;
} else if (deltatime_ms < mtt.m*m){
return m;
} else if (deltatime_ms < (mtt.m*m+h)){
return (mtt.m*m+h)-deltatime_ms;
} else {
return h;
}
MinRK
fix handling of no-checkpoint-defined...
r17715 };
Matthias BUSSONNIER
use momentjs for nice dates
r17474
SaveWidget.prototype._regularly_update_checkpoint_date = function(){
if (!this._checkpoint_date) {
MinRK
fix handling of no-checkpoint-defined...
r17715 this._set_checkpoint_status(null);
Matthias BUSSONNIER
use momentjs for nice dates
r17474 console.log('no checkpoint done');
MinRK
add missing return...
r10547 return;
MinRK
fix set_last_checkpoint when no checkpoint...
r10532 }
Matthias BUSSONNIER
use momentjs for nice dates
r17474 var chkd = moment(this._checkpoint_date);
var longdate = chkd.format('llll');
var that = this;
var recall = function(t){
// recall slightly later (1s) as long timeout in js might be imprecise,
// and you want to be call **after** the change of formatting should happend.
MinRK
fix handling of no-checkpoint-defined...
r17715 return setTimeout(
$.proxy(that._regularly_update_checkpoint_date, that),
t + 1000
);
Matthias BUSSONNIER
use momentjs for nice dates
r17474 }
var tdelta = Math.ceil(new Date()-this._checkpoint_date);
// update regularly for the first 6hours and show
// <x time> ago
if(tdelta < tdelta < 6*3600*1000){
recall(_next_timeago_update(tdelta));
MinRK
fix handling of no-checkpoint-defined...
r17715 this._set_checkpoint_status(chkd.fromNow(), longdate);
Matthias BUSSONNIER
use momentjs for nice dates
r17474 // otherwise update every hour and show
// <Today | yesterday|...> at hh,mm,ss
} else {
MinRK
fix handling of no-checkpoint-defined...
r17715 recall(1*3600*1000);
this._set_checkpoint_status(chkd.calendar(), longdate);
Matthias BUSSONNIER
use momentjs for nice dates
r17474 }
MinRK
fix handling of no-checkpoint-defined...
r17715 };
Matthias BUSSONNIER
use momentjs for nice dates
r17474
SaveWidget.prototype._set_last_checkpoint = function (checkpoint) {
MinRK
fix handling of no-checkpoint-defined...
r17715 if (checkpoint) {
this._checkpoint_date = new Date(checkpoint.last_modified);
} else {
this._checkpoint_date = null;
}
Matthias BUSSONNIER
use momentjs for nice dates
r17474 this._regularly_update_checkpoint_date();
Jonathan Frederic
Almost done!...
r17198 };
Brian E. Granger
Massive work on the notebook document format....
r4484
MinRK
use dirty event to set autosaved/unsaved changes...
r10829 SaveWidget.prototype.set_autosaved = function (dirty) {
if (dirty) {
this.set_save_status("(unsaved changes)");
} else {
this.set_save_status("(autosaved)");
}
Brian E. Granger
Save button becomes Rename when the notebook name changes.
r4630 };
Matthias BUSSONNIER
use dataformat module explicitely
r17443 // Backwards compatibility.
Brian E. Granger
Minors fixes and initial work on save widget....
r4369 IPython.SaveWidget = SaveWidget;
Jonathan Frederic
Return dicts instead of classes,...
r17201 return {'SaveWidget': SaveWidget};
Brian E. Granger
Minors fixes and initial work on save widget....
r4369
Jonathan Frederic
Almost done!...
r17198 });