UNPKG

ngx-currency-mask

Version:

A very simple currency mask directive that allows using a number attribute with the ngModel.

124 lines 5.72 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var input_manager_1 = require("./input.manager"); var InputService = (function () { function InputService(htmlInputElement, options) { this.htmlInputElement = htmlInputElement; this.options = options; this.inputManager = new input_manager_1.InputManager(htmlInputElement); } InputService.prototype.addNumber = function (keyCode) { if (!this.rawValue) { this.rawValue = this.applyMask(false, "0"); } var keyChar = String.fromCharCode(keyCode); var selectionStart = this.inputSelection.selectionStart; var selectionEnd = this.inputSelection.selectionEnd; this.rawValue = this.rawValue.substring(0, selectionStart) + keyChar + this.rawValue.substring(selectionEnd, this.rawValue.length); this.updateFieldValue(selectionStart + 1); }; InputService.prototype.applyMask = function (isNumber, rawValue) { var _a = this.options, allowNegative = _a.allowNegative, decimal = _a.decimal, precision = _a.precision, prefix = _a.prefix, suffix = _a.suffix, thousands = _a.thousands; rawValue = isNumber ? new Number(rawValue).toFixed(precision) : rawValue; var onlyNumbers = rawValue.replace(/[^0-9]/g, ""); if (!onlyNumbers && this.clearMask(rawValue) == 0) { return ""; } var integerPart = onlyNumbers.slice(0, onlyNumbers.length - precision).replace(/^0*/g, "").replace(/\B(?=(\d{3})+(?!\d))/g, thousands); if (integerPart == "") { integerPart = "0"; } var newRawValue = integerPart; var decimalPart = onlyNumbers.slice(onlyNumbers.length - precision); if (precision > 0) { newRawValue += decimal + decimalPart; } var isZero = parseInt(integerPart) == 0 && (parseInt(decimalPart) == 0 || decimalPart == ""); var operator = (rawValue.indexOf("-") > -1 && allowNegative && !isZero) ? "-" : ""; return operator + prefix + newRawValue + suffix; }; InputService.prototype.clearMask = function (rawValue) { var value = (rawValue || "0").replace(this.options.prefix, "").replace(this.options.suffix, ""); if (this.options.thousands) { value = value.replace(new RegExp("\\" + this.options.thousands, "g"), ""); } if (this.options.decimal) { value = value.replace(this.options.decimal, "."); } return parseFloat(value); }; InputService.prototype.changeToNegative = function () { if (this.options.allowNegative && this.rawValue != "" && this.rawValue.charAt(0) != "-" && this.value != 0) { this.rawValue = "-" + this.rawValue; } }; InputService.prototype.changeToPositive = function () { this.rawValue = this.rawValue.replace("-", ""); }; InputService.prototype.removeNumber = function (keyCode) { var selectionEnd = this.inputSelection.selectionEnd; var selectionStart = this.inputSelection.selectionStart; if (selectionStart > this.rawValue.length - this.options.suffix.length) { selectionEnd = this.rawValue.length - this.options.suffix.length; selectionStart = this.rawValue.length - this.options.suffix.length; } selectionEnd = keyCode == 46 || keyCode == 63272 ? selectionEnd + 1 : selectionEnd; selectionStart = keyCode == 8 ? selectionStart - 1 : selectionStart; this.rawValue = this.rawValue.substring(0, selectionStart) + this.rawValue.substring(selectionEnd, this.rawValue.length); this.updateFieldValue(selectionStart); }; InputService.prototype.updateFieldValue = function (selectionStart) { var newRawValue = this.applyMask(false, this.rawValue || ""); selectionStart = selectionStart == undefined ? this.rawValue.length : selectionStart; this.inputManager.updateValueAndCursor(newRawValue, this.rawValue.length, selectionStart); }; InputService.prototype.updateOptions = function (options) { var value = this.value; this.options = options; this.value = value; }; Object.defineProperty(InputService.prototype, "canInputMoreNumbers", { get: function () { return this.inputManager.canInputMoreNumbers; }, enumerable: true, configurable: true }); Object.defineProperty(InputService.prototype, "inputSelection", { get: function () { return this.inputManager.inputSelection; }, enumerable: true, configurable: true }); Object.defineProperty(InputService.prototype, "rawValue", { get: function () { return this.inputManager.rawValue; }, set: function (value) { this.inputManager.rawValue = value; }, enumerable: true, configurable: true }); Object.defineProperty(InputService.prototype, "storedRawValue", { get: function () { return this.inputManager.storedRawValue; }, enumerable: true, configurable: true }); Object.defineProperty(InputService.prototype, "value", { get: function () { return this.clearMask(this.rawValue); }, set: function (value) { this.rawValue = this.applyMask(true, "" + value); }, enumerable: true, configurable: true }); return InputService; }()); exports.InputService = InputService; //# sourceMappingURL=input.service.js.map