Javascript tree traversal and manipulation library (Forked from Nenad V. Nikolić, originally by Andrea Fiore)
- Version:
-
- 0.0.1
- Copyright:
-
- 2017 Vasiliy Altunin
- License:
-
- MIT
- Source:
Methods
-
<static> Arboreal( [parent] [, data] [, id] [, separator])
-
Creates Arboreal root tree object or child node object
Parameters:
Name Type Argument Description parent
Arboreal <optional>
Parent node
data
Object <optional>
Data to store in node
id
String <optional>
node id
separator
Char <optional>
separator for id numbers
- Source:
Returns:
Arboreal object
- Type
- Arboreal
Example
* var tree = new Arboreal(null,{category: 'JavaScript'}); console.log(tree.toString(true)); Result: 0 {"category":"JavaScript"}
-
<static> Arboreal.parse(object, childrenAttr, parent)
-
Parses Object and creates tree from it
Parameters:
Name Type Description object
Object Object to parse
childrenAttr
String name of object attr to use as children attr
parent
Arboreal Parent node, if null new tree created
- Source:
Returns:
Arboreal root object or parent object
- Type
- Arboreal
Example
var wikipediaJsCategory = { category: 'JavaScript', subcategories: [ {category: 'Ajax (programming)'}, {category: 'JavaScript engines'}, {category: 'JavaScript programming languages family', subcategories: [{ category: 'JavaScript dialect engines' }] }, {category: 'JavaScript based calendar components'}, {category: 'JavaScript based HTML editors'} ] }; tree = Arboreal.parse(wikipediaJsCategory, 'subcategories'); Result: 0 {"category":"JavaScript"} |- 0/0 {"category":"Ajax (programming)"} |- 0/1 {"category":"JavaScript engines"} |- 0/2 {"category":"JavaScript programming languages family"} |- 0/2/0 {"category":"JavaScript dialect engines"} |- 0/3 {"category":"JavaScript based calendar components"} |- 0/4 {"category":"JavaScript based HTML editors"}
-
<static> Arboreal#appendChild(data [, id])
-
Appends child node to tree
Parameters:
Name Type Argument Description data
Object Data to store in node
id
String <optional>
Node id
- Source:
Returns:
Arboreal root object or parent object
- Type
- Arboreal
Example
var tree = new Arboreal(null,{category: 'JavaScript'}); tree.appendChild({category: 'Ajax (programming)'}); Result 0 {"category":"JavaScript"} |- 0/0 {"category":"Ajax (programming)"}
-
<static> Arboreal#appendChildren(data, childrenAttr)
-
Parse data and add it to
this
nodeParameters:
Name Type Description data
Object Object to be parsed
childrenAttr
String Attr to use as children attr
- Source:
Returns:
Arboreal root object or parent object
- Type
- Arboreal
Example
var wikipediaJsCategory = { category: 'JavaScript', subcategories: [ {category: 'Ajax (programming)'}, {category: 'JavaScript engines'}, {category: 'JavaScript programming languages family', subcategories: [{ category: 'JavaScript dialect engines' }] }, {category: 'JavaScript based calendar components'}, {category: 'JavaScript based HTML editors'} ] }; tree = Arboreal.parse(wikipediaJsCategory, 'subcategories'); var subCategory = { category: 'JavaScript ajax query', subcategories: [ {category: 'JSON'}, {category: 'jQuery'} ]}; tree.children[0].appendChildren(subCategory, 'subcategories'); console.log(tree.toString(true)); Result: 0 {"category":"JavaScript"} |- 0/0 {"category":"Ajax (programming)"} |- 0/0/0 {"category":"JavaScript ajax query"} |- 0/0/0/0 {"category":"JSON"} |- 0/0/0/1 {"category":"jQuery"} |- 0/1 {"category":"JavaScript engines"} |- 0/2 {"category":"JavaScript programming languages family"} |- 0/2/0 {"category":"JavaScript dialect engines"} |- 0/3 {"category":"JavaScript based calendar components"} |- 0/4 {"category":"JavaScript based HTML editors"}
-
<static> Arboreal#bubbleUp(iterator)
-
Traverse tree bubble up
Parameters:
Name Type Description iterator
function Iterator function
- Source:
Example
var wikipediaJsCategory = { category: 'JavaScript', subcategories: [ {category: 'Ajax (programming)'}, {category: 'JavaScript engines'}, {category: 'JavaScript programming languages family', subcategories: [{ category: 'JavaScript dialect engines' }] }, {category: 'JavaScript based calendar components'}, {category: 'JavaScript based HTML editors'} ] }; function iterator (node) { var depth = "", i; for (i = 1; i <= node.depth; i++) depth += ">>"; console.log([depth, node.data.category].join(" ")); } tree.children[2].children[0].bubbleUp(iterator); Result: >>>> JavaScript dialect engines >> JavaScript programming languages family JavaScript
-
<static> Arboreal#find(finder)
-
Finds node in tree by using iterator
Parameters:
Name Type Description finder
function Iterator
- Source:
Returns:
First match
- Type
- Arboreal
Example
var wikipediaJsCategory = { category: 'JavaScript', subcategories: [ {category: 'Ajax (programming)'}, {category: 'JavaScript engines'}, {category: 'JavaScript programming languages family', subcategories: [{ category: 'JavaScript dialect engines' }] }, {category: 'JavaScript based calendar components'}, {category: 'JavaScript based HTML editors'} ] }; var result = tree.find(function (node) { return (/calendar/).test(node.data.category) }).data.category; console.log(result); Result: JavaScript based calendar components
-
<static> Arboreal#path(path [, separator])
-
Returns Arboreal for givent id path
Parameters:
Name Type Argument Description path
String Id string
separator
Char <optional>
Separator to use
- Source:
Returns:
Node finded by path
- Type
- Arboreal
Example
var wikipediaJsCategory = { category: 'JavaScript', subcategories: [ {category: 'Ajax (programming)'}, {category: 'JavaScript engines'}, {category: 'JavaScript programming languages family', subcategories: [{ category: 'JavaScript dialect engines' }] }, {category: 'JavaScript based calendar components'}, {category: 'JavaScript based HTML editors'} ] }; var result = tree.path("/2/0").data.category; console.log(result); Result: JavaScript dialect engines
-
<static> Arboreal#remove()
-
Removes
this
node- Source:
Returns:
- Type
- Arboreal
Example
var wikipediaJsCategory = { category: 'JavaScript', subcategories: [ {category: 'Ajax (programming)'}, {category: 'JavaScript engines'}, {category: 'JavaScript programming languages family', subcategories: [{ category: 'JavaScript dialect engines' }] }, {category: 'JavaScript based calendar components'}, {category: 'JavaScript based HTML editors'} ] }; tree.children[0].remove(); console.log(tree.toString(true)); Result: 0 {"category":"JavaScript"} |- 0/1 {"category":"JavaScript engines"} |- 0/2 {"category":"JavaScript programming languages family"} |- 0/2/0 {"category":"JavaScript dialect engines"} |- 0/3 {"category":"JavaScript based calendar components"} |- 0/4 {"category":"JavaScript based HTML editors"}
-
<static> Arboreal#removeChild(arg)
-
Remove child node from
this
nodeParameters:
Name Type Description arg
String | Node Node id or Arboreal
- Source:
Returns:
Arboreal
- Type
- Arboreal
Example
var wikipediaJsCategory = { category: 'JavaScript', subcategories: [ {category: 'Ajax (programming)'}, {category: 'JavaScript engines'}, {category: 'JavaScript programming languages family', subcategories: [{ category: 'JavaScript dialect engines' }] }, {category: 'JavaScript based calendar components'}, {category: 'JavaScript based HTML editors'} ] }; tree.removeChild(2); console.log(tree.toString(true)); Result: 0 {"category":"JavaScript"} |- 0/0 {"category":"Ajax (programming)"} |- 0/1 {"category":"JavaScript engines"} |- 0/3 {"category":"JavaScript based calendar components"} |- 0/4 {"category":"JavaScript based HTML editors"}
-
<static> Arboreal#toString( [isValue])
-
Retrns string representation of a tree
Parameters:
Name Type Argument Description isValue
boolean <optional>
If true than data value printed
- Source:
Returns:
- Type
- String
Example
var wikipediaJsCategory = { category: 'JavaScript', subcategories: [ {category: 'Ajax (programming)'}, {category: 'JavaScript engines'}, {category: 'JavaScript programming languages family', subcategories: [{ category: 'JavaScript dialect engines' }] }, {category: 'JavaScript based calendar components'}, {category: 'JavaScript based HTML editors'} ] }; console.log(tree.toString()); Result: 0 |- 0/0 |- 0/1 |- 0/2 |- 0/2/0 |- 0/3 |- 0/4 If True passed as arg result changed: 0 {"category":"JavaScript"} |- 0/0 {"category":"Ajax (programming)"} |- 0/1 {"category":"JavaScript engines"} |- 0/2 {"category":"JavaScript programming languages family"} |- 0/2/0 {"category":"JavaScript dialect engines"} |- 0/3 {"category":"JavaScript based calendar components"} |- 0/4 {"category":"JavaScript based HTML editors"}
-
<static> Arboreal#traverseDown(iterator)
-
Traverse tree down
Parameters:
Name Type Description iterator
function Iterator function
- Source:
Example
//Traverse and creating HTML list discribed in example //https://vasiliyaltunin.github.io/arboreal.js/examples var wikipediaJsCategory = { category: 'JavaScript', subcategories: [ {category: 'Ajax (programming)'}, {category: 'JavaScript engines'}, {category: 'JavaScript programming languages family', subcategories: [{ category: 'JavaScript dialect engines' }] }, {category: 'JavaScript based calendar components'}, {category: 'JavaScript based HTML editors'} ] }; function iterator (node) { var depth = "", i; for (i = 1; i <= node.depth; i++) depth += ">>"; console.log([depth, node.data.category].join(" ")); } tree.traverseDown(iterator); Result: JavaScript >> Ajax (programming) >> JavaScript engines >> JavaScript programming languages family >>>> JavaScript dialect engines >> JavaScript based calendar components >> JavaScript based HTML editors
-
<static> Arboreal#traverseUp(iterator)
-
Traverse tree up
Parameters:
Name Type Description iterator
function Iterator function
- Source:
Example
var wikipediaJsCategory = { category: 'JavaScript', subcategories: [ {category: 'Ajax (programming)'}, {category: 'JavaScript engines'}, {category: 'JavaScript programming languages family', subcategories: [{ category: 'JavaScript dialect engines' }] }, {category: 'JavaScript based calendar components'}, {category: 'JavaScript based HTML editors'} ] }; function iterator (node) { var depth = "", i; for (i = 1; i <= node.depth; i++) depth += ">>"; console.log([depth, node.data.category].join(" ")); } tree.children[2].traverseUp(iterator); Result: >> JavaScript programming languages family >>>> JavaScript dialect engines JavaScript >> Ajax (programming) >> JavaScript engines >> JavaScript based calendar components >> JavaScript based HTML editors