item-reorder.js 9.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. import { Directive, ElementRef, EventEmitter, Input, NgZone, Optional, Output, Renderer } from '@angular/core';
  2. import { Content } from '../content/content';
  3. import { DomController } from '../../platform/dom-controller';
  4. import { isTrueProperty, reorderArray } from '../../util/util';
  5. import { ItemReorderGesture } from './item-reorder-gesture';
  6. import { Platform } from '../../platform/platform';
  7. export class ReorderIndexes {
  8. constructor(from, to) {
  9. this.from = from;
  10. this.to = to;
  11. }
  12. applyTo(array) {
  13. reorderArray(array, this);
  14. }
  15. }
  16. /**
  17. * @name ItemReorder
  18. * @description
  19. * Item reorder adds the ability to change an item's order in a group.
  20. * It can be used within an `ion-list` or `ion-item-group` to provide a
  21. * visual drag and drop interface.
  22. *
  23. * ## Grouping Items
  24. *
  25. * All reorderable items must be grouped in the same element. If an item
  26. * should not be reordered, it shouldn't be included in this group. For
  27. * example, the following code works because the items are grouped in the
  28. * `<ion-list>`:
  29. *
  30. * ```html
  31. * <ion-list reorder="true">
  32. * <ion-item *ngFor="let item of items">{% raw %}{{ item }}{% endraw %}</ion-item>
  33. * </ion-list>
  34. * ```
  35. *
  36. * However, the below list includes a header that shouldn't be reordered:
  37. *
  38. * ```html
  39. * <ion-list reorder="true">
  40. * <ion-list-header>Header</ion-list-header>
  41. * <ion-item *ngFor="let item of items">{% raw %}{{ item }}{% endraw %}</ion-item>
  42. * </ion-list>
  43. * ```
  44. *
  45. * In order to mix different sets of items, `ion-item-group` should be used to
  46. * group the reorderable items:
  47. *
  48. * ```html
  49. * <ion-list>
  50. * <ion-list-header>Header</ion-list-header>
  51. * <ion-item-group reorder="true">
  52. * <ion-item *ngFor="let item of items">{% raw %}{{ item }}{% endraw %}</ion-item>
  53. * </ion-item-group>
  54. * </ion-list>
  55. * ```
  56. *
  57. * It's important to note that in this example, the `[reorder]` directive is applied to
  58. * the `<ion-item-group>` instead of the `<ion-list>`. This way makes it possible to
  59. * mix items that should and shouldn't be reordered.
  60. *
  61. *
  62. * ## Implementing the Reorder Function
  63. *
  64. * When the item is dragged and dropped into the new position, the `(ionItemReorder)` event is
  65. * emitted. This event provides the initial index (from) and the new index (to) of the reordered
  66. * item. For example, if the first item is dragged to the fifth position, the event will emit
  67. * `{from: 0, to: 4}`. Note that the index starts at zero.
  68. *
  69. * A function should be called when the event is emitted that handles the reordering of the items.
  70. * See [usage](#usage) below for some examples.
  71. *
  72. *
  73. * @usage
  74. *
  75. * ```html
  76. * <ion-list>
  77. * <ion-list-header>Header</ion-list-header>
  78. * <ion-item-group reorder="true" (ionItemReorder)="reorderItems($event)">
  79. * <ion-item *ngFor="let item of items">{% raw %}{{ item }}{% endraw %}</ion-item>
  80. * </ion-item-group>
  81. * </ion-list>
  82. * ```
  83. *
  84. * ```ts
  85. * class MyComponent {
  86. * items = [];
  87. *
  88. * constructor() {
  89. * for (let x = 0; x < 5; x++) {
  90. * this.items.push(x);
  91. * }
  92. * }
  93. *
  94. * reorderItems(indexes) {
  95. * let element = this.items[indexes.from];
  96. * this.items.splice(indexes.from, 1);
  97. * this.items.splice(indexes.to, 0, element);
  98. * }
  99. * }
  100. * ```
  101. *
  102. * Ionic also provides a helper function called `reorderArray` to
  103. * reorder the array of items. This can be used instead:
  104. *
  105. * ```ts
  106. * import { reorderArray } from 'ionic-angular';
  107. *
  108. * class MyComponent {
  109. * items = [];
  110. *
  111. * constructor() {
  112. * for (let x = 0; x < 5; x++) {
  113. * this.items.push(x);
  114. * }
  115. * }
  116. *
  117. * reorderItems(indexes) {
  118. * this.items = reorderArray(this.items, indexes);
  119. * }
  120. * }
  121. * ```
  122. * Alternatevely you can execute helper function inside template:
  123. *
  124. * ```html
  125. * <ion-list>
  126. * <ion-list-header>Header</ion-list-header>
  127. * <ion-item-group reorder="true" (ionItemReorder)="$event.applyTo(items)">
  128. * <ion-item *ngFor="let item of items">{% raw %}{{ item }}{% endraw %}</ion-item>
  129. * </ion-item-group>
  130. * </ion-list>
  131. * ```
  132. *
  133. * @demo /docs/demos/src/item-reorder/
  134. * @see {@link /docs/components#lists List Component Docs}
  135. * @see {@link ../../list/List List API Docs}
  136. * @see {@link ../Item Item API Docs}
  137. */
  138. export class ItemReorder {
  139. constructor(_plt, _dom, elementRef, _rendered, _zone, _content) {
  140. this._plt = _plt;
  141. this._dom = _dom;
  142. this._rendered = _rendered;
  143. this._zone = _zone;
  144. this._content = _content;
  145. this._enableReorder = false;
  146. this._visibleReorder = false;
  147. this._isStart = false;
  148. this._lastToIndex = -1;
  149. /**
  150. * @output {object} Emitted when the item is reordered. Emits an object
  151. * with `from` and `to` properties.
  152. */
  153. this.ionItemReorder = new EventEmitter();
  154. this._element = elementRef.nativeElement;
  155. }
  156. /**
  157. * @input {string} Which side of the view the ion-reorder should be placed. Default `"end"`.
  158. */
  159. set side(side) {
  160. this._isStart = side === 'start';
  161. }
  162. /**
  163. * @hidden
  164. */
  165. ngOnDestroy() {
  166. this._element = null;
  167. this._reorderGesture && this._reorderGesture.destroy();
  168. }
  169. /**
  170. * @hidden
  171. */
  172. get reorder() {
  173. return this._enableReorder;
  174. }
  175. set reorder(val) {
  176. let enabled = isTrueProperty(val);
  177. if (!enabled && this._reorderGesture) {
  178. this._reorderGesture.destroy();
  179. this._reorderGesture = null;
  180. this._visibleReorder = false;
  181. setTimeout(() => this._enableReorder = false, 400);
  182. }
  183. else if (enabled && !this._reorderGesture) {
  184. (void 0) /* console.debug */;
  185. this._reorderGesture = new ItemReorderGesture(this._plt, this);
  186. this._enableReorder = true;
  187. this._dom.write(() => {
  188. this._zone.run(() => {
  189. this._visibleReorder = true;
  190. });
  191. }, 16);
  192. }
  193. }
  194. _reorderPrepare() {
  195. let ele = this._element;
  196. let children = ele.children;
  197. for (let i = 0, ilen = children.length; i < ilen; i++) {
  198. var child = children[i];
  199. child.$ionIndex = i;
  200. child.$ionReorderList = ele;
  201. }
  202. }
  203. _reorderStart() {
  204. this.setElementClass('reorder-list-active', true);
  205. }
  206. _reorderEmit(fromIndex, toIndex) {
  207. this._reorderReset();
  208. if (fromIndex !== toIndex) {
  209. this._zone.run(() => {
  210. const indexes = new ReorderIndexes(fromIndex, toIndex);
  211. this.ionItemReorder.emit(indexes);
  212. });
  213. }
  214. }
  215. _scrollContent(scroll) {
  216. const scrollTop = this._content.scrollTop + scroll;
  217. if (scroll !== 0) {
  218. this._content.scrollTo(0, scrollTop, 0);
  219. }
  220. return scrollTop;
  221. }
  222. _reorderReset() {
  223. let children = this._element.children;
  224. let len = children.length;
  225. this.setElementClass('reorder-list-active', false);
  226. let transform = this._plt.Css.transform;
  227. for (let i = 0; i < len; i++) {
  228. children[i].style[transform] = '';
  229. }
  230. this._lastToIndex = -1;
  231. }
  232. _reorderMove(fromIndex, toIndex, itemHeight) {
  233. if (this._lastToIndex === -1) {
  234. this._lastToIndex = fromIndex;
  235. }
  236. let lastToIndex = this._lastToIndex;
  237. this._lastToIndex = toIndex;
  238. // TODO: I think both loops can be merged into a single one
  239. // but I had no luck last time I tried
  240. /********* DOM READ ********** */
  241. let children = this._element.children;
  242. /********* DOM WRITE ********* */
  243. let transform = this._plt.Css.transform;
  244. if (toIndex >= lastToIndex) {
  245. for (let i = lastToIndex; i <= toIndex; i++) {
  246. if (i !== fromIndex) {
  247. children[i].style[transform] = (i > fromIndex)
  248. ? `translateY(${-itemHeight}px)` : '';
  249. }
  250. }
  251. }
  252. if (toIndex <= lastToIndex) {
  253. for (let i = toIndex; i <= lastToIndex; i++) {
  254. if (i !== fromIndex) {
  255. children[i].style[transform] = (i < fromIndex)
  256. ? `translateY(${itemHeight}px)` : '';
  257. }
  258. }
  259. }
  260. }
  261. /**
  262. * @hidden
  263. */
  264. setElementClass(classname, add) {
  265. this._rendered.setElementClass(this._element, classname, add);
  266. }
  267. /**
  268. * @hidden
  269. */
  270. getNativeElement() {
  271. return this._element;
  272. }
  273. }
  274. ItemReorder.decorators = [
  275. { type: Directive, args: [{
  276. selector: 'ion-list[reorder],ion-item-group[reorder]',
  277. host: {
  278. '[class.reorder-enabled]': '_enableReorder',
  279. '[class.reorder-visible]': '_visibleReorder',
  280. '[class.reorder-side-start]': '_isStart'
  281. }
  282. },] },
  283. ];
  284. /** @nocollapse */
  285. ItemReorder.ctorParameters = () => [
  286. { type: Platform, },
  287. { type: DomController, },
  288. { type: ElementRef, },
  289. { type: Renderer, },
  290. { type: NgZone, },
  291. { type: Content, decorators: [{ type: Optional },] },
  292. ];
  293. ItemReorder.propDecorators = {
  294. 'ionItemReorder': [{ type: Output },],
  295. 'side': [{ type: Input, args: ['side',] },],
  296. 'reorder': [{ type: Input },],
  297. };
  298. //# sourceMappingURL=item-reorder.js.map