habitica-self-host/public/js/directives/directives.js

101 lines
3 KiB
JavaScript
Raw Normal View History

2013-08-25 01:06:37 +00:00
'use strict';
/**
* Directive that places focus on the element it is applied to when the expression it binds to evaluates to true.
*/
habitrpg.directive('taskFocus',
['$timeout',
function($timeout) {
return function(scope, elem, attrs) {
scope.$watch(attrs.taskFocus, function(newval) {
if ( newval ) {
$timeout(function() {
2013-08-25 01:06:37 +00:00
elem[0].focus();
}, 0, false);
}
});
};
}
]);
2013-08-28 17:16:53 +00:00
habitrpg.directive('habitrpgAdsense', function() {
return {
restrict: 'A',
transclude: true,
replace: true,
template: '<div ng-transclude></div>',
link: function ($scope, element, attrs) {}
}
});
2013-08-28 17:16:53 +00:00
2013-08-25 01:06:37 +00:00
habitrpg.directive('whenScrolled', function() {
return function(scope, elm, attr) {
var raw = elm[0];
elm.bind('scroll', function() {
if (raw.scrollTop + raw.offsetHeight >= raw.scrollHeight) {
scope.$apply(attr.whenScrolled);
}
});
};
2013-08-25 01:06:37 +00:00
});
habitrpg
.directive('habitrpgTasks', ['$rootScope', 'User', function($rootScope, User) {
return {
restrict: 'EA',
templateUrl: 'templates/habitrpg-tasks.html',
//transclude: true,
//scope: {
// main: '@', // true if it's the user's main list
// obj: '='
//},
controller: ['$scope', '$rootScope', function($scope, $rootScope){
$scope.editTask = function(task){
task._editing = !task._editing;
task._tags = User.user.preferences.tagsCollapsed;
task._advanced = User.user.preferences.advancedCollapsed;
if($rootScope.charts[task.id]) $rootScope.charts[task.id] = false;
};
}],
link: function(scope, element, attrs) {
// $scope.obj needs to come from controllers, so we can pass by ref
scope.main = attrs.main;
$rootScope.lists = [
{
header: window.env.t('habits'),
type: 'habit',
placeHolder: window.env.t('newHabit')
}, {
header: window.env.t('dailies'),
type: 'daily',
placeHolder: window.env.t('newDaily')
}, {
header: window.env.t('todos'),
type: 'todo',
placeHolder: window.env.t('newTodo')
}, {
header: window.env.t('rewards'),
type: 'reward',
placeHolder: window.env.t('newReward')
}
];
}
}
}]);
2013-11-17 19:52:39 +00:00
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);
});
}
}]);