Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 | 93x 93x 93x 93x 93x 127x 127x 98x 1x 1x 2x 1x 1x 98x 18x 18x 1x 1x 1x 1x 4x 4x 1x 1x 6x 6x 6x 9x 9x 9x 9x 5x 6x 3x 3x 4x 4x 4x 4x 9x 1x 4x 3x 14x 14x 14x 14x 3x 11x 14x 14x 13x 13x 14x 14x 23x 23x 23x 23x 14x 14x 14x 14x 14x 8x 8x 14x 14x 2x 9x 2x 2x 2x 2x 2x 1x 2x 1x 1x 2x 1x 2x 2x 1x 2x 2x 2x 2x 2x 2x 2x 2x 2x 4x 4x 4x 4x 4x 4x 12x 12x 12x 12x 46x 12x 9x 9x 1x 8x 9x | import { IDictionary, IEventParam, Loader, Utils } from '@zeedhi/core';
import { omit } from '../../utils';
import { Input } from '../zd-input/input';
import { Column } from '../zd-iterable/column';
import { DataEditor } from './data-editor';
import { GridEditableController } from './grid-editable-controller';
import { IDataEditorWithAdd, IGridColumnEditable } from './interfaces';
export class DataEditorWithAdd extends DataEditor implements IDataEditorWithAdd {
private NEW_ROW_IDENTIFIER = '__added_row';
/**
* Rows with newRowIdentifier
* @private
*/
private addedRows: Map<string | number, IDictionary> = new Map();
addCancelColumn(): void {
if (!this.grid.showCancelColumn) return;
const deleteColumn = this.grid.createColumn(this.getCancelColumnProps());
this.grid.columns.push(deleteColumn);
}
getCancelColumnProps(): IGridColumnEditable {
return {
name: `cancel_action_${this.grid.name}`,
type: 'action',
align: 'center',
actionFixed: true,
isVisible: `{{GridEditableController_${this.grid.name}.hasAddedRows}}`,
children: [
{
name: 'cancel_btn',
component: 'ZdButton',
icon: true,
iconName: 'clear',
small: true,
conditions: {
isVisible: `{{GridEditableController_${this.grid.name}.isAddedRow}}`,
},
events: {
click: ({ row }: IEventParam<Column>) => {
const rowKey = this.grid.getRowKey(row);
this.cancelAddedRow(rowKey);
},
},
},
],
};
}
createGridController(): void {
Loader.addController(`GridEditableController_${this.grid.name}`, GridEditableController, [this.grid]);
}
isAdded(row: IDictionary): boolean {
const rowKey = this.grid.getRowKey(row);
return !!this.addedRows.get(rowKey);
}
cancelAddedRow(key: string | number): Promise<any> {
this.editedRows.delete(key);
this.addedRows.delete(key);
const data = this.grid.datasource.getLoadedData();
const index = data.findIndex((row) => {
const rowKey = this.grid.getRowKey(row);
return rowKey === key;
});
data.splice(index, 1);
return this.grid.updateData(data);
}
async cancelAddedRows(): Promise<void> {
const loadedData = this.grid.datasource.getLoadedData();
Promise.resolve(loadedData);
for (let index = loadedData.length - 1; index >= 0; index -= 1) {
const row = loadedData[index];
const rowKey = this.grid.getRowKey(row);
const addedRow = this.addedRows.get(rowKey);
if (addedRow) {
loadedData.splice(index, 1);
}
}
await this.grid.updateData(loadedData);
}
getAddedRows(): IDictionary<any>[] {
const addedRows: IDictionary<any>[] = [];
this.addedRows.forEach((addedRow) => {
let row = { ...addedRow.originalRow, ...addedRow };
row = omit(row, 'originalRow');
row = omit(row, this.NEW_ROW_IDENTIFIER);
Object.keys(row).forEach((attr) => {
if (Object.hasOwn(row, `${attr}_original`)) {
delete row[`${attr}_original`];
}
});
addedRows.push(row);
});
return addedRows;
}
async addNewRow(row: IDictionary, position: 'end' | 'start' = 'end'): Promise<void> {
const newRow = { ...row };
const data: IDictionary<any>[] = this.grid.datasource.getLoadedData();
newRow[this.NEW_ROW_IDENTIFIER] = true;
if (position === 'start') {
data.unshift(newRow);
} else {
data.push(newRow);
}
const rowKey = this.grid.getRowKey(newRow);
if (rowKey) {
this.editedRows.set(rowKey, newRow);
this.addedRows.set(rowKey, newRow);
}
await this.grid.updateData(data);
this.grid.editing = true;
}
changeEditableComponent(column: IGridColumnEditable, row: IDictionary<any>, value: any, component?: Input): void {
const key = this.grid.getRowKey(row);
const columnName = column.name;
const editedRow = this.editedRows.get(key);
if (!Utils.isEqual(value, row[columnName])) {
const newRow: IDictionary = { ...editedRow };
newRow.originalRow = omit(row, 'originalRow');
newRow[columnName] = value;
newRow[`${columnName}_original`] = row[columnName];
if (component) {
column.checkOutValues(component, row, newRow);
component.value = value;
}
this.editedRows.set(key, newRow);
if (this.isAdded(row)) {
this.addedRows.set(key, newRow);
}
} else if (editedRow) {
let newRow = { ...editedRow };
newRow = omit(newRow, columnName);
newRow = omit(newRow, `${columnName}_original`);
this.editedRows.set(key, newRow);
if (this.isAdded(row)) {
this.addedRows.set(key, newRow);
}
if (component) {
column.checkOutValues(component, row, newRow);
component.value = value;
}
if (Object.keys(newRow).length === 1 && newRow.originalRow) {
this.editedRows.delete(key);
}
}
}
private addDataRow(row: IDictionary<any>) {
const key = this.grid.getRowKey(row);
if (this.addedRows.get(key)) this.grid.datasource.post(row);
else this.grid.datasource.put(row);
}
/**
* Saves all edited rows if they are valid
* @param revalidate Defines if the fields should be revalidated
* @throws Will throw when it finds an invalid row
*/
public async saveEditedRows(revalidate = false) {
const page = this.grid.getPage();
await this.cancelAddedRows();
const response = await Promise.all(this.getEditedRows(revalidate).map((row) => this.addDataRow(row)));
this.grid.editing = false;
this.editedRows.clear();
this.invalidComponents.clear();
this.addedRows.clear();
await this.grid.setPage(page);
return response;
}
/**
* Cancels all edited rows and enable grid components
*/
public async cancelEditedRows() {
const page = this.grid.getPage();
await this.cancelAddedRows();
await this.grid.setPage(page);
this.grid.editing = false;
this.editedRows.clear();
this.invalidComponents.clear();
}
protected omitInternals(editedRow: IDictionary) {
let row = { ...editedRow.originalRow, ...editedRow };
row = omit(row, 'originalRow');
row = omit(row, this.NEW_ROW_IDENTIFIER);
Object.keys(row).forEach((attr) => {
row = omit(row, `${attr}_original`);
});
return row;
}
reapplyCanEditRow(row: IDictionary<any>) {
let canEdit = true;
if (this.grid.editingNewRows) {
canEdit = !!row[this.NEW_ROW_IDENTIFIER];
} else {
canEdit =
!this.grid.canEditRow ||
typeof this.grid.canEditRow !== 'function' ||
this.grid.canEditRow({ row, component: this.grid });
}
this.canEditRowConditions.set(this.grid.getRowKey(row), canEdit);
}
}
|