@nativescript-community/ui-drawer 0.1.20 → 0.1.21

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/index.js DELETED
@@ -1,942 +0,0 @@
1
- import { GestureHandlerStateEvent, GestureHandlerTouchEvent, GestureState, HandlerType, Manager, PanGestureHandler, install as installGestures } from '@nativescript-community/gesturehandler';
2
- import { Animation, Application, CSSType, Color, CoreTypes, GridLayout, Property, Utils, View, booleanConverter } from '@nativescript/core';
3
- installGestures(false);
4
- const OPEN_DURATION = 200;
5
- const CLOSE_DURATION = 200;
6
- export const PAN_GESTURE_TAG = 12431;
7
- const DEFAULT_TRIGGER_WIDTH = 20;
8
- const DEFAULT_TRIGGER_HEIGHT = 20;
9
- function transformAnimationValues(values) {
10
- values.translate = { x: values.translateX || 0, y: values.translateY || 0 };
11
- values.scale = { x: values.scaleX || 1, y: values.scaleY || 1 };
12
- delete values.translateX;
13
- delete values.translateY;
14
- delete values.scaleX;
15
- delete values.scaleY;
16
- return values;
17
- }
18
- export const leftDrawerContentProperty = new Property({
19
- name: 'leftDrawer',
20
- defaultValue: undefined,
21
- valueChanged: (target, oldValue, newValue) => target._onDrawerContentChanged('left', oldValue, newValue)
22
- });
23
- export const rightDrawerContentProperty = new Property({
24
- name: 'rightDrawer',
25
- defaultValue: undefined,
26
- valueChanged: (target, oldValue, newValue) => target._onDrawerContentChanged('right', oldValue, newValue)
27
- });
28
- export const topDrawerContentProperty = new Property({
29
- name: 'topDrawer',
30
- defaultValue: undefined,
31
- valueChanged: (target, oldValue, newValue) => target._onDrawerContentChanged('top', oldValue, newValue)
32
- });
33
- export const bottomDrawerContentProperty = new Property({
34
- name: 'bottomDrawer',
35
- defaultValue: undefined,
36
- valueChanged: (target, oldValue, newValue) => target._onDrawerContentChanged('bottom', oldValue, newValue)
37
- });
38
- export const gestureEnabledProperty = new Property({
39
- name: 'gestureEnabled',
40
- defaultValue: true,
41
- valueConverter: booleanConverter
42
- });
43
- export const backdropColorProperty = new Property({
44
- name: 'backdropColor',
45
- valueConverter: (c) => (c ? new Color(c) : null)
46
- });
47
- export const leftDrawerModeProperty = new Property({
48
- name: 'leftDrawerMode'
49
- });
50
- export const rightDrawerModeProperty = new Property({
51
- name: 'rightDrawerMode'
52
- });
53
- export const topDrawerModeProperty = new Property({
54
- name: 'topDrawerMode'
55
- });
56
- export const bottomDrawerModeProperty = new Property({
57
- name: 'bottomDrawerMode'
58
- });
59
- export const translationFunctionProperty = new Property({
60
- name: 'translationFunction'
61
- });
62
- export const animationFunctionProperty = new Property({
63
- name: 'animationFunction'
64
- });
65
- export const backDropEnabledProperty = new Property({
66
- defaultValue: true,
67
- valueConverter: booleanConverter,
68
- name: 'backDropEnabled'
69
- });
70
- export const startingSideProperty = new Property({
71
- name: 'startingSide',
72
- defaultValue: null
73
- });
74
- export const gestureHandlerOptionsProperty = new Property({
75
- name: 'gestureHandlerOptions'
76
- });
77
- const SIDES = ['left', 'right', 'top', 'bottom'];
78
- let Drawer = class Drawer extends GridLayout {
79
- constructor() {
80
- super();
81
- this.gestureTag = PAN_GESTURE_TAG;
82
- this.gestureMinDist = 10;
83
- this.waitFor = [];
84
- this.simultaneousHandlers = [];
85
- this.leftSwipeDistance = 40;
86
- this.rightSwipeDistance = 40;
87
- this.bottomSwipeDistance = 40;
88
- this.topSwipeDistance = 40;
89
- this.backdropColor = new Color('rgba(0, 0, 0, 0.7)');
90
- this.leftOpenedDrawerAllowDraging = true;
91
- this.rightOpenedDrawerAllowDraging = true;
92
- this.bottomOpenedDrawerAllowDraging = true;
93
- this.topOpenedDrawerAllowDraging = true;
94
- this.leftClosedDrawerAllowDraging = true;
95
- this.rightClosedDrawerAllowDraging = true;
96
- this.bottomClosedDrawerAllowDraging = true;
97
- this.topClosedDrawerAllowDraging = true;
98
- this.gestureEnabled = true;
99
- this.backdropTapGestureEnabled = true;
100
- this.openAnimationDuration = OPEN_DURATION;
101
- this.closeAnimationDuration = CLOSE_DURATION;
102
- this.mIsPanning = false;
103
- this.mIsAnimating = false;
104
- this.mPrevDeltaX = 0;
105
- this.mPrevDeltaY = 0;
106
- this.mViewWidth = { left: undefined, right: undefined };
107
- this.mViewHeight = { bottom: undefined, top: undefined };
108
- this.mTranslationX = { left: 0, right: 0 };
109
- this.mTranslationY = { bottom: 0, top: 0 };
110
- this.mShowingSide = null;
111
- // private mNeedToSetSide: Side | VerticalSide;
112
- this.mModes = {};
113
- this.backDropEnabled = true;
114
- this.mViewByIdCache = {};
115
- this.isPassThroughParentEnabled = true;
116
- }
117
- _onBackDropEnabledValueChanged() {
118
- if (this.backDropEnabled && !this.backDrop) {
119
- this.backDrop = new GridLayout();
120
- this.backDrop.backgroundColor = this.backdropColor;
121
- this.backDrop.opacity = 0;
122
- this.backDrop.visibility = 'hidden';
123
- this.insertChild(this.backDrop, 0);
124
- }
125
- else if (!this.backDropEnabled && this.backDrop) {
126
- this.removeChild(this.backDrop);
127
- this.backDrop = null;
128
- }
129
- }
130
- [backDropEnabledProperty.setNative](value) {
131
- this._onBackDropEnabledValueChanged();
132
- }
133
- getActualSide(side) {
134
- return SIDES.indexOf(side) >= 0 ? side : null;
135
- }
136
- updateStartingSide(side) {
137
- startingSideProperty.nativeValueChange(this, side);
138
- }
139
- [startingSideProperty.setNative](value) {
140
- value = this.getActualSide(value);
141
- if (value === this.mShowingSide) {
142
- return;
143
- }
144
- if (value && !this.mViewWidth[value] && !this.mViewHeight[value]) {
145
- this.mShowingSide = value;
146
- const drawer = this[value + 'Drawer'];
147
- if (drawer) {
148
- drawer.visibility = this.mShowingSide === value ? 'visible' : 'hidden';
149
- }
150
- }
151
- else if (value) {
152
- this.open(value, 0);
153
- }
154
- else {
155
- this.close(value, 0);
156
- }
157
- }
158
- onBackdropTap() {
159
- this.close();
160
- }
161
- initGestures() {
162
- const manager = Manager.getInstance();
163
- const gestureHandler = manager.createGestureHandler(HandlerType.PAN, this.gestureTag, {
164
- shouldStartGesture: this.shouldStartGesture.bind(this),
165
- simultaneousHandlers: this.simultaneousHandlers,
166
- waitFor: this.waitFor,
167
- minDist: this.gestureMinDist,
168
- ...(this.gestureHandlerOptions || {})
169
- });
170
- gestureHandler.on(GestureHandlerTouchEvent, this.onGestureTouch, this);
171
- gestureHandler.on(GestureHandlerStateEvent, this.onGestureState, this);
172
- gestureHandler.attachToView(this);
173
- this.panGestureHandler = gestureHandler;
174
- }
175
- initNativeView() {
176
- if (this.backDropEnabled && !this.backDrop) {
177
- this[backDropEnabledProperty.setNative](this.backDropEnabled);
178
- }
179
- super.initNativeView();
180
- if (this.backDrop && this.backdropTapGestureEnabled) {
181
- this.backDrop.on('tap', this.onBackdropTap, this);
182
- }
183
- if (this.gestureEnabled) {
184
- this.initGestures();
185
- }
186
- }
187
- disposeNativeView() {
188
- super.disposeNativeView();
189
- if (this.backDrop) {
190
- this.backDrop.off('tap', this.onBackdropTap, this);
191
- }
192
- if (this.panGestureHandler) {
193
- this.panGestureHandler.off(GestureHandlerTouchEvent, this.onGestureTouch, this);
194
- this.panGestureHandler.off(GestureHandlerStateEvent, this.onGestureState, this);
195
- this.panGestureHandler.detachFromView();
196
- this.panGestureHandler = null;
197
- }
198
- }
199
- shouldStartSheetDraggingInternal(side) {
200
- let result = this[side + 'OpenedDrawerAllowDraging'];
201
- if (result && this.shouldStartSheetDragging) {
202
- result = this.shouldStartSheetDragging(side);
203
- }
204
- return result;
205
- }
206
- shouldStartGesture(data) {
207
- // const landscape = Application.orientation() === 'landscape';
208
- const width = Utils.layout.toDeviceIndependentPixels(this.getMeasuredWidth());
209
- const height = Utils.layout.toDeviceIndependentPixels(this.getMeasuredHeight());
210
- const side = this.mShowingSide;
211
- if (side) {
212
- if (side === 'left' || side === 'right') {
213
- const viewWidth = this.mViewWidth[side];
214
- if ((side === 'left' && data.x <= viewWidth) || (side === 'right' && data.x >= width - viewWidth)) {
215
- return this.shouldStartSheetDraggingInternal(side);
216
- }
217
- }
218
- else {
219
- const viewHeight = this.mViewHeight[side];
220
- if ((side === 'top' && data.y <= viewHeight) || (side === 'bottom' && data.y >= height - viewHeight)) {
221
- return this.shouldStartSheetDraggingInternal(side);
222
- }
223
- }
224
- // for now without backDrop we force allow gesture
225
- return !this.backDrop || this.backDrop.opacity !== 0;
226
- }
227
- else {
228
- let needToSetSide;
229
- if (this.leftDrawer && (!this.leftSwipeDistance || data.x <= this.leftSwipeDistance)) {
230
- needToSetSide = 'left';
231
- }
232
- else if (this.rightDrawer && (!this.rightSwipeDistance || data.x >= width - this.rightSwipeDistance)) {
233
- needToSetSide = 'right';
234
- }
235
- else if (this.bottomDrawer && (!this.bottomSwipeDistance || data.y >= height - this.bottomSwipeDistance)) {
236
- needToSetSide = 'bottom';
237
- }
238
- else if (this.topDrawer && (!this.topSwipeDistance || data.y <= this.topSwipeDistance)) {
239
- needToSetSide = 'top';
240
- }
241
- if (needToSetSide && this[needToSetSide + 'ClosedDrawerAllowDraging']) {
242
- // this.mNeedToSetSide = needToSetSide;
243
- return true;
244
- }
245
- }
246
- return false;
247
- }
248
- getDrawerToOpen(extraData) {
249
- if (extraData.translationX < 0 && this.rightDrawer) {
250
- return 'right';
251
- }
252
- else if (extraData.translationX > 0 && this.leftDrawer) {
253
- return 'left';
254
- }
255
- else if (extraData.translationY < 0 && this.bottomDrawer) {
256
- return 'bottom';
257
- }
258
- else if (extraData.translationY > 0 && this.topDrawer) {
259
- return 'top';
260
- }
261
- return null;
262
- }
263
- onGestureState(args) {
264
- const { state, prevState, extraData, view } = args.data;
265
- if (state === GestureState.ACTIVE) {
266
- if (!this.mShowingSide) {
267
- const shouldShowSide = this.getDrawerToOpen(extraData);
268
- if (shouldShowSide && shouldShowSide !== this.mShowingSide) {
269
- this[shouldShowSide + 'Drawer'].visibility = 'visible';
270
- this.mShowingSide = shouldShowSide;
271
- this.notify({ eventName: 'start', side: this.mShowingSide });
272
- }
273
- // if (this.mNeedToSetSide === 'left') {
274
- // this.leftDrawer.visibility = 'visible';
275
- // } else if (this.mNeedToSetSide === 'right') {
276
- // this.rightDrawer.visibility = 'visible';
277
- // } else if (this.mNeedToSetSide === 'bottom') {
278
- // this.bottomDrawer.visibility = 'visible';
279
- // } else if (this.mNeedToSetSide === 'top') {
280
- // this.topDrawer.visibility = 'visible';
281
- // }
282
- }
283
- }
284
- this.updateIsPanning(state);
285
- if (prevState === GestureState.ACTIVE) {
286
- const side = this.mShowingSide;
287
- // const side = this.mShowingSide || this.mNeedToSetSide;
288
- // this.mNeedToSetSide = null;
289
- if (!side || (this.shouldPan && !this.shouldPan(side))) {
290
- return;
291
- }
292
- const { velocityX, velocityY, translationX, translationY } = extraData;
293
- const dragToss = 0.05;
294
- let destSnapPoint = 0;
295
- if (side === 'left' || side === 'right') {
296
- const viewWidth = this.mViewWidth[side];
297
- const viewX = this.mTranslationX[side] - viewWidth;
298
- const x = translationX - this.mPrevDeltaX;
299
- this.mPrevDeltaX = 0;
300
- const totalDelta = x + dragToss * velocityX;
301
- if (side === 'left') {
302
- if (totalDelta < -DEFAULT_TRIGGER_WIDTH) {
303
- destSnapPoint = 0;
304
- }
305
- else if (totalDelta > DEFAULT_TRIGGER_WIDTH) {
306
- destSnapPoint = viewWidth;
307
- }
308
- else {
309
- const endOffsetX = viewX + totalDelta;
310
- const progress = Math.abs(endOffsetX / viewWidth);
311
- destSnapPoint = progress > 0.5 ? viewWidth : 0;
312
- }
313
- }
314
- else if (side === 'right') {
315
- if (-totalDelta < -DEFAULT_TRIGGER_WIDTH) {
316
- destSnapPoint = 0;
317
- }
318
- else if (-totalDelta > DEFAULT_TRIGGER_WIDTH) {
319
- destSnapPoint = viewWidth;
320
- }
321
- else {
322
- const endOffsetX = viewX + totalDelta;
323
- const progress = Math.abs(endOffsetX / viewWidth);
324
- destSnapPoint = progress > 0.5 ? viewWidth : 0;
325
- }
326
- }
327
- }
328
- else {
329
- const viewHeight = this.mViewHeight[side];
330
- const viewY = this.mTranslationY[side] - viewHeight;
331
- const y = translationY - this.mPrevDeltaY;
332
- this.mPrevDeltaY = 0;
333
- const totalDelta = y + dragToss * velocityY;
334
- if (side === 'top') {
335
- if (totalDelta < -DEFAULT_TRIGGER_HEIGHT) {
336
- destSnapPoint = 0;
337
- }
338
- else if (totalDelta > DEFAULT_TRIGGER_HEIGHT) {
339
- destSnapPoint = viewHeight;
340
- }
341
- else {
342
- const endOffsetY = viewY + totalDelta;
343
- const progress = Math.abs(endOffsetY / viewHeight);
344
- destSnapPoint = progress > 0.5 ? viewHeight : 0;
345
- }
346
- }
347
- else if (side === 'bottom') {
348
- if (-totalDelta < -DEFAULT_TRIGGER_HEIGHT) {
349
- destSnapPoint = 0;
350
- }
351
- else if (-totalDelta > DEFAULT_TRIGGER_HEIGHT) {
352
- destSnapPoint = viewHeight;
353
- }
354
- else {
355
- const endOffsetY = viewY + totalDelta;
356
- const progress = Math.abs(endOffsetY / viewHeight);
357
- destSnapPoint = progress > 0.5 ? viewHeight : 0;
358
- }
359
- }
360
- }
361
- this.animateToPosition(side, destSnapPoint);
362
- }
363
- }
364
- isSideVisible(side) {
365
- if (side === 'left' || side === 'right') {
366
- return this.mViewWidth[side] - this.mTranslationX[side];
367
- }
368
- else {
369
- return this.mViewHeight[side] - this.mTranslationY[side];
370
- }
371
- }
372
- onGestureTouch(args) {
373
- const data = args.data;
374
- const { state, extraData, view } = args.data;
375
- // const side = this.mShowingSide || this.mNeedToSetSide;
376
- if (data.state !== GestureState.ACTIVE || this.mIsAnimating) {
377
- return;
378
- }
379
- const shouldShowSide = this.getDrawerToOpen(extraData);
380
- if (shouldShowSide && (!this.mShowingSide || (shouldShowSide !== this.mShowingSide && !this.isSideVisible(this.mShowingSide)))) {
381
- if (this.mShowingSide) {
382
- this[this.mShowingSide + 'Drawer'].visibility = 'hidden';
383
- this.notify({ eventName: 'end', side: this.mShowingSide });
384
- }
385
- this[shouldShowSide + 'Drawer'].visibility = 'visible';
386
- this.mShowingSide = shouldShowSide;
387
- this.notify({ eventName: 'start', side: this.mShowingSide });
388
- }
389
- const side = this.mShowingSide;
390
- if (!side || this.mIsAnimating) {
391
- return;
392
- }
393
- if (side === 'left' || side === 'right') {
394
- const deltaX = extraData.translationX;
395
- if (this.mIsAnimating || !this.mIsPanning || deltaX === 0 || (this.shouldPan && !this.shouldPan(side))) {
396
- this.mPrevDeltaX = deltaX;
397
- return;
398
- }
399
- // if (this.mNeedToSetSide) {
400
- // this.mShowingSide = this.mNeedToSetSide;
401
- // this.mNeedToSetSide = null;
402
- // }
403
- const width = this.mViewWidth[side];
404
- const viewX = this.mTranslationX[side] - width;
405
- let x = deltaX - this.mPrevDeltaX;
406
- if (side === 'left') {
407
- x = -x;
408
- }
409
- const trX = this.constrainX(side, viewX + x);
410
- this.mTranslationX[side] = width + trX;
411
- const trData = this.computeTranslationData(side, width + trX);
412
- if (this.backDrop) {
413
- this.backDrop.visibility = trData.backDrop && trData.backDrop.opacity > 0 ? 'visible' : 'hidden';
414
- }
415
- this.applyTrData(trData, side);
416
- this.updateIsPanning(state);
417
- this.mPrevDeltaX = deltaX;
418
- }
419
- else {
420
- const deltaY = extraData.translationY;
421
- if (this.mIsAnimating || !this.mIsPanning || deltaY === 0 || (this.shouldPan && !this.shouldPan(side))) {
422
- this.mPrevDeltaY = deltaY;
423
- return;
424
- }
425
- // if (this.mNeedToSetSide) {
426
- // this.mShowingSide = this.mNeedToSetSide;
427
- // this.mNeedToSetSide = null;
428
- // }
429
- const height = this.mViewHeight[side];
430
- const viewY = this.mTranslationY[side] - height;
431
- let y = deltaY - this.mPrevDeltaY;
432
- if (side === 'top') {
433
- y = -y;
434
- }
435
- const trY = this.constrainY(side, viewY + y);
436
- this.mTranslationY[side] = height + trY;
437
- const trData = this.computeTranslationData(side, height + trY);
438
- if (this.backDrop) {
439
- this.backDrop.visibility = trData.backDrop && trData.backDrop.opacity > 0 ? 'visible' : 'hidden';
440
- }
441
- this.applyTrData(trData, side);
442
- this.updateIsPanning(state);
443
- this.mPrevDeltaY = deltaY;
444
- }
445
- }
446
- [gestureEnabledProperty.setNative](value) {
447
- if (this.panGestureHandler) {
448
- this.panGestureHandler.enabled = value;
449
- }
450
- else if (value && !this.panGestureHandler) {
451
- this.initGestures();
452
- }
453
- }
454
- [backdropColorProperty.setNative](value) {
455
- if (this.backDrop) {
456
- this.backDrop.backgroundColor = value;
457
- }
458
- }
459
- [leftDrawerModeProperty.setNative](value) {
460
- this.onSideModeChanged('left', value);
461
- }
462
- [rightDrawerModeProperty.setNative](value) {
463
- this.onSideModeChanged('right', value);
464
- }
465
- [topDrawerModeProperty.setNative](value) {
466
- this.onSideModeChanged('top', value);
467
- }
468
- [bottomDrawerModeProperty.setNative](value) {
469
- this.onSideModeChanged('bottom', value);
470
- }
471
- [gestureHandlerOptionsProperty.setNative](value) {
472
- if (this.panGestureHandler) {
473
- Object.assign(this.panGestureHandler, value || {});
474
- }
475
- }
476
- _onMainContentChanged(oldValue, newValue) {
477
- this._onBackDropEnabledValueChanged();
478
- if (oldValue) {
479
- this.removeChild(oldValue);
480
- }
481
- if (newValue) {
482
- const indexBack = this.backDrop ? this.getChildIndex(this.backDrop) : 0;
483
- const index = this.getChildIndex(newValue);
484
- if (index !== indexBack - 1 && newValue.parent === this) {
485
- this.removeChild(newValue);
486
- this.insertChild(newValue, indexBack);
487
- }
488
- else {
489
- this.insertChild(newValue, indexBack);
490
- }
491
- }
492
- }
493
- leftLayoutChanged(event) {
494
- return this.onLayoutChange('left', event);
495
- }
496
- rightLayoutChanged(event) {
497
- return this.onLayoutChange('right', event);
498
- }
499
- topLayoutChanged(event) {
500
- return this.onLayoutChange('top', event);
501
- }
502
- bottomLayoutChanged(event) {
503
- return this.onLayoutChange('bottom', event);
504
- }
505
- addChild(child) {
506
- // for now we ignore this
507
- // to make sure we add the view in the property change
508
- // this is to make sure the view does not get "visible" too quickly
509
- // before we apply the translation
510
- // super.addChild(child);
511
- }
512
- _onDrawerContentChanged(side, oldValue, newValue) {
513
- if (oldValue === newValue) {
514
- return;
515
- }
516
- this._onBackDropEnabledValueChanged();
517
- if (oldValue) {
518
- oldValue.off('layoutChanged', this[side + 'LayoutChanged'], this);
519
- this.removeChild(oldValue);
520
- }
521
- if (newValue) {
522
- // newValue.columns = "auto"
523
- if (side === 'left' || side === 'right') {
524
- newValue.horizontalAlignment = side;
525
- }
526
- else {
527
- newValue.verticalAlignment = side;
528
- }
529
- newValue.on('layoutChanged', this[side + 'LayoutChanged'], this);
530
- this.onSideModeChanged(side, this.mModes[side]);
531
- }
532
- }
533
- onSideModeChanged(side, mode, oldMode = this.mModes[side]) {
534
- if ((oldMode && oldMode === mode) || (oldMode && oldMode !== 'under' && mode !== 'under')) {
535
- return;
536
- }
537
- const drawer = this[side + 'Drawer'];
538
- if (!drawer) {
539
- return;
540
- }
541
- const indexBack = this.backDrop ? this.getChildIndex(this.backDrop) : 0;
542
- const index = this.getChildIndex(drawer);
543
- drawer.visibility = this.mShowingSide === side ? 'visible' : 'hidden';
544
- if (mode === 'under') {
545
- if (index > indexBack - 1 && drawer.parent === this) {
546
- drawer.reusable = true;
547
- this.removeChild(drawer);
548
- this.insertChild(drawer, Math.max(indexBack - 1, 0));
549
- drawer.reusable = false;
550
- }
551
- else {
552
- // initial addition
553
- // this.backDrop.visibility = trData.backDrop && trData.backDrop.opacity > 0 ? 'visible' : 'hidden';
554
- this.insertChild(drawer, 0);
555
- }
556
- }
557
- else {
558
- if (index <= indexBack && drawer.parent === this) {
559
- drawer.reusable = true;
560
- this.removeChild(drawer);
561
- this.insertChild(drawer, indexBack + 1);
562
- drawer.reusable = false;
563
- }
564
- else {
565
- // initial addition
566
- this.insertChild(drawer, indexBack + 1);
567
- }
568
- }
569
- }
570
- computeTranslationData(side, value) {
571
- if (side === 'left' || side === 'right') {
572
- const width = this.mViewWidth[side];
573
- const delta = Math.max(width - value, 0);
574
- const progress = delta / width;
575
- if (this.translationFunction) {
576
- return this.translationFunction(side, width, value, delta, progress, this);
577
- }
578
- if (this.mModes[side] === 'under') {
579
- return {
580
- mainContent: {
581
- translateX: side === 'right' ? -delta : delta
582
- },
583
- [side + 'Drawer']: {
584
- translateX: 0
585
- },
586
- backDrop: {
587
- translateX: side === 'right' ? -delta : delta,
588
- opacity: progress
589
- }
590
- };
591
- }
592
- else {
593
- return {
594
- mainContent: {
595
- // translateX: 0
596
- },
597
- [side + 'Drawer']: {
598
- translateX: side === 'left' ? -value : value
599
- },
600
- backDrop: {
601
- // translateX: 0,
602
- opacity: progress
603
- }
604
- };
605
- }
606
- }
607
- else {
608
- const height = this.mViewHeight[side];
609
- const delta = Math.max(height - value, 0);
610
- const progress = delta / height;
611
- if (this.translationFunction) {
612
- return this.translationFunction(side, height, value, delta, progress, this);
613
- }
614
- if (this.mModes[side] === 'under') {
615
- return {
616
- mainContent: {
617
- translateY: side === 'bottom' ? -delta : delta
618
- },
619
- [side + 'Drawer']: {
620
- translateY: 0
621
- },
622
- backDrop: {
623
- translateY: side === 'bottom' ? -delta : delta,
624
- opacity: progress
625
- }
626
- };
627
- }
628
- else {
629
- return {
630
- mainContent: {
631
- translateY: 0
632
- },
633
- [side + 'Drawer']: {
634
- translateY: side === 'top' ? -value : value
635
- },
636
- backDrop: {
637
- translateY: 0,
638
- opacity: progress
639
- }
640
- };
641
- }
642
- }
643
- }
644
- onLayoutChange(side, event) {
645
- const contentView = event.object;
646
- let data;
647
- let safeAreaOffset = 0;
648
- let changed = false;
649
- const viewWidth = Utils.layout.toDeviceIndependentPixels(contentView.getMeasuredWidth());
650
- const viewHeight = Utils.layout.toDeviceIndependentPixels(contentView.getMeasuredHeight());
651
- if (side === 'left' || side === 'right') {
652
- if (__IOS__ && !this.iosIgnoreSafeArea) {
653
- const deviceOrientation = UIDevice.currentDevice.orientation;
654
- if (deviceOrientation === 3 /* UIDeviceOrientation.LandscapeLeft */) {
655
- safeAreaOffset = Application.ios.window.safeAreaInsets.left;
656
- }
657
- else if (deviceOrientation === 4 /* UIDeviceOrientation.LandscapeRight */) {
658
- safeAreaOffset = Application.ios.window.safeAreaInsets.right;
659
- }
660
- }
661
- const width = Math.ceil(Utils.layout.toDeviceIndependentPixels(contentView.getMeasuredWidth()) + safeAreaOffset);
662
- const firstSet = this.mViewWidth[side] === undefined;
663
- changed = width !== this.mViewWidth[side];
664
- this.mViewWidth[side] = width;
665
- if (firstSet) {
666
- const offset = this.mShowingSide === side ? 0 : width;
667
- data = this.computeTranslationData(side, offset);
668
- const shown = this.mViewWidth[side] - offset;
669
- this.mTranslationX[side] = width - shown;
670
- }
671
- else {
672
- this.mTranslationX[side] = this.mTranslationX[side] === 0 ? 0 : width;
673
- }
674
- }
675
- else {
676
- safeAreaOffset = __IOS__ && !this.iosIgnoreSafeArea && Application.ios.window.safeAreaInsets ? Application.ios.window.safeAreaInsets.bottom : 0;
677
- const height = Math.ceil(viewHeight + safeAreaOffset);
678
- const firstSet = this.mViewHeight[side] === undefined;
679
- changed = height !== this.mViewHeight[side];
680
- this.mViewHeight[side] = height;
681
- if (firstSet) {
682
- const offset = this.mShowingSide === side ? 0 : height;
683
- data = this.computeTranslationData(side, offset);
684
- const shown = this.mViewHeight[side] - offset;
685
- this.mTranslationY[side] = height - shown;
686
- }
687
- else {
688
- this.mTranslationY[side] = this.mTranslationY[side] === 0 ? 0 : height;
689
- }
690
- }
691
- if (changed && data) {
692
- // delay applyTrData or it will create a layout issue on iOS
693
- setTimeout(() => {
694
- this.applyTrData(data, side);
695
- if (this.backDrop) {
696
- this.backDrop.visibility = data.backDrop && data.backDrop.opacity > 0 ? 'visible' : 'hidden';
697
- }
698
- }, 0);
699
- }
700
- }
701
- forceEnsureSize(side) {
702
- const contentView = this[side + 'Drawer'];
703
- let data;
704
- let safeAreaOffset = 0;
705
- if (side === 'left' || side === 'right') {
706
- if (__IOS__ && !this.iosIgnoreSafeArea) {
707
- const deviceOrientation = UIDevice.currentDevice.orientation;
708
- if (deviceOrientation === 3) {
709
- safeAreaOffset = Application.ios.window.safeAreaInsets.left;
710
- }
711
- else if (deviceOrientation === 4) {
712
- safeAreaOffset = Application.ios.window.safeAreaInsets.right;
713
- }
714
- }
715
- const width = Math.ceil(Utils.layout.toDeviceIndependentPixels(contentView.getMeasuredWidth()) + safeAreaOffset);
716
- this.mViewWidth[side] = width;
717
- }
718
- else {
719
- safeAreaOffset = __IOS__ && !this.iosIgnoreSafeArea && Application.ios.window.safeAreaInsets ? Application.ios.window.safeAreaInsets.bottom : 0;
720
- const height = Math.ceil(Utils.layout.toDeviceIndependentPixels(contentView.getMeasuredHeight()) + safeAreaOffset);
721
- this.mViewHeight[side] = height;
722
- }
723
- }
724
- onTapGestureState(args) {
725
- const { state } = args.data;
726
- if (state === GestureState.BEGAN) {
727
- this.close();
728
- }
729
- }
730
- updateIsPanning(state) {
731
- this.mIsPanning = state === GestureState.ACTIVE || state === GestureState.BEGAN;
732
- }
733
- applyTrData(trData, side) {
734
- const cache = this.mViewByIdCache;
735
- Object.keys(trData).forEach((k) => {
736
- let target = this[k] || cache[k];
737
- if (!target) {
738
- target = this.getViewById(k);
739
- if (target) {
740
- cache[k] = target;
741
- }
742
- }
743
- if (target) {
744
- Object.assign(target, trData[k]);
745
- }
746
- });
747
- }
748
- constrainX(side, x) {
749
- const width = this.mViewWidth[side];
750
- if (x > 0) {
751
- return 0;
752
- }
753
- else if (x < -width) {
754
- return -width;
755
- }
756
- return x;
757
- }
758
- constrainY(side, y) {
759
- const height = this.mViewHeight[side];
760
- if (y > 0) {
761
- return 0;
762
- }
763
- else if (y < -height) {
764
- return -height;
765
- }
766
- return y;
767
- }
768
- async animateToPosition(side, position, duration = this.openAnimationDuration) {
769
- if (this.mShowingSide && side !== this.mShowingSide) {
770
- this.animateToPosition(this.mShowingSide, 0, duration);
771
- }
772
- const shouldSendEvent = side !== this.mShowingSide || (this.mShowingSide === side && position === 0);
773
- let trData;
774
- if (side === 'left' || side === 'right') {
775
- const width = this.mViewWidth[side];
776
- trData = this.computeTranslationData(side, width - position);
777
- this.mTranslationX[side] = width - position;
778
- }
779
- else {
780
- const height = this.mViewHeight[side];
781
- trData = this.computeTranslationData(side, height - position);
782
- this.mTranslationY[side] = height - position;
783
- }
784
- if (position !== 0) {
785
- this.mShowingSide = side;
786
- const drawer = this[side + 'Drawer'];
787
- if (drawer) {
788
- drawer.visibility = 'visible';
789
- }
790
- if (this.backDrop && trData.backDrop && trData.backDrop.opacity > 0 && this.backDrop.visibility !== 'visible') {
791
- this.backDrop.opacity = 0;
792
- this.backDrop.visibility = 'visible';
793
- }
794
- }
795
- else {
796
- this.mShowingSide = null;
797
- }
798
- // TODO: custom animation curve + apply curve on gesture
799
- const params = Object.keys(trData)
800
- .map((k) => this[k] &&
801
- Object.assign({
802
- target: this[k],
803
- curve: CoreTypes.AnimationCurve.easeInOut,
804
- duration
805
- }, duration ? transformAnimationValues(trData[k]) : trData[k]))
806
- .filter((a) => !!a);
807
- try {
808
- if (duration) {
809
- if (this.animationFunction) {
810
- await this.animationFunction(side, duration, trData, params, this);
811
- }
812
- await new Animation(params).play();
813
- }
814
- }
815
- catch (err) {
816
- console.error('animateToPosition', this, err, err.stack);
817
- throw err;
818
- }
819
- finally {
820
- // apply tr data to prevent hickups on iOS
821
- // and handle animation cancelled errors
822
- if ((position !== 0 && this.mShowingSide === side) || (position === 0 && !this.mShowingSide)) {
823
- this.applyTrData(trData, side);
824
- if (position !== 0) {
825
- // if (trData.backDrop) {
826
- // this.backDrop.opacity = 1;
827
- // }
828
- }
829
- else {
830
- const drawer = this[side + 'Drawer'];
831
- if (drawer) {
832
- drawer.visibility = 'hidden';
833
- }
834
- if (this.backDrop && trData.backDrop) {
835
- this.backDrop.visibility = 'hidden';
836
- }
837
- }
838
- }
839
- if (shouldSendEvent) {
840
- if (position === 0) {
841
- this.notify({ eventName: 'close', side, duration });
842
- }
843
- else {
844
- this.notify({ eventName: 'open', side, duration });
845
- }
846
- }
847
- }
848
- }
849
- isSideOpened() {
850
- return !!this.mShowingSide;
851
- }
852
- isOpened(side) {
853
- if (side) {
854
- return this.mShowingSide === side;
855
- }
856
- return !!this.mShowingSide;
857
- }
858
- getDefaultSide() {
859
- if (this.leftDrawer) {
860
- return 'left';
861
- }
862
- else if (this.rightDrawer) {
863
- return 'right';
864
- }
865
- else if (this.bottomDrawer) {
866
- return 'bottom';
867
- }
868
- else if (this.topDrawer) {
869
- return 'top';
870
- }
871
- return null;
872
- }
873
- async toggle(side) {
874
- side = this.getActualSide(side) || this.getDefaultSide();
875
- if (!side) {
876
- return;
877
- }
878
- if (this.isOpened(side)) {
879
- this.close(side);
880
- }
881
- else {
882
- this.open(side);
883
- }
884
- }
885
- async open(side, duration = this.openAnimationDuration) {
886
- side = this.getActualSide(side) || this.getDefaultSide();
887
- if (!side) {
888
- return;
889
- }
890
- if (this.mShowingSide && this.mShowingSide !== side) {
891
- this.close();
892
- }
893
- if (!this.isOpened(side)) {
894
- this.forceEnsureSize(side);
895
- }
896
- if (side === 'left' || side === 'right') {
897
- this.animateToPosition(side, this.mViewWidth[side], duration);
898
- }
899
- else {
900
- this.animateToPosition(side, this.mViewHeight[side], duration);
901
- }
902
- }
903
- async close(side, duration = this.closeAnimationDuration) {
904
- side = this.getActualSide(side) || this.mShowingSide;
905
- if (!side) {
906
- return;
907
- }
908
- // this.mShowingSide = null;
909
- return this.animateToPosition(side, 0, duration);
910
- }
911
- };
912
- Drawer = __decorate([
913
- CSSType('Drawer')
914
- ], Drawer);
915
- export { Drawer };
916
- export const mainContentProperty = new Property({
917
- name: 'mainContent',
918
- defaultValue: undefined,
919
- valueChanged: (target, oldValue, newValue) => {
920
- target._onMainContentChanged(oldValue, newValue);
921
- }
922
- });
923
- mainContentProperty.register(Drawer);
924
- backdropColorProperty.register(Drawer);
925
- leftDrawerContentProperty.register(Drawer);
926
- rightDrawerContentProperty.register(Drawer);
927
- topDrawerContentProperty.register(Drawer);
928
- bottomDrawerContentProperty.register(Drawer);
929
- gestureEnabledProperty.register(Drawer);
930
- leftDrawerModeProperty.register(Drawer);
931
- rightDrawerModeProperty.register(Drawer);
932
- bottomDrawerModeProperty.register(Drawer);
933
- topDrawerModeProperty.register(Drawer);
934
- translationFunctionProperty.register(Drawer);
935
- animationFunctionProperty.register(Drawer);
936
- backDropEnabledProperty.register(Drawer);
937
- startingSideProperty.register(Drawer);
938
- gestureHandlerOptionsProperty.register(Drawer);
939
- export function install() {
940
- installGestures();
941
- }
942
- //# sourceMappingURL=index.js.map