ngx-currency-mask
Version:
A very simple currency mask directive that allows using a number attribute with the ngModel.
122 lines • 5.38 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var input_service_1 = require("./input.service");
var InputHandler = (function () {
function InputHandler(htmlInputElement, options) {
this.inputService = new input_service_1.InputService(htmlInputElement, options);
}
InputHandler.prototype.handleCut = function (event) {
var _this = this;
setTimeout(function () {
_this.inputService.updateFieldValue();
_this.setValue(_this.inputService.value);
_this.onModelChange(_this.inputService.value);
}, 0);
};
//TODO: Check if is possible to get selectionEnd instead selectionStart in all devices.
InputHandler.prototype.handleInput = function (event, isGalaxy) {
var keyCode = this.inputService.rawValue.charCodeAt(this.inputService.rawValue.length - 1);
var rawValueLength = this.inputService.rawValue.length;
var rawValueSelectionStart = isGalaxy ? this.inputService.inputSelection.selectionEnd : this.inputService.inputSelection.selectionStart;
var storedRawValueLength = this.inputService.storedRawValue.length;
this.inputService.rawValue = this.inputService.storedRawValue;
if (rawValueLength != rawValueSelectionStart || Math.abs(rawValueLength - storedRawValueLength) != 1) {
this.setCursorPosition(event);
return;
}
if (rawValueLength < storedRawValueLength) {
this.inputService.removeNumber(8);
}
if (rawValueLength > storedRawValueLength) {
switch (keyCode) {
case 43:
this.inputService.changeToPositive();
break;
case 45:
this.inputService.changeToNegative();
break;
default:
if (!this.inputService.canInputMoreNumbers) {
return;
}
this.inputService.addNumber(keyCode);
}
}
this.setCursorPosition(event);
this.onModelChange(this.inputService.value);
};
InputHandler.prototype.handleKeydown = function (event) {
var keyCode = event.which || event.charCode || event.keyCode;
if (keyCode == 8 || keyCode == 46 || keyCode == 63272) {
event.preventDefault();
var selectionRangeLength = Math.abs(this.inputService.inputSelection.selectionEnd - this.inputService.inputSelection.selectionStart);
if (selectionRangeLength == 0) {
this.inputService.removeNumber(keyCode);
this.onModelChange(this.inputService.value);
}
if (selectionRangeLength == this.inputService.rawValue.length) {
this.setValue(0);
this.onModelChange(this.inputService.value);
}
}
};
InputHandler.prototype.handleKeypress = function (event) {
var keyCode = event.which || event.charCode || event.keyCode;
switch (keyCode) {
case undefined:
case 9:
case 13:
return;
case 43:
this.inputService.changeToPositive();
break;
case 45:
this.inputService.changeToNegative();
break;
default:
if (this.inputService.canInputMoreNumbers) {
var selectionRangeLength = Math.abs(this.inputService.inputSelection.selectionEnd - this.inputService.inputSelection.selectionStart);
if (selectionRangeLength == this.inputService.rawValue.length) {
this.setValue(0);
}
this.inputService.addNumber(keyCode);
}
}
event.preventDefault();
this.onModelChange(this.inputService.value);
};
InputHandler.prototype.handlePaste = function (event) {
var _this = this;
setTimeout(function () {
_this.inputService.updateFieldValue();
_this.setValue(_this.inputService.value);
_this.onModelChange(_this.inputService.value);
}, 1);
};
InputHandler.prototype.updateOptions = function (options) {
this.inputService.updateOptions(options);
};
InputHandler.prototype.getOnModelChange = function () {
return this.onModelChange;
};
InputHandler.prototype.setOnModelChange = function (callbackFunction) {
this.onModelChange = callbackFunction;
};
InputHandler.prototype.getOnModelTouched = function () {
return this.onModelTouched;
};
InputHandler.prototype.setOnModelTouched = function (callbackFunction) {
this.onModelTouched = callbackFunction;
};
InputHandler.prototype.setValue = function (value) {
this.inputService.value = value;
};
InputHandler.prototype.setCursorPosition = function (event) {
setTimeout(function () {
event.target.setSelectionRange(event.target.value.length, event.target.value.length);
}, 0);
};
return InputHandler;
}());
exports.InputHandler = InputHandler;
//# sourceMappingURL=input.handler.js.map