@studiometa/ui 0.2.11 → 0.2.12
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -156,7 +156,7 @@ var Slider = class extends import_js_toolkit.Base {
|
|
|
156
156
|
}
|
|
157
157
|
this.goTo(this.currentIndex - 1);
|
|
158
158
|
}
|
|
159
|
-
goTo(index) {
|
|
159
|
+
goTo(index, { withInstantMove = true } = {}) {
|
|
160
160
|
if (index < 0 || index > this.indexMax) {
|
|
161
161
|
throw new Error("Index out of bound.");
|
|
162
162
|
}
|
|
@@ -165,7 +165,7 @@ var Slider = class extends import_js_toolkit.Base {
|
|
|
165
165
|
const itemsToMove = this.getVisibleItems(state);
|
|
166
166
|
const invisibleItemsToMoveInstantly = this.getInvisibleItems(state);
|
|
167
167
|
itemsToMove.forEach((item) => {
|
|
168
|
-
if (currentState !== state) {
|
|
168
|
+
if (currentState !== state && withInstantMove) {
|
|
169
169
|
item.moveInstantly(currentState);
|
|
170
170
|
}
|
|
171
171
|
(0, import_utils.nextFrame)(() => item.move(state));
|
|
@@ -197,7 +197,7 @@ var Slider = class extends import_js_toolkit.Base {
|
|
|
197
197
|
const minimumDifference = Math.min(...absoluteDifferencesBetweenDistanceAndState);
|
|
198
198
|
const closestIndex = absoluteDifferencesBetweenDistanceAndState.findIndex((number) => number === minimumDifference);
|
|
199
199
|
if (this.$options.fitBounds) {
|
|
200
|
-
this.goTo(closestIndex);
|
|
200
|
+
this.goTo(closestIndex, { withInstantMove: false });
|
|
201
201
|
} else {
|
|
202
202
|
if (this.$options.contain) {
|
|
203
203
|
finalX = Math.min(this.containMinState, finalX);
|
|
@@ -182,9 +182,12 @@ export default class Slider extends Base {
|
|
|
182
182
|
*
|
|
183
183
|
* @this {SliderInterface}
|
|
184
184
|
* @param {number} index
|
|
185
|
+
* @param {{ withInstantMove?: boolean }} [options]
|
|
185
186
|
* @returns {void}
|
|
186
187
|
*/
|
|
187
|
-
goTo(this: SliderInterface, index: number
|
|
188
|
+
goTo(this: SliderInterface, index: number, { withInstantMove }?: {
|
|
189
|
+
withInstantMove?: boolean;
|
|
190
|
+
}): void;
|
|
188
191
|
/**
|
|
189
192
|
* Listen to the Draggable `start` event.
|
|
190
193
|
*
|
|
@@ -1 +1 @@
|
|
|
1
|
-
import{Base as d}from"@studiometa/js-toolkit";import{clamp as c,inertiaFinalValue as u,nextFrame as h,isDev as f}from"@studiometa/js-toolkit/utils";import x from"./SliderDrag.js";import g from"./SliderItem.js";class I extends d{static config={name:"Slider",refs:["wrapper","debug"],emits:["goto","index"],components:{SliderItem:g,SliderDrag:x},options:{mode:{type:String,default:"left"},fitBounds:Boolean,contain:Boolean,sensitivity:{type:Number,default:1},dropSensitivity:{type:Number,default:2}}};__distanceX=0;__initialX=0;__currentIndex=0;get currentIndex(){return this.__currentIndex}set currentIndex(
|
|
1
|
+
import{Base as d}from"@studiometa/js-toolkit";import{clamp as c,inertiaFinalValue as u,nextFrame as h,isDev as f}from"@studiometa/js-toolkit/utils";import x from"./SliderDrag.js";import g from"./SliderItem.js";class I extends d{static config={name:"Slider",refs:["wrapper","debug"],emits:["goto","index"],components:{SliderItem:g,SliderDrag:x},options:{mode:{type:String,default:"left"},fitBounds:Boolean,contain:Boolean,sensitivity:{type:Number,default:1},dropSensitivity:{type:Number,default:2}}};__distanceX=0;__initialX=0;__currentIndex=0;get currentIndex(){return this.__currentIndex}set currentIndex(t){this.currentSliderItem.disactivate(),this.$emit("index",t),this.__currentIndex=t,this.currentSliderItem.activate()}states=[];origins={left:0,center:0,right:0};hasFocus=!1;get currentState(){return this.states[this.currentIndex]}get firstState(){return this.states[0]}get lastState(){return this.states[this.states.length-1]}get containMinState(){return this.getStateValueByMode(this.firstState.x,"left")}get containMaxState(){return this.getStateValueByMode(this.lastState.x,"right")}get indexMax(){return this.$children.SliderItem.length-1}get currentSliderItem(){return this.$children.SliderItem[this.currentIndex]}getStates(){const{wrapper:t}=this.$refs,e=t.getBoundingClientRect();this.origins={left:e.left,center:e.x+e.width/2,right:e.x+e.width};const s=this.$children.SliderItem.map(i=>({x:{left:(i.rect.x-this.origins.left)*-1,center:(i.rect.x+i.rect.width/2-this.origins.center)*-1,right:(i.rect.x+i.rect.width-this.origins.right)*-1}}));if(this.$options.contain){const{mode:i}=this.$options;if(i==="left"){const n=this.$children.SliderItem.at(-1),a=s.find(r=>{const o=n.rect.x-this.origins.left+n.rect.width+r.x.left,l=e.width-o;return l>0?(r.x.left=Math.min(r.x.left+l,0),!0):!1});if(a)return s.map(r=>(r.x.left=Math.max(r.x.left,a.x.left),r))}if(i==="right"){const n=s.findIndex(r=>r.x.right<=0),a=n<0?s.at(-1):s[n-1];return s.map(r=>(r.x.right=n<0?a.x.right:Math.min(r.x.right,0),r))}i==="center"&&f&&console.warn(`[${this.$id}]`,"The `center` mode is not yet compatible with the `contain` mode.")}return s}getOriginByMode(t){return this.origins[t??this.$options.mode]}getStateValueByMode(t,e){return t[e??this.$options.mode]}mounted(){this.states=this.getStates(),this.prepareInvisibleItems(),this.goTo(this.currentIndex)}resized(){h(()=>{this.states=this.getStates(),h(()=>{this.prepareInvisibleItems(),this.goTo(this.currentIndex)})})}goNext(){this.currentIndex+1>this.indexMax||this.goTo(this.currentIndex+1)}goPrev(){this.currentIndex-1<0||this.goTo(this.currentIndex-1)}goTo(t,{withInstantMove:e=!0}={}){if(t<0||t>this.indexMax)throw new Error("Index out of bound.");const s=this.getStateValueByMode(this.currentState.x),i=this.getStateValueByMode(this.states[t].x),n=this.getVisibleItems(i),a=this.getInvisibleItems(i);n.forEach(r=>{s!==i&&e&&r.moveInstantly(s),h(()=>r.move(i))}),a.forEach(r=>{r.moveInstantly(i)}),this.currentIndex=t,this.$emit("goto",t)}onSliderDragStart(){this.__initialX=this.currentSliderItem?this.currentSliderItem.x:0}onSliderDragDrag(t){Math.abs(t.delta.y)>Math.abs(t.delta.x)||(this.__distanceX=this.__initialX+t.distance.x*this.$options.sensitivity,this.getVisibleItems(this.__distanceX).forEach(e=>{e.moveInstantly(this.__distanceX)}))}onSliderDragDrop(t){if(Math.abs(t.delta.y)>Math.abs(t.delta.x))return;let e=c(u(this.__distanceX,t.delta.x*this.$options.dropSensitivity),0,this.getStateValueByMode(this.lastState.x));const s=this.states.map(a=>Math.abs(e-this.getStateValueByMode(a.x))),i=Math.min(...s),n=s.findIndex(a=>a===i);this.$options.fitBounds?this.goTo(n,{withInstantMove:!1}):(this.$options.contain&&(e=Math.min(this.containMinState,e),e=Math.max(this.containMaxState,e)),this.$children.SliderItem.forEach(a=>{a.move(e)}),this.currentIndex=n)}onWrapperFocus(){this.hasFocus=!0}onWrapperBlur(){this.hasFocus=!1}keyed({LEFT:t,RIGHT:e,isDown:s}){this.hasFocus&&s&&(t?this.goPrev():e&&this.goNext())}prepareInvisibleItems(){const t=this.states[this.currentIndex],e=[],s=[];this.getInvisibleItems(this.getStateValueByMode(t.x)).forEach((i,n)=>{if(n>this.currentIndex){e.push(i);return}n<this.currentIndex&&s.push(i)}),e.forEach(i=>{const n=this.getStateWhereItemWillBeInvisible(i);n&&i.moveInstantly(this.getStateValueByMode(n.x))}),s.forEach(i=>{const n=this.getStateWhereItemWillBeInvisible(i,{reversed:!0});n&&i.moveInstantly(this.getStateValueByMode(n.x))})}getStateWhereItemWillBeInvisible(t,{reversed:e=!1}={}){const s=this.states.filter(o=>t.willBeVisible(this.getStateValueByMode(o.x))),i=s[0],n=s[s.length-1],a=this.states.findIndex(o=>this.getStateValueByMode(o.x)===this.getStateValueByMode(i.x)),r=this.states.findIndex(o=>o.x===n.x);return e?this.states[r+1]:this.states[a-1]}getVisibleItems(t){return this.$children.SliderItem.filter(e=>e.isVisible||e.willBeVisible(t))}getInvisibleItems(t){return this.$children.SliderItem.filter(e=>!e.isVisible&&!e.willBeVisible(t))}}export{I as default};
|