From 4978bfe4665d8b372f765381941d30dcb29ce535 Mon Sep 17 00:00:00 2001 From: Paola De Bartolo Date: Mon, 11 Sep 2023 16:24:21 -0300 Subject: [PATCH] feat: add right click selection --- .../selectiongridpro/SelectionGridPro.java | 15 +++++++++++++++ .../META-INF/resources/frontend/src/helpers.js | 16 ++++++++++++++++ .../frontend/src/vcf-selection-grid-pro.js | 15 ++++++++++++++- 3 files changed, 45 insertions(+), 1 deletion(-) diff --git a/selection-grid-pro-flow/src/main/java/com/vaadin/componentfactory/selectiongridpro/SelectionGridPro.java b/selection-grid-pro-flow/src/main/java/com/vaadin/componentfactory/selectiongridpro/SelectionGridPro.java index 6badd55..6222a9a 100644 --- a/selection-grid-pro-flow/src/main/java/com/vaadin/componentfactory/selectiongridpro/SelectionGridPro.java +++ b/selection-grid-pro-flow/src/main/java/com/vaadin/componentfactory/selectiongridpro/SelectionGridPro.java @@ -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. + * + * + * @param enabled true to enable right click selection + */ + public void setRightClickSelection(boolean enabled) { + this.getElement().setProperty("rightClickEnabled", enabled); + } + } diff --git a/selection-grid-pro-flow/src/main/resources/META-INF/resources/frontend/src/helpers.js b/selection-grid-pro-flow/src/main/resources/META-INF/resources/frontend/src/helpers.js index 3ac4b1c..044cdc1 100644 --- a/selection-grid-pro-flow/src/main/resources/META-INF/resources/frontend/src/helpers.js +++ b/selection-grid-pro-flow/src/main/resources/META-INF/resources/frontend/src/helpers.js @@ -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) { diff --git a/selection-grid-pro-flow/src/main/resources/META-INF/resources/frontend/src/vcf-selection-grid-pro.js b/selection-grid-pro-flow/src/main/resources/META-INF/resources/frontend/src/vcf-selection-grid-pro.js index 66ab3db..c27061e 100644 --- a/selection-grid-pro-flow/src/main/resources/META-INF/resources/frontend/src/vcf-selection-grid-pro.js +++ b/selection-grid-pro-flow/src/main/resources/META-INF/resources/frontend/src/vcf-selection-grid-pro.js @@ -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)) { @@ -28,6 +29,7 @@ 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() { @@ -35,6 +37,10 @@ class VcfSelectionGridPro extends ElementMixin(ThemableMixin(GridPro)) { rangeSelectRowFrom: { type: Number, value: -1 + }, + rightClickEnabled: { + type: Boolean, + value: false } }; } @@ -42,11 +48,18 @@ class VcfSelectionGridPro extends ElementMixin(ThemableMixin(GridPro)) { 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) {