|
| 1 | +import { Component, OnInit, ViewEncapsulation, } from '@angular/core'; |
| 2 | +import { |
| 3 | + AngularGridInstance, |
| 4 | + Column, |
| 5 | + Editors, |
| 6 | + Formatters, |
| 7 | + GridOption, |
| 8 | + SlickGlobalEditorLock, |
| 9 | +} from '../modules/angular-slickgrid'; |
| 10 | + |
| 11 | +@Component({ |
| 12 | + templateUrl: './grid-drag-recycle.component.html', |
| 13 | + styleUrls: ['./grid-drag-recycle.component.scss'], |
| 14 | + encapsulation: ViewEncapsulation.None, |
| 15 | +}) |
| 16 | +export class GridDragRecycleComponent implements OnInit { |
| 17 | + angularGrid!: AngularGridInstance; |
| 18 | + gridOptions!: GridOption; |
| 19 | + columnDefinitions!: Column[]; |
| 20 | + dataset!: any[]; |
| 21 | + dragHelper?: HTMLElement; |
| 22 | + dragRows: number[] = []; |
| 23 | + dragMode = ''; |
| 24 | + |
| 25 | + ngOnInit(): void { |
| 26 | + this.defineGrids(); |
| 27 | + |
| 28 | + // mock a dataset |
| 29 | + this.dataset = this.mockData(); |
| 30 | + } |
| 31 | + |
| 32 | + angularGridReady(angularGrid: AngularGridInstance) { |
| 33 | + this.angularGrid = angularGrid; |
| 34 | + } |
| 35 | + |
| 36 | + isBrowserDarkModeEnabled() { |
| 37 | + return window.matchMedia?.('(prefers-color-scheme: dark)').matches ?? false; |
| 38 | + } |
| 39 | + |
| 40 | + /* Define grid Options and Columns */ |
| 41 | + defineGrids() { |
| 42 | + this.columnDefinitions = [ |
| 43 | + { |
| 44 | + id: 'name', |
| 45 | + name: 'Name', |
| 46 | + field: 'name', |
| 47 | + width: 300, |
| 48 | + cssClass: 'cell-title', |
| 49 | + editor: { model: Editors.Text, }, |
| 50 | + validator: this.requiredFieldValidator |
| 51 | + }, |
| 52 | + { |
| 53 | + id: 'complete', |
| 54 | + name: 'Complete', |
| 55 | + width: 60, |
| 56 | + cssClass: 'cell-effort-driven', |
| 57 | + field: 'complete', |
| 58 | + cannotTriggerInsert: true, |
| 59 | + formatter: Formatters.checkmarkMaterial, |
| 60 | + editor: { model: Editors.Checkbox }, |
| 61 | + } |
| 62 | + ]; |
| 63 | + |
| 64 | + this.gridOptions = { |
| 65 | + enableAutoResize: false, |
| 66 | + gridHeight: 225, |
| 67 | + gridWidth: 800, |
| 68 | + rowHeight: 33, |
| 69 | + enableCellNavigation: true, |
| 70 | + enableRowSelection: true, |
| 71 | + enableRowMoveManager: true, |
| 72 | + rowSelectionOptions: { |
| 73 | + // True (Single Selection), False (Multiple Selections) |
| 74 | + selectActiveRow: false |
| 75 | + }, |
| 76 | + rowMoveManager: { |
| 77 | + columnIndexPosition: 0, |
| 78 | + cancelEditOnDrag: true, |
| 79 | + disableRowSelection: true, |
| 80 | + hideRowMoveShadow: false, |
| 81 | + onBeforeMoveRows: this.onBeforeMoveRows.bind(this), |
| 82 | + onMoveRows: this.onMoveRows.bind(this), |
| 83 | + |
| 84 | + // you can also override the usability of the rows, for example make every 2nd row the only moveable rows, |
| 85 | + // usabilityOverride: (row, dataContext, grid) => dataContext.id % 2 === 1 |
| 86 | + }, |
| 87 | + }; |
| 88 | + } |
| 89 | + |
| 90 | + mockData() { |
| 91 | + return [ |
| 92 | + { id: 0, name: 'Make a list', complete: true }, |
| 93 | + { id: 1, name: 'Check it twice', complete: false }, |
| 94 | + { id: 2, name: `Find out who's naughty`, complete: false }, |
| 95 | + { id: 3, name: `Find out who's nice`, complete: false } |
| 96 | + ]; |
| 97 | + } |
| 98 | + |
| 99 | + onBeforeMoveRows(e: MouseEvent | TouchEvent, data: { rows: number[]; insertBefore: number; }) { |
| 100 | + for (const dataRow of data.rows) { |
| 101 | + // no point in moving before or after itself |
| 102 | + if (dataRow === data.insertBefore || dataRow === data.insertBefore - 1) { |
| 103 | + e.stopPropagation(); |
| 104 | + return false; |
| 105 | + } |
| 106 | + } |
| 107 | + return true; |
| 108 | + } |
| 109 | + |
| 110 | + onMoveRows(_e: MouseEvent | TouchEvent, args: { rows: number[]; insertBefore: number; }) { |
| 111 | + const extractedRows: any[] = []; |
| 112 | + const rows = args.rows; |
| 113 | + const insertBefore = args.insertBefore; |
| 114 | + const left = this.dataset.slice(0, insertBefore); |
| 115 | + const right = this.dataset.slice(insertBefore, this.dataset.length); |
| 116 | + |
| 117 | + rows.sort((a, b) => a - b); |
| 118 | + |
| 119 | + for (const row of rows) { |
| 120 | + extractedRows.push(this.dataset[row]); |
| 121 | + } |
| 122 | + |
| 123 | + rows.reverse(); |
| 124 | + |
| 125 | + for (const row of rows) { |
| 126 | + if (row < insertBefore) { |
| 127 | + left.splice(row, 1); |
| 128 | + } else { |
| 129 | + right.splice(row - insertBefore, 1); |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + this.dataset = left.concat(extractedRows.concat(right)); |
| 134 | + |
| 135 | + const selectedRows: number[] = []; |
| 136 | + for (let i = 0; i < rows.length; i++) { |
| 137 | + selectedRows.push(left.length + i); |
| 138 | + } |
| 139 | + |
| 140 | + this.angularGrid.slickGrid?.resetActiveCell(); |
| 141 | + this.dataset = [...this.dataset]; |
| 142 | + } |
| 143 | + |
| 144 | + handleOnDragInit(e: CustomEvent) { |
| 145 | + // prevent the grid from cancelling drag'n'drop by default |
| 146 | + e.stopImmediatePropagation(); |
| 147 | + } |
| 148 | + |
| 149 | + handleOnDragStart(e: CustomEvent) { |
| 150 | + const cell = this.angularGrid.slickGrid?.getCellFromEvent(e); |
| 151 | + |
| 152 | + if (!cell || cell.cell === 0) { |
| 153 | + this.dragMode = ''; |
| 154 | + return; |
| 155 | + } |
| 156 | + |
| 157 | + const row = cell.row; |
| 158 | + if (!this.dataset[row]) { |
| 159 | + return; |
| 160 | + } |
| 161 | + |
| 162 | + if (SlickGlobalEditorLock.isActive()) { |
| 163 | + return; |
| 164 | + } |
| 165 | + |
| 166 | + e.stopImmediatePropagation(); |
| 167 | + this.dragMode = 'recycle'; |
| 168 | + |
| 169 | + let selectedRows: number[] = this.angularGrid.slickGrid?.getSelectedRows() || []; |
| 170 | + |
| 171 | + if (!selectedRows.length || selectedRows.findIndex(row => row === row) === -1) { |
| 172 | + selectedRows = [row]; |
| 173 | + this.angularGrid.slickGrid?.setSelectedRows(selectedRows); |
| 174 | + } |
| 175 | + |
| 176 | + this.dragRows = selectedRows; |
| 177 | + const dragCount = selectedRows.length; |
| 178 | + |
| 179 | + const dragMsgElm = document.createElement('span'); |
| 180 | + dragMsgElm.className = 'drag-message'; |
| 181 | + dragMsgElm.textContent = `Drag to Recycle Bin to delete ${dragCount} selected row(s)`; |
| 182 | + this.dragHelper = dragMsgElm; |
| 183 | + document.body.appendChild(dragMsgElm); |
| 184 | + document.querySelector<HTMLDivElement>('#dropzone')?.classList.add('drag-dropzone'); |
| 185 | + |
| 186 | + return dragMsgElm; |
| 187 | + } |
| 188 | + |
| 189 | + handleOnDrag(e: MouseEvent, args: any) { |
| 190 | + if (this.dragMode !== 'recycle') { |
| 191 | + return; |
| 192 | + } |
| 193 | + if (this.dragHelper instanceof HTMLElement) { |
| 194 | + this.dragHelper.style.top = `${e.pageY + 5}px`; |
| 195 | + this.dragHelper.style.left = `${e.pageX + 5}px`; |
| 196 | + } |
| 197 | + |
| 198 | + // add/remove pink background color when hovering recycle bin |
| 199 | + const dropzoneElm = document.querySelector<HTMLDivElement>('#dropzone')!; |
| 200 | + if (args.target instanceof HTMLElement && (args.target.id === 'dropzone' || args.target === dropzoneElm)) { |
| 201 | + dropzoneElm.classList.add('drag-hover'); // OR: dd.target.style.background = 'pink'; |
| 202 | + } else { |
| 203 | + dropzoneElm.classList.remove('drag-hover'); |
| 204 | + } |
| 205 | + } |
| 206 | + |
| 207 | + handleOnDragEnd(e: CustomEvent, args: any) { |
| 208 | + if (this.dragMode != 'recycle') { |
| 209 | + return; |
| 210 | + } |
| 211 | + this.dragHelper?.remove(); |
| 212 | + document.querySelector<HTMLDivElement>('#dropzone')?.classList.remove('drag-dropzone', 'drag-hover'); |
| 213 | + |
| 214 | + if (this.dragMode != 'recycle' || args.target.id !== 'dropzone') { |
| 215 | + return; |
| 216 | + } |
| 217 | + |
| 218 | + // reaching here means that we'll remove the row that we started dragging from the dataset |
| 219 | + const rowsToDelete = this.dragRows.sort().reverse(); |
| 220 | + for (const rowToDelete of rowsToDelete) { |
| 221 | + this.dataset.splice(rowToDelete, 1); |
| 222 | + } |
| 223 | + this.angularGrid.slickGrid?.invalidate(); |
| 224 | + this.angularGrid.slickGrid?.setSelectedRows([]); |
| 225 | + this.dataset = [...this.dataset]; |
| 226 | + } |
| 227 | + |
| 228 | + requiredFieldValidator(value: any) { |
| 229 | + if (value == null || value == undefined || !value.length) { |
| 230 | + return { valid: false, msg: 'This is a required field' }; |
| 231 | + } else { |
| 232 | + return { valid: true, msg: null }; |
| 233 | + } |
| 234 | + } |
| 235 | +} |
0 commit comments