MCPcopy
hub / github.com/autoNumeric/autoNumeric / _formatDefaultValueOnPageLoad

Method _formatDefaultValueOnPageLoad

src/AutoNumeric.js:7964–8062  ·  view source on GitHub ↗

* Formats the default value on page load. * This is called only if the `formatOnPageLoad` option is set to `true`. * * @param {number|string|null} forcedInitialValue The value that should be used for initialization, in place on the eventual html one

(forcedInitialValue = null)

Source from the content-addressed store, hash-verified

7962 * @param {number|string|null} forcedInitialValue The value that should be used for initialization, in place on the eventual html one
7963 */
7964 _formatDefaultValueOnPageLoad(forcedInitialValue = null) {
7965 let setValue = true;
7966 let currentValue;
7967 if (!AutoNumericHelper.isNull(forcedInitialValue)) {
7968 currentValue = forcedInitialValue;
7969 } else {
7970 // Make sure the initial value does not have any superfluous whitespaces around it (Fix issue #479)
7971 currentValue = AutoNumericHelper.getElementValue(this.domElement).trim();
7972 // Correct the DOM attribute in case some whitespaces were present
7973 this.domElement.setAttribute('value', currentValue);
7974 }
7975
7976 if (this.isInputElement || this.isContentEditable) {
7977 /*
7978 * If the input value has been set by the dev, but not directly as an attribute in the html, then it takes
7979 * precedence and should get formatted during the initialization (if this input value is a valid number and that the
7980 * developer wants it formatted on init (cf. the `settings.formatOnPageLoad` option)).
7981 * Note; this is true whatever the developer has set for `data-default-value-override` in the html (asp.net users).
7982 *
7983 * In other words : if `defaultValueOverride` is not null, it means the developer is trying to prevent postback problems.
7984 * But if `input.value` is set to a number, and the html `value` attribute is not set, then it means the dev has
7985 * changed the input value, and then it means we should not overwrite his own decision to do so.
7986 * Hence, if `defaultValueOverride` is not null, but `input.value` is a number and `this.domElement.hasAttribute('value')`
7987 * is false, we should ignore `defaultValueOverride` altogether.
7988 */
7989 const unLocalizedCurrentValue = this.constructor._toNumericValue(currentValue, this.settings); // This allows to use a localized value on startup
7990 if (!this.domElement.hasAttribute('value') || this.domElement.getAttribute('value') === '') {
7991 // Check if the `value` is valid or not
7992 if (!isNaN(Number(unLocalizedCurrentValue)) && Infinity !== unLocalizedCurrentValue) {
7993 this.set(unLocalizedCurrentValue);
7994 setValue = false;
7995 } else {
7996 // If not, inform the developer that nothing usable has been provided
7997 AutoNumericHelper.throwError(`The value [${currentValue}] used in the input is not a valid value autoNumeric can work with.`);
7998 }
7999 } else {
8000 /* Checks for :
8001 * - page reload from back button, and
8002 * - ASP.net form post back
8003 * The following HTML data attribute is REQUIRED (data-an-default="same value as the value attribute")
8004 * example: <asp:TextBox runat="server" id="someID" text="1234.56" data-an-default="1234.56">
8005 */
8006 if ((this.settings.defaultValueOverride !== null && this.settings.defaultValueOverride.toString() !== currentValue) ||
8007 (this.settings.defaultValueOverride === null && currentValue !== '' && currentValue !== this.domElement.getAttribute('value')) ||
8008 (currentValue !== '' && this.domElement.getAttribute('type') === 'hidden' && !AutoNumericHelper.isNumber(unLocalizedCurrentValue))) {
8009 if (this.settings.saveValueToSessionStorage && (this.settings.decimalPlacesShownOnFocus !== null || this.settings.divisorWhenUnfocused)) {
8010 this._setRawValue(this._getValueFromPersistentStorage());
8011 }
8012
8013 // If the decimalPlacesShownOnFocus value should NOT be saved in sessionStorage
8014 if (!this.settings.saveValueToSessionStorage) {
8015 const toStrip = this.constructor._removeBrackets(currentValue, this.settings);
8016 if ((this.settings.negativePositiveSignPlacement === AutoNumeric.options.negativePositiveSignPlacement.suffix ||
8017 (this.settings.negativePositiveSignPlacement !== AutoNumeric.options.negativePositiveSignPlacement.prefix && this.settings.currencySymbolPlacement === AutoNumeric.options.currencySymbolPlacement.suffix)) &&
8018 this.settings.negativeSignCharacter !== '' &&
8019 AutoNumericHelper.isNegative(currentValue, this.settings.negativeSignCharacter)) {
8020 this._setRawValue(`-${this.constructor._stripAllNonNumberCharacters(toStrip, this.settings, true, this.isFocused)}`);
8021 } else {

Callers 1

constructorMethod · 0.95

Calls 12

setMethod · 0.95
_setRawValueMethod · 0.95
_setElementValueMethod · 0.95
isNullMethod · 0.80
getElementValueMethod · 0.80
_toNumericValueMethod · 0.80
throwErrorMethod · 0.80
isNumberMethod · 0.80
_removeBracketsMethod · 0.80
isNegativeMethod · 0.80

Tested by

no test coverage detected