##// END OF EJS Templates
Merge pull request #7186 from minrk/moment...
Matthias Bussonnier -
r19549:96038745 merge
parent child Browse files
Show More
@@ -5,9 +5,10 b' define(['
5 'base/js/namespace',
5 'base/js/namespace',
6 'jquery',
6 'jquery',
7 'codemirror/lib/codemirror',
7 'codemirror/lib/codemirror',
8 'moment',
8 // silently upgrades CodeMirror
9 // silently upgrades CodeMirror
9 'codemirror/mode/meta',
10 'codemirror/mode/meta',
10 ], function(IPython, $, CodeMirror){
11 ], function(IPython, $, CodeMirror, moment){
11 "use strict";
12 "use strict";
12
13
13 IPython.load_extensions = function () {
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 var utils = {
824 var utils = {
789 regex_split : regex_split,
825 regex_split : regex_split,
790 uuid : uuid,
826 uuid : uuid,
@@ -820,6 +856,7 b' define(['
820 resolve_promises_dict: resolve_promises_dict,
856 resolve_promises_dict: resolve_promises_dict,
821 reject: reject,
857 reject: reject,
822 typeset: typeset,
858 typeset: typeset,
859 time: time,
823 };
860 };
824
861
825 // Backwards compatability.
862 // Backwards compatability.
@@ -1,1 +1,1 b''
1 Subproject commit 87ff70d96567bf055eb94161a41e7b3e6da31b23
1 Subproject commit 6b578bb789708ded9b6611039d162c71135dfd68
@@ -141,47 +141,22 b' define(['
141 var long_date = chkd.format('llll');
141 var long_date = chkd.format('llll');
142 var human_date;
142 var human_date;
143 var tdelta = Math.ceil(new Date() - this._last_modified);
143 var tdelta = Math.ceil(new Date() - this._last_modified);
144 if (tdelta < 24 * H){
144 if (tdelta < utils.time.milliseconds.d){
145 // less than 24 hours old, use relative date
145 // less than 24 hours old, use relative date
146 human_date = chkd.fromNow();
146 human_date = chkd.fromNow();
147 } else {
147 } else {
148 // otherwise show calendar
148 // otherwise show calendar
149 // otherwise update every hour and show
150 // <Today | yesterday|...> at hh,mm,ss
149 // <Today | yesterday|...> at hh,mm,ss
151 human_date = chkd.calendar();
150 human_date = chkd.calendar();
152 }
151 }
153 el.text(human_date).attr('title', long_date);
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 SaveWidget.prototype._schedule_render_last_modified = function () {
155 SaveWidget.prototype._schedule_render_last_modified = function () {
180 /** schedule the next update to relative date
156 /** schedule the next update to relative date
181
157
182 periodically updated, so short values like 'a few seconds ago' don't get stale.
158 periodically updated, so short values like 'a few seconds ago' don't get stale.
183 */
159 */
184 var that = this;
185 if (!this._last_modified) {
160 if (!this._last_modified) {
186 return;
161 return;
187 }
162 }
@@ -189,12 +164,10 b' define(['
189 clearTimeout(this._last_modified_timeout);
164 clearTimeout(this._last_modified_timeout);
190 }
165 }
191 var dt = Math.ceil(new Date() - this._last_modified);
166 var dt = Math.ceil(new Date() - this._last_modified);
192 if (dt < 24 * H) {
193 this._last_modified_timeout = setTimeout(
167 this._last_modified_timeout = setTimeout(
194 $.proxy(this._render_last_modified, this),
168 $.proxy(this._render_last_modified, this),
195 _timeout_from_dt(dt)
169 utils.time.timeout_from_dt(dt)
196 );
170 );
197 }
198 };
171 };
199
172
200 return {'SaveWidget': SaveWidget};
173 return {'SaveWidget': SaveWidget};
@@ -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 }
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