/
grid-layout-common.ts
389 lines (323 loc) · 9.62 KB
/
grid-layout-common.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
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
import { GridLayout as GridLayoutDefinition, ItemSpec as ItemSpecDefinition } from '.';
import { LayoutBase } from '../layout-base';
import { View, CSSType } from '../../core/view';
import { Property, makeParser, makeValidator } from '../../core/properties';
import { Observable } from '../../../data/observable';
function validateArgs(element: View): View {
if (!element) {
throw new Error('element cannot be null or undefined.');
}
return element;
}
View.prototype.row = 0;
View.prototype.col = 0;
View.prototype.rowSpan = 1;
View.prototype.colSpan = 1;
Object.defineProperty(View.prototype, 'column', {
get(this: View): number {
return this.col;
},
set(this: View, value: number) {
this.col = value;
},
enumerable: true,
configurable: true,
});
Object.defineProperty(View.prototype, 'columnSpan', {
get(this: View): number {
return this.colSpan;
},
set(this: View, value: number) {
this.colSpan = value;
},
enumerable: true,
configurable: true,
});
function validateItemSpec(itemSpec: ItemSpec): void {
if (!itemSpec) {
throw new Error('Value cannot be undefined.');
}
if (itemSpec.owner) {
throw new Error('itemSpec is already added to GridLayout.');
}
}
function convertGridLength(value: string): ItemSpec {
if (value === GridUnitType.AUTO) {
return ItemSpec.create(1, GridUnitType.AUTO);
} else if (value.indexOf('*') !== -1) {
const starCount = parseInt(value.replace('*', '') || '1');
return ItemSpec.create(starCount, GridUnitType.STAR);
} else if (!isNaN(parseInt(value))) {
return ItemSpec.create(parseInt(value), GridUnitType.PIXEL);
} else {
throw new Error(`Cannot parse item spec from string: ${value}`);
}
}
function parseAndAddItemSpecs(value: string, func: (itemSpec: ItemSpec) => void): void {
// ensure value is a string since view bindings could be parsed as number/int's here
const arr = `${value}`.split(/[\s,]+/);
for (let i = 0, length = arr.length; i < length; i++) {
const str = arr[i].trim();
if (str.length > 0) {
func(convertGridLength(arr[i].trim()));
}
}
}
export class ItemSpec extends Observable implements ItemSpecDefinition {
private _value: number;
private _unitType: GridUnitType;
constructor(...args) {
super();
if (args.length === 0) {
this._value = 1;
this._unitType = GridUnitType.STAR;
} else if (arguments.length === 2) {
const value = args[0];
const type = args[1];
if (typeof value === 'number' && typeof type === 'string') {
if (value < 0 || isNaN(value) || !isFinite(value)) {
throw new Error(`Value should not be negative, NaN or Infinity: ${value}`);
}
this._value = value;
this._unitType = GridUnitType.parse(type);
} else {
throw new Error('First argument should be number, second argument should be string.');
}
} else {
throw new Error('ItemSpec expects 0 or 2 arguments');
}
this.index = -1;
}
public owner: GridLayoutBase;
public index: number;
public _actualLength = 0;
public static create(value: number, type: GridUnitType): ItemSpec {
const spec = new ItemSpec();
spec._value = value;
spec._unitType = type;
return spec;
}
public get actualLength(): number {
return this._actualLength;
}
public static equals(value1: ItemSpec, value2: ItemSpec): boolean {
return value1.gridUnitType === value2.gridUnitType && value1.value === value2.value && value1.owner === value2.owner && value1.index === value2.index;
}
get gridUnitType(): GridUnitType {
return this._unitType;
}
get isAbsolute(): boolean {
return this._unitType === GridUnitType.PIXEL;
}
get isAuto(): boolean {
return this._unitType === GridUnitType.AUTO;
}
get isStar(): boolean {
return this._unitType === GridUnitType.STAR;
}
get value(): number {
return this._value;
}
}
@CSSType('GridLayout')
export class GridLayoutBase extends LayoutBase implements GridLayoutDefinition {
private _rows: Array<ItemSpec> = new Array<ItemSpec>();
private _cols: Array<ItemSpec> = new Array<ItemSpec>();
public static getColumn(element: View): number {
return validateArgs(element).col;
}
public static setColumn(element: View, value: number): void {
validateArgs(element).col = value;
}
public static getColumnSpan(element: View): number {
return validateArgs(element).colSpan;
}
public static setColumnSpan(element: View, value: number): void {
validateArgs(element).colSpan = value;
}
public static getRow(element: View): number {
return validateArgs(element).row;
}
public static setRow(element: View, value: number): void {
validateArgs(element).row = value;
}
public static getRowSpan(element: View): number {
return validateArgs(element).rowSpan;
}
public static setRowSpan(element: View, value: number): void {
validateArgs(element).rowSpan = value;
}
public addRow(itemSpec: ItemSpec) {
validateItemSpec(itemSpec);
itemSpec.owner = this;
this._rows.push(itemSpec);
this._onRowAdded(itemSpec);
this.invalidate();
}
public addColumn(itemSpec: ItemSpec) {
validateItemSpec(itemSpec);
itemSpec.owner = this;
this._cols.push(itemSpec);
this._onColumnAdded(itemSpec);
this.invalidate();
}
public addChildAtCell(view: View, row: number, column: number, rowSpan?: number, columnSpan?: number): void {
this.addChild(view);
GridLayoutBase.setRow(view, row);
GridLayoutBase.setColumn(view, column);
if (rowSpan) {
GridLayoutBase.setRowSpan(view, rowSpan);
}
if (columnSpan) {
GridLayoutBase.setColumnSpan(view, columnSpan);
}
}
public removeRow(itemSpec: ItemSpec): void {
if (!itemSpec) {
throw new Error('Value is null.');
}
const index = this._rows.indexOf(itemSpec);
if (itemSpec.owner !== this || index < 0) {
throw new Error('Row is not child of this GridLayout');
}
itemSpec.index = -1;
this._rows.splice(index, 1);
this._onRowRemoved(itemSpec, index);
this.invalidate();
}
public removeColumn(itemSpec: ItemSpec): void {
if (!itemSpec) {
throw new Error('Value is null.');
}
const index = this._cols.indexOf(itemSpec);
if (itemSpec.owner !== this || index < 0) {
throw new Error('Column is not child of this GridLayout');
}
itemSpec.index = -1;
this._cols.splice(index, 1);
this._onColumnRemoved(itemSpec, index);
this.invalidate();
}
public removeColumns() {
for (let i = this._cols.length - 1; i >= 0; i--) {
const colSpec = this._cols[i];
this._onColumnRemoved(colSpec, i);
colSpec.index = -1;
}
this._cols.length = 0;
this.invalidate();
}
public removeRows() {
for (let i = this._rows.length - 1; i >= 0; i--) {
const rowSpec = this._rows[i];
this._onRowRemoved(rowSpec, i);
rowSpec.index = -1;
}
this._rows.length = 0;
this.invalidate();
}
public onRowChanged(element: View, oldValue: number, newValue: number) {
this.invalidate();
}
public onRowSpanChanged(element: View, oldValue: number, newValue: number) {
this.invalidate();
}
public onColumnChanged(element: View, oldValue: number, newValue: number) {
this.invalidate();
}
public onColumnSpanChanged(element: View, oldValue: number, newValue: number) {
this.invalidate();
}
public _onRowAdded(itemSpec: ItemSpec) {
//
}
public _onColumnAdded(itemSpec: ItemSpec) {
//
}
public _onRowRemoved(itemSpec: ItemSpec, index: number): void {
//
}
public _onColumnRemoved(itemSpec: ItemSpec, index: number): void {
//
}
public getColumns(): Array<ItemSpec> {
return this._cols.slice();
}
public getRows(): Array<ItemSpec> {
return this._rows.slice();
}
protected get columnsInternal(): Array<ItemSpec> {
return this._cols;
}
protected get rowsInternal(): Array<ItemSpec> {
return this._rows;
}
protected invalidate(): void {
// handled natively in android and overridden in ios.
}
set rows(value: string) {
this.removeRows();
parseAndAddItemSpecs(value, (spec: ItemSpec) => this.addRow(spec));
}
set columns(value: string) {
this.removeColumns();
parseAndAddItemSpecs(value, (spec: ItemSpec) => this.addColumn(spec));
}
}
GridLayoutBase.prototype.recycleNativeView = 'auto';
export const columnProperty = new Property<View, number>({
name: 'col',
defaultValue: 0,
valueChanged: (target, oldValue, newValue) => {
const grid = target.parent;
if (grid instanceof GridLayoutBase) {
grid.onColumnChanged(target, oldValue, newValue);
}
},
valueConverter: (v) => Math.max(0, parseInt(v)),
});
columnProperty.register(View);
export const columnSpanProperty = new Property<View, number>({
name: 'colSpan',
defaultValue: 1,
valueChanged: (target, oldValue, newValue) => {
const grid = target.parent;
if (grid instanceof GridLayoutBase) {
grid.onColumnSpanChanged(target, oldValue, newValue);
}
},
valueConverter: (v) => Math.max(1, parseInt(v)),
});
columnSpanProperty.register(View);
export const rowProperty = new Property<View, number>({
name: 'row',
defaultValue: 0,
valueChanged: (target, oldValue, newValue) => {
const grid = target.parent;
if (grid instanceof GridLayoutBase) {
grid.onRowChanged(target, oldValue, newValue);
}
},
valueConverter: (v) => Math.max(0, parseInt(v)),
});
rowProperty.register(View);
export const rowSpanProperty = new Property<View, number>({
name: 'rowSpan',
defaultValue: 1,
valueChanged: (target, oldValue, newValue) => {
const grid = target.parent;
if (grid instanceof GridLayoutBase) {
grid.onRowSpanChanged(target, oldValue, newValue);
}
},
valueConverter: (v) => Math.max(1, parseInt(v)),
});
rowSpanProperty.register(View);
export type GridUnitType = 'pixel' | 'star' | 'auto';
export namespace GridUnitType {
export const PIXEL = 'pixel';
export const STAR = 'star';
export const AUTO = 'auto';
export const isValid = makeValidator<GridUnitType>(PIXEL, STAR, AUTO);
export const parse = makeParser<GridUnitType>(isValid);
}