// because this script can be reused many times and only when needed, we need not polute the global space with the declarations - // wrap everything in an IIFE (Immediately Invoked Function Expression), which creates a scope each time (function () { console.log('Loaded File System Explorer script'); //+======================================================================================+ //| DOM Elements | //+======================================================================================+ const modalBackgroundMenu = document.getElementById('modal-background-menu'); const modalBackgroundArticle = document.getElementById('modal-background-article'); const fileSystemBrowserDialog = document.getElementById('file-system-browser-dialog'); const fileSystemBrowserTreeview = document.getElementById('file-system-browser-treeview'); const pathSegmentsContainer = document.getElementById('navigator-path-segments'); const article = document.querySelector('article'); const fileSystemTreeViewContainer = document.getElementById('file-system-browser-file-system-treeview-container'); const fileSystemExplorerContainer = document.getElementById('file-system-browser-file-system-explorer-container'); const fileSystemExplorer = document.getElementById('file-system-browser-file-system-explorer'); const fileSystemDetailsHeader = document.getElementById('file-system-browser-file-system-explorer-details-header'); const addressBarInput = document.getElementById('navigator-address-bar-input'); const addressBar = document.getElementById('navigator-address-bar'); const btnNavigatorNavigateBack = document.getElementById('navigator-navigate-back'); const btnNavigatorNavigateForward = document.getElementById('navigator-navigate-forward'); const btnNavigatorNavigateUp = document.getElementById('navigator-navigate-up'); const btnNavigatorToggleTreeView = document.getElementById('navigator-toggle-tree-view-button'); const btnNavigatorToggleThumbnails = document.getElementById('navigator-toggle-thumbnails-button'); const btnNavigatorToggleSelectionMode = document.getElementById('navigator-toggle-selection-button'); const btnNavigatorHiddenElements = document.getElementById('navigator-toggle-hidden-button'); const btnEditPath = document.getElementById('navigator-edit-path-button'); const svgTreeView = document.querySelectorAll('.navigator-icon-fill.tree-view'); const svgThumbnails = document.querySelectorAll('.navigator-icon-fill.thumbnails'); const svgSelectionMode = document.querySelectorAll('.navigator-icon-fill.selection-mode'); const svgHiddenElements = document.querySelectorAll('.navigator-icon-fill.hidden-elements'); const svgListView = document.querySelectorAll('.navigator-icon-fill.list'); const svgDetailsView = document.querySelectorAll('.navigator-icon-fill.details'); const svgSmallIconsView = document.querySelectorAll('.navigator-icon-fill.small-icons'); const svgMediumIconsView = document.querySelectorAll('.navigator-icon-fill.medium-icons'); const svgLargeIconsView = document.querySelectorAll('.navigator-icon-fill.large-icons'); const svgExtraLargeIconsView = document.querySelectorAll('.navigator-icon-fill.extra-large-icons'); const btnListView = document.getElementById('navigator-list-view'); const btnDetailsView = document.getElementById('navigator-details-view'); const btnSmallIconsView = document.getElementById('navigator-small-icons-view'); const btnMediumIconsView = document.getElementById('navigator-medium-icons-view'); const btnLargeIconsView = document.getElementById('navigator-large-icons-view'); const btnExtraLargeIconsView = document.getElementById('navigator-extra-large-icons-view'); const btnFileSystemBrowserEditPath = document.getElementById('navigator-edit-path-button'); const btnFileSystemBrowserSubmitPath = document.getElementById('navigator-navigate-button'); const btnFileSystemBrowserConfirm = document.getElementById('file-system-browser-confirm-button'); const btnFileSystemBrowserCancel = document.getElementById('file-system-browser-cancel-button'); const selectedFileSystemElementsInput = document.getElementById('file-system-browser-selected-element'); const selectionRectangle = document.getElementById('selection-rectangle'); const visibleSelectionRectangle = document.getElementById('visible-selection-rectangle'); //+======================================================================================+ //| Variables | //+======================================================================================+ // base URL of the file type icons of the active theme, empty when no theme is applied, in which case no icons are rendered let iconBaseUrl = ''; // URL of the theme file icons mapping, mapping file extensions to icon paths, empty when no theme is applied let fileIconsUrl = ''; // the file extension to icon path mapping of the active theme, null until the mapping is loaded let fileIcons = null; // the raw theme sub templates for the dynamic content, null when the theme does not provide a template and the built-in markup is used instead let treeNodeTemplate = null; let explorerItemTemplate = null; let pathSegmentTemplate = null; let clientBasePath; let _path; let pathSeparator = '/'; let platformType = 'Unix'; let showFileSystemTreeView = true; // TODO: take from config let showHiddenElements = true; // TODO: take from config let includeFiles = true; // TODO: take from config let viewMode = 'list-icons'; // TODO: take from config let undoStack = []; let redoStack = []; let selectionStartPosition = { x: 0, y: 0 }; let currentMousePosition = { x: 0, y: 0 }; let isSelecting = false; let isSelectionMode = false; let scrollTimeout; // timeout to delay thumbnail loading after scrolling let resizeTimeout; // timeout to delay thumbnail loading after resizing let abortController; // allows the cancellation of thumbnail retrieval jobs let hasScrolledAfterModeChange = false; // flag to track if scrolling occurred after a mode change let showThumbnails = true; // whether to show thumbnails or not let scrollThumbnailRetrievalTimeout = 1000; // the time span to wait after the last scroll, before getting thumbnails let inspectFileForThumbnails = false; // whether to check the actual header bytes in order to determine if a file is an image, rather than its extension let thumbnailsRetrievalBatchSize = 20; // the number of thumbnails to ask from the server, concurrently let imagePreviewsQuality = 70; // the quality used for image thumbnails let fullImageQuality = 90; // the quality used for full images //+======================================================================================+ //| Initialization | //+======================================================================================+ /** * Initializes the values needed by the file sytem browser. * @param {any} config The configuration of the file system browser. */ async function initFileSystemBrowser(config) { clientBasePath = config.clientBasePath; iconBaseUrl = config.iconBaseUrl || ''; fileIconsUrl = config.fileIconsUrl || ''; readSubTemplates(); await getFileSystemPropertiesAsync(); if (fileIconsUrl !== '') await loadFileIconsAsync(); let initialPath = config.path; if (!initialPath.endsWith(pathSeparator)) initialPath += pathSeparator; _path = initialPath; addressBarInput.value = initialPath; showFileSystemTreeView = true; // TODO: take from config addHorizontalScrolling(addressBar); switchViewMode(config.viewMode, config.viewMode === 'list' ? null : config.iconSize, true); } /** * Reads the raw theme sub templates of the dynamic file system browser content from the template elements of the component. */ function readSubTemplates() { treeNodeTemplate = getSubTemplate('fsb-template-tree-node'); explorerItemTemplate = getSubTemplate('fsb-template-explorer-item'); pathSegmentTemplate = getSubTemplate('fsb-template-path-segment'); } /** * Gets the raw source of a theme sub template element, or null when the theme does not provide one. * The theme inlines the sub template sources into plain text script elements, so that the browser never parses the theme markup into live elements, * which could otherwise make it request their resources, and the raw source is recovered by reading the element text. * @param {string} id - The id of the template element. * @returns {string|null} The raw template source, or null when the element is missing. */ function getSubTemplate(id) { const templateElement = document.getElementById(id); if (!templateElement) return null; // the theme template engine may be absent on a partially loaded page, in which case the built-in markup is used if (typeof renderThemeTemplate !== 'function') return null; // an empty template element means the theme does not provide the template, in which case the built-in markup is used return templateElement.textContent || null; } /** * Loads the file extension to icon path mapping of the active theme, degrading to the default icons when the mapping cannot be loaded. */ async function loadFileIconsAsync() { try { const response = await fetch(fileIconsUrl); if (response.ok) fileIcons = await response.json(); } catch (error) { fileIcons = null; } } /** * Resolves the URL of the icon of a file system entry, based on the file extension mapping of the active theme. * @param {string} filename - The full name of the file including its extension. * @param {string} type - The type of the entry, 'Directory' or 'File'. * @returns {string} The icon URL, or an empty string when the active theme does not provide icons. */ function resolveIconUrl(filename, type) { if (iconBaseUrl === '') return ''; if (type === 'Directory') { const iconPath = fileIcons && fileIcons.directory ? fileIcons.directory : 'directory.svg'; return iconBaseUrl + '/' + iconPath; } const fileExtension = filename.split('.').pop().toLowerCase(); const iconPath = fileIcons && fileIcons[fileExtension] ? fileIcons[fileExtension] : (fileIcons && fileIcons.default ? fileIcons.default : 'file.svg'); return iconBaseUrl + '/' + iconPath; } /** * Shows the file system browser dialog. */ async function showFileSystemBrowserDialogAsync(path, showHiddenElementsValue, includeFilesValue, onConfirmCallback) { if (path !== undefined && path !== null) { showHiddenElements = showHiddenElementsValue; includeFiles = includeFilesValue; modalBackgroundMenu.style.display = 'block'; modalBackgroundArticle.style.display = 'block'; fileSystemBrowserDialog.style.display = 'block'; if (!path.endsWith(pathSeparator)) path += pathSeparator; addressBarInput.value = path; await toggleShowFileSystemHiddenElementsAsync(false); await toggleFileSystemTreeViewAsync(false); await toggleShowFileSystemElementsThumbnailsAsync(false); await navigateToPathAsync(path, false, false); // add click handler for confirm button btnFileSystemBrowserConfirm.onclick = function () { const selectedPaths = selectedFileSystemElementsInput.value; if (onConfirmCallback && typeof onConfirmCallback === 'function') onConfirmCallback(selectedPaths); // hide dialog hideFileSystemBrowserDialog(); }; btnFileSystemBrowserCancel.onclick = hideFileSystemBrowserDialog; } else notificationService.Show("Path cannot be null!", NotificationType.ERROR); // TODO: should ask error message from server when translation is implemented } /** * Hides the file system browser dialog. */ function hideFileSystemBrowserDialog() { modalBackgroundMenu.style.display = 'none'; modalBackgroundArticle.style.display = 'none'; fileSystemBrowserDialog.style.display = 'none'; } /** * Retrieves file system properties from the server, asynchronously. * @returns {Promise} A promise that resolves once the properties have been fetched. */ async function getFileSystemPropertiesAsync() { // call the API to get the file system type (platform type) const fileSystemTypeResponse = await callApiGetAsync(`${clientBasePath}file-system/api-get-type`); if (fileSystemTypeResponse !== undefined) platformType = fileSystemTypeResponse.platformType; // call the API to get the path separator for the file system const pathSeparatorResponse = await callApiGetAsync(`${clientBasePath}path/api-get-path-separator`); if (pathSeparatorResponse !== undefined) pathSeparator = pathSeparatorResponse.pathSeparator; } /** * Checks if the specified path is a valid file system path, and exists. * @param {any} path The path to validate and check. * @returns {Promise} A promise that resolves to whether the path is valid and exists, or not. */ async function checkPathAsync(path) { const pathValidResponse = await callApiGetAsync(`${clientBasePath}path/api-validate?path=${encodeURIComponent(path)}`); if (pathValidResponse === undefined) { // the API rejected the path with a validation error (e.g. it is empty), treat it as invalid and restore the address bar addressBarInput.value = _path; if (addressBarInput.value !== null && !addressBarInput.value.endsWith(pathSeparator)) addressBarInput.value += pathSeparator; return false; } if (!pathValidResponse.isValid) { notificationService.show("Specified path is not valid!", NotificationType.ERROR); // TODO: should ask error message from server when translation is implemented addressBarInput.value = _path; if (addressBarInput.value !== null && !addressBarInput.value.endsWith(pathSeparator)) addressBarInput.value += pathSeparator; } else { const pathExistsResponse = await callApiGetAsync(`${clientBasePath}path/api-check-path-exists?path=${encodeURIComponent(path)}&shouldIncludeHiddenElements=${showHiddenElements}`); if (pathExistsResponse !== undefined && !pathExistsResponse.exists) { notificationService.show("Specified path does not exist!", NotificationType.ERROR); // TODO: should ask error message from server when translation is implemented return false; } } return pathValidResponse.isValid; } /** * Navigates to the specified path, * @param {any} path The path o navigate to. * @param {any} isForwardNavigation Whether the Forward button was clicked, and navigation is done to a history location or not. * @param {any} isBackwardNavigation Whether the Back button was clicked, and navigation is done to a history location or not. */ async function navigateToPathAsync(path, isForwardNavigation, isBackwardNavigation) { if (await checkPathAsync(path)) { // if there was a job for thumbnails retrieval, cancel it first if (abortController) { abortController.abort(); abortController = null; } // check if backward navigation is possible if (isBackwardNavigation) { if (undoStack.length > 0) { // get the first path in the backward navigation list const backwardPath = undoStack.pop(); // put the current path in the forward navigationlist, before navigating to the backward path if (addressBarInput.value !== null) { if (!addressBarInput.value.endsWith(pathSeparator)) addressBarInput.value += pathSeparator; redoStack.push(addressBarInput.value); } path = backwardPath; } else return; } else if (isForwardNavigation) { // check if forward navigation is possible if (redoStack.length > 0) { // get the first path in the forward navigation list const forwardPath = redoStack.pop(); // put the current path in the backward navigation list, before navigating to the forward path if (addressBarInput.value !== null) { if (!addressBarInput.value.endsWith(pathSeparator)) addressBarInput.value += pathSeparator; if (undoStack.length === 0 || (undoStack.length > 0 && undoStack[undoStack.length - 1] !== addressBarInput.value)) undoStack.push(addressBarInput.value); } path = forwardPath; } else return; } else { // navigation to a brand new location (up, directory browse, manual path edit, etc) redoStack = []; if (addressBarInput.value !== null && addressBarInput.value !== path) { // when navigating up, addressBarInput.value will be set after navigation if (undoStack.length === 0 || (undoStack.length > 0 && undoStack[undoStack.length - 1] !== addressBarInput.value)) undoStack.push(addressBarInput.value); } else if (_path !== null && _path !== path) { // when editing the address, addressBarInput.value already comes set, check against control value if (undoStack.length === 0 || (undoStack.length > 0 && undoStack[undoStack.length - 1] !== _path)) undoStack.push(_path); } } _path = path; // we are about to navigate to the new path, store its value in the control variable addressBarInput.style.display = 'none'; // hide #address-bar-input addressBar.style.display = 'block'; // show #address-bar await getNavigatorPathAsync(path); await getFileSystemItemsAsync(path); if (showFileSystemTreeView) await getFileSystemTreeViewAsync(path); if (showThumbnails) getFileSystemVisibleItems(); } addressBarInput.value = _path; if (addressBarInput.value !== null && !addressBarInput.value.endsWith(pathSeparator)) addressBarInput.value += pathSeparator; } /** * Navigates up one level from the current path */ async function navigateUpAsync() { if (addressBarInput.value !== null) { // ask the API for the parent path const parentLocationResponse = await callApiGetAsync(`${clientBasePath}path/api-get-path-parent?path=${encodeURIComponent(addressBarInput.value)}`); if (parentLocationResponse !== undefined && parentLocationResponse.pathSegments !== null) { // if the answer was valid, and we can navigate up, reconstruct the provided path from its segments, and navigate to it platformType = parentLocationResponse.pathSegments; let parentConcatenatedPath = parentLocationResponse.pathSegments[0].path; for (var i = 1; i < parentLocationResponse.pathSegments.length; i++) parentConcatenatedPath = (parentConcatenatedPath !== pathSeparator ? parentConcatenatedPath : '') + pathSeparator + parentLocationResponse.pathSegments[i].path; if (!parentConcatenatedPath.endsWith(pathSeparator)) parentConcatenatedPath += pathSeparator; await navigateToPathAsync(parentConcatenatedPath, false, false); } } } //+======================================================================================+ //| Navigator | //+======================================================================================+ /** * Gets the navigator path segments. * @param {any} path The path for which to get the navigator path segments. */ async function getNavigatorPathAsync(path) { const pathSplitResponse = await callApiGetAsync(`${clientBasePath}path/api-split?path=${encodeURIComponent(path)}`); if (pathSplitResponse !== undefined) renderAddressBar(pathSplitResponse.pathSegments); } /** * Render the address bar based on the provided path segments. * @param {Array} pathSegments - An array of path segments. */ function renderAddressBar(pathSegments) { pathSegmentsContainer.removeEventListener('change', handlePathSegmentComboboxChangeAsync); // remove previous listeners pathSegmentsContainer.addEventListener('change', handlePathSegmentComboboxChangeAsync); // add new listener pathSegmentsContainer.innerHTML = ""; // clear existing segments if (pathSegments !== null) { pathSegments.forEach((segment, index) => { const li = renderPathSegment(segment, index, pathSegments); pathSegmentsContainer.appendChild(li); }); } else pathSegmentsContainer.querySelectorAll('li').forEach(li => li.remove()); // no address, clear path segments } /** * Renders a single path segment of the address bar, using the theme sub template when the active theme provides one, or the built-in markup otherwise. * @param {Object} segment - The path segment data. * @param {number} index - The index of the segment in the address bar. * @param {Array} pathSegments - All the path segments of the address bar. * @returns {HTMLElement} The created list item element of the path segment. */ function renderPathSegment(segment, index, pathSegments) { const concatenatedPath = (platformType === "Unix" ? '/' : '') + pathSegments.slice(0, index + 1).map(segmentItem => segmentItem.path).join(pathSeparator); const dataPath = concatenatedPath + (!concatenatedPath.endsWith(pathSeparator) ? pathSeparator : ''); if (pathSegmentTemplate !== null) { const html = renderThemeTemplate(pathSegmentTemplate, { id: `path-segment-${index}`, path: segment.path, dataPath: dataPath, toggleId: `segment-toggle-${index}`, dropdownId: `navigator-dropdown-${index}` }); const container = document.createElement("div"); container.innerHTML = html; return container.firstElementChild; } const li = document.createElement("li"); li.id = `path-segment-${index}`; // create the combobox const combobox = document.createElement("div"); combobox.className = "navigator-combobox inline-block"; // the shine effect const shineEffect = document.createElement("div"); shineEffect.className = "shine-effect"; shineEffect.style.top = "1px"; combobox.appendChild(shineEffect); const toggleCheckbox = document.createElement("input"); toggleCheckbox.type = "checkbox"; toggleCheckbox.className = "navigator-toggle-checkbox"; toggleCheckbox.id = `segment-toggle-${index}`; combobox.appendChild(toggleCheckbox); const toggleLabel = document.createElement("label"); toggleLabel.className = "navigator-toggle"; toggleLabel.htmlFor = `segment-toggle-${index}`; const span = document.createElement("span"); span.className = "navigator-selected-text"; span.innerText = segment.path; toggleLabel.appendChild(span); // add the arrow element const arrowSpan = document.createElement("span"); arrowSpan.className = "navigator-arrow"; toggleLabel.appendChild(arrowSpan); combobox.appendChild(toggleLabel); const dropdown = document.createElement("div"); dropdown.className = "navigator-dropdown"; dropdown.id = `navigator-dropdown-${index}`; dropdown.setAttribute('data-path-segment-id', `path-segment-${index}`); combobox.setAttribute('data-path', dataPath); combobox.appendChild(dropdown); li.appendChild(combobox); return li; } /** * Handle the change event for the comboboxes inside the path-segments. * This function is triggered when any of the comboboxes (checkboxes) in the navigation bar is toggled. * @param {Event} e - The change event object. */ async function handlePathSegmentComboboxChangeAsync(e) { // ensure the event was triggered by an element with the 'navigator-toggle-checkbox' class if (e.target.classList.contains('navigator-toggle-checkbox')) { const comboboxId = e.target.id; const dropdownId = comboboxId.replace('segment-toggle-', 'navigator-dropdown-'); const dropdown = document.getElementById(dropdownId); reattachDropdown(); if (!e.target.checked) return; const comboboxElement = e.target.closest('.navigator-combobox'); // get the closest parent (or self) with the specified class const pathValue = comboboxElement.getAttribute('data-path'); // retrieve the data-path attribute value // clear any existing dropdown elements dropdown.innerHTML = ""; showBusyIndicator(); setupDropdown(dropdown, e.target); // add combobox entry for current directory addDirectoryOption(dropdown, pathValue, ".", async () => { addressBarInput.value = pathValue; await navigateToPathAsync(pathValue, false, false); }); // add combobox entry for parent directory addDirectoryOption(dropdown, pathValue, "..", async () => { addressBarInput.value = pathValue; await navigateUpAsync(); }); // fetch directory data for the path try { await fetchFileSystemDirectoriesAsync(pathValue, (parsedItem) => { addDirectoryOption(dropdown, pathValue, parsedItem.name, async () => { const newPath = pathValue + (!pathValue.endsWith(pathSeparator) ? pathSeparator : '') + parsedItem.name + pathSeparator; addressBarInput.value = newPath; await navigateToPathAsync(newPath, false, false); }); }); } catch (error) { console.error('Error:', error); notificationService.show(error, NotificationType.ERROR); } finally { hideBusyIndicator(); } } } /** * Configures and positions the dropdown for directory navigation. * @param {HTMLElement} dropdown - The dropdown element to set up. * @param {HTMLElement} toggleElement - The element that triggered the dropdown. */ function setupDropdown(dropdown, toggleElement) { // when drop down opens, need to show address bar overflow, otherwise clipping of drop down occurs // store the id of the parent combobox of the dropdown - the dropdown will be reparented // because of manadatory "overflow hidden" of scrollable area and clipping isues dropdown.setAttribute('data-parent', toggleElement.id); dropdown.setAttribute('data-detached', true); const position = toggleElement.closest('.navigator-combobox').getBoundingClientRect(); // detach dropdown and reposition document.body.appendChild(dropdown); dropdown.style.position = 'absolute'; dropdown.style.top = `${position.bottom}px`; dropdown.style.left = `${position.left}px`; dropdown.style.display = 'block'; // handle addressBar scroll to adjust dropdown position addressBar.addEventListener('scroll', adjustDropdownPosition); } /** * Adds a directory option to the dropdown. * @param {HTMLElement} dropdown - The dropdown to add the option to. * @param {string} pathValue - The current path. * @param {string} name - The name of the directory option. * @param {Function} clickHandler - Function to call when the option is clicked. */ function addDirectoryOption(dropdown, pathValue, name, clickHandler) { const directoryDiv = document.createElement('div'); directoryDiv.className = "navigator-option"; directoryDiv.dataset.value = pathValue + (name !== "." && name !== ".." ? name : ""); directoryDiv.textContent = name; directoryDiv.addEventListener('click', clickHandler); dropdown.appendChild(directoryDiv); } /** * Re-attaches the detached visible drop down to the path segment or platform combobox it originally belonged to */ function reattachDropdown() { // when the combobox closes, re-attach the detached dropdown const dropdown = document.querySelector('.navigator-dropdown[data-detached="true"], .enlightenment-dropdown[data-detached="true"]'); if (dropdown) { // reset its absolute positioning and stuff dropdown.style.position = ''; dropdown.style.top = ''; dropdown.style.left = ''; dropdown.style.display = ''; // re-attach it to original parent const dataParent = dropdown.getAttribute('data-parent'); dropdown.removeAttribute('data-detached'); const originalParent = document.getElementById(dataParent).parentNode; if (originalParent) originalParent.appendChild(dropdown); // remove scroll event listener from the addressBar addressBar.removeEventListener('scroll', adjustDropdownPosition); } } /** * Adjusts the position of a detached drop down, such that it is always just under its original * path segment combobox, even when this parent is moved through scrolling */ function adjustDropdownPosition() { const dropdown = document.querySelector('.navigator-dropdown[data-detached="true"]'); if (dropdown) { const comboboxElement = document.getElementById(dropdown.getAttribute('data-parent')); // get the related combobox const position = comboboxElement.parentNode.getBoundingClientRect(); dropdown.style.top = position.bottom + 'px'; dropdown.style.left = position.left + 'px'; } } /** * 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, dropdown) { const element = document.getElementById(pathSegmentId); if (element && dropdown) { const rect = getElementOffset(pathSegmentId); dropdown.style.top = `${rect.top + rect.height}px`; dropdown.style.left = `${rect.left}px`; } } //+======================================================================================+ //| Tree View | //+======================================================================================+ /** * Gets the file system tree view * @param {string} initialPath - The initial path to expand in the tree view (optional). * @returns {Promise} */ async function getFileSystemTreeViewAsync(initialPath) { // clear the existing tree view fileSystemBrowserTreeview.innerHTML = ''; // fetch available drives asynchronously const drives = await getFileSystemDrivesAsync(); // iterate through each drive and add it to the tree view as root node for (const drive of drives) { // create a tree node for the drive const driveNode = createFileSystemTreeViewTreeNode(drive); // append the drive node to the tree view fileSystemBrowserTreeview.appendChild(driveNode); // if the initial path starts with this drive's path, expand the tree view to show the initial path if (initialPath.toLowerCase().startsWith(drive.path.toLowerCase())) await expandInitialTreeViewPathAsync(driveNode, initialPath); } } /** * Asynchronously fetches the list of available drives from the server. * @returns {Promise|undefined>} A promise that resolves to an array of drive objects, or undefined if the API call fails. */ async function getFileSystemDrivesAsync() { const getDrivesResponse = await callApiGetAsync(`${clientBasePath}drives/api-get-drives`); if (getDrivesResponse !== undefined) return getDrivesResponse.drives; } /** * Creates a tree node element for the file system tree view. * @param {Object} nodeData - Data for the node (path, itemType, name). * @returns {HTMLElement} The created tree node element. */ function createFileSystemTreeViewTreeNode(nodeData) { if (treeNodeTemplate !== null) { const html = renderThemeTemplate(treeNodeTemplate, { path: nodeData.path, name: nodeData.name, itemType: nodeData.itemType, isRoot: nodeData.itemType === 'Root', isDirectory: nodeData.itemType !== 'File', isFile: nodeData.itemType === 'File', isExpanded: !!nodeData.isExpanded, icon: getNodeIcon(nodeData) }); const container = document.createElement("div"); container.innerHTML = html; const treeNodeDiv = container.firstElementChild; // the theme template must keep the DOM contract the script binds its events to const nodeContent = treeNodeDiv.querySelector('.node-content'); if (nodeContent) nodeContent.addEventListener('click', handleNodeClickAsync); const expandButton = treeNodeDiv.querySelector('.expand-button'); if (expandButton) expandButton.addEventListener('click', toggleFileSysemTreeViewNodeExpandAsync); return treeNodeDiv; } const treeNodeDiv = document.createElement("div"); treeNodeDiv.className = "tree-node"; treeNodeDiv.setAttribute('data-path', nodeData.path); const nodeContent = document.createElement("span"); nodeContent.className = "node-content"; nodeContent.addEventListener('click', handleNodeClickAsync); const expandButton = document.createElement("span"); expandButton.className = nodeData.itemType !== 'File' ? "expand-button" : "spacer"; expandButton.addEventListener('click', toggleFileSysemTreeViewNodeExpandAsync); const nodeIcon = document.createElement("span"); nodeIcon.className = "node-icon"; nodeIcon.textContent = getNodeIcon(nodeData); const nodeName = document.createElement("span"); nodeName.className = "node-name"; nodeName.textContent = nodeData.name; // assemble the node structure nodeContent.appendChild(expandButton); nodeContent.appendChild(nodeIcon); nodeContent.appendChild(nodeName); treeNodeDiv.appendChild(nodeContent); const childNodes = document.createElement("div"); childNodes.className = "child-nodes"; treeNodeDiv.appendChild(childNodes); return treeNodeDiv; } /** * Handles click events on tree nodes. * @param {Event} e - The click event object. */ async function handleNodeClickAsync(e) { // get the node that was clicked const treeNode = e.target.closest('.tree-node'); // retrieve the path stored on the clicked node selectedPath = treeNode.getAttribute('data-path'); if (selectedPath !== null) { // ensure path ends with separator if (!selectedPath.endsWith(pathSeparator)) selectedPath += pathSeparator; // set the retrieved path as the navigator path, and navigate to it addressBarInput.value = selectedPath; await navigateToPathAsync(selectedPath, false, false); } } /** * Toggles the expansion of a file system tree view node. * @param {Event} e - The click event on the expand button. */ async function toggleFileSysemTreeViewNodeExpandAsync(e) { e.stopPropagation(); // get the node that was clicked const treeNode = e.target.closest('.tree-node'); const treeNodeIcon = treeNode.querySelector('.node-icon'); const nodeData = { itemType: 'Directory' }; const expandButton = e.target; // get the div that hosts child nodes in tthe clicked node const childNodes = treeNode.querySelector('.child-nodes'); const path = treeNode.getAttribute('data-path'); // get the current expanded state of the clicked node, and toggle it const isExpanded = expandButton.classList.contains('expanded'); nodeData.isExpanded = !isExpanded; if (treeNodeIcon) treeNodeIcon.textContent = getNodeIcon(nodeData); // expand or collapse the node if (isExpanded) { expandButton.classList.remove('expanded'); if (childNodes) childNodes.style.display = 'none'; } else { expandButton.classList.add('expanded'); if (childNodes) { childNodes.style.display = 'block'; // if node is expanded and child nodes are not yet loaded, load them if (childNodes.children.length === 0) await loadFileSystemTreeViewChildNodesAsync(treeNode, path); } } } /** * Loads child nodes for a given parent node in the file system tree view. * @param {HTMLElement} parentNode - The parent node element in the tree view. * @param {string} path - The file system path to load children from. */ async function loadFileSystemTreeViewChildNodesAsync(parentNode, path) { // get the div that hosts child nodes const childNodes = parentNode.querySelector('.child-nodes'); if (!childNodes) return; // clear existing child nodes childNodes.innerHTML = ''; try { showBusyIndicator(); // fetch directory information and create nodes for each await fetchFileSystemDirectoriesAsync(path, (dir) => { dir.itemType = 'Directory'; const dirNode = createFileSystemTreeViewTreeNode(dir); childNodes.appendChild(dirNode); }); } catch (error) { console.error('Error:', error); notificationService.show(error, NotificationType.ERROR); } finally { hideBusyIndicator(); } } /** * Fetches directory information from the server for a given path. * @param {string} path - The path to get directories from. * @param {Function} callback - Function to call for each directory received. */ async function fetchFileSystemDirectoriesAsync(path, callback) { await fetchJsonDataAsync(`${clientBasePath}directories/api-get-directories?path=${encodeURIComponent(path)}&shouldIncludeHiddenElements=${showHiddenElements}`, callback, 'Directory'); } /** * Fetches file information from the server for a given path. * @param {string} path - The path to get files from. * @param {Function} callback - Function to call for each file received. */ async function fetchFileSystemFilesAsync(path, callback) { await fetchJsonDataAsync(`${clientBasePath}files/api-get-tree-files?path=${encodeURIComponent(path)}&shouldIncludeHiddenElements=${showHiddenElements}`, callback, 'File'); } /** * Fetches and processes JSON data from a given URL. * This function handles the data by processing all items in the response.data array * and passing them through the callback function. * * @param {string} url - The URL to fetch JSON data from. * @param {Function} callback - Function to call for each processed item. * @param {string} itemType - Type of item being processed ('File' or 'Directory'). */ async function fetchJsonDataAsync(url, callback, itemType) { const response = await callApiGetAsync(url); if (response !== undefined) { // process each item in the response.data array for (let item of response) { // manually specify the type to the parsed object item.itemType = itemType; // call the provided callback function with the processed item callback(item); } } } /** * Expands the tree view to a specific target path. * @param {HTMLElement} node - The starting node in the tree view. * @param {string} targetPath - The full path to expand to. */ async function expandInitialTreeViewPathAsync(node, targetPath) { const nodePath = node.getAttribute('data-path'); // exit if the target path doesn't start with the node path if (!targetPath.toLowerCase().startsWith(nodePath.toLowerCase())) return; // expand the current node if it's not already expanded const expandButton = node.querySelector('.expand-button'); if (expandButton && !expandButton.classList.contains('expanded')) await toggleFileSysemTreeViewNodeExpandAsync({ target: expandButton, stopPropagation: () => { } }); // split the remaining path into segments, using the separator of the file system the browser is showing const pathSegments = targetPath.slice(nodePath.length).split(pathSeparator).filter(Boolean); let currentNode = node; // iterate through each path segment for (let i = 0; i < pathSegments.length; i++) { const segment = pathSegments[i]; const childNodes = currentNode.querySelector('.child-nodes'); let found = false; // search for the child node matching the current path segment for (const childNode of childNodes.children) { if (childNode.querySelector('.node-name').textContent.toLowerCase() === segment.toLowerCase()) { currentNode = childNode; found = true; if (i === pathSegments.length - 1) { // if this is the last segment (target node) childNode.scrollIntoView(); // bring it into view, if its not already // mark it as selected childNode.querySelector('.node-content').classList.add('selected'); // toggle this last node, because all its children will be loaded const expandChildButton = childNode.querySelector('.expand-button'); if (expandChildButton) await toggleFileSysemTreeViewNodeExpandAsync({ target: expandChildButton, stopPropagation: () => { } }); // load the node's children await loadFileSystemTreeViewChildNodesAsync(childNode, childNode.getAttribute('data-path')); } else { // if this is an intermediate node, expand it if necessary const childExpandButton = childNode.querySelector('.expand-button'); if (childExpandButton && !childExpandButton.classList.contains('expanded')) await toggleFileSysemTreeViewNodeExpandAsync({ target: childExpandButton, stopPropagation: () => { } }); } break; } } // if the current segment wasn't found, stop expanding if (!found) break; } } //+======================================================================================+ //| File System | //+======================================================================================+ /** * Fetches and displays file system items for a given path. * @param {string} path - The path to fetch items from. */ async function getFileSystemItemsAsync(path) { // clear the current contents of the file system explorer fileSystemExplorer.innerHTML = ''; try { showBusyIndicator(); // fetch file system items and create explorer items for each await fetchFileSystemItemsAsync(path, (item) => { const newItem = createFileSystemExplorerItem(item); fileSystemExplorer.appendChild(newItem); }); } catch (error) { console.error('Error:', error); notificationService.show(error, NotificationType.ERROR); } finally { hideBusyIndicator(); } } /** * Fetches both directories and files from a specified path. * @param {string} path - The file system path to fetch items from. * @param {Function} callback - Function to call for each item (directory or file) received. */ async function fetchFileSystemItemsAsync(path, callback) { await fetchFileSystemDirectoriesAsync(path, callback); if (includeFiles) await fetchFileSystemFilesAsync(path, callback); } /** * Creates a DOM element representing a file system item (file or directory). * @param {Object} item - The file system item data. * @returns {HTMLElement} The created DOM element for the file system item. */ function createFileSystemExplorerItem(item) { const itemType = item.itemType; if (explorerItemTemplate !== null) { const html = renderThemeTemplate(explorerItemTemplate, { path: item.path, name: item.name, itemType: itemType, isDirectory: itemType === 'Directory', isFile: itemType === 'File', cssClass: viewMode, iconUrl: resolveIconUrl(item.path, itemType) }); const container = document.createElement('div'); container.innerHTML = html; const itemDiv = container.firstElementChild; // add event listeners for mouse interactions itemDiv.onmouseup = handleFileSystemEntityMouseUpAsync; itemDiv.onmousedown = handleFileSystemEntityMouseDown; return itemDiv; } const itemDiv = document.createElement('div'); // set up the main item div with appropriate classes and data attributes itemDiv.className = `e ${viewMode}`; itemDiv.setAttribute('data-path', item.path); itemDiv.setAttribute('data-type', itemType); // create and set up the icon div const iconDiv = document.createElement('div'); iconDiv.className = 'icon'; const iconUrl = resolveIconUrl(item.path, itemType); iconDiv.innerHTML = iconUrl !== '' ? `` : ''; // create and set up the text div const textDiv = document.createElement('div'); textDiv.className = 'text'; // use different class for directory ('d') and file ('f') textDiv.innerHTML = `${item.name}`; // assemble the item structure itemDiv.appendChild(iconDiv); itemDiv.appendChild(textDiv); // add event listeners for mouse interactions itemDiv.onmouseup = handleFileSystemEntityMouseUpAsync; itemDiv.onmousedown = handleFileSystemEntityMouseDown; return itemDiv; } /** * Handles mouse up events on file system entities in the explorer view. * Manages selection, double-click actions, and navigation. * @param {MouseEvent} e - The mouse event. */ async function handleFileSystemEntityMouseUpAsync(e) { e.preventDefault(); if (!isSelecting) e.stopPropagation(); const allEClassElements = Array.from(fileSystemExplorer.querySelectorAll('.e')); const currentIndex = allEClassElements.indexOf(e.currentTarget); // if a previous click timestamp exists, check the time difference const previousTime = parseInt(e.currentTarget.dataset.selectionStartTime || 0); const currentTime = new Date().getTime(); const timeDifference = currentTime - previousTime; // TODO: needs to account for when the element is already selected, and a double click occurs - on first click it would check the difference and go to rename - // maybe just wait half second before performing action // if time difference is less than 300ms, treat it as a double-click if (timeDifference < 300) { if (e.currentTarget.dataset.type === 'Directory') { // directories addressBarInput.value = e.currentTarget.dataset.path; await navigateToPathAsync(addressBarInput.value, false, false); // TODO: add to undo return; } else { // files } // reset the timestamp used to track double clicks e.currentTarget.dataset.selectionStartTime = 0; return; } else e.currentTarget.dataset.selectionStartTime = currentTime; // element was single clicked, update the selection start time to now if (e.ctrlKey || isSelectionMode) { // when CTRL key is pressed // toggle selection for the current clicked item e.currentTarget.classList.toggle('selected'); // set the selection start if it's not already set if (fileSystemExplorer.dataset.selectionStart === undefined) fileSystemExplorer.dataset.selectionStart = currentIndex; } else if ((e.shiftKey && fileSystemExplorer.dataset.selectionStart !== undefined) || isSelectionMode) { // when SHIFT key is pressed const start = parseInt(fileSystemExplorer.dataset.selectionStart); // remove all selections allEClassElements.forEach(elem => { elem.classList.remove('selected'); }); // define the range to select items from and to const [from, to] = start < currentIndex ? [start, currentIndex] : [currentIndex, start]; // add the 'selected' class to items in the range for (let i = from; i <= to; i++) allEClassElements[i].classList.add('selected'); } else { // when neither CTRL nor SHIFT keys are pressed if (!isSelectionMode) { // get all elements that are selected const selectedElements = allEClassElements.filter(element => element.classList.contains('selected')); // add 'selected' class to the current clicked item e.currentTarget.classList.add('selected'); // if there are no selected elements, or if there are more than one, deselect all of them, and select just the clicked one if ((selectedElements.length === 1 && e.currentTarget !== selectedElements[0]) || selectedElements.length !== 1) { // deselect all items selectedElements.forEach(elem => { if (elem !== e.currentTarget) { elem.classList.remove('selected'); // remove selectionStartTime from all items delete elem.dataset.selectionStartTime; } }); // set the current item as the selection start fileSystemExplorer.dataset.selectionStart = currentIndex; // store the current timestamp for future double-click checks e.currentTarget.dataset.selectionStartTime = currentTime; } } else // if its selection mode, just toggle selection of item, when clicked e.currentTarget.classList.toggle('selected'); } const selectedElements = document.querySelectorAll('.e.selected'); btnFileSystemBrowserConfirm.disabled = selectedElements.length === 0; // disable Confirm button if no item is selected if (selectedElements.length > 0) { var selectedPaths = '"'; selectedElements.forEach((element) => { selectedPaths += element.dataset.path + '","'; }); selectedFileSystemElementsInput.value = selectedPaths.substring(0, selectedPaths.length - 2); } else selectedFileSystemElementsInput.value = ''; } /** * Handler for the mouse down event of file system browser dialog files and directories elements. * @param {MouseEvent} e - The mouse event whose propagation is stopped. */ function handleFileSystemEntityMouseDown(e) { // stop the event from bubbling up to the explorer - we dont want to start selection rectangles when clicking files or directories e.stopPropagation(); } //+======================================================================================+ //| View Mode | //+======================================================================================+ /** * Switches to a different view mode. * @param {string} mode - The view mode to switch to ('list', 'details', or 'icons'). * @param {string|null} iconSize - The icon size for 'icons' mode ('small', 'medium', 'large', 'extra-large'). Use null for non-icon modes. * @param {boolean} isManualSet - Indicates whether the mode change is triggered manually (by clicking a mode change icon) or automatically. */ function switchViewMode(mode, iconSize, isManualSet) { hasScrolledAfterModeChange = false; // reset the flag indicating a mode change setViewMode(mode, iconSize); // check if content has been scrolled after the mode change; since scroll is the one triggering thumbnails retrieval, // and there is no initial scroll when mode is changed, we need to trigger the retrieval manually setTimeout(() => { if (!hasScrolledAfterModeChange && showThumbnails && isManualSet) getFileSystemVisibleItems(); }, 50); // wait 50ms to ensure all other events have processed } /** * Sets the explorer view mode. * @param {string} mode - View mode ('list', 'details', or 'icons'). * @param {string} [iconSize] - Icon size for 'icons' mode ('small', 'medium', 'large', 'extra-large'). */ function setViewMode(mode, iconSize) { if (!fileSystemExplorer) return; const isIconMode = mode === 'icons'; const viewClass = isIconMode ? 'details' : mode; const iconClass = isIconMode ? `${iconSize}-icons` : `${mode}-icons`; viewMode = iconClass; fileSystemExplorer.className = `${viewClass}`; fileSystemExplorer.style.flexDirection = mode === 'list' ? 'column' : isIconMode ? 'row' : 'column'; fileSystemExplorerContainer.classList.toggle('scroll-horizontal', mode === 'list'); fileSystemExplorerContainer.classList.toggle('scroll-vertical', mode !== 'list'); fileSystemDetailsHeader.style.height = mode === 'details' ? '20px' : '0px'; fileSystemDetailsHeader.style.position = mode === 'details' ? 'sticky' : 'static'; fileSystemDetailsHeader.style.top = mode === 'details' ? '-1px' : ''; setEnabledViewModeIcon(isIconMode ? `${iconSize}-icons` : mode); mode === 'list' ? addHorizontalScrolling(fileSystemExplorer) : removeHorizontalScrolling(fileSystemExplorer); fileSystemExplorer.querySelectorAll('.e').forEach(el => { el.className = `e ${iconClass}`; }); mode === 'details' ? showExtraDetails() : hideExtraDetails(); } /** * Hides additional item details such as date modified, type, and size. */ function hideExtraDetails() { fileSystemExplorer.querySelectorAll('.date-modified, .type, .size').forEach(el => { el.style.display = 'none'; }); } /** * Shows additional item details such as date modified, type, and size. */ function showExtraDetails() { fileSystemExplorer.querySelectorAll('.date-modified, .type, .size').forEach(el => { el.style.display = 'flex'; }); } /** * Sets the enabled view mode icon. * @param {string} viewType - The type of view to enable ('list', 'details', 'small-icons', 'medium-icons', 'large-icons', 'extra-large-icons'). */ function setEnabledViewModeIcon(viewType) { const allViews = [ svgListView, svgDetailsView, svgSmallIconsView, svgMediumIconsView, svgLargeIconsView, svgExtraLargeIconsView ]; allViews.forEach((view, index) => { view.forEach(element => { if (index === ['list', 'details', 'small-icons', 'medium-icons', 'large-icons', 'extra-large-icons'].indexOf(viewType)) element.classList.add('enabled'); else element.classList.remove('enabled'); }); }); } //+======================================================================================+ //| Selection | //+======================================================================================+ /** * Gets the height of the fixed top bar of the active theme, declared by the theme script, used to translate * page coordinates into the content coordinate space of the file system browser. */ function getThemeTopOffset() { return parseInt(window.getComputedStyle(document.documentElement).getPropertyValue('--theme-top-offset')) || 0; } /** * Handler for the file system browser dialog selection start. * @param {MouseEvent} e - The mousedown event object. */ function startSelection(e) { const explorerScrollbars = hasScrollbars(fileSystemExplorer); const containerExplorerScrollbars = hasScrollbars(fileSystemExplorerContainer); // check if clicked on the horizontal or vertical scrollbar if (explorerScrollbars.horizontal && e.clientY > fileSystemExplorer.getBoundingClientRect().top + fileSystemExplorer.offsetHeight - scrollbarHeight) return; if (containerExplorerScrollbars.vertical && e.clientX > fileSystemExplorerContainer.getBoundingClientRect().left + fileSystemExplorerContainer.offsetWidth - scrollbarWidth) return; // store information about selection start isSelecting = true; document.body.style.overflowY = 'hidden'; // needed in order to not put a vertical scrollbar on the document, while in vertical layout selection document.documentElement.style.overflowY = 'hidden'; selectionStartPosition.x = e.pageX + fileSystemExplorer.scrollLeft; selectionStartPosition.y = (e.pageY - getThemeTopOffset()) + fileSystemExplorerContainer.scrollTop; // not a mistake - in vertical mode, this is the overflow container! // place the selection rectangle at the clicked location (relative to the whole page, and taking into account scroll offset), with 0 size visibleSelectionRectangle.style.left = e.pageX + 'px'; visibleSelectionRectangle.style.top = (e.pageY - getThemeTopOffset()) + 'px'; visibleSelectionRectangle.style.width = '0px'; visibleSelectionRectangle.style.height = '0px'; visibleSelectionRectangle.style.visibility = 'visible'; selectionRectangle.style.left = e.pageX + 'px'; selectionRectangle.style.top = (e.pageY - getThemeTopOffset()) + 'px'; selectionRectangle.style.width = '0px'; selectionRectangle.style.height = '0px'; } /** * Handler for the file system browser dialog selection update. * @param {MouseEvent} e - The mousemove event object. */ function updateSelection(e) { if (!isSelecting) return; currentMousePosition.x = e.pageX; currentMousePosition.y = e.pageY - getThemeTopOffset(); handleSelection(); } /** * Handler for the file system browser dialog selection end. */ function endSelection() { // selection ended, hide the selection rectangle isSelecting = false; document.body.style.removeProperty('overflow-y'); document.documentElement.style.removeProperty('overflow-y'); visibleSelectionRectangle.style.visibility = 'hidden'; selectionRectangle.style.visibility = 'hidden'; selectionRectangle.style.left = '0px'; selectionRectangle.style.top = '0px'; selectionRectangle.style.width = '0px'; selectionRectangle.style.height = '0px'; visibleSelectionRectangle.style.left = '0px'; visibleSelectionRectangle.style.top = '0px'; visibleSelectionRectangle.style.width = '0px'; visibleSelectionRectangle.style.height = '0px'; // go through each .e item that was inside the selection rectangle, and mark it as selected fileSystemExplorer.querySelectorAll('.e.selection-hover').forEach(item => { item.classList.add('selected'); item.classList.remove('selection-hover'); }); const selectedElements = document.querySelectorAll('.e.selected'); btnFileSystemBrowserConfirm.disabled = selectedElements.length === 0; // disable Confirm button if no item is selected if (selectedElements.length > 0) { var selectedPaths = '"'; selectedElements.forEach((element) => { selectedPaths += element.dataset.path + '","'; }); selectedFileSystemElementsInput.value = selectedPaths.substring(0, selectedPaths.length - 2); } else selectedFileSystemElementsInput.value = ''; } /** * Function for handling the file system browser dialog selection. */ function handleSelection() { if (!isSelecting) // only relevant when we have a selection initialized return; // calculate dimensions of the selection rectangle const currentX = currentMousePosition.x; const currentY = currentMousePosition.y; const width = currentX - (selectionStartPosition.x - fileSystemExplorer.scrollLeft); const height = currentY - (selectionStartPosition.y - fileSystemExplorerContainer.scrollTop); // update selection rectangle dimensions visibleSelectionRectangle.style.width = Math.abs(width) + 'px'; visibleSelectionRectangle.style.height = Math.abs(height) + 'px'; selectionRectangle.style.width = Math.abs(width) + 'px'; selectionRectangle.style.height = Math.abs(height) + 'px'; // adjust position based on selection direction if (width < 0) { selectionRectangle.style.left = currentX + 'px'; visibleSelectionRectangle.style.left = currentX + 'px'; } else { selectionRectangle.style.left = (selectionStartPosition.x - fileSystemExplorer.scrollLeft) + 'px'; visibleSelectionRectangle.style.left = (selectionStartPosition.x - fileSystemExplorer.scrollLeft) + 'px'; } if (height < 0) { selectionRectangle.style.top = currentY + 'px'; visibleSelectionRectangle.style.top = currentY + 'px'; } else { selectionRectangle.style.top = (selectionStartPosition.y - fileSystemExplorerContainer.scrollTop) + 'px'; visibleSelectionRectangle.style.top = (selectionStartPosition.y - fileSystemExplorerContainer.scrollTop) + 'px'; } // do not let the selection rectangle go outside of explorer container, clip it if needed const containerRect = fileSystemExplorerContainer.getBoundingClientRect(); const left = parseFloat(visibleSelectionRectangle.style.left); const top = parseFloat(visibleSelectionRectangle.style.top) + 1; const right = left + parseFloat(visibleSelectionRectangle.style.width); const bottom = top + parseFloat(visibleSelectionRectangle.style.height); if (left < containerRect.left) { const difference = containerRect.left - left; visibleSelectionRectangle.style.width = (parseFloat(visibleSelectionRectangle.style.width) - difference) + 'px'; visibleSelectionRectangle.style.left = containerRect.left + 'px'; } if (right > containerRect.right) visibleSelectionRectangle.style.width = (containerRect.right - left) + 'px'; if (top < containerRect.top - getThemeTopOffset()) { const difference = containerRect.top - top - getThemeTopOffset(); visibleSelectionRectangle.style.height = (parseFloat(visibleSelectionRectangle.style.height) - difference) + 'px'; visibleSelectionRectangle.style.top = (containerRect.top - getThemeTopOffset()) + 'px'; } if (bottom > containerRect.bottom) visibleSelectionRectangle.style.height = (containerRect.bottom - top) + 'px'; // change color of items that fall within selection rectangle const selectionRect = selectionRectangle.getBoundingClientRect(); // go through each .e item which has not had its thumbnail retrieved yet fileSystemExplorer.querySelectorAll('.e').forEach(item => { const itemRect = item.getBoundingClientRect(); // check if the item is fully or partially visible horizontally and vertically const isFullyVisibleHorizontally = itemRect.left >= selectionRect.left && itemRect.right <= selectionRect.right; const isFullyVisibleVertically = itemRect.top >= selectionRect.top && itemRect.bottom <= selectionRect.bottom; const isPartiallyVisibleHorizontally = itemRect.left < selectionRect.right && itemRect.right > selectionRect.left; const isPartiallyVisibleVertically = itemRect.top < selectionRect.bottom && itemRect.bottom > selectionRect.top; // if the item is fully or partially visible in both directions, add it to our list if ((isFullyVisibleHorizontally || isPartiallyVisibleHorizontally) && (isFullyVisibleVertically || isPartiallyVisibleVertically)) item.classList.add('selection-hover'); else item.classList.remove('selection-hover'); }); } /** * Enables or disables the display of the file system tree view. * @param {MouseEvent} e - The mouseup event object. */ function toggleFileSystemSelectionMode(e) { isSelectionMode = !isSelectionMode; e.preventDefault(); e.stopPropagation(); svgSelectionMode.forEach(element => { isSelectionMode ? element.classList.add('enabled') : element.classList.remove('enabled'); }); } /** * Handler for the mouse up event of the file system browser dialog explorer * @param {MouseEvent} e - The mouseup event object. */ function handleFileSystemExplorerMouseUp(e) { const explorerScrollbars = hasScrollbars(fileSystemExplorer); // check if clicked on the horizontal scrollbar if (explorerScrollbars.horizontal && e.clientY > fileSystemExplorer.getBoundingClientRect().top + fileSystemExplorer.offsetHeight - scrollbarHeight) return; if (!e.ctrlKey && !e.shiftKey && !isSelectionMode) { // deselect all file system elements when their explorer is clicked fileSystemExplorer.querySelectorAll('.e.selected').forEach(item => { item.classList.remove('selected'); }); // do not store information about what item was clicked first, in context of Shift-selection fileSystemExplorer.removeAttribute('data-selection-start'); btnFileSystemBrowserConfirm.disabled = document.querySelectorAll('.e.selected').length === 0; // disable Confirm button if no item is selected } } /** * Handles the scroll event for the explorer or explorerContainer. * Clears any previous timeout and creates a new one to fetch visible items. */ function handleFileSystemBrowserScrollEvent() { // clear any previously set timeouts to debounce the getFileSystemVisibleItems call if (scrollTimeout) clearTimeout(scrollTimeout); // if there's an ongoing operation, abort it if (abortController) { abortController.abort(); abortController = null; } // set a timeout to call getFileSystemVisibleItems once the user stops scrolling scrollTimeout = setTimeout(() => { if (showThumbnails) getFileSystemVisibleItems(); }, scrollThumbnailRetrievalTimeout); hasScrolledAfterModeChange = true; } /** * Handles the window's resize event. * Clears any previous timeout and creates a new one to fetch file system browser visible items. */ function handleFileSystemBrowserResizeEvent() { // clear any previously set timeouts to debounce the getFileSystemVisibleItems call during resize if (resizeTimeout) clearTimeout(resizeTimeout); // if there's an ongoing operation, abort it if (abortController) { abortController.abort(); abortController = null; } // set a timeout to call getFileSystemVisibleItems once the user stops resizing resizeTimeout = setTimeout(() => { if (showThumbnails) getFileSystemVisibleItems(); }, scrollThumbnailRetrievalTimeout); } //+======================================================================================+ //| Thumbnails | //+======================================================================================+ /** * Identifies items within the explorerContainer that are either fully or partially visible. * Items with thumbnails already retrieved are excluded. * Populates a list of such visible items and passes them for further processing. */ function getFileSystemVisibleItems() { const containerRect = fileSystemExplorerContainer.getBoundingClientRect(); const visibleItems = []; // go through each .e item which has not had its thumbnail retrieved yet const fileElements = Array.from(fileSystemExplorer.querySelectorAll('.e:not([data-thumbnail-retrieved])[data-type="File"]')); // if the option for checking file header bytes is disabled, filter images by extension const itemsToProcess = inspectFileForThumbnails ? fileElements : fileElements.filter(item => isImageFile(item.getAttribute('data-path'))); itemsToProcess.forEach(item => { const itemPath = item.getAttribute('data-path'); const itemRect = item.getBoundingClientRect(); // check if the item is fully or partially visible horizontally and vertically const isFullyVisibleHorizontally = itemRect.left >= containerRect.left && itemRect.right <= containerRect.right; const isFullyVisibleVertically = itemRect.top >= containerRect.top && itemRect.bottom <= containerRect.bottom; const isPartiallyVisibleHorizontally = itemRect.left < containerRect.right && itemRect.right > containerRect.left; const isPartiallyVisibleVertically = itemRect.top < containerRect.bottom && itemRect.bottom > containerRect.top; // if the item is fully or partially visible in both directions, add it to our list if ((isFullyVisibleHorizontally || isPartiallyVisibleHorizontally) && (isFullyVisibleVertically || isPartiallyVisibleVertically)) { // skip entries without an image element, which happens when the active theme does not provide icons const imgElement = item.querySelector('img'); if (imgElement) { visibleItems.push({ path: itemPath, img: imgElement }); } } }); // process the list of visible items processVisibleItems(visibleItems); } /** * Processes a given item by retrieving its thumbnail from an API and updating the item's thumbnail image. * If there are more items in the visibleItems list, it continues to process the next item. * If an error occurs during the retrieval process (including an aborted fetch operation), it is handled gracefully. * @param {Object} item - The item containing its path and corresponding image element. * @param {Array} visibleItems - List of remaining visible items to process. * @throws {AbortError} - Indicates the fetch operation was aborted. * @throws {Error} - Catches and logs other errors that might occur during the thumbnail retrieval. */ async function processItem(item, visibleItems) { try { const result = await getThumbnailApiCall(item.path, imagePreviewsQuality); // update the item's thumbnail using the retrieved data if (typeof result.base64Data !== "undefined") item.img.src = `data:${result.mimeType};base64,${result.base64Data}`; item.img.closest('.e').setAttribute('data-thumbnail-retrieved', 'true'); // continue with next item if there are more in the queue if (visibleItems.length > 0) { const nextItem = visibleItems.shift(); processItem(nextItem, visibleItems); } } catch (error) { if (error.name === 'AbortError') { // console.log('Fetch operation was aborted.'); } else { // on error, revert the icons to the default ones of the active theme let itemType = item.img.closest('.e').getAttribute('data-type'); const iconUrl = resolveIconUrl('', itemType); if (iconUrl !== '') item.img.src = iconUrl; console.error('Error processing item:', error); } } } /** * Makes an API call to retrieve a thumbnail for the given item. * @param {string} path - The path of the item for which the thumbnail needs to be fetched. * @returns {Promise} - Returns a Promise resolving to an object containing base64Data and mimeType. * @throws {Error} - Throws an error if the API call fails. */ async function getThumbnailApiCall(path, quality) { // fetch the thumbnail for the given item from the server, through the Web application, which proxies the request with the authentication of the current user const response = await fetch(`${clientBasePath}thumbnails/api-get-thumbnail?path=` + encodeURIComponent(path) + '&quality=' + quality, { signal: abortController.signal // use the abort signal }); if (!response.ok) throw new Error('Failed to fetch the thumbnail'); // convert the response to a JSON object const blob = await response.blob(); const base64Data = await blobToBase64Async(blob); const mimeType = response.headers.get('Content-Type'); return { base64Data: base64Data, mimeType: mimeType }; } /** * Initiates the processing of visible items. Processing is batched based on thumbnailsRetrievalBatchSize. * * @param {Array} visibleItemsList - List of visible items to be processed. */ async function processVisibleItems(visibleItemsList) { const visibleItems = [...visibleItemsList]; abortController = new AbortController(); // create a new AbortController // process items in batches determined by thumbnailsRetrievalBatchSize for (let i = 0; i < Math.min(thumbnailsRetrievalBatchSize, visibleItems.length); i++) { const item = visibleItems.shift(); processItem(item, visibleItems); } } //+======================================================================================+ //| Miscellaneous | //+======================================================================================+ /** * Enables or disables the display of the file system tree view. * @param {boolean} isManualSet - Indicates whether the toggle is triggered manually (by clicking on the icon) or automatically. */ async function toggleFileSystemTreeViewAsync(isManualSet) { if (isManualSet) showFileSystemTreeView = !showFileSystemTreeView; showFileSystemTreeView ? fileSystemTreeViewContainer.classList.remove('collapsed') : fileSystemTreeViewContainer.classList.add('collapsed'); svgTreeView.forEach(element => { showFileSystemTreeView ? element.classList.add('enabled') : element.classList.remove('enabled'); }); showFileSystemTreeView ? fileSystemTreeViewContainer.classList.remove('collapsed') : fileSystemTreeViewContainer.classList.add('collapsed'); // if file system treeview needs to be displayed, get its file system tree structure from the API if (showFileSystemTreeView && isManualSet && addressBarInput.value !== null) { if (!addressBarInput.value.endsWith(pathSeparator)) addressBarInput.value += pathSeparator; await navigateToPathAsync(addressBarInput.value, false, false); } } /** * Enables or disables the display of hidden file system elements. * @param {boolean} isManualSet - Indicates whether the toggle is triggered manually (by clicking on the icon) or automatically. */ async function toggleShowFileSystemHiddenElementsAsync(isManualSet) { if (isManualSet) showHiddenElements = !showHiddenElements; svgHiddenElements.forEach(element => { showHiddenElements ? element.classList.add('enabled') : element.classList.remove('enabled'); }); if (isManualSet && addressBarInput.value !== null) { if (!addressBarInput.value.endsWith(pathSeparator)) addressBarInput.value += pathSeparator; await navigateToPathAsync(addressBarInput.value, false, false); } } /** * Enables or disables the display of file system elements thumbnails. * @param {boolean} isManualSet - Indicates whether the toggle is triggered manually (by clicking on the icon) or automatically. */ async function toggleShowFileSystemElementsThumbnailsAsync(isManualSet) { if (isManualSet) showThumbnails = !showThumbnails; svgThumbnails.forEach(element => { showThumbnails ? element.classList.add('enabled') : element.classList.remove('enabled'); }); if (isManualSet && addressBarInput.value !== null) { if (!addressBarInput.value.endsWith(pathSeparator)) addressBarInput.value += pathSeparator; await navigateToPathAsync(addressBarInput.value, false, false); } } /** * Gets the icon of the node, based on it type. * @param {any} node The node for which to get the icon. * @returns The icon on the node. */ function getNodeIcon(node) { switch (node.itemType) { case "Root": return "💿"; case "Directory": return node.isExpanded ? "📂" : "📁"; case "File": return "📄"; default: return "❓"; } } //+======================================================================================+ //| Event Handlers | //+======================================================================================+ /** * Registers an event handler only when the element exists, so that a theme template may omit optional elements without breaking the script. * @param {HTMLElement|null} element - The element to register the handler on, or null when the element is absent. * @param {string} eventName - The name of the event. * @param {Function} handler - The event handler. * @param {object} [options] - Optional options passed to the event listener registration. */ function addEventListenerIfPresent(element, eventName, handler, options) { if (element) element.addEventListener(eventName, handler, options); } // event handlers for changing file system view modes addEventListenerIfPresent(btnListView, 'click', () => switchViewMode('list', null, true)); addEventListenerIfPresent(btnDetailsView, 'click', () => switchViewMode('details', null, true)); addEventListenerIfPresent(btnSmallIconsView, 'click', () => switchViewMode('icons', 'small', true)); addEventListenerIfPresent(btnMediumIconsView, 'click', () => switchViewMode('icons', 'medium', true)); addEventListenerIfPresent(btnLargeIconsView, 'click', () => switchViewMode('icons', 'large', true)); addEventListenerIfPresent(btnExtraLargeIconsView, 'click', () => switchViewMode('icons', 'extra-large', true)); addEventListenerIfPresent(btnNavigatorToggleTreeView, 'click', async () => await toggleFileSystemTreeViewAsync(true)); addEventListenerIfPresent(btnNavigatorToggleThumbnails, 'click', async () => await toggleShowFileSystemElementsThumbnailsAsync(true)); addEventListenerIfPresent(btnNavigatorToggleSelectionMode, 'click', toggleFileSystemSelectionMode); addEventListenerIfPresent(btnNavigatorHiddenElements, 'click', async () => await toggleShowFileSystemHiddenElementsAsync(true)); addEventListenerIfPresent(btnNavigatorNavigateUp, 'click', navigateUpAsync); addEventListenerIfPresent(fileSystemExplorer, 'mouseup', handleFileSystemExplorerMouseUp); addEventListenerIfPresent(fileSystemExplorer, 'scroll', handleFileSystemBrowserScrollEvent); addEventListenerIfPresent(fileSystemExplorerContainer, 'scroll', handleFileSystemBrowserScrollEvent); addEventListenerIfPresent(fileSystemExplorerContainer, 'mousedown', startSelection); addEventListenerIfPresent(fileSystemExplorerContainer, 'mousemove', updateSelection); addEventListenerIfPresent(fileSystemExplorerContainer, 'mouseup', endSelection); addEventListenerIfPresent(fileSystemExplorerContainer, 'wheel', handleSelection, { passive: true }); addEventListenerIfPresent(fileSystemExplorerContainer, 'scroll', handleSelection); /** * Event handler for the address bar input navigate button Click event. */ addEventListenerIfPresent(btnFileSystemBrowserSubmitPath, 'click', async function () { await navigateToPathAsync(addressBarInput.value, false, false); addressBarInput.style.display = 'none'; addressBar.style.display = 'block'; }); /** * Event handler for the address bar input edit button Click event. */ addEventListenerIfPresent(btnFileSystemBrowserEditPath, 'click', function () { addressBarInput.style.display = 'block'; addressBarInput.focus(); addressBar.style.display = 'none'; }); /** * Event handler for the navigator back button Click event. */ addEventListenerIfPresent(btnNavigatorNavigateBack, 'click', async function () { if (addressBarInput.value !== null) await navigateToPathAsync(addressBarInput.value, false, true); }); /** * Event handler for the navigator address bar input KeyDown event. */ addEventListenerIfPresent(addressBarInput, 'keydown', async function (e) { if (e.key === 'Escape') { e.preventDefault(); addressBarInput.style.display = 'none'; addressBar.style.display = 'block'; } else if (e.key === 'Enter') await navigateToPathAsync(addressBarInput.value, false, false); }); /** * Event handler for the addressbar Click event. */ addEventListenerIfPresent(addressBar, 'click', function (e) { // if the clicked element is the ul itself or one of its direct children (but not deeper nested children) if (e.target === e.currentTarget || e.target.parentElement === e.currentTarget) { this.style.display = 'none'; // hide #addressBar addressBarInput.style.display = 'block'; // show #addressBarInput addressBarInput.focus(); // focus on the input } }); /** * Handles the window resize event. */ window.addEventListener('resize', function () { handleFileSystemBrowserResizeEvent(); // update all dropdowns' positions on resize const dropdown = document.querySelector('.navigator-dropdown[data-detached="true"]'); if (dropdown) { const pathSegmentId = dropdown.getAttribute('data-path-segment-id'); if (pathSegmentId) updateDropdownPosition(pathSegmentId, dropdown); } }); // expose the functions and DOM elements that need to be accessed globally window.initFileSystemBrowser = initFileSystemBrowser; window.showFileSystemBrowserDialogAsync = showFileSystemBrowserDialogAsync; window.addressBar = addressBar; window.reattachDropdown = reattachDropdown; window.adjustDropdownPosition = adjustDropdownPosition; })();