diff --git a/public/js/directives/directives.js b/public/js/directives/directives.js index 95e136a997..fa8d58b622 100644 --- a/public/js/directives/directives.js +++ b/public/js/directives/directives.js @@ -153,3 +153,16 @@ habitrpg } }]); +habitrpg.directive('fromNow', ['$interval', function($interval){ + return function(scope, element, attr){ + var updateText = function(){ element.text(moment(scope.message.timestamp).fromNow()) }; + updateText(); + // Update the counter every 60secs if was sent less than one hour ago otherwise every hour + // OPTIMIZATION, every time the interval is run, update the interval time + var intervalTime = moment().diff(scope.message.timestamp, 'minute') < 60 ? 60000 : 3600000; + var interval = $interval(function(){ updateText() }, intervalTime, false); + scope.$on('$destroy', function() { + $interval.cancel(interval); + }); + } +}]); diff --git a/views/options/social/chat-message.jade b/views/options/social/chat-message.jade index f94d2820ea..8c0dfa08db 100644 --- a/views/options/social/chat-message.jade +++ b/views/options/social/chat-message.jade @@ -4,7 +4,7 @@ li(ng-repeat='message in group.chat', ng-class='{highlight: isUserMentioned(user |   span(ng-bind-html="message.text | linky:'_blank'") |  -  - span.muted.time - | {{moment(message.timestamp).fromNow() + ' '}} + span.muted.time(from-now='message.timestamp') a(ng-show='user.contributor.admin || message.uuid == user.id', ng-click='deleteChatMessage(group, message)') + | i.icon-remove(tooltip='Delete')