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.

To initialize a Tree, the user has to include the following files to the head of the page:

  • smart.default.css - the CSS file containing the styles for the element
  • 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.

The following code adds Smart.Tree 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>

Demo

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

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

KeyAction
TabThe Tree receives focus by tabbing into it.
Arrow Up / Arrow DownNavigation between visible items.
Left ArrowOn an expanded item, collapses the item. On a collapsed or a "leaf" item moves focus to the item's parent item.
Right ArrowOn a collapsed item expands the item. On an expanded item, moves to the first child item, or does nothing on a "leaf" item.
HomeMoves to the top enabled item.
EndMoves to the last enabled item.
EnterExpands/collapses an item or selects a "leaf" item.
EscapeIf the menu is minimized, closes the minimized pop-up.
SpaceToggles an item's checked state.