Show More
@@ -5,9 +5,10 b' define([' | |||
|
5 | 5 | 'base/js/namespace', |
|
6 | 6 | 'jquery', |
|
7 | 7 | 'codemirror/lib/codemirror', |
|
8 | 'moment', | |
|
8 | 9 | // silently upgrades CodeMirror |
|
9 | 10 | 'codemirror/mode/meta', |
|
10 | ], function(IPython, $, CodeMirror){ | |
|
11 | ], function(IPython, $, CodeMirror, moment){ | |
|
11 | 12 | "use strict"; |
|
12 | 13 | |
|
13 | 14 | IPython.load_extensions = function () { |
@@ -785,6 +786,41 b' define([' | |||
|
785 | 786 | }); |
|
786 | 787 | }; |
|
787 | 788 | |
|
789 | var time = {}; | |
|
790 | time.milliseconds = {}; | |
|
791 | time.milliseconds.s = 1000; | |
|
792 | time.milliseconds.m = 60 * time.milliseconds.s; | |
|
793 | time.milliseconds.h = 60 * time.milliseconds.m; | |
|
794 | time.milliseconds.d = 24 * time.milliseconds.h; | |
|
795 | ||
|
796 | time.thresholds = { | |
|
797 | // moment.js thresholds in milliseconds | |
|
798 | s: moment.relativeTimeThreshold('s') * time.milliseconds.s, | |
|
799 | m: moment.relativeTimeThreshold('m') * time.milliseconds.m, | |
|
800 | h: moment.relativeTimeThreshold('h') * time.milliseconds.h, | |
|
801 | d: moment.relativeTimeThreshold('d') * time.milliseconds.d, | |
|
802 | }; | |
|
803 | ||
|
804 | time.timeout_from_dt = function (dt) { | |
|
805 | /** compute a timeout based on dt | |
|
806 | ||
|
807 | input and output both in milliseconds | |
|
808 | ||
|
809 | use moment's relative time thresholds: | |
|
810 | ||
|
811 | - 10 seconds if in 'seconds ago' territory | |
|
812 | - 1 minute if in 'minutes ago' | |
|
813 | - 1 hour otherwise | |
|
814 | */ | |
|
815 | if (dt < time.thresholds.s) { | |
|
816 | return 10 * time.milliseconds.s; | |
|
817 | } else if (dt < time.thresholds.m) { | |
|
818 | return time.milliseconds.m; | |
|
819 | } else { | |
|
820 | return time.milliseconds.h; | |
|
821 | } | |
|
822 | }; | |
|
823 | ||
|
788 | 824 | var utils = { |
|
789 | 825 | regex_split : regex_split, |
|
790 | 826 | uuid : uuid, |
@@ -820,6 +856,7 b' define([' | |||
|
820 | 856 | resolve_promises_dict: resolve_promises_dict, |
|
821 | 857 | reject: reject, |
|
822 | 858 | typeset: typeset, |
|
859 | time: time, | |
|
823 | 860 | }; |
|
824 | 861 | |
|
825 | 862 | // Backwards compatability. |
@@ -1,1 +1,1 b'' | |||
|
1 | Subproject commit 87ff70d96567bf055eb94161a41e7b3e6da31b23 | |
|
1 | Subproject commit 6b578bb789708ded9b6611039d162c71135dfd68 |
@@ -141,47 +141,22 b' define([' | |||
|
141 | 141 | var long_date = chkd.format('llll'); |
|
142 | 142 | var human_date; |
|
143 | 143 | var tdelta = Math.ceil(new Date() - this._last_modified); |
|
144 | if (tdelta < 24 * H){ | |
|
144 | if (tdelta < utils.time.milliseconds.d){ | |
|
145 | 145 | // less than 24 hours old, use relative date |
|
146 | 146 | human_date = chkd.fromNow(); |
|
147 | 147 | } else { |
|
148 | 148 |
// otherwise show calendar |
|
149 | // otherwise update every hour and show | |
|
150 | 149 | // <Today | yesterday|...> at hh,mm,ss |
|
151 | 150 | human_date = chkd.calendar(); |
|
152 | 151 | } |
|
153 | 152 | el.text(human_date).attr('title', long_date); |
|
154 | 153 | }; |
|
155 | 154 | |
|
156 | ||
|
157 | var S = 1000; | |
|
158 | var M = 60*S; | |
|
159 | var H = 60*M; | |
|
160 | var thresholds = { | |
|
161 | s: 45 * S, | |
|
162 | m: 45 * M, | |
|
163 | h: 22 * H | |
|
164 | }; | |
|
165 | var _timeout_from_dt = function (ms) { | |
|
166 | /** compute a timeout to update the last-modified timeout | |
|
167 | ||
|
168 | based on the delta in milliseconds | |
|
169 | */ | |
|
170 | if (ms < thresholds.s) { | |
|
171 | return 5 * S; | |
|
172 | } else if (ms < thresholds.m) { | |
|
173 | return M; | |
|
174 | } else { | |
|
175 | return 5 * M; | |
|
176 | } | |
|
177 | }; | |
|
178 | ||
|
179 | 155 | SaveWidget.prototype._schedule_render_last_modified = function () { |
|
180 | 156 | /** schedule the next update to relative date |
|
181 | 157 | |
|
182 | 158 | periodically updated, so short values like 'a few seconds ago' don't get stale. |
|
183 | 159 | */ |
|
184 | var that = this; | |
|
185 | 160 | if (!this._last_modified) { |
|
186 | 161 | return; |
|
187 | 162 | } |
@@ -189,12 +164,10 b' define([' | |||
|
189 | 164 | clearTimeout(this._last_modified_timeout); |
|
190 | 165 | } |
|
191 | 166 | var dt = Math.ceil(new Date() - this._last_modified); |
|
192 | if (dt < 24 * H) { | |
|
193 | 167 |
|
|
194 | 168 |
|
|
195 |
|
|
|
169 | utils.time.timeout_from_dt(dt) | |
|
196 | 170 |
|
|
197 | } | |
|
198 | 171 | }; |
|
199 | 172 | |
|
200 | 173 | return {'SaveWidget': SaveWidget}; |
@@ -155,93 +155,58 b' define([' | |||
|
155 | 155 | this.element.find('span.autosave_status').text(msg); |
|
156 | 156 | }; |
|
157 | 157 | |
|
158 |
SaveWidget.prototype._set_checkpoint |
|
|
159 | var el = this.element.find('span.checkpoint_status'); | |
|
160 | if(human_date){ | |
|
161 | el.text("Last Checkpoint: "+human_date).attr('title',iso_date); | |
|
158 | SaveWidget.prototype._set_last_checkpoint = function (checkpoint) { | |
|
159 | if (checkpoint) { | |
|
160 | this._checkpoint_date = new Date(checkpoint.last_modified); | |
|
162 | 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 | |
|
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; | |
|
167 | SaveWidget.prototype._render_checkpoint = function () { | |
|
168 | /** actually set the text in the element, from our _checkpoint value | |
|
186 | 169 | |
|
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 | } | |
|
200 | }; | |
|
201 | ||
|
202 | SaveWidget.prototype._regularly_update_checkpoint_date = function(){ | |
|
170 | called directly, and periodically in timeouts. | |
|
171 | */ | |
|
172 | this._schedule_render_checkpoint(); | |
|
173 | var el = this.element.find('span.checkpoint_status'); | |
|
203 | 174 | if (!this._checkpoint_date) { |
|
204 | this._set_checkpoint_status(null); | |
|
205 | console.log('no checkpoint done'); | |
|
175 | el.text('').attr('title', 'no checkpoint'); | |
|
206 | 176 | return; |
|
207 | 177 | } |
|
208 | 178 | var chkd = moment(this._checkpoint_date); |
|
209 | var longdate = chkd.format('llll'); | |
|
210 | ||
|
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 | }; | |
|
179 | var long_date = chkd.format('llll'); | |
|
180 | var human_date; | |
|
222 | 181 | 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 | |
|
182 | if (tdelta < utils.time.milliseconds.d){ | |
|
183 | // less than 24 hours old, use relative date | |
|
184 | human_date = chkd.fromNow(); | |
|
231 | 185 |
} else |
|
232 | recall(1*3600*1000); | |
|
233 | this._set_checkpoint_status(chkd.calendar(), longdate); | |
|
186 | // otherwise show calendar | |
|
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 | 212 | SaveWidget.prototype.set_autosaved = function (dirty) { |
General Comments 0
You need to be logged in to leave comments.
Login now