/
reorder-group.tsx
328 lines (279 loc) · 8.85 KB
/
reorder-group.tsx
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
import { Component, ComponentInterface, Element, Event, EventEmitter, Method, Prop, QueueApi, State, Watch } from '@stencil/core';
import { Gesture, GestureDetail, ItemReorderEventDetail } from '../../interface';
import { hapticSelectionChanged, hapticSelectionEnd, hapticSelectionStart } from '../../utils/haptic';
const enum ReordeGroupState {
Idle = 0,
Active = 1,
Complete = 2
}
@Component({
tag: 'ion-reorder-group',
styleUrl: 'reorder-group.scss'
})
export class ReorderGroup implements ComponentInterface {
private selectedItemEl?: HTMLElement;
private selectedItemHeight!: number;
private lastToIndex = -1;
private cachedHeights: number[] = [];
private scrollEl?: HTMLElement;
private gesture?: Gesture;
private scrollElTop = 0;
private scrollElBottom = 0;
private scrollElInitial = 0;
private containerTop = 0;
private containerBottom = 0;
@State() state = ReordeGroupState.Idle;
@Element() el!: HTMLElement;
@Prop({ context: 'queue' }) queue!: QueueApi;
@Prop({ context: 'document' }) doc!: Document;
/**
* If `true`, the reorder will be hidden.
*/
@Prop() disabled = true;
@Watch('disabled')
disabledChanged() {
if (this.gesture) {
this.gesture.setDisabled(this.disabled);
}
const a = { a: 2 };
delete a.a;
}
/**
* Event that needs to be listened to in order to complete the reorder action.
* Once the event has been emitted, the `complete()` method then needs
* to be called in order to finalize the reorder action.
*/
@Event() ionItemReorder!: EventEmitter<ItemReorderEventDetail>;
async componentDidLoad() {
const contentEl = this.el.closest('ion-content');
if (contentEl) {
await contentEl.componentOnReady();
this.scrollEl = await contentEl.getScrollElement();
}
this.gesture = (await import('../../utils/gesture')).createGesture({
el: this.doc.body,
queue: this.queue,
gestureName: 'reorder',
gesturePriority: 110,
threshold: 0,
direction: 'y',
passive: false,
canStart: detail => this.canStart(detail),
onStart: ev => this.onStart(ev),
onMove: ev => this.onMove(ev),
onEnd: () => this.onEnd(),
});
this.disabledChanged();
}
componentDidUnload() {
this.onEnd();
if (this.gesture) {
this.gesture.destroy();
this.gesture = undefined;
}
}
/**
* This method must be called once the `ionItemReorder` event is handled in order
* to complete the reorder operation.
*/
@Method()
complete(listOrReorder?: boolean | any[]): Promise<any> {
return Promise.resolve(this.completeSync(listOrReorder));
}
private canStart(ev: GestureDetail): boolean {
if (this.selectedItemEl || this.state !== ReordeGroupState.Idle) {
return false;
}
const target = ev.event.target as HTMLElement;
const reorderEl = target.closest('ion-reorder');
if (!reorderEl) {
return false;
}
const item = findReorderItem(reorderEl, this.el);
if (!item) {
return false;
}
ev.data = item;
return true;
}
private onStart(ev: GestureDetail) {
ev.event.preventDefault();
const item = this.selectedItemEl = ev.data;
const heights = this.cachedHeights;
heights.length = 0;
const el = this.el;
const children: any = el.children;
if (!children || children.length === 0) {
return;
}
let sum = 0;
for (let i = 0; i < children.length; i++) {
const child = children[i];
sum += child.offsetHeight;
heights.push(sum);
child.$ionIndex = i;
}
const box = el.getBoundingClientRect();
this.containerTop = box.top;
this.containerBottom = box.bottom;
if (this.scrollEl) {
const scrollBox = this.scrollEl.getBoundingClientRect();
this.scrollElInitial = this.scrollEl.scrollTop;
this.scrollElTop = scrollBox.top + AUTO_SCROLL_MARGIN;
this.scrollElBottom = scrollBox.bottom - AUTO_SCROLL_MARGIN;
} else {
this.scrollElInitial = 0;
this.scrollElTop = 0;
this.scrollElBottom = 0;
}
this.lastToIndex = indexForItem(item);
this.selectedItemHeight = item.offsetHeight;
this.state = ReordeGroupState.Active;
item.classList.add(ITEM_REORDER_SELECTED);
hapticSelectionStart();
}
private onMove(ev: GestureDetail) {
const selectedItem = this.selectedItemEl;
if (!selectedItem) {
return;
}
// Scroll if we reach the scroll margins
const scroll = this.autoscroll(ev.currentY);
// // Get coordinate
const top = this.containerTop - scroll;
const bottom = this.containerBottom - scroll;
const currentY = Math.max(top, Math.min(ev.currentY, bottom));
const deltaY = scroll + currentY - ev.startY;
const normalizedY = currentY - top;
const toIndex = this.itemIndexForTop(normalizedY);
if (toIndex !== this.lastToIndex) {
const fromIndex = indexForItem(selectedItem);
this.lastToIndex = toIndex;
hapticSelectionChanged();
this.reorderMove(fromIndex, toIndex);
}
// Update selected item position
selectedItem.style.transform = `translateY(${deltaY}px)`;
}
private onEnd() {
const selectedItem = this.selectedItemEl;
this.state = ReordeGroupState.Complete;
if (!selectedItem) {
this.state = ReordeGroupState.Idle;
return;
}
const toIndex = this.lastToIndex;
const fromIndex = indexForItem(selectedItem);
if (toIndex === fromIndex) {
selectedItem.style.transition = 'transform 200ms ease-in-out';
setTimeout(() => this.completeSync(), 200);
} else {
this.ionItemReorder.emit({
from: fromIndex,
to: toIndex,
complete: this.completeSync.bind(this)
});
}
hapticSelectionEnd();
}
private completeSync(listOrReorder?: boolean | any[]): any {
const selectedItemEl = this.selectedItemEl;
if (selectedItemEl && this.state === ReordeGroupState.Complete) {
const children = this.el.children as any;
const len = children.length;
const toIndex = this.lastToIndex;
const fromIndex = indexForItem(selectedItemEl);
if (listOrReorder === true) {
const ref = (fromIndex < toIndex)
? children[toIndex + 1]
: children[toIndex];
this.el.insertBefore(selectedItemEl, ref);
}
if (Array.isArray(listOrReorder)) {
listOrReorder = reorderArray(listOrReorder, fromIndex, toIndex);
}
for (let i = 0; i < len; i++) {
children[i].style['transform'] = '';
}
selectedItemEl.style.transition = '';
selectedItemEl.classList.remove(ITEM_REORDER_SELECTED);
this.selectedItemEl = undefined;
this.state = ReordeGroupState.Idle;
}
return listOrReorder;
}
private itemIndexForTop(deltaY: number): number {
const heights = this.cachedHeights;
let i = 0;
// TODO: since heights is a sorted array of integers, we can do
// speed up the search using binary search. Remember that linear-search is still
// faster than binary-search for small arrays (<64) due CPU branch misprediction.
for (i = 0; i < heights.length; i++) {
if (heights[i] > deltaY) {
break;
}
}
return i;
}
/********* DOM WRITE ********* */
private reorderMove(fromIndex: number, toIndex: number) {
const itemHeight = this.selectedItemHeight;
const children = this.el.children;
for (let i = 0; i < children.length; i++) {
const style = (children[i] as any).style;
let value = '';
if (i > fromIndex && i <= toIndex) {
value = `translateY(${-itemHeight}px)`;
} else if (i < fromIndex && i >= toIndex) {
value = `translateY(${itemHeight}px)`;
}
style['transform'] = value;
}
}
private autoscroll(posY: number): number {
if (!this.scrollEl) {
return 0;
}
let amount = 0;
if (posY < this.scrollElTop) {
amount = -SCROLL_JUMP;
} else if (posY > this.scrollElBottom) {
amount = SCROLL_JUMP;
}
if (amount !== 0) {
this.scrollEl.scrollBy(0, amount);
}
return this.scrollEl.scrollTop - this.scrollElInitial;
}
hostData() {
return {
class: {
'reorder-enabled': !this.disabled,
'reorder-list-active': this.state !== ReordeGroupState.Idle,
}
};
}
}
function indexForItem(element: any): number {
return element['$ionIndex'];
}
function findReorderItem(node: HTMLElement | null, container: HTMLElement): HTMLElement | undefined {
let parent: HTMLElement | null;
while (node) {
parent = node.parentElement;
if (parent === container) {
return node;
}
node = parent;
}
return undefined;
}
const AUTO_SCROLL_MARGIN = 60;
const SCROLL_JUMP = 10;
const ITEM_REORDER_SELECTED = 'reorder-selected';
function reorderArray(array: any[], from: number, to: number): any[] {
const element = array[from];
array.splice(from, 1);
array.splice(to, 0, element);
return array.slice();
}