@countriesdb/widget 0.1.27 → 0.1.29

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
@@ -940,6 +940,27 @@
940
940
  select.innerHTML += `<option value="${defaultValue}" disabled>Error: No country select present</option>`;
941
941
  }
942
942
  }
943
+ /**
944
+ * Get current subdivision config from window.CountriesDBConfig
945
+ * This ensures we always use the latest config values, even after widget reload
946
+ */
947
+ function getCurrentSubdivisionConfig(apiKey, backendUrl) {
948
+ const globalConfig = typeof window !== 'undefined' && window.CountriesDBConfig
949
+ ? window.CountriesDBConfig
950
+ : null;
951
+ return {
952
+ followRelated: globalConfig?.followRelated || false,
953
+ followUpward: globalConfig?.followUpward || false,
954
+ showSubdivisionType: globalConfig?.showSubdivisionType !== false,
955
+ allowParentSelection: globalConfig?.allowParentSelection || false,
956
+ preferOfficialSubdivisions: globalConfig?.preferOfficialSubdivisions || false,
957
+ subdivisionRomanizationPreference: globalConfig?.subdivisionRomanizationPreference,
958
+ preferLocalVariant: globalConfig?.preferLocalVariant || false,
959
+ forcedLanguage: globalConfig?.forcedLanguage,
960
+ defaultLanguage: globalConfig?.defaultLanguage,
961
+ subdivisionNameFilter: globalConfig?.subdivisionNameFilter,
962
+ };
963
+ }
943
964
  /**
944
965
  * Update subdivisions for all linked subdivision selects when country changes
945
966
  */
@@ -1040,8 +1061,13 @@
1040
1061
  }
1041
1062
  // Dispatch update event for user-initiated country change
1042
1063
  dispatchUpdateEvent(select, { type: 'country', reason: 'regular' });
1043
- // Update subdivisions
1044
- await updateSubdivisions(select, apiKey, backendUrl, state, subdivisionConfig);
1064
+ // Get current config dynamically to ensure we use the latest values
1065
+ const currentSubdivisionConfig = getCurrentSubdivisionConfig(apiKey, backendUrl);
1066
+ const currentConfig = typeof window !== 'undefined' && window.CountriesDBConfig
1067
+ ? window.CountriesDBConfig
1068
+ : config;
1069
+ // Update subdivisions with current config
1070
+ await updateSubdivisions(select, apiKey, backendUrl, state, currentSubdivisionConfig);
1045
1071
  const chosen = select.value;
1046
1072
  if (!chosen) {
1047
1073
  return;
@@ -1053,8 +1079,9 @@
1053
1079
  }
1054
1080
  // followUpward from country perspective
1055
1081
  // Only works when country is single-select (never for multi-select)
1056
- if (config.followUpward && !select.multiple && picked.is_subdivision_of) {
1057
- await handleFollowUpwardFromCountry(select, apiKey, backendUrl, state, config.followUpward, (s, key, code) => updateSubdivisionSelect(s, key, backendUrl, state, subdivisionConfig, code));
1082
+ const currentFollowUpward = currentConfig?.followUpward || false;
1083
+ if (currentFollowUpward && !select.multiple && picked.is_subdivision_of) {
1084
+ await handleFollowUpwardFromCountry(select, apiKey, backendUrl, state, currentFollowUpward, (s, key, code) => updateSubdivisionSelect(s, key, backendUrl, state, currentSubdivisionConfig, code));
1058
1085
  }
1059
1086
  };
1060
1087
  // Store and attach the handler
@@ -1108,9 +1135,19 @@
1108
1135
  initialized: false,
1109
1136
  initPromise: null,
1110
1137
  version: 0,
1138
+ eventHandlers: new WeakMap(),
1111
1139
  };
1112
1140
  }
1113
1141
  const NS = window[NS_KEY];
1142
+ // Ensure eventHandlers exists (for backwards compatibility)
1143
+ if (!NS.eventHandlers) {
1144
+ NS.eventHandlers = new WeakMap();
1145
+ }
1146
+ // Always re-initialize when CountriesWidgetLoad is called (even if already initialized)
1147
+ // This allows the widget to find new select elements after navigation or DOM changes
1148
+ // Reset the initialized flag so the widget re-scans the DOM
1149
+ NS.initialized = false;
1150
+ NS.initPromise = null;
1114
1151
  // Share the same promise across concurrent calls
1115
1152
  NS.initPromise = (async () => {
1116
1153
  // Wait for DOM if needed
@@ -1131,12 +1168,13 @@
1131
1168
  return false;
1132
1169
  }
1133
1170
  // Initialize widget state
1171
+ // Use global eventHandlers from namespace so we can clean up old handlers on re-initialization
1134
1172
  const state = {
1135
1173
  countriesMap: new WeakMap(),
1136
1174
  subdivisionsMap: new WeakMap(),
1137
1175
  subdivisionsLanguageMap: new WeakMap(),
1138
1176
  isInitializing: new Set(),
1139
- eventHandlers: new WeakMap(),
1177
+ eventHandlers: NS.eventHandlers, // Use global WeakMap that persists across calls
1140
1178
  };
1141
1179
  // Use empty string if publicKey is missing (will show error when API calls fail)
1142
1180
  const apiKey = config.publicKey || '';
@@ -1171,7 +1209,7 @@
1171
1209
  followRelated: config.followRelated || false,
1172
1210
  followUpward: config.followUpward || false,
1173
1211
  countryNameFilter: config.countryNameFilter,
1174
- }, subdivisionConfig);
1212
+ });
1175
1213
  // After countries are set up, update subdivisions for any preselected countries
1176
1214
  const countrySelects = Array.from(document.querySelectorAll('.country-selection'));
1177
1215
  for (const select of countrySelects) {