diff --git a/background.html b/background.html index 3cd56896..c323c65f 100644 --- a/background.html +++ b/background.html @@ -77,7 +77,7 @@
{{ message }}
@@ -325,6 +325,7 @@ + diff --git a/js/views/message_view.js b/js/views/message_view.js index a5b01219..6add9a96 100644 --- a/js/views/message_view.js +++ b/js/views/message_view.js @@ -17,6 +17,7 @@ this.listenTo(this.model, 'destroy', this.remove); this.listenTo(this.model, 'pending', this.renderPending); this.listenTo(this.model, 'done', this.renderDone); + this.timeStampView = new Whisper.MessageTimestampView(); }, events: { 'click .timestamp': 'select', @@ -67,11 +68,13 @@ this.$el.html( Mustache.render(this.template, { message: this.model.get('body'), - timestamp: moment(this.model.get('sent_at')).fromNow(), + timestamp: this.model.get('sent_at'), sender: (contact && contact.getTitle()) || '', avatar: (contact && contact.getAvatar()) }, this.render_partials()) ); + this.timeStampView.setElement(this.$('.timestamp')); + this.timeStampView.update(); this.renderControl(); diff --git a/js/views/timestamp_view.js b/js/views/timestamp_view.js new file mode 100644 index 00000000..a4f99eb8 --- /dev/null +++ b/js/views/timestamp_view.js @@ -0,0 +1,53 @@ +/* + * vim: ts=4:sw=4:expandtab + */ +(function () { + 'use strict'; + window.Whisper = window.Whisper || {}; + + Whisper.MessageTimestampView = Whisper.View.extend({ + update: function() { + clearTimeout(this.timeout); + var millis_now = Date.now(); + var millis = this.$el.data('timestamp'); + if (millis >= millis_now) { + millis = millis_now; + } + var lastWeek = moment(millis_now).subtract(7, 'days'); + var time = moment(millis); + var result = ''; + if (time > moment(millis_now).startOf('day')) { + // t units ago + result = time.fromNow(); + } else if (time > lastWeek) { + // Fri 1:30 PM or Fri 13:30 + result = time.format('ddd ') + time.format('LT'); + } else { + // Oct 31 1:30 PM or Oct 31 + result = time.format('MMM D ') + time.format('LT'); + } + this.$el.text(result); + + var delay; + var millis_since = millis_now - millis; + if (millis_since <= moment.relativeTimeThreshold('s') * 1000) { + // a few seconds ago + delay = 45 * 1000 - millis_since; + } else if (millis_since <= moment.relativeTimeThreshold('m') * 1000 * 60) { + // N minutes ago + delay = 60 * 1000; + } else if (millis_since <= moment.relativeTimeThreshold('h') * 1000 * 60 * 60) { + // N hours ago + delay = 60 * 60 * 1000; + } else if (time > lastWeek) { + // Day of week + time + delay = 7 * 24 * 60 * 60 * 1000 - millis_since; + } + + if (delay) { + if (delay < 0) { delay = 0; } + this.timeout = setTimeout(this.update.bind(this), delay); + } + } + }); +})();