123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. import { ChangeDetectionStrategy, Component, ContentChild, ContentChildren, ElementRef, EventEmitter, NgZone, Optional, Output, Renderer, ViewEncapsulation, forwardRef } from '@angular/core';
  2. import { swipeShouldReset } from '../../util/util';
  3. import { Item } from './item';
  4. import { List } from '../list/list';
  5. import { Platform } from '../../platform/platform';
  6. import { ItemOptions } from './item-options';
  7. const SWIPE_MARGIN = 30;
  8. const ELASTIC_FACTOR = 0.55;
  9. const ITEM_SIDE_FLAG_NONE = 0;
  10. const ITEM_SIDE_FLAG_LEFT = 1 << 0;
  11. const ITEM_SIDE_FLAG_RIGHT = 1 << 1;
  12. const ITEM_SIDE_FLAG_BOTH = ITEM_SIDE_FLAG_LEFT | ITEM_SIDE_FLAG_RIGHT;
  13. /**
  14. * @name ItemSliding
  15. * @description
  16. * A sliding item is a list item that can be swiped to reveal buttons. It requires
  17. * an [Item](../Item) component as a child and a [List](../../list/List) component as
  18. * a parent. All buttons to reveal can be placed in the `<ion-item-options>` element.
  19. *
  20. * @usage
  21. * ```html
  22. * <ion-list>
  23. * <ion-item-sliding #item>
  24. * <ion-item>
  25. * Item
  26. * </ion-item>
  27. * <ion-item-options side="left">
  28. * <button ion-button (click)="favorite(item)">Favorite</button>
  29. * <button ion-button color="danger" (click)="share(item)">Share</button>
  30. * </ion-item-options>
  31. *
  32. * <ion-item-options side="right">
  33. * <button ion-button (click)="unread(item)">Unread</button>
  34. * </ion-item-options>
  35. * </ion-item-sliding>
  36. * </ion-list>
  37. * ```
  38. *
  39. * ### Swipe Direction
  40. * By default, the buttons are revealed when the sliding item is swiped from right to left,
  41. * so the buttons are placed in the right side. But it's also possible to reveal them
  42. * in the right side (sliding from left to right) by setting the `side` attribute
  43. * on the `ion-item-options` element. Up to 2 `ion-item-options` can used at the same time
  44. * in order to reveal two different sets of buttons depending the swipping direction.
  45. *
  46. * ```html
  47. * <ion-item-options side="right">
  48. * <button ion-button (click)="archive(item)">
  49. * <ion-icon name="archive"></ion-icon>
  50. * Archive
  51. * </button>
  52. * </ion-item-options>
  53. *
  54. * <ion-item-options side="left">
  55. * <button ion-button (click)="archive(item)">
  56. * <ion-icon name="archive"></ion-icon>
  57. * Archive
  58. * </button>
  59. * </ion-item-options>
  60. * ```
  61. *
  62. * ### Listening for events (ionDrag) and (ionSwipe)
  63. * It's possible to know the current relative position of the sliding item by subscribing
  64. * to the (ionDrag)` event.
  65. *
  66. * ```html
  67. * <ion-item-sliding (ionDrag)="logDrag($event)">
  68. * <ion-item>Item</ion-item>
  69. * <ion-item-options>
  70. * <button ion-button>Favorite</button>
  71. * </ion-item-options>
  72. * </ion-item-sliding>
  73. * ```
  74. *
  75. * ### Button Layout
  76. * If an icon is placed with text in the option button, by default it will
  77. * display the icon on top of the text. This can be changed to display the icon
  78. * to the left of the text by setting `icon-start` as an attribute on the
  79. * `<ion-item-options>` element.
  80. *
  81. * ```html
  82. * <ion-item-options icon-start>
  83. * <button ion-button (click)="archive(item)">
  84. * <ion-icon name="archive"></ion-icon>
  85. * Archive
  86. * </button>
  87. * </ion-item-options>
  88. *
  89. * ```
  90. *
  91. * ### Expandable Options
  92. *
  93. * Options can be expanded to take up the full width of the item if you swipe past
  94. * a certain point. This can be combined with the `ionSwipe` event to call methods
  95. * on the class.
  96. *
  97. * ```html
  98. *
  99. * <ion-item-sliding (ionSwipe)="delete(item)">
  100. * <ion-item>Item</ion-item>
  101. * <ion-item-options>
  102. * <button ion-button expandable (click)="delete(item)">Delete</button>
  103. * </ion-item-options>
  104. * </ion-item-sliding>
  105. * ```
  106. *
  107. * We can call `delete` by either clicking the button, or by doing a full swipe on the item.
  108. *
  109. * @demo /docs/demos/src/item-sliding/
  110. * @see {@link /docs/components#lists List Component Docs}
  111. * @see {@link ../Item Item API Docs}
  112. * @see {@link ../../list/List List API Docs}
  113. */
  114. export class ItemSliding {
  115. constructor(list, _plt, _renderer, _elementRef, _zone) {
  116. this._plt = _plt;
  117. this._renderer = _renderer;
  118. this._elementRef = _elementRef;
  119. this._zone = _zone;
  120. this._openAmount = 0;
  121. this._startX = 0;
  122. this._optsWidthRightSide = 0;
  123. this._optsWidthLeftSide = 0;
  124. this._tmr = null;
  125. this._optsDirty = true;
  126. this._state = 2 /* Disabled */;
  127. /**
  128. * @output {event} Emitted when the sliding position changes.
  129. * It reports the relative position.
  130. *
  131. * ```ts
  132. * ondrag(item) {
  133. * let percent = item.getSlidingPercent();
  134. * if (percent > 0) {
  135. * // positive
  136. * console.log('right side');
  137. * } else {
  138. * // negative
  139. * console.log('left side');
  140. * }
  141. * if (Math.abs(percent) > 1) {
  142. * console.log('overscroll');
  143. * }
  144. * }
  145. * ```
  146. *
  147. */
  148. this.ionDrag = new EventEmitter();
  149. list && list.containsSlidingItem(true);
  150. _elementRef.nativeElement.$ionComponent = this;
  151. this.setElementClass('item-wrapper', true);
  152. }
  153. set _itemOptions(itemOptions) {
  154. let sides = 0;
  155. // Reset left and right options in case they were removed
  156. this._leftOptions = this._rightOptions = null;
  157. for (var item of itemOptions.toArray()) {
  158. if (item.isRightSide()) {
  159. this._rightOptions = item;
  160. sides |= ITEM_SIDE_FLAG_RIGHT;
  161. }
  162. else {
  163. this._leftOptions = item;
  164. sides |= ITEM_SIDE_FLAG_LEFT;
  165. }
  166. }
  167. this._optsDirty = true;
  168. this._sides = sides;
  169. }
  170. /**
  171. * @hidden
  172. */
  173. getOpenAmount() {
  174. return this._openAmount;
  175. }
  176. /**
  177. * @hidden
  178. */
  179. getSlidingPercent() {
  180. const openAmount = this._openAmount;
  181. if (openAmount > 0) {
  182. return openAmount / this._optsWidthRightSide;
  183. }
  184. else if (openAmount < 0) {
  185. return openAmount / this._optsWidthLeftSide;
  186. }
  187. else {
  188. return 0;
  189. }
  190. }
  191. /**
  192. * @hidden
  193. */
  194. startSliding(startX) {
  195. if (this._tmr) {
  196. this._plt.cancelTimeout(this._tmr);
  197. this._tmr = null;
  198. }
  199. if (this._openAmount === 0) {
  200. this._optsDirty = true;
  201. this._setState(4 /* Enabled */);
  202. }
  203. this._startX = startX + this._openAmount;
  204. this.item.setElementStyle(this._plt.Css.transition, 'none');
  205. }
  206. /**
  207. * @hidden
  208. */
  209. moveSliding(x) {
  210. if (this._optsDirty) {
  211. this.calculateOptsWidth();
  212. return;
  213. }
  214. let openAmount = (this._startX - x);
  215. switch (this._sides) {
  216. case ITEM_SIDE_FLAG_RIGHT:
  217. openAmount = Math.max(0, openAmount);
  218. break;
  219. case ITEM_SIDE_FLAG_LEFT:
  220. openAmount = Math.min(0, openAmount);
  221. break;
  222. case ITEM_SIDE_FLAG_BOTH: break;
  223. case ITEM_SIDE_FLAG_NONE: return;
  224. default:
  225. (void 0) /* assert */;
  226. break;
  227. }
  228. if (openAmount > this._optsWidthRightSide) {
  229. const optsWidth = this._optsWidthRightSide;
  230. openAmount = optsWidth + (openAmount - optsWidth) * ELASTIC_FACTOR;
  231. }
  232. else if (openAmount < -this._optsWidthLeftSide) {
  233. const optsWidth = -this._optsWidthLeftSide;
  234. openAmount = optsWidth + (openAmount - optsWidth) * ELASTIC_FACTOR;
  235. }
  236. this._setOpenAmount(openAmount, false);
  237. return openAmount;
  238. }
  239. /**
  240. * @hidden
  241. */
  242. endSliding(velocity) {
  243. let restingPoint = (this._openAmount > 0)
  244. ? this._optsWidthRightSide
  245. : -this._optsWidthLeftSide;
  246. // Check if the drag didn't clear the buttons mid-point
  247. // and we aren't moving fast enough to swipe open
  248. const isResetDirection = (this._openAmount > 0) === !(velocity < 0);
  249. const isMovingFast = Math.abs(velocity) > 0.3;
  250. const isOnCloseZone = Math.abs(this._openAmount) < Math.abs(restingPoint / 2);
  251. if (swipeShouldReset(isResetDirection, isMovingFast, isOnCloseZone)) {
  252. restingPoint = 0;
  253. }
  254. this.fireSwipeEvent();
  255. this._setOpenAmount(restingPoint, true);
  256. return restingPoint;
  257. }
  258. /**
  259. * @hidden
  260. */
  261. fireSwipeEvent() {
  262. if (this._state & 32 /* SwipeRight */) {
  263. this._zone.run(() => this._rightOptions.ionSwipe.emit(this));
  264. }
  265. else if (this._state & 64 /* SwipeLeft */) {
  266. this._zone.run(() => this._leftOptions.ionSwipe.emit(this));
  267. }
  268. }
  269. /**
  270. * @hidden
  271. */
  272. calculateOptsWidth() {
  273. if (!this._optsDirty) {
  274. return;
  275. }
  276. this._optsWidthRightSide = 0;
  277. if (this._rightOptions) {
  278. this._optsWidthRightSide = this._rightOptions.width();
  279. (void 0) /* assert */;
  280. }
  281. this._optsWidthLeftSide = 0;
  282. if (this._leftOptions) {
  283. this._optsWidthLeftSide = this._leftOptions.width();
  284. (void 0) /* assert */;
  285. }
  286. this._optsDirty = false;
  287. }
  288. _setOpenAmount(openAmount, isFinal) {
  289. const platform = this._plt;
  290. if (this._tmr) {
  291. platform.cancelTimeout(this._tmr);
  292. this._tmr = null;
  293. }
  294. this._openAmount = openAmount;
  295. if (isFinal) {
  296. this.item.setElementStyle(platform.Css.transition, '');
  297. }
  298. if (openAmount > 0) {
  299. var state = (openAmount >= (this._optsWidthRightSide + SWIPE_MARGIN))
  300. ? 8 /* Right */ | 32 /* SwipeRight */
  301. : 8 /* Right */;
  302. this._setState(state);
  303. }
  304. else if (openAmount < 0) {
  305. const state = (openAmount <= (-this._optsWidthLeftSide - SWIPE_MARGIN))
  306. ? 16 /* Left */ | 64 /* SwipeLeft */
  307. : 16 /* Left */;
  308. this._setState(state);
  309. }
  310. else {
  311. (void 0) /* assert */;
  312. this._tmr = platform.timeout(() => {
  313. this._setState(2 /* Disabled */);
  314. this._tmr = null;
  315. }, 600);
  316. this.item.setElementStyle(platform.Css.transform, '');
  317. return;
  318. }
  319. this.item.setElementStyle(platform.Css.transform, `translate3d(${-openAmount}px,0,0)`);
  320. const ionDrag = this.ionDrag;
  321. if (ionDrag.observers.length > 0) {
  322. ionDrag.emit(this);
  323. }
  324. }
  325. _setState(state) {
  326. if (state === this._state) {
  327. return;
  328. }
  329. this.setElementClass('active-slide', (state !== 2 /* Disabled */));
  330. this.setElementClass('active-options-right', !!(state & 8 /* Right */));
  331. this.setElementClass('active-options-left', !!(state & 16 /* Left */));
  332. this.setElementClass('active-swipe-right', !!(state & 32 /* SwipeRight */));
  333. this.setElementClass('active-swipe-left', !!(state & 64 /* SwipeLeft */));
  334. this._state = state;
  335. }
  336. /**
  337. * Close the sliding item. Items can also be closed from the [List](../../list/List).
  338. *
  339. * The sliding item can be closed by grabbing a reference to `ItemSliding`. In the
  340. * below example, the template reference variable `slidingItem` is placed on the element
  341. * and passed to the `share` method.
  342. *
  343. * ```html
  344. * <ion-list>
  345. * <ion-item-sliding #slidingItem>
  346. * <ion-item>
  347. * Item
  348. * </ion-item>
  349. * <ion-item-options>
  350. * <button ion-button (click)="share(slidingItem)">Share</button>
  351. * </ion-item-options>
  352. * </ion-item-sliding>
  353. * </ion-list>
  354. * ```
  355. *
  356. * ```ts
  357. * import { Component } from '@angular/core';
  358. * import { ItemSliding } from 'ionic-angular';
  359. *
  360. * @Component({...})
  361. * export class MyClass {
  362. * constructor() { }
  363. *
  364. * share(slidingItem: ItemSliding) {
  365. * slidingItem.close();
  366. * }
  367. * }
  368. * ```
  369. */
  370. close() {
  371. this._setOpenAmount(0, true);
  372. }
  373. /**
  374. * @hidden
  375. */
  376. setElementClass(cssClass, shouldAdd) {
  377. this._renderer.setElementClass(this._elementRef.nativeElement, cssClass, shouldAdd);
  378. }
  379. }
  380. ItemSliding.decorators = [
  381. { type: Component, args: [{
  382. selector: 'ion-item-sliding',
  383. template: `
  384. <ng-content select="ion-item,[ion-item]"></ng-content>
  385. <ng-content select="ion-item-options"></ng-content>
  386. `,
  387. changeDetection: ChangeDetectionStrategy.OnPush,
  388. encapsulation: ViewEncapsulation.None
  389. },] },
  390. ];
  391. /** @nocollapse */
  392. ItemSliding.ctorParameters = () => [
  393. { type: List, decorators: [{ type: Optional },] },
  394. { type: Platform, },
  395. { type: Renderer, },
  396. { type: ElementRef, },
  397. { type: NgZone, },
  398. ];
  399. ItemSliding.propDecorators = {
  400. 'item': [{ type: ContentChild, args: [Item,] },],
  401. 'ionDrag': [{ type: Output },],
  402. '_itemOptions': [{ type: ContentChildren, args: [forwardRef(() => ItemOptions),] },],
  403. };
  404. //# sourceMappingURL=item-sliding.js.map