@ionic/core
Version:
Base components for Ionic
4 lines • 6.03 kB
JavaScript
/*!
* (C) Ionic http://ionicframework.com - MIT License
*/
import{r as t,h as s,d as r,g as i,c as e}from"./p-4DxY6_gG.js";import{j as o,k as n}from"./p-DV3sJJW8.js";import{b as h}from"./p-Br3vSlYh.js";import{f as a,g as c}from"./p-BhNEp2QP.js";import{r as d}from"./p-C-Cct-6D.js";import{b as l,a as m,h as p}from"./p-CKvCXMs9.js";import"./p-CIGNaXM1.js";import"./p-ZjP4CjeZ.js";const f=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(r,{key:"e6807bb349725682e99e791ac65e729a360d64e8",class:t},s("slot",{key:"1c691cdbffa6427ba08dc12184c69559ed5d5506"},s("ion-icon",{key:"8b4150302cdca475379582b2251737b5e74079b1",icon:"ios"===t?o:n,lazy:!1,class:"reorder-icon",part:"icon","aria-hidden":"true"})))}get el(){return i(this)}};f.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=e(this,"ionItemReorder",7),this.ionReorderStart=e(this,"ionReorderStart",7),this.ionReorderMove=e(this,"ionReorderMove",7),this.ionReorderEnd=e(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-Cl0B-RWe.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 r=g(s,this.el);return!!r&&(t.data=r,!0)}onStart(t){t.event.preventDefault();const s=this.selectedItemEl=t.data,r=this.cachedHeights;r.length=0;const i=this.el,e=i.__children;if(!e||0===e.length)return;let o=0;for(let t=0;t<e.length;t++){const s=e[t];o+=s.offsetHeight,r.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(v),l(),this.ionReorderStart.emit()}onMove(t){const s=this.selectedItemEl;if(!s)return;const r=this.autoscroll(t.currentY),i=this.containerTop-r,e=Math.max(i,Math.min(t.currentY,this.containerBottom-r)),o=r+e-t.startY,n=this.lastToIndex,h=this.itemIndexForTop(e-i);if(h!==this.lastToIndex){const t=u(s);this.lastToIndex=h,m(),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,r=u(t);s===r?this.completeReorder():this.ionItemReorder.emit({from:r,to:s,complete:this.completeReorder.bind(this)}),p(),this.ionReorderEnd.emit({from:r,to:s,complete:this.completeReorder.bind(this)})}completeReorder(t){const s=this.selectedItemEl;if(s&&2===this.state){const r=this.el.__children,i=r.length,e=this.lastToIndex,o=u(s);d((()=>{e===o||void 0!==t&&!0!==t||this.el.insertBefore(s,o<e?r[e+1]:r[e]);for(let t=0;t<i;t++)r[t].style.transform=""})),Array.isArray(t)&&(t=w(t,o,e)),s.style.transition="",s.classList.remove(v),this.selectedItemEl=void 0,this.state=0}return t}itemIndexForTop(t){const s=this.cachedHeights;for(let r=0;r<s.length;r++)if(s[r]>t)return r;return s.length-1}reorderMove(t,s){const r=this.selectedItemHeight,i=this.el.__children;for(let e=0;e<i.length;e++){let o="";e>t&&e<=s?o=`translateY(${-r}px)`:e<t&&e>=s&&(o=`translateY(${r}px)`),i[e].style.transform=o}}autoscroll(t){if(!this.scrollEl)return 0;let s=0;return t<this.scrollElTop?s=-10:t>this.scrollElBottom&&(s=y),0!==s&&this.scrollEl.scrollBy(0,s),this.scrollEl.scrollTop-this.scrollElInitial}render(){const t=h(this);return s(r,{key:"8996890164b63f4aa68779f26e4f308958582a76",class:{[t]:!0,"reorder-enabled":!this.disabled,"reorder-list-active":0!==this.state}})}get el(){return i(this)}static get watchers(){return{disabled:["disabledChanged"]}}},u=t=>t.$ionIndex,g=(t,s)=>{let r;for(;t;){if(r=t.parentElement,r===s)return t;t=r}},k=60,y=10,v="reorder-selected",w=(t,s,r)=>{const i=t[s];return t.splice(s,1),t.splice(r,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{f as ion_reorder,b as ion_reorder_group}