Implements directory browser dialog, part 2: path navigator

This commit is contained in:
BinaryAlley committed 2024-09-17 21:27:05 +03:00
1 parent 4e4ecbbfca
commit a4f9199d2c
34 files changed
+1450 -200

No files matched your search

+104 -19
View File
@@ -1,25 +1,110 @@
/**
* Adjusts the height of a dropdown identified by `comboboxId` to fit in the available space.
* @param {string} comboboxId The ID of the combobox whose dropdown should be adjusted.
* @param {string} comboboxId The Id of the combobox whose dropdown should be adjusted.
*/
function adjustDropdownHeight(comboboxId) {
const combobox = document.getElementById(comboboxId);
const dropdown = combobox.querySelector('.enlightenment-dropdown');
// get the bounds of the combobox and the height of the viewport
const comboboxRect = combobox.getBoundingClientRect();
const viewportHeight = window.innerHeight;
// calculate the space available between the combobox and the bottom of the viewport, with a 10px margin
const dropdownTopPosition = comboboxRect.top + comboboxRect.height;
const availableSpace = viewportHeight - dropdownTopPosition - 10; // 10px margin
dropdown.style.maxHeight = `${availableSpace}px`;
function adjustDropdownHeight(comboboxId = null) {
const comboboxes = comboboxId
? [document.getElementById(comboboxId)] // if comboboxId is provided, use only that element
: document.querySelectorAll('[id^="combobox_"]:not([id^="combobox_checkbox_"])'); // otherwise, select all dropdowns
if (comboboxes) {
comboboxes.forEach(combobox => {
if (!combobox) return; // skip if no element is found
const dropdown = combobox.querySelector('.enlightenment-dropdown');
if (!dropdown) return; // skip if there's no dropdown inside
// get the bounds of the combobox and the height of the viewport
const comboboxRect = combobox.getBoundingClientRect();
const viewportHeight = window.innerHeight;
// calculate the space available between the combobox and the bottom of the viewport, with a 10px margin
const dropdownTopPosition = comboboxRect.top + comboboxRect.height;
const availableSpace = viewportHeight - dropdownTopPosition - 10; // 10px margin
// adjust the dropdown height
dropdown.style.maxHeight = `${availableSpace}px`;
});
}
}
window.showDirectoryPicker = async () => {
try {
const dirHandle = await window.showDirectoryPicker().ConfigureAwait(false);
return dirHandle.name;
} catch (err) {
console.error(err);
return null;
/**
* Gets the position and size of an element relative to the viewport.
*
* @param {string} id - The ID of the element to get the offset for.
* @returns {Object} An object with the top, left, width, and height of the element.
*/
function getElementOffset(id) {
const element = document.getElementById(id);
if (element) {
const rect = element.getBoundingClientRect();
return {
top: rect.top + window.scrollY,
left: rect.left + window.scrollX,
width: rect.width,
height: rect.height
};
}
};
return {
top: 0,
left: 0,
width: 0,
height: 0
};
}
/**
* Updates the position and height of the dropdown relative to the specified path segment.
*
* @param {string} pathSegmentId - The ID of the path segment element to position the dropdown relative to.
*/
function updateDropdownPosition(pathSegmentId) {
const element = document.getElementById(pathSegmentId);
const dropdown = document.getElementById('navigatorDropdown');
if (element && dropdown) {
const rect = getElementOffset(pathSegmentId);
dropdown.style.top = `${rect.top + rect.height}px`;
dropdown.style.left = `${rect.left}px`;
// calculate the available height between the dropdown and the bottom of the viewport
const viewportHeight = window.innerHeight;
const availableHeight = viewportHeight - (rect.top + rect.height) - 10; // 10 px margin
// set the dropdown's max height, ensuring it doesn't exceed the available height
dropdown.style.maxHeight = `${availableHeight}px`;
}
}
/**
* Unchecks all checkboxes except the one with the specified ID.
*
* @param {string} id - The Id of the checkbox to leave checked.
*/
function uncheckAllExcept(id) {
const checkboxes = document.querySelectorAll('.navigator-toggle-checkbox');
checkboxes.forEach((checkbox) => {
if (checkbox.id !== id) {
checkbox.checked = false;
}
});
}
/**
* Enables horizontal scrolling for the address bar using the mouse wheel.
*/
function makeAddressBarHorizontallyScrollable() {
const addressBar = document.getElementById('navigator-address-bar');
if (addressBar) {
addressBar.addEventListener('wheel', function (event) {
event.preventDefault();
this.scrollLeft += (event.deltaY > 0 ? 1 : -1) * 80;
}, { passive: false });
}
}
/**
* Handles the window resize event.
*/
window.addEventListener('resize', function () {
adjustDropdownHeight();
// update all dropdowns' positions on resize
const dropdown = document.getElementById('navigatorDropdown');
if (dropdown) {
const pathSegmentId = dropdown.getAttribute('data-path-segment-id');
if (pathSegmentId)
updateDropdownPosition(pathSegmentId);
}
});