@ -155,93 +155,58 @@ define([
this . element . find ( 'span.autosave_status' ) . text ( msg ) ;
} ;
SaveWidget . prototype . _set _checkpoint _status = function ( human _date , iso _date ) {
var el = this . element . find ( 'span.checkpoint_status' ) ;
if ( human _date ) {
el . text ( "Last Checkpoint: " + human _date ) . attr ( 'title' , iso _date ) ;
SaveWidget . prototype . _set _last _checkpoint = function ( checkpoint ) {
if ( checkpoint ) {
this . _checkpoint _date = new Date ( checkpoint . last _modified ) ;
} else {
el . text ( '' ) . attr ( 'title' , 'no-checkpoint' ) ;
}
} ;
// 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 ;
this . _checkpoint _date = null ;
}
this . _render _checkpoint ( ) ;
} ;
SaveWidget . prototype . _regularly _update _checkpoint _date = function ( ) {
if ( ! this . _checkpoint _date ) {
this . _set _checkpoint _status ( null ) ;
console . log ( 'no checkpoint done' ) ;
SaveWidget . prototype . _render _checkpoint = function ( ) {
/ * * a c t u a l l y s e t t h e t e x t i n t h e e l e m e n t , f r o m o u r _ c h e c k p o i n t v a l u e
called directly , and periodically in timeouts .
* /
this . _schedule _render _checkpoint ( ) ;
var el = this . element . find ( 'span.checkpoint_status' ) ;
if ( ! this . _checkpoint _date ) {
el . text ( '' ) . attr ( 'title' , 'no checkpoint' ) ;
return ;
}
var chkd = moment ( this . _checkpoint _date ) ;
var longdate = chkd . format ( 'llll' ) ;
var that = this ;
var recall = function ( t ) {
/ * *
* recall slightly later ( 1 s ) as long timeout in js might be imprecise ,
* and you want to be call * * after * * the change of formatting should happend .
* /
return setTimeout (
$ . proxy ( that . _regularly _update _checkpoint _date , that ) ,
t + 1000
) ;
} ;
var tdelta = Math . ceil ( new Date ( ) - this . _checkpoint _date ) ;
// update regularly for the first 6hours and show
// <x time> ago
if ( tdelta < 6 * 3600 * 1000 ) {
recall ( _next _timeago _update ( tdelta ) ) ;
this . _set _checkpoint _status ( chkd . fromNow ( ) , longdate ) ;
// otherwise update every hour and show
// <Today | yesterday|...> at hh,mm,ss
} else {
recall ( 1 * 3600 * 1000 ) ;
this . _set _checkpoint _status ( chkd . calendar ( ) , longdate ) ;
var long _date = chkd . format ( 'llll' ) ;
var human _date ;
var tdelta = Math . ceil ( new Date ( ) - this . _checkpoint _date ) ;
if ( tdelta < utils . time . milliseconds . d ) {
// less than 24 hours old, use relative date
human _date = chkd . fromNow ( ) ;
} else {
// otherwise show calendar
// <Today | yesterday|...> at hh,mm,ss
human _date = chkd . calendar ( ) ;
}
el . text ( 'Last Checkpoint: ' + human _date ) . attr ( 'title' , long _date ) ;
} ;
SaveWidget . prototype . _set _last _checkpoint = function ( checkpoint ) {
if ( checkpoint ) {
this . _checkpoint _date = new Date ( checkpoint . last _modified ) ;
} else {
this . _checkpoint _date = null ;
SaveWidget . prototype . _schedule _render _checkpoint = function ( ) {
/ * * s c h e d u l e t h e n e x t u p d a t e t o r e l a t i v e d a t e
periodically updated , so short values like 'a few seconds ago' don ' t get stale .
* /
if ( ! this . _checkpoint _date ) {
return ;
}
this . _regularly _update _checkpoint _date ( ) ;
if ( ( this . _checkpoint _timeout ) ) {
clearTimeout ( this . _checkpoint _timeout ) ;
}
var dt = Math . ceil ( new Date ( ) - this . _checkpoint _date ) ;
this . _checkpoint _timeout = setTimeout (
$ . proxy ( this . _render _checkpoint , this ) ,
utils . time . timeout _from _dt ( dt )
) ;
} ;
SaveWidget . prototype . set _autosaved = function ( dirty ) {