diff --git a/.npmignore b/.npmignore new file mode 100644 index 0000000..0030936 --- /dev/null +++ b/.npmignore @@ -0,0 +1,5 @@ +node_modules/ +coverage/ +.nyc_output/ +nyc_output/ +docs/ diff --git a/.travis.yml b/.travis.yml index 74b5fcc..52593d2 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,2 +1,5 @@ -before_script: "npm install --dev" +language: node_js +node_js: + - "6" + - "5" script: "npm test" diff --git a/Gruntfile.js b/Gruntfile.js new file mode 100644 index 0000000..a1343c4 --- /dev/null +++ b/Gruntfile.js @@ -0,0 +1,91 @@ +"use strict"; + +module.exports = function (grunt) { + + + + // Project configuration. + grunt.initConfig({ + // Metadata. + pkg: grunt.file.readJSON('arboreal.json'), + banner: '/*! <%= pkg.title || pkg.name %> - v<%= pkg.version %> - ' + + '<%= grunt.template.today("yyyy-mm-dd") %>\n' + + '<%= pkg.homepage ? "* " + pkg.homepage + "\\n" : "" %>' + + '* Copyright (c) <%= grunt.template.today("yyyy") %> <%= pkg.author.name %> (<%= pkg.author.email %>);' + + ' Licensed <%= _.map(pkg.licenses, "type").join(", ") %>; See MIT-LICENSE.txt for more info*/\n', + // Task configuration. + clean: { + files: ['lib/arboreal.min.js'] + }, + uglify: { + options: { + banner: '<%= banner %>', + mangle: true + }, + dist: { + files: [{ + 'lib/arboreal.min.js': 'lib/arboreal.js', + }] + } + }, + jshint: { + options: { + jshintrc: true, + reporterOutput: "", + }, + gruntfile: { + src: 'Gruntfile.js' + }, + src: { + src: ['lib/**/*.js'] + }, + test: { + src: ['test/**/*.js'] + }, + }, + watch: { + gruntfile: { + files: '<%= jshint.gruntfile.src %>', + tasks: ['jshint:gruntfile'] + }, + src: { + files: '', + tasks: ['', ''] + }, + test: { + files: '<%= jshint.test.src %>', + tasks: ['jshint:test', 'qunit'] + }, + }, + qunit: { + files: ['test/**/*.html'] + }, + + jsdoc : { + dist : { + expand: true, + src: ['lib/**/*.js', 'README.md'], + options: { + destination: 'docs/doc', + template : "node_modules/ink-docstrap/template", + configure : "jsdoc.json", + } + } + }, + + + }); + + // These plugins provide necessary tasks. + grunt.loadNpmTasks('grunt-contrib-uglify'); + grunt.loadNpmTasks('grunt-contrib-jshint'); + grunt.loadNpmTasks('grunt-contrib-watch'); + grunt.loadNpmTasks('grunt-contrib-qunit'); + grunt.loadNpmTasks('grunt-contrib-clean'); + grunt.loadNpmTasks('grunt-jsdoc'); + + + // Default task. + grunt.registerTask('default', ['clean', 'jshint', 'qunit', 'jsdoc','uglify']); + +}; \ No newline at end of file diff --git a/MIT-LICENSE.txt b/MIT-LICENSE.txt index c16d38a..eb04270 100644 --- a/MIT-LICENSE.txt +++ b/MIT-LICENSE.txt @@ -1,5 +1,9 @@ The MIT License (MIT) Copyright © 2013 Andrea Fiore +Copyright © 2013 Evgeny Gusev +Copyright © 2014 Alex Pernot +Copyright © 2016 Nenad V. Nikolić +Copyright © 2017 Vasiliy Altunin Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal diff --git a/README.md b/README.md index 20543f7..c7c7de7 100644 --- a/README.md +++ b/README.md @@ -1,153 +1,84 @@ -# Arboreal.js +[![NPM](https://nodei.co/npm/arboreal.js.png?downloads=true&downloadRank=true&stars=true)](https://nodei.co/npm/arboreal.js/) -A micro-library for traversing and manipulating tree-like data -structures in JavaScript; works with both node.js and the browser. - - - -## Installation - -In node.js: - - git clone git://github.com/afiore/arboreal - npm install - -To use it in the browser, just load `lib/arboreal.js` in -a script tag. +# Arboreal.js [![Built with Grunt](https://cdn.gruntjs.com/builtwith.png)](http://gruntjs.com/) -## Usage - -Arboreal provides a set of methods for parsing, manipulating, and -traversing tree like data structures. A tree can be created from scratch and then extended with child elements. - - var tree = new Arboreal() - - tree - .appendChild() - .appendChild() - .children[0] - .appendChild() - .appendChild(); - -For each child node, Arboreal will automatically assign an id string representing the depth and the index -the position of the node within the tree structure. +[![Build Status](https://travis-ci.org/vasiliyaltunin/arboreal.js.svg?branch=master)](https://travis-ci.org/vasiliyaltunin/arboreal.js) [![GitHub license](https://img.shields.io/badge/license-MIT-blue.svg)](https://raw.githubusercontent.com/vasiliyaltunin/arboreal.js/master/MIT-LICENSE.txt) [![dependencies Status](https://david-dm.org/vasiliyaltunin/arboreal.js/status.svg)](https://david-dm.org/vasiliyaltunin/arboreal.js) [![devDependencies Status](https://david-dm.org/vasiliyaltunin/arboreal.js/dev-status.svg)](https://david-dm.org/vasiliyaltunin/arboreal.js?type=dev) - tree.children[0].children[1].id - // => 0/0/1 - -Alternatively, Arboreal can also parse an existing object into a tree (though it will need to -know the name of the 'children' attribute). - - 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 tree = Arborel.parse(wikipediaJsCategory, 'subcategories'); - -### Traversal - -An Arboreal object can be traversed either upwards or downwards. - - function iterator (node) { - var depth = "", i; - for (i = 1; i <= node.depth; i++) depth += ">>"; - console.info([depth, node.data.category].join(" ")); - } - - tree.traverseDown(iterator); +A micro-library for traversing and manipulating tree-like data +structures in JavaScript, works with both node.js and the browser. - // => JavaScript - // >> Ajax (programming) - // >> JavaScript engines - // >> JavaScript produgramming languages family - // >>>> JavaScript dialect engines - // >> JavaScript based calendar components - // >> JavaScript based HTML editors +(Forked from [Nenad V. Nikolić](https://github.com/shonzilla/arboreal), originally by [Andrea Fiore](https://github.com/afiore/arboreal)) +## Installation - tree.children[2].traverseUp(iterator); +Install via npm: - // => >> JavaScript produgramming languages family - // >>>> JavaScript dialect engines - // JavaScript - // >> Ajax (programming) - // >> JavaScript engines - // >> JavaScript based calendar components - // >> JavaScript based HTML editors +```bash +% npm install arboreal.js +``` +## Usage -Note that in both the `traverseDown` and the `traverseUp` methods, the -value of `this` in the iterator is bound to the value of the -currently traversed `node`. Our iterator function can in fact be -rewritten as: - function iterator () { - var depth = "", i; - for (i = 1; i <= this.depth; i++) depth += ">>"; - console.info([depth, this.data.category].join(" ")); - } +Add script to you webpage -### Search +```html + +``` -In order to search for a single node into an arboreal object, one can use the `find` -method. +Arboreal.js provides a set of methods for parsing, manipulating, and +traversing tree like data structures. A tree can be created from scratch and then extended with child elements. - tree.find(function (node) { - return (/calendar/).test(node.data.category) - }).data.category; + var tree = new Arboreal(null, {category: 'JavaScript'}); - // => JavaScript based calendar components + tree.appendChild({category: 'Ajax (programming)'}) + .appendChild({category: 'JavaScript engines'}) + .appendChild({category: 'JavaScript programming languages family'}) + .children[2] + .appendChild({category: 'JavaScript dialect engines'}) + .parent + .appendChild({category: 'JavaScript based calendar components'}) + .appendChild({category: 'JavaScript based HTML editors'}); -The find method will also accept a string as an argument. In that case, -it will try to find a node by id. +For each child node, Arboreal.js will automatically assign an id string representing the depth and the index +the position of the node within the tree structure. - tree.find("0/2/0").data.category + 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"} - // => JavaScript dialect engines +Check our [wiki](https://github.com/vasiliyaltunin/arboreal.js/wiki) for more usage examples. -### Manipulation +Check our [documentation](https://vasiliyaltunin.github.io/arboreal.js/doc/module-arboreal.html) -While traversing a tree, nodes can be deleted by calling the `remove` -method on the node object bound to the iterator function. +Also check this [demo](https://vasiliyaltunin.github.io/arboreal.js/examples). - tree.length +## Contributing - // => 7 +Here's a quick guide: - tree.traverseDown(function (item) { - var toDelete = 'JavaScript programming languages family'; - if (item.data.category === toDelete) { - this.remove(); - } - }); +1. Fork the repo - tree.length; +2. `npm install` - // 5 +3. `grunt` -## Testing +4. Make you changes and add test for you functionality. Look into `/test` folder. -Arboreal test suite uses [Jasmine](http://pivotal.github.com/jasmine/). -To run it in node.js.. +5. Check that all test passed by running `grunt` - cd /home/me/code/arboreal && npm test +6. Push to your fork and submit a pull request. -To run it in the browser, just open the `test/index.html` ## Minfication -A minified version of Arboreal can be generated by running +A minified version generated into `/lib` when you run `grunt` + +## Licence - node make.js +Released under MIT License - https://opensource.org/licenses/MIT diff --git a/arboreal.json b/arboreal.json new file mode 100644 index 0000000..f16bf3f --- /dev/null +++ b/arboreal.json @@ -0,0 +1,26 @@ +{ + "name": "arboreal.js", + "title": "Arboreal.js", + "description": "Javascript tree traversal and manipulation library (Forked from Nenad V. Nikolić, originally by Andrea Fiore)", + "version": "0.0.3", + "homepage": "https://github.com/vasiliyaltunin/arboreal.js", + "author": { + "name": "Vasiliy Altunin", + "email": "skyr@altunin.online", + "url": "http://altunin.online" + }, + "repository": { + "type": "git", + "url": "git://github.com/vasiliyaltunin/arboreal.git" + }, + "bugs": "https://github.com/vasiliyaltunin/arboreal/issues", + "licenses": [ + { + "type": "MIT", + "url": "https://opensource.org/licenses/MIT" + } + ], + "dependencies": { + }, + "keywords": [] +} \ No newline at end of file diff --git a/config.json b/config.json deleted file mode 100644 index ef23d2b..0000000 --- a/config.json +++ /dev/null @@ -1,19 +0,0 @@ -{ - "VERSION": "0.1", - "JSHINT_OPTS": { - "onevar": true, - "newcap": true, - "undef": true, - "browser": true, - "devel": true, - "maxerr": 1000, - "indent": 2, - "predef": [ - "module", "require", "window" - ] - }, - "JAVASCRIPT": { - "DIST_DIR": "build", - "arboreal": ["lib/arboreal.js"] - } -} diff --git a/contributors.md b/contributors.md new file mode 100644 index 0000000..17baf15 --- /dev/null +++ b/contributors.md @@ -0,0 +1,14 @@ +###### Contributors +[Nenad V. Nikolić](https://github.com/shonzilla) +11 Commits / 37++ / 22-- +57.89% ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||

+[EvgenyGusev](https://github.com/EvgenyGusev) +6 Commits / 124++ / 20-- +31.58% ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||

+[Alex Pernot](https://github.com/AlexPernot) +1 Commits / 7++ / 5-- +05.26% ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||

+[Andrea Fiore](https://github.com/afiore) +1 Commits / 20++ / 0-- +05.26% ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||

+###### [Generated](https://github.com/jakeleboeuf/contributor) on Wed Apr 12 2017 \ No newline at end of file diff --git a/docs/README.md b/docs/README.md new file mode 100644 index 0000000..c7c7de7 --- /dev/null +++ b/docs/README.md @@ -0,0 +1,84 @@ +[![NPM](https://nodei.co/npm/arboreal.js.png?downloads=true&downloadRank=true&stars=true)](https://nodei.co/npm/arboreal.js/) + +# Arboreal.js [![Built with Grunt](https://cdn.gruntjs.com/builtwith.png)](http://gruntjs.com/) + +[![Build Status](https://travis-ci.org/vasiliyaltunin/arboreal.js.svg?branch=master)](https://travis-ci.org/vasiliyaltunin/arboreal.js) [![GitHub license](https://img.shields.io/badge/license-MIT-blue.svg)](https://raw.githubusercontent.com/vasiliyaltunin/arboreal.js/master/MIT-LICENSE.txt) [![dependencies Status](https://david-dm.org/vasiliyaltunin/arboreal.js/status.svg)](https://david-dm.org/vasiliyaltunin/arboreal.js) [![devDependencies Status](https://david-dm.org/vasiliyaltunin/arboreal.js/dev-status.svg)](https://david-dm.org/vasiliyaltunin/arboreal.js?type=dev) + + +A micro-library for traversing and manipulating tree-like data +structures in JavaScript, works with both node.js and the browser. + +(Forked from [Nenad V. Nikolić](https://github.com/shonzilla/arboreal), originally by [Andrea Fiore](https://github.com/afiore/arboreal)) + +## Installation + +Install via npm: + +```bash +% npm install arboreal.js +``` + +## Usage + + +Add script to you webpage + +```html + +``` + +Arboreal.js provides a set of methods for parsing, manipulating, and +traversing tree like data structures. A tree can be created from scratch and then extended with child elements. + + var tree = new Arboreal(null, {category: 'JavaScript'}); + + tree.appendChild({category: 'Ajax (programming)'}) + .appendChild({category: 'JavaScript engines'}) + .appendChild({category: 'JavaScript programming languages family'}) + .children[2] + .appendChild({category: 'JavaScript dialect engines'}) + .parent + .appendChild({category: 'JavaScript based calendar components'}) + .appendChild({category: 'JavaScript based HTML editors'}); + +For each child node, Arboreal.js will automatically assign an id string representing the depth and the index +the position of the node within the tree structure. + + 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"} + +Check our [wiki](https://github.com/vasiliyaltunin/arboreal.js/wiki) for more usage examples. + +Check our [documentation](https://vasiliyaltunin.github.io/arboreal.js/doc/module-arboreal.html) + +Also check this [demo](https://vasiliyaltunin.github.io/arboreal.js/examples). + +## Contributing + +Here's a quick guide: + +1. Fork the repo + +2. `npm install` + +3. `grunt` + +4. Make you changes and add test for you functionality. Look into `/test` folder. + +5. Check that all test passed by running `grunt` + +6. Push to your fork and submit a pull request. + + +## Minfication + +A minified version generated into `/lib` when you run `grunt` + +## Licence + +Released under MIT License - https://opensource.org/licenses/MIT diff --git a/docs/doc/arboreal.js.html b/docs/doc/arboreal.js.html new file mode 100644 index 0000000..ce560b8 --- /dev/null +++ b/docs/doc/arboreal.js.html @@ -0,0 +1,995 @@ + + + + + + + Arboreal.js Source: arboreal.js + + + + + + + + + + + + + +
+
+ + +
+ +
+ + +

Source: arboreal.js

+ +
+
+
/**
+ * @file Arboreal.js
+ * @author Vasiliy Altunin <skyr@altunin.online>
+ * @copyright 2017 Vasiliy Altunin
+ * @version 0.0.1
+ * @license MIT
+ * @description Javascript tree traversal and manipulation library (Forked from Nenad V. Nikolić, originally by Andrea Fiore)
+ * @module arboreal
+ */
+
+(function () {
+
+    /**
+     * @private
+     * @param {type} array
+     * @param {type} item
+     * @returns {Number|arboreal_L8.indexOf.i}
+     */
+    function indexOf(array, item) {
+        for (var i = 0, j = array.length; i < j; i++) {
+            if (array[i] === item) {
+                return i;
+            }
+        }
+        return -1;
+    }
+
+    /**
+     * @private
+     * @param {type} array
+     * @param {type} item
+     * @returns {Boolean}
+     */
+    function include(array, item) {
+        return indexOf(array, item) > -1;
+    }
+
+    /**
+     * Traverse down tree iterator
+     * @private
+     * @param {type} context  
+     * @param {function} iterator Iterator function to be called when traverse node
+     * @param {function} iteratorAfter Optional iterator function to be called AFTER node was traversed
+     */
+    function _traverseDown(context, iterator, iteratorAfter) {
+        var doContinue = true;
+
+        (function walkDown(node) {
+            var i, newContext;
+
+            if (!doContinue) {
+                return;
+            }
+
+            if (iterator(node) === false) {
+                //break the traversal loop if the iterator returns a falsy value
+                doContinue = false;
+            } else {
+                for (i = 0; i < node.children.length; i++) {
+                    newContext = node.children[i];
+                    walkDown(newContext);
+                    if (iteratorAfter !== undefined)
+                    {
+                        iteratorAfter.call(newContext, newContext);
+                    }
+                }
+            }
+        })(context);
+    }
+
+    /**
+     * Traverse up tree iterator
+     * @private
+     * @param {type} context
+     * @param {function} iterator Iterator function to be called when traverse node
+     */
+    function _traverseUp(context, iterator) {
+        var i, node;
+
+        while (context) {
+            if (iterator(context) === false) {
+                return;
+            }
+
+            for (i = 0; i < context.children.length; i++) {
+                node = context.children[i];
+                if (iterator(node) === false) {
+                    return;
+                }
+            }
+            context = context.parent;
+        }
+    }
+
+    /**
+     * Bubble up tree iterator
+     * @private
+     * @param {type} context
+     * @param {function} iterator Iterator function to be called when traverse node     * 
+     */
+    function _bubbleUp(context, iterator) {
+
+        while (context) {
+            if (iterator(context) === false) {
+                return;
+            }
+            context = context.parent;
+        }
+    }
+
+    /**
+     * Traverse node and calls callback
+     * @private
+     * @param {type} context
+     * @param {type} iterator Iterator function to be called when traverse node
+     * @param {type} callback Iterator callback
+     * @param {type} iteratorAfter Optional iterator function to be called AFTER node was traversed
+     */
+    function _traverse(context, iterator, callback, iteratorAfter) {
+        var visited = [],
+                callIterator = function (node) {
+                    var id = node.id,
+                            returned;
+
+                    if (!include(visited, id)) {
+                        returned = iterator.call(node, node);
+                        visited.push(id);
+
+                        if (returned === false) {
+                            return returned;
+                        }
+                    }
+                };
+        // ,i, node;
+
+        callback(context, callIterator, iteratorAfter);
+    }
+
+
+    /**
+     * Removes node 
+     * @private
+     * @param {node} node
+     * @returns {Arboreal}
+     */
+    function _removeChild(node) {
+        var parent = node.parent,
+                child,
+                i;
+
+        for (i = 0; i < parent.children.length; i++) {
+            child = parent.children[i];
+
+            if (child === node) {
+                return parent.children.splice(i, 1).shift();
+            }
+        }
+    }
+
+    /**
+     * Build Id for node by concat parent node id with separator and parent children length
+     * @private
+     * @param {Node} parent Parent node 
+     * @param {Char} Separator for numbers in Id
+     * @returns {String} Id string
+     */
+    function _nodeId(parent, separator) {
+        separator = separator || '/';
+        if (parent) {
+            return [parent.id, parent.children.length].join(separator);
+        } else {
+            return '0';
+        }
+    }
+
+    /**
+     * Creates Arboreal root tree object or child node object
+     * @param {Arboreal=} parent Parent node 
+     * @param {Object=} data Data to store in node
+     * @param {String=} id - node id
+     * @param {Char=} separator - separator for id numbers
+     * @memberOf module:arboreal
+     * @example      * 
+     * var tree = new Arboreal(null,{category: 'JavaScript'});
+     * console.log(tree.toString(true));
+     * 
+     * Result:
+     * 
+     * 0 {"category":"JavaScript"}
+     * @returns {Arboreal} Arboreal object
+     */
+    function Arboreal(parent, data, id, separator) {
+        this.depth = parent ? parent.depth + 1 : 0;
+        this.data = data || {};
+        this.parent = parent || null;
+
+        if (separator === undefined)
+        {
+            if ((parent !== undefined) && (parent !== null))
+            {
+                this.separator = this.parent.separator;
+            } else
+            {
+                this.separator = '/';
+            }
+        } else
+        {
+            this.separator = separator;
+        }
+        this.id = id || _nodeId(parent, this.separator);
+        this.children = [];
+    }
+
+    /**
+     * Parses Object and creates tree from it
+     * @param {Object} object Object to parse
+     * @param {String} childrenAttr - name of object attr to use as children attr
+     * @param {Arboreal} parent - Parent node, if null new tree created
+     * @memberOf module: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"}
+     * @returns {Arboreal} Arboreal root object or parent object
+     */
+    Arboreal.parse = function (object, childrenAttr, parent) {
+        var root, getNodeData = function (node) {
+            var attr, nodeData = {};
+            for (attr in node) {
+                if (attr !== childrenAttr) {
+                    nodeData[attr] = node[attr];
+                }
+            }
+            return nodeData;
+        };
+
+        (function walkDown(node, parent) {
+            var newNode, i;
+
+            if (!parent) {
+                newNode = root = new Arboreal(null, getNodeData(node));
+            } else {
+                newNode = new Arboreal(parent, getNodeData(node));
+                parent.children.push(newNode);
+            }
+            if (childrenAttr in node) {
+                for (i = 0; i < node[childrenAttr].length; i++) {
+                    walkDown(node[childrenAttr][i], newNode);
+                }
+            }
+        })(object, parent);
+
+        return root || parent;
+
+    };
+
+    /**
+     * Appends child node to tree
+     * @param {Object} data Data to store in node
+     * @param {String=} id - Node id
+     * @memberOf module:arboreal
+     * @example
+     * var tree = new Arboreal(null,{category: 'JavaScript'});
+     * tree.appendChild({category: 'Ajax (programming)'});
+     * 
+     * Result
+     * 
+     * 0 {"category":"JavaScript"}
+     *  |- 0/0 {"category":"Ajax (programming)"}
+     * @returns {Arboreal} Arboreal root object or parent object
+     */
+    Arboreal.prototype.appendChild = function (data, id) {
+        var child = new Arboreal(this, data, id);
+        this.children.push(child);
+        return this;
+    };
+    /**
+     * Parse data and add it to `this` node
+     * @param {Object} data Object to be parsed
+     * @param {String} childrenAttr Attr to use as children attr
+     * @memberOf module: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"}     
+     * @returns {Arboreal} Arboreal root object or parent object
+     */
+    Arboreal.prototype.appendChildren = function (data, childrenAttr) {
+        return Arboreal.parse(data, childrenAttr, this);
+    };
+
+    /**
+     * Remove child node from `this` node
+     * @param {String= | Node=} arg Node id or Arboreal
+     * @memberOf module: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"}     
+     * @returns {Arboreal} Arboreal
+     */
+    Arboreal.prototype.removeChild = function (arg) {
+        if (typeof arg === 'number' && this.children[arg]) {
+            return this.children.splice(arg, 1).shift();
+        }
+        if (arg instanceof Arboreal) {
+            return _removeChild(arg);
+        }
+        throw new Error("Invalid argument " + arg);
+    };
+
+    /**
+     * Removes `this` node
+     * @memberOf module: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"}
+     * @returns {Arboreal}
+     */
+    Arboreal.prototype.remove = function () {
+        return _removeChild(this);
+    };
+
+    /**
+     * Traverse tree down
+     * @param {function} iterator Iterator function
+     * @memberOf module:arboreal
+     * @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
+     */
+    Arboreal.prototype.traverseDown = function (iterator, iteratorAfter) {
+        _traverse(this, iterator, _traverseDown, iteratorAfter);
+    };
+
+
+
+    /**
+     * Traverse tree up
+     * @param {function} iterator Iterator function
+     * @memberOf module: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'}
+     *     ]
+     * };
+     * 
+     * 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
+     */
+    Arboreal.prototype.traverseUp = function (iterator) {
+        _traverse(this, iterator, _traverseUp);
+    };
+
+    /**
+     * Traverse tree bubble up
+     * @param {function} iterator Iterator function
+     * @memberOf module: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'}
+     *     ]
+     * };
+     * 
+     * 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
+     */
+    Arboreal.prototype.bubbleUp = function (iterator) {
+        _traverse(this, iterator, _bubbleUp);
+    };
+
+    /**
+     * Retrns string representation of a tree
+     * @param {boolean=} isValue If true than data value printed
+     * @memberOf module: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'}
+     *     ]
+     * };
+     * 
+     * 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"}
+     * @returns {String}
+     */
+    Arboreal.prototype.toString = function (isValue) {
+        var lines = [];
+        isValue = isValue || false;
+
+        this.traverseDown(function (node) {
+            var separator = '|- ', indentation = '', i;
+
+            var value = "";
+            if (isValue)
+            {
+                value = JSON.stringify(node.data);
+            }
+
+            if (node.depth === 0) {
+                lines.push(node.id + " " + value);
+                return;
+            }
+            for (i = 0; i < node.depth; i++) {
+                indentation += ' ';
+            }
+
+            lines.push(indentation + separator + node.id + indentation + value);
+        });
+        return lines.join("\n");
+    };
+
+    /**
+     * Finds node in tree by using iterator
+     * @param {function} finder Iterator
+     * @memberOf module: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
+     * @returns {Arboreal} First match
+     */
+
+    Arboreal.prototype.find = function (finder) {
+        var match = null,
+                iterator = (typeof finder === 'function') ?
+                finder : function (node) {
+                    if (node.id === finder) {
+                        match = node;
+                        return false;
+                    }
+                };
+
+        this.traverseDown(function (node) {
+            if (iterator.call(this, node)) {
+                match = node;
+                return false;
+            }
+        });
+
+        return match;
+    };
+
+    /**
+     * Returns Arboreal for givent id path
+     * @param {String} path Id string 
+     * @param {Char=} separator Separator to use
+     * @memberOf module: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
+     * @returns {Arboreal} Node finded by path
+     */
+    Arboreal.prototype.path = function (path, separator) {
+        separator = separator || '/';
+        //allow path to begin with 
+        if (path[0] === separator) {
+            path = path.substring(1);
+        }
+
+        var indexes = path.split(separator),
+                index = null,
+                context = this,
+                i;
+
+        for (i = 0; i < indexes.length; i++) {
+            index = parseInt(indexes[i], 10);
+            context = (context.children.length && context.children.length > index) ?
+                    context.children[index] : null;
+        }
+
+        return context;
+    };
+
+
+    /**
+     * Converts tree to array of objects
+     * @private
+     * @returns {array} Array pepresentation of tree
+     */
+    Arboreal.prototype.toArray = function () {
+        var nodeList = [];
+        this.traverseDown(function (node) {
+            nodeList.push(node);
+        });
+        return nodeList;
+    };
+
+    /**
+     * Returns root node for `this`
+     * @private
+     * @returns {Arboreal}
+     */
+    Arboreal.prototype.root = function () {
+        var node = this;
+
+        if (!node.parent) {
+            return this;
+        }
+
+        while (node.parent) {
+            node = node.parent;
+        }
+        return node;
+    };
+
+    /**
+     * Checks is root
+     * @private
+     * @returns {Arboreal}
+     */
+    Arboreal.prototype.isRoot = function () {
+        return !this.parent;
+    };
+
+    /**
+     * @private
+     */
+    Object.defineProperty(Arboreal.prototype, 'length', {
+        get: function () {
+            return this.toArray().length;
+        }
+    });
+
+    if (typeof module !== 'undefined' && module.exports) {
+        module.exports = Arboreal;
+    } else {
+        this.Arboreal = Arboreal;
+    }
+
+}(this));
+
+
+
+ + + + + +
+
+ +
+ + + +
+
+ + + + + +
+ + + + Copyright (c) 2017 Vasiliy Altunin. + + + + Documentation generated by JSDoc 3.4.3 + + on Fri Apr 14th 2017 + + using the DocStrap template. + +
+ + + + + + + + + + + + + + + + + + + + + + + diff --git a/docs/doc/arboreal.module_js.html b/docs/doc/arboreal.module_js.html new file mode 100644 index 0000000..f56076b --- /dev/null +++ b/docs/doc/arboreal.module_js.html @@ -0,0 +1,321 @@ + + + + + + + Arboreal.js Module: arboreal.js + + + + + + + + + + + + + +
+
+ + +
+ +
+ + +

Module: arboreal.js

+
+ +
+ +
+ + +
+
+ + +
Javascript tree traversal and manipulation library (Forked from Nenad V. Nikolić, originally by Andrea Fiore)
+ + + +
+ + + +
Version:
+
+
    +
  • 0.0.1
  • +
+
+ + + + + + + + + + + + + + + + + +
Author:
+
+ +
+ + + + + + + + +
License:
+
+
    +
  • MIT; See MIT-LICENSE.txt for more info
  • +
+
+ + + + + + + +
Source:
+
+ +
+ + + + + + + +
+ + + + +
+ + + + + + + + + + + + + + + + + + +
+ +
+ + + + +
+
+ +
+ + +
+ +
+ + +
+
+ + + + + +
+ + + + Copyright (c) 2017 Vasiliy Altunin. + + + + Documentation generated by JSDoc 3.4.3 + + on Wed Apr 12th 2017 + + using the DocStrap template. + +
+ + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/docs/doc/fonts/glyphicons-halflings-regular.eot b/docs/doc/fonts/glyphicons-halflings-regular.eot new file mode 100644 index 0000000..b93a495 Binary files /dev/null and b/docs/doc/fonts/glyphicons-halflings-regular.eot differ diff --git a/docs/doc/fonts/glyphicons-halflings-regular.svg b/docs/doc/fonts/glyphicons-halflings-regular.svg new file mode 100644 index 0000000..94fb549 --- /dev/null +++ b/docs/doc/fonts/glyphicons-halflings-regular.svg @@ -0,0 +1,288 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/docs/doc/fonts/glyphicons-halflings-regular.ttf b/docs/doc/fonts/glyphicons-halflings-regular.ttf new file mode 100644 index 0000000..1413fc6 Binary files /dev/null and b/docs/doc/fonts/glyphicons-halflings-regular.ttf differ diff --git a/docs/doc/fonts/glyphicons-halflings-regular.woff b/docs/doc/fonts/glyphicons-halflings-regular.woff new file mode 100644 index 0000000..9e61285 Binary files /dev/null and b/docs/doc/fonts/glyphicons-halflings-regular.woff differ diff --git a/docs/doc/fonts/glyphicons-halflings-regular.woff2 b/docs/doc/fonts/glyphicons-halflings-regular.woff2 new file mode 100644 index 0000000..64539b5 Binary files /dev/null and b/docs/doc/fonts/glyphicons-halflings-regular.woff2 differ diff --git a/docs/doc/img/glyphicons-halflings-white.png b/docs/doc/img/glyphicons-halflings-white.png new file mode 100644 index 0000000..3bf6484 Binary files /dev/null and b/docs/doc/img/glyphicons-halflings-white.png differ diff --git a/docs/doc/img/glyphicons-halflings.png b/docs/doc/img/glyphicons-halflings.png new file mode 100644 index 0000000..a996999 Binary files /dev/null and b/docs/doc/img/glyphicons-halflings.png differ diff --git a/docs/doc/index.html b/docs/doc/index.html new file mode 100644 index 0000000..eede6ff --- /dev/null +++ b/docs/doc/index.html @@ -0,0 +1,275 @@ + + + + + + + Arboreal.js Index + + + + + + + + + + + + + +
+
+ + +
+ +
+ + + + + + + + + + + + + + + + + + + + + + +
+

NPM

+

Arboreal.js Built with Grunt

Build Status GitHub license dependencies Status devDependencies Status

+

A micro-library for traversing and manipulating tree-like data +structures in JavaScript, works with both node.js and the browser.

+

(Forked from Nenad V. Nikolić, originally by Andrea Fiore)

+

Installation

Install via npm:

+
% npm install arboreal.js

Usage

Add script to you webpage

+
<script src="../lib/arboreal.min.js" type="text/javascript"></script>

Arboreal.js provides a set of methods for parsing, manipulating, and +traversing tree like data structures. A tree can be created from scratch and then extended with child elements.

+
var tree = new Arboreal(null, {category: 'JavaScript'});
+
+tree.appendChild({category: 'Ajax (programming)'})
+        .appendChild({category: 'JavaScript engines'})
+        .appendChild({category: 'JavaScript programming languages family'})
+            .children[2]
+            .appendChild({category: 'JavaScript dialect engines'})
+        .parent
+        .appendChild({category: 'JavaScript based calendar components'})
+        .appendChild({category: 'JavaScript based HTML editors'});

For each child node, Arboreal.js will automatically assign an id string representing the depth and the index +the position of the node within the tree structure.

+
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"}

Check our wiki for more usage examples.

+

Check our documentation

+

Also check this demo.

+

Contributing

Here's a quick guide:

+
    +
  1. Fork the repo

    +
  2. +
  3. npm install

    +
  4. +
  5. grunt

    +
  6. +
  7. Make you changes and add test for you functionality. Look into /test folder.

    +
  8. +
  9. Check that all test passed by running grunt

    +
  10. +
  11. Push to your fork and submit a pull request.

    +
  12. +
+

Minfication

A minified version generated into /lib when you run grunt

+

Licence

Released under MIT License - https://opensource.org/licenses/MIT

+
+ + + + + + + +
+
+ +
+ + +
+ +
+ + +
+
+ + + + + +
+ + + + Copyright (c) 2017 Vasiliy Altunin. + + + + Documentation generated by JSDoc 3.4.3 + + on Fri Apr 14th 2017 + + using the DocStrap template. + +
+ + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/docs/doc/module-arboreal.html b/docs/doc/module-arboreal.html new file mode 100644 index 0000000..a616ba5 --- /dev/null +++ b/docs/doc/module-arboreal.html @@ -0,0 +1,2500 @@ + + + + + + + Arboreal.js Module: arboreal + + + + + + + + + + + + + +
+
+ + +
+ +
+ + +

Module: arboreal

+
+ +
+ +
+ + +
+
+ + +

Javascript tree traversal and manipulation library (Forked from Nenad V. Nikolić, originally by Andrea Fiore)

+ + + +
+ + + +
Version:
+
+
    +
  • 0.0.1
  • +
+
+ + + + + + + + + + + + + + + + + +
Author:
+
+ +
+ + + + + + + + +
License:
+
+
    +
  • MIT
  • +
+
+ + + + + + + +
Source:
+
+ +
+ + + + + + + +
+ + + + +
+ + + + + + + + + + + + + + +

Methods

+ +
+ +
+
+

<static> Arboreal( [parent] [, data] [, id] [, separator])

+ + +
+
+ + +
+

Creates Arboreal root tree object or child node object

+
+ + + + + + + + +
Parameters:
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeArgumentDescription
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:
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeDescription
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:
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeArgumentDescription
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 node

+
+ + + + + + + + +
Parameters:
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeDescription
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:
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeDescription
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:
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeDescription
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:
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeArgumentDescription
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 node

+
+ + + + + + + + +
Parameters:
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeDescription
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:
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeArgumentDescription
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:
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeDescription
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:
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeDescription
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
+ + + +
+ +
+ + + + + +
+ +
+ + + + +
+
+ +
+ + +
+ +
+ + +
+
+ + + + + +
+ + + + Copyright (c) 2017 Vasiliy Altunin. + + + + Documentation generated by JSDoc 3.4.3 + + on Fri Apr 14th 2017 + + using the DocStrap template. + +
+ + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/docs/doc/modules.list.html b/docs/doc/modules.list.html new file mode 100644 index 0000000..14aabed --- /dev/null +++ b/docs/doc/modules.list.html @@ -0,0 +1,286 @@ + + + + + + + Arboreal.js Modules + + + + + + + + + + + + + +
+
+ + +
+ +
+ + +

Modules

+
+ +
+ +

+ +

+ + +
+ + +
+
+ + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + + +
+ + + + + + + + + + + + + + + + + + +
+ +
+ + + + +
+
+ +
+ + +
+ +
+ + +
+
+ + + + + +
+ + + + Copyright (c) 2017 Vasiliy Altunin. + + + + Documentation generated by JSDoc 3.4.3 + + on Fri Apr 14th 2017 + + using the DocStrap template. + +
+ + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/docs/doc/quicksearch.html b/docs/doc/quicksearch.html new file mode 100644 index 0000000..80aea3c --- /dev/null +++ b/docs/doc/quicksearch.html @@ -0,0 +1,31 @@ + + + + + + + + + + + + + diff --git a/docs/doc/scripts/docstrap.lib.js b/docs/doc/scripts/docstrap.lib.js new file mode 100644 index 0000000..09d9272 --- /dev/null +++ b/docs/doc/scripts/docstrap.lib.js @@ -0,0 +1,11 @@ +if(!function(a,b){"object"==typeof module&&"object"==typeof module.exports?module.exports=a.document?b(a,!0):function(a){if(!a.document)throw new Error("jQuery requires a window with a document");return b(a)}:b(a)}("undefined"!=typeof window?window:this,function(a,b){function c(a){var b="length"in a&&a.length,c=_.type(a);return"function"!==c&&!_.isWindow(a)&&(!(1!==a.nodeType||!b)||("array"===c||0===b||"number"==typeof b&&b>0&&b-1 in a))}function d(a,b,c){if(_.isFunction(b))return _.grep(a,function(a,d){return!!b.call(a,d,a)!==c});if(b.nodeType)return _.grep(a,function(a){return a===b!==c});if("string"==typeof b){if(ha.test(b))return _.filter(b,a,c);b=_.filter(b,a)}return _.grep(a,function(a){return U.call(b,a)>=0!==c})}function e(a,b){for(;(a=a[b])&&1!==a.nodeType;);return a}function f(a){var b=oa[a]={};return _.each(a.match(na)||[],function(a,c){b[c]=!0}),b}function g(){Z.removeEventListener("DOMContentLoaded",g,!1),a.removeEventListener("load",g,!1),_.ready()}function h(){Object.defineProperty(this.cache={},0,{get:function(){return{}}}),this.expando=_.expando+h.uid++}function i(a,b,c){var d;if(void 0===c&&1===a.nodeType)if(d="data-"+b.replace(ua,"-$1").toLowerCase(),c=a.getAttribute(d),"string"==typeof c){try{c="true"===c||"false"!==c&&("null"===c?null:+c+""===c?+c:ta.test(c)?_.parseJSON(c):c)}catch(a){}sa.set(a,b,c)}else c=void 0;return c}function j(){return!0}function k(){return!1}function l(){try{return Z.activeElement}catch(a){}}function m(a,b){return _.nodeName(a,"table")&&_.nodeName(11!==b.nodeType?b:b.firstChild,"tr")?a.getElementsByTagName("tbody")[0]||a.appendChild(a.ownerDocument.createElement("tbody")):a}function n(a){return a.type=(null!==a.getAttribute("type"))+"/"+a.type,a}function o(a){var b=Ka.exec(a.type);return b?a.type=b[1]:a.removeAttribute("type"),a}function p(a,b){for(var c=0,d=a.length;d>c;c++)ra.set(a[c],"globalEval",!b||ra.get(b[c],"globalEval"))}function q(a,b){var c,d,e,f,g,h,i,j;if(1===b.nodeType){if(ra.hasData(a)&&(f=ra.access(a),g=ra.set(b,f),j=f.events)){delete g.handle,g.events={};for(e in j)for(c=0,d=j[e].length;d>c;c++)_.event.add(b,e,j[e][c])}sa.hasData(a)&&(h=sa.access(a),i=_.extend({},h),sa.set(b,i))}}function r(a,b){var c=a.getElementsByTagName?a.getElementsByTagName(b||"*"):a.querySelectorAll?a.querySelectorAll(b||"*"):[];return void 0===b||b&&_.nodeName(a,b)?_.merge([a],c):c}function s(a,b){var c=b.nodeName.toLowerCase();"input"===c&&ya.test(a.type)?b.checked=a.checked:("input"===c||"textarea"===c)&&(b.defaultValue=a.defaultValue)}function t(b,c){var d,e=_(c.createElement(b)).appendTo(c.body),f=a.getDefaultComputedStyle&&(d=a.getDefaultComputedStyle(e[0]))?d.display:_.css(e[0],"display");return e.detach(),f}function u(a){var b=Z,c=Oa[a];return c||(c=t(a,b),"none"!==c&&c||(Na=(Na||_("