-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfile-tree.js
More file actions
335 lines (302 loc) · 12.3 KB
/
file-tree.js
File metadata and controls
335 lines (302 loc) · 12.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
// File Tree - Sidebar file browser for Marketbuffer
// This module handles the file tree data structure and rendering
const FileTree = {
// File tree data structure
data: {
name: 'marketbuffer',
type: 'folder',
expanded: true,
children: [
{
name: 'src',
type: 'folder',
expanded: true,
children: [
{
name: 'components',
type: 'folder',
expanded: false,
children: [
{ name: 'Button.js', type: 'file', path: 'demo/src/components/Button.js' },
{ name: 'Modal.js', type: 'file', path: 'demo/src/components/Modal.js' },
{ name: 'Sidebar.js', type: 'file', path: 'demo/src/components/Sidebar.js' },
],
},
{
name: 'utils',
type: 'folder',
expanded: false,
children: [
{ name: 'helpers.js', type: 'file', path: 'demo/src/utils/helpers.js' },
{ name: 'constants.js', type: 'file', path: 'demo/src/utils/constants.js' },
],
},
{ name: 'index.js', type: 'file', path: 'demo/src/index.js' },
{ name: 'App.js', type: 'file', path: 'demo/src/App.js' },
],
},
{
name: 'public',
type: 'folder',
expanded: false,
children: [
{ name: 'index.html', type: 'file', path: 'demo/public/index.html' },
{ name: 'favicon.ico', type: 'file', path: 'demo/public/favicon.ico' },
],
},
{
name: 'styles',
type: 'folder',
expanded: false,
children: [
{ name: 'main.css', type: 'file', path: 'demo/styles/main.css' },
{ name: 'variables.css', type: 'file', path: 'demo/styles/variables.css' },
],
},
{
name: 'algos',
type: 'folder',
expanded: true,
children: [
{ name: 'random-walk.algo', type: 'file', path: 'demo/algos/random-walk.algo' },
{ name: 'last-close.algo', type: 'file', path: 'demo/algos/last-close.algo' },
{ name: 'momentum.algo', type: 'file', path: 'demo/algos/momentum.algo' },
{ name: 'momentum-10d.algo', type: 'file', path: 'demo/algos/momentum-10d.algo' },
{ name: 'momentum-10d-no-tsla.algo', type: 'file', path: 'demo/algos/momentum-10d-no-tsla.algo' },
],
},
{ name: 'package.json', type: 'file', path: 'demo/package.json' },
{ name: 'README.md', type: 'file', path: 'demo/README.md' },
{ name: '.gitignore', type: 'file', path: 'demo/.gitignore' },
],
},
// Cache for loaded file contents (session memory)
fileContentsCache: {},
// Callbacks registered by index.html
_callbacks: {
getSystemState: null,
saveSystemState: null,
},
// Register callbacks from index.html
registerCallbacks(callbacks) {
Object.assign(this._callbacks, callbacks);
},
// ============================================
// Rendering
// ============================================
render() {
// Load saved expansion state before rendering
this.loadState();
const container = document.getElementById('file-tree');
// Render children of root folder directly (don't show root)
let html = '';
if (this.data.children) {
this.data.children.forEach(child => {
html += this.renderNode(child, 0);
});
}
container.innerHTML = html;
this.attachListeners();
},
renderNode(node, depth) {
const isFolder = node.type === 'folder';
const hasChildren = isFolder && node.children && node.children.length > 0;
const toggleIcon = hasChildren ? (node.expanded ? '▼' : '▶') : '';
const icon = isFolder ? '📁' : '📄';
const expandedClass = node.expanded ? 'expanded' : '';
const pathAttr = node.path ? `data-path="${node.path}"` : '';
const isPinned = !isFolder && node.path && OS.isPinned(node.path);
const pinClass = isPinned ? 'pinned' : '';
const pinIcon = isPinned ? '📌' : '📌'; // pushpin icon
const menuHtml = !isFolder && node.path ? `
<button class="tree-file-menu-btn" data-path="${node.path}" type="button">
<svg viewBox="0 0 24 24" fill="currentColor"><circle cx="5" cy="12" r="2.5"/><circle cx="12" cy="12" r="2.5"/><circle cx="19" cy="12" r="2.5"/></svg>
</button>
<div class="tree-item-menu" data-path="${node.path}">
<div class="tree-item-menu-option" data-action="rename"><span class="menu-icon"><svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M21.174 6.812a1 1 0 0 0-3.986-3.987L3.842 16.174a2 2 0 0 0-.5.83l-1.321 4.352a.5.5 0 0 0 .623.622l4.353-1.32a2 2 0 0 0 .83-.497z"/><path d="m15 5 4 4"/></svg></span>Rename</div>
<div class="tree-item-menu-option" data-action="star"><span class="menu-icon"><svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M11.525 2.295a.53.53 0 0 1 .95 0l2.31 4.679a2.123 2.123 0 0 0 1.595 1.16l5.166.756a.53.53 0 0 1 .294.904l-3.736 3.638a2.123 2.123 0 0 0-.611 1.878l.882 5.14a.53.53 0 0 1-.771.56l-4.618-2.428a2.122 2.122 0 0 0-1.973 0L6.396 21.01a.53.53 0 0 1-.77-.56l.881-5.139a2.122 2.122 0 0 0-.611-1.879L2.16 9.795a.53.53 0 0 1 .294-.906l5.165-.755a2.122 2.122 0 0 0 1.597-1.16z"/></svg></span>Star</div>
<div class="tree-item-menu-separator"></div>
<div class="tree-item-menu-option delete" data-action="delete"><span class="menu-icon"><svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M3 6h18"/><path d="M19 6v14a2 2 0 0 1-2 2H7a2 2 0 0 1-2-2V6"/><path d="M8 6V4a2 2 0 0 1 2-2h4a2 2 0 0 1 2 2v2"/><path d="M10 11v6"/><path d="M14 11v6"/></svg></span>Delete</div>
</div>
` : '';
let html = `
<div class="tree-item" data-depth="${depth}" data-type="${node.type}" data-name="${node.name}" ${pathAttr}>
<div class="tree-item-row">
<span class="tree-toggle">${toggleIcon}</span>
<span class="tree-icon">${icon}</span>
<span class="tree-label">${node.name}</span>
${menuHtml}
</div>
`;
if (hasChildren) {
html += `<div class="tree-children ${expandedClass}">`;
node.children.forEach(child => {
html += this.renderNode(child, depth + 1);
});
html += '</div>';
}
html += '</div>';
return html;
},
attachListeners() {
document.querySelectorAll('.tree-item-row').forEach(row => {
row.addEventListener('click', (e) => {
// Handle pin button click
if (e.target.classList.contains('tree-pin')) {
e.stopPropagation();
const path = e.target.dataset.path;
if (path) {
OS.togglePinFile(path);
}
return;
}
// Handle menu button click
if (e.target.closest('.tree-file-menu-btn')) {
e.stopPropagation();
// Close any other open menus
document.querySelectorAll('.tree-item-menu.open').forEach(m => m.classList.remove('open'));
const menu = row.querySelector('.tree-item-menu');
const btn = e.target.closest('.tree-file-menu-btn');
if (menu && btn) {
const rect = btn.getBoundingClientRect();
menu.style.left = (rect.right + 4) + 'px';
menu.style.top = rect.top + 'px';
menu.classList.toggle('open');
}
return;
}
// Handle menu option click
if (e.target.closest('.tree-item-menu-option')) {
e.stopPropagation();
const action = e.target.dataset.action;
const path = row.closest('.tree-item').dataset.path;
// Handle actions here (rename, star, delete)
if (action === 'delete') {
// Could implement file deletion
}
// Close menu
const menu = row.querySelector('.tree-item-menu');
if (menu) menu.classList.remove('open');
return;
}
// Skip if clicking inside the menu
if (e.target.closest('.tree-item-menu')) {
e.stopPropagation();
return;
}
e.stopPropagation();
const item = row.closest('.tree-item');
const type = item.dataset.type;
const name = item.dataset.name;
const path = item.dataset.path;
// Handle selection
document.querySelectorAll('.tree-item-row.selected').forEach(el => {
el.classList.remove('selected');
});
row.classList.add('selected');
// Toggle folder expansion
if (type === 'folder') {
const children = item.querySelector('.tree-children');
const toggle = row.querySelector('.tree-toggle');
if (children) {
const isExpanded = children.classList.toggle('expanded');
toggle.innerHTML = isExpanded ? '▼' : '▶';
// Update data model and save to localStorage
this.updateNodeExpanded(this.data, name, isExpanded);
this.saveState();
}
} else if (type === 'file' && path) {
// Open file with appropriate app
OS.openFile(path);
}
});
});
// Close tree item menus when clicking outside
document.addEventListener('click', (e) => {
if (!e.target.closest('.tree-item-menu-btn') && !e.target.closest('.tree-item-menu')) {
document.querySelectorAll('.tree-item-menu.open').forEach(m => m.classList.remove('open'));
}
});
},
// ============================================
// Node operations
// ============================================
updateNodeExpanded(node, name, expanded) {
if (node.name === name) {
node.expanded = expanded;
return true;
}
if (node.children) {
for (const child of node.children) {
if (this.updateNodeExpanded(child, name, expanded)) return true;
}
}
return false;
},
// ============================================
// State persistence
// ============================================
saveState() {
const state = {};
this.collectExpansionState(this.data, state);
const systemState = this._callbacks.getSystemState ? this._callbacks.getSystemState() : null;
if (systemState) {
systemState.fileTree = state;
if (this._callbacks.saveSystemState) {
this._callbacks.saveSystemState();
}
}
},
collectExpansionState(node, state) {
if (node.type === 'folder') {
state[node.name] = node.expanded || false;
}
if (node.children) {
node.children.forEach(child => this.collectExpansionState(child, state));
}
},
loadState() {
const systemState = this._callbacks.getSystemState ? this._callbacks.getSystemState() : null;
if (systemState && systemState.fileTree && Object.keys(systemState.fileTree).length > 0) {
this.applyExpansionState(this.data, systemState.fileTree);
}
},
applyExpansionState(node, state) {
if (node.type === 'folder' && state.hasOwnProperty(node.name)) {
node.expanded = state[node.name];
}
if (node.children) {
node.children.forEach(child => this.applyExpansionState(child, state));
}
},
// ============================================
// Utilities
// ============================================
highlightFile(filePath) {
// Remove existing selection
document.querySelectorAll('.tree-item-row.selected').forEach(el => {
el.classList.remove('selected');
});
// Find and select the matching file
const fileItem = document.querySelector(`.tree-item[data-path="${filePath}"]`);
if (fileItem) {
const row = fileItem.querySelector('.tree-item-row');
if (row) {
row.classList.add('selected');
}
}
},
// Get cached file contents
getFileContents(path) {
return this.fileContentsCache[path];
},
// Set cached file contents
setFileContents(path, contents) {
this.fileContentsCache[path] = contents;
},
};
// Backward compatibility aliases
const fileTreeData = FileTree.data;
const fileContentsCache = FileTree.fileContentsCache;
function renderFileTree() { FileTree.render(); }
function highlightFileInTree(filePath) { FileTree.highlightFile(filePath); }