Skip to content

Commit 9a756cf

Browse files
committed
refactor: move logic to shouldOpenTooltip
1 parent ba960c7 commit 9a756cf

File tree

4 files changed

+58
-51
lines changed

4 files changed

+58
-51
lines changed

dist/autofill-debug.js

Lines changed: 16 additions & 15 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/autofill.js

Lines changed: 16 additions & 15 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Form/Form.js

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import {
2121

2222
import { getInputSubtype, getInputMainType, createMatching, getInputVariant, getInputType, getMainTypeFromType } from './matching.js';
2323
import { getIconStylesAutofilled, getIconStylesBase, getIconStylesAlternate } from './inputStyles.js';
24-
import { canBeInteractedWith, getInputConfig, isFieldDecorated } from './inputTypeConfig.js';
24+
import { canBeInteractedWith, getInputConfig, isEligibleCCSubType, isFieldDecorated } from './inputTypeConfig.js';
2525

2626
import {
2727
getUnifiedExpiryDate,
@@ -531,7 +531,7 @@ class Form {
531531
* @param {boolean} shouldCheckForDecorate
532532
*/
533533
execOnInputs(fn, inputType = 'all', shouldCheckForDecorate = true) {
534-
const inputs = [...this.inputs[inputType]];
534+
const inputs = this.inputs[inputType];
535535
for (const input of inputs) {
536536
let canExecute = true;
537537
// sometimes we want to execute even if we didn't decorate
@@ -807,12 +807,15 @@ class Form {
807807
}
808808
}
809809

810-
if (this.device.globalConfig.isExtension || this.device.globalConfig.isMobileApp) {
810+
const isNotTouchedAndFilled = !this.touched.has(input) && !input.classList.contains('ddg-autofilled');
811+
const isMobileApp = this.device.globalConfig.isMobileApp;
812+
if (this.device.globalConfig.isExtension || isMobileApp) {
811813
// Don't open the tooltip on input focus whenever it's showing in-context signup
812814
if (isIncontextSignupAvailable) return false;
815+
if (isMobileApp && isNotTouchedAndFilled && isEligibleCCSubType(subtype)) return true;
813816
}
814817

815-
return !this.touched.has(input) && !input.classList.contains('ddg-autofilled');
818+
return isNotTouchedAndFilled;
816819
}
817820

818821
/**
@@ -950,15 +953,14 @@ class Form {
950953
return isLoginOrHybrid && this.device.credentialsImport.isAvailable();
951954
}
952955

953-
getFirstViableInputForType(dataType) {
954-
return [...this.inputs[dataType]].find((input) => canBeInteractedWith(input) && isPotentiallyViewable(input));
956+
getFirstViableInputForCredentials() {
957+
return [...this.inputs.credentials].find((input) => canBeInteractedWith(input) && isPotentiallyViewable(input));
955958
}
956959

957960
async promptLoginIfNeeded() {
958-
const isMobileCCForm = this.isCCForm && this.device.globalConfig.isMobileApp;
959-
if (document.visibilityState !== 'visible' || !(this.isLogin || isMobileCCForm)) return;
961+
if (document.visibilityState !== 'visible' || !this.isLogin) return;
960962

961-
const firstViableInput = this.getFirstViableInputForType('credentials') || this.getFirstViableInputForType('creditCards');
963+
const firstViableInput = this.getFirstViableInputForCredentials();
962964
const input = this.activeInput || firstViableInput;
963965
if (!input) return;
964966

@@ -981,14 +983,12 @@ class Form {
981983
const topMostElementFromPoint = document.elementFromPoint(elHCenter, elVCenter);
982984

983985
if (this.form.contains(topMostElementFromPoint)) {
984-
const dataTypeForExec = this.isCCForm ? 'creditCards' : this.isLogin ? 'credentials' : null;
985986
// Add inputs to the touched set only for the dataTypeForExec, which currentl
986-
dataTypeForExec &&
987-
this.execOnInputs((input) => {
988-
if (isPotentiallyViewable(input)) {
989-
this.touched.add(input);
990-
}
991-
}, dataTypeForExec);
987+
this.execOnInputs((input) => {
988+
if (isPotentiallyViewable(input)) {
989+
this.touched.add(input);
990+
}
991+
}, 'credentials');
992992
this.device.attachTooltip({
993993
form: this,
994994
input,

src/Form/inputTypeConfig.js

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,12 @@ const canBeAutofilled = async (input, device) => {
8888
return Boolean(canAutofill);
8989
};
9090

91+
/**
92+
* @param {string} subtype
93+
* @returns {boolean}
94+
*/
95+
const isEligibleCCSubType = (subtype) => subtype in ['cardNumber', 'cardSecurityCode'];
96+
9197
/**
9298
* A map of config objects. These help by centralising here some complexity
9399
* @type {InputTypeConfig}
@@ -155,18 +161,17 @@ const inputTypeConfig = {
155161
getIconBase: (input, form) => {
156162
const { device } = form;
157163
const subtype = getInputSubtype(input);
158-
const isValidInputSubType = subtype === 'cardNumber' || subtype === 'cardSecurityCode';
159164

160-
if (canBeInteractedWith(input) && device.globalConfig.isMobileApp && isValidInputSubType) return ddgPasswordIcons.ddgCcIconBase;
165+
if (canBeInteractedWith(input) && device.globalConfig.isMobileApp && isEligibleCCSubType(subtype))
166+
return ddgPasswordIcons.ddgCcIconBase;
161167

162168
return '';
163169
},
164170
getIconFilled: (input, form) => {
165171
const { device } = form;
166172
const subtype = getInputSubtype(input);
167-
const isValidInputSubType = subtype === 'cardNumber' || subtype === 'cardSecurityCode';
168173

169-
if (device.globalConfig.isMobileApp && isValidInputSubType) return ddgPasswordIcons.ddgCcIconFilled;
174+
if (device.globalConfig.isMobileApp && isEligibleCCSubType(subtype)) return ddgPasswordIcons.ddgCcIconFilled;
170175

171176
return '';
172177
},
@@ -234,4 +239,4 @@ const isFieldDecorated = (input) => {
234239
return input.hasAttribute(constants.ATTR_INPUT_TYPE);
235240
};
236241

237-
export { getInputConfig, getInputConfigFromType, canBeInteractedWith, isFieldDecorated };
242+
export { getInputConfig, getInputConfigFromType, canBeInteractedWith, isFieldDecorated, isEligibleCCSubType };

0 commit comments

Comments
 (0)