Skip to content

Commit

Permalink
fix(behaviour): Fix issues with functional behaviours
Browse files Browse the repository at this point in the history
* remove issue with event propagation from the list
* country update issue with displaying PL instance
* account country update issue
* add settings link to plugins list view
  • Loading branch information
mfilip committed May 22, 2024
1 parent 67c0b3e commit 8298db1
Show file tree
Hide file tree
Showing 13 changed files with 179 additions and 40 deletions.
2 changes: 1 addition & 1 deletion cypress/integration/005_country.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ describe("Country watching", () => {
});

describe("when enabled", () => {
it("disables address finder if non-uk country selected", function () {
it.skip("disables address finder if non-uk country selected", function () {
cy.visit("/");
cy.get("a").contains("My account").click({ force: true });
cy.get("a").contains("Addresses").click({ force: true });
Expand Down
2 changes: 1 addition & 1 deletion lib/account_billing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export const selectors = {
post_town: "#billing_city",
county: "#billing_state",
postcode: "#billing_postcode",
organisation_name: "#billing_company",
organisation: "#billing_company",
country: "#billing_country",
};

Expand Down
2 changes: 1 addition & 1 deletion lib/account_shipping.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export const selectors = {
post_town: "#shipping_city",
county: "#shipping_state",
postcode: "#shipping_postcode",
organisation_name: "#shipping_company",
organisation: "#shipping_company",
country: "#shipping_country",
};

Expand Down
8 changes: 4 additions & 4 deletions lib/admin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const orderBilling = {
post_town: "#_billing_city",
county: "#_billing_state",
postcode: "#_billing_postcode",
organisation_name: "#_billing_company",
organisation: "#_billing_company",
country: "#_billing_country",
};

Expand All @@ -20,7 +20,7 @@ const orderShipping = {
post_town: "#_shipping_city",
county: "#_shipping_state",
postcode: "#_shipping_postcode",
organisation_name: "#_shipping_company",
organisation: "#_shipping_company",
country: "#_shipping_country",
};

Expand All @@ -31,7 +31,7 @@ const userBilling = {
post_town: "#billing_city",
county: "#billing_state",
postcode: "#billing_postcode",
organisation_name: "#billing_company",
organisation: "#billing_company",
country: "#billing_country",
};

Expand All @@ -41,7 +41,7 @@ const userShipping = {
post_town: "#shipping_city",
county: "#shipping_state",
postcode: "#shipping_postcode",
organisation_name: "#shipping_company",
organisation: "#shipping_company",
country: "#shipping_country",
};

Expand Down
2 changes: 1 addition & 1 deletion lib/checkout_billing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export const selectors = {
post_town: "#billing_city",
county: "#billing_state",
postcode: "#billing_postcode",
organisation_name: "#billing_company",
organisation: "#billing_company",
country: "#billing_country",
};

Expand Down
2 changes: 1 addition & 1 deletion lib/checkout_blocks_billing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export const selectors = {
post_town: "#billing-city",
county: "#billing-state",
postcode: "#billing-postcode",
organisation_name: "#billing-company",
organisation: "#billing-company",
country: "#billing-country input",
};

Expand Down
2 changes: 1 addition & 1 deletion lib/checkout_blocks_shipping.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export const selectors = {
post_town: "#shipping-city",
county: "#shipping-state",
postcode: "#shipping-postcode",
organisation_name: "#shipping-company",
organisation: "#shipping-company",
country: "#shipping-country input",
};

Expand Down
2 changes: 1 addition & 1 deletion lib/checkout_shipping.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export const selectors = {
post_town: "#shipping_city",
county: "#shipping_state",
postcode: "#shipping_postcode",
organisation_name: "#shipping_company",
organisation: "#shipping_company",
country: "#shipping_country",
};

Expand Down
179 changes: 152 additions & 27 deletions lib/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,29 @@ import {
isString,
OutputFields,
hide,
show,
toHtmlElem,
isInput,
update,
setupBind,
insertBefore,
Targets,
getParent,
AnyAddress
AnyAddress,
loaded,
markLoaded,
toArray,
} from "@ideal-postcodes/jsutil";

if (!window.IdealPostcodes) window.IdealPostcodes = {};
window.IdealPostcodes.AddressFinder = AddressFinder;
window.IdealPostcodes.PostcodeLookup = PostcodeLookup;

const isSupported = (c: string | null): boolean =>
["GB", "IM", "JE", "GG"].indexOf(c || "") !== -1;
const isSupported = (c: string | null): boolean => {
if(c === null) return false;
if(c.length > 2) {
return [ "United Kingdom (UK)", "Isle of Man", "Jersey", "Guernsey" ].indexOf(c || "") !== -1;
}
return [ "GB", "IM", "JE", "GG" ].indexOf(c || "") !== -1;
}

const isDiv = (e: HTMLElement | null): e is HTMLDivElement => {
if (e === null) return false;
Expand Down Expand Up @@ -242,9 +248,73 @@ interface WooConfig extends Config {
contextClass?: string;
}

const getAnchors = (selector: string): HTMLElement[] => {
const matches = window.document.querySelectorAll(selector);
const anchors = toArray(matches).filter((e) => !loaded(e));
if (anchors.length === 0) return [];
anchors.forEach((anchor) => markLoaded(anchor));
return anchors;
};

interface PageBindings {
anchor: HTMLElement;
targets: Targets;
parent: HTMLElement;
}

interface SetupOptions {
selectors: Selectors
}

interface Setup {
(options: SetupOptions): PageBindings[];
}

const getTargets = (
parent: HTMLElement,
selectors: Selectors
): Targets | null => {
const line_1: HTMLElement | null = toHtmlElem(parent, selectors.line_1);
const post_town: HTMLElement | null = toHtmlElem(parent, selectors.post_town);
const postcode: HTMLElement | null = toHtmlElem(parent, selectors.postcode);
const line_2: HTMLElement | null = toHtmlElem(parent, selectors.line_2);
const line_3: HTMLElement | null = toHtmlElem(parent, selectors.line_3);
const country: HTMLElement | null = toHtmlElem(parent, selectors.country);
const county: HTMLElement | null = toHtmlElem(parent, selectors.county);
const organisation: HTMLElement | null = toHtmlElem(
parent,
selectors.organisation
);

return {
line_1,
line_2,
line_3,
post_town,
county,
postcode,
organisation,
country,
};
};

const setup: Setup = ({ selectors }) => {
const anchors = getAnchors(selectors.line_1);
return anchors.reduce<PageBindings[]>((prev, anchor) => {
const parent = getParent(anchor, "form");
if (!parent) return prev;

const targets = getTargets(parent, selectors);
if (targets === null) return prev;

prev.push({ targets, parent, anchor });
return prev;
}, []);
}

export const newBind = (selectors: Selectors, blocks: boolean = false) => (config: WooConfig) => {
if (config.enabled !== true) return;
const pageBindings = setupBind({ selectors });
const pageBindings = setup({ selectors });
const outputFields = toOutputFields(config, selectors);

pageBindings.forEach((binding) => {
Expand Down Expand Up @@ -281,6 +351,9 @@ export const newBind = (selectors: Selectors, blocks: boolean = false) => (confi
update(county, address.county_code);
}
}
//ensure blur on population for better reopen search after click
//@ts-expect-error
setTimeout(() => this.input.blur(), 200);
}
}
updateCheckout();
Expand Down Expand Up @@ -324,38 +397,90 @@ export const newBind = (selectors: Selectors, blocks: boolean = false) => (confi
}
});
}

let country = (targets.country as HTMLSelectElement) || null;
if(isDiv(country)) {
//@ts-expect-error
country = country.querySelector("input");
}

const checkCountry = () => {
if (isSupported(country.value)) {
if (plContainer) plContainer.style.display = blocks ? "flex" : "block";
} else {
if (plContainer) hide(plContainer);
}
};

country?.addEventListener("change", checkCountry);

const handleValueChange = () => {
country.dispatchEvent(new Event("change", { bubbles: true }))
}

let oldValue = country.value;
// Create a MutationObserver to detect changes
const observer = new MutationObserver((mutations) => {
mutations.forEach((mutation) => {
if (mutation.type === 'attributes' && mutation.attributeName === 'value') {
const newValue = country.value;
if (newValue !== oldValue) {
oldValue = newValue;
handleValueChange();
}
}
});
});

// Configure the observer
observer.observe(country, {
attributes: true
});
//observer for sudo select etc in accounts
const shadowSelectSpan = document.querySelector(`#select2-${selectors.country.replace(/#|\./g, "")}-container`);
if(shadowSelectSpan !== null) {
let oldValue = shadowSelectSpan.getAttribute("title");
// Create a MutationObserver to detect changes
const observerShadow = new MutationObserver((mutations) => {
mutations.forEach((mutation) => {
if (mutation.type === 'attributes' && mutation.attributeName === 'title') {
const newValue = shadowSelectSpan.getAttribute("title");
if (newValue !== oldValue) {
oldValue = newValue;
handleValueChange();
}
}
});
});
observerShadow.observe(shadowSelectSpan, {
attributes: true
});
}

checkCountry();
}

let f: FinderContainer | null;
if (config.autocomplete) {
f = config.separateFinder ? insertAddressFinder(targets) : null;
const af = AddressFinder.setup({
//injectStyle: false, // To be dropped in breaking change release
AddressFinder.setup({
...config,
autocomplete: AddressFinder.defaults.autocomplete, // Temporary fix for clash
...localConfig,
inputField: f ? f.input : selectors.line_1,
...config.autocompleteOverride,
});

const country = (targets.country as HTMLSelectElement) || null;

const checkCountry = () => {
if (isSupported(country.value)) {
if (plContainer) show(plContainer);
if (f) show(f.elem);
if (af) af.attach();
} else {
if (plContainer) hide(plContainer);
if (f) hide(f.elem);
if (af) af.detach();
onMounted: function() {
if(config.autocompleteOverride.onMounted !== undefined
&& typeof config.autocompleteOverride.onMounted === "function") {
config.autocompleteOverride.onMounted.call(this);
}
this.list.addEventListener("mousedown", (e) => {
e.preventDefault();
e.stopImmediatePropagation();
e.stopPropagation();
});
}
};

if (config.watchCountry && country) {
(window.jQuery as any)(country).change(checkCountry);
checkCountry();
}
});
}
});
};
2 changes: 1 addition & 1 deletion uk-address-postcode-validation/js/admin-woocommerce.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion uk-address-postcode-validation/js/woocommerce.min.js

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions uk-address-postcode-validation/readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@ Prices range between 2 and 2.5p per lookup, with alternate pricing options avail

== Changelog ==

= 3.5.5 =
* Fix malfunctioning event propagations
* Improve visibility of Address Finder for multinational forms

= 3.5.4 =
* Improve Postcode Lookup layout on WooCommerce blocks checkout

Expand Down
10 changes: 10 additions & 0 deletions uk-address-postcode-validation/uk-address-postcode-validation.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,16 @@
//includes
require_once ABSPATH . "wp-admin/includes/plugin.php";

add_filter( 'plugin_action_links_' . plugin_basename(__FILE__), 'add_action_links' );

function add_action_links ( $actions ) {
$mylinks = array(
'<a href="/wp-admin/admin.php?page=wc-settings&tab=integration&section=idealpostcodes">Settings</a>',
);
$actions = array_merge( $actions, $mylinks );
return $actions;
}

if (!class_exists("WC_IdealPostcodes")) {

add_action( 'before_woocommerce_init', function() {
Expand Down

0 comments on commit 8298db1

Please sign in to comment.