• Stars
    star
    168
  • Rank 225,507 (Top 5 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created over 8 years ago
  • Updated 6 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A browser-ready tree library that can efficiently display a large amount of data using infinite scrolling.

Infinite Tree build status Coverage Status

NPM

A browser-ready tree library that can efficiently display a large tree with smooth scrolling.

Demo: http://cheton.github.io/infinite-tree

infinite-tree

Features

Browser Support

Chrome
Chrome
Edge
Edge
Firefox
Firefox
IE
IE
Opera
Opera
Safari
Safari
Yes Yes Yes 8+ Yes Yes

Need to include es5-shim polyfill for IE8

React Support

Check out react-infinite-tree at https://github.com/cheton/react-infinite-tree.

Installation

npm install --save infinite-tree

Usage

const InfiniteTree = require('infinite-tree');

// when using webpack and browserify
require('infinite-tree/dist/infinite-tree.css');

const data = {
    id: 'fruit',
    name: 'Fruit',
    children: [{
        id: 'apple',
        name: 'Apple'
    }, {
        id: 'banana',
        name: 'Banana',
        children: [{
            id: 'cherry',
            name: 'Cherry',
            loadOnDemand: true
        }]
    }]
};

const tree = new InfiniteTree({
    el: document.querySelector('#tree'),
    data: data,
    autoOpen: true, // Defaults to false
    droppable: { // Defaults to false
        hoverClass: 'infinite-tree-droppable-hover',
        accept: function(event, options) {
            return true;
        },
        drop: function(event, options) {
        }
    },
    shouldLoadNodes: function(parentNode) {
        if (!parentNode.hasChildren() && parentNode.loadOnDemand) {
            return true;
        }
        return false;
    },
    loadNodes: function(parentNode, next) {
        // Loading...
        const nodes = [];
        nodes.length = 1000;
        for (let i = 0; i < nodes.length; ++i) {
            nodes[i] = {
                id: `${parentNode.id}.${i}`,
                name: `${parentNode.name}.${i}`,
                loadOnDemand: true
            };
        }

        next(null, nodes, function() {
            // Completed
        });
    },
    nodeIdAttr: 'data-id', // the node id attribute
    rowRenderer: function(node, treeOptions) { // Customizable renderer
        return '<div data-id="<node-id>" class="infinite-tree-item">' + node.name + '</div>';
    },
    shouldSelectNode: function(node) { // Determine if the node is selectable
        if (!node || (node === tree.getSelectedNode())) {
            return false; // Prevent from deselecting the current node
        }
        return true;
    }
});

Functions Usage

Learn more: Tree / Node

const node = tree.getNodeById('fruit');
// → Node { id: 'fruit', ... }
tree.selectNode(node);
// → true
console.log(node.getFirstChild());
// → Node { id: 'apple', ... }
console.log(node.getFirstChild().getNextSibling());
// → Node { id: 'banana', ... }
console.log(node.getFirstChild().getPreviousSibling());
// → null

Events Usage

Learn more: Events

tree.on('click', function(event) {});
tree.on('doubleClick', function(event) {});
tree.on('keyDown', function(event) {});
tree.on('keyUp', function(event) {});
tree.on('clusterWillChange', function() {});
tree.on('clusterDidChange', function() {});
tree.on('contentWillUpdate', function() {});
tree.on('contentDidUpdate', function() {});
tree.on('openNode', function(Node) {});
tree.on('closeNode', function(Node) {});
tree.on('selectNode', function(Node) {});
tree.on('checkNode', function(Node) {});
tree.on('willOpenNode', function(Node) {});
tree.on('willCloseNode', function(Node) {});
tree.on('willSelectNode', function(Node) {});
tree.on('willCheckNode', function(Node) {});

API Documentation

FAQ

Index

Creating tree nodes with checkboxes

Sets the checked attribute in your rowRenderer:

const tag = require('html5-tag');

const checkbox = tag('input', {
    type: 'checkbox',
    checked: node.state.checked,
    'class': 'checkbox',
    'data-indeterminate': node.state.indeterminate
});

In your tree, add 'click', 'contentDidUpdate', 'clusterDidChange' event listeners as below:

// `indeterminate` doesn't have a DOM attribute equivalent, so you need to update DOM on the fly.
const updateIndeterminateState = (tree) => {
    const checkboxes = tree.contentElement.querySelectorAll('input[type="checkbox"]');
    for (let i = 0; i < checkboxes.length; ++i) {
        const checkbox = checkboxes[i];
        if (checkbox.hasAttribute('data-indeterminate')) {
            checkbox.indeterminate = true;
        } else {
            checkbox.indeterminate = false;
        }
    }
};

tree.on('click', function(node) {
    const currentNode = tree.getNodeFromPoint(event.clientX, event.clientY);
    if (!currentNode) {
        return;
    }

    if (event.target.className === 'checkbox') {
        event.stopPropagation();
        tree.checkNode(currentNode);
        return;
    }
});

tree.on('contentDidUpdate', () => {
    updateIndeterminateState(tree);
});

tree.on('clusterDidChange', () => {
    updateIndeterminateState(tree);
});

How to attach click event listeners to nodes?

Use event delegation [1, 2]

const el = document.querySelector('#tree');
const tree = new InfiniteTree(el, { /* options */ });

tree.on('click', function(event) {
    const target = event.target || event.srcElement; // IE8
    let nodeTarget = target;

    while (nodeTarget && nodeTarget.parentElement !== tree.contentElement) {
        nodeTarget = nodeTarget.parentElement;
    }

    // Call event.stopPropagation() if you want to prevent the execution of
    // default tree operations like selectNode, openNode, and closeNode.
    event.stopPropagation(); // [optional]
    
    // Matches the specified group of selectors.
    const selectors = '.dropdown .btn';
    if (nodeTarget.querySelector(selectors) !== target) {
        return;
    }

    // do stuff with the target element.
    console.log(target);
};

Event delegation with jQuery:

const el = document.querySelector('#tree');
const tree = new InfiniteTree(el, { /* options */ });

// jQuery
$(tree.contentElement).on('click', '.dropdown .btn', function(event) {
    // Call event.stopPropagation() if you want to prevent the execution of
    // default tree operations like selectNode, openNode, and closeNode.
    event.stopPropagation();
    
    // do stuff with the target element.
    console.log(event.target);
});

How to use keyboard shortcuts to navigate through nodes?

tree.on('keyDown', (event) => {
    // Prevent the default scroll
    event.preventDefault();

    const node = tree.getSelectedNode();
    const nodeIndex = tree.getSelectedIndex();

    if (event.keyCode === 37) { // Left
        tree.closeNode(node);
    } else if (event.keyCode === 38) { // Up
        if (tree.filtered) { // filtered mode
            let prevNode = node;
            for (let i = nodeIndex - 1; i >= 0; --i) {
                if (tree.nodes[i].state.filtered) {
                    prevNode = tree.nodes[i];
                    break;
                }
            }
            tree.selectNode(prevNode);
        } else {
            const prevNode = tree.nodes[nodeIndex - 1] || node;
            tree.selectNode(prevNode);
        }
    } else if (event.keyCode === 39) { // Right
        tree.openNode(node);
    } else if (event.keyCode === 40) { // Down
        if (tree.filtered) { // filtered mode
            let nextNode = node;
            for (let i = nodeIndex + 1; i < tree.nodes.length; ++i) {
                if (tree.nodes[i].state.filtered) {
                    nextNode = tree.nodes[i];
                    break;
                }
            }
            tree.selectNode(nextNode);
        } else {
            const nextNode = tree.nodes[nodeIndex + 1] || node;
            tree.selectNode(nextNode);
        }
    }
});

How to filter nodes?

In your row renderer, returns undefined or an empty string to filter out unwanted nodes (i.e. node.state.filtered === false):

import tag from 'html5-tag';

const renderer = (node, treeOptions) => {
    if (node.state.filtered === false) {
        return;
    }

    // Do something

    return tag('div', treeNodeAttributes, treeNode);
};
Usage
tree.filter(predicate, options)

Use a string or a function to test each node of the tree. Otherwise, it will render nothing after filtering (e.g. tree.filter(), tree.filter(null), tree.flter(0), tree.filter({}), etc.). If the predicate is an empty string, all nodes will be filtered. If the predicate is a function, returns true to keep the node, false otherwise.

Filter by string
const keyword = 'text-to-filter';
const filterOptions = {
    caseSensitive: false,
    exactMatch: false,
    filterPath: 'props.name', // Defaults to 'name'
    includeAncestors: true,
    includeDescendants: true
};
tree.filter(keyword, filterOptions);
Filter by function
const keyword = 'text-to-filter';
const filterOptions = {
    includeAncestors: true,
    includeDescendants: true
};
tree.filter(function(node) {
    const name = node.name || '';
    return name.toLowerCase().indexOf(keyword) >= 0;
});
Turn off filter

Calls tree.unfilter() to turn off filter.

tree.unfilter();

How to select multiple nodes using the ctrl key (or meta key)?

You need to maintain an array of selected nodes by yourself. See below for details:

let selectedNodes = [];
tree.on('click', (event) => {
    // Return the node at the specified point
    const currentNode = tree.getNodeFromPoint(event.clientX, event.clientY);
    if (!currentNode) {
        return;
    }

    const multipleSelectionMode = event.ctrlKey || event.metaKey;

    if (!multipleSelectionMode) {
        if (selectedNodes.length > 0) {
            // Call event.stopPropagation() to stop event bubbling
            event.stopPropagation();

            // Empty an array of selected nodes
            selectedNodes.forEach(selectedNode => {
                selectedNode.state.selected = false;
                tree.updateNode(selectedNode, {}, { shallowRendering: true });
            });
            selectedNodes = [];

            // Select current node
            tree.state.selectedNode = currentNode;
            currentNode.state.selected = true;
            tree.updateNode(currentNode, {}, { shallowRendering: true });
        }
        return;
    }

    // Call event.stopPropagation() to stop event bubbling
    event.stopPropagation();

    const selectedNode = tree.getSelectedNode();
    if (selectedNodes.length === 0 && selectedNode) {
        selectedNodes.push(selectedNode);
        tree.state.selectedNode = null;
    }

    const index = selectedNodes.indexOf(currentNode);

    // Remove current node if the array length of selected nodes is greater than 1
    if (index >= 0 && selectedNodes.length > 1) {
        currentNode.state.selected = false;
        selectedNodes.splice(index, 1);
        tree.updateNode(currentNode, {}, { shallowRendering: true });
    }

    // Add current node to the selected nodes
    if (index < 0) {
        currentNode.state.selected = true;
        selectedNodes.push(currentNode);
        tree.updateNode(currentNode, {}, { shallowRendering: true });
    }
});

License

MIT

More Repositories

1

browserify-css

A Browserify transform for bundling, rebasing, inlining, and minifying CSS files.
JavaScript
144
star
2

is-electron

Detect if running in Electron.
JavaScript
103
star
3

react-infinite-tree

The infinite-tree library for React.
JavaScript
72
star
4

github-release-cli

A command-line tool for managing release assets on a GitHub repository.
JavaScript
72
star
5

find-imports

Find all imported modules in JavaScript files.
JavaScript
37
star
6

flattree

Convert hierarchical tree structure to flat structure.
JavaScript
31
star
7

react-repeatable

A press and hold wrapper component that can trigger hold action multiple times while holding down.
JavaScript
23
star
8

universal-logger

A universal logging library for Node and the browser.
JavaScript
12
star
9

robotarm

JavaScript Powered Robot Arm with Johnny-Five
JavaScript
12
star
10

notes

Notes
Vim Script
12
star
11

webapp-boilerplate

WebApp Boilerplate is a production-ready JavaScript stack for both the front-end and back-end, which is comprised of HTML/JavaScript/CSS tools and frameworks for building web applications.
JavaScript
10
star
12

webappengine

A web application server that can host multiple web apps running with Node.js.
JavaScript
9
star
13

ensure-type

Ensure runtime type safety and perform type coercion if needed.
JavaScript
7
star
14

namespace-constants

Namespacing Redux action type constant values.
JavaScript
5
star
15

universal-logger-browser

Browser plugins for universal logger.
JavaScript
5
star
16

chained-function

Chained function calls
JavaScript
5
star
17

react-foreach

A React component that renders each element of collection using a function as child component.
JavaScript
5
star
18

react-bootstrap-buttons

React Bootstrap buttons.
JavaScript
5
star
19

i18next-text

Using i18next translations without the need to maintain i18n keys.
JavaScript
5
star
20

react-webapp-base

Building a complete web application with React
JavaScript
4
star
21

iot-weather-display

IoT Weather Display with Johnny-Five
JavaScript
4
star
22

multihost

Hosting multiple Express apps on the same server.
JavaScript
3
star
23

html5-tag

A simple utility for creating HTML5 tags.
JavaScript
3
star
24

rbac

Role-based access control with javascript (incl. jquery plugin)
JavaScript
3
star
25

readable-size

Converts bytes into human readable size units.
JavaScript
3
star
26

generator-webappengine

A Yeoman Generator for WebAppEngine. It includes Gulp, Browserify, Babelify, Stylus, Handlebars, i18next, and React.
JavaScript
3
star
27

kickstart.js

Using a Universal Module Definition (UMD) pattern to create a library or module that offers compatibility with the most popular script loaders of the day.
JavaScript
3
star
28

react-facebook-loading

The Facebook loading animation for React.
JavaScript
2
star
29

jquery-checkboxset

jQuery Checkbox Set Plugin
JavaScript
2
star
30

common-utility-libraries

Common utility libraries used in my projects
JavaScript
2
star
31

jsdc2015

JSDC 2015 - Road to JavaScript Maker
HTML
2
star
32

tm-react-intro

Introduction to React
JavaScript
1
star
33

cheton

1
star
34

html-webpack-plugin-addons

Addons for the html-webpack-plugin to alter the html in your webpack config.
JavaScript
1
star