MCPcopy
hub / github.com/autoNumeric/autoNumeric / _initialCaretPosition

Method _initialCaretPosition

src/AutoNumeric.js:5514–5694  ·  view source on GitHub ↗

* Calculate where to put the caret position on focus if the element content is not selected. * This calculation is affected by the `caretPositionOnFocus` option which can be either `null`, `'start'`, `'end'`, `'decimalLeft'` or 'decimalRight'`, and will decide where to put the caret (on the lef

(value)

Source from the content-addressed store, hash-verified

5512 * @private
5513 */
5514 _initialCaretPosition(value) {
5515 if (AutoNumericHelper.isNull(this.settings.caretPositionOnFocus) &&
5516 this.settings.selectOnFocus === AutoNumeric.options.selectOnFocus.doNotSelect) {
5517 AutoNumericHelper.throwError('`_initialCaretPosition()` should never be called when the `caretPositionOnFocus` option is `null`.');
5518 }
5519
5520 const isValueNegative = this.rawValue < 0;
5521 const isZeroOrHasNoValue = AutoNumericHelper.isZeroOrHasNoValue(value);
5522 const totalLength = value.length;
5523
5524 let valueSize = 0;
5525 let integerSize = 0;
5526 let hasDecimalChar = false;
5527 let offsetDecimalChar = 0;
5528 if (this.settings.caretPositionOnFocus !== AutoNumeric.options.caretPositionOnFocus.start) {
5529 value = value.replace(this.settings.negativeSignCharacter, '');
5530 value = value.replace(this.settings.positiveSignCharacter, '');
5531 value = value.replace(this.settings.currencySymbol, '');
5532 valueSize = value.length;
5533 hasDecimalChar = AutoNumericHelper.contains(value, this.settings.decimalCharacter);
5534
5535 if (this.settings.caretPositionOnFocus === AutoNumeric.options.caretPositionOnFocus.decimalLeft ||
5536 this.settings.caretPositionOnFocus === AutoNumeric.options.caretPositionOnFocus.decimalRight) {
5537 if (hasDecimalChar) {
5538 integerSize = value.indexOf(this.settings.decimalCharacter);
5539 offsetDecimalChar = this.settings.decimalCharacter.length;
5540 } else {
5541 integerSize = valueSize;
5542 offsetDecimalChar = 0;
5543 }
5544 }
5545 }
5546
5547 let signToUse = '';
5548 if (isValueNegative) {
5549 signToUse = this.settings.negativeSignCharacter;
5550 } else if (this.settings.showPositiveSign && !isZeroOrHasNoValue) {
5551 signToUse = this.settings.positiveSignCharacter;
5552 }
5553
5554 const positiveNegativeSignSize = signToUse.length;
5555 const currencySymbolSize = this.settings.currencySymbol.length;
5556
5557 // Calculate the caret position based on `currencySymbolPlacement`, `negativePositiveSignPlacement` and `caretPositionOnFocus`
5558 let caretPosition;
5559 if (this.settings.currencySymbolPlacement === AutoNumeric.options.currencySymbolPlacement.prefix) {
5560 if (this.settings.caretPositionOnFocus === AutoNumeric.options.caretPositionOnFocus.start) {
5561 if (this.settings.negativePositiveSignPlacement !== AutoNumeric.options.negativePositiveSignPlacement.none &&
5562 (isValueNegative || (!isValueNegative && this.settings.showPositiveSign && !isZeroOrHasNoValue))) {
5563 switch (this.settings.negativePositiveSignPlacement) {
5564 case AutoNumeric.options.negativePositiveSignPlacement.prefix: // +€|12.34
5565 case AutoNumeric.options.negativePositiveSignPlacement.left: // +€|12.34
5566 case AutoNumeric.options.negativePositiveSignPlacement.right: // €+|12.34
5567 caretPosition = positiveNegativeSignSize + currencySymbolSize;
5568 break;
5569 case AutoNumeric.options.negativePositiveSignPlacement.suffix: // €|12.34+
5570 caretPosition = currencySymbolSize;
5571 break;

Callers 1

_onFocusInMethod · 0.95

Calls 4

isNullMethod · 0.80
throwErrorMethod · 0.80
isZeroOrHasNoValueMethod · 0.80
containsMethod · 0.80

Tested by

no test coverage detected