Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
mfilip committed Oct 31, 2023
1 parent a411fc4 commit 1da1f4a
Showing 1 changed file with 70 additions and 69 deletions.
139 changes: 70 additions & 69 deletions lib/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
Country,
OutputFields,
toElem,
Targets
Targets,
} from "@ideal-postcodes/jsutil";

import { AddressFinder } from "@ideal-postcodes/address-finder";
Expand All @@ -39,19 +39,16 @@ export const hoistCountry = (
if (config.hoistCountry !== true) return;
if (!outputFields.country) return;
if (!outputFields.line_1) return;
const elem = getParent(
outputFields.country,
"div",
(e) => e.classList.contains("field")
const elem = getParent(outputFields.country, "div", (e) =>
e.classList.contains("field")
);
if (!elem) return;
const target = getLinesContainer(outputFields, linesIdentifier);
if (!target) return;
if(!elem.hasAttribute("country-hoist")) {
if (!elem.hasAttribute("country-hoist")) {
elem.setAttribute("country-hoist", "true");
insertBefore({ elem, target });
}

};

export const getLinesContainer = (
Expand All @@ -66,11 +63,7 @@ export const getLinesContainer = (
? linesIdentifier.parentTest
: (e: HTMLElement) => e.classList.contains("field");

return getParent(
line_1 as HTMLElement,
parentScope,
parentTest
);
return getParent(line_1 as HTMLElement, parentScope, parentTest);
};

type SupportedCountry = UkCountry | CountryIso | Country;
Expand All @@ -91,16 +84,13 @@ const SUPPORTED_COUNTRIES: SupportedCountry[] = [
"GG",
];

const EXTENDED_COUNTRIES: string[] =[
"United States of America",
"US"
]
const EXTENDED_COUNTRIES: string[] = ["United States of America", "US"];

export const supportedCountries = (extended: boolean): string[] => {
//@ts-expect-error
if(extended) return SUPPORTED_COUNTRIES.concat(EXTENDED_COUNTRIES);
if (extended) return SUPPORTED_COUNTRIES.concat(EXTENDED_COUNTRIES);
return SUPPORTED_COUNTRIES;
}
};

export const countryIsSupported = (
e: HTMLInputElement | HTMLSelectElement,
Expand Down Expand Up @@ -136,8 +126,7 @@ export const watchCountry = (
) => {
if (!country) return NOOP;
const checkCountry = (target: HTMLInputElement | HTMLSelectElement) => {
if (countryIsSupported(target, extended))
return activate();
if (countryIsSupported(target, extended)) return activate();
deactivate();
};
country.addEventListener("change", (event: any) => {
Expand All @@ -146,8 +135,11 @@ export const watchCountry = (
return checkCountry(country);
};

const getFields = (outputFields: OutputFields, scope: HTMLElement | Document): Targets => {
const result:any = {};
const getFields = (
outputFields: OutputFields,
scope: HTMLElement | Document | null
): Targets => {
const result: any = {};
Object.keys(outputFields).forEach((key) => {
//@ts-expect-error
result[key] = toElem(outputFields[key], scope);
Expand All @@ -163,55 +155,59 @@ export const setupPostcodeLookup = (
) => {
if (config.postcodeLookup !== true) return;
PostcodeLookup.watch(
{
apiKey: config.apiKey,
checkKey: true,
context: "div.idpc_lookup",
outputFields,
selectStyle:{
"margin-top": "5px",
"margin-bottom": "5px"
},
buttonStyle: {
"position": "absolute",
"right": 0
},
contextStyle: {
"position": "relative"
},
onLoaded () {
// Add search label
const label = addLookupLabel(this.context);
watchCountry(this.options.outputFields,() => {
{
apiKey: config.apiKey,
checkKey: true,
context: "div.idpc_lookup",
outputFields,
selectStyle: {
"margin-top": "5px",
"margin-bottom": "5px",
},
buttonStyle: {
position: "absolute",
right: 0,
},
contextStyle: {
position: "relative",
},
onLoaded() {
// Add search label
const label = addLookupLabel(this.context);
watchCountry(
this.options.outputFields,
() => {
label.hidden = false;
this.context.style.display = "block";
}, () =>
{
},
() => {
label.hidden = true;
this.context.style.display = "none"
});
},
...config.postcodeLookupOverride
this.context.style.display = "none";
}
);
},
...config.postcodeLookupOverride,
},
{
getScope: (anchor: HTMLElement) => getParent(anchor, "FORM"),
anchor: outputFields.line_2,
onAnchorFound(options) {
const { scope } = options;
const targets = getFields(outputFields, scope);
const target = getLinesContainer(targets, linesIdentifier);
//@ts-expect-error
options.config.outputFields = targets;
if (target === null) return;
hoistCountry(config, targets, linesIdentifier);
if (target.parentElement?.querySelector('.idpc_lookup[idpc="true"]'))
return;
const postcodeField = document.createElement("div");
postcodeField.className = "idpc_lookup field";
options.config.context = postcodeField;
return insertBefore({ target, elem: postcodeField });
},
{
getScope: (anchor: HTMLElement) => getParent(anchor, "FORM"),
anchor: outputFields.line_2,
onAnchorFound(options) {
const { scope } = options;
const targets = getFields(outputFields, scope);
const target = getLinesContainer(targets, linesIdentifier);
//@ts-expect-error
options.config.outputFields = targets;
if (target === null) return;
hoistCountry(config, targets, linesIdentifier);
if(target.parentElement?.querySelector('.idpc_lookup[idpc="true"]')) return;
const postcodeField = document.createElement("div");
postcodeField.className = "idpc_lookup field";
options.config.context = postcodeField;
return insertBefore({ target, elem: postcodeField });
},
...options,
}
...options,
}
);
};

Expand All @@ -232,10 +228,15 @@ export const setupAutocomplete = async (
this.options.outputFields = getFields(outputFields, this.scope);
//@ts-expect-error
hoistCountry(config, this.options.outputFields, linesIdentifier);
watchCountry(this.options.outputFields, () => this.attach(), () => this.detach(), true);
watchCountry(
this.options.outputFields,
() => this.attach(),
() => this.detach(),
true
);
},
outputFields,
...config.autocompleteOverride
...config.autocompleteOverride,
},
options
);
Expand Down

0 comments on commit 1da1f4a

Please sign in to comment.