Skip to content

Commit

Permalink
feat: add right click selection
Browse files Browse the repository at this point in the history
  • Loading branch information
paodb committed Sep 11, 2023
1 parent ce46561 commit 4978bfe
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -226,4 +226,19 @@ public void removeThemeVariants(SelectionGridVariant... variants) {
getThemeNames().removeAll(Stream.of(variants)
.map(SelectionGridVariant::getVariantName).collect(Collectors.toList()));
}

/**
* Sets whether right click selection for rows is enabled or not.
* <ul>
* <li>if no rows selected, then right click on a row -> select the target row </li>
* <li>if already few rows selected, then right click on unselected row -> unselect already selected and select target row </li>
* <li>if already few rows selected, then right click on selected row -> keep all selected </li>
* </ul>
*
* @param enabled true to enable right click selection
*/
public void setRightClickSelection(boolean enabled) {
this.getElement().setProperty("rightClickEnabled", enabled);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,22 @@ export function _selectionGridSelectRowWithItem(e, item, index) {
}
}

export function _selectionGridRightClickSelectRow(e) {
const tr = e.composedPath().find((p) => p.nodeName === "TR");
if (tr && typeof tr.index != 'undefined') {
const item = tr._item;
const index = tr.index;

if (this.$server) {
if (this.selectedItems && this.selectedItems.some((i) => i.key === item.key)) {
// keep all selected
return;
} else {
this.$server.selectRangeOnly(index, index);
}
}
}
}

export function _getItemOverriden(index, el) {
if (index >= this._effectiveSize) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ import { GridPro } from '@vaadin/grid-pro/src/vaadin-grid-pro.js';
import {
_getItemOverriden,
_selectionGridSelectRow,
_selectionGridSelectRowWithItem
_selectionGridSelectRowWithItem,
_selectionGridRightClickSelectRow
} from './helpers';

class VcfSelectionGridPro extends ElementMixin(ThemableMixin(GridPro)) {
Expand All @@ -28,25 +29,37 @@ class VcfSelectionGridPro extends ElementMixin(ThemableMixin(GridPro)) {
this._getItemOverriden = _getItemOverriden.bind(this);
this._selectionGridSelectRow = _selectionGridSelectRow.bind(this);
this._selectionGridSelectRowWithItem = _selectionGridSelectRowWithItem.bind(this);
this._selectionGridRightClickSelectRow = _selectionGridRightClickSelectRow.bind(this);
}

static get properties() {
return {
rangeSelectRowFrom: {
type: Number,
value: -1
},
rightClickEnabled: {
type: Boolean,
value: false
}
};
}

ready() {
super.ready();
this._getItem = this._getItemOverriden;
this.$.scroller.addEventListener('contextmenu', this._onRightClick.bind(this));
}

connectedCallback() {
super.connectedCallback();
}

_onRightClick(e) {
if(this.rightClickEnabled){
e.preventDefault();
this._selectionGridRightClickSelectRow(e);
}
}

focusOnCell(rowNumber, cellNumber, nbOfCalls = 1) {
Expand Down

0 comments on commit 4978bfe

Please sign in to comment.