|
| 1 | +define(function (require, exports, module) { |
| 2 | + const AppInit = require("utils/AppInit"); |
| 3 | + const ProjectManager = require("project/ProjectManager"); |
| 4 | + |
| 5 | + /** |
| 6 | + * This is the main function that handles the closing of all the directories |
| 7 | + */ |
| 8 | + function handleCollapseBtnClick() { |
| 9 | + // this will give us an array of array's |
| 10 | + // the root level directories will be at index 0, its next level will be at index 1 and so on |
| 11 | + const openNodes = ProjectManager._actionCreator.model.getOpenNodes(); |
| 12 | + if (!openNodes || openNodes.length === 0) { |
| 13 | + return; |
| 14 | + } |
| 15 | + |
| 16 | + // traversing from the back because the deepest nested directories should be closed first |
| 17 | + // Note: this is an array of all the directories at the deepest level |
| 18 | + for (let i = openNodes.length - 1; i >= 0; i--) { |
| 19 | + // close all the directories |
| 20 | + openNodes[i].forEach(function (folderPath) { |
| 21 | + try { |
| 22 | + // to close each dir |
| 23 | + ProjectManager._actionCreator.setDirectoryOpen(folderPath, false); |
| 24 | + } catch (error) { |
| 25 | + console.error("Failed to close folder:", folderPath, error); |
| 26 | + } |
| 27 | + }); |
| 28 | + } |
| 29 | + } |
| 30 | + |
| 31 | + /** |
| 32 | + * This function is responsible to create the 'Collapse All' button |
| 33 | + * and append it to the sidebar area on the project-files-header |
| 34 | + */ |
| 35 | + function createCollapseButton() { |
| 36 | + const $projectFilesHeader = $("#project-files-header"); |
| 37 | + // make sure that we were able to get the project-files-header DOM element |
| 38 | + if ($projectFilesHeader.length === 0) { |
| 39 | + return; |
| 40 | + } |
| 41 | + |
| 42 | + // create the collapse btn |
| 43 | + const $collapseBtn = $(` |
| 44 | + <div id="collapse-folders" class="btn-alt-quiet" title="Collapse All"> |
| 45 | + <i class="fa-solid fa-chevron-down collapse-icon" aria-hidden="true"></i> |
| 46 | + <i class="fa-solid fa-chevron-up collapse-icon" aria-hidden="true"></i> |
| 47 | + </div> |
| 48 | + `); |
| 49 | + |
| 50 | + $collapseBtn.on("click", handleCollapseBtnClick); |
| 51 | + |
| 52 | + $projectFilesHeader.append($collapseBtn); // append the btn to the project-files-header |
| 53 | + |
| 54 | + _setupHoverBehavior($collapseBtn); // hover functionality to show/hide the button |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * This function is responsible for the hover behavior to show/hide the collapse button |
| 59 | + * we only show the button when the cursor is over the sidebar area |
| 60 | + */ |
| 61 | + function _setupHoverBehavior($collapseBtn) { |
| 62 | + const $sidebar = $("#sidebar"); |
| 63 | + if ($sidebar.length === 0) { |
| 64 | + return; |
| 65 | + } |
| 66 | + |
| 67 | + $sidebar.on("mouseenter", function () { |
| 68 | + $collapseBtn.addClass("show"); |
| 69 | + }); |
| 70 | + |
| 71 | + $sidebar.on("mouseleave", function () { |
| 72 | + $collapseBtn.removeClass("show"); |
| 73 | + }); |
| 74 | + } |
| 75 | + |
| 76 | + AppInit.appReady(function () { |
| 77 | + createCollapseButton(); |
| 78 | + }); |
| 79 | +}); |
0 commit comments