UNPKG

ngx-currency-mask

Version:

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

115 lines 5.31 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var core_1 = require("@angular/core"); var forms_1 = require("@angular/forms"); var currency_mask_config_1 = require("./currency-mask.config"); var input_handler_1 = require("./input.handler"); exports.CURRENCYMASKDIRECTIVE_VALUE_ACCESSOR = { provide: forms_1.NG_VALUE_ACCESSOR, useExisting: core_1.forwardRef(function () { return CurrencyMaskDirective; }), multi: true }; var CurrencyMaskDirective = (function () { function CurrencyMaskDirective(currencyMaskConfig, elementRef, keyValueDiffers) { this.currencyMaskConfig = currencyMaskConfig; this.elementRef = elementRef; this.keyValueDiffers = keyValueDiffers; this.options = {}; this.optionsTemplate = { align: "right", allowNegative: true, allowZero: true, decimal: ".", precision: 2, prefix: "$ ", suffix: "", thousands: "," }; if (currencyMaskConfig) { this.optionsTemplate = currencyMaskConfig; } this.keyValueDiffer = keyValueDiffers.find({}).create(null); } CurrencyMaskDirective.prototype.ngAfterViewInit = function () { this.elementRef.nativeElement.style.textAlign = this.options.align ? this.options.align : this.optionsTemplate.align; }; CurrencyMaskDirective.prototype.ngDoCheck = function () { if (this.keyValueDiffer.diff(this.options)) { this.elementRef.nativeElement.style.textAlign = this.options.align ? this.options.align : this.optionsTemplate.align; this.inputHandler.updateOptions(Object.assign({}, this.optionsTemplate, this.options)); } }; CurrencyMaskDirective.prototype.ngOnInit = function () { this.inputHandler = new input_handler_1.InputHandler(this.elementRef.nativeElement, Object.assign({}, this.optionsTemplate, this.options)); }; CurrencyMaskDirective.prototype.handleBlur = function (event) { this.inputHandler.getOnModelTouched().apply(event); }; CurrencyMaskDirective.prototype.handleCut = function (event) { if (!this.isChromeAndroid()) { this.inputHandler.handleCut(event); } }; CurrencyMaskDirective.prototype.handleInput = function (event) { if (this.isChromeAndroid()) { this.inputHandler.handleInput(event, this.isSamsungChromeAndroid()); } }; CurrencyMaskDirective.prototype.handleKeydown = function (event) { if (!this.isChromeAndroid()) { this.inputHandler.handleKeydown(event); } }; CurrencyMaskDirective.prototype.handleKeypress = function (event) { if (!this.isChromeAndroid()) { this.inputHandler.handleKeypress(event); } }; CurrencyMaskDirective.prototype.handlePaste = function (event) { if (!this.isChromeAndroid()) { this.inputHandler.handlePaste(event); } }; CurrencyMaskDirective.prototype.isChromeAndroid = function () { return /chrome/i.test(navigator.userAgent) && /android/i.test(navigator.userAgent); }; CurrencyMaskDirective.prototype.isSamsungChromeAndroid = function () { return navigator.userAgent.match(/SAMSUNG|SGH-|GT-[I|P|N]|SM-|SHV-E|SCH-|SPH-L/i) ? true : false; }; CurrencyMaskDirective.prototype.registerOnChange = function (callbackFunction) { this.inputHandler.setOnModelChange(callbackFunction); }; CurrencyMaskDirective.prototype.registerOnTouched = function (callbackFunction) { this.inputHandler.setOnModelTouched(callbackFunction); }; CurrencyMaskDirective.prototype.setDisabledState = function (value) { this.elementRef.nativeElement.disabled = value; }; CurrencyMaskDirective.prototype.writeValue = function (value) { this.inputHandler.setValue(value); }; return CurrencyMaskDirective; }()); CurrencyMaskDirective.decorators = [ { type: core_1.Directive, args: [{ selector: "[currencyMask]", providers: [exports.CURRENCYMASKDIRECTIVE_VALUE_ACCESSOR] },] }, ]; /** @nocollapse */ CurrencyMaskDirective.ctorParameters = function () { return [ { type: undefined, decorators: [{ type: core_1.Optional }, { type: core_1.Inject, args: [currency_mask_config_1.CURRENCY_MASK_CONFIG,] },] }, { type: core_1.ElementRef, }, { type: core_1.KeyValueDiffers, }, ]; }; CurrencyMaskDirective.propDecorators = { 'options': [{ type: core_1.Input },], 'handleBlur': [{ type: core_1.HostListener, args: ["blur", ["$event"],] },], 'handleCut': [{ type: core_1.HostListener, args: ["cut", ["$event"],] },], 'handleInput': [{ type: core_1.HostListener, args: ["input", ["$event"],] },], 'handleKeydown': [{ type: core_1.HostListener, args: ["keydown", ["$event"],] },], 'handleKeypress': [{ type: core_1.HostListener, args: ["keypress", ["$event"],] },], 'handlePaste': [{ type: core_1.HostListener, args: ["paste", ["$event"],] },], }; exports.CurrencyMaskDirective = CurrencyMaskDirective; //# sourceMappingURL=currency-mask.directive.js.map