UNPKG

10.7 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, '__esModule', { value: true });
4
5const index = require('./index-a0a08b2a.js');
6const ionicGlobal = require('./ionic-global-06f21c1a.js');
7const haptic = require('./haptic-9f199ada.js');
8
9const reorderIosCss = ":host([slot]){display:none;line-height:0;z-index:100}.reorder-icon{display:block;font-size:22px}.reorder-icon{font-size:34px;opacity:0.4}";
10
11const reorderMdCss = ":host([slot]){display:none;line-height:0;z-index:100}.reorder-icon{display:block;font-size:22px}.reorder-icon{font-size:31px;opacity:0.3}";
12
13const Reorder = class {
14 constructor(hostRef) {
15 index.registerInstance(this, hostRef);
16 }
17 onClick(ev) {
18 const reorderGroup = this.el.closest('ion-reorder-group');
19 ev.preventDefault();
20 // Only stop event propagation if the reorder is inside of an enabled
21 // reorder group. This allows interaction with clickable children components.
22 if (!reorderGroup || !reorderGroup.disabled) {
23 ev.stopImmediatePropagation();
24 }
25 }
26 render() {
27 const mode = ionicGlobal.getIonMode(this);
28 const reorderIcon = mode === 'ios' ? 'reorder-three-outline' : 'reorder-two-sharp';
29 return (index.h(index.Host, { class: mode }, index.h("slot", null, index.h("ion-icon", { name: reorderIcon, lazy: false, class: "reorder-icon", part: "icon" }))));
30 }
31 get el() { return index.getElement(this); }
32};
33Reorder.style = {
34 ios: reorderIosCss,
35 md: reorderMdCss
36};
37
38const reorderGroupCss = ".reorder-list-active>*{-webkit-transition:-webkit-transform 300ms;transition:-webkit-transform 300ms;transition:transform 300ms;transition:transform 300ms, -webkit-transform 300ms;will-change:transform}.reorder-enabled{-webkit-user-select:none;-moz-user-select:none;-ms-user-select:none;user-select:none}.reorder-enabled ion-reorder{display:block;cursor:-webkit-grab;cursor:grab;pointer-events:all;-ms-touch-action:none;touch-action:none}.reorder-selected,.reorder-selected ion-reorder{cursor:-webkit-grabbing;cursor:grabbing}.reorder-selected{position:relative;-webkit-transition:none !important;transition:none !important;-webkit-box-shadow:0 0 10px rgba(0, 0, 0, 0.4);box-shadow:0 0 10px rgba(0, 0, 0, 0.4);opacity:0.8;z-index:100}.reorder-visible ion-reorder .reorder-icon{-webkit-transform:translate3d(0, 0, 0);transform:translate3d(0, 0, 0)}";
39
40const ReorderGroup = class {
41 constructor(hostRef) {
42 index.registerInstance(this, hostRef);
43 this.ionItemReorder = index.createEvent(this, "ionItemReorder", 7);
44 this.lastToIndex = -1;
45 this.cachedHeights = [];
46 this.scrollElTop = 0;
47 this.scrollElBottom = 0;
48 this.scrollElInitial = 0;
49 this.containerTop = 0;
50 this.containerBottom = 0;
51 this.state = 0 /* Idle */;
52 /**
53 * If `true`, the reorder will be hidden.
54 */
55 this.disabled = true;
56 }
57 disabledChanged() {
58 if (this.gesture) {
59 this.gesture.enable(!this.disabled);
60 }
61 }
62 async connectedCallback() {
63 const contentEl = this.el.closest('ion-content');
64 if (contentEl) {
65 this.scrollEl = await contentEl.getScrollElement();
66 }
67 this.gesture = (await Promise.resolve().then(function () { return require('./index-a1dd5c93.js'); })).createGesture({
68 el: this.el,
69 gestureName: 'reorder',
70 gesturePriority: 110,
71 threshold: 0,
72 direction: 'y',
73 passive: false,
74 canStart: detail => this.canStart(detail),
75 onStart: ev => this.onStart(ev),
76 onMove: ev => this.onMove(ev),
77 onEnd: () => this.onEnd(),
78 });
79 this.disabledChanged();
80 }
81 disconnectedCallback() {
82 this.onEnd();
83 if (this.gesture) {
84 this.gesture.destroy();
85 this.gesture = undefined;
86 }
87 }
88 /**
89 * Completes the reorder operation. Must be called by the `ionItemReorder` event.
90 *
91 * If a list of items is passed, the list will be reordered and returned in the
92 * proper order.
93 *
94 * If no parameters are passed or if `true` is passed in, the reorder will complete
95 * and the item will remain in the position it was dragged to. If `false` is passed,
96 * the reorder will complete and the item will bounce back to its original position.
97 *
98 * @param listOrReorder A list of items to be sorted and returned in the new order or a
99 * boolean of whether or not the reorder should reposition the item.
100 */
101 complete(listOrReorder) {
102 return Promise.resolve(this.completeSync(listOrReorder));
103 }
104 canStart(ev) {
105 if (this.selectedItemEl || this.state !== 0 /* Idle */) {
106 return false;
107 }
108 const target = ev.event.target;
109 const reorderEl = target.closest('ion-reorder');
110 if (!reorderEl) {
111 return false;
112 }
113 const item = findReorderItem(reorderEl, this.el);
114 if (!item) {
115 return false;
116 }
117 ev.data = item;
118 return true;
119 }
120 onStart(ev) {
121 ev.event.preventDefault();
122 const item = this.selectedItemEl = ev.data;
123 const heights = this.cachedHeights;
124 heights.length = 0;
125 const el = this.el;
126 const children = el.children;
127 if (!children || children.length === 0) {
128 return;
129 }
130 let sum = 0;
131 for (let i = 0; i < children.length; i++) {
132 const child = children[i];
133 sum += child.offsetHeight;
134 heights.push(sum);
135 child.$ionIndex = i;
136 }
137 const box = el.getBoundingClientRect();
138 this.containerTop = box.top;
139 this.containerBottom = box.bottom;
140 if (this.scrollEl) {
141 const scrollBox = this.scrollEl.getBoundingClientRect();
142 this.scrollElInitial = this.scrollEl.scrollTop;
143 this.scrollElTop = scrollBox.top + AUTO_SCROLL_MARGIN;
144 this.scrollElBottom = scrollBox.bottom - AUTO_SCROLL_MARGIN;
145 }
146 else {
147 this.scrollElInitial = 0;
148 this.scrollElTop = 0;
149 this.scrollElBottom = 0;
150 }
151 this.lastToIndex = indexForItem(item);
152 this.selectedItemHeight = item.offsetHeight;
153 this.state = 1 /* Active */;
154 item.classList.add(ITEM_REORDER_SELECTED);
155 haptic.hapticSelectionStart();
156 }
157 onMove(ev) {
158 const selectedItem = this.selectedItemEl;
159 if (!selectedItem) {
160 return;
161 }
162 // Scroll if we reach the scroll margins
163 const scroll = this.autoscroll(ev.currentY);
164 // // Get coordinate
165 const top = this.containerTop - scroll;
166 const bottom = this.containerBottom - scroll;
167 const currentY = Math.max(top, Math.min(ev.currentY, bottom));
168 const deltaY = scroll + currentY - ev.startY;
169 const normalizedY = currentY - top;
170 const toIndex = this.itemIndexForTop(normalizedY);
171 if (toIndex !== this.lastToIndex) {
172 const fromIndex = indexForItem(selectedItem);
173 this.lastToIndex = toIndex;
174 haptic.hapticSelectionChanged();
175 this.reorderMove(fromIndex, toIndex);
176 }
177 // Update selected item position
178 selectedItem.style.transform = `translateY(${deltaY}px)`;
179 }
180 onEnd() {
181 const selectedItemEl = this.selectedItemEl;
182 this.state = 2 /* Complete */;
183 if (!selectedItemEl) {
184 this.state = 0 /* Idle */;
185 return;
186 }
187 const toIndex = this.lastToIndex;
188 const fromIndex = indexForItem(selectedItemEl);
189 if (toIndex === fromIndex) {
190 this.completeSync();
191 }
192 else {
193 this.ionItemReorder.emit({
194 from: fromIndex,
195 to: toIndex,
196 complete: this.completeSync.bind(this)
197 });
198 }
199 haptic.hapticSelectionEnd();
200 }
201 completeSync(listOrReorder) {
202 const selectedItemEl = this.selectedItemEl;
203 if (selectedItemEl && this.state === 2 /* Complete */) {
204 const children = this.el.children;
205 const len = children.length;
206 const toIndex = this.lastToIndex;
207 const fromIndex = indexForItem(selectedItemEl);
208 if (toIndex !== fromIndex && (listOrReorder === undefined || listOrReorder === true)) {
209 const ref = (fromIndex < toIndex)
210 ? children[toIndex + 1]
211 : children[toIndex];
212 this.el.insertBefore(selectedItemEl, ref);
213 }
214 if (Array.isArray(listOrReorder)) {
215 listOrReorder = reorderArray(listOrReorder, fromIndex, toIndex);
216 }
217 for (let i = 0; i < len; i++) {
218 children[i].style['transform'] = '';
219 }
220 selectedItemEl.style.transition = '';
221 selectedItemEl.classList.remove(ITEM_REORDER_SELECTED);
222 this.selectedItemEl = undefined;
223 this.state = 0 /* Idle */;
224 }
225 return listOrReorder;
226 }
227 itemIndexForTop(deltaY) {
228 const heights = this.cachedHeights;
229 // TODO: since heights is a sorted array of integers, we can do
230 // speed up the search using binary search. Remember that linear-search is still
231 // faster than binary-search for small arrays (<64) due CPU branch misprediction.
232 for (let i = 0; i < heights.length; i++) {
233 if (heights[i] > deltaY) {
234 return i;
235 }
236 }
237 return heights.length - 1;
238 }
239 /********* DOM WRITE ********* */
240 reorderMove(fromIndex, toIndex) {
241 const itemHeight = this.selectedItemHeight;
242 const children = this.el.children;
243 for (let i = 0; i < children.length; i++) {
244 const style = children[i].style;
245 let value = '';
246 if (i > fromIndex && i <= toIndex) {
247 value = `translateY(${-itemHeight}px)`;
248 }
249 else if (i < fromIndex && i >= toIndex) {
250 value = `translateY(${itemHeight}px)`;
251 }
252 style['transform'] = value;
253 }
254 }
255 autoscroll(posY) {
256 if (!this.scrollEl) {
257 return 0;
258 }
259 let amount = 0;
260 if (posY < this.scrollElTop) {
261 amount = -SCROLL_JUMP;
262 }
263 else if (posY > this.scrollElBottom) {
264 amount = SCROLL_JUMP;
265 }
266 if (amount !== 0) {
267 this.scrollEl.scrollBy(0, amount);
268 }
269 return this.scrollEl.scrollTop - this.scrollElInitial;
270 }
271 render() {
272 const mode = ionicGlobal.getIonMode(this);
273 return (index.h(index.Host, { class: {
274 [mode]: true,
275 'reorder-enabled': !this.disabled,
276 'reorder-list-active': this.state !== 0 /* Idle */,
277 } }));
278 }
279 get el() { return index.getElement(this); }
280 static get watchers() { return {
281 "disabled": ["disabledChanged"]
282 }; }
283};
284const indexForItem = (element) => {
285 return element['$ionIndex'];
286};
287const findReorderItem = (node, container) => {
288 let parent;
289 while (node) {
290 parent = node.parentElement;
291 if (parent === container) {
292 return node;
293 }
294 node = parent;
295 }
296 return undefined;
297};
298const AUTO_SCROLL_MARGIN = 60;
299const SCROLL_JUMP = 10;
300const ITEM_REORDER_SELECTED = 'reorder-selected';
301const reorderArray = (array, from, to) => {
302 const element = array[from];
303 array.splice(from, 1);
304 array.splice(to, 0, element);
305 return array.slice();
306};
307ReorderGroup.style = reorderGroupCss;
308
309exports.ion_reorder = Reorder;
310exports.ion_reorder_group = ReorderGroup;