greco / tree-model-js

Manipulate and traverse tree-like structures in javascript.

Home Page:http://jnuno.com/tree-model-js

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

TreeModel

Manipulate and traverse tree-like structures in javascript.

For download and demos, please visit TreeModel website.

Installation

Node

TreeModel is available as an npm module so you can install it with npm install tree-model and use it in your script:

var TreeModel = require('tree-model'),
    tree = new TreeModel(),
    root = tree.parse({name: 'a', children: [{name: 'b'}]});

Browser

Using requirejs

<script src="path/to/require.js"></script>
<script>
    require(["path/to/TreeModel"], function(TreeModel) {
        var tree = new TreeModel(),
            root = tree.parse({name: 'a', children: [{name: 'b'}]});
    });
</script>

As a global variable

<script src="path/to/TreeModel.js"></script>
<script>
    var tree = new TreeModel(),
        root = tree.parse({name: 'a', children: [{name: 'b'}]});
</script>

API Reference

var tree = new TreeModel(options)

Create a new TreeModel with the given options.

Valid options are:
  • childrenPropertyName - The name for the children array property. Default is children;
  • modelComparatorFn - A comparator function to sort the children when parsing the model and adding children. The default order policy is to keep the parsed order and append new children. The comparator function receives the model for two nodes just like the Array.sort function.

Node tree.parse(model)

Parse the given user defined model and return the root Node object.

#### `Boolean node.isRoot()`

Return `true` if this Node is the root, `false` otherwise.

#### `Node node.addChild(node)`

Add the given node as child of this one. Return the child Node.

#### `Array node.getPath()`

Get the array of Nodes representing the path from the root to this Node (inclusive).

#### `Node node.drop()`

Drop the subtree starting at this node. Returns the node itself, which is now a root node.

#### `Node node.first(predicate)`

Starting from this node, find the first Node that matches the predicate and return it.

The **predicate** is a function wich receives the visited Node and returns `true` if the Node should be picked and `false` otherwise.

#### `Array node.all(predicate)`

Starting from this node, find all Nodes that match the predicate and return these.

#### `node.walk(action)`

Starting from this node, traverse the subtree calling the action for each visited node. The action is a function which receives the visited Node as argument. The traversal can be halted by returning `false` from the action.


**Note** - `first`, `all` and `walk` can optionally receive as first argument an object with traversal options. Currently the only supported option is the traversal `strategy` which can be any of the following:
  • {strategy: 'pre'} - Depth-first pre-order [default];
  • {strategy: 'post'} - Depth-first post-order;
  • {strategy: 'breadth'} - Breadth-first.

[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/joaonuno/tree-model-js/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

About

Manipulate and traverse tree-like structures in javascript.

http://jnuno.com/tree-model-js

License:MIT License