File size: 18,638 Bytes
875e204
 
 
 
 
5d74c00
875e204
5d74c00
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
875e204
5d74c00
 
 
 
 
875e204
 
 
 
5d74c00
 
875e204
 
5d74c00
 
 
 
 
 
 
 
875e204
 
5d74c00
 
 
 
 
875e204
5d74c00
875e204
5d74c00
 
875e204
 
5d74c00
 
875e204
 
 
5d74c00
 
875e204
 
5d74c00
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
875e204
 
5d74c00
 
 
875e204
 
5d74c00
 
 
 
 
 
 
 
875e204
 
5d74c00
 
 
875e204
 
5d74c00
 
 
 
 
 
 
 
 
875e204
5d74c00
 
 
875e204
5d74c00
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
875e204
5d74c00
 
 
 
 
 
 
 
875e204
 
5d74c00
 
 
 
 
 
 
 
 
 
875e204
 
5d74c00
 
 
 
 
 
 
875e204
5d74c00
875e204
 
5d74c00
 
875e204
 
 
5d74c00
 
 
 
 
 
 
 
 
 
875e204
5d74c00
875e204
5d74c00
 
 
 
 
 
875e204
5d74c00
 
875e204
5d74c00
 
 
875e204
5d74c00
 
875e204
5d74c00
 
 
875e204
5d74c00
 
875e204
 
 
 
 
 
5d74c00
 
875e204
5d74c00
 
875e204
5d74c00
 
875e204
5d74c00
 
 
 
 
875e204
5d74c00
 
 
 
 
 
 
 
875e204
 
5d74c00
 
875e204
5d74c00
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
875e204
5d74c00
 
875e204
 
5d74c00
 
 
 
 
 
 
 
875e204
5d74c00
875e204
 
5d74c00
 
 
 
 
 
 
 
 
 
 
 
 
 
875e204
 
 
5d74c00
875e204
 
5d74c00
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
875e204
5d74c00
 
875e204
5d74c00
 
 
 
 
 
 
 
 
 
 
 
875e204
 
5d74c00
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
875e204
5d74c00
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
875e204
5d74c00
 
 
 
 
 
 
 
 
 
 
 
 
875e204
5d74c00
875e204
5d74c00
 
 
875e204
5d74c00
 
 
 
 
 
 
 
 
 
 
 
 
 
 
875e204
5d74c00
875e204
5d74c00
 
 
 
 
 
 
 
875e204
5d74c00
875e204
5d74c00
 
 
 
 
875e204
5d74c00
875e204
5d74c00
875e204
5d74c00
875e204
5d74c00
875e204
5d74c00
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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Squarified Treemap - Folder Explorer</title>
    <style>
        /*
This space solves for Treemap visualization of the file space produced by folders, directories, paths, files, and properties of files and directories such as their accumulated size, create date, modified date, file type, words mentioned in filename, groups that are emergent on multivariate file spaces with omni modality.

Papers that represent ideas implemented:

Inspiration:
A heuristic extending the Squarified treemapping algorithm. abstract: https://arxiv.org/abs/1609.00754
A heuristic extending the Squarified treemapping algorithm. pdf: https://arxiv.org/pdf/1609.00754
Squarified Treemaps: https://vanwijk.win.tue.nl/stm.pdf
Treemaps with Bounded Aspect Ratio: https://arxiv.org/abs/1012.1749 https://arxiv.org/pdf/1012.1749
Interactive Visualisation of Hierarchical Quantitative Data: an Evaluation https://www.arxiv.org/abs/1908.01277v1 https://www.arxiv.org/pdf/1908.01277v1
https://en.wikipedia.org/wiki/Treemapping
A Novel Algorithm for Real-time Procedural Generation of Building Floor Plans https://ar5iv.labs.arxiv.org/html/1211.5842
Fat Polygonal Partitions with Applications to Visualization and Embeddings https://arxiv.org/abs/1009.1866 https://arxiv.org/pdf/1009.1866
Tiling heuristics and evaluation metrics for treemaps with a target node aspect ratio: https://www.diva-portal.org/smash/get/diva2:1129639/FULLTEXT01.pdf
        */
        /* General Styling */
        body {
            font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
            margin: 0;
            padding: 2rem;
            background-color: #f4f4f9;
            color: #333;
            text-align: center;
            display: flex;
            flex-direction: column;
            align-items: center;
            justify-content: flex-start;
            min-height: 100vh;
        }

        h1 {
            color: #2c3e50;
        }

        p {
            color: #555;
            margin-bottom: 2rem;
            max-width: 600px;
        }

        /* Input Button Styling */
        .folder-picker-label {
            display: inline-block;
            padding: 12px 24px;
            background-color: #3498db;
            color: white;
            border-radius: 8px;
            cursor: pointer;
            font-weight: bold;
            transition: background-color 0.3s ease, transform 0.2s ease;
        }

        .folder-picker-label:hover {
            background-color: #2980b9;
            transform: translateY(-2px);
        }

        #folder-picker {
            display: none; /* Hide the default file input */
        }

        /* Treemap Container */
        #treemap-container {
            position: relative;
            width: 90vw;
            max-width: 1200px;
            height: 75vh;
            margin: 2rem auto;
            border: 1px solid #ccc;
            box-shadow: 0 4px 12px rgba(0,0,0,0.1);
            background-color: #fff;
            border-radius: 8px;
            overflow: hidden; /* Ensures nodes don't spill out */
        }
        
        /* Styling for node groups (both leaves and internal directories) */
        .node-group {
            position: absolute;
            box-sizing: border-box;
            overflow: hidden;
        }

        /* Styling for internal nodes (directories) to give them a frame */
        .internal {
            border: 1px solid #aaa;
        }

        /* Styling for leaf nodes (files) */
        .leaf {
            background-clip: padding-box;
            box-shadow: inset 0px 0px 0px 1px rgba(255, 255, 255, 0.8);
            transition: filter 0.2s ease-in-out;
            display: flex; /* Use flexbox for alignment of label */
            align-items: flex-start; /* Align text to top */
            justify-content: flex-start; /* Align text to left */
        }

        .leaf:hover {
            filter: brightness(1.15);
            z-index: 10;
        }

        /* Directory labels (now as links) */
        .node-label {
            display: block;
            padding: 2px 5px;
            color: #fff;
            background-color: rgba(0,0,0,0.4);
            font-size: 12px;
            font-weight: bold;
            white-space: nowrap;
            overflow: hidden;
            text-overflow: ellipsis;
            text-decoration: none;
            cursor: pointer;
        }
        .node-label:hover {
            background-color: rgba(0,0,0,0.6);
        }
        
        /* File labels */
        .leaf-label {
            padding: 3px;
            color: rgba(255, 255, 255, 0.95);
            text-shadow: 1px 1px 2px rgba(0,0,0,0.7);
            text-align: left;
            font-size: 11px; /* Smaller font */
            white-space: normal; /* Allow wrapping */
            word-break: break-word; /* Break long words */
            pointer-events: none; /* Make sure label doesn't block mouse events on parent */
        }

        /* Tooltip for Hover-overs */
        #tooltip {
            position: fixed; /* Use fixed to position relative to viewport */
            background-color: rgba(0, 0, 0, 0.85);
            color: white;
            padding: 8px 12px;
            border-radius: 4px;
            pointer-events: none; /* Allows mouse events to pass through to elements below */
            opacity: 0;
            transition: opacity 0.2s;
            font-size: 14px;
            z-index: 1001;
            transform: translate(15px, 10px); /* Offset from cursor */
        }

        /* Custom Context Menu */
        #context-menu {
            position: fixed;
            display: none;
            background-color: #ecf0f1;
            border: 1px solid #bdc3c7;
            box-shadow: 0 2px 10px rgba(0,0,0,0.2);
            border-radius: 5px;
            padding: 5px 0;
            z-index: 1000;
        }

        #context-menu button {
            display: block;
            width: 100%;
            padding: 8px 20px;
            border: none;
            background: none;
            text-align: left;
            cursor: pointer;
            font-size: 14px;
        }

        #context-menu button:hover {
            background-color: #3498db;
            color: white;
        }

        /* Modal for confirmation */
        .modal-overlay {
            position: fixed;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background: rgba(0,0,0,0.5);
            display: none;
            align-items: center;
            justify-content: center;
            z-index: 2000;
        }
        .modal-content {
            background: white;
            padding: 20px;
            border-radius: 8px;
            text-align: center;
            box-shadow: 0 5px 15px rgba(0,0,0,0.3);
        }
        .modal-content p {
            margin-bottom: 20px;
        }
        .modal-content button {
            padding: 10px 20px;
            border: none;
            border-radius: 5px;
            cursor: pointer;
            margin: 0 10px;
        }
        #modal-confirm {
            background-color: #e74c3c;
            color: white;
        }
        #modal-cancel {
            background-color: #bdc3c7;
        }

    </style>
</head>
<body>

    <h1>Squarified Treemap Folder Explorer 🗺️</h1>
    <p>Select a folder to visualize its contents. Hover over files or directories for info. Right-click on a file for options.</p>

    <label for="folder-picker" class="folder-picker-label">Choose a Folder</label>
    <input type="file" id="folder-picker" webkitdirectory directory multiple />

    <div id="treemap-container"></div>
    <div id="tooltip"></div>

    <!-- Custom Context Menu Structure -->
    <div id="context-menu">
        <button id="menu-copy-path">Copy Path</button>
        <button id="menu-delete">Delete from View</button>
    </div>

    <!-- Modal Structure -->
    <div id="delete-modal" class="modal-overlay">
        <div class="modal-content">
            <p>This will only remove the item from the visualization.<br>It <strong>will not</strong> be deleted from your computer. Do you want to continue?</p>
            <button id="modal-confirm">Yes, Remove</button>
            <button id="modal-cancel">Cancel</button>
        </div>
    </div>


    <!-- D3.js Library for data visualization -->
    <script src="https://d3js.org/d3.v7.min.js"></script>

    <!-- JS App Logic -->
    <script>
        document.addEventListener('DOMContentLoaded', () => {
            const folderPicker = document.getElementById('folder-picker');
            const treemapContainer = document.getElementById('treemap-container');
            const tooltip = document.getElementById('tooltip');
            const contextMenu = document.getElementById('context-menu');
            const deleteModal = document.getElementById('delete-modal');

            let currentFileTree = null; // To store the current data structure for modification
            let nodeToDelete = null; // To store the node targeted for deletion

            /**
             * Processes the selected files and initiates the treemap rendering.
             */
            function handleFileSelect(event) {
                const files = event.target.files;
                if (files.length === 0) {
                    treemapContainer.innerHTML = '<p style="padding: 2rem;">No files selected or folder is empty.</p>';
                    return;
                }
                currentFileTree = buildFileTree(files);
                renderTreemap(currentFileTree);
            }

            /**
             * Builds a hierarchical tree structure from a flat FileList.
             * This version adds a 'path' property to directories as well.
             */
            function buildFileTree(files) {
                const root = { name: "root", path: "", children: [] }; // FIX: Added path property to root
                for (const file of files) {
                    if (file.size === 0) continue;
                    const pathParts = file.webkitRelativePath.split('/');
                    let currentNode = root;
                    let currentPath = '';

                    for (let i = 0; i < pathParts.length; i++) {
                        const part = pathParts[i];
                        // Reconstruct path at each level
                        currentPath = currentPath ? `${currentPath}/${part}` : part;

                        if (i === pathParts.length - 1) { // It's a file
                            currentNode.children.push({ name: part, value: file.size, path: currentPath });
                        } else { // It's a directory
                            let dirNode = currentNode.children.find(child => child.name === part && child.children);
                            if (!dirNode) {
                                dirNode = { name: part, children: [], path: currentPath };
                                currentNode.children.push(dirNode);
                            }
                            currentNode = dirNode;
                        }
                    }
                }
                return root;
            }

            /**
             * Renders the squarified treemap using D3.js.
             */
            function renderTreemap(data) {
                treemapContainer.innerHTML = '';
                const width = treemapContainer.clientWidth;
                const height = treemapContainer.clientHeight;

                const root = d3.hierarchy(data).sum(d => d.value).sort((a, b) => b.value - a.value);

                const treemapLayout = d3.treemap()
                    .size([width, height])
                    .paddingInner(1)
                    .paddingOuter(3)
                    .paddingTop(20)
                    .tile(d3.treemapSquarify);

                treemapLayout(root);
                const color = d3.scaleOrdinal(d3.schemeCategory10);

                const node = d3.select('#treemap-container')
                    .selectAll('div')
                    .data(root.descendants())
                    .join('div')
                    .attr('class', d => `node-group ${d.children ? 'internal' : 'leaf'}`)
                    .style('left', d => `${d.x0}px`)
                    .style('top', d => `${d.y0}px`)
                    .style('width', d => `${d.x1 - d.x0}px`)
                    .style('height', d => `${d.y1 - d.y0}px`);
                
                // --- Handle Leaves (Files) ---
                const leaves = node.filter(d => !d.children);
                
                leaves.style('background-color', d => {
                        let ancestor = d;
                        while (ancestor.depth > 1) { ancestor = ancestor.parent; }
                        return color(ancestor.data.name);
                    })
                    .on('contextmenu', (event, d) => {
                        event.preventDefault();
                        event.stopPropagation(); // Stop event from bubbling to parent context menus
                        nodeToDelete = d; 
                        contextMenu.style.display = 'block';
                        contextMenu.style.left = `${event.clientX}px`;
                        contextMenu.style.top = `${event.clientY}px`;
                    });

                // Add labels to the leaf nodes
                leaves.append('div')
                    .attr('class', 'leaf-label')
                    .text(d => d.data.name);

                // Attach hover listeners to leaves
                leaves.on('mouseenter', (event, d) => {
                        tooltip.style.opacity = 1; // FIX: Use direct style property
                        tooltip.innerHTML = `
                            <strong>File:</strong> ${d.data.name}<br>
                            <strong>Path:</strong> ${d.data.path}<br>
                            <strong>Size:</strong> ${formatBytes(d.value)}
                        `;
                    })
                    .on('mousemove', (event) => {
                        tooltip.style.left = `${event.clientX}px`; // FIX: Use direct style property
                        tooltip.style.top = `${event.clientY}px`; // FIX: Use direct style property
                    })
                    .on('mouseleave', () => {
                        tooltip.style.opacity = 0; // FIX: Use direct style property
                    });

                // --- Handle Directories ---
                const directories = node.filter(d => d.children);

                // Add labels to parent nodes (directories) as clickable links
                directories.append('a')
                    .attr('class', 'node-label')
                    .attr('href', d => `file:///${d.data.path ? d.data.path.replace(/\//g, '\\') : ''}`) // FIX: Check if path exists
                    .on('click', event => event.preventDefault()) // Prevent default left-click behavior
                    .text(d => d.data.name);

                // Attach hover listeners to directories
                directories.on('mouseenter', (event, d) => {
                        tooltip.style.opacity = 1; // FIX: Use direct style property
                        tooltip.innerHTML = `
                            <strong>Directory:</strong> ${d.data.path}<br>
                            <strong>Total Size:</strong> ${formatBytes(d.value)}
                        `;
                    })
                    .on('mousemove', (event) => {
                        tooltip.style.left = `${event.clientX}px`; // FIX: Use direct style property
                        tooltip.style.top = `${event.clientY}px`; // FIX: Use direct style property
                    })
                    .on('mouseleave', () => {
                        tooltip.style.opacity = 0; // FIX: Use direct style property
                    });
            }
            
            // --- Event Listeners ---
            folderPicker.addEventListener('change', handleFileSelect);

            // Hide context menu on any click
            window.addEventListener('click', () => {
                contextMenu.style.display = 'none';
            });

            // Context menu actions
            document.getElementById('menu-copy-path').addEventListener('click', () => {
                if (nodeToDelete && navigator.clipboard) {
                    navigator.clipboard.writeText(nodeToDelete.data.path).catch(err => console.error('Failed to copy path: ', err));
                }
            });

            document.getElementById('menu-delete').addEventListener('click', () => {
                if (nodeToDelete) {
                    deleteModal.style.display = 'flex';
                }
            });

            // Modal actions
            document.getElementById('modal-cancel').addEventListener('click', () => {
                deleteModal.style.display = 'none';
                nodeToDelete = null;
            });

            document.getElementById('modal-confirm').addEventListener('click', () => {
                if (nodeToDelete && nodeToDelete.parent) {
                    // Find and remove the node from its parent's children array in the data
                    const children = nodeToDelete.parent.data.children;
                    const index = children.findIndex(child => child.path === nodeToDelete.data.path);
                    if (index > -1) {
                        children.splice(index, 1);
                    }
                    renderTreemap(currentFileTree); // Re-render with the modified data
                }
                deleteModal.style.display = 'none';
                nodeToDelete = null;
            });

            // Resize handler
            window.addEventListener('resize', () => {
                if (currentFileTree) {
                    renderTreemap(currentFileTree);
                }
            });

            /**
             * Formats bytes into a human-readable string (KB, MB, GB).
             */
            function formatBytes(bytes) {
                if (bytes === 0) return '0 Bytes';
                const k = 1024;
                const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB'];
                const i = Math.floor(Math.log(bytes) / Math.log(k));
                return `${parseFloat((bytes / Math.pow(k, i)).toFixed(2))} ${sizes[i]}`;
            }
        });
    </script>

</body>
</html>