UNPKG

ngx-currency-mask

Version:

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

102 lines 4.81 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var InputManager = (function () { function InputManager(htmlInputElement) { this.htmlInputElement = htmlInputElement; } InputManager.prototype.setCursorAt = function (position) { if (this.htmlInputElement.setSelectionRange) { this.htmlInputElement.focus(); this.htmlInputElement.setSelectionRange(position, position); } else if (this.htmlInputElement.createTextRange) { var textRange = this.htmlInputElement.createTextRange(); textRange.collapse(true); textRange.moveEnd("character", position); textRange.moveStart("character", position); textRange.select(); } }; InputManager.prototype.updateValueAndCursor = function (newRawValue, oldLength, selectionStart) { this.rawValue = newRawValue; var newLength = newRawValue.length; selectionStart = selectionStart - (oldLength - newLength); this.setCursorAt(selectionStart); }; Object.defineProperty(InputManager.prototype, "canInputMoreNumbers", { get: function () { var haventReachedMaxLength = !(this.rawValue.length >= this.htmlInputElement.maxLength && this.htmlInputElement.maxLength >= 0); var selectionStart = this.inputSelection.selectionStart; var selectionEnd = this.inputSelection.selectionEnd; var haveNumberSelected = (selectionStart != selectionEnd && this.htmlInputElement.value.substring(selectionStart, selectionEnd).match(/\d/)) ? true : false; var startWithZero = (this.htmlInputElement.value.substring(0, 1) == "0"); return haventReachedMaxLength || haveNumberSelected || startWithZero; }, enumerable: true, configurable: true }); Object.defineProperty(InputManager.prototype, "inputSelection", { get: function () { var selectionStart = 0; var selectionEnd = 0; if (typeof this.htmlInputElement.selectionStart == "number" && typeof this.htmlInputElement.selectionEnd == "number") { selectionStart = this.htmlInputElement.selectionStart; selectionEnd = this.htmlInputElement.selectionEnd; } else { var range = document.selection.createRange(); if (range && range.parentElement() == this.htmlInputElement) { var lenght = this.htmlInputElement.value.length; var normalizedValue = this.htmlInputElement.value.replace(/\r\n/g, "\n"); var startRange = this.htmlInputElement.createTextRange(); startRange.moveToBookmark(range.getBookmark()); var endRange = this.htmlInputElement.createTextRange(); endRange.collapse(false); if (startRange.compareEndPoints("StartToEnd", endRange) > -1) { selectionStart = selectionEnd = lenght; } else { selectionStart = -startRange.moveStart("character", -lenght); selectionStart += normalizedValue.slice(0, selectionStart).split("\n").length - 1; if (startRange.compareEndPoints("EndToEnd", endRange) > -1) { selectionEnd = lenght; } else { selectionEnd = -startRange.moveEnd("character", -lenght); selectionEnd += normalizedValue.slice(0, selectionEnd).split("\n").length - 1; } } } } return { selectionStart: selectionStart, selectionEnd: selectionEnd }; }, enumerable: true, configurable: true }); Object.defineProperty(InputManager.prototype, "rawValue", { get: function () { return this.htmlInputElement && this.htmlInputElement.value; }, set: function (value) { this._storedRawValue = value; if (this.htmlInputElement) { this.htmlInputElement.value = value; } }, enumerable: true, configurable: true }); Object.defineProperty(InputManager.prototype, "storedRawValue", { get: function () { return this._storedRawValue; }, enumerable: true, configurable: true }); return InputManager; }()); exports.InputManager = InputManager; //# sourceMappingURL=input.manager.js.map