"use strict";
var inputNumericController = function($element, $interval, $scope) {
var self = this;
self.input = undefined;
self.internValue = 0;
$scope.$watch("$ctrl.value",
function(newVal, oldVal) {
//if model changes outside this component
if (!self.validate(newVal, false))
self.setValue(self.min);
else
self.setValue(newVal);
});
$scope.$watch("[$ctrl.min, $ctrl.max, $ctrl.step, $ctrl.numberOfDecimals]", self.validateOptions);
self.$postLink = function() {
var input = $($element.find("input")[0]);
input.keydown(function(e) {
if (!self.readOnly)
self.keypress(e);
}.bind(self));
self.input = input;
};
self.start = new Date();
self.$onInit = function () {
//set default values
Iif (!self.step)
self.step = 1;
Eif (!self.numberOfDecimals)
self.numberOfDecimals = 0;
self.validateOptions();
};
self.validateOptions = function() {
Iif (isNaN(self.step)) {
console.log("[numeric-input] step is not valid");
}
Iif (isNaN(self.numberOfDecimals)) {
console.log("[numeric-input] numberOfDecimals is not valid");
} else {
var stepString = self.step.toString();
var index = stepString.indexOf(".");
Iif (index >= 0) {
var stepLength = stepString.length;
var stepLengthDecimals = stepLength - (index + 1);
if (self.numberOfDecimals < stepLengthDecimals) {
console.log("[numeric-input] numberOfDecimals cant be smaller than decimals of stepLength");
self.numberOfDecimals = undefined;
}
}
}
Eif (!isNaN(self.min) || !isNaN(self.max)) {
Iif (self.min > self.max) {
console.log("[numeric-input] min cant be lower than high");
}
}
};
self.lostFocus = function() {
if (!self.readOnly) {
var value = self.input.val();
if (self.validate(value)) {
self.setValue(value);
} else {
self.setValue(0);
}
}
};
self.keypress = function(event) {
if (event.keyCode === 38)
self.decrease();
else if (event.keyCode === 40)
self.increase();
};
self.setValue = function(val) {
Eif (!isNaN(self.numberOfDecimals)) {
val = parseFloat(val).toFixed(self.numberOfDecimals);
}
self.internValue = val;
self.value = parseFloat(val);
self.input.val(val);
};
self.validate = function(value, log) {
if (log !== false)
log = true;
value = parseFloat(value);
var retVal = true;
if (isNaN(value)) {
Iif (log)
if (self.debug)
console.log("[numeric-input] not a number");
retVal = false;
} else if (self.max < value) {
Iif (log)
if (self.debug)
console.log("[numeric-input] value too high");
retVal = false;
} else if (self.min > value) {
Iif (log)
if (self.debug)
console.log("[numeric-input] value too low");
retVal = false;
} else {
}
return retVal;
};
self.interval = undefined;
self.start = function(e) {
if (!self.readOnly) {
if (self.interval)
self.stop();
var intervalFunction;
if (e === "up")
intervalFunction = self.decrease;
else if (e === "down")
intervalFunction = self.increase;
if (typeof intervalFunction === "function") {
intervalFunction();
if (!self.interval)
self.interval = $interval(function() {
intervalFunction();
},
150);
}
}
};
self.stop = function() {
var a = $interval.cancel(self.interval);
if (a) {
self.interval = undefined;
}
};
self.decrease = function() {
var val = parseFloat(self.internValue) + parseFloat(self.step);
Eif (self.validate(val)) {
self.setValue(val);
}
else
self.setValue(self.max);
};
self.increase = function() {
var val = parseFloat(self.internValue) - parseFloat(self.step);
Eif (self.validate(val)) {
self.setValue(val);
}
else
self.setValue(self.min);
};
};
angular.module("inputNumeric",
[
"templates"
])
.component("inputNumeric",
{
templateUrl: "input-numeric.component.html",
controller: inputNumericController,
bindings: {
min: "<",
max: "<",
step: "<",
numberOfDecimals: "<",
options: "<",
value: "=",
debug: "<",
readOnly: "<"
}
}); |