npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@machinek56/tree

v1.8.0

Published

a lightweight tree widget, compatible with originaljs/react/vue, 9.6kb size for tree.min.js&tree.min.css without gzip.

Downloads

168

Readme

@widgetjs/tree

NPM version David deps npm downloads gzip size

A lightweight tree widget.

Features

  • Compatible with VanillaJS / React / Vue
  • Tiny size after gzip
  • Zero dependence

Demo

Online Demo

demo.gif

Install

npm i -S @widgetjs/tree

Usage

React/Vue usage

import Tree from '@widgetjs/tree';

VanillaJS usage

<script src="path/to/tree.min.js"></script>

Initialize

new Tree(treeContainer, parameters), returns initialized Tree instance.

  • treeContainer - string - css selector of the tree container(document.querySelector inside).
  • parameters - object - options of the tree.

For example:

var myTree = new Tree('#container', {
  url: '/api/treeJson',
});

Basic Node Format

{
  "id": "unique_ID",
  "text": "node-0",
  "attributes": {},
  "children": [],
  "check": true
}

| Name | Type | Description | Required | | ---------- | ------- | ----------------------------------- | -------- | | id | any | unique id | Required | | text | string | tree node label | Required | | attributes | object | custom attributes of the node | Optional | | children | array | children of current node | Optional | | check | boolean | whether the node is selected or not | Optional |

Parameters

| Name | Type | Description | | ---------- | -------- | ------------------------------------------------------------------- | | url | string | a URL to retrieve remote data,or use data | | method | string | http method(GET/POST), default 'GET' | | data | array | the json tree data | | values | array | ids which you want to check | | closeDepth | integer | expand level | | beforeLoad | function | invoke before the tree load data. Format raw data in this function. | | loaded | function | invoke after the tree load data | | onChange | function | invoke when the node status change |

Example

var myTree = new Tree('#container', {
  url: '/api/treeJson',
  method: 'GET',

  values: ['1', '2', '3'],

  // only expand level 1 node
  closeDepth: 1,

  beforeLoad: function(rawData) {
    function formatData() {
      // do some format
    }
    return formatData(rawData);
  },

  loaded: function() {
    // do something or set values after Tree loaded callback
    // do not use arrow function `()=>` , if you use `this`, use function instead.
    // this context bind current tree instance
    this.values = ['0-1'];
  },

  onChange: function() {
    console.log(this.values);
  },
});

Properties

| Property | Type | Operation | Description | | ------------- | ----- | --------- | ------------------------------------------ | | values | array | get/set | selected values. | | selectedNodes | array | get | selected nodes data with attributes. | | disables | array | get/set | get disabled values, or set disable nodes. | | disabledNodes | array | get | disabled nodes data with attributes. |

myTree.values

// get
var values = myTree.values;

// set
tree.values = ['0-1'];

myTree.selectedNodes

// get
var selectedNodes = myTree.selectedNodes;

myTree.disables

// get
var disables = myTree.disables;

// set
tree.disables = ['0-1'];

myTree.disabledNodes

// get
var disabledNodes = myTree.disabledNodes;

Events

| Event | Parameters | Description | | ---------- | ------------ | ---------------------------------- | | beforeLoad | current data | invoke before the tree load data | | loaded | null | invoke after the tree load data | | onChange | null | invoke when the node status change |

Examples

var treeData = [
  {
    id: '0',
    text: 'node-0',
    children: [
      {
        id: '0-0',
        text: 'node-0-0',
        children: [
          {id: '0-0-0', text: 'node-0-0-0'},
          {id: '0-0-1', text: 'node-0-0-1'},
          {id: '0-0-2', text: 'node-0-0-2'},
        ],
      },
      {id: '0-1', text: 'node-0-1'},
    ],
  },
  {
    id: '1',
    text: 'node-1',
    children: [{id: '1-0', text: 'node-1-0'}, {id: '1-1', text: 'node-1-1'}],
  },
];

var myTree = new Tree('#container', {
  data: treeData,
});

var myTree = new Tree('#container', {
  url: '/api/treeWithCheckedStatusJson',
});

Like @widgetjs/tree? just 🌟 star the project! Create issues if you find bug.