mirror of
https://github.com/HabitRPG/habitica.git
synced 2025-12-19 15:48:04 +01:00
Encapsulate directive functions in anonymous functions - avoid the global namespace
This commit is contained in:
@@ -1,44 +1,46 @@
|
||||
'use strict';
|
||||
|
||||
angular
|
||||
.module('habitrpg')
|
||||
.directive('fromNow', fromNow);
|
||||
(function(){
|
||||
angular
|
||||
.module('habitrpg')
|
||||
.directive('fromNow', fromNow);
|
||||
|
||||
fromNow.$inject = [
|
||||
'$interval',
|
||||
'$timeout'
|
||||
];
|
||||
fromNow.$inject = [
|
||||
'$interval',
|
||||
'$timeout'
|
||||
];
|
||||
|
||||
function fromNow($interval, $timeout) {
|
||||
return function(scope, element, attr){
|
||||
var interval, timeout;
|
||||
function fromNow($interval, $timeout) {
|
||||
return function(scope, element, attr){
|
||||
var interval, timeout;
|
||||
|
||||
var updateText = function(){
|
||||
element.text(moment(scope.message.timestamp).fromNow());
|
||||
};
|
||||
var updateText = function(){
|
||||
element.text(moment(scope.message.timestamp).fromNow());
|
||||
};
|
||||
|
||||
var setupInterval = function() {
|
||||
if(interval) $interval.cancel(interval);
|
||||
if(timeout) $timeout.cancel(timeout);
|
||||
var setupInterval = function() {
|
||||
if(interval) $interval.cancel(interval);
|
||||
if(timeout) $timeout.cancel(timeout);
|
||||
|
||||
var diff = moment().diff(scope.message.timestamp, 'minute');
|
||||
var diff = moment().diff(scope.message.timestamp, 'minute');
|
||||
|
||||
if(diff < 60) {
|
||||
// Update every minute
|
||||
interval = $interval(updateText, 60000, false);
|
||||
timeout = $timeout(setupInterval, diff * 60000);
|
||||
} else {
|
||||
// Update every hour
|
||||
interval = $interval(updateText, 3600000, false);
|
||||
}
|
||||
};
|
||||
if(diff < 60) {
|
||||
// Update every minute
|
||||
interval = $interval(updateText, 60000, false);
|
||||
timeout = $timeout(setupInterval, diff * 60000);
|
||||
} else {
|
||||
// Update every hour
|
||||
interval = $interval(updateText, 3600000, false);
|
||||
}
|
||||
};
|
||||
|
||||
updateText();
|
||||
setupInterval();
|
||||
updateText();
|
||||
setupInterval();
|
||||
|
||||
scope.$on('$destroy', function() {
|
||||
if(interval) $interval.cancel(interval);
|
||||
if(timeout) $timeout.cancel(timeout);
|
||||
});
|
||||
scope.$on('$destroy', function() {
|
||||
if(interval) $interval.cancel(interval);
|
||||
if(timeout) $timeout.cancel(timeout);
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}());
|
||||
|
||||
Reference in New Issue
Block a user