Skip to content

SteelDrEgg/TreeView.js

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

14 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

🌳 TreeView.js

Light weight tree-view component

sample.png

Check out [Live Demo]

Getting Started

Import script from CDN

https://cdn.jsdelivr.net/gh/SteelDrEgg/TreeView.js@main/dist/treeview.min.js

Import css from CDN

https://cdn.jsdelivr.net/gh/SteelDrEgg/TreeView.js@main/css/TreeView.min.css

Create a div and TreeView.js could attach itself onto it.

<!DOCTYPE html>
<html>
<head>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/SteelDrEgg/TreeView.js@main/css/TreeView.min.css">
    <script src="https://cdn.jsdelivr.net/gh/SteelDrEgg/TreeView.js@main/dist/treeview.min.js"></script>
</head>
<body>
    <div id="my-tree"></div>
    <script>
        const tree = TreeViewJS.createTree('my-tree', 'white');
        const folder = tree.createNode('πŸ“ Documents', false, 'images/folder.png');
        folder.createChildNode('πŸ“„ File 1.txt', false, 'images/file.png');
        
        tree.drawTree();
    </script>
</body>
</html>

API

Create a Tree

Create a new tree and attach it to a div by calling createTree

const tree = TreeViewJS.createTree(containerId, backgroundColor, contextMenus);

Parameters:

  • containerId (string) - DOM element ID for tree container
  • backgroundColor (string, optional) - Background color (default: 'white')
  • contextMenus (object, optional) - Context menu configuration

Node Management

Create note / child

const node = tree.createNode(text, expanded, icon, parent, tag, contextMenu);
const child = node.createChildNode(text, expanded, icon, tag, contextMenu);

Node operations

node.expandNode();        // Expand single node
node.collapseNode();      // Collapse single node  
node.toggleNode();        // Toggle expand/collapse
node.expandSubtree();     // Expand node and all children
node.collapseSubtree();   // Collapse node and all children
node.setText(newText);    // Update node text
node.removeNode();        // Delete node and children
node.removeChildNodes();  // Delete all children

Tree operations

tree.drawTree();          // Render the tree
tree.expandTree();        // Expand all nodes
tree.collapseTree();      // Collapse all nodes
tree.selectNode(node);    // Select specific node

Events

Before expanding a node

tree.nodeBeforeOpenEvent = (node) => {
    console.log('Before expanding:', node.text);
};

After expanding a node

tree.nodeAfterOpenEvent = (node) => {
    console.log('After expanding:', node.text);
};

Before collapsing a node

tree.nodeBeforeCloseEvent = (node) => {
    console.log('Before collapsing:', node.text);
};

Context Menus

A right click context menu is defined by an object

export interface ContextMenuItem {
    text: string;
    icon?: string;
    action: (node: TreeNode) => void;
    submenu?: ContextMenu;
}

export interface ContextMenu {
    elements: ContextMenuItem[];
}

export interface ContextMenus {
    [key: string]: ContextMenu;
}

Example

const contextMenus = {
    'menu1': {
        elements: [
            {
                text: 'Acton 1',
                icon: 'icon.png',
                action: (node) => {
                    const newText = prompt('Enter new text:', node.text);
                    if (newText) node.setText(newText);
                }
            },
            {
                text: 'Actions',
                icon: 'folder.png', 
                submenu: {
                    elements: [
                        {
                            text: 'Add Child',
                            icon: 'add.png',
                            action: (node) => {
                                node.createChildNode('New Child', false, 'file.png');
                            }
                        },
                        {
                            text: 'Delete Node',
                            icon: 'delete.png',
                            action: (node) => {
                                if (confirm(`Delete "${node.text}"?`)) {
                                    node.removeNode();
                                }
                            }
                        }
                    ]
                }
            }
        ]
    }
};

Build

TS

TreeView.js uses ESBuild by default.

npm run build

The product is located in /dist

dist/
β”œβ”€β”€ treeview.js          # Development bundle
β”œβ”€β”€ treeview.js.map      # Source map for debugging
β”œβ”€β”€ treeview.min.js      # Production bundle
└── treeview.min.js.map  # Minified source map

CSS

TreeView.js uses sass for css minification.

sass css/TreeView.css css/TreeView.min.css --style compressed

About

Light TreeView component for web

Resources

License

Stars

Watchers

Forks

Contributors