@@ -83,30 +83,25 @@ fun TagConsumer<*>.BackfillSearchForm(
83
83
84
84
if (!datalist) return;
85
85
86
- // Check if datalist is already populated to avoid duplicate fetches
87
- if (datalist.children.length > 0) return;
88
-
89
86
// Fetch and populate backfill names
90
- function loadBackfillNames() {
91
- const url = '/services/$serviceName /variants/$variantName /backfill-names';
92
-
93
- fetch(url)
94
- .then(response => response.json())
95
- .then(data => {
96
- datalist.innerHTML = '';
97
- if (data.backfill_names) {
98
- data.backfill_names.forEach(name => {
99
- const option = document.createElement('option');
100
- option.value = name;
101
- datalist.appendChild(option);
102
- });
103
- }
104
- })
105
- .catch(err => console.error('Error loading backfill names:', err));
106
- }
87
+ const url = '/services/$serviceName /variants/$variantName /backfill-names';
88
+ fetch(url)
89
+ .then(response => response.json())
90
+ .then(data => {
91
+ datalist.innerHTML = '';
92
+ if (data.backfill_names) {
93
+ data.backfill_names.forEach(name => {
94
+ const option = document.createElement('option');
95
+ option.value = name;
96
+ datalist.appendChild(option);
97
+ });
98
+ }
99
+ })
100
+ .catch(err => console.error('Error loading backfill names:', err));
107
101
108
102
// Clear filters button handler
109
- if (clearFiltersBtn) {
103
+ if (clearFiltersBtn && !clearFiltersBtn.hasAttribute('data-initialized')) {
104
+ clearFiltersBtn.setAttribute('data-initialized', 'true');
110
105
clearFiltersBtn.addEventListener('click', function() {
111
106
let baseUrl = '/services/$serviceName ';
112
107
if ('$variantName ' !== 'default') {
@@ -115,19 +110,44 @@ fun TagConsumer<*>.BackfillSearchForm(
115
110
window.location.href = baseUrl;
116
111
});
117
112
}
118
-
119
- loadBackfillNames();
120
113
}
121
114
122
115
// Run on initial page load
123
116
if (document.readyState === 'loading') {
124
- document.addEventListener('DOMContentLoaded', initBackfillForm);
117
+ document.addEventListener('DOMContentLoaded', function() {
118
+ initBackfillForm();
119
+ setupAutoReloadWatcher();
120
+ });
125
121
} else {
126
122
initBackfillForm();
123
+ setupAutoReloadWatcher();
127
124
}
128
125
129
- // Simple periodic check to ensure dropdown stays populated after AutoReload
130
- setInterval(initBackfillForm, 5000); // Check every 5 seconds
126
+ function setupAutoReloadWatcher() {
127
+ // Watch for when the datalist gets replaced/removed by AutoReload
128
+ const observer = new MutationObserver(function(mutations) {
129
+ mutations.forEach(function(mutation) {
130
+ if (mutation.type === 'childList') {
131
+ // Check if nodes were added that contain our datalist
132
+ mutation.addedNodes.forEach(function(node) {
133
+ if (node.nodeType === Node.ELEMENT_NODE) {
134
+ if (node.id === 'backfill-names' ||
135
+ (node.querySelector && node.querySelector('#backfill-names'))) {
136
+ // Datalist was re-added, initialize it
137
+ setTimeout(initBackfillForm, 10);
138
+ }
139
+ }
140
+ });
141
+ }
142
+ });
143
+ });
144
+
145
+ // Start observing the entire document for changes
146
+ observer.observe(document.body, {
147
+ childList: true,
148
+ subtree: true
149
+ });
150
+ }
131
151
""" .trimIndent()
132
152
}
133
153
}
0 commit comments