##// END OF EJS Templates
fix regular checkpoint updates in notebook...
Min RK -
Show More
@@ -155,93 +155,58 b' define(['
155 this.element.find('span.autosave_status').text(msg);
155 this.element.find('span.autosave_status').text(msg);
156 };
156 };
157
157
158 SaveWidget.prototype._set_checkpoint_status = function (human_date, iso_date) {
158 SaveWidget.prototype._set_last_checkpoint = function (checkpoint) {
159 var el = this.element.find('span.checkpoint_status');
159 if (checkpoint) {
160 if(human_date){
160 this._checkpoint_date = new Date(checkpoint.last_modified);
161 el.text("Last Checkpoint: "+human_date).attr('title',iso_date);
162 } else {
161 } else {
163 el.text('').attr('title', 'no-checkpoint');
162 this._checkpoint_date = null;
164 }
165 };
166
167 // compute (roughly) the remaining time in millisecond until the next
168 // moment.js relative time update of the string, which by default
169 // happend at
170 // (a few seconds ago)
171 // - 45sec,
172 // (a minute ago)
173 // - 90sec,
174 // ( x minutes ago)
175 // - then every minutes until
176 // - 45 min,
177 // (an hour ago)
178 // - 1h45,
179 // (x hours ago )
180 // - then every hours
181 // - 22 hours ago
182 var _next_timeago_update = function(deltatime_ms){
183 var s = 1000;
184 var m = 60*s;
185 var h = 60*m;
186
187 var mtt = moment.relativeTimeThreshold;
188
189 if(deltatime_ms < mtt.s*s){
190 return mtt.s*s-deltatime_ms;
191 } else if (deltatime_ms < (mtt.s*s+m)) {
192 return (mtt.s*s+m)-deltatime_ms;
193 } else if (deltatime_ms < mtt.m*m){
194 return m;
195 } else if (deltatime_ms < (mtt.m*m+h)){
196 return (mtt.m*m+h)-deltatime_ms;
197 } else {
198 return h;
199 }
163 }
164 this._render_checkpoint();
200 };
165 };
201
166
202 SaveWidget.prototype._regularly_update_checkpoint_date = function(){
167 SaveWidget.prototype._render_checkpoint = function () {
203 if (!this._checkpoint_date) {
168 /** actually set the text in the element, from our _checkpoint value
204 this._set_checkpoint_status(null);
169
205 console.log('no checkpoint done');
170 called directly, and periodically in timeouts.
171 */
172 this._schedule_render_checkpoint();
173 var el = this.element.find('span.checkpoint_status');
174 if (!this._checkpoint_date) {
175 el.text('').attr('title', 'no checkpoint');
206 return;
176 return;
207 }
177 }
208 var chkd = moment(this._checkpoint_date);
178 var chkd = moment(this._checkpoint_date);
209 var longdate = chkd.format('llll');
179 var long_date = chkd.format('llll');
210
180 var human_date;
211 var that = this;
181 var tdelta = Math.ceil(new Date() - this._checkpoint_date);
212 var recall = function(t){
182 if (tdelta < utils.time.milliseconds.d){
213 /**
183 // less than 24 hours old, use relative date
214 * recall slightly later (1s) as long timeout in js might be imprecise,
184 human_date = chkd.fromNow();
215 * and you want to be call **after** the change of formatting should happend.
185 } else {
216 */
186 // otherwise show calendar
217 return setTimeout(
187 // <Today | yesterday|...> at hh,mm,ss
218 $.proxy(that._regularly_update_checkpoint_date, that),
188 human_date = chkd.calendar();
219 t + 1000
220 );
221 };
222 var tdelta = Math.ceil(new Date()-this._checkpoint_date);
223
224 // update regularly for the first 6hours and show
225 // <x time> ago
226 if(tdelta < 6*3600*1000){
227 recall(_next_timeago_update(tdelta));
228 this._set_checkpoint_status(chkd.fromNow(), longdate);
229 // otherwise update every hour and show
230 // <Today | yesterday|...> at hh,mm,ss
231 } else {
232 recall(1*3600*1000);
233 this._set_checkpoint_status(chkd.calendar(), longdate);
234 }
189 }
190 el.text('Last Checkpoint: ' + human_date).attr('title', long_date);
235 };
191 };
236
192
237 SaveWidget.prototype._set_last_checkpoint = function (checkpoint) {
193
238 if (checkpoint) {
194 SaveWidget.prototype._schedule_render_checkpoint = function () {
239 this._checkpoint_date = new Date(checkpoint.last_modified);
195 /** schedule the next update to relative date
240 } else {
196
241 this._checkpoint_date = null;
197 periodically updated, so short values like 'a few seconds ago' don't get stale.
198 */
199 if (!this._checkpoint_date) {
200 return;
242 }
201 }
243 this._regularly_update_checkpoint_date();
202 if ((this._checkpoint_timeout)) {
244
203 clearTimeout(this._checkpoint_timeout);
204 }
205 var dt = Math.ceil(new Date() - this._checkpoint_date);
206 this._checkpoint_timeout = setTimeout(
207 $.proxy(this._render_checkpoint, this),
208 utils.time.timeout_from_dt(dt)
209 );
245 };
210 };
246
211
247 SaveWidget.prototype.set_autosaved = function (dirty) {
212 SaveWidget.prototype.set_autosaved = function (dirty) {
General Comments 0
You need to be logged in to leave comments. Login now