* Initialize the AutoNumeric object onto the given DOM element, and attach the settings and related event listeners to it. * The options passed as a parameter is an object that contains the settings (i.e. {digitGroupSeparator: ".", decimalCharacter: ",", currencySymbol: '€ '}) * * @ex
(arg1 = null, arg2 = null, arg3 = null)
| 113 | * @throws |
| 114 | */ |
| 115 | constructor(arg1 = null, arg2 = null, arg3 = null) { |
| 116 | // -------------------------------------------------------- |
| 117 | // -------------- Initialization |
| 118 | // Initialize the arguments |
| 119 | const { domElement, initialValue, userOptions } = AutoNumeric._setArgumentsValues(arg1, arg2, arg3); |
| 120 | |
| 121 | // Initialize the element |
| 122 | this.domElement = domElement; |
| 123 | |
| 124 | // Generate the settings |
| 125 | this.defaultRawValue = ''; // The default raw value to set when initializing an AutoNumeric object |
| 126 | this._setSettings(userOptions, false); |
| 127 | //TODO If `styleRules` is not null, add by default a class 'autoNumeric' that adds transition to color, background-color, border-color properties |
| 128 | // Check if the DOM element is supported |
| 129 | this._checkElement(); |
| 130 | |
| 131 | // Store the additional attributes inside the AutoNumeric object |
| 132 | // Note: This variable is needed and not a duplicate of `initialValueOnFirstKeydown` nor `valueOnFocus` since it serves a different purpose and has a different lifecycle |
| 133 | this.savedCancellableValue = null; |
| 134 | |
| 135 | // Initialize the undo/redo variables |
| 136 | /** @typedef {{ value: string|null|number, start: number, end: string }} HistoryTableEntry */ // TODO can rawValue be number? |
| 137 | /** @type {Array<HistoryTableEntry>} */ |
| 138 | this.historyTable = []; // Keep track of *all* valid states of the element value |
| 139 | this.historyTableIndex = -1; // Pointer to the current undo/redo state. This will be set to '0' during initialization since it first adds itself. |
| 140 | this.onGoingRedo = false; // Variable that keeps track if a 'redo' is ongoing (in order to prevent an 'undo' to be launch when releasing the shift key before the ctrl key after a 'redo' shortcut) |
| 141 | |
| 142 | // Initialize the parent form element, if any |
| 143 | this.parentForm = this._getParentForm(); |
| 144 | |
| 145 | // Set the initial value if it exists and if the `formatOnPageLoad` option will allow it |
| 146 | if (!this.runOnce && this.settings.formatOnPageLoad) { |
| 147 | // Format the element value if needed |
| 148 | this._formatDefaultValueOnPageLoad(initialValue); |
| 149 | } else { |
| 150 | // Otherwise set the `rawValue` and the element value, but do not format the latter yet |
| 151 | let valueToSet; |
| 152 | if (AutoNumericHelper.isNull(initialValue)) { |
| 153 | switch (this.settings.emptyInputBehavior) { |
| 154 | case AutoNumeric.options.emptyInputBehavior.min: |
| 155 | valueToSet = this.settings.minimumValue; |
| 156 | break; |
| 157 | case AutoNumeric.options.emptyInputBehavior.max: |
| 158 | valueToSet = this.settings.maximumValue; |
| 159 | break; |
| 160 | case AutoNumeric.options.emptyInputBehavior.zero: |
| 161 | valueToSet = '0'; |
| 162 | break; |
| 163 | case AutoNumeric.options.emptyInputBehavior.focus: |
| 164 | case AutoNumeric.options.emptyInputBehavior.press: |
| 165 | case AutoNumeric.options.emptyInputBehavior.always: |
| 166 | valueToSet = ''; |
| 167 | break; |
| 168 | // It's possible to set the `null` value as the initial value |
| 169 | case AutoNumeric.options.emptyInputBehavior.null: |
| 170 | valueToSet = null; |
| 171 | break; |
| 172 | // When `emptyInputBehavior` is a number or a string representing a number |
nothing calls this directly
no test coverage detected