##// END OF EJS Templates
fix regular checkpoint updates in notebook...
Min RK -
Show More
@@ -155,93 +155,58 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 }
163 }
164 this._render_checkpoint();
165 };
165 };
166
166
167 // compute (roughly) the remaining time in millisecond until the next
167 SaveWidget.prototype._render_checkpoint = function () {
168 // moment.js relative time update of the string, which by default
168 /** actually set the text in the element, from our _checkpoint value
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
169
187 var mtt = moment.relativeTimeThreshold;
170 called directly, and periodically in timeouts.
188
171 */
189 if(deltatime_ms < mtt.s*s){
172 this._schedule_render_checkpoint();
190 return mtt.s*s-deltatime_ms;
173 var el = this.element.find('span.checkpoint_status');
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 }
200 };
201
202 SaveWidget.prototype._regularly_update_checkpoint_date = function(){
203 if (!this._checkpoint_date) {
174 if (!this._checkpoint_date) {
204 this._set_checkpoint_status(null);
175 el.text('').attr('title', 'no checkpoint');
205 console.log('no checkpoint done');
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;
212 var recall = function(t){
213 /**
214 * recall slightly later (1s) as long timeout in js might be imprecise,
215 * and you want to be call **after** the change of formatting should happend.
216 */
217 return setTimeout(
218 $.proxy(that._regularly_update_checkpoint_date, that),
219 t + 1000
220 );
221 };
222 var tdelta = Math.ceil(new Date()-this._checkpoint_date);
181 var tdelta = Math.ceil(new Date() - this._checkpoint_date);
223
182 if (tdelta < utils.time.milliseconds.d){
224 // update regularly for the first 6hours and show
183 // less than 24 hours old, use relative date
225 // <x time> ago
184 human_date = chkd.fromNow();
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 {
185 } else {
232 recall(1*3600*1000);
186 // otherwise show calendar
233 this._set_checkpoint_status(chkd.calendar(), longdate);
187 // <Today | yesterday|...> at hh,mm,ss
188 human_date = chkd.calendar();
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) {
238 if (checkpoint) {
239 this._checkpoint_date = new Date(checkpoint.last_modified);
240 } else {
241 this._checkpoint_date = null;
242 }
243 this._regularly_update_checkpoint_date();
244
193
194 SaveWidget.prototype._schedule_render_checkpoint = function () {
195 /** schedule the next update to relative date
196
197 periodically updated, so short values like 'a few seconds ago' don't get stale.
198 */
199 if (!this._checkpoint_date) {
200 return;
201 }
202 if ((this._checkpoint_timeout)) {
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