add utils.time

consolidate some time-related utils, used in save widgets
This commit is contained in:
Min RK 2014-12-11 15:08:56 -08:00
parent 8ea37a4b2b
commit b11fe93abb
2 changed files with 45 additions and 35 deletions

View File

@ -5,9 +5,10 @@ define([
'base/js/namespace',
'jquery',
'codemirror/lib/codemirror',
'moment',
// silently upgrades CodeMirror
'codemirror/mode/meta',
], function(IPython, $, CodeMirror){
], function(IPython, $, CodeMirror, moment){
"use strict";
IPython.load_extensions = function () {
@ -824,7 +825,42 @@ define([
return MathJax.Hub.Queue(["Typeset", MathJax.Hub, this]);
});
};
var time = {};
time.milliseconds = {};
time.milliseconds.s = 1000;
time.milliseconds.m = 60 * time.milliseconds.s;
time.milliseconds.h = 60 * time.milliseconds.m;
time.milliseconds.d = 24 * time.milliseconds.h;
time.thresholds = {
// moment.js thresholds in milliseconds
s: moment.relativeTimeThreshold('s') * time.milliseconds.s,
m: moment.relativeTimeThreshold('m') * time.milliseconds.m,
h: moment.relativeTimeThreshold('h') * time.milliseconds.h,
d: moment.relativeTimeThreshold('d') * time.milliseconds.d,
};
time.timeout_from_dt = function (dt) {
/** compute a timeout based on dt
input and output both in milliseconds
use moment's relative time thresholds:
- 10 seconds if in 'seconds ago' territory
- 1 minute if in 'minutes ago'
- 1 hour otherwise
*/
if (dt < time.thresholds.s) {
return 10 * time.milliseconds.s;
} else if (dt < time.thresholds.m) {
return time.milliseconds.m;
} else {
return time.milliseconds.h;
}
};
var utils = {
regex_split : regex_split,
uuid : uuid,
@ -860,6 +896,7 @@ define([
resolve_promises_dict: resolve_promises_dict,
reject: reject,
typeset: typeset,
time: time,
};
// Backwards compatability.

View File

@ -141,47 +141,22 @@ define([
var long_date = chkd.format('llll');
var human_date;
var tdelta = Math.ceil(new Date() - this._last_modified);
if (tdelta < 24 * H){
if (tdelta < utils.time.milliseconds.d){
// less than 24 hours old, use relative date
human_date = chkd.fromNow();
} else {
// otherwise show calendar
// otherwise update every hour and show
// otherwise show calendar
// <Today | yesterday|...> at hh,mm,ss
human_date = chkd.calendar();
}
el.text(human_date).attr('title', long_date);
};
var S = 1000;
var M = 60*S;
var H = 60*M;
var thresholds = {
s: 45 * S,
m: 45 * M,
h: 22 * H
};
var _timeout_from_dt = function (ms) {
/** compute a timeout to update the last-modified timeout
based on the delta in milliseconds
*/
if (ms < thresholds.s) {
return 5 * S;
} else if (ms < thresholds.m) {
return M;
} else {
return 5 * M;
}
};
SaveWidget.prototype._schedule_render_last_modified = function () {
/** schedule the next update to relative date
periodically updated, so short values like 'a few seconds ago' don't get stale.
*/
var that = this;
if (!this._last_modified) {
return;
}
@ -189,12 +164,10 @@ define([
clearTimeout(this._last_modified_timeout);
}
var dt = Math.ceil(new Date() - this._last_modified);
if (dt < 24 * H) {
this._last_modified_timeout = setTimeout(
$.proxy(this._render_last_modified, this),
_timeout_from_dt(dt)
);
}
this._last_modified_timeout = setTimeout(
$.proxy(this._render_last_modified, this),
utils.time.timeout_from_dt(dt)
);
};
return {'SaveWidget': SaveWidget};