|
| 1 | +import React, { useState } from 'react'; |
| 2 | +import PropTypes from 'prop-types'; |
| 3 | +import { UncontrolledTreeEnvironment, Tree } from 'react-complex-tree'; |
| 4 | +import useComplexTreeViewApi from './useComplexTreeViewApi'; |
| 5 | + |
| 6 | +const CSS_CLASS = 'complex-tree-view'; |
| 7 | +const TREE_ID = 'tree-1'; |
| 8 | + |
| 9 | +const ComplexTreeView = ({ |
| 10 | + currentRecordID = 0, |
| 11 | + apiEndpoint = '/admin/pages/tree/jsonview', |
| 12 | +}) => { |
| 13 | + const [loadingNodeId, setLoadingNodeId] = useState(null); |
| 14 | + const { |
| 15 | + data, |
| 16 | + dataProvider, |
| 17 | + error, |
| 18 | + initialViewState, |
| 19 | + loading, |
| 20 | + rootId, |
| 21 | + loadSubtree, |
| 22 | + mergeSubtree, |
| 23 | + } = useComplexTreeViewApi(TREE_ID, currentRecordID, apiEndpoint); |
| 24 | + |
| 25 | + /** |
| 26 | + * Renders the DOM for a tree item label. |
| 27 | + * For synthetic limited indicator nodes, renders as a clickable link. |
| 28 | + * Otherwise renders the regular title. |
| 29 | + */ |
| 30 | + const renderTreeItemTitle = ({ item }) => { |
| 31 | + const isLimitedIndicator = item.data.isLimitedIndicator === true; |
| 32 | + const isLoading = loadingNodeId === item.index; |
| 33 | + |
| 34 | + if (isLimitedIndicator) { |
| 35 | + return ( |
| 36 | + <span className={`${CSS_CLASS}__item-link ${CSS_CLASS}__item-link--limited`}> |
| 37 | + Too many records ( |
| 38 | + <a href={item.data.linkUrl} className={`${CSS_CLASS}__limited-link`}>show as list</a> |
| 39 | + ) |
| 40 | + </span> |
| 41 | + ); |
| 42 | + } |
| 43 | + |
| 44 | + if (isLoading) { |
| 45 | + return ( |
| 46 | + <span className={`${CSS_CLASS}__item-link`} data-item-id={item.index}> |
| 47 | + <span className={`${CSS_CLASS}__loading`}>Loading...</span> |
| 48 | + {item.data.title} |
| 49 | + </span> |
| 50 | + ); |
| 51 | + } |
| 52 | + |
| 53 | + return ( |
| 54 | + <span className={`${CSS_CLASS}__item-link`} data-item-id={item.index}> |
| 55 | + {item.data.title} |
| 56 | + </span> |
| 57 | + ); |
| 58 | + }; |
| 59 | + |
| 60 | + /** |
| 61 | + * This is triggered by Double Click (mouse) or Enter (keyboard) on a focused item. |
| 62 | + * For branch nodes, this should only navigate if the item is a leaf or if the title was clicked. |
| 63 | + */ |
| 64 | + const handlePrimaryAction = (items) => { |
| 65 | + window.location.href = `/admin/pages/edit/show/${items.data.id}`; |
| 66 | + }; |
| 67 | + |
| 68 | + /** |
| 69 | + * Handle mousedown events on tree items (capture phase). |
| 70 | + * For branch nodes, only allow expansion if clicking on the icon (not the title). |
| 71 | + * For title clicks, we'll handle navigation separately. |
| 72 | + */ |
| 73 | + const handleRootMouseDown = (e) => { |
| 74 | + const button = e.target.closest('[data-rct-item-interactive="true"]'); |
| 75 | + if (!button) { |
| 76 | + return; |
| 77 | + } |
| 78 | + const titleSpan = button.querySelector('.complex-tree-view__item-link'); |
| 79 | + if (!titleSpan || !titleSpan.contains(e.target)) { |
| 80 | + return; |
| 81 | + } |
| 82 | + const treeItem = button.closest('[role="treeitem"]'); |
| 83 | + if (!treeItem || !treeItem.hasAttribute('aria-expanded')) { |
| 84 | + return; |
| 85 | + } |
| 86 | + const itemId = titleSpan.getAttribute('data-item-id'); |
| 87 | + if (itemId && data && data.items && data.items[itemId]) { |
| 88 | + const item = data.items[itemId]; |
| 89 | + if (item && item.data && item.data.id) { |
| 90 | + e.preventDefault(); |
| 91 | + e.stopPropagation(); |
| 92 | + window.location.href = `/admin/pages/edit/show/${item.data.id}`; |
| 93 | + } |
| 94 | + } |
| 95 | + }; |
| 96 | + |
| 97 | + /** |
| 98 | + * Handle click events on tree items (capture phase). |
| 99 | + * Prevents react-complex-tree from processing clicks on branch node titles. |
| 100 | + */ |
| 101 | + const handleRootClick = (e) => { |
| 102 | + const button = e.target.closest('[data-rct-item-interactive="true"]'); |
| 103 | + if (!button) { |
| 104 | + return; |
| 105 | + } |
| 106 | + const titleSpan = button.querySelector('.complex-tree-view__item-link'); |
| 107 | + if (!titleSpan || !titleSpan.contains(e.target)) { |
| 108 | + return; |
| 109 | + } |
| 110 | + const treeItem = button.closest('[role="treeitem"]'); |
| 111 | + if (!treeItem || !treeItem.hasAttribute('aria-expanded')) { |
| 112 | + return; |
| 113 | + } |
| 114 | + e.preventDefault(); |
| 115 | + e.stopPropagation(); |
| 116 | + }; |
| 117 | + |
| 118 | + /** |
| 119 | + * Handler for item selection - no-op for now |
| 120 | + */ |
| 121 | + const handleSelectItems = () => {}; |
| 122 | + |
| 123 | + /** |
| 124 | + * Handles expanding a tree item. If the item has unloaded children, |
| 125 | + * loads the subtree and merges it into the tree. |
| 126 | + */ |
| 127 | + // eslint-disable-next-line no-unused-vars |
| 128 | + const handleExpandItem = async (item, treeId) => { |
| 129 | + const hasUnloadedChildren = item.data.expanded === false && item.data.count > 0 && item.children.length === 0; |
| 130 | + if (hasUnloadedChildren) { |
| 131 | + try { |
| 132 | + setLoadingNodeId(item.index); |
| 133 | + const subtree = await loadSubtree(item.data.id); |
| 134 | + await mergeSubtree(item.data.id, subtree.items); |
| 135 | + } finally { |
| 136 | + setLoadingNodeId(null); |
| 137 | + } |
| 138 | + } |
| 139 | + }; |
| 140 | + |
| 141 | + /** |
| 142 | + * INTERCEPT KEYDOWN |
| 143 | + * This is the key fix. We intercept the event at the container level. |
| 144 | + */ |
| 145 | + const handleRootKeyDown = (e) => { |
| 146 | + // Check if the key pressed is Space |
| 147 | + if (e.key !== ' ') { |
| 148 | + return; |
| 149 | + } |
| 150 | + // Find the closest tree item container |
| 151 | + const treeItem = e.target.closest('[role="treeitem"]'); |
| 152 | + |
| 153 | + // If we are on a tree item... |
| 154 | + if (treeItem) { |
| 155 | + // Check if it is a Folder. Folders have 'aria-expanded' (true or false). |
| 156 | + // Leaves do NOT have this attribute. |
| 157 | + const isFolder = treeItem.hasAttribute('aria-expanded'); |
| 158 | + |
| 159 | + // If it's NOT a folder (it's a leaf), prevent the default browser behavior. |
| 160 | + // This stops the browser from turning "Space" into a "Click" event. |
| 161 | + if (!isFolder) { |
| 162 | + e.preventDefault(); |
| 163 | + } |
| 164 | + } |
| 165 | + }; |
| 166 | + |
| 167 | + if (loading) { |
| 168 | + return <div>Loading page tree...</div>; |
| 169 | + } |
| 170 | + if (error) { |
| 171 | + return <div style={{ color: 'red' }}>Error loading data: {error}</div>; |
| 172 | + } |
| 173 | + if (!dataProvider || !initialViewState) { |
| 174 | + return <div>No data available.</div>; |
| 175 | + } |
| 176 | + |
| 177 | + return ( |
| 178 | + <div |
| 179 | + className={CSS_CLASS} |
| 180 | + onKeyDown={handleRootKeyDown} |
| 181 | + onMouseDownCapture={handleRootMouseDown} |
| 182 | + onClickCapture={handleRootClick} |
| 183 | + > |
| 184 | + <UncontrolledTreeEnvironment |
| 185 | + dataProvider={dataProvider} |
| 186 | + getItemTitle={item => item.data.title} |
| 187 | + viewState={initialViewState} |
| 188 | + // canDragAndDrop={false} |
| 189 | + // canDropOnFolder={false} |
| 190 | + // canReorderItems={false} |
| 191 | + canRenameItems={false} |
| 192 | + canSearchItems={false} |
| 193 | + canSearchByStartingTyping={false} |
| 194 | + renderItemTitle={renderTreeItemTitle} |
| 195 | + onPrimaryAction={handlePrimaryAction} |
| 196 | + onSelectItems={handleSelectItems} |
| 197 | + onExpandItem={handleExpandItem} |
| 198 | + > |
| 199 | + <Tree treeId={TREE_ID} rootItem={rootId} treeLabel="Page Tree" /> |
| 200 | + </UncontrolledTreeEnvironment> |
| 201 | + </div> |
| 202 | + ); |
| 203 | +}; |
| 204 | + |
| 205 | +ComplexTreeView.propTypes = { |
| 206 | + currentRecordID: PropTypes.number, |
| 207 | + apiEndpoint: PropTypes.string, |
| 208 | +}; |
| 209 | + |
| 210 | +export { ComplexTreeView as Component }; |
| 211 | +export default ComplexTreeView; |
0 commit comments