Files
FreeERP.Applet/miniprogram/miniprogram_npm/tdesign-miniprogram/picker-item/picker-item.js
2025-11-28 16:49:36 +08:00

1 line
6.0 KiB
JavaScript

import{__decorate}from"tslib";import{SuperComponent,wxComponent}from"../common/src/index";import config from"../common/config";import props from"./props";const{prefix:prefix}=config,name=`${prefix}-picker-item`,ANIMATION_DURATION=1e3,INERTIA_TIME=300,INERTIA_DISTANCE=15,VIRTUAL_SCROLL_CONFIG={ENABLE_THRESHOLD:100,BUFFER_COUNT:8,THROTTLE_TIME:16,FAST_SCROLL_BUFFER:12,FAST_SCROLL_THRESHOLD:50},range=function(t,e,i){return Math.min(Math.max(t,e),i)},momentum=(t,e)=>{let i=t;return i=Math.abs(i/e)/.005*(i<0?-1:1),i};let PickerItem=class extends SuperComponent{constructor(){super(...arguments),this.relations={"../picker/picker":{type:"parent",linked(t){if("keys"in t.data){const{keys:e}=t.data;if(null===e||JSON.stringify(this.data.pickerKeys)===JSON.stringify(e))return;this.setData({pickerKeys:Object.assign(Object.assign({},this.data.pickerKeys),e)})}}}},this.options={multipleSlots:!0},this.externalClasses=[`${prefix}-class`],this.properties=props,this.observers={"options, pickerKeys"(){this.update()}},this.data={prefix:prefix,classPrefix:name,offset:0,duration:0,value:"",curIndex:0,columnIndex:0,pickerKeys:{value:"value",label:"label",icon:"icon"},formatOptions:props.options.value,enableVirtualScroll:!1,visibleOptions:[],virtualStartIndex:0,virtualOffsetY:0,totalHeight:0,itemHeight:40,visibleItemCount:5,wrapperPaddingY:72},this.lifetimes={created(){this.StartY=0,this.StartOffset=0,this.startTime=0,this._moveTimer=null,this._animationTimer=null,this._lastOffset=0,this._lastMoveTime=0,this._scrollDirection=0},detached(){this._moveTimer&&(clearTimeout(this._moveTimer),this._moveTimer=null),this._animationTimer&&(clearInterval(this._animationTimer),this._animationTimer=null)}},this.methods={onClickItem(t){const{index:e}=t.currentTarget.dataset,{itemHeight:i}=this.data,a=range(e,0,this.getCount()-1);a!==this._selectedIndex&&this.setData({offset:-a*i,curIndex:a,duration:200}),this.updateSelected(a,!0)},onTouchStart(t){this.StartY=t.touches[0].clientY,this.StartOffset=this.data.offset,this.startTime=Date.now(),this.setData({duration:0})},onTouchMove(t){const{StartY:e,StartOffset:i}=this,{itemHeight:a,enableVirtualScroll:s}=this.data,n=Date.now(),o=t.touches[0].clientY-e,r=range(i+o,-this.getCount()*a,0),l=r-this._lastOffset,h=n-this._lastMoveTime||16,m=16*Math.abs(l/h);this._scrollDirection=l>0?1:l<0?-1:0;const d=m>VIRTUAL_SCROLL_CONFIG.FAST_SCROLL_THRESHOLD;this._moveTimer&&!d||(this._moveTimer&&(clearTimeout(this._moveTimer),this._moveTimer=null),this.setData({offset:r}),s&&this.updateVisibleOptions(r,d),this._moveTimer=setTimeout(()=>{this._moveTimer=null},VIRTUAL_SCROLL_CONFIG.THROTTLE_TIME)),this._lastOffset=r,this._lastMoveTime=n},onTouchEnd(t){this._moveTimer&&(clearTimeout(this._moveTimer),this._moveTimer=null);const{offset:e,itemHeight:i}=this.data,{startTime:a}=this;if(e===this.StartOffset)return;let s=0;const n=t.changedTouches[0].clientY-this.StartY,o=Date.now()-a;o<300&&Math.abs(n)>15&&(s=momentum(n,o));const r=range(e+s,-this.getCount()*i,0),l=range(Math.round(-r/i),0,this.getCount()-1),h=Math.abs(s)>3*i;if(this.data.enableVirtualScroll&&this.updateVisibleOptions(-l*i,h),this._animationTimer&&(clearInterval(this._animationTimer),this._animationTimer=null),this.data.enableVirtualScroll&&Math.abs(s)>0){const t=e,a=-l*i,s=Date.now();this._animationTimer=setInterval(()=>{const e=Date.now()-s,i=Math.min(e/1e3,1),n=1-Math.pow(1-i,3),o=t+(a-t)*n;this.updateVisibleOptions(o,h),i>=1&&(clearInterval(this._animationTimer),this._animationTimer=null)},16)}this.setData({offset:-l*i,duration:1e3,curIndex:l},()=>{this._animationTimer&&(clearInterval(this._animationTimer),this._animationTimer=null),this.data.enableVirtualScroll&&this.updateVisibleOptions(-l*i,!1)}),l!==this._selectedIndex&&this.updateSelected(l,!0)},formatOption:(t,e,i)=>"function"!=typeof i?t:t.map(t=>i(t,e)),updateSelected(t,e){var i,a,s;const{columnIndex:n,pickerKeys:o,formatOptions:r}=this.data;this._selectedIndex=t,this._selectedValue=null===(i=r[t])||void 0===i?void 0:i[null==o?void 0:o.value],this._selectedLabel=null===(a=r[t])||void 0===a?void 0:a[null==o?void 0:o.label],e&&(null===(s=this.$parent)||void 0===s||s.triggerColumnChange({index:t,column:n}))},update(){var t;const{options:e,value:i,pickerKeys:a,format:s,columnIndex:n,itemHeight:o,visibleItemCount:r}=this.data,l=this.formatOption(e,n,s),h=l.length,m=h>=VIRTUAL_SCROLL_CONFIG.ENABLE_THRESHOLD;let d=-1;if(h>500){d=null!==(t=new Map(l.map((t,e)=>[t[null==a?void 0:a.value],e])).get(i))&&void 0!==t?t:-1}else d=l.findIndex(t=>t[null==a?void 0:a.value]===i);const u=d>0?d:0,c={formatOptions:l,offset:-u*o,curIndex:u,enableVirtualScroll:m,totalHeight:h*o,wrapperPaddingY:(r-1)/2*o};if(m){const t=this.computeVirtualRange(-u*o,h,o);c.visibleOptions=l.slice(t.startIndex,t.endIndex),c.virtualStartIndex=t.startIndex,c.virtualOffsetY=t.startIndex*o}else c.visibleOptions=l,c.virtualStartIndex=0,c.virtualOffsetY=0;this.setData(c,()=>{this.updateSelected(u,!1)})},computeVirtualRange(t,e,i,a=!1){const s=Math.abs(t),{BUFFER_COUNT:n,FAST_SCROLL_BUFFER:o}=VIRTUAL_SCROLL_CONFIG,{visibleItemCount:r}=this.data,l=a?o:n,h=-1===this._scrollDirection?l+2:l,m=1===this._scrollDirection?l+2:l,d=Math.floor(s/i);return{startIndex:Math.max(0,d-h),endIndex:Math.min(e,d+r+m)}},updateVisibleOptions(t,e=!1){const{formatOptions:i,itemHeight:a,enableVirtualScroll:s}=this.data;if(!s)return;const n=void 0!==t?t:this.data.offset,o=this.computeVirtualRange(n,i.length,a,e);o.startIndex===this.data.virtualStartIndex&&o.endIndex===this.data.virtualStartIndex+this.data.visibleOptions.length||this.setData({visibleOptions:i.slice(o.startIndex,o.endIndex),virtualStartIndex:o.startIndex,virtualOffsetY:o.startIndex*a})},getCount(){var t,e;return null===(e=null===(t=this.data)||void 0===t?void 0:t.options)||void 0===e?void 0:e.length},getCurrentSelected(){var t,e;const{offset:i,itemHeight:a,formatOptions:s,pickerKeys:n}=this.data,o=Math.max(0,Math.min(Math.round(-i/a),this.getCount()-1));return{index:o,value:null===(t=s[o])||void 0===t?void 0:t[null==n?void 0:n.value],label:null===(e=s[o])||void 0===e?void 0:e[null==n?void 0:n.label]}}}}};PickerItem=__decorate([wxComponent()],PickerItem);export default PickerItem;