@countriesdb/widget 0.1.25 → 0.1.26

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -752,27 +752,47 @@
752
752
  select.innerHTML += `<option value="${defaultValue}" disabled>Error: No country select present</option>`;
753
753
  }
754
754
  }
755
+ // Remove old event handlers if they exist (for re-initialization)
756
+ const oldHandlers = state.eventHandlers.get(select);
757
+ if (oldHandlers) {
758
+ if (oldHandlers.update) {
759
+ select.removeEventListener('change', oldHandlers.update);
760
+ }
761
+ if (oldHandlers.followRelated) {
762
+ select.removeEventListener('change', oldHandlers.followRelated);
763
+ }
764
+ if (oldHandlers.followUpward) {
765
+ select.removeEventListener('change', oldHandlers.followUpward);
766
+ }
767
+ }
768
+ // Create new event handlers
769
+ const handlers = {};
755
770
  // Always dispatch an update event for user-initiated subdivision changes
756
- select.addEventListener('change', (event) => {
771
+ handlers.update = (event) => {
757
772
  if (isWidgetInitiatedEvent(event)) {
758
773
  return;
759
774
  }
760
775
  dispatchUpdateEvent(select, { type: 'subdivision', reason: 'regular' });
761
- });
776
+ };
777
+ select.addEventListener('change', handlers.update);
762
778
  // --- follow_related (forward direction) ---
763
- select.addEventListener('change', async (event) => {
779
+ handlers.followRelated = async (event) => {
764
780
  if (isWidgetInitiatedEvent(event)) {
765
781
  return;
766
782
  }
767
783
  await handleFollowRelatedFromSubdivision(select, apiKey, backendUrl, state, config.followRelated, (s, key, code) => updateSubdivisionSelect(s, key, backendUrl, state, config, code));
768
- });
784
+ };
785
+ select.addEventListener('change', handlers.followRelated);
769
786
  // --- follow_upward (reverse direction) ---
770
- select.addEventListener('change', async (event) => {
787
+ handlers.followUpward = async (event) => {
771
788
  if (isWidgetInitiatedEvent(event)) {
772
789
  return;
773
790
  }
774
791
  await handleFollowUpwardFromSubdivision(select, apiKey, backendUrl, state, config.followUpward, (s, key, code) => updateSubdivisionSelect(s, key, backendUrl, state, config, code));
775
- });
792
+ };
793
+ select.addEventListener('change', handlers.followUpward);
794
+ // Store handlers for future cleanup
795
+ state.eventHandlers.set(select, handlers);
776
796
  }
777
797
  }
778
798
  /**
@@ -991,8 +1011,13 @@
991
1011
  // Subdivisions will be loaded by event handler
992
1012
  loadedInitialSubdivisions = true;
993
1013
  }
994
- // On change => update subdivisions
995
- select.addEventListener('change', async (event) => {
1014
+ // Remove old event handlers if they exist (for re-initialization)
1015
+ const oldCountryHandlers = state.eventHandlers.get(select);
1016
+ if (oldCountryHandlers?.countryChange) {
1017
+ select.removeEventListener('change', oldCountryHandlers.countryChange);
1018
+ }
1019
+ // Create new country change handler
1020
+ const countryChangeHandler = async (event) => {
996
1021
  if (isWidgetInitiatedEvent(event)) {
997
1022
  return;
998
1023
  }
@@ -1014,7 +1039,12 @@
1014
1039
  if (config.followUpward && !select.multiple && picked.is_subdivision_of) {
1015
1040
  await handleFollowUpwardFromCountry(select, apiKey, backendUrl, state, config.followUpward, (s, key, code) => updateSubdivisionSelect(s, key, backendUrl, state, subdivisionConfig, code));
1016
1041
  }
1017
- });
1042
+ };
1043
+ // Store and attach the handler
1044
+ const countryHandlers = state.eventHandlers.get(select) || {};
1045
+ countryHandlers.countryChange = countryChangeHandler;
1046
+ state.eventHandlers.set(select, countryHandlers);
1047
+ select.addEventListener('change', countryChangeHandler);
1018
1048
  }
1019
1049
  catch (error) {
1020
1050
  console.error('Failed to fetch countries:', error);
@@ -1089,6 +1119,7 @@
1089
1119
  subdivisionsMap: new WeakMap(),
1090
1120
  subdivisionsLanguageMap: new WeakMap(),
1091
1121
  isInitializing: new Set(),
1122
+ eventHandlers: new WeakMap(),
1092
1123
  };
1093
1124
  // Use empty string if publicKey is missing (will show error when API calls fail)
1094
1125
  const apiKey = config.publicKey || '';