Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/kanety/stimulus-tree


https://github.com/kanety/stimulus-tree

Last synced: 2 months ago
JSON representation

Awesome Lists containing this project

README

        

# stimulus-tree

A stimulus controller for simple tree ui.

## Dependencies

* @hotwired/stimulus 3.0+

## Installation

Install from npm:

$ npm install @kanety/stimulus-tree --save

## Usage

Register controller:

```javascript
import { Application } from '@hotwired/stimulus';
import TreeController from '@kanety/stimulus-tree';

const application = Application.start();
application.register('tree', TreeController);
```

Import css:

```css
@import '@kanety/stimulus-tree';
```

Build html as follows:

```html




  • text of 1



    • text of 1.1



      • text of 1.1.1



      • text of 1.1.2






```

### Options

#### store-key

Save node state to `sessionStorage`:

```html



```

### Callbacks

Set callbacks running when a node is opened or closed:

```javascript
let element = document.querySelector('[data-controller="tree"]');
element.addEventListener('tree:opened', (e) => {
console.log('opened: ' + e.detail.node.getAttribute('data-node-id'));
});
element.addEventListener('tree:closed', (e) => {
console.log('closed: ' + e.detail.node.getAttribute('data-node-id'));
});
```

### Event operations

Expand or collapse tree nodes:

```html



```

```javascript
let element = document.querySelector('[data-controller="tree"]');
element.dispatchEvent(new CustomEvent('tree:expand'));
element.dispatchEvent(new CustomEvent('tree:collapse'));
```

### Lazy load

Set URL for getting new HTML to each node using `data-node-lazy` attribute:

```html




  • text of 1


```

The new HTML should contains exapnded node like following example:

```html




  • text of 1



    • text of 1.1




```

## License

The library is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).