Skip to content

Instantly share code, notes, and snippets.

@bwiggs
Created April 22, 2014 17:43
Show Gist options
  • Save bwiggs/11188102 to your computer and use it in GitHub Desktop.
Save bwiggs/11188102 to your computer and use it in GitHub Desktop.
function TimeDurationDirective() {
var tpl = "<div> \
<input type='text' ng-model='num' size='80' /> \
<select ng-model='unit'> \
<option value='secs'>Seconds</option> \
<option value='mins'>Minutes</option> \
<option value='hours'>Hours</option> \
<option value='days'>Days</option> \
</select> \
</div>";
return {
restrict: 'E',
template: tpl,
require: 'ngModel',
replace: true,
link: function(scope, iElement, iAttrs, ngModelCtrl) {
// Units of time
multiplierMap = {seconds: 1, minutes: 60, hours: 3600, days: 86400};
multiplierTypes = ['seconds', 'minutes', 'hours', 'days']
ngModelCtrl.$formatters.push(function(modelValue) {
var unit = 'minutes', num = 0, i, unitName;
modelValue = parseInt(modelValue || 0);
// Figure out the largest unit of time the model value
// fits into. For example, 3600 is 1 hour, but 1800 is 30 minutes.
for (i = multiplierTypes.length-1; i >= 0; i--) {
unitName = multiplierTypes[i];
if (modelValue % multiplierMap[unitName] === 0) {
unit = unitName;
break;
}
}
if (modelValue) {
num = modelValue / multiplierMap[unit]
}
return {
unit: unit,
num: num
};
});
ngModelCtrl.$parsers.push(function(viewValue) {
var unit = viewValue.unit, num = viewValue.num, multiplier;
multiplier = multiplierMap[unit];
return num * multiplier;
});
scope.$watch('time_unit + time_num', function() {
ngModelCtrl.$setViewValue({ unit: scope.unit, num: scope.num });
});
ngModelCtrl.$render = function() {
if (!$viewValue) $viewValue = { unit: 'hours', num: 1 };
scope.unit = ngModelCtrl.$viewValue.unit;
scope.num = ngModelCtrl.$viewValue.num;
};
}
};
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment