Capture accumulated application, UI, CLI, regression tests and design documentation changes, including task project mapping and default CalDAV list naming. Keep deployment data, backups, local credentials and build artifacts out of Git. Move the Compose enrollment token to an ignored local environment file.
102 lines
5.0 KiB
JavaScript
102 lines
5.0 KiB
JavaScript
(() => {
|
|
const dialog = document.querySelector('[data-path-picker-dialog]');
|
|
if (!dialog) return;
|
|
const list = dialog.querySelector('[data-path-picker-list]');
|
|
const status = dialog.querySelector('[data-path-picker-status]');
|
|
const current = dialog.querySelector('[data-path-picker-current]');
|
|
const up = dialog.querySelector('[data-path-picker-up]');
|
|
const chooseFolder = dialog.querySelector('[data-path-picker-folder]');
|
|
const choose = dialog.querySelector('[data-path-picker-select]');
|
|
let target = null, mode = 'any', path = '/', parent = '/', selected = '', request = null;
|
|
|
|
const labels = {
|
|
loading: dialog.dataset.loading,
|
|
empty: dialog.dataset.empty,
|
|
failed: dialog.dataset.failed,
|
|
truncated: dialog.dataset.truncated,
|
|
};
|
|
const formatBytes = value => {
|
|
if (!Number.isFinite(value) || value <= 0) return '';
|
|
const units = ['B', 'KiB', 'MiB', 'GiB'];
|
|
let number = value, index = 0;
|
|
while (number >= 1024 && index < units.length - 1) { number /= 1024; index++; }
|
|
return number.toFixed(index ? 1 : 0) + ' ' + units[index];
|
|
};
|
|
const commit = value => {
|
|
if (!target) return;
|
|
target.value = value;
|
|
target.dispatchEvent(new Event('input', {bubbles: true}));
|
|
target.dispatchEvent(new Event('change', {bubbles: true}));
|
|
dialog.close();
|
|
target.focus();
|
|
};
|
|
const markSelected = value => {
|
|
selected = value;
|
|
choose.disabled = !selected;
|
|
list.querySelectorAll('[role=option]').forEach(item => item.setAttribute('aria-selected', String(item.dataset.path === selected)));
|
|
};
|
|
const load = async next => {
|
|
request?.abort();
|
|
request = new AbortController();
|
|
status.textContent = labels.loading;
|
|
list.replaceChildren();
|
|
choose.disabled = true;
|
|
selected = '';
|
|
try {
|
|
const response = await fetch('/_/api/v1/path-picker?path=' + encodeURIComponent(next || '/') + '&mode=' + encodeURIComponent(mode), {headers: {Accept: 'application/json'}, credentials: 'same-origin', cache: 'no-store', signal: request.signal});
|
|
if (!response.ok) throw new Error('picker unavailable');
|
|
const data = await response.json();
|
|
path = data.path || '/'; parent = data.parent || '/'; current.textContent = path;
|
|
up.disabled = path === '/';
|
|
chooseFolder.hidden = !(mode === 'folder' || mode === 'any' || mode === 'image-or-folder');
|
|
const fragment = document.createDocumentFragment();
|
|
for (const entry of data.entries || []) {
|
|
const item = document.createElement('button');
|
|
item.type = 'button'; item.className = 'config-path-picker-item'; item.dataset.path = entry.path;
|
|
item.setAttribute('role', 'option'); item.setAttribute('aria-selected', 'false');
|
|
const icon = document.createElement('span'); icon.textContent = entry.kind === 'folder' ? '▰' : (entry.content_type || '').startsWith('image/') ? '▧' : '▤'; icon.setAttribute('aria-hidden', 'true');
|
|
const name = document.createElement('span'); name.textContent = entry.name;
|
|
const detail = document.createElement('small'); detail.textContent = entry.kind === 'folder' ? '' : formatBytes(entry.size);
|
|
item.append(icon, name, detail);
|
|
if (entry.kind === 'folder') item.addEventListener('click', () => load(entry.path));
|
|
else {
|
|
item.addEventListener('click', () => markSelected(entry.path));
|
|
item.addEventListener('dblclick', () => commit(entry.path));
|
|
}
|
|
fragment.append(item);
|
|
}
|
|
list.append(fragment);
|
|
if (data.selected) markSelected(data.selected);
|
|
status.textContent = data.truncated ? labels.truncated : (data.entries || []).length ? '' : labels.empty;
|
|
} catch (error) {
|
|
if (error.name !== 'AbortError') status.textContent = labels.failed;
|
|
}
|
|
};
|
|
|
|
document.addEventListener('click', event => {
|
|
const opener = event.target.closest('[data-path-picker]');
|
|
if (!opener) return;
|
|
target = document.getElementById(opener.dataset.pickerTarget);
|
|
if (!target) return;
|
|
mode = opener.dataset.pickerMode || 'any';
|
|
dialog.showModal();
|
|
load(target.value.trim() || '/');
|
|
});
|
|
up.addEventListener('click', () => load(parent));
|
|
chooseFolder.addEventListener('click', () => commit(path));
|
|
choose.addEventListener('click', () => selected && commit(selected));
|
|
dialog.querySelectorAll('[data-path-picker-close]').forEach(button => button.addEventListener('click', () => dialog.close()));
|
|
dialog.addEventListener('close', () => request?.abort());
|
|
dialog.addEventListener('click', event => {
|
|
if (event.target !== dialog) return;
|
|
const box = dialog.getBoundingClientRect();
|
|
if (event.clientX < box.left || event.clientX > box.right || event.clientY < box.top || event.clientY > box.bottom) dialog.close();
|
|
});
|
|
|
|
document.querySelectorAll('[data-range-output]').forEach(input => {
|
|
const output = document.getElementById(input.dataset.rangeOutput);
|
|
const update = () => { if (output) output.value = input.value + '%'; };
|
|
input.addEventListener('input', update); update();
|
|
});
|
|
})();
|