Tree - HTML UI Elements for Mobile & Web Applications | www.HtmlElements.com

Overview

Smart.Tree represents an element that displays hierarchical data in a tree structure. The tree supports keyboard navigation, expand/collapse animations, kinetic scrolling, drag&drop, checkboxes mode.

Files

  • CSS files
    • smart.default.css - the CSS file containing the styles for the element.
  • Javascript module
    • smart.tree.js - the JS module which is in the source/modules/ folder and loads all script dependencies.
  • Javascript files
    • smart.element.js - the base class
    • smart.button.js - this file contains the smart-repeat-button custom element. The scroll buttons of Smart.Tree are instances of smart-repeat-button
    • smart.menu.js - this file defines the main functionality of the Smart.Menu custom element, used as base element for the tree
    • smart.scrollbar.js - defines scrollbar functionality
    • smart.tree.js - this file defines the main functionality of the Smart.Tree custom element.

    Usage

    • Import a module

      With this approach, we import a module and create the web component by using the Smart function. The #tree is a smart-tree tag.

      import {smartTree} from "../../source/modules/smart.tree.js";
      
      Smart('#tree', class {
      get properties() {
      	return {
      		filterable: true
      	}
      }
      });
      
      document.readyState === 'complete' ? init() : window.onload = init;
      
      function init() { 
      
      }
      
      Using the Smart function is optional. You can use const tree = document.querySelector("#tree"); and set the properties like that:
      const tree = document.querySelector("#tree");
      
      tree.filterable = true;
      

    • Import a module, Init on Demand

      The following imports the web component's module and creates it on demand, when the document is ready. The #tree is a DIV tag.

      import {smartTree} from "../../source/modules/smart.tree.js";
      
      document.readyState === 'complete' ? init() : window.onload = init;
      
      function init() { 
      const tree = new smartTree('#tree', {
         		filterable : true
      });
      }
      
    • Load scripts

      The following code adds the custom element to the page.

      <!DOCTYPE html>
      <html lang="en">
      <head>
       <link rel="stylesheet" href="../../source/styles/smart.default.css" type="text/css" />
       <script type="text/javascript" src="../../source/smart.element.js"></script>
       <script type="text/javascript" src="../../source/smart.button.js"></script>
       <script type="text/javascript" src="../../source/smart.menu.js"></script>
       <script type="text/javascript" src="../../source/smart.scrollbar.js"></script>
       <script type="text/javascript" src="../../source/smart.tree.js"></script>
      </head>
      <body>
          <smart-tree>
              <smart-tree-items-group>
                  <i class="material-icons"></i> Attractions
                  <smart-tree-item>Movies</smart-tree-item>
                  <smart-tree-item>Circus</smart-tree-item>
                  <smart-tree-item>Concerts</smart-tree-item>
                  <smart-tree-item>Monuments</smart-tree-item>
              </smart-tree-items-group>
                  <smart-tree-items-group>
                  <i class="material-icons"></i> Dining
                  <smart-tree-item>Restaurants</smart-tree-item>
                  <smart-tree-item>Cafés</smart-tree-item>
                  <smart-tree-item>Bars</smart-tree-item>
              </smart-tree-items-group>
          </smart-tree>
      </body>
      </html>

      Note how smart.element.js is declared before everything else. This is mandatory for all custom elements.

      Demo

Create, Append, Remove, Get/Set Property, Invoke Method, Bind to Event


Create a new element:
const tree = document.createElement('smart-tree');

Append it to the DOM:
document.body.appendChild(tree);

Remove it from the DOM:
tree.parentNode.removeChild(tree);

Set a property:
tree.propertyName = propertyValue;

Get a property value:
const propertyValue = tree.propertyName;

Invoke a method:
tree.methodName(argument1, argument2);

Add Event Listener:
const eventHandler = (event) => {
// your code here.
};

tree.addEventListener(eventName, eventHandler);

Remove Event Listener:
tree.removeEventListener(eventName, eventHandler, true);

Appearance

The tree structure contains smart-tree-item and smart-tree-items-group elements.

The following attributes are available for these custom elements.

  • label - the label to be displayed in the Tree
  • level - level of nesting
  • selected - selected state of the element
  • value - a custom value that is not displayed, but is passed as an argument to the itemClick event (called when an item is chosen).
  • separator - if present, a visual separator (horizontal line) is added after the item
  • disabled - disables the selection of an item

The following attribute is only available for smart-tree-item.

  • shortcut - a helper text/icon that can represent a keyboard shortcut that activates the item.

The following attribute is only available for smart-tree-items-group.

  • expanded - controls expand/collapse

Animated tree could be achieved when animation class is added .

 <smart-tree class="animation"></smart-tree>

By setting selectionMode property, smart-tree has several types of selection:

  • none
  • oneOrManyExtended
  • zeroOrMany
  • oneOrMany
  • zeroOrOne
  • one
  • checkBox
  • radioButton

In selectionMode: checkBox can be enabled "hasThreeStates". In this case sub-item selection affects the selection of parent items.

 <smart-tree selection-mode="checkBox"></smart-tree>

Demo

To display multi-level hierarchy, Smart.Tree uses connectiong lines, displaying the relations between tree items and groups. They are controlled by showLines property. Smart.Tree also supports guiding lines from each item to the root item. This feature is controlled by showRootLines property.

 <smart-tree show-lines show-root-lines></smart-tree>

Demo

Smart.Tree allows two scrolling modes - 'scrollbar' and 'scrollButtons'.
In the following example is shown the behavior of these two modes, in different oferflow settings.

 <smart-tree filterable scroll-mode="scrollbar"></smart-tree>
 <smart-tree filterable scroll-mode="scrollButtons"></smart-tree>

Demo

toggleElementPosition is used for positioning togle elements on left or right side. Allows two options - 'near' and 'far'.,

 <smart-tree toggle-element-position="far"></smart-tree>

Demo

Data Biding

To initialize a populated Smart.Tree custom element, insert the required inner structure consisting of the auxiliary custom elements smart-menu-item and smart-menu-items-group, e.g.:

<!DOCTYPE html>
<html lang="en">
<head>
 <link rel="stylesheet" href="../../source/styles/smart.default.css" type="text/css" />
 <script type="text/javascript" src="../../source/smart.element.js"></script>
 <script type="text/javascript" src="../../source/smart.button.js"></script>
 <script type="text/javascript" src="../../source/smart.menu.js"></script>
 <script type="text/javascript" src="../../source/smart.scrollbar.js"></script>
 <script type="text/javascript" src="../../source/smart.tree.js"></script>
</head>
<body>
 <smart-tree id="tree" class="animation">
    <smart-tree-items-group>
        Cats
        <smart-tree-item>Tiger</smart-tree-item>
        <smart-tree-item>Lion</smart-tree-item>
        <smart-tree-item>Jaguar</smart-tree-item>
        <smart-tree-item>Leopard</smart-tree-item>
        <smart-tree-item>Serval</smart-tree-item>
        <smart-tree-item>Domestic cat</smart-tree-item>
    </smart-tree-items-group>
    <smart-tree-items-group>
        Dogs
        <smart-tree-item>Gray wolf</smart-tree-item>
        <smart-tree-item>Ethiopian wolf</smart-tree-item>
        <smart-tree-item>Arctic fox</smart-tree-item>
        <smart-tree-item>Dingo</smart-tree-item>
        <smart-tree-item>Domestic dog</smart-tree-item>
  </smart-tree-items-group>
 </smart-tree>
</body>
</html>

Populating from dataSource.

<!DOCTYPE html>
<html lang="en">
<head>
 <link rel="stylesheet" href="../../source/styles/smart.default.css" type="text/css" />
 <script type="text/javascript" src="../../source/smart.element.js"></script>
 <script type="text/javascript" src="../../source/smart.button.js"></script>
 <script type="text/javascript" src="../../source/smart.menu.js"></script>
 <script type="text/javascript" src="../../source/smart.scrollbar.js"></script>
 <script type="text/javascript" src="../../source/smart.tree.js"></script>
 <script>
 window.onload = function () {
     document.querySelector('smart-tree').dataSource = [{
                label: 'Cats',
                selected: true,
                items: [
                    {
                        label: 'Tiger',
                        selected: true
                    },
                    {
                        label: 'Lion'
                    },
                    {
                        label: 'Jaguar'
                    },
                    {
                        label: 'Leopard'
                    },
                    {
                        label: 'Serval'
                    },
                    {
                        label: 'Domestic cat'
                    }
                ]
            },
                {
                    label: 'Dogs',
                    expanded: true,
                    items: [
                        {
                            label: 'Gray wolf'
                        },
                        {
                            label: 'Ethiopian wolf',
                            selected: true
                        },
                        {
                            label: 'Arctic fox',
                            selected: true
                        },
                        {
                            label: 'Dingo'
                        },
                        {
                            label: 'Domestic dog',
                            selected: true
                        }
                    ]
                }
            ];
     }
 </script>
</head>
<body>
    <smart-tree></smart-tree>
</body>
</html>

Demo

Behavior

Smart.Tree allows three optionsof user interaction used for expanding or collapsing item groups. They are available as settings of toggleMode property:

  • click - single click on group's label toggles the group.
  • dblclick - double click on group's label toggles the group(default setting).
  • arrow - clicking only on group's arrow toggles the group. Clicking group's label doesn't affect the state.
 <smart-tree toggle-mode="arrow"></smart-tree>

Demo

Tree items could be moved in tree's structure and/or between different tree instances. This behavior can be achieved via allowDrag and allowDrop properties.

  • allowDrag - items of a tree with allowDrag set to true can be dragged. When the dragginng starts is fired 'dragStart' event.
  • allowDrop - allows drop in the focussed tree of items that are its children or children of another tree. On drop is fired 'dragEnd' event.

event.preventDefault() of both events prevents drag/drop(depending of event type). It can be used to deny drag or drop functionality of particular Smart.Tree item.

 <smart-tree  class="animation" allow-drag allow-drop></smart-tree>
 <smart-tree  class="animation" allow-drag allow-drop></smart-tree>

Demo

Filtering feature is unlocked by setting filterable to true. This will show filter input. The element filters by particular criteria items in all levels and shows only these who matched the criteria. By setting filterInputPlaceholder to custom text, could be changed the placeholder of this element.
smart-tree allows few filtering options. They can be set by filterMode property:

  • contains
  • containsIgnoreCase
  • doesNotContain
  • doesNotContainIgnoreCase
  • equals
  • equalsIgnoreCase
  • startsWith
  • startsWithIgnoreCase
  • endsWith
  • endsWithIgnoreCase
 <smart-tree filterable filter-input-placeholder="Filter query..." filter-mode="containsIgnoreCase"></smart-tree>

Demo

Methods

The element offers the following methods:

  • addAfter - Adds an item after another item as a sibling.
     <script>
         window.onload = function () {
             let tree = document.querySelector('smart-tree'),
                newItem = document.createElement('smart-tree-item');
             newItem.innerHTML = '0';
             tree.addAfter(newItem, 'three');
         }
     </script>
    
  • addBefore - adds new item to the tree.
     <script>
         window.onload = function () {
             let tree = document.querySelector('smart-tree'),
                newItem = document.createElement('smart-tree-item');
             newItem.label = 'Y';
             tree.addBefore(newItem, 'zed');
         }
     </script>
    
  • addTo - Adds an item as the last child of a parent item.
     <script>
         window.onload = function () {
             let tree = document.querySelector('smart-tree'),
                newItem = document.createElement('smart-tree-item');
             newItem.label = 'D';
             tree.addTo(newItem, 'letters');
         }
     </script>
    
  • clearSelection - clearSelection
     <script>
         window.onload = function () {
             let tree = document.querySelector('smart-tree');
             tree.clearSelection();
         }
     </script>
    
  • collapseAll - Collapses all items.
     <script>
         window.onload = function () {
             let tree = document.querySelector('smart-tree');
             tree.collapseAll();
         }
     </script>
    
  • ensureVisible - Makes sure an item is visible by scrolling to it.
     <script>
         window.onload = function () {
             let tree = document.querySelector('smart-tree');
             tree.ensureVisible('0.0');
         }
     </script>
    
  • expandAll - Expands all items.
     <script>
         window.onload = function () {
             let tree = document.querySelector('smart-tree');
             tree.expandAll();
         }
     </script>
    
  • expandItem - Expands an item.
     <script>
         window.onload = function () {
             let tree = document.querySelector('smart-tree');
             tree.expandItem('0.0');
         }
     </script>
    
  • filter - Filters the Tree.
     <script>
         window.onload = function () {
             let tree = document.querySelector('smart-tree');
             tree.filter();
         }
     </script>
    
  • getState - Returns Smart.Tree's state
     <script>
         window.onload = function () {
             let tree = document.querySelector('smart-tree');
             tree.getState();
         }
     </script>
    
  • loadState - Loads the Tree's state.
     <script>
         window.onload = function () {
             let tree = document.querySelector('smart-tree');
             tree.loadState();
         }
     </script>
    
  • moveDown - Moves an item down relative to its siblings.
     <script>
         window.onload = function () {
             let tree = document.querySelector('smart-tree');
             tree.moveDown('0.0');
         }
     </script>
    
  • moveUp - Moves an item up relative to its siblings.
     <script>
         window.onload = function () {
             let tree = document.querySelector('smart-tree');
             tree.moveUp('0.0');
         }
     </script>
    
  • removeItem - Removes an item.
     <script>
         window.onload = function () {
             let tree = document.querySelector('smart-tree');
             tree.removeItem('0.0');
         }
     </script>
    
  • saveState - Saves the Tree's state.
     <script>
         window.onload = function () {
             let tree = document.querySelector('smart-tree');
             tree.saveState();
         }
     </script>
    
  • select - Selects an item.
     <script>
         window.onload = function () {
             let tree = document.querySelector('smart-tree');
             tree.select('0.0');
         }
     </script>
    
  • unselect - Unselects an item.
     <script>
         window.onload = function () {
             let tree = document.querySelector('smart-tree');
             tree.unselect('0.0');
         }
     </script>
    
  • updateItem - Updates an item.
     <script>
         window.onload = function () {
             let tree = document.querySelector('smart-tree');
             tree.updateItem('0.0', { disabled: false });
         }
     </script>
    

Keyboard Support

Key Action
Tab The Tree receives focus by tabbing into it.
Arrow Up / Arrow Down Navigation between visible items.
Left Arrow On an expanded item, collapses the item. On a collapsed or a "leaf" item moves focus to the item's parent item.
Right Arrow On a collapsed item expands the item. On an expanded item, moves to the first child item, or does nothing on a "leaf" item.
Home Moves to the top enabled item.
End Moves to the last enabled item.
Enter Expands/collapses an item or selects a "leaf" item.
Escape If the menu is minimized, closes the minimized pop-up.
Space Toggles an item's checked state.