-
Notifications
You must be signed in to change notification settings - Fork 18
/
d-item-updater.ts
447 lines (408 loc) · 12.8 KB
/
d-item-updater.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
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
/*
* Copyright (C) 2019 Toshiba Corporation
* SPDX-License-Identifier: Apache-2.0
*/
import { DApplications } from "./d-applications";
import { DBase } from "./d-base";
import { isNumber } from "./util/is-number";
import { UtilKeyboardEvent } from "./util/util-keyboard-event";
export interface DItemUpdaterItem<VALUE> extends DBase {
readonly value?: VALUE;
readonly index?: number;
set(value: VALUE, index: number, forcibly?: boolean): void;
unset(): void;
}
export type DItemUpdaterNewItem<DATA, ITEM> = (data: DATA) => ITEM;
export type DItemUpdaterInitItem<DATA, ITEM> = (item: ITEM, index: number, data: DATA) => ITEM;
/**
* Item updater options.
*/
export interface DItemUpdaterOptions<VALUE, DATA, ITEM> {
/**
* Called to create items.
*/
newItem?: DItemUpdaterNewItem<DATA, ITEM>;
/**
* Called to initialize items.
*/
initItem?: DItemUpdaterInitItem<DATA, ITEM>;
/**
* True to make stripe-colored items. Defaults to true.
*/
stripe?: boolean;
}
export interface DItemUpdaterDataSelection<VALUE> {
contains(value: VALUE): boolean;
}
export interface DItemUpdaterDataMapped<VALUE> {
size(): number;
each(iteratee: (value: VALUE, index: number) => void | boolean, from: number, to: number): void;
}
export interface DItemUpdaterData<VALUE> {
readonly selection: DItemUpdaterDataSelection<VALUE>;
}
/**
* The primary purpose of this class is to minimize the number of rendered items (e.g., {@link DListItem})
* as low as possible and to update their positions and states. For this sake, the updater calculates the
* required number of items whenever their container size changes and creates items if needed.
*/
export abstract class DItemUpdater<
VALUE = unknown,
DATA extends DItemUpdaterData<VALUE> = DItemUpdaterData<VALUE>,
DATA_MAPPED extends DItemUpdaterDataMapped<VALUE> = DItemUpdaterDataMapped<VALUE>,
ITEM extends DItemUpdaterItem<VALUE> = DItemUpdaterItem<VALUE>,
OPTIONS extends DItemUpdaterOptions<VALUE, DATA, ITEM> = DItemUpdaterOptions<VALUE, DATA, ITEM>
> {
protected _updateItemsCount: number;
protected _isUpdateItemsCalled: boolean;
protected _isUpdateItemsCalledForcibly: boolean;
protected _itemHeight: number;
protected _itemWidth: number;
protected _multiplicity: number;
protected _itemIndexStart: number;
protected _itemIndexEnd: number;
protected _workItems: ITEM[];
protected _data: DATA;
protected _content: DBase;
protected _container: DBase;
protected _newItem: DItemUpdaterNewItem<DATA, ITEM>;
protected _initItem: DItemUpdaterInitItem<DATA, ITEM>;
constructor(data: DATA, content: DBase, container: DBase, options?: OPTIONS) {
this._updateItemsCount = 0;
this._isUpdateItemsCalled = false;
this._isUpdateItemsCalledForcibly = false;
this._itemHeight = -1;
this._itemWidth = -1;
this._multiplicity = 1;
this._itemIndexStart = 0;
this._itemIndexEnd = 0;
this._workItems = [];
this._data = data;
this._content = content;
this._container = container;
this._newItem = this.toNewItem(options);
this._initItem = this.toInitItem(options);
}
protected toNewItem(
options?: DItemUpdaterOptions<VALUE, DATA, ITEM>
): DItemUpdaterNewItem<DATA, ITEM> {
return options?.newItem || this.newItem;
}
protected abstract newItem(this: undefined, data: DATA): ITEM;
protected toInitItem(
options?: DItemUpdaterOptions<VALUE, DATA, ITEM>
): DItemUpdaterInitItem<DATA, ITEM> {
if (options) {
const initItem = options.initItem;
if (initItem) {
return initItem;
}
if (options.stripe !== false) {
return this.initItem;
}
return this.initItemNoStriping;
}
return this.initItem;
}
protected initItem(this: undefined, item: ITEM, index: number, data: DATA): ITEM {
item.state.isAlternated = index % 2 === 1;
return item;
}
protected initItemNoStriping(this: undefined, item: ITEM, index: number, data: DATA): ITEM {
return item;
}
get multiplicity(): number {
return this._multiplicity;
}
lock(): void {
this._updateItemsCount += 1;
if (this._updateItemsCount === 1) {
this._isUpdateItemsCalled = false;
this._isUpdateItemsCalledForcibly = false;
}
}
unlock(callIfNeeded: boolean): void {
this._updateItemsCount -= 1;
if (this._updateItemsCount === 0) {
if (callIfNeeded && this._isUpdateItemsCalled) {
this.update(this._isUpdateItemsCalledForcibly);
}
this._isUpdateItemsCalled = false;
this._isUpdateItemsCalledForcibly = false;
}
}
update(forcibly?: boolean): void {
if (0 < this._updateItemsCount) {
this._isUpdateItemsCalled = true;
if (forcibly) {
this._isUpdateItemsCalledForcibly = true;
}
return;
}
const content = this._content;
const contentParent = content.parent;
if (contentParent == null) {
return;
}
const container = this._container;
const items = container.children as ITEM[];
const height = contentParent.height;
const data = this._data;
const mapped = this.toMapped(data);
const dataSize = mapped.size();
const oldItemIndexStart = this._itemIndexStart;
let oldItemIndexEnd = this._itemIndexEnd;
let oldItemCount = oldItemIndexEnd - oldItemIndexStart;
const newItem = this._newItem;
const initItem = this._initItem;
let itemHeight = this._itemHeight;
let itemWidth = this._itemWidth;
if (this._itemHeight < 0) {
let item: ITEM;
if (0 < items.length) {
item = items[0];
} else {
item = initItem(newItem(data), oldItemIndexStart, data);
container.addChild(item);
oldItemIndexEnd += 1;
oldItemCount += 1;
}
itemHeight = Math.max(1, item.height);
if (isNumber(item.getWidth())) {
itemWidth = Math.max(1, item.width);
}
this._itemHeight = itemHeight;
this._itemWidth = itemWidth;
}
const contentPadding = content.padding;
const contentPaddingTop = contentPadding.getTop();
const contentPaddingBottom = contentPadding.getBottom();
const contentPaddingLeft = contentPadding.getLeft();
const contentPaddingRight = contentPadding.getRight();
const contentWidthAvailable = Math.max(
0,
content.width - contentPaddingLeft - contentPaddingRight
);
const multiplicity =
0 < itemWidth ? Math.max(1, Math.floor(contentWidthAvailable / itemWidth)) : 1;
this._multiplicity = multiplicity;
const y = content !== container ? container.transform.position.y : contentPaddingTop;
const newHeight = Math.ceil(dataSize / multiplicity) * itemHeight;
const newContentHeight = Math.max(
height,
contentPaddingTop + newHeight + contentPaddingBottom
);
const newContentY = Math.max(height - newContentHeight, content.position.y);
const newItemIndexLowerBound = Math.floor(
((0 - (newContentY + y)) * multiplicity) / itemHeight
);
const newItemIndexUpperBound = Math.floor(
((height - (newContentY + y)) * multiplicity) / itemHeight
);
let newItemIndexStart = newItemIndexLowerBound - multiplicity;
if (newItemIndexStart % 2 !== 0) {
newItemIndexStart -= 1;
}
let newItemIndexEnd = newItemIndexUpperBound + multiplicity + multiplicity;
if (newItemIndexEnd % 2 !== 0) {
newItemIndexEnd += 1;
}
let newItemCount = newItemIndexEnd - newItemIndexStart;
if (newItemCount < oldItemCount) {
newItemCount = oldItemCount;
newItemIndexEnd = newItemIndexStart + newItemCount;
}
if (oldItemCount < newItemCount) {
for (let i = oldItemCount; i < newItemCount; ++i) {
const oldItemIndex = oldItemIndexStart + i;
const item = initItem(newItem(data), oldItemIndex, data);
container.addChild(item);
}
oldItemCount = newItemCount;
oldItemIndexEnd = oldItemIndexStart + oldItemCount;
} else if (newItemCount < oldItemCount) {
for (let i = oldItemCount - 1; newItemCount <= i; --i) {
container.removeChild(items[i]);
}
oldItemCount = newItemCount;
oldItemIndexEnd = oldItemIndexStart + oldItemCount;
}
this._itemIndexStart = newItemIndexStart;
this._itemIndexEnd = newItemIndexEnd;
const itemIndexStartDelta = newItemIndexStart - oldItemIndexStart;
const itemIndexStartDeltaAbs = Math.abs(itemIndexStartDelta);
const itemsLength = items.length;
if (0 < itemIndexStartDeltaAbs && itemIndexStartDeltaAbs < itemsLength) {
const work = this._workItems;
if (0 < itemIndexStartDelta) {
for (let i = 0; i < itemIndexStartDeltaAbs; ++i) {
const item = items[i];
this.reset(item);
work.push(item);
}
for (let i = itemIndexStartDeltaAbs; i < itemsLength; ++i) {
items[i - itemIndexStartDeltaAbs] = items[i];
}
for (let i = 0; i < itemIndexStartDeltaAbs; ++i) {
items[itemsLength - itemIndexStartDeltaAbs + i] = work[i];
}
} else {
for (let i = 0; i < itemIndexStartDeltaAbs; ++i) {
const item = items[itemsLength - itemIndexStartDeltaAbs + i];
this.reset(item);
work.push(item);
}
for (let i = itemsLength - itemIndexStartDeltaAbs - 1; 0 <= i; --i) {
items[i + itemIndexStartDeltaAbs] = items[i];
}
for (let i = 0; i < itemIndexStartDeltaAbs; ++i) {
items[i] = work[i];
}
}
work.length = 0;
}
let itemOffsetX = 0;
let itemOffsetY = 0;
if (content === container) {
itemOffsetX = contentPaddingLeft;
itemOffsetY = contentPaddingTop;
}
mapped.each(
(datum: VALUE, index: number): void | boolean => {
const item = items[index - newItemIndexStart];
const ix = index % multiplicity;
const iy = Math.floor(index / multiplicity);
item.position.set(itemOffsetX + ix * itemWidth, itemOffsetY + iy * itemHeight);
this.set(item, datum, index, forcibly);
},
newItemIndexStart,
newItemIndexStart + itemsLength
);
for (let i = 0; newItemIndexStart + i < 0 && i < itemsLength; ++i) {
const item = items[i];
const index = newItemIndexStart + i;
const ix = index % multiplicity;
const iy = Math.floor(index / multiplicity);
item.position.set(itemOffsetX + ix * itemWidth, itemOffsetY + iy * itemHeight);
this.unset(item);
}
for (let i = itemsLength - 1; dataSize <= newItemIndexStart + i && 0 <= i; --i) {
const item = items[i];
const index = newItemIndexStart + i;
const ix = index % multiplicity;
const iy = Math.floor(index / multiplicity);
item.position.set(itemOffsetX + ix * itemWidth, itemOffsetY + iy * itemHeight);
this.unset(item);
}
this.lock();
content.position.y = newContentY;
content.height = newContentHeight;
if (content !== container) {
container.height = newHeight;
}
this.unlock(false);
}
protected abstract toMapped(data: DATA): DATA_MAPPED;
protected set(item: ITEM, value: VALUE, index: number, forcibly?: boolean): void {
item.set(value, index, forcibly);
}
protected unset(item: ITEM): void {
item.unset();
}
protected reset(item: ITEM): ITEM {
item.blur(true);
const cells = item.children;
for (let i = 0, imax = cells.length; i < imax; ++i) {
const cell = cells[i];
if (cell instanceof DBase) {
const state = cell.state;
state.lock();
state.isPressed = false;
state.isHovered = false;
state.unlock();
}
}
item.state.isHovered = false;
return item;
}
moveFocus(
e: KeyboardEvent,
target: DBase,
moveVertically: boolean,
moveHorizontally: boolean
): boolean {
if (!(moveVertically || moveHorizontally)) {
return false;
}
const isUp = moveVertically && UtilKeyboardEvent.isArrowUpKey(e);
const isDown = moveVertically && UtilKeyboardEvent.isArrowDownKey(e);
const isLeft = moveHorizontally && UtilKeyboardEvent.isArrowLeftKey(e);
const isRight = moveHorizontally && UtilKeyboardEvent.isArrowRightKey(e);
if (!(isUp || isDown || isLeft || isRight)) {
return false;
}
if (!target.state.isActionable) {
return false;
}
const layer = DApplications.getLayer(target);
if (layer == null) {
return false;
}
const focusController = layer.getFocusController();
const focused = focusController.get();
if (focused == null) {
return false;
}
const container = this._container;
if (focused.parent !== container) {
return false;
}
const item = focused as ITEM;
const index = item.index;
if (index == null) {
return false;
}
const multiplicity = this._multiplicity;
const data = this._data;
const mapped = this.toMapped(data);
const dataSize = mapped.size();
let newIndex = index;
if (isLeft || isRight) {
if (isLeft) {
if (0 <= newIndex - 1) {
newIndex -= 1;
}
} else {
if (newIndex + 1 < dataSize) {
newIndex += 1;
}
}
}
if (isUp || isDown) {
if (isUp) {
if (0 <= newIndex - multiplicity) {
newIndex -= multiplicity;
}
} else {
if (newIndex + multiplicity < dataSize) {
newIndex += multiplicity;
}
}
}
if (newIndex === index) {
return false;
}
const items = container.children as ITEM[];
const itemIndex = items.indexOf(item);
if (itemIndex < 0) {
return false;
}
const newItemIndex = itemIndex + (newIndex - index);
if (newItemIndex < 0 || items.length <= newItemIndex) {
return false;
}
const newItem = items[newItemIndex];
focusController.focus(newItem);
return true;
}
}