UNPKG

@ionic/core

Version:
4 lines • 6.08 kB
/*! * (C) Ionic http://ionicframework.com - MIT License */ import{r as t,h as s,f as e,g as i,d as r}from"./p-BpRUsN_W.js";import{n as o,o as n}from"./p-BvabGprD.js";import{b as h}from"./p-SEecsUh7.js";import{a,g as c}from"./p-CXs4J83x.js";import{r as d}from"./p-Co7eql8D.js";import{b as l,a as f,h as p}from"./p-DPNVo1nr.js";import"./p-Dojwmvde.js";import"./p-C2LaBx3E.js";const m=class{constructor(s){t(this,s)}onClick(t){const s=this.el.closest("ion-reorder-group");t.preventDefault(),s&&s.disabled||t.stopImmediatePropagation()}render(){const t=h(this);return s(e,{key:"f278c473b4695a6a93565100e4fcaa7f9e3e9ff5",class:t},s("slot",{key:"cc26b5ed297f68a0bea82e27e34a7fe945186f4e"},s("ion-icon",{key:"696f5e4c2b09e64bd08abf0d0818b1554cd29737",icon:"ios"===t?o:n,lazy:!1,class:"reorder-icon",part:"icon","aria-hidden":"true"})))}get el(){return i(this)}};m.style={ios:":host([slot]){display:none;line-height:0;z-index:100}.reorder-icon{display:block}::slotted(ion-icon){font-size:dynamic-font(16px)}.reorder-icon{font-size:2.125rem;opacity:0.4}",md:":host([slot]){display:none;line-height:0;z-index:100}.reorder-icon{display:block}::slotted(ion-icon){font-size:dynamic-font(16px)}.reorder-icon{font-size:1.9375rem;opacity:0.3}"};const b=class{constructor(s){t(this,s),this.ionItemReorder=r(this,"ionItemReorder",7),this.ionReorderStart=r(this,"ionReorderStart",7),this.ionReorderMove=r(this,"ionReorderMove",7),this.ionReorderEnd=r(this,"ionReorderEnd",7),this.lastToIndex=-1,this.cachedHeights=[],this.scrollElTop=0,this.scrollElBottom=0,this.scrollElInitial=0,this.containerTop=0,this.containerBottom=0,this.state=0,this.disabled=!0}disabledChanged(){this.gesture&&this.gesture.enable(!this.disabled)}async connectedCallback(){const t=a(this.el);t&&(this.scrollEl=await c(t)),this.gesture=(await import("./p-D0YpjgON.js")).createGesture({el:this.el,gestureName:"reorder",gesturePriority:110,threshold:0,direction:"y",passive:!1,canStart:t=>this.canStart(t),onStart:t=>this.onStart(t),onMove:t=>this.onMove(t),onEnd:()=>this.onEnd()}),this.disabledChanged()}disconnectedCallback(){this.onEnd(),this.gesture&&(this.gesture.destroy(),this.gesture=void 0)}complete(t){return Promise.resolve(this.completeReorder(t))}canStart(t){if(this.selectedItemEl||0!==this.state)return!1;const s=t.event.target.closest("ion-reorder");if(!s)return!1;const e=g(s,this.el);return!!e&&(t.data=e,!0)}onStart(t){t.event.preventDefault();const s=this.selectedItemEl=t.data,e=this.cachedHeights;e.length=0;const i=this.el,r=i.__children||i.children;if(!r||0===r.length)return;let o=0;for(let t=0;t<r.length;t++){const s=r[t];o+=s.offsetHeight,e.push(o),s.$ionIndex=t}const n=i.getBoundingClientRect();if(this.containerTop=n.top,this.containerBottom=n.bottom,this.scrollEl){const t=this.scrollEl.getBoundingClientRect();this.scrollElInitial=this.scrollEl.scrollTop,this.scrollElTop=t.top+k,this.scrollElBottom=t.bottom-k}else this.scrollElInitial=0,this.scrollElTop=0,this.scrollElBottom=0;this.lastToIndex=u(s),this.selectedItemHeight=s.offsetHeight,this.state=1,s.classList.add(w),l(),this.ionReorderStart.emit()}onMove(t){const s=this.selectedItemEl;if(!s)return;const e=this.autoscroll(t.currentY),i=this.containerTop-e,r=Math.max(i,Math.min(t.currentY,this.containerBottom-e)),o=e+r-t.startY,n=this.lastToIndex,h=this.itemIndexForTop(r-i);if(h!==this.lastToIndex){const t=u(s);this.lastToIndex=h,f(),this.reorderMove(t,h)}s.style.transform=`translateY(${o}px)`,this.ionReorderMove.emit({from:n,to:h})}onEnd(){const t=this.selectedItemEl;if(this.state=2,!t)return void(this.state=0);const s=this.lastToIndex,e=u(t);s===e?this.completeReorder():this.ionItemReorder.emit({from:e,to:s,complete:this.completeReorder.bind(this)}),p(),this.ionReorderEnd.emit({from:e,to:s,complete:this.completeReorder.bind(this)})}completeReorder(t){const s=this.selectedItemEl;if(s&&2===this.state){const e=this.el.__children||this.el.children,i=e.length,r=this.lastToIndex,o=u(s);d((()=>{r===o||void 0!==t&&!0!==t||this.el.insertBefore(s,o<r?e[r+1]:e[r]);for(let t=0;t<i;t++)e[t].style.transform=""})),Array.isArray(t)&&(t=v(t,o,r)),s.style.transition="",s.classList.remove(w),this.selectedItemEl=void 0,this.state=0}return t}itemIndexForTop(t){const s=this.cachedHeights;for(let e=0;e<s.length;e++)if(s[e]>t)return e;return s.length-1}reorderMove(t,s){const e=this.selectedItemHeight,i=this.el.__children||this.el.children;for(let r=0;r<i.length;r++){let o="";r>t&&r<=s?o=`translateY(${-e}px)`:r<t&&r>=s&&(o=`translateY(${e}px)`),i[r].style.transform=o}}autoscroll(t){if(!this.scrollEl)return 0;let s=0;return t<this.scrollElTop?s=-y:t>this.scrollElBottom&&(s=y),0!==s&&this.scrollEl.scrollBy(0,s),this.scrollEl.scrollTop-this.scrollElInitial}render(){const t=h(this);return s(e,{key:"779332508f44409696b6bab80eda72f9b1d7869d",class:{[t]:!0,"reorder-enabled":!this.disabled,"reorder-list-active":0!==this.state}})}get el(){return i(this)}static get watchers(){return{disabled:[{disabledChanged:0}]}}},u=t=>t.$ionIndex,g=(t,s)=>{let e;for(;t;){if(e=t.parentElement,e===s)return t;t=e}},k=60,y=10,w="reorder-selected",v=(t,s,e)=>{const i=t[s];return t.splice(s,1),t.splice(e,0,i),t.slice()};b.style=".reorder-list-active>*{display:block;-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)}";export{m as ion_reorder,b as ion_reorder_group}