Skip to content

Commit 39d1f61

Browse files
author
Arthur Perrot
committed
feat: clean sources presets filtered via backend
1 parent 0af3fc6 commit 39d1f61

1 file changed

Lines changed: 39 additions & 10 deletions

File tree

templates/source_form.html

Lines changed: 39 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -140,14 +140,37 @@ <h1>{% if editing %}Edit calendar source{% else %}Connect a calendar{% endif %}<
140140

141141
var userEdited = urlInput.value !== '';
142142

143-
function syncBackendFromPreset() {
144-
// Presets carry a data-backend attribute; the "exchange" preset is EWS,
145-
// every other preset is CalDAV. Selecting a preset flips the Backend
146-
// dropdown so it stays consistent.
147-
var opt = select.options[select.selectedIndex];
148-
var backend = opt.getAttribute('data-backend') || 'caldav';
149-
if (typeSelect.value !== backend) {
150-
typeSelect.value = backend;
143+
// Snapshot of all presets at load time so we can rebuild the dropdown
144+
// after filtering by backend (some browsers ignore display:none on <option>).
145+
var allPresets = Array.prototype.map.call(select.options, function(o) {
146+
return {
147+
value: o.value,
148+
text: o.textContent,
149+
url: o.getAttribute('data-url') || '',
150+
backend: o.getAttribute('data-backend') || 'caldav',
151+
};
152+
});
153+
154+
function filterPresetsByBackend() {
155+
var backend = typeSelect.value;
156+
var previous = select.value;
157+
select.innerHTML = '';
158+
var matched = false;
159+
allPresets.forEach(function(p) {
160+
if (p.backend !== backend) return;
161+
var opt = document.createElement('option');
162+
opt.value = p.value;
163+
opt.textContent = p.text;
164+
opt.setAttribute('data-url', p.url);
165+
opt.setAttribute('data-backend', p.backend);
166+
if (p.value === previous) {
167+
opt.selected = true;
168+
matched = true;
169+
}
170+
select.appendChild(opt);
171+
});
172+
if (!matched && select.options.length > 0) {
173+
select.selectedIndex = 0;
151174
}
152175
}
153176

@@ -161,6 +184,7 @@ <h1>{% if editing %}Edit calendar source{% else %}Connect a calendar{% endif %}<
161184

162185
function updateProvider(autoFill) {
163186
var opt = select.options[select.selectedIndex];
187+
if (!opt) return;
164188
var id = opt.value;
165189
var url = opt.getAttribute('data-url');
166190
var isGoogle = (id === 'google');
@@ -184,13 +208,18 @@ <h1>{% if editing %}Edit calendar source{% else %}Connect a calendar{% endif %}<
184208
document.getElementById('google-connect').style.display = isGoogle ? 'block' : 'none';
185209
document.getElementById('basic-auth-fields').style.display = isGoogle ? 'none' : 'block';
186210

187-
syncBackendFromPreset();
188211
updateUrlLabel();
189212
}
190213

191214
urlInput.addEventListener('input', function() { userEdited = true; });
192215
select.addEventListener('change', function() { userEdited = false; updateProvider(true); });
193-
typeSelect.addEventListener('change', updateUrlLabel);
216+
typeSelect.addEventListener('change', function() {
217+
userEdited = false;
218+
filterPresetsByBackend();
219+
updateProvider(true);
220+
});
221+
222+
filterPresetsByBackend();
194223
updateProvider(false);
195224
})();
196225
</script>

0 commit comments

Comments
 (0)