@countriesdb/widget 0.1.6 → 0.1.8
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/follow-logic.d.ts +1 -1
- package/dist/follow-logic.js +33 -6
- package/dist/index.esm.js +99 -9
- package/dist/index.esm.js.map +1 -1
- package/dist/index.js +99 -9
- package/dist/index.js.map +1 -1
- package/dist/initialization.d.ts +6 -0
- package/dist/initialization.js +2 -4
- package/package.json +1 -1
package/dist/follow-logic.d.ts
CHANGED
|
@@ -5,7 +5,7 @@ import type { SelectElement, WidgetState } from './types';
|
|
|
5
5
|
/**
|
|
6
6
|
* Trigger follow logic when a subdivision is selected
|
|
7
7
|
*/
|
|
8
|
-
export declare function triggerFollowLogic(select: SelectElement, apiKey: string, backendUrl: string, state: WidgetState, followRelated: boolean, followUpward: boolean, updateSubdivisionSelectFn: (select: SelectElement, apiKey: string, countryCode: string) => Promise<void>): Promise<void>;
|
|
8
|
+
export declare function triggerFollowLogic(select: SelectElement, apiKey: string, backendUrl: string, state: WidgetState, followRelated: boolean, followUpward: boolean, updateSubdivisionSelectFn: (select: SelectElement, apiKey: string, countryCode: string) => Promise<void>, updateSubdivisionsFn?: (countrySelect: SelectElement, apiKey: string) => Promise<void>): Promise<void>;
|
|
9
9
|
/**
|
|
10
10
|
* Handle follow_related from subdivision change event
|
|
11
11
|
*/
|
package/dist/follow-logic.js
CHANGED
|
@@ -5,7 +5,7 @@ import { dispatchUpdateEvent } from './event-system';
|
|
|
5
5
|
/**
|
|
6
6
|
* Trigger follow logic when a subdivision is selected
|
|
7
7
|
*/
|
|
8
|
-
export async function triggerFollowLogic(select, apiKey, backendUrl, state, followRelated, followUpward, updateSubdivisionSelectFn) {
|
|
8
|
+
export async function triggerFollowLogic(select, apiKey, backendUrl, state, followRelated, followUpward, updateSubdivisionSelectFn, updateSubdivisionsFn) {
|
|
9
9
|
if (!followRelated && !followUpward) {
|
|
10
10
|
return;
|
|
11
11
|
}
|
|
@@ -39,9 +39,17 @@ export async function triggerFollowLogic(select, apiKey, backendUrl, state, foll
|
|
|
39
39
|
relatedSubsSelect.dataset._widgetTempPreselect = targetSubdivision; // one-time preselect
|
|
40
40
|
}
|
|
41
41
|
if (linkedCountrySelect.value !== targetCountry) {
|
|
42
|
+
// Directly set value like old widget did
|
|
42
43
|
linkedCountrySelect.value = targetCountry;
|
|
43
44
|
dispatchUpdateEvent(linkedCountrySelect, { type: 'country', reason: 'regular' });
|
|
44
|
-
|
|
45
|
+
// Update all subdivision selects for the new country (like old widget)
|
|
46
|
+
if (updateSubdivisionsFn) {
|
|
47
|
+
await updateSubdivisionsFn(linkedCountrySelect, apiKey);
|
|
48
|
+
}
|
|
49
|
+
else if (relatedSubsSelect) {
|
|
50
|
+
// Fallback: update just the one subdivision select
|
|
51
|
+
await updateSubdivisionSelectFn(relatedSubsSelect, apiKey, targetCountry);
|
|
52
|
+
}
|
|
45
53
|
}
|
|
46
54
|
else {
|
|
47
55
|
if (relatedSubsSelect && targetSubdivision) {
|
|
@@ -61,9 +69,19 @@ export async function triggerFollowLogic(select, apiKey, backendUrl, state, foll
|
|
|
61
69
|
}
|
|
62
70
|
}
|
|
63
71
|
if (linkedCountrySelect.value !== parentCode) {
|
|
72
|
+
// Directly set value like old widget did
|
|
64
73
|
linkedCountrySelect.value = parentCode;
|
|
65
74
|
dispatchUpdateEvent(linkedCountrySelect, { type: 'country', reason: 'regular' });
|
|
66
|
-
|
|
75
|
+
// Update all subdivision selects for the new country (like old widget)
|
|
76
|
+
if (updateSubdivisionsFn) {
|
|
77
|
+
await updateSubdivisionsFn(linkedCountrySelect, apiKey);
|
|
78
|
+
}
|
|
79
|
+
else {
|
|
80
|
+
const relatedSubsSelect = document.querySelector(`.subdivision-selection[data-country="${linkedCountrySelect.dataset.name}"]`);
|
|
81
|
+
if (relatedSubsSelect) {
|
|
82
|
+
await updateSubdivisionSelectFn(relatedSubsSelect, apiKey, parentCode);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
67
85
|
}
|
|
68
86
|
else if (parentSub) {
|
|
69
87
|
const relatedSubsSelect = document.querySelector(`.subdivision-selection[data-country="${linkedCountrySelect.dataset.name}"]`);
|
|
@@ -107,9 +125,12 @@ export async function handleFollowRelatedFromSubdivision(select, apiKey, backend
|
|
|
107
125
|
relatedSubsSelect.dataset.preselected = targetSubdivision;
|
|
108
126
|
}
|
|
109
127
|
if (linkedCountrySelect.value !== targetCountry) {
|
|
128
|
+
// Directly set value like old widget did
|
|
110
129
|
linkedCountrySelect.value = targetCountry;
|
|
111
130
|
dispatchUpdateEvent(linkedCountrySelect, { type: 'country', reason: 'regular' });
|
|
112
|
-
// Update
|
|
131
|
+
// Update all subdivision selects for the new country (like old widget)
|
|
132
|
+
// Note: updateSubdivisionsFn is not available in handleFollowRelatedFromSubdivision,
|
|
133
|
+
// so we update the subdivision select manually
|
|
113
134
|
if (relatedSubsSelect) {
|
|
114
135
|
await updateSubdivisionSelectFn(relatedSubsSelect, apiKey, targetCountry);
|
|
115
136
|
}
|
|
@@ -157,9 +178,12 @@ export async function handleFollowUpwardFromSubdivision(select, apiKey, backendU
|
|
|
157
178
|
}
|
|
158
179
|
}
|
|
159
180
|
if (linkedCountrySelect.value !== parentCode) {
|
|
181
|
+
// Directly set value like old widget did
|
|
160
182
|
linkedCountrySelect.value = parentCode;
|
|
161
183
|
dispatchUpdateEvent(linkedCountrySelect, { type: 'country', reason: 'follow' });
|
|
162
|
-
// Update
|
|
184
|
+
// Update all subdivision selects for the new country (like old widget)
|
|
185
|
+
// Note: updateSubdivisionsFn is not available in handleFollowUpwardFromSubdivision,
|
|
186
|
+
// so we update the subdivision select manually
|
|
163
187
|
const relatedSubsSelect = document.querySelector(`.subdivision-selection[data-country="${linkedCountrySelect.dataset.name}"]`);
|
|
164
188
|
if (relatedSubsSelect) {
|
|
165
189
|
await updateSubdivisionSelectFn(relatedSubsSelect, apiKey, parentCode);
|
|
@@ -204,9 +228,12 @@ export async function handleFollowUpwardFromCountry(select, apiKey, backendUrl,
|
|
|
204
228
|
}
|
|
205
229
|
}
|
|
206
230
|
}
|
|
231
|
+
// Directly set value like old widget did
|
|
207
232
|
select.value = parentCode;
|
|
208
233
|
dispatchUpdateEvent(select, { type: 'country', reason: 'regular' });
|
|
209
|
-
// Update all
|
|
234
|
+
// Update all subdivision selects for the new country (like old widget)
|
|
235
|
+
// Note: updateSubdivisionsFn is not available in handleFollowUpwardFromCountry,
|
|
236
|
+
// so we update all subdivision selects manually
|
|
210
237
|
const linkedSubdivisionSelects = Array.from(document.querySelectorAll(`.subdivision-selection[data-country="${select.dataset.name}"]`));
|
|
211
238
|
for (const s of linkedSubdivisionSelects) {
|
|
212
239
|
await updateSubdivisionSelectFn(s, apiKey, parentCode);
|
package/dist/index.esm.js
CHANGED
|
@@ -169,10 +169,91 @@ function isWidgetInitiatedEvent(event) {
|
|
|
169
169
|
/**
|
|
170
170
|
* Trigger follow logic when a subdivision is selected
|
|
171
171
|
*/
|
|
172
|
-
async function triggerFollowLogic(select, apiKey, backendUrl, state, followRelated, followUpward, updateSubdivisionSelectFn) {
|
|
173
|
-
{
|
|
172
|
+
async function triggerFollowLogic(select, apiKey, backendUrl, state, followRelated, followUpward, updateSubdivisionSelectFn, updateSubdivisionsFn) {
|
|
173
|
+
if (!followRelated && !followUpward) {
|
|
174
174
|
return;
|
|
175
175
|
}
|
|
176
|
+
const linkedCountrySelect = document.querySelector(`.country-selection[data-name="${select.dataset.country}"]`);
|
|
177
|
+
// Only work if country is single-select (never when country is multi)
|
|
178
|
+
if (!linkedCountrySelect || linkedCountrySelect.multiple) {
|
|
179
|
+
return;
|
|
180
|
+
}
|
|
181
|
+
const allSubs = state.subdivisionsMap.get(select);
|
|
182
|
+
if (!allSubs) {
|
|
183
|
+
return;
|
|
184
|
+
}
|
|
185
|
+
// For follow_upward, only work if subdivision is single-select
|
|
186
|
+
if (followUpward && select.multiple) {
|
|
187
|
+
return;
|
|
188
|
+
}
|
|
189
|
+
const selectedCode = select.value;
|
|
190
|
+
if (!selectedCode) {
|
|
191
|
+
return;
|
|
192
|
+
}
|
|
193
|
+
const picked = allSubs.find((s) => s.code === selectedCode);
|
|
194
|
+
if (!picked) {
|
|
195
|
+
return;
|
|
196
|
+
}
|
|
197
|
+
// follow_related
|
|
198
|
+
if (followRelated && picked.related_country_code) {
|
|
199
|
+
const targetCountry = picked.related_country_code;
|
|
200
|
+
const targetSubdivision = picked.related_subdivision_code;
|
|
201
|
+
const relatedSubsSelect = document.querySelector(`.subdivision-selection[data-country="${linkedCountrySelect.dataset.name}"]`);
|
|
202
|
+
if (relatedSubsSelect && targetSubdivision) {
|
|
203
|
+
relatedSubsSelect.dataset._widgetTempPreselect = targetSubdivision; // one-time preselect
|
|
204
|
+
}
|
|
205
|
+
if (linkedCountrySelect.value !== targetCountry) {
|
|
206
|
+
// Directly set value like old widget did
|
|
207
|
+
linkedCountrySelect.value = targetCountry;
|
|
208
|
+
dispatchUpdateEvent(linkedCountrySelect, { type: 'country', reason: 'regular' });
|
|
209
|
+
// Update all subdivision selects for the new country (like old widget)
|
|
210
|
+
if (updateSubdivisionsFn) {
|
|
211
|
+
await updateSubdivisionsFn(linkedCountrySelect, apiKey);
|
|
212
|
+
}
|
|
213
|
+
else if (relatedSubsSelect) {
|
|
214
|
+
// Fallback: update just the one subdivision select
|
|
215
|
+
await updateSubdivisionSelectFn(relatedSubsSelect, apiKey, targetCountry);
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
else {
|
|
219
|
+
if (relatedSubsSelect && targetSubdivision) {
|
|
220
|
+
await updateSubdivisionSelectFn(relatedSubsSelect, apiKey, targetCountry);
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
// follow_upward
|
|
225
|
+
if (followUpward && picked.is_subdivision_of) {
|
|
226
|
+
const parentCode = picked.is_subdivision_of.parent_country_code;
|
|
227
|
+
const parentSub = picked.is_subdivision_of.subdivision_code;
|
|
228
|
+
if (parentSub) {
|
|
229
|
+
const relatedSubsSelect = document.querySelector(`.subdivision-selection[data-country="${linkedCountrySelect.dataset.name}"]`);
|
|
230
|
+
// Only preselect if subdivision is single-select (follow_upward doesn't work with multi)
|
|
231
|
+
if (relatedSubsSelect && !relatedSubsSelect.multiple) {
|
|
232
|
+
relatedSubsSelect.dataset._widgetTempPreselect = parentSub; // one-time preselect
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
if (linkedCountrySelect.value !== parentCode) {
|
|
236
|
+
// Directly set value like old widget did
|
|
237
|
+
linkedCountrySelect.value = parentCode;
|
|
238
|
+
dispatchUpdateEvent(linkedCountrySelect, { type: 'country', reason: 'regular' });
|
|
239
|
+
// Update all subdivision selects for the new country (like old widget)
|
|
240
|
+
if (updateSubdivisionsFn) {
|
|
241
|
+
await updateSubdivisionsFn(linkedCountrySelect, apiKey);
|
|
242
|
+
}
|
|
243
|
+
else {
|
|
244
|
+
const relatedSubsSelect = document.querySelector(`.subdivision-selection[data-country="${linkedCountrySelect.dataset.name}"]`);
|
|
245
|
+
if (relatedSubsSelect) {
|
|
246
|
+
await updateSubdivisionSelectFn(relatedSubsSelect, apiKey, parentCode);
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
else if (parentSub) {
|
|
251
|
+
const relatedSubsSelect = document.querySelector(`.subdivision-selection[data-country="${linkedCountrySelect.dataset.name}"]`);
|
|
252
|
+
if (relatedSubsSelect && !relatedSubsSelect.multiple) {
|
|
253
|
+
await updateSubdivisionSelectFn(relatedSubsSelect, apiKey, parentCode);
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
}
|
|
176
257
|
}
|
|
177
258
|
/**
|
|
178
259
|
* Handle follow_related from subdivision change event
|
|
@@ -208,9 +289,12 @@ async function handleFollowRelatedFromSubdivision(select, apiKey, backendUrl, st
|
|
|
208
289
|
relatedSubsSelect.dataset.preselected = targetSubdivision;
|
|
209
290
|
}
|
|
210
291
|
if (linkedCountrySelect.value !== targetCountry) {
|
|
292
|
+
// Directly set value like old widget did
|
|
211
293
|
linkedCountrySelect.value = targetCountry;
|
|
212
294
|
dispatchUpdateEvent(linkedCountrySelect, { type: 'country', reason: 'regular' });
|
|
213
|
-
// Update
|
|
295
|
+
// Update all subdivision selects for the new country (like old widget)
|
|
296
|
+
// Note: updateSubdivisionsFn is not available in handleFollowRelatedFromSubdivision,
|
|
297
|
+
// so we update the subdivision select manually
|
|
214
298
|
if (relatedSubsSelect) {
|
|
215
299
|
await updateSubdivisionSelectFn(relatedSubsSelect, apiKey, targetCountry);
|
|
216
300
|
}
|
|
@@ -258,9 +342,12 @@ async function handleFollowUpwardFromSubdivision(select, apiKey, backendUrl, sta
|
|
|
258
342
|
}
|
|
259
343
|
}
|
|
260
344
|
if (linkedCountrySelect.value !== parentCode) {
|
|
345
|
+
// Directly set value like old widget did
|
|
261
346
|
linkedCountrySelect.value = parentCode;
|
|
262
347
|
dispatchUpdateEvent(linkedCountrySelect, { type: 'country', reason: 'follow' });
|
|
263
|
-
// Update
|
|
348
|
+
// Update all subdivision selects for the new country (like old widget)
|
|
349
|
+
// Note: updateSubdivisionsFn is not available in handleFollowUpwardFromSubdivision,
|
|
350
|
+
// so we update the subdivision select manually
|
|
264
351
|
const relatedSubsSelect = document.querySelector(`.subdivision-selection[data-country="${linkedCountrySelect.dataset.name}"]`);
|
|
265
352
|
if (relatedSubsSelect) {
|
|
266
353
|
await updateSubdivisionSelectFn(relatedSubsSelect, apiKey, parentCode);
|
|
@@ -305,9 +392,12 @@ async function handleFollowUpwardFromCountry(select, apiKey, backendUrl, state,
|
|
|
305
392
|
}
|
|
306
393
|
}
|
|
307
394
|
}
|
|
395
|
+
// Directly set value like old widget did
|
|
308
396
|
select.value = parentCode;
|
|
309
397
|
dispatchUpdateEvent(select, { type: 'country', reason: 'regular' });
|
|
310
|
-
// Update all
|
|
398
|
+
// Update all subdivision selects for the new country (like old widget)
|
|
399
|
+
// Note: updateSubdivisionsFn is not available in handleFollowUpwardFromCountry,
|
|
400
|
+
// so we update all subdivision selects manually
|
|
311
401
|
const linkedSubdivisionSelects = Array.from(document.querySelectorAll(`.subdivision-selection[data-country="${select.dataset.name}"]`));
|
|
312
402
|
for (const s of linkedSubdivisionSelects) {
|
|
313
403
|
await updateSubdivisionSelectFn(s, apiKey, parentCode);
|
|
@@ -446,9 +536,7 @@ async function updateSubdivisionSelect(select, apiKey, backendUrl, state, config
|
|
|
446
536
|
reason: 'preselected',
|
|
447
537
|
});
|
|
448
538
|
valueSetByWidget = true;
|
|
449
|
-
await triggerFollowLogic(select, apiKey, backendUrl, state,
|
|
450
|
-
false, // followUpward - handled in event listeners
|
|
451
|
-
(s, key, code) => updateSubdivisionSelect(s, key, backendUrl, state, config, code));
|
|
539
|
+
await triggerFollowLogic(select, apiKey, backendUrl, state, config.followRelated, config.followUpward, (s, key, code) => updateSubdivisionSelect(s, key, backendUrl, state, config, code), (countrySelect, key) => updateSubdivisions(countrySelect, key, backendUrl, state, config));
|
|
452
540
|
}
|
|
453
541
|
else {
|
|
454
542
|
// Try GeoIP preselect
|
|
@@ -472,7 +560,7 @@ async function updateSubdivisionSelect(select, apiKey, backendUrl, state, config
|
|
|
472
560
|
reason: 'geoip',
|
|
473
561
|
});
|
|
474
562
|
valueSetByWidget = true;
|
|
475
|
-
await triggerFollowLogic(select, apiKey, backendUrl, state,
|
|
563
|
+
await triggerFollowLogic(select, apiKey, backendUrl, state, config.followRelated, config.followUpward, (s, key, code) => updateSubdivisionSelect(s, key, backendUrl, state, config, code), (countrySelect, key) => updateSubdivisions(countrySelect, key, backendUrl, state, config));
|
|
476
564
|
}
|
|
477
565
|
}
|
|
478
566
|
}
|
|
@@ -699,6 +787,8 @@ async function CountriesWidgetLoad(options = {}) {
|
|
|
699
787
|
subdivisionNameFilter: config.subdivisionNameFilter,
|
|
700
788
|
});
|
|
701
789
|
const subdivisionConfig = {
|
|
790
|
+
followRelated: config.followRelated || false,
|
|
791
|
+
followUpward: config.followUpward || false,
|
|
702
792
|
showSubdivisionType: config.showSubdivisionType !== false,
|
|
703
793
|
allowParentSelection: config.allowParentSelection || false,
|
|
704
794
|
subdivisionRomanizationPreference: config.subdivisionRomanizationPreference,
|