all files / src/ input-numeric.component.js

58.49% Statements 62/106
34.29% Branches 24/70
60% Functions 9/15
58.49% Lines 62/106
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187          11×                                                                                                     13× 13×   13× 13× 13×     13× 13× 13× 13×     12×     11×             13×                                                                                                                  
"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: "<"
        }
    });