├── LICENSE
├── README.md
├── demo
├── example.jpg
└── index.html
├── tree.js
├── tree.min.js
├── treejs.css
└── treejs.min.css
/LICENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2018 Matthias Thalmann
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # TreeJS
2 |
3 | TreeJS is a simple JavaScript library, to display a TreeView like in the windows filebrowser.
4 | It implements partially the Java Swing TreeModel etc.
5 |
6 | **Demo:** https://m-thalmann.github.io/treejs/demo/
7 |
8 | ## Navigation
9 | - [Installation](#installation)
10 | - [Usage](#usage)
11 | - [Documentation](#documentation)
12 | - [TreeView](#treeview)
13 | - [TreeNode](#treenode)
14 | - [TreePath](#treepath)
15 | - [TreeUtil](#treeutil)
16 | - [TreeConfig](#treeconfig)
17 | - [Events](#events)
18 | - [Options](#options)
19 | - [Example](#example)
20 |
21 | ## Installation
22 | 1. Download the .zip-File and put it in your project-folder.
23 |
24 | 2. Add this script-tag to the head of the file
25 | ```html
26 |
27 | ```
28 |
29 | 3. Add this link-tag to the head of the file, to include the styles
30 | ```html
31 |
32 | ```
33 |
34 | 4. Start using the library!
35 |
36 | ## Usage
37 | ### Create new TreeView
38 | ```javascript
39 | var root = new TreeNode("root"); // Create the root-node
40 | var tree = new TreeView(root); // Create the tree
41 | ```
42 |
43 | ### Set a container to display the tree
44 | ```javascript
45 | tree.setContainer("#container"); // Uses document.querySelector
46 | ```
47 | or
48 | ```javascript
49 | tree.setContainer(document.getElementById("container"));
50 | ```
51 |
52 | ### (Re-)load the tree
53 | ```javascript
54 | tree.reload(); // Always use this, when you change the TreeView or any of its nodes
55 | ```
56 | ## Documentation
57 | ### TreeView
58 | It's the main object to display the Tree.
59 | #### Instanciating
60 | ```javascript
61 | new TreeView(root, container, options);
62 | ```
63 | - **root** (TreeNode): The root-node of the tree.
64 | - **container** (DOM-Element/querySelector): The container for the tree to display **(optional)**
65 | - **options** (object): A object with options for the tree (see [below](#options)) **(optional)**
66 |
67 | After the instanciation, the TreeView is reloaded/rendered
68 |
69 | #### Methods
70 | ```javascript
71 | tree.setRoot(root); // Resets the root-node (TreeNode)
72 | tree.getRoot(); // Returns the root-node
73 |
74 | tree.expandAllNodes(); // Expands all nodes of the tree
75 | tree.expandPath(path); // Expands all nodes that are in the path (TreePath)
76 | tree.collapseAllNodes(); // Collapses all nodes of the tree
77 |
78 | tree.setContainer(container); // Resets the container (DOM-Element/querySelector)
79 | tree.getContainer(); // Returns the container
80 |
81 | tree.setOptions(options); // Resets the options (object)
82 | tree.changeOption(option, value); // Changes one option (string, object)
83 | tree.getOptions(); // Returns the options
84 |
85 | tree.getSelectedNodes(); // Returns all selected nodes in the tree
86 | tree.reload(); // Reloads/Renders the tree inside of the container
87 | ```
88 |
89 | ### TreeNode
90 | It represents a node inside of a tree. You can append children to it and specify a userobject, which is used to display text on a node. This object can be a string but can also be a other object, where the toString() function is used to display the text.
91 | #### Instanciating
92 | ```javascript
93 | new TreeNode(userobject, options);
94 | ```
95 |
96 | - **userobject** (object): This object is used to display text on the node (if not string, toString() is used) **(optional)**
97 | - **options** (object): A object with options for the node (see [below](#options)) **(optional)**
98 |
99 | #### Methods
100 | ```javascript
101 | node.addChild(node); // Adds a child to the current node and sets the parent of the node (TreeNode)
102 | node.removeChildPos(pos); // Removes the child at this position (integer)
103 | node.removeChild(node); // Removes the child from the current node, if contained (TreeNode)
104 | node.getChildren(); // Returns a array with the children of the current node
105 | node.getChildCount(); // Returns the number of children
106 | node.getIndexOfChild(node); // Returns the position of the child; -1 is returned if not found (TreeNode)
107 |
108 | node.getRoot(); // Tries to get the root node of this node
109 |
110 | node.setUserObject(userobject); // Resets the userobject (object)
111 | node.getUserObject(); // Returns the userobject
112 |
113 | node.setOptions(options); // Resets the options (object)
114 | node.changeOption(option, value); // Changes one option (string, object)
115 | node.getOptions(); // Returns the options
116 |
117 | node.isLeaf(); // Returns true, if the node doesn't have any children, else false
118 |
119 | node.setExpanded(true|false); // Sets the expanded-state of the node (if it shows its children) (boolean)
120 | node.toggleExpanded(); // Toggles the expanded-state of the node
121 | node.isExpanded(); // Returns, if the node is expanded or not
122 |
123 | node.setEnabled(true|false); // Sets the enabled-state of the node (if it is enabled) (boolean)
124 | node.toggleEnabled(); // Toggles the enabled-state of the node
125 | node.isEnabled(); // Returns, if the node is enabled or not
126 |
127 | node.setSelected(true|false); // Sets the selected-state of the node (if it is selected) (boolean)
128 | node.toggleSelected(); // Toggles the selected-state of the node
129 | node.isSelected(); // Returns, if the node is selected or not
130 |
131 | node.open(); // Triggers the "open"-event of the node
132 |
133 | node.on(event, callback); // Sets the eventlistener of the event, if the callback is specified;
134 | // if only the event is set, it returns the callback-function; if that is not
135 | // set, it returns a empty function (string, function)
136 | node.getListener(event); // Returns the callback-function for this event, if set (string)
137 |
138 | node.equals(node); // Returns if the node is equal to the parameter (TreeNode)
139 |
140 | node.toString(); // Returns the generated string from the userobject
141 | ```
142 |
143 | ### TreePath
144 | It represents a path inside of a tree (containing all nodes that form the path).
145 | #### Instanciating
146 | ```javascript
147 | new TreePath(root, node);
148 | ```
149 |
150 | - **root** & **node** (TreeNode): if they are both set, the method setPath(root, node) is executed **(optional)**
151 |
152 | #### Methods
153 | ```javascript
154 | path.setPath(root, node); // Generates the path between root and node (TreeNode, TreeNode)
155 | path.getPath(); // Returns the generated path as a array
156 |
157 | path.toString(); // Returns the path as a string (nodes joined with a ' - ')
158 | ```
159 |
160 | ### TreeUtil
161 | A collection of default values and methods. Can't be instanciated.
162 | #### Variables
163 | ```javascript
164 | TreeUtil.default_leaf_icon // String, that represents the default icon for a leaf-node
165 | TreeUtil.default_parent_icon // String, that represents the default icon for a normal node (with children)
166 | TreeUtil.default_open_icon // String, that represents the default expanded-icon
167 | TreeUtil.default_close_icon // String, that represents the default collapsed-icon
168 | ```
169 | #### Methods
170 | ```javascript
171 | TreeUtil.isDOM(object); // Returns true, if the object is a DOM-Element, else false (object)
172 |
173 | TreeUtil.getProperty(opt, o, def); // Returns the value of 'o' in the array/object opt, if it is set;
174 | // else it returns def (object, string, object)
175 | TreeUtil.expandNode(node); // Expands the node and all it's children and theirs etc. (TreeNode)
176 | TreeUtil.collapseNode(node); // Collapses the node and all it's children and theirs etc. (TreeNode)
177 |
178 | TreeUtil.getSelectedNodesForNode(n); // Returns all selected nodes inside of this node (and it's self,
179 | // if its selected) (TreeNode)
180 | ```
181 |
182 | ### TreeConfig
183 | A collection of values, that you can change (directly inside of the file or with JavaScript for only one site).
184 | #### Variables
185 | ```javascript
186 | TreeConfig.leaf_icon // The icon for a leaf-node (default: TreeUtil.default_leaf_icon)
187 | TreeConfig.parent_icon // The icon for a normal node (default: TreeUtil.default_parent_icon)
188 | TreeConfig.open_icon // The expanded-icon (default: TreeUtil.default_open_icon)
189 | TreeConfig.close_icon // The collapsed-icon (default: TreeUtil.default_close_icon)
190 | TreeConfig.context_menu // A function that is executed when a contextmenu is opened (default: undefined)
191 | ```
192 |
193 | ### Events
194 | It is possible to attach a event to a TreeNode: ``node.on(event, callback);``
195 |
196 | | Event | Callback-Parameter(s) | Definition | Restriction |
197 | |-----------------|--------------------------------------|--------------------------------------------------------------------------|---------------|
198 | | click | e[click_event], node[TreeNode] | Is triggered when the node is clicked | - |
199 | | expand | node[TreeNode] | Is triggered when the node is expanded | Not-leaf only |
200 | | collapse | node[TreeNode] | Is triggered when the node is collapsed | Not-leaf only |
201 | | toggle_expanded | node[TreeNode] | Is triggered when the node is either expanded or collapsed | Not-leaf only |
202 | | open | node[TreeNode] | Is triggered when the open()-Function is executed or the leaf is clicked | Leaf only |
203 | | enable | node[TreeNode] | Is triggered when the node is enabled | - |
204 | | disable | node[TreeNode] | Is triggered when the node is disabled | - |
205 | | toggle_enabled | node[TreeNode] | Is triggered when the node is either enabled or disabled | - |
206 | | select | node[TreeNode] | Is triggered when the node is selected | - |
207 | | deselect | node[TreeNode] | Is triggered when the node is deselected | - |
208 | | toggle_selected | node[TreeNode] | Is triggered when the node is either selected or deselected | - |
209 | | contextmenu | e[contextmenu_event], node[TreeNode] | Is triggered when a contextmenu is opened on a node | - |
210 |
211 | ### Options
212 | #### for TreeView
213 |
214 | | Option | Values | Definition |
215 | |-------------|----------|-----------------------------------------------------------------------------|
216 | | leaf_icon | [string] | Sets the leaf-icon for this tree to the string (can be overwritten by node) |
217 | | parent_icon | [string] | Sets the node-icon for this tree to the string (can be overwritten by node) |
218 | | show_root | [boolean] | Sets whether the root node is shown or not |
219 |
220 | #### for TreeNode
221 |
222 | | Option | Values | Definition |
223 | |----------------|------------|----------------------------------------------------------------|
224 | | expanded | [boolean] | On creation, the node will have the expanded value set to this |
225 | | enabled | [boolean] | On creation, the node will have the enabled value set to this |
226 | | selected | [boolean] | On creation, the node will have the selected value set to this |
227 | | icon | [string] | Sets the icon for this node to the string |
228 | | allowsChildren | [boolean] | Sets if there can be added new children to this node |
229 | | forceParent | [boolean] | This node will be displayed as parent, even if it is empty |
230 |
231 | ## Example
232 | ### Code:
233 | ```javascript
234 | var root = new TreeNode("root");
235 | var n1 = new TreeNode("1");
236 | var n11 = new TreeNode("1.1");
237 | var n2 = new TreeNode("2");
238 | var n3 = new TreeNode("3");
239 | var n31 = new TreeNode("3.1");
240 | var n32 = new TreeNode("3.2");
241 | var n321 = new TreeNode("3.2.1");
242 | var n33 = new TreeNode("3.3");
243 |
244 | root.addChild(n1);
245 | root.addChild(n2);
246 | root.addChild(n3);
247 |
248 | n1.addChild(n11);
249 |
250 | n3.addChild(n31);
251 | n3.addChild(n32);
252 | n3.addChild(n33);
253 |
254 | n32.addChild(n321);
255 |
256 | n3.setEnabled(false);
257 |
258 | var view = new TreeView(root, "#container");
259 | ```
260 |
261 | ### Output:
262 |
263 | 
264 |
--------------------------------------------------------------------------------
/demo/example.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/m-thalmann/treejs/1c8abf45078827eda2559c08582e2dd91905ce38/demo/example.jpg
--------------------------------------------------------------------------------
/demo/index.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |