2015-11-06 22:58:26 +00:00
|
|
|
/*
|
|
|
|
* vim: ts=4:sw=4:expandtab
|
|
|
|
*/
|
|
|
|
(function () {
|
|
|
|
'use strict';
|
|
|
|
window.Whisper = window.Whisper || {};
|
|
|
|
|
2015-12-20 12:34:35 +00:00
|
|
|
Whisper.TimestampView = Whisper.View.extend({
|
2016-04-19 02:48:54 +00:00
|
|
|
initialize: function(options) {
|
2015-11-07 00:47:30 +00:00
|
|
|
extension.windows.onClosed(this.clearTimeout.bind(this));
|
|
|
|
},
|
2015-11-06 22:58:26 +00:00
|
|
|
update: function() {
|
2015-11-07 00:47:30 +00:00
|
|
|
this.clearTimeout();
|
2015-11-06 22:58:26 +00:00
|
|
|
var millis_now = Date.now();
|
|
|
|
var millis = this.$el.data('timestamp');
|
2016-02-24 20:03:25 +00:00
|
|
|
if (millis === "") {
|
|
|
|
return;
|
|
|
|
}
|
2015-11-06 22:58:26 +00:00
|
|
|
if (millis >= millis_now) {
|
|
|
|
millis = millis_now;
|
|
|
|
}
|
2015-12-20 12:34:35 +00:00
|
|
|
var result = this.getRelativeTimeSpanString(millis);
|
2015-11-06 22:58:26 +00:00
|
|
|
this.$el.text(result);
|
|
|
|
|
2016-04-19 02:48:54 +00:00
|
|
|
var timestamp = moment(millis);
|
|
|
|
this.$el.attr('title', timestamp.format('llll'));
|
|
|
|
|
2015-11-06 22:58:26 +00:00
|
|
|
var millis_since = millis_now - millis;
|
2016-04-19 02:48:54 +00:00
|
|
|
if (this.delay) {
|
|
|
|
if (this.delay < 0) { this.delay = 1000; }
|
|
|
|
this.timeout = setTimeout(this.update.bind(this), this.delay);
|
2016-01-27 20:10:42 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
clearTimeout: function() {
|
|
|
|
clearTimeout(this.timeout);
|
|
|
|
},
|
2015-12-20 12:34:35 +00:00
|
|
|
getRelativeTimeSpanString: function(timestamp_) {
|
|
|
|
// Convert to moment timestamp if it isn't already
|
|
|
|
var timestamp = moment(timestamp_),
|
2016-10-20 17:53:08 +00:00
|
|
|
now = moment(),
|
|
|
|
timediff = moment.duration(now - timestamp);
|
2015-12-20 12:34:35 +00:00
|
|
|
|
|
|
|
if (timediff.years() > 0) {
|
2016-04-19 02:48:54 +00:00
|
|
|
this.delay = null;
|
2016-04-21 04:13:56 +00:00
|
|
|
return timestamp.format(this._format.y);
|
2015-12-20 12:34:35 +00:00
|
|
|
} else if (timediff.months() > 0 || timediff.days() > 6) {
|
2016-04-19 02:48:54 +00:00
|
|
|
this.delay = null;
|
2016-08-12 00:42:00 +00:00
|
|
|
return timestamp.format(this._format.M);
|
2015-12-20 12:34:35 +00:00
|
|
|
} else if (timediff.days() > 0) {
|
2016-10-20 17:53:08 +00:00
|
|
|
this.delay = moment(timestamp).add(timediff.days() + 1,'d').diff(now);
|
2016-04-21 04:13:56 +00:00
|
|
|
return timestamp.format(this._format.d);
|
2015-12-20 12:34:35 +00:00
|
|
|
} else if (timediff.hours() > 1) {
|
2016-10-20 17:53:08 +00:00
|
|
|
this.delay = moment(timestamp).add(timediff.hours() + 1,'h').diff(now);
|
2016-08-10 18:33:56 +00:00
|
|
|
return this.relativeTime(timediff.hours(), 'h');
|
2016-04-19 02:48:54 +00:00
|
|
|
} else if (timediff.hours() === 1) {
|
2016-10-20 17:53:08 +00:00
|
|
|
this.delay = moment(timestamp).add(timediff.hours() + 1,'h').diff(now);
|
2016-04-19 02:48:54 +00:00
|
|
|
return this.relativeTime(timediff.hours(), 'h');
|
2015-12-20 12:34:35 +00:00
|
|
|
} else if (timediff.minutes() > 1) {
|
2016-10-20 17:53:08 +00:00
|
|
|
this.delay = moment(timestamp).add(timediff.minutes() + 1,'m').diff(now);
|
2016-08-10 18:33:56 +00:00
|
|
|
return this.relativeTime(timediff.minutes(), 'm');
|
2016-04-19 02:48:54 +00:00
|
|
|
} else if (timediff.minutes() === 1) {
|
2016-10-20 17:53:08 +00:00
|
|
|
this.delay = moment(timestamp).add(timediff.minutes() + 1,'m').diff(now);
|
2016-04-19 02:48:54 +00:00
|
|
|
return this.relativeTime(timediff.minutes(), 'm');
|
2015-12-20 12:34:35 +00:00
|
|
|
} else {
|
2016-10-20 17:53:08 +00:00
|
|
|
this.delay = moment(timestamp).add(1,'m').diff(now);
|
2016-04-19 02:48:54 +00:00
|
|
|
return this.relativeTime(timediff.seconds(), 's');
|
2015-12-20 12:34:35 +00:00
|
|
|
}
|
|
|
|
},
|
2016-08-10 18:33:56 +00:00
|
|
|
relativeTime : function (number, string) {
|
|
|
|
return moment.duration(number, string).humanize();
|
2016-04-19 02:48:54 +00:00
|
|
|
},
|
|
|
|
_format: {
|
2016-08-12 00:42:00 +00:00
|
|
|
y: "ll",
|
|
|
|
M: i18n('timestampFormat_M') || "MMM D",
|
2016-08-05 20:41:23 +00:00
|
|
|
d: "ddd"
|
2016-04-19 02:48:54 +00:00
|
|
|
}
|
2015-12-20 12:34:35 +00:00
|
|
|
});
|
|
|
|
Whisper.ExtendedTimestampView = Whisper.TimestampView.extend({
|
2016-06-26 18:36:36 +00:00
|
|
|
relativeTime : function (number, string, isFuture) {
|
2016-08-10 18:33:56 +00:00
|
|
|
return moment.duration(-1 * number, string).humanize(string !== 's');
|
2015-12-20 12:34:35 +00:00
|
|
|
},
|
2016-04-19 02:48:54 +00:00
|
|
|
_format: {
|
2016-08-12 00:42:00 +00:00
|
|
|
y: "lll",
|
|
|
|
M: (i18n('timestampFormat_M') || "MMM D") + ' LT',
|
2016-08-05 20:41:23 +00:00
|
|
|
d: "ddd LT"
|
2016-04-19 02:48:54 +00:00
|
|
|
}
|
2015-12-20 12:34:35 +00:00
|
|
|
});
|
2015-11-06 22:58:26 +00:00
|
|
|
})();
|