@countriesdb/widget 0.1.32 → 0.1.34

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.
@@ -105,9 +105,8 @@ export function applyPreselectedValue(select, apiKey) {
105
105
  // Read from select element like old widget (line 740)
106
106
  const tempOnce = select.dataset._widgetTempPreselect;
107
107
  const permanent = select.getAttribute('data-preselected') || select.dataset.preselected;
108
- const chosen = (tempOnce !== undefined && tempOnce !== null && String(tempOnce).trim() !== '')
109
- ? tempOnce
110
- : permanent;
108
+ const hasTemp = tempOnce !== undefined && tempOnce !== null && String(tempOnce).trim() !== '';
109
+ const chosen = hasTemp ? tempOnce : permanent;
111
110
  if (!chosen || (typeof chosen === 'string' && chosen.trim() === '')) {
112
111
  return;
113
112
  }
@@ -133,6 +132,12 @@ export function applyPreselectedValue(select, apiKey) {
133
132
  if (select.dataset._widgetTempPreselect !== undefined) {
134
133
  delete select.dataset._widgetTempPreselect;
135
134
  }
135
+ // Mark permanent preselect as consumed after first application
136
+ // This prevents it from being reapplied on user-initiated country changes
137
+ // But reload: true will clear this flag to allow reapplication
138
+ if (permanent !== undefined && permanent !== null && String(permanent).trim() !== '') {
139
+ select.dataset._widgetPreselectedConsumed = '1';
140
+ }
136
141
  }
137
142
  /**
138
143
  * Handle API error by showing error message in select
package/dist/index.esm.js CHANGED
@@ -106,9 +106,8 @@ function applyPreselectedValue(select, apiKey) {
106
106
  // Read from select element like old widget (line 740)
107
107
  const tempOnce = select.dataset._widgetTempPreselect;
108
108
  const permanent = select.getAttribute('data-preselected') || select.dataset.preselected;
109
- const chosen = (tempOnce !== undefined && tempOnce !== null && String(tempOnce).trim() !== '')
110
- ? tempOnce
111
- : permanent;
109
+ const hasTemp = tempOnce !== undefined && tempOnce !== null && String(tempOnce).trim() !== '';
110
+ const chosen = hasTemp ? tempOnce : permanent;
112
111
  if (!chosen || (typeof chosen === 'string' && chosen.trim() === '')) {
113
112
  return;
114
113
  }
@@ -134,6 +133,12 @@ function applyPreselectedValue(select, apiKey) {
134
133
  if (select.dataset._widgetTempPreselect !== undefined) {
135
134
  delete select.dataset._widgetTempPreselect;
136
135
  }
136
+ // Mark permanent preselect as consumed after first application
137
+ // This prevents it from being reapplied on user-initiated country changes
138
+ // But reload: true will clear this flag to allow reapplication
139
+ if (permanent !== undefined && permanent !== null && String(permanent).trim() !== '') {
140
+ select.dataset._widgetPreselectedConsumed = '1';
141
+ }
137
142
  }
138
143
  /**
139
144
  * Handle API error by showing error message in select
@@ -622,7 +627,13 @@ async function updateSubdivisionSelect(select, apiKey, backendUrl, state, config
622
627
  if (!userSelectionRestored) {
623
628
  // Check if preselected value exists (applyPreselectedValue will read it from select element)
624
629
  const hasPreselectedValue = (select.getAttribute('data-preselected') || select.dataset.preselected || select.dataset._widgetTempPreselect) !== undefined;
625
- if (hasPreselectedValue) {
630
+ const preselectedConsumed = select.dataset._widgetPreselectedConsumed === '1';
631
+ // Only apply preselected if:
632
+ // 1. It exists AND
633
+ // 2. Either it hasn't been consumed yet, OR this is the initial load (not a reload)
634
+ // This prevents reapplying preselected values on user-initiated country changes
635
+ // But allows them on initial load and on reload: true (which clears the consumed flag)
636
+ if (hasPreselectedValue && (!preselectedConsumed || !isReload)) {
626
637
  applyPreselectedValue(select, apiKey);
627
638
  dispatchUpdateEvent(select, {
628
639
  type: 'subdivision',
@@ -690,23 +701,62 @@ async function updateSubdivisionSelect(select, apiKey, backendUrl, state, config
690
701
  }
691
702
  }
692
703
  /**
693
- * Get current subdivision config from window.CountriesDBConfig
704
+ * Get current subdivision config from window.CountriesDBConfig and script URL
694
705
  * This ensures we always use the latest config values, even after widget reload
695
706
  */
696
707
  function getCurrentSubdivisionConfig(apiKey, backendUrl) {
697
708
  const globalConfig = typeof window !== 'undefined' && window.CountriesDBConfig
698
709
  ? window.CountriesDBConfig
699
710
  : null;
711
+ // Also check script URL parameters (for backward compatibility and reload scenarios)
712
+ let scriptUrl = null;
713
+ try {
714
+ const scripts = Array.from(document.getElementsByTagName('script'));
715
+ const loaderScript = scripts.find((s) => s.src && (s.src.includes('@countriesdb/widget') ||
716
+ s.src.includes('widget/dist/index.js'))) || null;
717
+ if (loaderScript && loaderScript.src) {
718
+ scriptUrl = new URL(loaderScript.src);
719
+ }
720
+ }
721
+ catch {
722
+ // Ignore errors
723
+ }
724
+ // Import parseBoolean from dom-manipulation
725
+ const parseBoolean = (value) => {
726
+ if (value === null || value === undefined) {
727
+ return false;
728
+ }
729
+ const lowered = String(value).trim().toLowerCase();
730
+ return !(lowered === '0' || lowered === 'false');
731
+ };
700
732
  return {
701
- followRelated: globalConfig?.followRelated || false,
702
- followUpward: globalConfig?.followUpward || false,
703
- showSubdivisionType: globalConfig?.showSubdivisionType !== false,
704
- allowParentSelection: globalConfig?.allowParentSelection || false,
705
- preferOfficialSubdivisions: globalConfig?.preferOfficialSubdivisions || false,
706
- subdivisionRomanizationPreference: globalConfig?.subdivisionRomanizationPreference,
707
- preferLocalVariant: globalConfig?.preferLocalVariant || false,
708
- forcedLanguage: globalConfig?.forcedLanguage,
709
- defaultLanguage: globalConfig?.defaultLanguage,
733
+ followRelated: globalConfig?.followRelated !== undefined
734
+ ? globalConfig.followRelated
735
+ : parseBoolean(scriptUrl?.searchParams.get('follow_related') ?? 'false'),
736
+ followUpward: globalConfig?.followUpward !== undefined
737
+ ? globalConfig.followUpward
738
+ : parseBoolean(scriptUrl?.searchParams.get('follow_upward') ?? 'false'),
739
+ showSubdivisionType: globalConfig?.showSubdivisionType !== undefined
740
+ ? globalConfig.showSubdivisionType
741
+ : parseBoolean(scriptUrl?.searchParams.get('show_subdivision_type') ?? '1'),
742
+ allowParentSelection: globalConfig?.allowParentSelection !== undefined
743
+ ? globalConfig.allowParentSelection
744
+ : parseBoolean(scriptUrl?.searchParams.get('allow_parent_selection') ?? 'false'),
745
+ preferOfficialSubdivisions: globalConfig?.preferOfficialSubdivisions !== undefined
746
+ ? globalConfig.preferOfficialSubdivisions
747
+ : parseBoolean(scriptUrl?.searchParams.get('prefer_official') ?? 'false'),
748
+ subdivisionRomanizationPreference: globalConfig?.subdivisionRomanizationPreference ||
749
+ scriptUrl?.searchParams.get('subdivision_romanization_preference') ||
750
+ undefined,
751
+ preferLocalVariant: globalConfig?.preferLocalVariant !== undefined
752
+ ? globalConfig.preferLocalVariant
753
+ : parseBoolean(scriptUrl?.searchParams.get('prefer_local_variant') ?? 'false'),
754
+ forcedLanguage: globalConfig?.forcedLanguage ||
755
+ scriptUrl?.searchParams.get('forced_language') ||
756
+ undefined,
757
+ defaultLanguage: globalConfig?.defaultLanguage ||
758
+ scriptUrl?.searchParams.get('default_language') ||
759
+ undefined,
710
760
  subdivisionNameFilter: globalConfig?.subdivisionNameFilter,
711
761
  };
712
762
  }
@@ -897,6 +947,16 @@ async function CountriesWidgetLoad(options = {}) {
897
947
  if (shouldReload) {
898
948
  NS.initialized = false;
899
949
  NS.initPromise = null;
950
+ // Clear consumed preselected flags to allow preselected values to be reapplied on reload: true
951
+ // This ensures full reload behavior where preselected values are reapplied
952
+ if (typeof document !== 'undefined') {
953
+ const subdivisionSelects = document.querySelectorAll('.subdivision-selection');
954
+ subdivisionSelects.forEach((select) => {
955
+ if (select.dataset._widgetPreselectedConsumed) {
956
+ delete select.dataset._widgetPreselectedConsumed;
957
+ }
958
+ });
959
+ }
900
960
  }
901
961
  // Share the same promise across concurrent calls
902
962
  NS.initPromise = (async () => {