Skip to content

Commit

Permalink
Merge pull request #23 from johanrd/master
Browse files Browse the repository at this point in the history
Add support for `contextMenu` event
  • Loading branch information
btecu authored Jun 27, 2019
2 parents dd36d06 + 70e92db commit ada6804
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 1 deletion.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,16 @@ Fired when a node is selected.
{{x-tree model=tree onSelect=(action 'onSelect')}}
```

#### onContextMenu

Returns: `node`

Fired on contextMenu event.

```handlebars
{{x-tree model=tree onContextMenu=(action 'onContextMenu')}}
```

### Available options

#### checkable
Expand Down
7 changes: 7 additions & 0 deletions addon/components/x-tree-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@ export default Component.extend({
return get(this, 'model.id') === this.chosenId;
}),

contextMenu(e) {
if (this.onContextMenu) {
e.preventDefault();
this.onContextMenu(this.model)
}
},

click() {
if (this.onSelect && !get(this, 'model.isDisabled')) {
let wasChecked = get(this, 'model.isChecked');
Expand Down
2 changes: 2 additions & 0 deletions addon/templates/components/x-tree-branch.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
chosenId=chosenId
onCheck=onCheck
onSelect=onSelect
onContextMenu=onContextMenu
onHover=onHover
onHoverOut=onHoverOut
model=child
Expand All @@ -26,6 +27,7 @@
chosenId=chosenId
onCheck=onCheck
onSelect=onSelect
onContextMenu=onContextMenu
onHover=onHover
onHoverOut=onHoverOut
expandedIcon=expandedIcon
Expand Down
4 changes: 4 additions & 0 deletions addon/templates/components/x-tree-children.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
chosenId=chosenId
onCheck=onCheck
onSelect=onSelect
onContextMenu=onContextMenu
onHover=onHover
onHoverOut=onHoverOut
model=model
Expand All @@ -24,6 +25,7 @@
chosenId=chosenId
onCheck=onCheck
onSelect=onSelect
onContextMenu=onContextMenu
onHover=onHover
onHoverOut=onHoverOut
model=model.children
Expand All @@ -43,6 +45,7 @@
chosenId=chosenId
onCheck=onCheck
onSelect=onSelect
onContextMenu=onContextMenu
onHover=onHover
onHoverOut=onHoverOut
expandedIcon=expandedIcon
Expand All @@ -57,6 +60,7 @@
chosenId=chosenId
onCheck=onCheck
onSelect=onSelect
onContextMenu=onContextMenu
onHover=onHover
onHoverOut=onHoverOut
expandedIcon=expandedIcon
Expand Down
2 changes: 2 additions & 0 deletions addon/templates/components/x-tree.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
chosenId=chosenId
onCheck=onCheck
onSelect=onSelect
onContextMenu=onContextMenu
onHover=onHover
onHoverOut=onHoverOut
model=model
Expand All @@ -22,6 +23,7 @@
chosenId=chosenId
onCheck=onCheck
onSelect=onSelect
onContextMenu=onContextMenu
onHover=onHover
onHoverOut=onHoverOut
expandedIcon=expandedIcon
Expand Down
16 changes: 15 additions & 1 deletion tests/integration/components/x-tree-test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { module, test } from 'qunit';
import { set } from '@ember/object';
import { setupRenderingTest } from 'ember-qunit';
import { render, find, findAll, click } from '@ember/test-helpers';
import { render, find, findAll, click, triggerEvent } from '@ember/test-helpers';
import hbs from 'htmlbars-inline-precompile';
import Component from '@ember/component';

Expand Down Expand Up @@ -241,4 +241,18 @@ module('Integration | Component | x-tree', function(hooks) {
await click('.tree-node span');
assert.equal(this.selected, false, 're-enabled tree nodes can be selected again');
});

test('contextmenu event', async function(assert) {
this.set('onContextMenu', (item) => {
this.name = item.name;
});
this.set('tree', standardTree);

await render(hbs`{{x-tree model=tree onContextMenu=onContextMenu}}`);
await triggerEvent('.tree-node span', 'contextmenu')

assert.equal(this.name, 'Root', 'item from contextmenu event is returned as expected');

});

});

0 comments on commit ada6804

Please sign in to comment.