"use strict"; (function () { 'use strict'; angular.module('myitsmApp') .constant('inputCounterConfig', { inlineLayout: false }) .controller('inputCounterController', [ '$scope', '$element', '$attrs', function ($scope, $element, $attrs) { var ngModelCtrl = { $setViewValue: angular.noop }; $scope.label = angular.isDefined($attrs.label) ? $attrs.label : undefined; $scope.labelHidden = angular.isDefined($attrs.labelHidden); $scope.minValue = angular.isDefined($attrs.min) ? parseInt($attrs.min, 10) : undefined; $scope.maxValue = angular.isDefined($attrs.max) ? parseInt($attrs.max, 10) : undefined; $scope.step = angular.isDefined($attrs.step) ? parseInt($attrs.step, 10) : 1; $scope.required = angular.isDefined($attrs.required); $scope.placeholder = angular.isDefined($attrs.placeholder) ? $attrs.placeholder : ""; $scope.tabindex = angular.isDefined($attrs.tabindex) ? $attrs.tabindex : 0; $element.removeAttr('tabindex'); function updateValue() { ngModelCtrl.$setViewValue($scope.counterValue); } this.init = function (_ngModel, input) { var counterInput = input.eq(0); ngModelCtrl = _ngModel; ngModelCtrl.$formatters.unshift(function (modelValue) { $scope.counterValue = modelValue || null; return modelValue ? modelValue : null; }); counterInput.bind('keyup', function () { $scope.counterValue = parseInt(this.value, 10); updateValue(); }); }; $scope.incrementCounter = function () { if (angular.isDefined($scope.maxValue)) { if ($scope.counterValue + $scope.step <= $scope.maxValue) { $scope.counterValue += $scope.step; updateValue(); } } else { $scope.counterValue += $scope.step; updateValue(); } }; $scope.decrementCounter = function () { if (angular.isDefined($scope.minValue)) { if ($scope.counterValue - $scope.step >= $scope.minValue) { $scope.counterValue -= $scope.step; updateValue(); } } else { $scope.counterValue -= $scope.step; updateValue(); } }; } ]) .directive('inputCounter', ['inputCounterConfig', function () { return { restrict: 'A', require: ['inputCounter', '?^ngModel'], controller: 'inputCounterController', controllerAs: 'counter', replace: true, scope: { onEnter: '&' }, templateUrl: 'views/common/input-counter.html', link: function (scope, element, attrs, ctrls) { var counterCtrl = ctrls[0], ngModelCtrl = ctrls[1]; if (ngModelCtrl) { counterCtrl.init(ngModelCtrl, element.find('input')); } } }; }]); }());