/
item-sliding.ts
484 lines (435 loc) · 13.7 KB
/
item-sliding.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
import { ChangeDetectionStrategy, Component, ContentChildren, ContentChild, Directive, ElementRef, EventEmitter, Input, Optional, Output, QueryList, Renderer, ViewEncapsulation } from '@angular/core';
import { CSS, nativeRaf, nativeTimeout, clearNativeTimeout } from '../../util/dom';
import { Item } from './item';
import { isPresent } from '../../util/util';
import { List } from '../list/list';
const SWIPE_MARGIN = 20;
const ELASTIC_FACTOR = 0.55;
export const enum ItemSideFlags {
None = 0,
Left = 1 << 0,
Right = 1 << 1,
Both = Left | Right
}
/**
* @name ItemOptions
* @description
* The option buttons for an `ion-item-sliding`. These buttons can be placed either on the left or right side.
* You can combind the `(ionSiwpe)` event plus the `expandable` directive to create a full swipe action for the item.
*
* @usage
*
* ```html
* <ion-item-sliding>
* <ion-item>
* Item 1
* </ion-item>
* <ion-item-options side="right" (ionSwipe)="saveItem(item)">
* <button expandable (click)="saveItem(item)">
* <ion-icon name="star"></ion-icon>
* </button>
* </ion-item-options>
* </ion-item-sliding>
*```
*/
@Directive({
selector: 'ion-item-options',
})
export class ItemOptions {
/**
* @input {string} the side the option button should be on. Defaults to right
* If you have multiple `ion-item-options`, a side must be provided for each.
*/
@Input() side: string;
/**
* @output {event} Expression to evaluate when the item has been fully swiped.
*/
@Output() ionSwipe: EventEmitter<ItemSliding> = new EventEmitter<ItemSliding>();
constructor(private _elementRef: ElementRef, private _renderer: Renderer) {}
/**
* @private
*/
setCssStyle(property: string, value: string) {
this._renderer.setElementStyle(this._elementRef.nativeElement, property, value);
}
/**
* @private
*/
getSides(): ItemSideFlags {
if (isPresent(this.side) && this.side === 'left') {
return ItemSideFlags.Left;
} else {
return ItemSideFlags.Right;
}
}
/**
* @private
*/
width() {
return this._elementRef.nativeElement.offsetWidth;
}
}
const enum SlidingState {
Disabled = 1 << 1,
Enabled = 1 << 2,
Right = 1 << 3,
Left = 1 << 4,
SwipeRight = 1 << 5,
SwipeLeft = 1 << 6,
}
/**
* @name ItemSliding
* @description
* A sliding item is a list item that can be swiped to reveal buttons. It requires
* an [Item](../Item) component as a child and a [List](../../list/List) component as
* a parent. All buttons to reveal can be placed in the `<ion-item-options>` element.
*
* @usage
* ```html
* <ion-list>
* <ion-item-sliding #item>
* <ion-item>
* Item
* </ion-item>
* <ion-item-options side="left">
* <button (click)="favorite(item)">Favorite</button>
* <button danger (click)="share(item)">Share</button>
* </ion-item-options>
* <ion-item-options side="right">
* <button (click)="unread(item)">Unread</button>
* </ion-item-options>
* </ion-item-sliding>
* </ion-list>
* ```
*
* ### Swipe Direction
* By default, the buttons are revealed when the sliding item is swiped from right to left,
* so the buttons are placed in the right side. But it's also possible to reveal them
* in the right side (sliding from left to right) by setting the `side` attribute
* on the `ion-item-options` element. Up to 2 `ion-item-options` can used at the same time
* in order to reveal two different sets of buttons depending the swipping direction.
*
* ```html
* <ion-item-options side="right">
* <button (click)="archive(item)">
* <ion-icon name="archive"></ion-icon>
* Archive
* </button>
* </ion-item-options>
* <ion-item-options side="left">
* <button (click)="archive(item)">
* <ion-icon name="archive"></ion-icon>
* Archive
* </button>
* </ion-item-options>
* ```
*
* ### Listening for events (ionDrag) and (ionSwipe)
* It's possible to know the current relative position of the sliding item by subscribing
* to the (ionDrag)` event.
*
* ```html
* <ion-item-sliding (ionDrag)="logDrag($event)">
* <ion-item>Item</ion-item>
* <ion-item-options>
* <button>Favorite</button>
* </ion-item-options>
* </ion-item-sliding>
* ```
*
* ### Button Layout
* If an icon is placed with text in the option button, by default it will
* display the icon on top of the text. This can be changed to display the icon
* to the left of the text by setting `icon-left` as an attribute on the
* `<ion-item-options>` element.
*
* ```html
* <ion-item-options icon-left>
* <button (click)="archive(item)">
* <ion-icon name="archive"></ion-icon>
* Archive
* </button>
* </ion-item-options>
*
* ```
*
*
* @demo /docs/v2/demos/item-sliding/
* @see {@link /docs/v2/components#lists List Component Docs}
* @see {@link ../Item Item API Docs}
* @see {@link ../../list/List List API Docs}
*/
@Component({
selector: 'ion-item-sliding',
template:
'<ng-content select="ion-item,[ion-item]"></ng-content>' +
'<ng-content select="ion-item-options"></ng-content>',
changeDetection: ChangeDetectionStrategy.OnPush,
encapsulation: ViewEncapsulation.None
})
export class ItemSliding {
private _openAmount: number = 0;
private _startX: number = 0;
private _optsWidthRightSide: number = 0;
private _optsWidthLeftSide: number = 0;
private _sides: ItemSideFlags;
private _timer: number = null;
private _leftOptions: ItemOptions;
private _rightOptions: ItemOptions;
private _optsDirty: boolean = true;
private _state: SlidingState = SlidingState.Disabled;
@ContentChild(Item) private item: Item;
/**
* @output {event} Expression to evaluate when the sliding position changes.
* It reports the relative position.
*
* ```ts
* ondrag(item) {
* let percent = item.getSlidingPercent();
* if (percent > 0) {
* // positive
* console.log('right side');
* } else {
* // negative
* console.log('left side');
* }
* if (Math.abs(percent) > 1) {
* console.log('overscroll');
* }
* }
* ```
*
*/
@Output() ionDrag: EventEmitter<ItemSliding> = new EventEmitter<ItemSliding>();
constructor( @Optional() list: List, private _renderer: Renderer, private _elementRef: ElementRef) {
list && list.containsSlidingItem(true);
_elementRef.nativeElement.$ionComponent = this;
this.setCssClass('item-wrapper', true);
}
@ContentChildren(ItemOptions)
private set _itemOptions(itemOptions: QueryList<ItemOptions>) {
let sides = 0;
for (var item of itemOptions.toArray()) {
var side = item.getSides();
if (side === ItemSideFlags.Left) {
this._leftOptions = item;
} else {
this._rightOptions = item;
}
sides |= item.getSides();
}
this._optsDirty = true;
this._sides = sides;
}
/**
* @private
*/
getOpenAmount(): number {
return this._openAmount;
}
/**
* @private
*/
getSlidingPercent(): number {
let openAmount = this._openAmount;
if (openAmount > 0) {
return openAmount / this._optsWidthRightSide;
} else if (openAmount < 0) {
return openAmount / this._optsWidthLeftSide;
} else {
return 0;
}
}
/**
* @private
*/
startSliding(startX: number) {
if (this._timer) {
clearNativeTimeout(this._timer);
this._timer = null;
}
if (this._openAmount === 0) {
this._optsDirty = true;
this._setState(SlidingState.Enabled);
}
this._startX = startX + this._openAmount;
this.item.setCssStyle(CSS.transition, 'none');
}
/**
* @private
*/
moveSliding(x: number): number {
if (this._optsDirty) {
this.calculateOptsWidth();
return;
}
let openAmount = (this._startX - x);
switch (this._sides) {
case ItemSideFlags.Right: openAmount = Math.max(0, openAmount); break;
case ItemSideFlags.Left: openAmount = Math.min(0, openAmount); break;
case ItemSideFlags.Both: break;
default: return;
}
if (openAmount > this._optsWidthRightSide) {
var optsWidth = this._optsWidthRightSide;
openAmount = optsWidth + (openAmount - optsWidth) * ELASTIC_FACTOR;
} else if (openAmount < -this._optsWidthLeftSide) {
var optsWidth = -this._optsWidthLeftSide;
openAmount = optsWidth + (openAmount - optsWidth) * ELASTIC_FACTOR;
}
this._setOpenAmount(openAmount, false);
return openAmount;
}
/**
* @private
*/
endSliding(velocity: number): number {
let restingPoint = (this._openAmount > 0)
? this._optsWidthRightSide
: -this._optsWidthLeftSide;
// Check if the drag didn't clear the buttons mid-point
// and we aren't moving fast enough to swipe open
let isCloseDirection = (this._openAmount > 0) === !(velocity < 0);
let isMovingFast = Math.abs(velocity) > 0.3;
let isOnCloseZone = Math.abs(this._openAmount) < Math.abs(restingPoint / 2);
if (shouldClose(isCloseDirection, isMovingFast, isOnCloseZone)) {
restingPoint = 0;
}
this._setOpenAmount(restingPoint, true);
this.fireSwipeEvent();
return restingPoint;
}
private fireSwipeEvent() {
if (this._state & SlidingState.SwipeRight) {
this._rightOptions.ionSwipe.emit(this);
} else if (this._state & SlidingState.SwipeLeft) {
this._leftOptions.ionSwipe.emit(this);
}
}
private calculateOptsWidth() {
nativeRaf(() => {
if (!this._optsDirty) {
return;
}
this._optsWidthRightSide = 0;
if (this._rightOptions) {
this._optsWidthRightSide = this._rightOptions.width();
}
this._optsWidthLeftSide = 0;
if (this._leftOptions) {
this._optsWidthLeftSide = this._leftOptions.width();
}
this._optsDirty = false;
});
}
private _setOpenAmount(openAmount: number, isFinal: boolean) {
if (this._timer) {
clearNativeTimeout(this._timer);
this._timer = null;
}
this._openAmount = openAmount;
if (isFinal) {
this.item.setCssStyle(CSS.transition, '');
} else {
if (openAmount > 0) {
let state = (openAmount >= (this._optsWidthRightSide + SWIPE_MARGIN))
? SlidingState.Right | SlidingState.SwipeRight
: SlidingState.Right;
this._setState(state);
} else if (openAmount < 0) {
let state = (openAmount <= (-this._optsWidthLeftSide - SWIPE_MARGIN))
? SlidingState.Left | SlidingState.SwipeLeft
: SlidingState.Left;
this._setState(state);
}
}
if (openAmount === 0) {
this._timer = nativeTimeout(() => {
this._setState(SlidingState.Disabled);
this._timer = null;
}, 600);
this.item.setCssStyle(CSS.transform, '');
return;
}
this.item.setCssStyle(CSS.transform, `translate3d(${-openAmount}px,0,0)`);
this.ionDrag.emit(this);
}
private _setState(state: SlidingState) {
if (state === this._state) {
return;
}
this.setCssClass('active-slide', (state !== SlidingState.Disabled));
this.setCssClass('active-options-right', !!(state & SlidingState.Right));
this.setCssClass('active-options-left', !!(state & SlidingState.Left));
this.setCssClass('active-swipe-right', !!(state & SlidingState.SwipeRight));
this.setCssClass('active-swipe-left', !!(state & SlidingState.SwipeLeft));
this._state = state;
}
/**
* Close the sliding item. Items can also be closed from the [List](../../list/List).
*
* The sliding item can be closed by grabbing a reference to `ItemSliding`. In the
* below example, the template reference variable `slidingItem` is placed on the element
* and passed to the `share` method.
*
* ```html
* <ion-list>
* <ion-item-sliding #slidingItem>
* <ion-item>
* Item
* </ion-item>
* <ion-item-options>
* <button (click)="share(slidingItem)">Share</button>
* </ion-item-options>
* </ion-item-sliding>
* </ion-list>
* ```
*
* ```ts
* import {Component} from '@angular/core';
* import {ItemSliding} from 'ionic-angular';
*
* @Component({...})
* export class MyClass {
* constructor() { }
*
* share(slidingItem: ItemSliding) {
* slidingItem.close();
* }
* }
* ```
*/
close() {
this._setOpenAmount(0, true);
}
/**
* @private
*/
setCssClass(cssClass: string, shouldAdd: boolean) {
this._renderer.setElementClass(this._elementRef.nativeElement, cssClass, shouldAdd);
}
/**
* @private
*/
setCssStyle(property: string, value: string) {
this._renderer.setElementStyle(this._elementRef.nativeElement, property, value);
}
}
function shouldClose(isCloseDirection: boolean, isMovingFast: boolean, isOnCloseZone: boolean): boolean {
// The logic required to know when the sliding item should close (openAmount=0)
// depends on three booleans (isCloseDirection, isMovingFast, isOnCloseZone)
// and it ended up being too complicated to be written manually without errors
// so the truth table is attached below: (0=false, 1=true)
// isCloseDirection | isMovingFast | isOnCloseZone || shouldClose
// 0 | 0 | 0 || 0
// 0 | 0 | 1 || 1
// 0 | 1 | 0 || 0
// 0 | 1 | 1 || 0
// 1 | 0 | 0 || 0
// 1 | 0 | 1 || 1
// 1 | 1 | 0 || 1
// 1 | 1 | 1 || 1
// The resulting expression was generated by resolving the K-map (Karnaugh map):
let shouldClose = (!isMovingFast && isOnCloseZone) || (isCloseDirection && isMovingFast);
return shouldClose;
}