1.1.0 • Published 3 years ago

ember-cli-jstree v1.1.0

Weekly downloads
330
License
MIT
Repository
github
Last release
3 years ago

ember-cli-jstree

Travis-CI status Ember Observer Score

Brings jsTree functionality into your Ember app.

Demo: http://ritesh83.github.io/ember-cli-jstree/#/static

Installation

Ember CLI addons can be installed with ember install

ember install ember-cli-jstree

Usage

Out of the box, the bare minimum you need on the template is data. Run supported actions on the tree by registering it to your controller with the actionReceiver property.

<div class="sample-tree">
    {{ember-jstree
        actionReceiver=jstreeActionReceiver
        selectedNodes=jstreeSelectedNodes
        data=data
        plugins=plugins
        themes=themes
        checkboxOptions=checkboxOptions
        contextmenuOptions=contextmenuOptions
        stateOptions=stateOptions
        typesOptions=typesOptions
        searchOptions=searchOptions
        searchTerm=searchTerm
        sort=sort
        contextMenuReportClicked=(action "contextMenuReportClicked")
        eventDidBecomeReady=(action "handleTreeDidBecomeReady")
    }}
</div>

Adding classes

As per the jsTree JSON docs, you can add custom classes to both the <li> and <a> tags of each individual node. These are passed on to jQuery's attr function.

For example, to add hint.css tooltips, use the following in your JSON data hash.

{
	'id': 'node15',
	'text': 'Node title',
	'state': { 'selected': true },
	'a_attr': { 'class': 'hint--bottom', 'data-hint': 'Some hint goes here' }
}

This will get rendered in HTML as

<a class="jstree-anchor jstree-clicked hint--bottom" href="#" tabindex="-1" data-hint="Some hint goes here" id="node15_anchor"><i class="jstree-icon jstree-themeicon" role="presentation"></i>Node title</a>

Event Handling

The addon listens for events from jstree and sends them back to you using actions bound to the Handlebars template. Simply set the property to the string name of the action in your controller.

{{ember-jstree
    [...]
    eventDidChange=(action "handleJstreeEventDidChange")
    treeObject=jstreeObject
}}

Supported events

The following events have basic support included. More are on the way.

jsTree EventEmber Action
after_open.jstreeeventDidOpen
after_close.jstreeeventDidClose
changed.jstreeeventDidChange
dehover_node.jstreeeventDidDehoverNode
deselect_node.jstreeeventDidDeselectNode
hover_node.jstreeeventDidHoverNode
init.jstreeeventDidInit
loading.jstreeeventIsLoading
loaded.jstreeeventDidLoad
ready.jstreeeventDidBecomeReady
redraw.jstreeeventDidRedraw
show_node.jstreeeventDidShowNode
select_node.jstreeeventDidSelectNode
(destroyed - no event)eventDidDestroy
move_node.jstreeeventDidMoveNode
disable_checkbox.jstreeeventDidDisableCheckbox
enable_checkbox.jstreeeventDidEnableCheckbox
check_node.jstreeeventDidCheckNode
uncheck_node.jstreeeventDidUncheckNode
check_all.jstreeeventDidCheckAll
uncheck_all.jstreeeventDidUncheckAll

Note: In the meantime, you can add event listeners yourself by calling them on a mapped treeObject property.

_handleOpenNode: function() {
    var treeObject = this.get('jstreeObject');
    treeObject.on('open_node.jstree', function(e, data) {
        console.info('A node was opened.');
        console.log(data);
    }.bind(this));
}

Selected nodes

Selected nodes are always available through the selectedNodes property

Plugins

Plugins for your tree should be specified by a plugins string property. Multiple plugins should be separated with commas.

{{ember-jstree
    data=data
    plugins=plugins
}}

The following plugins are currently supported. More on the way!

  • Checkbox
  • Contextmenu
  • Search
  • Sort
  • State
  • Types
  • Wholerow
  • Drag and Drop

Configuring plugins

Send a hash containing the jsTree options through to the addon through the <plugin name>Options key.

In your controller:

jstreeStateOptionHash: {
    'key': 'ember-cli-jstree-dummy'
},
plugins: 'state'

In Handlebars:

{{ember-jstree
    [...]
    plugins=plugins
    stateOptions=jstreeStateOptionHash
}}

The sort plugin accepts a sort function instead of an options hash.

{{ember-jstree
    [...]
    plugins=plugins
    sort=sortFunction
}}

Configuring tree refresh

Send in the following properties to control how the tree is refreshed when you change the data

  • skipLoading
  • forgetState

Both default to false if nothing is passed in

Sending actions to jsTree

The addon component will try to register an actionReceiver (see view helper example) to a property in your controller if you define it. You can then send actions through that bound property:

this.get('jstreeActionReceiver').send('redraw');

Note: Action names in Ember are camelized (e.g.: get_node() in jsTree is mapped to getNode() in Ember).

If the corresponding jsTree method has a return value, the addon will send an action with the name corresponding to supported actions in the table below. Because the addon actually calls these jsTree events, if any events occur because of an action, they will be sent as actions (see Event Handling above).

Supported actions

jsTree ActionEmber ActionReturn Action
copy_nodecopyNode
close_allcloseAll
close_nodecloseNode
create_nodecreateNodeactionCreateNode
delete_nodedeleteNodeactionDeleteNode
deselect_alldeselectAll
deselect_nodedeselectNode
destroydestroy
get_children_domgetChildrenDomactionGetChildrenDom
get_containergetContaineractionGetContainer
get_nodegetNodeactionGetNode
get_parentgetParentactionGetParent
get_pathgetPathactionGetPath
get_textgetTextactionGetText
last_errorlastErroractionLastError
load_allloadAllactionLoadAll
load_nodeloadNodeactionLoadNode
move_nodemoveNode
open_allopenAll
open_nodeopenNode
redrawredraw
rename_noderenameNodeactionRenameNode
select_allselectAll
select_nodeselectNode
toggle_nodetoggleNode

Receiving return values

In your Handlebars component, map the return action (as above, most of which follow the pattern action<action name>):

{{ember-jstree
    [...]
    actionGetNode=(action "handleJstreeGetNode")
}}

Any params that jsTree returns will be given in the order specified by its API.

actionGetNode: function (node) {
    this.set('someValue', node);
}

Demo

http://ritesh83.github.io/ember-cli-jstree/#/static

Both dynamic (AJAX loaded) and static examples are in the dummy demo.

  • Clone this repo: git clone
  • Install packages: yarn
  • Run ember serve
  • Visit the sample app at http://localhost:4200.

Guides

http://ridhwana.com/articles/2017/05/28/introduction-to-ember-cli-js-tree.html

1.1.0

3 years ago

1.0.12

6 years ago

1.0.11

6 years ago

1.0.9

7 years ago

1.0.8

7 years ago

1.0.7

8 years ago

1.0.6

8 years ago

1.0.5

8 years ago

1.0.4

8 years ago

1.0.3

8 years ago

1.0.2

8 years ago

1.0.1

8 years ago

1.0.0

8 years ago

0.1.2

9 years ago

0.1.1

9 years ago

0.1.0

9 years ago

0.0.18

9 years ago

0.0.17

9 years ago

0.0.16

9 years ago

0.0.15

9 years ago

0.0.14

9 years ago

0.0.13

9 years ago

0.0.12

9 years ago

0.0.11

9 years ago

0.0.10

9 years ago

0.0.9

9 years ago

0.0.8

9 years ago

0.0.7

9 years ago

0.0.6

9 years ago

0.0.5

9 years ago

0.0.4

9 years ago

0.0.3

9 years ago

0.0.2

9 years ago

0.0.1

9 years ago

0.0.0

9 years ago