/
picker-component.ts
649 lines (546 loc) · 18.4 KB
/
picker-component.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
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
import { Component, ElementRef, EventEmitter, Input, HostListener, NgZone, Output, QueryList, Renderer, ViewChild, ViewChildren, ViewEncapsulation } from '@angular/core';
import { clamp, isNumber, isPresent, isString, assert } from '../../util/util';
import { Config } from '../../config/config';
import { DomController, DomDebouncer } from '../../platform/dom-controller';
import { GestureController, BlockerDelegate, BLOCK_ALL } from '../../gestures/gesture-controller';
import { Haptic } from '../../tap-click/haptic';
import { Key } from '../../platform/key';
import { NavParams } from '../../navigation/nav-params';
import { Picker } from './picker';
import { PickerOptions, PickerColumn, PickerColumnOption } from './picker-options';
import { Platform } from '../../platform/platform';
import { pointerCoord } from '../../util/dom';
import { UIEventManager } from '../../gestures/ui-event-manager';
import { ViewController } from '../../navigation/view-controller';
/**
* @private
*/
@Component({
selector: '.picker-col',
template:
'<div *ngIf="col.prefix" class="picker-prefix" [style.width]="col.prefixWidth">{{col.prefix}}</div>' +
'<div class="picker-opts" #colEle [style.max-width]="col.optionsWidth">' +
'<button *ngFor="let o of col.options; let i=index"' +
'[class.picker-opt-disabled]="o.disabled" ' +
'class="picker-opt" disable-activated (click)="optClick($event, i)">' +
'{{o.text}}' +
'</button>' +
'</div>' +
'<div *ngIf="col.suffix" class="picker-suffix" [style.width]="col.suffixWidth">{{col.suffix}}</div>',
host: {
'[style.max-width]': 'col.columnWidth',
'[class.picker-opts-left]': 'col.align=="left"',
'[class.picker-opts-right]': 'col.align=="right"',
}
})
export class PickerColumnCmp {
@ViewChild('colEle') colEle: ElementRef;
@Input() col: PickerColumn;
y: number = 0;
colHeight: number;
optHeight: number;
velocity: number;
pos: number[] = [];
startY: number = null;
rafId: number;
bounceFrom: number;
minY: number;
maxY: number;
rotateFactor: number;
scaleFactor: number;
lastIndex: number;
lastTempIndex: number;
decelerateFunc: Function;
debouncer: DomDebouncer;
events: UIEventManager;
@Output() ionChange: EventEmitter<any> = new EventEmitter();
constructor(
config: Config,
private _plt: Platform,
private elementRef: ElementRef,
private _zone: NgZone,
private _haptic: Haptic,
plt: Platform,
domCtrl: DomController,
) {
this.events = new UIEventManager(plt);
this.rotateFactor = config.getNumber('pickerRotateFactor', 0);
this.scaleFactor = config.getNumber('pickerScaleFactor', 1);
this.decelerateFunc = this.decelerate.bind(this);
this.debouncer = domCtrl.debouncer();
}
ngAfterViewInit() {
// get the scrollable element within the column
let colEle: HTMLElement = this.colEle.nativeElement;
this.colHeight = colEle.clientHeight;
// get the height of one option
this.optHeight = (colEle.firstElementChild ? colEle.firstElementChild.clientHeight : 0);
// set the scroll position for the selected option
this.setSelected(this.col.selectedIndex, 0);
// Listening for pointer events
this.events.pointerEvents({
element: this.elementRef.nativeElement,
pointerDown: this.pointerStart.bind(this),
pointerMove: this.pointerMove.bind(this),
pointerUp: this.pointerEnd.bind(this),
capture: true,
zone: false
});
}
ngOnDestroy() {
this._plt.cancelRaf(this.rafId);
this.events.destroy();
}
pointerStart(ev: UIEvent): boolean {
console.debug('picker, pointerStart', ev.type, this.startY);
this._haptic.gestureSelectionStart();
// We have to prevent default in order to block scrolling under the picker
// but we DO NOT have to stop propagation, since we still want
// some "click" events to capture
ev.preventDefault();
// cancel any previous raf's that haven't fired yet
this._plt.cancelRaf(this.rafId);
// remember where the pointer started from`
this.startY = pointerCoord(ev).y;
// reset everything
this.velocity = 0;
this.pos.length = 0;
this.pos.push(this.startY, Date.now());
let options = this.col.options;
let minY = (options.length - 1);
let maxY = 0;
for (var i = 0; i < options.length; i++) {
if (!options[i].disabled) {
minY = Math.min(minY, i);
maxY = Math.max(maxY, i);
}
}
this.minY = (minY * this.optHeight * -1);
this.maxY = (maxY * this.optHeight * -1);
return true;
}
pointerMove(ev: UIEvent) {
ev.preventDefault();
ev.stopPropagation();
let currentY = pointerCoord(ev).y;
this.pos.push(currentY, Date.now());
this.debouncer.write(() => {
if (this.startY === null) {
return;
}
// update the scroll position relative to pointer start position
let y = this.y + (currentY - this.startY);
if (y > this.minY) {
// scrolling up higher than scroll area
y = Math.pow(y, 0.8);
this.bounceFrom = y;
} else if (y < this.maxY) {
// scrolling down below scroll area
y += Math.pow(this.maxY - y, 0.9);
this.bounceFrom = y;
} else {
this.bounceFrom = 0;
}
this.update(y, 0, false, false);
let currentIndex = Math.max(Math.abs(Math.round(y / this.optHeight)), 0);
if (currentIndex !== this.lastTempIndex) {
// Trigger a haptic event for physical feedback that the index has changed
this._haptic.gestureSelectionChanged();
this.lastTempIndex = currentIndex;
}
});
}
pointerEnd(ev: UIEvent) {
ev.preventDefault();
this.debouncer.cancel();
if (this.startY === null) {
return;
}
console.debug('picker, pointerEnd', ev.type);
this.velocity = 0;
if (this.bounceFrom > 0) {
// bounce back up
this.update(this.minY, 100, true, true);
return;
} else if (this.bounceFrom < 0) {
// bounce back down
this.update(this.maxY, 100, true, true);
return;
}
let endY = pointerCoord(ev).y;
this.pos.push(endY, Date.now());
let endPos = (this.pos.length - 1);
let startPos = endPos;
let timeRange = (Date.now() - 100);
// move pointer to position measured 100ms ago
for (var i = endPos; i > 0 && this.pos[i] > timeRange; i -= 2) {
startPos = i;
}
if (startPos !== endPos) {
// compute relative movement between these two points
var timeOffset = (this.pos[endPos] - this.pos[startPos]);
var movedTop = (this.pos[startPos - 1] - this.pos[endPos - 1]);
// based on XXms compute the movement to apply for each render step
var velocity = ((movedTop / timeOffset) * FRAME_MS);
this.velocity = clamp(-MAX_PICKER_SPEED, velocity, MAX_PICKER_SPEED);
}
if (Math.abs(endY - this.startY) > 3) {
var y = this.y + (endY - this.startY);
this.update(y, 0, true, true);
}
this.startY = null;
this.decelerate();
}
decelerate() {
let y = 0;
if (isNaN(this.y) || !this.optHeight) {
// fallback in case numbers get outta wack
this.update(y, 0, true, true);
this._haptic.gestureSelectionEnd();
} else if (Math.abs(this.velocity) > 0) {
// still decelerating
this.velocity *= DECELERATION_FRICTION;
// do not let it go slower than a velocity of 1
this.velocity = (this.velocity > 0)
? Math.max(this.velocity, 1)
: Math.min(this.velocity, -1);
y = Math.round(this.y - this.velocity);
if (y > this.minY) {
// whoops, it's trying to scroll up farther than the options we have!
y = this.minY;
this.velocity = 0;
} else if (y < this.maxY) {
// gahh, it's trying to scroll down farther than we can!
y = this.maxY;
this.velocity = 0;
}
var notLockedIn = (y % this.optHeight !== 0 || Math.abs(this.velocity) > 1);
this.update(y, 0, true, !notLockedIn);
if (notLockedIn) {
// isn't locked in yet, keep decelerating until it is
this.rafId = this._plt.raf(this.decelerateFunc);
}
} else if (this.y % this.optHeight !== 0) {
// needs to still get locked into a position so options line up
var currentPos = Math.abs(this.y % this.optHeight);
// create a velocity in the direction it needs to scroll
this.velocity = (currentPos > (this.optHeight / 2) ? 1 : -1);
this._haptic.gestureSelectionEnd();
this.decelerate();
}
let currentIndex = Math.max(Math.abs(Math.round(y / this.optHeight)), 0);
if (currentIndex !== this.lastTempIndex) {
// Trigger a haptic event for physical feedback that the index has changed
this._haptic.gestureSelectionChanged();
}
this.lastTempIndex = currentIndex;
}
optClick(ev: UIEvent, index: number) {
if (!this.velocity) {
ev.preventDefault();
ev.stopPropagation();
this.setSelected(index, 150);
}
}
setSelected(selectedIndex: number, duration: number) {
// if there is a selected index, then figure out it's y position
// if there isn't a selected index, then just use the top y position
let y = (selectedIndex > -1) ? ((selectedIndex * this.optHeight) * -1) : 0;
this._plt.cancelRaf(this.rafId);
this.velocity = 0;
// so what y position we're at
this.update(y, duration, true, true);
}
update(y: number, duration: number, saveY: boolean, emitChange: boolean) {
// ensure we've got a good round number :)
y = Math.round(y);
let i: number;
let button: any;
let opt: any;
let optOffset: number;
let visible: boolean;
let translateX: number;
let translateY: number;
let translateZ: number;
let rotateX: number;
let transform: string;
let selected: boolean;
const parent = this.colEle.nativeElement;
const children = parent.children;
const length = children.length;
const selectedIndex = this.col.selectedIndex = Math.min(Math.max(Math.round(-y / this.optHeight), 0), length - 1);
const durationStr = (duration === 0) ? null : duration + 'ms';
const scaleStr = `scale(${this.scaleFactor})`;
for (i = 0; i < length; i++) {
button = children[i];
opt = <any>this.col.options[i];
optOffset = (i * this.optHeight) + y;
visible = true;
transform = '';
if (this.rotateFactor !== 0) {
rotateX = optOffset * this.rotateFactor;
if (Math.abs(rotateX) > 90) {
visible = false;
} else {
translateX = 0;
translateY = 0;
translateZ = 90;
transform = `rotateX(${rotateX}deg) `;
}
} else {
translateX = 0;
translateZ = 0;
translateY = optOffset;
if (Math.abs(translateY) > 170) {
visible = false;
}
}
selected = selectedIndex === i;
if (visible) {
transform += `translate3d(0px,${translateY}px,${translateZ}px) `;
if (this.scaleFactor !== 1 && !selected) {
transform += scaleStr;
}
} else {
transform = 'translate3d(-9999px,0px,0px)';
}
// Update transition duration
if (duration !== opt._dur) {
opt._dur = duration;
button.style[this._plt.Css.transitionDuration] = durationStr;
}
// Update transform
if (transform !== opt._trans) {
opt._trans = transform;
button.style[this._plt.Css.transform] = transform;
}
// Update selected item
if (selected !== opt._selected) {
opt._selected = selected;
if (selected) {
button.classList.add(PICKER_OPT_SELECTED);
} else {
button.classList.remove(PICKER_OPT_SELECTED);
}
}
}
if (saveY) {
this.y = y;
}
if (emitChange) {
if (this.lastIndex === undefined) {
// have not set a last index yet
this.lastIndex = this.col.selectedIndex;
} else if (this.lastIndex !== this.col.selectedIndex) {
// new selected index has changed from the last index
// update the lastIndex and emit that it has changed
this.lastIndex = this.col.selectedIndex;
var ionChange = this.ionChange;
if (ionChange.observers.length > 0) {
this._zone.run(ionChange.emit.bind(ionChange, this.col.options[this.col.selectedIndex]));
}
}
}
}
refresh() {
let min = this.col.options.length - 1;
let max = 0;
for (var i = 0; i < this.col.options.length; i++) {
if (!this.col.options[i].disabled) {
min = Math.min(min, i);
max = Math.max(max, i);
}
}
const selectedIndex = clamp(min, this.col.selectedIndex, max);
if (selectedIndex !== this.col.selectedIndex) {
var y = (selectedIndex * this.optHeight) * -1;
this.update(y, 150, true, true);
}
}
}
/**
* @private
*/
@Component({
selector: 'ion-picker-cmp',
template: `
<ion-backdrop (click)="bdClick()"></ion-backdrop>
<div class="picker-wrapper">
<div class="picker-toolbar">
<div *ngFor="let b of d.buttons" class="picker-toolbar-button" [ngClass]="b.cssRole">
<button ion-button (click)="btnClick(b)" [ngClass]="b.cssClass" class="picker-button" clear>
{{b.text}}
</button>
</div>
</div>
<div class="picker-columns">
<div class="picker-above-highlight"></div>
<div *ngFor="let c of d.columns" [col]="c" class="picker-col" (ionChange)="_colChange($event)"></div>
<div class="picker-below-highlight"></div>
</div>
</div>
`,
host: {
'role': 'dialog'
},
encapsulation: ViewEncapsulation.None,
})
export class PickerCmp {
@ViewChildren(PickerColumnCmp) _cols: QueryList<PickerColumnCmp>;
d: PickerOptions;
enabled: boolean;
lastClick: number;
id: number;
mode: string;
_gestureBlocker: BlockerDelegate;
constructor(
private _viewCtrl: ViewController,
private _elementRef: ElementRef,
config: Config,
private _plt: Platform,
gestureCtrl: GestureController,
params: NavParams,
renderer: Renderer
) {
this._gestureBlocker = gestureCtrl.createBlocker(BLOCK_ALL);
this.d = params.data;
this.mode = config.get('mode');
renderer.setElementClass(_elementRef.nativeElement, `picker-${this.mode}`, true);
if (this.d.cssClass) {
this.d.cssClass.split(' ').forEach(cssClass => {
renderer.setElementClass(_elementRef.nativeElement, cssClass, true);
});
}
this.id = (++pickerIds);
this.lastClick = 0;
}
ionViewWillLoad() {
// normalize the data
let data = this.d;
data.buttons = data.buttons.map(button => {
if (isString(button)) {
return { text: button };
}
if (button.role) {
button.cssRole = `picker-toolbar-${button.role}`;
}
return button;
});
// clean up dat data
data.columns = data.columns.map(column => {
if (!isPresent(column.options)) {
column.options = [];
}
column.selectedIndex = column.selectedIndex || 0;
column.options = column.options.map(inputOpt => {
let opt: PickerColumnOption = {
text: '',
value: '',
disabled: inputOpt.disabled,
};
if (isPresent(inputOpt)) {
if (isString(inputOpt) || isNumber(inputOpt)) {
opt.text = inputOpt.toString();
opt.value = inputOpt;
} else {
opt.text = isPresent(inputOpt.text) ? inputOpt.text : inputOpt.value;
opt.value = isPresent(inputOpt.value) ? inputOpt.value : inputOpt.text;
}
}
return opt;
});
return column;
});
}
ionViewWillEnter() {
this._gestureBlocker.block();
}
ionViewDidLeave() {
this._gestureBlocker.unblock();
}
refresh() {
this._cols.forEach(column => {
column.refresh();
});
}
_colChange(selectedOption: PickerColumnOption) {
// one of the columns has changed its selected index
var picker = <Picker>this._viewCtrl;
picker.ionChange.emit(this.getSelected());
}
@HostListener('body:keyup', ['$event'])
_keyUp(ev: KeyboardEvent) {
if (this.enabled && this._viewCtrl.isLast()) {
if (ev.keyCode === Key.ENTER) {
if (this.lastClick + 1000 < Date.now()) {
// do not fire this click if there recently was already a click
// this can happen when the button has focus and used the enter
// key to click the button. However, both the click handler and
// this keyup event will fire, so only allow one of them to go.
console.debug('picker, enter button');
let button = this.d.buttons[this.d.buttons.length - 1];
this.btnClick(button);
}
} else if (ev.keyCode === Key.ESCAPE) {
console.debug('picker, escape button');
this.bdClick();
}
}
}
ionViewDidEnter() {
this._plt.focusOutActiveElement();
let focusableEle = this._elementRef.nativeElement.querySelector('button');
if (focusableEle) {
focusableEle.focus();
}
this.enabled = true;
}
btnClick(button: any) {
if (!this.enabled) {
return;
}
// keep the time of the most recent button click
this.lastClick = Date.now();
let shouldDismiss = true;
if (button.handler) {
// a handler has been provided, execute it
// pass the handler the values from the inputs
if (button.handler(this.getSelected()) === false) {
// if the return value of the handler is false then do not dismiss
shouldDismiss = false;
}
}
if (shouldDismiss) {
this.dismiss(button.role);
}
}
bdClick() {
if (this.enabled && this.d.enableBackdropDismiss) {
this.dismiss('backdrop');
}
}
dismiss(role: any): Promise<any> {
return this._viewCtrl.dismiss(this.getSelected(), role);
}
getSelected(): any {
let selected: {[k: string]: any} = {};
this.d.columns.forEach((col, index) => {
let selectedColumn = col.options[col.selectedIndex];
selected[col.name] = {
text: selectedColumn ? selectedColumn.text : null,
value: selectedColumn ? selectedColumn.value : null,
columnIndex: index,
};
});
return selected;
}
ngOnDestroy() {
assert(this._gestureBlocker.blocked === false, 'gesture blocker must be already unblocked');
this._gestureBlocker.destroy();
}
}
let pickerIds = -1;
const PICKER_OPT_SELECTED = 'picker-opt-selected';
const DECELERATION_FRICTION = 0.97;
const FRAME_MS = (1000 / 60);
const MAX_PICKER_SPEED = 60;