UNPKG

@syncfusion/ej2-grids

Version:

Feature-rich JavaScript datagrid (datatable) control with built-in support for editing, filtering, grouping, paging, sorting, and exporting to Excel.

1,036 lines 221 kB
import { Browser, EventHandler, createElement } from '@syncfusion/ej2-base'; import { isNullOrUndefined, isUndefined, addClass, removeClass } from '@syncfusion/ej2-base'; import { remove, closest, select } from '@syncfusion/ej2-base'; import { DataManager } from '@syncfusion/ej2-data'; import { setCssInGridPopUp, getPosition, isGroupAdaptive, addRemoveActiveClasses, removeAddCboxClasses, getCellFromRow } from '../base/util'; import { getCellsByTableName, parentsUntil, getScrollBarWidth } from '../base/util'; import * as events from '../base/constant'; import { RenderType } from '../base/enum'; import { iterateExtend, setChecked, isComplexField, getObject } from '../base/util'; import { addRemoveEventListener } from '../base/util'; import * as literals from '../base/string-literals'; /** * The `Selection` module is used to handle cell and row selection. */ var Selection = /** @class */ (function () { /** * Constructor for the Grid selection module * * @param {IGrid} parent - specifies the IGrid * @param {SelectionSettings} selectionSettings - specifies the selectionsettings * @param {ServiceLocator} locator - specifies the ServiceLocator * @hidden */ function Selection(parent, selectionSettings, locator) { //Internal letiables /** * @hidden */ this.selectedRowIndexes = []; /** * @hidden */ this.selectedRowCellIndexes = []; /** * @hidden */ this.selectedRecords = []; /** * @hidden */ this.preventFocus = false; /** * @hidden */ this.selectedColumnsIndexes = []; this.checkBoxState = false; this.isMultiShiftRequest = false; this.isMultiCtrlRequest = false; this.isMultiCtrlRequestCell = false; this.enableSelectMultiTouch = false; this.clearRowCheck = false; this.selectRowCheck = false; /** * @hidden */ this.selectedRowState = {}; this.unSelectedRowState = {}; this.totalRecordsCount = 0; this.chkAllCollec = []; this.isCheckedOnAdd = false; this.persistSelectedData = []; this.virtualSelectedData = []; this.deSelectedData = []; this.isHdrSelectAllClicked = false; this.needColumnSelection = false; this.isCancelDeSelect = false; this.isPreventCellSelect = false; this.disableUI = false; this.isPersisted = false; this.cmdKeyPressed = false; this.cellselected = false; this.isMultiSelection = false; this.isAddRowsToSelection = false; this.initialRowSelection = false; this.isPrevRowSelection = false; this.isKeyAction = false; this.isRowDragSelected = false; this.isPartialSelection = false; this.rmtHdrChkbxClicked = false; this.isCheckboxReset = false; this.isRowDeselect = false; /** * @hidden */ this.autoFillRLselection = true; this.isFocusLastCell = false; this.bottom = '0 0 2px 0'; this.top = '2px 0 0 0'; /* eslint-disable */ this.right_bottom = '0 2px 2px 0'; this.bottom_left = '0 0 2px 2px'; this.top_right = '2px 2px 0 0'; this.top_left = '2px 0 0 2px'; this.top_bottom = '2px 0 2px 0'; this.top_right_bottom = '2px 2px 2px 0'; this.top_bottom_left = '2px 0 2px 2px'; this.top_right_left = '2px 2px 0 2px'; this.right_bottom_left = '0 2px 2px 2px'; this.all_border = '2px'; this.parent = parent; this.selectionSettings = selectionSettings; this.factory = locator.getService('rendererFactory'); this.focus = locator.getService('focus'); this.addEventListener(); this.wireEvents(); } Selection.prototype.initializeSelection = function () { this.parent.log('selection_key_missing'); this.render(); }; /** * The function used to trigger onActionBegin * * @param {Object} args - specifies the args * @param {string} type - specifies the type * @returns {void} * @hidden */ Selection.prototype.onActionBegin = function (args, type) { this.parent.trigger(type, this.fDataUpdate(args)); }; Selection.prototype.fDataUpdate = function (args) { if (!this.isMultiSelection && (!isNullOrUndefined(args.cellIndex) || !isNullOrUndefined(args.rowIndex))) { var rowObj = this.getRowObj(isNullOrUndefined(args.rowIndex) ? isNullOrUndefined(args.cellIndex) ? this.currentIndex : args.cellIndex.rowIndex : args.rowIndex); args.foreignKeyData = rowObj.foreignKeyData; } return args; }; /** * The function used to trigger onActionComplete * * @param {Object} args - specifies the args * @param {string} type - specifies the type * @returns {void} * @hidden */ Selection.prototype.onActionComplete = function (args, type) { this.parent.trigger(type, this.fDataUpdate(args)); this.isMultiSelection = false; }; /** * For internal use only - Get the module name. * * @returns {string} returns the module name * @private */ Selection.prototype.getModuleName = function () { return 'selection'; }; /** * To destroy the selection * * @returns {void} * @hidden */ Selection.prototype.destroy = function () { var gridElement = this.parent.element; if (!gridElement || (!gridElement.querySelector('.' + literals.gridHeader) && !gridElement.querySelector('.' + literals.gridContent))) { return; } this.hidePopUp(); this.clearSelection(); this.destroyAutoFillElements(); this.removeEventListener(); this.unWireEvents(); EventHandler.remove(this.parent.getContent(), 'mousedown', this.mouseDownHandler); EventHandler.remove(this.parent.getHeaderContent(), 'mousedown', this.mouseDownHandler); }; Selection.prototype.isEditing = function () { return (this.parent.editSettings.mode === 'Normal' || (this.parent.editSettings.mode === 'Batch' && this.parent.editModule && this.parent.editModule.formObj && !this.parent.editModule.formObj.validate())) && (this.parent.isEdit && !this.parent.editSettings.showAddNewRow) && !this.parent.isPersistSelection; }; Selection.prototype.getCurrentBatchRecordChanges = function () { var gObj = this.parent; if (gObj.editSettings.mode === 'Batch' && gObj.editModule) { var currentRecords = iterateExtend(this.parent.getCurrentViewRecords()); currentRecords = gObj.editSettings.newRowPosition === 'Bottom' ? currentRecords.concat(this.parent.editModule.getBatchChanges()[literals.addedRecords]) : this.parent.editModule.getBatchChanges()[literals.addedRecords].concat(currentRecords); var deletedRecords = this.parent.editModule.getBatchChanges()[literals.deletedRecords]; var primaryKey = this.parent.getPrimaryKeyFieldNames()[0]; for (var i = 0; i < (deletedRecords.length); i++) { for (var j = 0; j < currentRecords.length; j++) { if (deletedRecords[parseInt(i.toString(), 10)]["" + primaryKey] === currentRecords[parseInt(j.toString(), 10)]["" + primaryKey]) { currentRecords.splice(j, 1); break; } } } return currentRecords; } else if (this.parent.enableVirtualization && this.parent.groupSettings.columns.length && !this.parent.isPersistSelection) { var selectedGroupedData = gObj.getCurrentViewRecords().filter(function (col) { return col['key'] === undefined; }); return selectedGroupedData; } else { return gObj.getCurrentViewRecords(); } }; /** * Selects a row by the given index. * * @param {number} index - Defines the row index. * @param {boolean} isToggle - If set to true, then it toggles the selection. * @returns {void} */ Selection.prototype.selectRow = function (index, isToggle) { if (this.selectedRowIndexes.length && this.selectionSettings.enableSimpleMultiRowSelection) { this.addRowsToSelection([index]); return; } var gObj = this.parent; var selectedRow = gObj.getRowByIndex(index); var rowObj = selectedRow && gObj.getRowObjectFromUID(selectedRow.getAttribute('data-uid')); if (this.isPartialSelection && rowObj && rowObj.isDataRow && !rowObj.isSelectable) { return; } var selectData; var isRemoved = false; if (gObj.enableVirtualization && index > -1) { var e = { selectedIndex: index, isAvailable: true }; this.parent.notify(events.selectVirtualRow, e); if (selectedRow && (gObj.getRowObjectFromUID(selectedRow.getAttribute('data-uid')))) { selectData = gObj.getRowObjectFromUID(selectedRow.getAttribute('data-uid')).data; } else { if (e.isAvailable && !gObj.selectionSettings.persistSelection) { var prevSelectedData = this.parent.getSelectedRecords(); if (prevSelectedData.length > 0) { this.clearRowSelection(); } } return; } } else { selectData = this.getRowObj(index).data; } if (!this.isRowType() || !selectedRow || this.isEditing()) { // if (this.isEditing()) { // gObj.selectedRowIndex = index; // } return; } var isRowSelected = selectedRow.hasAttribute('aria-selected'); this.activeTarget(); if (!isToggle || !this.selectedRowIndexes.length) { isToggle = false; } else { var isCheckboxModeResetOnRowClick = this.selectionSettings.checkboxMode === 'ResetOnRowClick'; var isSelectionTypeMultiple = !this.parent.isCheckBoxSelection && this.selectionSettings.type === 'Multiple'; if ((!isCheckboxModeResetOnRowClick && !isSelectionTypeMultiple) || (this.selectedRowIndexes.length === 1 && (isCheckboxModeResetOnRowClick || isSelectionTypeMultiple))) { isToggle = !(this.isKeyAction && this.parent.isCheckBoxSelection) ? this.selectedRowIndexes.indexOf(index) !== -1 : false; } else { isToggle = false; } } this.isKeyAction = false; var args; var can = 'cancel'; if (!isToggle) { args = { data: selectData, rowIndex: index, isCtrlPressed: this.isMultiCtrlRequest, isShiftPressed: this.isMultiShiftRequest, row: selectedRow, previousRow: gObj.getRowByIndex(this.prevRowIndex), previousRowIndex: this.prevRowIndex, target: this.actualTarget, cancel: false, isInteracted: this.isInteracted, isHeaderCheckboxClicked: this.isHeaderCheckboxClicked }; this.parent.trigger(events.rowSelecting, this.fDataUpdate(args), this.rowSelectingCallBack(args, isToggle, index, selectData, isRemoved, isRowSelected, can)); } else { if (this.selectionSettings.checkboxMode !== 'ResetOnRowClick' && this.selectionSettings.persistSelection) { this.rowDeselect(events.rowDeselecting, [rowObj.index], [rowObj.data], [selectedRow], [rowObj.foreignKeyData], this.actualTarget); if (this.isCancelDeSelect) { return; } if (!this.parent.isCheckBoxSelection) { this.selectedRowIndexes = []; this.selectedRecords = []; this.isRowSelected = false; this.selectRowIndex(-1); } this.rowDeselect(events.rowDeselected, [rowObj.index], [rowObj.data], [selectedRow], [rowObj.foreignKeyData], this.actualTarget, undefined, undefined, undefined); } this.rowSelectingCallBack(args, isToggle, index, selectData, isRemoved, isRowSelected, can)(args); } }; Selection.prototype.rowSelectingCallBack = function (args, isToggle, index, selectData, isRemoved, isRowSelected, can) { var _this = this; return function (args) { if (!isNullOrUndefined(args) && args["" + can] === true) { _this.disableInteracted(); return; } _this.index = index; _this.toggle = isToggle; _this.data = selectData; _this.removed = isRemoved; if (isRowSelected && _this.selectionSettings.persistSelection && !(_this.selectionSettings.checkboxMode === 'ResetOnRowClick')) { _this.clearSelectedRow(index); _this.selectRowCallBack(); } else if (!isRowSelected && _this.selectionSettings.persistSelection && _this.parent.isCheckBoxSelection && _this.selectionSettings.checkboxMode !== 'ResetOnRowClick') { _this.selectRowCallBack(); } if (_this.selectionSettings.checkboxMode === 'ResetOnRowClick') { _this.isCheckboxReset = true; _this.clearSelection(); } if (!_this.selectionSettings.persistSelection || _this.selectionSettings.checkboxMode === 'ResetOnRowClick' || (!_this.parent.isCheckBoxSelection && _this.selectionSettings.persistSelection)) { _this.selectRowCheck = true; _this.clearRow(); } }; }; Selection.prototype.selectRowCallBack = function () { var gObj = this.parent; var args; var index = this.index; var isToggle = this.toggle; var selectData = this.data; var isRemoved = this.removed; var selectedRow = gObj.getRowByIndex(index); if (!isToggle && !isRemoved) { if (this.selectedRowIndexes.indexOf(index) <= -1) { this.updateRowSelection(selectedRow, index); } this.selectRowIndex(index); } if (!isToggle) { args = { data: selectData, rowIndex: index, row: selectedRow, previousRow: gObj.getRowByIndex(this.prevRowIndex), previousRowIndex: this.prevRowIndex, target: this.actualTarget, isInteracted: this.isInteracted, isHeaderCheckBoxClicked: this.isHeaderCheckboxClicked, rowIndexes: index }; this.onActionComplete(args, events.rowSelected); } this.isInteracted = false; this.updateRowProps(index); }; /** * Selects a range of rows from start and end row indexes. * * @param {number} startIndex - Specifies the start row index. * @param {number} endIndex - Specifies the end row index. * @returns {void} */ Selection.prototype.selectRowsByRange = function (startIndex, endIndex) { this.selectRows(this.getCollectionFromIndexes(startIndex, endIndex)); this.selectRowIndex(endIndex); }; Selection.prototype.selectedDataUpdate = function (selectedData, foreignKeyData, selectedRows, rowIndexes, selectableRowIndex) { for (var i = 0, len = rowIndexes.length; i < len; i++) { var currentRow = this.parent.getDataRows()[rowIndexes[parseInt(i.toString(), 10)]]; if (this.parent.enableVirtualization) { currentRow = this.parent.getRowByIndex(rowIndexes[parseInt(i.toString(), 10)]); } var rowObj = this.getRowObj(currentRow); if (rowObj && rowObj.isDataRow && rowObj.isSelectable) { selectedData.push(rowObj.data); selectedRows.push(currentRow); foreignKeyData.push(rowObj.foreignKeyData); } else { if (this.isPartialSelection && selectableRowIndex) { selectableRowIndex.splice(selectableRowIndex.indexOf(rowIndexes[parseInt(i.toString(), 10)]), 1); } } } }; /** * Selects a collection of rows by index. * * @param {number[]} rowIndexes - Specifies an array of row indexes. * @returns {void} */ Selection.prototype.selectRows = function (rowIndexes) { var _this = this; var gObj = this.parent; var selectableRowIndex = rowIndexes.slice(); var rowIndex = !this.isSingleSel() ? rowIndexes[0] : rowIndexes[rowIndexes.length - 1]; this.isMultiSelection = true; var selectedRows = []; var foreignKeyData = []; var can = 'cancel'; var selectedData = []; if (!this.isRowType() || this.isEditing()) { return; } this.selectedDataUpdate(selectedData, foreignKeyData, selectedRows, rowIndexes, selectableRowIndex); this.activeTarget(); var args = { cancel: false, rowIndexes: selectableRowIndex, row: selectedRows, rowIndex: rowIndex, target: this.actualTarget, prevRow: gObj.getRows()[this.prevRowIndex], previousRowIndex: this.prevRowIndex, isInteracted: this.isInteracted, isCtrlPressed: this.isMultiCtrlRequest, isShiftPressed: this.isMultiShiftRequest, data: selectedData, isHeaderCheckboxClicked: this.isHeaderCheckboxClicked, foreignKeyData: foreignKeyData }; this.parent.trigger(events.rowSelecting, this.fDataUpdate(args), function (args) { if (!isNullOrUndefined(args) && args["" + can] === true) { _this.disableInteracted(); return; } if (!(_this.selectionSettings.persistSelection && gObj.allowRowDragAndDrop && _this.isDragged)) { _this.clearRow(); } _this.selectRowIndex(selectableRowIndex.slice(-1)[0]); var selectRowFn = function (index, preventFocus) { _this.updateRowSelection(gObj.getRowByIndex(index), index, preventFocus); _this.updateRowProps(rowIndex); }; if (!_this.isSingleSel()) { for (var _i = 0, selectableRowIndex_1 = selectableRowIndex; _i < selectableRowIndex_1.length; _i++) { var rowIdx = selectableRowIndex_1[_i]; selectRowFn(rowIdx, gObj.enableVirtualization ? true : false); } } else { selectRowFn(rowIndex); } if (_this.checkVirtualCheckBox() && !_this.parent.isPersistSelection) { if (selectableRowIndex.length === _this.totalRecordsCount) { _this.virtualSelectedData = _this.virtualCheckBoxData().slice(); _this.selectedRowIndexes = Object.keys(_this.virtualSelectedData).map(function (key) { return parseInt(key, 10); }); _this.setCheckAllState(); } else { var selectionData = selectableRowIndex.filter(function (index) { return _this.selectedRowIndexes.indexOf(index) === -1; }); if (selectionData.length > 0) { var allData = _this.virtualCheckBoxData().slice(); for (var i = 0; i < selectionData.length; i++) { var record = allData[selectionData[i]]; if (!isNullOrUndefined(record)) { _this.virtualSelectedData.push(record); _this.selectedRowIndexes.push(selectionData[i]); } } _this.setCheckAllState(); } } } args = { rowIndexes: selectableRowIndex, row: selectedRows, rowIndex: rowIndex, target: _this.actualTarget, prevRow: gObj.getRows()[_this.prevRowIndex], previousRowIndex: _this.prevRowIndex, data: _this.getSelectedRecords(), isInteracted: _this.isInteracted, isHeaderCheckboxClicked: _this.isHeaderCheckboxClicked, foreignKeyData: foreignKeyData }; if (_this.isRowSelected) { _this.onActionComplete(args, events.rowSelected); } _this.isInteracted = false; }); }; /** * Select rows with existing row selection by passing row indexes. * * @param {number} rowIndexes - Specifies the row indexes. * @returns {void} * @hidden */ Selection.prototype.addRowsToSelection = function (rowIndexes) { var gObj = this.parent; var can = 'cancel'; var target = this.target; this.isMultiSelection = true; var selectedRows = []; var foreignKeyData = []; var selectedData = []; var indexes = gObj.getSelectedRowIndexes().concat(rowIndexes); var selectedRow = !this.isSingleSel() ? gObj.getRowByIndex(rowIndexes[0]) : gObj.getRowByIndex(rowIndexes[rowIndexes.length - 1]); if ((!this.isRowType() || this.isEditing()) && !this.selectionSettings.checkboxOnly) { return; } var args; var checkboxColumn = this.parent.getColumns().filter(function (col) { return col.type === 'checkbox'; }); if (this.isMultiCtrlRequest && !checkboxColumn.length) { this.selectedDataUpdate(selectedData, foreignKeyData, selectedRows, indexes); } for (var _i = 0, rowIndexes_1 = rowIndexes; _i < rowIndexes_1.length; _i++) { var rowIndex = rowIndexes_1[_i]; var rowObj = this.getRowObj(rowIndex); var isUnSelected = this.selectedRowIndexes.indexOf(rowIndex) > -1; if (this.isPartialSelection && rowObj && rowObj.isDataRow && !rowObj.isSelectable) { continue; } this.selectRowIndex(rowIndex); if (isUnSelected && ((checkboxColumn.length ? true : this.selectionSettings.enableToggle) || this.isMultiCtrlRequest)) { this.isAddRowsToSelection = true; this.rowDeselect(events.rowDeselecting, [rowIndex], [rowObj.data], [selectedRow], [rowObj.foreignKeyData], target); if (this.isCancelDeSelect) { return; } if (this.checkVirtualCheckBox() && !this.parent.isPersistSelection) { this.virtualSelectedData.splice(this.virtualSelectedData.indexOf(rowObj.data), 1); } this.selectedRowIndexes.splice(this.selectedRowIndexes.indexOf(rowIndex), 1); this.selectedRecords.splice(this.selectedRecords.indexOf(selectedRow), 1); this.selectRowIndex(this.selectedRowIndexes.length ? this.selectedRowIndexes[this.selectedRowIndexes.length - 1] : -1); selectedRow.removeAttribute('aria-selected'); this.addRemoveClassesForRow(selectedRow, false, null, 'e-selectionbackground', 'e-active'); this.rowDeselect(events.rowDeselected, [rowIndex], [rowObj.data], [selectedRow], [rowObj.foreignKeyData], target, undefined, undefined, undefined); this.isInteracted = false; this.isMultiSelection = false; this.isAddRowsToSelection = false; this.isHdrSelectAllClicked = false; } else { this.activeTarget(); args = { cancel: false, data: selectedData.length ? selectedData : rowObj.data, rowIndex: rowIndex, row: selectedRows.length ? selectedRows : selectedRow, target: this.actualTarget, prevRow: gObj.getRows()[this.prevRowIndex], previousRowIndex: this.prevRowIndex, isCtrlPressed: this.isMultiCtrlRequest, isShiftPressed: this.isMultiShiftRequest, foreignKeyData: foreignKeyData.length ? foreignKeyData : rowObj.foreignKeyData, isInteracted: this.isInteracted, isHeaderCheckboxClicked: this.isHeaderCheckboxClicked, rowIndexes: indexes }; this.parent.trigger(events.rowSelecting, this.fDataUpdate(args)); if (!isNullOrUndefined(args) && args["" + can] === true) { this.disableInteracted(); return; } if (this.isSingleSel()) { this.clearRow(); } this.updateRowSelection(selectedRow, rowIndex); } if (!isUnSelected) { args = { data: selectedData.length ? selectedData : rowObj.data, rowIndex: rowIndex, row: selectedRows.length ? selectedRows : selectedRow, target: this.actualTarget, prevRow: gObj.getRows()[this.prevRowIndex], previousRowIndex: this.prevRowIndex, foreignKeyData: foreignKeyData.length ? foreignKeyData : rowObj.foreignKeyData, isInteracted: this.isInteracted, isHeaderCheckboxClicked: this.isHeaderCheckboxClicked, rowIndexes: indexes }; this.onActionComplete(args, events.rowSelected); } this.isInteracted = false; this.updateRowProps(rowIndex); if (this.isSingleSel()) { break; } } }; Selection.prototype.getCollectionFromIndexes = function (startIndex, endIndex) { var indexes = []; // eslint-disable-next-line prefer-const var _a = (startIndex <= endIndex) ? { i: startIndex, max: endIndex } : { i: endIndex, max: startIndex }, i = _a.i, max = _a.max; for (; i <= max; i++) { indexes.push(i); } if (startIndex > endIndex) { indexes.reverse(); } return indexes; }; Selection.prototype.clearRow = function () { this.clearRowCheck = true; this.clearRowSelection(); }; Selection.prototype.clearRowCallBack = function () { if (this.isCancelDeSelect && this.parent.checkAllRows !== 'Check') { return; } this.selectedRowIndexes = []; this.selectedRecords = []; this.selectRowIndex(-1); if (this.isSingleSel() && this.parent.isPersistSelection) { this.selectedRowState = {}; } }; Selection.prototype.clearSelectedRow = function (index) { if (this.toggle) { var selectedEle = this.parent.getRowByIndex(index); if (!this.disableUI) { selectedEle.removeAttribute('aria-selected'); this.addRemoveClassesForRow(selectedEle, false, true, 'e-selectionbackground', 'e-active'); } this.removed = true; this.updatePersistCollection(selectedEle, false); this.updateCheckBoxes(selectedEle); this.selectedRowIndexes.splice(this.selectedRowIndexes.indexOf(index), 1); this.selectedRecords.splice(this.selectedRecords.indexOf(this.parent.getRowByIndex(index)), 1); } }; Selection.prototype.updateRowProps = function (startIndex) { this.prevRowIndex = startIndex; this.isRowSelected = this.selectedRowIndexes.length && true; }; Selection.prototype.getPkValue = function (pkField, data) { return pkField ? isComplexField(pkField) ? getObject(pkField, data) : data["" + pkField] : data["" + pkField]; }; Selection.prototype.updatePersistCollection = function (selectedRow, chkState) { var _this = this; if ((this.parent.isPersistSelection || this.parent.selectionSettings.persistSelection && this.parent.getPrimaryKeyFieldNames().length > 0) && !isNullOrUndefined(selectedRow)) { if (!this.parent.isPersistSelection) { this.ensureCheckboxFieldSelection(); } var rowObj = this.getRowObj(selectedRow); var pKey_1 = rowObj.data ? this.getPkValue(this.primaryKey, rowObj.data) : null; if (pKey_1 === null) { return; } rowObj.isSelected = chkState; if ((chkState && !this.isPartialSelection) || (this.isPartialSelection && rowObj.isSelectable && rowObj.isSelected)) { this.selectedRowState["" + pKey_1] = chkState; delete (this.unSelectedRowState["" + pKey_1]); if (!this.persistSelectedData.some(function (data) { return _this.getPkValue(_this.primaryKey, data) === pKey_1; })) { this.persistSelectedData.push(rowObj.data); } } else { this.updatePersistDelete(pKey_1); } } }; Selection.prototype.updatePersistDelete = function (pKey, isPartialSelection) { var _this = this; delete (this.selectedRowState["" + pKey]); if (this.rmtHdrChkbxClicked) { this.unSelectedRowState["" + pKey] = true; } var index; var isPresent = this.persistSelectedData.some(function (data, i) { index = i; return _this.getPkValue(_this.primaryKey, data) === pKey; }); if (isPresent) { this.persistSelectedData.splice(index, 1); if (isPartialSelection) { this.parent.partialSelectedRecords.splice(index, 1); } } }; Selection.prototype.updateCheckBoxes = function (row, chkState, rowIndex) { if (!isNullOrUndefined(row)) { var chkBox = row.querySelector('.e-checkselect'); if (!isNullOrUndefined(chkBox)) { removeAddCboxClasses(chkBox.nextElementSibling, chkState); setChecked(chkBox, chkState); if (isNullOrUndefined(this.checkedTarget) || (!isNullOrUndefined(this.checkedTarget) && !this.checkedTarget.classList.contains('e-checkselectall'))) { this.setCheckAllState(rowIndex); } } } }; Selection.prototype.updateRowSelection = function (selectedRow, startIndex, preventFocus) { if (!selectedRow) { return; } if (this.selectedRowIndexes.indexOf(startIndex) === -1) { if (this.checkVirtualCheckBox() && !this.parent.isPersistSelection) { this.virtualSelectedData.push(this.parent.getRowObjectFromUID(selectedRow.getAttribute('data-uid')).data); } this.selectedRowIndexes.push(startIndex); this.selectedRecords.push(selectedRow); } selectedRow.setAttribute('aria-selected', 'true'); this.updatePersistCollection(selectedRow, true); this.updateCheckBoxes(selectedRow, true); this.addRemoveClassesForRow(selectedRow, true, null, 'e-selectionbackground', 'e-active'); if (!this.preventFocus && !(this.parent.isFocusFirstCell || this.isFocusLastCell)) { var target = this.focus.getPrevIndexes().cellIndex ? selectedRow.cells[this.focus.getPrevIndexes().cellIndex] : selectedRow.querySelector('.e-selectionbackground:not(.e-hide):not(.e-detailrowcollapse):not(.e-detailrowexpand)'); if (this.parent.contextMenuModule && this.mouseButton === 2) { target = this.parent.contextMenuModule.cell; } if (!target || preventFocus) { return; } this.focus.onClick({ target: target }, true); } else { if (this.parent.isFocusFirstCell || this.isFocusLastCell) { this.parent.isFocusFirstCell = false; var selector = this.isFocusLastCell ? 'last-child' : 'first-child'; var target = selectedRow.querySelector('.e-selectionbackground.e-rowcell:not(.e-hide, .e-detailrowcollapse, .e-detailrowexpand, .e-rowdragdrop, .e-gridchkbox):' + selector); this.isFocusLastCell = false; if (target) { this.focus.onClick({ target: target }, true, true); } } } }; /** * Deselects the currently selected rows and cells. * * @returns {void} */ Selection.prototype.clearSelection = function () { this.checkSelectAllClicked = true; if (this.selectionSettings.persistSelection && this.persistSelectedData.length) { this.deSelectedData = iterateExtend(this.persistSelectedData); } if (!this.parent.isPersistSelection || (this.parent.isPersistSelection && !this.parent.isEdit) || (!isNullOrUndefined(this.checkedTarget) && this.checkedTarget.classList.contains('e-checkselectall'))) { var span = this.parent.element.querySelector('.e-gridpopup').querySelector('span'); if (span.classList.contains('e-rowselect')) { span.classList.remove('e-spanclicked'); } if (this.parent.isPersistSelection) { this.persistSelectedData = []; this.selectedRowState = {}; } this.clearRowSelection(); this.clearCellSelection(); this.clearColumnSelection(); this.prevRowIndex = undefined; this.prevCIdxs = undefined; this.prevECIdxs = undefined; this.enableSelectMultiTouch = false; this.isInteracted = false; this.checkSelectAllClicked = false; this.isHdrSelectAllClicked = false; } }; /** * Deselects the currently selected rows. * * @returns {void} */ Selection.prototype.clearRowSelection = function () { var _this = this; if (this.isRowSelected) { var rows_1 = this.parent.getDataRows(); var data_1 = []; var row_1 = []; var rowIndex_1 = []; var foreignKeyData_1 = []; var target_1 = this.target; this.isRowDeselect = true; for (var i = 0, len = this.selectedRowIndexes.length; i < len; i++) { var currentRow = void 0; if (this.parent.enableVirtualization || (this.parent.enableInfiniteScrolling && this.parent.infiniteScrollSettings.enableCache)) { currentRow = this.parent.getRowByIndex(this.selectedRowIndexes[parseInt(i.toString(), 10)]); } else { currentRow = this.parent.editSettings.mode === 'Batch' ? this.parent.getRows()[this.selectedRowIndexes[parseInt(i.toString(), 10)]] : this.parent.getDataRows()[this.selectedRowIndexes[parseInt(i.toString(), 10)]]; } var rowObj = this.getRowObj(currentRow); if (rowObj) { data_1.push(rowObj.data); row_1.push(currentRow); rowIndex_1.push(this.selectedRowIndexes[parseInt(i.toString(), 10)]); foreignKeyData_1.push(rowObj.foreignKeyData); } } if (this.selectionSettings.persistSelection && this.selectionSettings.checkboxMode !== 'ResetOnRowClick') { this.isRowClicked = this.checkSelectAllClicked ? true : false; } this.rowDeselect(events.rowDeselecting, rowIndex_1, data_1, row_1, foreignKeyData_1, target_1, null, function () { if (_this.isCancelDeSelect && (_this.isRowClicked || _this.checkSelectAllClicked || (_this.isInteracted && !_this.parent.isPersistSelection))) { if (_this.parent.isPersistSelection) { if (_this.getCheckAllStatus(_this.parent.element.querySelector('.e-checkselectall')) === 'Intermediate' || _this.parent.isPersistSelection) { for (var i = 0; i < _this.selectedRecords.length; i++) { _this.updatePersistCollection(_this.selectedRecords[parseInt(i.toString(), 10)], true); } } else { _this.parent.checkAllRows = 'Check'; _this.updatePersistSelectedData(true); } } if (_this.clearRowCheck) { _this.clearRowCallBack(); _this.clearRowCheck = false; if (_this.selectRowCheck) { _this.selectRowCallBack(); _this.selectRowCheck = false; } } return; } var element = [].slice.call(rows_1.filter(function (record) { return record.hasAttribute('aria-selected'); })); for (var j = 0; j < element.length; j++) { if (!_this.disableUI) { element[parseInt(j.toString(), 10)].removeAttribute('aria-selected'); _this.addRemoveClassesForRow(element[parseInt(j.toString(), 10)], false, true, 'e-selectionbackground', 'e-active'); } // tslint:disable-next-line:align if (!_this.isPrevRowSelection) { _this.updatePersistCollection(element[parseInt(j.toString(), 10)], false); } _this.updateCheckBoxes(element[parseInt(j.toString(), 10)]); } if ((_this.parent.enableVirtualization || (_this.parent.enableInfiniteScrolling && _this.parent.infiniteScrollSettings.enableCache)) && _this.selectedRecords.length && !element.length && !_this.parent.selectionSettings.persistSelection && !_this.disableUI && !_this.parent.isCheckBoxSelection) { _this.addRemoveClassesForRow(null, false, true); } _this.virtualSelectedData = []; _this.selectedRowIndexes = []; _this.selectedRecords = []; _this.isRowSelected = false; _this.selectRowIndex(-1); _this.isPrevRowSelection = false; _this.rowDeselect(events.rowDeselected, rowIndex_1, data_1, row_1, foreignKeyData_1, target_1, null, undefined, null); if (_this.clearRowCheck) { _this.clearRowCallBack(); _this.clearRowCheck = false; if (_this.selectRowCheck) { _this.selectRowCallBack(); _this.selectRowCheck = false; } } }, null); this.isRowDeselect = false; } else { if (this.clearRowCheck) { this.clearRowCallBack(); this.clearRowCheck = false; if (this.selectRowCheck) { this.selectRowCallBack(); this.selectRowCheck = false; } } } }; Selection.prototype.rowDeselect = function (type, rowIndex, data, row, // eslint-disable-next-line @typescript-eslint/no-unused-vars foreignKeyData, target, mRow, rowDeselectCallBack, frozenRightRow) { var _this = this; if ((this.selectionSettings.persistSelection && (this.isRowClicked || !this.isRowDeselect || this.checkSelectAllClicked || (this.focus['activeKey'] && this.focus.currentInfo.element.classList.contains('e-gridchkbox') && this.focus['activeKey'] === 'space'))) || !this.selectionSettings.persistSelection) { var cancl_1 = 'cancel'; var isSingleDeSel = rowIndex.length === 1 && this.deSelectedData.length === 1; var rowDeselectObj = { rowIndex: rowIndex[0], data: this.selectionSettings.persistSelection && (this.parent.checkAllRows === 'Uncheck' && !isSingleDeSel) && this.selectionSettings.checkboxMode !== 'ResetOnRowClick' ? this.deSelectedData : data, foreignKeyData: foreignKeyData, cancel: false, isInteracted: this.isInteracted, isHeaderCheckboxClicked: this.isHeaderCheckboxClicked }; if (type === 'rowDeselected') { delete rowDeselectObj.cancel; } var rowInString = 'row'; var target_2 = 'target'; var rowidx = 'rowIndex'; var rowidxex = 'rowIndexes'; var dataTxt = 'data'; var foreignKey = 'foreignKeyData'; rowDeselectObj["" + rowInString] = row; rowDeselectObj["" + target_2] = this.actualTarget; var isHeaderCheckBxClick = this.actualTarget && !isNullOrUndefined(closest(this.actualTarget, 'thead')); if (isHeaderCheckBxClick || rowIndex.length > 1) { rowDeselectObj["" + rowidx] = rowIndex[0]; rowDeselectObj["" + rowidxex] = rowIndex; } else if (rowIndex.length === 1) { rowDeselectObj["" + dataTxt] = rowDeselectObj["" + dataTxt][0]; rowDeselectObj["" + rowInString] = rowDeselectObj["" + rowInString][0]; rowDeselectObj["" + foreignKey] = rowDeselectObj["" + foreignKey][0]; if (this.isAddRowsToSelection) { rowDeselectObj["" + rowidxex] = rowIndex; } } this.parent.trigger(type, rowDeselectObj, function (args) { _this.isCancelDeSelect = args["" + cancl_1]; if (!_this.isCancelDeSelect || (!_this.isRowClicked && !_this.isInteracted && !_this.checkSelectAllClicked)) { _this.updatePersistCollection(row[0], false); _this.updateCheckBoxes(row[0], undefined, rowIndex[0]); } if (rowDeselectCallBack !== undefined) { rowDeselectCallBack(); } }); } else if (this.selectionSettings.persistSelection && !this.isInteracted) { if (rowDeselectCallBack !== undefined) { rowDeselectCallBack(); } } }; Selection.prototype.getRowObj = function (row) { if (row === void 0) { row = this.currentIndex; } if (isNullOrUndefined(row)) { return {}; } if (typeof row === 'number') { row = this.parent.getRowByIndex(row); } if (row) { return this.parent.getRowObjectFromUID(row.getAttribute('data-uid')) || {}; } return {}; }; /** * Selects a cell by the given index. * * @param {IIndex} cellIndex - Defines the row and column indexes. * @param {boolean} isToggle - If set to true, then it toggles the selection. * @returns {void} */ Selection.prototype.selectCell = function (cellIndex, isToggle) { if (!this.isCellType() || (this.isCellType() && (this.parent.enableVirtualization || (this.parent.enableInfiniteScrolling && this.parent.infiniteScrollSettings.enableCache)))) { return; } var gObj = this.parent; var args; var selectedCell = gObj.getCellFromIndex(cellIndex.rowIndex, this.getColIndex(cellIndex.rowIndex, cellIndex.cellIndex)); this.currentIndex = cellIndex.rowIndex; var selectedData = this.getCurrentBatchRecordChanges()[this.currentIndex]; if (!this.isCellType() || !selectedCell || this.isEditing()) { return; } var isCellSelected = selectedCell.classList.contains('e-cellselectionbackground'); isToggle = !isToggle ? isToggle : (!isUndefined(this.prevCIdxs) && cellIndex.rowIndex === this.prevCIdxs.rowIndex && cellIndex.cellIndex === this.prevCIdxs.cellIndex && isCellSelected); if (!isToggle) { args = { data: selectedData, cellIndex: cellIndex, isCtrlPressed: this.isMultiCtrlRequest, isShiftPressed: this.isMultiShiftRequest, previousRowCell: this.prevECIdxs ? this.getCellIndex(this.prevECIdxs.rowIndex, this.prevECIdxs.cellIndex) : undefined, cancel: false }; var currentCell = 'currentCell'; args["" + currentCell] = selectedCell; var previousRowCellIndex = 'previousRowCellIndex'; args["" + previousRowCellIndex] = this.prevECIdxs; this.parent.trigger(events.cellSelecting, this.fDataUpdate(args), this.successCallBack(args, isToggle, cellIndex, selectedCell, selectedData)); this.cellselected = true; } else { this.successCallBack(args, isToggle, cellIndex, selectedCell, selectedData)(args); } }; Selection.prototype.successCallBack = function (cellSelectingArgs, isToggle, cellIndex, selectedCell, selectedData) { var _this = this; return function (cellSelectingArgs) { var cncl = 'cancel'; var currentCell = 'currentCell'; if (!isNullOrUndefined(cellSelectingArgs) && cellSelectingArgs["" + cncl] === true) { return; } if (!isToggle) { cellSelectingArgs["" + currentCell] = cellSelectingArgs["" + currentCell] ? cellSelectingArgs["" + currentCell] : selectedCell; } _this.clearCell(); if (!isToggle) { _this.updateCellSelection(selectedCell, cellIndex.rowIndex, cellIndex.cellIndex); } if (!isToggle) { var args = { data: selectedData, cellIndex: cellIndex, currentCell: selectedCell, selectedRowCellIndex: _this.selectedRowCellIndexes, previousRowCell: _this.prevECIdxs ? _this.getCellIndex(_this.prevECIdxs.rowIndex, _this.prevECIdxs.cellIndex) : undefined }; var previousRowCellIndex = 'previousRowCellIndex'; args["" + previousRowCellIndex] = _this.prevECIdxs; _this.updateCellProps(cellIndex, cellIndex); _this.onActionComplete(args, events.cellSelected); } }; }; Selection.prototype.getCellIndex = function (rIdx, cIdx) { return this.parent.getCellFromIndex(rIdx, cIdx); }; /** * Selects a range of cells from start and end indexes. * * @param {IIndex} startIndex - Specifies the row and column's start index. * @param {IIndex} endIndex - Specifies the row and column's end index. * @returns {void} */ Selection.prototype.selectCellsByRange = function (startIndex, endIndex) { var _this = this; if (!this.isCellType()) { return; } var gObj = this.parent; var selectedCell = this.parent.isSpan ? getCellFromRow(this.parent, startIndex.rowIndex, startIndex.cellIndex) : gObj.getCellFromIndex(startIndex.rowIndex, startIndex.cellIndex); var min; var max; var stIndex = startIndex; var edIndex = endIndex = endIndex ? endIndex : startIndex; var cellIndexes; this.currentIndex = startIndex.rowIndex; var cncl = 'cancel'; var selectedData = this.getCurrentBatchRecordChanges()[this.currentIndex]; if (this.isSingleSel() || !this.isCellType() || this.isEditing()) { return; } var args = { data: selectedData, cellIndex: startIndex, currentCell: selectedCell, isCtrlPressed: this.isMultiCtrlRequest, isShiftPressed: this.isMultiShiftRequest, previousRowCell: this.prevECIdxs ? this.getCellIndex(this.prevECIdxs.rowIndex, this.prevECIdxs.cellIndex) : undefined }; var previousRowCellIndex = 'previousRowCellIndex'; args["" + previousRowCellIndex] = this.prevECIdxs; this.parent.trigger(events.cellSelecting, this.fDataUpdate(args), function (cellSelectingArgs) { if (!isNullOrUndefined(cellSelectingArgs) && cellSelectingArgs["" + cncl] === true) { return; } _this.clearCell(); if (startIndex.rowIndex > endIndex.rowIndex) { var temp = startIndex; startIndex = endIndex; endIndex = temp; } for (var i = startIndex.rowIndex; i <= endIndex.rowIndex; i++) { if (_this.selectionSettings.cellSelectionMode.indexOf('Box') < 0) { min = i === startIndex.rowIndex ? (startIndex.cellIndex) : 0; max = i === endIndex.rowIndex ? (endIndex.cellIndex) : _this.getLastColIndex(i); } else { min = startIndex.cellIndex; max = endIndex.cellIndex; } cellIndexes = []; for (var j = min < max ? min : max, len = min > max ? min : max; j <= len; j++) { selectedCell = _this.parent.isSpan ? getCellFromRow(gObj, i, j) : gObj.getCellFromIndex(i,