UI for Zipcoin Blue

item-reorder.js 10KB

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