@countriesdb/widget 0.1.24 → 0.1.25

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/README.md CHANGED
@@ -78,7 +78,6 @@ import { CountriesWidgetLoad } from '@countriesdb/widget';
78
78
 
79
79
  CountriesWidgetLoad({
80
80
  publicKey: 'YOUR_PUBLIC_KEY',
81
- backendUrl: 'https://api.countriesdb.com',
82
81
  defaultLanguage: 'en',
83
82
  enableGeolocation: true
84
83
  });
package/dist/index.esm.js CHANGED
@@ -895,23 +895,27 @@ async function CountriesWidgetLoad(options = {}) {
895
895
  * Get configuration from options or script URL parameters
896
896
  */
897
897
  function getConfigFromOptionsOrScript(options) {
898
+ // Check for global config first (for bundled widgets that need config before auto-init)
899
+ const globalConfig = typeof window !== 'undefined' && window.CountriesDBConfig
900
+ ? window.CountriesDBConfig
901
+ : null;
898
902
  // Try to get config from script URL (for backward compatibility with widget.blade.php)
899
903
  let scriptUrl = null;
900
904
  try {
901
905
  let loaderScript = null;
902
906
  // First try document.currentScript (works during script execution)
907
+ // But only if it matches the widget pattern (not a bundled file)
903
908
  if (document.currentScript && document.currentScript instanceof HTMLScriptElement) {
904
- loaderScript = document.currentScript;
909
+ const src = document.currentScript.src;
910
+ if (src && (src.includes('@countriesdb/widget') || src.includes('widget/dist/index.js'))) {
911
+ loaderScript = document.currentScript;
912
+ }
905
913
  }
906
- else {
907
- // Fallback: find script tag with @countriesdb/widget in src
914
+ // If currentScript didn't match, search for widget script
915
+ if (!loaderScript) {
908
916
  const scripts = Array.from(document.getElementsByTagName('script'));
909
917
  loaderScript = scripts.find((s) => s.src && (s.src.includes('@countriesdb/widget') ||
910
918
  s.src.includes('widget/dist/index.js'))) || null;
911
- // If still not found, try the last script with src
912
- if (!loaderScript) {
913
- loaderScript = scripts.filter((s) => s.src).pop() || null;
914
- }
915
919
  }
916
920
  if (loaderScript && loaderScript.src) {
917
921
  scriptUrl = new URL(loaderScript.src);
@@ -921,39 +925,57 @@ function getConfigFromOptionsOrScript(options) {
921
925
  // Ignore errors
922
926
  }
923
927
  const config = {
924
- publicKey: options.publicKey || scriptUrl?.searchParams.get('public_key') || '',
925
- backendUrl: options.backendUrl || scriptUrl?.searchParams.get('backend_url') || getBackendUrlFromScript(),
926
- defaultLanguage: options.defaultLanguage || scriptUrl?.searchParams.get('default_language') || undefined,
927
- forcedLanguage: options.forcedLanguage || scriptUrl?.searchParams.get('forced_language') || undefined,
928
+ // Priority: options > globalConfig > scriptUrl params > defaults
929
+ publicKey: options.publicKey ?? globalConfig?.publicKey ?? scriptUrl?.searchParams.get('public_key') ?? '',
930
+ backendUrl: options.backendUrl ?? globalConfig?.backendUrl ?? scriptUrl?.searchParams.get('backend_url') ?? getBackendUrlFromScript(),
931
+ defaultLanguage: options.defaultLanguage ?? globalConfig?.defaultLanguage ?? scriptUrl?.searchParams.get('default_language') ?? undefined,
932
+ forcedLanguage: options.forcedLanguage ?? globalConfig?.forcedLanguage ?? scriptUrl?.searchParams.get('forced_language') ?? undefined,
928
933
  showSubdivisionType: options.showSubdivisionType !== undefined
929
934
  ? options.showSubdivisionType
930
- : parseBoolean(scriptUrl?.searchParams.get('show_subdivision_type') ?? '1'),
935
+ : globalConfig?.showSubdivisionType !== undefined
936
+ ? globalConfig.showSubdivisionType
937
+ : parseBoolean(scriptUrl?.searchParams.get('show_subdivision_type') ?? '1'),
931
938
  followRelated: options.followRelated !== undefined
932
939
  ? options.followRelated
933
- : parseBoolean(scriptUrl?.searchParams.get('follow_related') ?? 'false'),
940
+ : globalConfig?.followRelated !== undefined
941
+ ? globalConfig.followRelated
942
+ : parseBoolean(scriptUrl?.searchParams.get('follow_related') ?? 'false'),
934
943
  followUpward: options.followUpward !== undefined
935
944
  ? options.followUpward
936
- : parseBoolean(scriptUrl?.searchParams.get('follow_upward') ?? 'false'),
945
+ : globalConfig?.followUpward !== undefined
946
+ ? globalConfig.followUpward
947
+ : parseBoolean(scriptUrl?.searchParams.get('follow_upward') ?? 'false'),
937
948
  allowParentSelection: options.allowParentSelection !== undefined
938
949
  ? options.allowParentSelection
939
- : parseBoolean(scriptUrl?.searchParams.get('allow_parent_selection') ?? 'false'),
950
+ : globalConfig?.allowParentSelection !== undefined
951
+ ? globalConfig.allowParentSelection
952
+ : parseBoolean(scriptUrl?.searchParams.get('allow_parent_selection') ?? 'false'),
940
953
  isoCountryNames: options.isoCountryNames !== undefined
941
954
  ? options.isoCountryNames
942
- : parseBoolean(scriptUrl?.searchParams.get('iso_country_names') ?? 'false'),
955
+ : globalConfig?.isoCountryNames !== undefined
956
+ ? globalConfig.isoCountryNames
957
+ : parseBoolean(scriptUrl?.searchParams.get('iso_country_names') ?? 'false'),
943
958
  subdivisionRomanizationPreference: options.subdivisionRomanizationPreference ||
959
+ globalConfig?.subdivisionRomanizationPreference ||
944
960
  scriptUrl?.searchParams.get('subdivision_romanization_preference') ||
945
961
  undefined,
946
962
  preferLocalVariant: options.preferLocalVariant !== undefined
947
963
  ? options.preferLocalVariant
948
- : parseBoolean(scriptUrl?.searchParams.get('prefer_local_variant') ?? 'false'),
964
+ : globalConfig?.preferLocalVariant !== undefined
965
+ ? globalConfig.preferLocalVariant
966
+ : parseBoolean(scriptUrl?.searchParams.get('prefer_local_variant') ?? 'false'),
949
967
  preferOfficialSubdivisions: options.preferOfficialSubdivisions !== undefined
950
968
  ? options.preferOfficialSubdivisions
951
- : parseBoolean(scriptUrl?.searchParams.get('prefer_official') ?? 'false'),
952
- countryNameFilter: options.countryNameFilter,
953
- subdivisionNameFilter: options.subdivisionNameFilter,
969
+ : globalConfig?.preferOfficialSubdivisions !== undefined
970
+ ? globalConfig.preferOfficialSubdivisions
971
+ : parseBoolean(scriptUrl?.searchParams.get('prefer_official') ?? 'false'),
972
+ countryNameFilter: options.countryNameFilter ?? globalConfig?.countryNameFilter,
973
+ subdivisionNameFilter: options.subdivisionNameFilter ?? globalConfig?.subdivisionNameFilter,
954
974
  autoInit: options.autoInit !== undefined
955
975
  ? options.autoInit
956
- : parseBoolean(scriptUrl?.searchParams.get('auto_init') ?? 'true'),
976
+ : globalConfig?.autoInit !== undefined
977
+ ? globalConfig.autoInit
978
+ : parseBoolean(scriptUrl?.searchParams.get('auto_init') ?? 'true'),
957
979
  };
958
980
  // Resolve filter functions from global scope if specified by name
959
981
  if (scriptUrl) {
@@ -981,18 +1003,19 @@ function getBackendUrlFromScript() {
981
1003
  try {
982
1004
  let loaderScript = null;
983
1005
  // First try document.currentScript (works during script execution)
1006
+ // But only if it matches the widget pattern (not a bundled file)
984
1007
  if (document.currentScript && document.currentScript instanceof HTMLScriptElement) {
985
- loaderScript = document.currentScript;
1008
+ const src = document.currentScript.src;
1009
+ if (src && (src.includes('@countriesdb/widget') || src.includes('widget/dist/index.js'))) {
1010
+ loaderScript = document.currentScript;
1011
+ }
986
1012
  }
987
- else {
988
- // Fallback: find script tag with @countriesdb/widget in src
1013
+ // If currentScript didn't match, search for widget script
1014
+ if (!loaderScript) {
1015
+ // Only consider script tags that loaded the widget bundle
989
1016
  const scripts = Array.from(document.getElementsByTagName('script'));
990
1017
  loaderScript = scripts.find((s) => s.src && (s.src.includes('@countriesdb/widget') ||
991
1018
  s.src.includes('widget/dist/index.js'))) || null;
992
- // If still not found, try the last script with src
993
- if (!loaderScript) {
994
- loaderScript = scripts.filter((s) => s.src).pop() || null;
995
- }
996
1019
  }
997
1020
  if (loaderScript && loaderScript.src) {
998
1021
  const scriptUrl = new URL(loaderScript.src);
@@ -1075,18 +1098,28 @@ if (typeof window !== 'undefined') {
1075
1098
  // Find the script tag that loaded this widget
1076
1099
  let loaderScript = null;
1077
1100
  // First try document.currentScript (works during script execution)
1101
+ // But only if it matches the widget pattern (not a bundled file)
1078
1102
  if (document.currentScript && document.currentScript instanceof HTMLScriptElement) {
1079
- loaderScript = document.currentScript;
1103
+ const src = document.currentScript.src;
1104
+ if (src && (src.includes('@countriesdb/widget') || src.includes('widget/dist/index.js'))) {
1105
+ loaderScript = document.currentScript;
1106
+ }
1080
1107
  }
1081
- else {
1082
- // Fallback: find script tag with @countriesdb/widget in src
1108
+ // If currentScript didn't match, search for widget script
1109
+ if (!loaderScript) {
1083
1110
  const scripts = Array.from(document.getElementsByTagName('script'));
1084
1111
  loaderScript = scripts.find((s) => s.src && (s.src.includes('@countriesdb/widget') ||
1085
1112
  s.src.includes('widget/dist/index.js'))) || null;
1086
1113
  }
1087
1114
  // Default to auto-init = true (only disable if explicitly set to false)
1088
1115
  let shouldAutoInit = true;
1089
- if (loaderScript && loaderScript.src) {
1116
+ const globalConfig = typeof window !== 'undefined'
1117
+ ? window.CountriesDBConfig || null
1118
+ : null;
1119
+ if (globalConfig && typeof globalConfig.autoInit !== 'undefined') {
1120
+ shouldAutoInit = !!globalConfig.autoInit;
1121
+ }
1122
+ else if (loaderScript && loaderScript.src) {
1090
1123
  try {
1091
1124
  const scriptUrl = new URL(loaderScript.src);
1092
1125
  const autoInit = scriptUrl.searchParams.get('auto_init');