Skip to content

Commit

Permalink
Merge pull request #290 from offirgolan/select-on-click
Browse files Browse the repository at this point in the history
[FEATURE] Add selectOnClick option
  • Loading branch information
offirgolan authored Dec 16, 2016
2 parents 0862443 + dbfe7ba commit 7ec547b
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 22 deletions.
48 changes: 32 additions & 16 deletions addon/components/lt-body.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,17 @@ export default Component.extend({
*/
canSelect: true,

/**
* Select a row on click. If this is set to `false` and multiSelect is
* enabled, using click + `shift`, `cmd`, or `ctrl` will still work as
* intended, while clicking on the row will not set the row as selected.
*
* @property selectOnClick
* @type {Boolean}
* @default true
*/
selectOnClick: true,

/**
* Allows for expanding row. This will create a new row under the row that was
* clicked with the template provided by `body.expanded-row`.
Expand Down Expand Up @@ -271,7 +282,6 @@ export default Component.extend({
columns: computed.readOnly('table.visibleColumns'),
colspan: computed.readOnly('columns.length'),

_currSelectedIndex: -1,
_prevSelectedIndex: -1,

init() {
Expand Down Expand Up @@ -357,10 +367,10 @@ export default Component.extend({
},

toggleExpandedRow(row) {
let multi = this.get('multiRowExpansion');
let multiRowExpansion = this.get('multiRowExpansion');
let shouldExpand = !row.expanded;

if (multi) {
if (multiRowExpansion) {
row.toggleProperty('expanded');
} else {
this.get('table.expandedRows').setEach('expanded', false);
Expand All @@ -380,32 +390,38 @@ export default Component.extend({
let multiSelect = this.get('multiSelect');
let multiSelectRequiresKeyboard = this.get('multiSelectRequiresKeyboard');
let canSelect = this.get('canSelect');
let selectOnClick = this.get('selectOnClick');
let canExpand = this.get('canExpand');
let expandOnClick = this.get('expandOnClick');
let isSelected = row.get('selected');
let currIndex = rows.indexOf(row);
let prevIndex = this._prevSelectedIndex === -1 ? currIndex : this._prevSelectedIndex;

this._currSelectedIndex = currIndex;
this._prevSelectedIndex = prevIndex;
this._prevSelectedIndex = currIndex;

let toggleExpandedRow = () => {
if (canExpand && expandOnClick) {
this.toggleExpandedRow(row);
}
};

if (canSelect) {
if (e.shiftKey && multiSelect) {
rows.slice(Math.min(currIndex, prevIndex), Math.max(currIndex, prevIndex) + 1).forEach((r) => r.set('selected', !isSelected));
this._prevSelectedIndex = currIndex;
rows
.slice(Math.min(currIndex, prevIndex), Math.max(currIndex, prevIndex) + 1)
.forEach((r) => r.set('selected', !isSelected));
} else if ((!multiSelectRequiresKeyboard || (e.ctrlKey || e.metaKey)) && multiSelect) {
row.toggleProperty('selected');
} else {
this.get('table.selectedRows').setEach('selected', false);
row.set('selected', !isSelected);

if (this.get('canExpand') && this.get('expandOnClick')) {
this.toggleExpandedRow(row);
if (selectOnClick) {
this.get('table.selectedRows').setEach('selected', false);
row.set('selected', !isSelected);
}

toggleExpandedRow();
}
this._prevSelectedIndex = currIndex;
} else {
if (this.get('canExpand') && this.get('expandOnClick')) {
this.toggleExpandedRow(row);
}
toggleExpandedRow();
}

this.sendAction('onRowClick', ...arguments);
Expand Down
14 changes: 8 additions & 6 deletions tests/dummy/app/templates/components/selectable-table.hbs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
{{!-- BEGIN-SNIPPET selectable-table --}}
{{#ember-wormhole to="table-actions"}}
{{#if hasSelection}}
<div class="table-action fa fa-check-square-o" title="Deselect all" {{action 'deselectAll'}}></div>
<div class="table-action fa fa-trash-o delete" title="Delete selected" {{action 'deleteAll'}}></div>
{{else}}
<div class="table-action fa fa-square-o" title="Select all" {{action 'selectAll'}}></div>
{{/if}}
<div>
{{#if hasSelection}}
<div class="table-action fa fa-check-square-o" title="Deselect all" {{action 'deselectAll'}}></div>
<div class="table-action fa fa-trash-o delete" title="Delete selected" {{action 'deleteAll'}}></div>
{{else}}
<div class="table-action fa fa-square-o" title="Select all" {{action 'selectAll'}}></div>
{{/if}}
</div>
{{/ember-wormhole}}

{{#light-table table height='65vh' as |t|}}
Expand Down

0 comments on commit 7ec547b

Please sign in to comment.