* Set the given element value, and format it immediately. * Additionally, this `set()` method can accept options that will be merged into the current AutoNumeric element, taking precedence over any previous settings. * * @example anElement.set('12345.67') // Formats the value * @
(newValue, options = null, saveChangeToHistory = true)
| 2064 | * @throws |
| 2065 | */ |
| 2066 | set(newValue, options = null, saveChangeToHistory = true) { |
| 2067 | //TODO Add the `saveSettings` options. If `true`, then when `options` is passed, then it overwrite the current `this.settings`. If `false` the `options` are only used once and `this.settings` is not modified |
| 2068 | if (AutoNumericHelper.isUndefined(newValue)) { |
| 2069 | AutoNumericHelper.warning(`You are trying to set an 'undefined' value ; an error could have occurred.`, this.settings.showWarnings); |
| 2070 | return this; |
| 2071 | } |
| 2072 | |
| 2073 | // The options update is done only if the `newValue` is not `undefined` |
| 2074 | if (!AutoNumericHelper.isNull(options)) { |
| 2075 | this._setSettings(options, true); // We do not call `update` here since this would call `set` too |
| 2076 | } |
| 2077 | |
| 2078 | if (newValue === null && this.settings.emptyInputBehavior !== AutoNumeric.options.emptyInputBehavior.null) { |
| 2079 | AutoNumericHelper.warning(`You are trying to set the \`null\` value while the \`emptyInputBehavior\` option is set to ${this.settings.emptyInputBehavior}. If you want to be able to set the \`null\` value, you need to change the 'emptyInputBehavior' option to \`'null'\`.`, this.settings.showWarnings); |
| 2080 | return this; |
| 2081 | } |
| 2082 | |
| 2083 | let value; |
| 2084 | if (newValue === null) { |
| 2085 | //TODO Merge this into a global `if (newValue === null) {` test, with the test above |
| 2086 | // Here this.settings.emptyInputBehavior === AutoNumeric.options.emptyInputBehavior.null |
| 2087 | this._setElementAndRawValue(null, null, saveChangeToHistory); |
| 2088 | this._saveValueToPersistentStorage(); |
| 2089 | |
| 2090 | return this; |
| 2091 | } |
| 2092 | |
| 2093 | value = this.constructor._toNumericValue(newValue, this.settings); |
| 2094 | if (isNaN(Number(value))) { |
| 2095 | //TODO Do not modify the element value if the newValue results in `NaN`. Make sure the settings, if modified, are revert back too. |
| 2096 | AutoNumericHelper.warning(`The value you are trying to set results in \`NaN\`. The element value is set to the empty string instead.`, this.settings.showWarnings); |
| 2097 | this.setValue('', saveChangeToHistory); |
| 2098 | |
| 2099 | return this; |
| 2100 | } |
| 2101 | |
| 2102 | if (value === '') { |
| 2103 | switch (this.settings.emptyInputBehavior) { |
| 2104 | case AutoNumeric.options.emptyInputBehavior.zero: |
| 2105 | value = 0; |
| 2106 | break; |
| 2107 | case AutoNumeric.options.emptyInputBehavior.min: |
| 2108 | value = this.settings.minimumValue; |
| 2109 | break; |
| 2110 | case AutoNumeric.options.emptyInputBehavior.max: |
| 2111 | value = this.settings.maximumValue; |
| 2112 | break; |
| 2113 | default: |
| 2114 | if (AutoNumericHelper.isNumber(this.settings.emptyInputBehavior)) { |
| 2115 | value = Number(this.settings.emptyInputBehavior); |
| 2116 | } |
| 2117 | } |
| 2118 | } |
| 2119 | |
| 2120 | if (value !== '') { |
| 2121 | const [minTest, maxTest] = this.constructor._checkIfInRangeWithOverrideOption(value, this.settings); |
| 2122 | |
| 2123 | // Modify the formatted value if the rawValue is found in the `valuesToStrings` option |