@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/dist/index.js CHANGED
@@ -1141,23 +1141,27 @@
1141
1141
  * Get configuration from options or script URL parameters
1142
1142
  */
1143
1143
  function getConfigFromOptionsOrScript(options) {
1144
+ // Check for global config first (for bundled widgets that need config before auto-init)
1145
+ const globalConfig = typeof window !== 'undefined' && window.CountriesDBConfig
1146
+ ? window.CountriesDBConfig
1147
+ : null;
1144
1148
  // Try to get config from script URL (for backward compatibility with widget.blade.php)
1145
1149
  let scriptUrl = null;
1146
1150
  try {
1147
1151
  let loaderScript = null;
1148
1152
  // First try document.currentScript (works during script execution)
1153
+ // But only if it matches the widget pattern (not a bundled file)
1149
1154
  if (document.currentScript && document.currentScript instanceof HTMLScriptElement) {
1150
- loaderScript = document.currentScript;
1155
+ const src = document.currentScript.src;
1156
+ if (src && (src.includes('@countriesdb/widget') || src.includes('widget/dist/index.js'))) {
1157
+ loaderScript = document.currentScript;
1158
+ }
1151
1159
  }
1152
- else {
1153
- // Fallback: find script tag with @countriesdb/widget in src
1160
+ // If currentScript didn't match, search for widget script
1161
+ if (!loaderScript) {
1154
1162
  const scripts = Array.from(document.getElementsByTagName('script'));
1155
1163
  loaderScript = scripts.find((s) => s.src && (s.src.includes('@countriesdb/widget') ||
1156
1164
  s.src.includes('widget/dist/index.js'))) || null;
1157
- // If still not found, try the last script with src
1158
- if (!loaderScript) {
1159
- loaderScript = scripts.filter((s) => s.src).pop() || null;
1160
- }
1161
1165
  }
1162
1166
  if (loaderScript && loaderScript.src) {
1163
1167
  scriptUrl = new URL(loaderScript.src);
@@ -1167,39 +1171,57 @@
1167
1171
  // Ignore errors
1168
1172
  }
1169
1173
  const config = {
1170
- publicKey: options.publicKey || scriptUrl?.searchParams.get('public_key') || '',
1171
- backendUrl: options.backendUrl || scriptUrl?.searchParams.get('backend_url') || getBackendUrlFromScript(),
1172
- defaultLanguage: options.defaultLanguage || scriptUrl?.searchParams.get('default_language') || undefined,
1173
- forcedLanguage: options.forcedLanguage || scriptUrl?.searchParams.get('forced_language') || undefined,
1174
+ // Priority: options > globalConfig > scriptUrl params > defaults
1175
+ publicKey: options.publicKey ?? globalConfig?.publicKey ?? scriptUrl?.searchParams.get('public_key') ?? '',
1176
+ backendUrl: options.backendUrl ?? globalConfig?.backendUrl ?? scriptUrl?.searchParams.get('backend_url') ?? getBackendUrlFromScript(),
1177
+ defaultLanguage: options.defaultLanguage ?? globalConfig?.defaultLanguage ?? scriptUrl?.searchParams.get('default_language') ?? undefined,
1178
+ forcedLanguage: options.forcedLanguage ?? globalConfig?.forcedLanguage ?? scriptUrl?.searchParams.get('forced_language') ?? undefined,
1174
1179
  showSubdivisionType: options.showSubdivisionType !== undefined
1175
1180
  ? options.showSubdivisionType
1176
- : parseBoolean(scriptUrl?.searchParams.get('show_subdivision_type') ?? '1'),
1181
+ : globalConfig?.showSubdivisionType !== undefined
1182
+ ? globalConfig.showSubdivisionType
1183
+ : parseBoolean(scriptUrl?.searchParams.get('show_subdivision_type') ?? '1'),
1177
1184
  followRelated: options.followRelated !== undefined
1178
1185
  ? options.followRelated
1179
- : parseBoolean(scriptUrl?.searchParams.get('follow_related') ?? 'false'),
1186
+ : globalConfig?.followRelated !== undefined
1187
+ ? globalConfig.followRelated
1188
+ : parseBoolean(scriptUrl?.searchParams.get('follow_related') ?? 'false'),
1180
1189
  followUpward: options.followUpward !== undefined
1181
1190
  ? options.followUpward
1182
- : parseBoolean(scriptUrl?.searchParams.get('follow_upward') ?? 'false'),
1191
+ : globalConfig?.followUpward !== undefined
1192
+ ? globalConfig.followUpward
1193
+ : parseBoolean(scriptUrl?.searchParams.get('follow_upward') ?? 'false'),
1183
1194
  allowParentSelection: options.allowParentSelection !== undefined
1184
1195
  ? options.allowParentSelection
1185
- : parseBoolean(scriptUrl?.searchParams.get('allow_parent_selection') ?? 'false'),
1196
+ : globalConfig?.allowParentSelection !== undefined
1197
+ ? globalConfig.allowParentSelection
1198
+ : parseBoolean(scriptUrl?.searchParams.get('allow_parent_selection') ?? 'false'),
1186
1199
  isoCountryNames: options.isoCountryNames !== undefined
1187
1200
  ? options.isoCountryNames
1188
- : parseBoolean(scriptUrl?.searchParams.get('iso_country_names') ?? 'false'),
1201
+ : globalConfig?.isoCountryNames !== undefined
1202
+ ? globalConfig.isoCountryNames
1203
+ : parseBoolean(scriptUrl?.searchParams.get('iso_country_names') ?? 'false'),
1189
1204
  subdivisionRomanizationPreference: options.subdivisionRomanizationPreference ||
1205
+ globalConfig?.subdivisionRomanizationPreference ||
1190
1206
  scriptUrl?.searchParams.get('subdivision_romanization_preference') ||
1191
1207
  undefined,
1192
1208
  preferLocalVariant: options.preferLocalVariant !== undefined
1193
1209
  ? options.preferLocalVariant
1194
- : parseBoolean(scriptUrl?.searchParams.get('prefer_local_variant') ?? 'false'),
1210
+ : globalConfig?.preferLocalVariant !== undefined
1211
+ ? globalConfig.preferLocalVariant
1212
+ : parseBoolean(scriptUrl?.searchParams.get('prefer_local_variant') ?? 'false'),
1195
1213
  preferOfficialSubdivisions: options.preferOfficialSubdivisions !== undefined
1196
1214
  ? options.preferOfficialSubdivisions
1197
- : parseBoolean(scriptUrl?.searchParams.get('prefer_official') ?? 'false'),
1198
- countryNameFilter: options.countryNameFilter,
1199
- subdivisionNameFilter: options.subdivisionNameFilter,
1215
+ : globalConfig?.preferOfficialSubdivisions !== undefined
1216
+ ? globalConfig.preferOfficialSubdivisions
1217
+ : parseBoolean(scriptUrl?.searchParams.get('prefer_official') ?? 'false'),
1218
+ countryNameFilter: options.countryNameFilter ?? globalConfig?.countryNameFilter,
1219
+ subdivisionNameFilter: options.subdivisionNameFilter ?? globalConfig?.subdivisionNameFilter,
1200
1220
  autoInit: options.autoInit !== undefined
1201
1221
  ? options.autoInit
1202
- : parseBoolean(scriptUrl?.searchParams.get('auto_init') ?? 'true'),
1222
+ : globalConfig?.autoInit !== undefined
1223
+ ? globalConfig.autoInit
1224
+ : parseBoolean(scriptUrl?.searchParams.get('auto_init') ?? 'true'),
1203
1225
  };
1204
1226
  // Resolve filter functions from global scope if specified by name
1205
1227
  if (scriptUrl) {
@@ -1227,18 +1249,19 @@
1227
1249
  try {
1228
1250
  let loaderScript = null;
1229
1251
  // First try document.currentScript (works during script execution)
1252
+ // But only if it matches the widget pattern (not a bundled file)
1230
1253
  if (document.currentScript && document.currentScript instanceof HTMLScriptElement) {
1231
- loaderScript = document.currentScript;
1254
+ const src = document.currentScript.src;
1255
+ if (src && (src.includes('@countriesdb/widget') || src.includes('widget/dist/index.js'))) {
1256
+ loaderScript = document.currentScript;
1257
+ }
1232
1258
  }
1233
- else {
1234
- // Fallback: find script tag with @countriesdb/widget in src
1259
+ // If currentScript didn't match, search for widget script
1260
+ if (!loaderScript) {
1261
+ // Only consider script tags that loaded the widget bundle
1235
1262
  const scripts = Array.from(document.getElementsByTagName('script'));
1236
1263
  loaderScript = scripts.find((s) => s.src && (s.src.includes('@countriesdb/widget') ||
1237
1264
  s.src.includes('widget/dist/index.js'))) || null;
1238
- // If still not found, try the last script with src
1239
- if (!loaderScript) {
1240
- loaderScript = scripts.filter((s) => s.src).pop() || null;
1241
- }
1242
1265
  }
1243
1266
  if (loaderScript && loaderScript.src) {
1244
1267
  const scriptUrl = new URL(loaderScript.src);
@@ -1321,18 +1344,28 @@
1321
1344
  // Find the script tag that loaded this widget
1322
1345
  let loaderScript = null;
1323
1346
  // First try document.currentScript (works during script execution)
1347
+ // But only if it matches the widget pattern (not a bundled file)
1324
1348
  if (document.currentScript && document.currentScript instanceof HTMLScriptElement) {
1325
- loaderScript = document.currentScript;
1349
+ const src = document.currentScript.src;
1350
+ if (src && (src.includes('@countriesdb/widget') || src.includes('widget/dist/index.js'))) {
1351
+ loaderScript = document.currentScript;
1352
+ }
1326
1353
  }
1327
- else {
1328
- // Fallback: find script tag with @countriesdb/widget in src
1354
+ // If currentScript didn't match, search for widget script
1355
+ if (!loaderScript) {
1329
1356
  const scripts = Array.from(document.getElementsByTagName('script'));
1330
1357
  loaderScript = scripts.find((s) => s.src && (s.src.includes('@countriesdb/widget') ||
1331
1358
  s.src.includes('widget/dist/index.js'))) || null;
1332
1359
  }
1333
1360
  // Default to auto-init = true (only disable if explicitly set to false)
1334
1361
  let shouldAutoInit = true;
1335
- if (loaderScript && loaderScript.src) {
1362
+ const globalConfig = typeof window !== 'undefined'
1363
+ ? window.CountriesDBConfig || null
1364
+ : null;
1365
+ if (globalConfig && typeof globalConfig.autoInit !== 'undefined') {
1366
+ shouldAutoInit = !!globalConfig.autoInit;
1367
+ }
1368
+ else if (loaderScript && loaderScript.src) {
1336
1369
  try {
1337
1370
  const scriptUrl = new URL(loaderScript.src);
1338
1371
  const autoInit = scriptUrl.searchParams.get('auto_init');