MCPcopy
hub / github.com/autoNumeric/autoNumeric / _onFocusInAndMouseEnter

Method _onFocusInAndMouseEnter

src/AutoNumeric.js:6331–6411  ·  view source on GitHub ↗

* Handler for 'focusin' and 'mouseenter' events * On focusin, multiple things happens : * - If `Alt` is pressed, unformat * - Remove the separators if `showOnlyNumbersOnFocus` is set * - Depending on `emptyInputBehavior`, reformat the empty formatted value * - Display the co

(e)

Source from the content-addressed store, hash-verified

6329 * @private
6330 */
6331 _onFocusInAndMouseEnter(e) {
6332 //TODO Create separate handlers for the focus and mouseenter events
6333 this.isEditing = false; // Just in case no `keyUp` event have been sent (i.e. if the user lost the focus from the current window while typing)
6334
6335 if (!this.formulaMode && this.settings.unformatOnHover && e.type === 'mouseenter' && e.altKey) {
6336 this.constructor._unformatAltHovered(this);
6337
6338 return;
6339 }
6340
6341 if (e.type === 'focus') { //TODO Move that back to the 'focus' event handler when the separation between the 'focus' and 'mouseenter' handler will be done
6342 // Keep track if the element is currently focused
6343 this.isFocused = true;
6344 this.rawValueOnFocus = this.rawValue; // Keep track of the initial rawValue. This is needed to define if a change event must be dispatched later
6345 }
6346
6347 if (e.type === 'focus' && this.settings.unformatOnHover && this.hoveredWithAlt) {
6348 this.constructor._reformatAltHovered(this);
6349 }
6350
6351 if (e.type === 'focus' || e.type === 'mouseenter' && !this.isFocused) {
6352 let elementValueToSet = null; // Store the value we want to set on the element, and only call `_setElementValue()` once
6353
6354 if (this.settings.emptyInputBehavior === AutoNumeric.options.emptyInputBehavior.focus &&
6355 this.rawValue < 0 && this.settings.negativeBracketsTypeOnBlur !== null && this.settings.isNegativeSignAllowed) { //FIXME this is called a second time in _addGroupSeparators too. Prevent this, if possible.
6356 // Only remove the brackets if the value is negative
6357 elementValueToSet = this.constructor._removeBrackets(AutoNumericHelper.getElementValue(this.domElement), this.settings);
6358 //FIXME The element value is set here, why continue and set it again later in that same parent logic block?
6359 }
6360
6361 // Use the `rawValue`, multiplied by `rawValueDivisor` if defined
6362 const rawValueToFormat = this._getRawValueToFormat(this.rawValue);
6363
6364 // Modify the element value according to the number of decimal places to show on focus or the `showOnlyNumbersOnFocus` option
6365 if (rawValueToFormat !== '') {
6366 // Round the given value according to the object state (focus/unfocused)
6367 const roundedValue = this.constructor._roundFormattedValueShownOnFocusOrBlur(rawValueToFormat, this.settings, this.isFocused);
6368
6369 if (this.settings.showOnlyNumbersOnFocus === AutoNumeric.options.showOnlyNumbersOnFocus.onlyNumbers) {
6370 //TODO Use a `this.settingsOverride` object instead of modifying the `this.settings` object
6371 this.settings.digitGroupSeparator = '';
6372 this.settings.currencySymbol = '';
6373 this.settings.suffixText = '';
6374 elementValueToSet = roundedValue.replace('.', this.settings.decimalCharacter);
6375 } else {
6376 let formattedValue;
6377 if (AutoNumericHelper.isNull(roundedValue)) {
6378 formattedValue = '';
6379 } else {
6380 formattedValue = this.constructor._addGroupSeparators(roundedValue.replace('.', this.settings.decimalCharacter), this.settings, this.isFocused, rawValueToFormat);
6381 }
6382
6383 elementValueToSet = formattedValue;
6384 }
6385 }
6386
6387 // In order to send a 'native' change event when blurring the input, we need to first store the initial input value on focus.
6388 if (AutoNumericHelper.isNull(elementValueToSet)) {

Callers 1

_createEventListenersMethod · 0.95

Tested by

no test coverage detected