-
Notifications
You must be signed in to change notification settings - Fork 155
/
Copy pathrow-model-generator.ts
154 lines (136 loc) · 6.22 KB
/
row-model-generator.ts
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
import { isNullOrUndefined, getValue, setValue } from '@syncfusion/ej2-base';
import { IModelGenerator, ICell, IRow, IGrid, InfiniteScrollArgs, SaveEventArgs } from '../base/interface';
import { Row } from '../models/row';
import { CellType, Action } from '../base/enum';
import { Column } from '../models/column';
import { Cell } from '../models/cell';
import { getUid } from '../base/util';
import { getForeignData } from '../../grid/base/util';
import * as events from '../base/constant';
/**
* RowModelGenerator is used to generate grid data rows.
*
* @hidden
*/
export class RowModelGenerator implements IModelGenerator<Column> {
//Module declarations
protected parent: IGrid;
/**
* Constructor for header renderer module
*
* @param {IGrid} parent - specifies the IGrid
*/
constructor(parent?: IGrid) {
this.parent = parent;
}
public generateRows(data: Object, args?: { startIndex?: number, requestType?: Action }): Row<Column>[] {
const rows: Row<Column>[] = [];
let startIndex: number = this.parent.enableVirtualization && args ? args.startIndex : 0;
startIndex = this.parent.enableInfiniteScrolling && args ? this.getInfiniteIndex(args) : startIndex;
for (let i: number = 0, len: number = Object.keys(data).length; i < len; i++ , startIndex++) {
rows[i] = this.generateRow(data[i], startIndex);
}
return rows;
}
protected ensureColumns(): Cell<Column>[] {
//TODO: generate dummy column for group, detail here;
const cols: Cell<Column>[] = [];
if (this.parent.detailTemplate || this.parent.childGrid) {
const args: object = {};
this.parent.notify(events.detailIndentCellInfo, args);
cols.push(this.generateCell(args as Column, null, CellType.DetailExpand));
}
if (this.parent.isRowDragable()) {
cols.push(this.generateCell({} as Column, null, CellType.RowDragIcon));
}
return cols;
}
protected generateRow(
data: Object, index: number, cssClass?: string, indent?: number, pid?: number, tIndex?: number, parentUid?: string): Row<Column> {
const options: IRow<Column> = {};
options.foreignKeyData = {};
options.uid = getUid('grid-row');
options.data = data;
options.index = index;
options.indent = indent;
options.tIndex = tIndex;
options.isDataRow = true;
options.parentGid = pid;
options.parentUid = parentUid;
if (this.parent.isPrinting) {
if (this.parent.hierarchyPrintMode === 'All') {
options.isExpand = true;
} else if (this.parent.hierarchyPrintMode === 'Expanded' && this.parent.expandedRows && this.parent.expandedRows[index]) {
options.isExpand = this.parent.expandedRows[index].isExpand;
}
}
options.cssClass = cssClass;
options.isAltRow = this.parent.enableAltRow ? index % 2 !== 0 : false;
options.isAltRow = this.parent.enableAltRow ? index % 2 !== 0 : false;
options.isSelected = this.parent.getSelectedRowIndexes().indexOf(index) > -1;
this.refreshForeignKeyRow(options);
const cells: Cell<Column>[] = this.ensureColumns();
const row: Row<Column> = new Row<Column>(<{ [x: string]: Object }>options, this.parent);
row.cells = this.parent.getFrozenMode() === 'Right' ? this.generateCells(options).concat(cells)
: cells.concat(this.generateCells(options));
return row;
}
protected refreshForeignKeyRow(options: IRow<Column>): void {
const foreignKeyColumns: Column[] = this.parent.getForeignKeyColumns();
for (let i: number = 0; i < foreignKeyColumns.length; i++) {
setValue(foreignKeyColumns[i].field, getForeignData(foreignKeyColumns[i], options.data), options.foreignKeyData);
}
}
protected generateCells(options: IRow<Column>): Cell<Column>[] {
const dummies: Column[] = this.parent.getColumns() as Column[];
const tmp: Cell<Column>[] = [];
for (let i: number = 0; i < dummies.length; i++) {
tmp.push(this.generateCell(
dummies[i], <string>options.uid, isNullOrUndefined(dummies[i].commands) ? undefined : CellType.CommandColumn, null, i,
options.foreignKeyData));
}
return tmp;
}
/**
*
* @param {Column} column - Defines column details
* @param {string} rowId - Defines row id
* @param {CellType} cellType - Defines cell type
* @param {number} colSpan - Defines colSpan
* @param {number} oIndex - Defines index
* @param {Object} foreignKeyData - Defines foreign key data
* @returns {Cell<Column>} returns cell model
* @hidden
*/
public generateCell(
column: Column, rowId?: string, cellType?: CellType, colSpan?: number,
oIndex?: number, foreignKeyData?: Object): Cell<Column> {
const opt: ICell<Column> = {
'visible': column.visible,
'isDataCell': !isNullOrUndefined(column.field || column.template),
'isTemplate': !isNullOrUndefined(column.template),
'rowID': rowId,
'column': column,
'cellType': !isNullOrUndefined(cellType) ? cellType : CellType.Data,
'colSpan': colSpan,
'commands': column.commands,
'isForeignKey': column.isForeignColumn && column.isForeignColumn(),
'foreignKeyData': column.isForeignColumn && column.isForeignColumn() && getValue(column.field, foreignKeyData)
};
if (opt.isDataCell || opt.column.type === 'checkbox' || opt.commands) {
opt.index = oIndex;
}
return new Cell<Column>(<{ [x: string]: Object }>opt);
}
public refreshRows(input?: Row<Column>[]): Row<Column>[] {
for (let i: number = 0; i < input.length; i++) {
this.refreshForeignKeyRow(input[i]);
input[i].cells = this.generateCells(input[i]);
}
return input;
}
private getInfiniteIndex(args: InfiniteScrollArgs): number {
return args.requestType === 'infiniteScroll' || args.requestType === 'delete' || (args as SaveEventArgs).action === 'add'
? args.startIndex : 0;
}
}