@ysx-libs/mobile-picker
Version:
A mobile list picker plugin, use vanilla ts
855 lines (854 loc) • 31.9 kB
JavaScript
var __defProp = Object.defineProperty;
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
var __publicField = (obj, key, value) => {
__defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
return value;
};
var __accessCheck = (obj, member, msg) => {
if (!member.has(obj))
throw TypeError("Cannot " + msg);
};
var __privateGet = (obj, member, getter) => {
__accessCheck(obj, member, "read from private field");
return getter ? getter.call(obj) : member.get(obj);
};
var __privateAdd = (obj, member, value) => {
if (member.has(obj))
throw TypeError("Cannot add the same private member more than once");
member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
};
var __privateSet = (obj, member, value, setter) => {
__accessCheck(obj, member, "write to private field");
setter ? setter.call(obj, value) : member.set(obj, value);
return value;
};
var __privateMethod = (obj, member, method) => {
__accessCheck(obj, member, "access private method");
return method;
};
var _inTransition, _currentY, _ownDocument, _dragStartInfo, _moveEventController, _endEventController, _startEventController, _transitionEndEventController, _wheelEventController, _canPointer, _momentumConfig, _wheelEndTimer, _moveFunc, _innerClickToSelectConfig, _innerMouseWheelConfig, _innerScrollShape, _changeTrigger, _getClickToSelectConfig, getClickToSelectConfig_fn, _getMouseWheelConfig, getMouseWheelConfig_fn, _getScrollShape, getScrollShape_fn, _init, init_fn, _initEvents, initEvents_fn, _wheelHandler, wheelHandler_fn, _wheelEndDetector, wheelEndDetector_fn, _handleChange, handleChange_fn, _startHandler, startHandler_fn, _moveHandler, moveHandler_fn, _endHandler, endHandler_fn, _transitionEndHandler, transitionEndHandler_fn, _saveIndex, saveIndex_fn, _findNearestItem, findNearestItem_fn, _findYByIndex, findYByIndex_fn, _fixedBoundaryPosition, fixedBoundaryPosition_fn, _transitionDuration, transitionDuration_fn, _transitionY, transitionY_fn, _cleanup, cleanup_fn, _pickerViewInstances, _initPicker, initPicker_fn, _initPickerView, initPickerView_fn, _handleChange2, handleChange_fn2, _cleanup2, cleanup_fn2;
const TransitionDurationForFix = 500;
const TransitionDurationForWheel = 300;
const DefaultMomentumConfig = {
time: 300,
distance: 15,
duration: 2500,
deceleration: 15e-4
};
const DefaultClickToSelectConfig = {
duration: 300
};
const DefaultMouseWheelConfig = {
speed: 20,
discreteTime: 400
};
const DefaultOptions = {
selectedIndexes: [],
usePointerEvents: false,
scrollShape: "scale",
moveThreshold: 20,
momentum: DefaultMomentumConfig,
itemClassName: "mobile-picker-view-item",
clickToSelect: false,
mouseWheel: false,
onlyLeftButton: true
};
function userAgent(pattern) {
if (typeof window !== "undefined" && window.navigator) {
return !!/* @__PURE__ */ navigator.userAgent.match(pattern);
}
return false;
}
const Safari = userAgent(/safari/i) && !userAgent(/chrome/i) && !userAgent(/android/i);
const IOS = userAgent(/iP(ad|od|hone)/i);
const DragStartCls = "picker-drag-start";
const DraggingCls = "picker-dragging";
function clamp(value, min, max) {
return Math.min(Math.max(value, min), max);
}
function validSelectedIndex(value, itemCount) {
const numValue = value || 0;
return clamp(numValue, 0, itemCount);
}
function exceedBoundary(value, min, max) {
return value > max || value < min;
}
function momentum(current, start, timeDiff, options) {
const distance = current - start;
const speed = Math.abs(distance) / timeDiff;
const { deceleration } = options;
const destination = speed / deceleration * (distance < 0 ? -1 : 1);
return Math.round(destination);
}
const DefItemRotate = 15;
const DefItemScale = 0.1;
function flatItems(items) {
if (!(items == null ? void 0 : items.length))
return;
for (let index2 = 0; index2 < items.length; index2++) {
const item = items[index2];
item.style.transform = "none";
}
}
function scaleItems(y, itemHeight, items) {
if (!(items == null ? void 0 : items.length))
return;
for (let index2 = 0; index2 < items.length; index2++) {
const item = items[index2];
const scale = 1 - DefItemScale * Math.abs(y / itemHeight + index2);
item.style.transform = `scale(${scale.toFixed(2)})`;
}
}
function rotateItems(y, itemHeight, items) {
if (!(items == null ? void 0 : items.length))
return;
for (let index2 = 0; index2 < items.length; index2++) {
const item = items[index2];
const deg = DefItemRotate * (y / itemHeight + index2);
item.style.transform = `rotateX(${deg.toFixed(0)}deg)`;
}
}
const ScrollShapeStrategies = {
flat: null,
scale: scaleItems,
rotate: rotateItems
};
function easeOut(progress) {
return 1 - Math.pow(1 - progress, 3);
}
function requestMove({
startPoi,
duration,
destPoi,
onRunning,
onEnd
}) {
const startTime = performance.now();
let currentPoi = startPoi;
let id = -1;
let isCancelled = false;
const step = (timestamp) => {
if (isCancelled) {
return;
}
const elapsed = timestamp - startTime;
const progress = Math.min(elapsed / duration, 1);
const easedProgress = easeOut(progress);
currentPoi = startPoi + (destPoi - startPoi) * easedProgress;
onRunning(currentPoi);
if (progress < 1) {
id = requestAnimationFrame(step);
} else {
onEnd();
}
};
id = requestAnimationFrame(step);
const cancel = () => {
if (!isCancelled) {
isCancelled = true;
cancelAnimationFrame(id);
}
};
return {
cancel
};
}
function getClientCoordinateFromTouchList(touches) {
if (touches.length === 0) {
return void 0;
}
const touch = touches.item(0);
if (!touch) {
return void 0;
}
return {
clientX: touch.clientX,
clientY: touch.clientY
};
}
const getClientCoordinateFromEvent = (event) => {
if ("clientX" in event && "clientY" in event) {
return {
clientX: event.clientX,
clientY: event.clientY
};
}
if ("touches" in event) {
const touch = getClientCoordinateFromTouchList(event.touches);
if (touch) {
return touch;
}
}
if ("changedTouches" in event) {
const touch = getClientCoordinateFromTouchList(event.changedTouches);
if (touch) {
return touch;
}
}
return void 0;
};
const isFunction = (value) => typeof value === "function";
function closest(el, selector) {
let result = null;
while (el && !result) {
if ((el == null ? void 0 : el.matches) && el.matches(selector)) {
result = el;
}
el = el.parentNode;
}
return result;
}
function index(el, selector) {
let index2 = 0;
if (!el || !el.parentNode) {
return -1;
}
let preEl = el.previousElementSibling;
while (preEl) {
if (!selector || preEl.matches(selector)) {
index2++;
}
preEl = preEl.previousElementSibling;
}
return index2;
}
const defaultMeta = {
listContainerHeight: 0,
itemHeight: 0,
minScrollY: 0,
maxScrollY: 0,
limitMinScrollY: 0,
limitMaxScrollY: 0
};
class PickerView {
constructor(rootNode, options) {
__privateAdd(this, _getClickToSelectConfig);
__privateAdd(this, _getMouseWheelConfig);
__privateAdd(this, _getScrollShape);
__privateAdd(this, _init);
__privateAdd(this, _initEvents);
__privateAdd(this, _wheelHandler);
__privateAdd(this, _wheelEndDetector);
__privateAdd(this, _handleChange);
__privateAdd(this, _startHandler);
__privateAdd(this, _moveHandler);
__privateAdd(this, _endHandler);
__privateAdd(this, _transitionEndHandler);
__privateAdd(this, _saveIndex);
__privateAdd(this, _findNearestItem);
__privateAdd(this, _findYByIndex);
__privateAdd(this, _fixedBoundaryPosition);
__privateAdd(this, _transitionDuration);
__privateAdd(this, _transitionY);
__privateAdd(this, _cleanup);
__privateAdd(this, _inTransition, false);
__privateAdd(this, _currentY, 0);
__privateAdd(this, _ownDocument, null);
__publicField(this, "metaInfo", defaultMeta);
__privateAdd(this, _dragStartInfo, null);
__privateAdd(this, _moveEventController, null);
__privateAdd(this, _endEventController, null);
__privateAdd(this, _startEventController, null);
__privateAdd(this, _transitionEndEventController, null);
__privateAdd(this, _wheelEventController, null);
__privateAdd(this, _canPointer, void 0);
__privateAdd(this, _momentumConfig, void 0);
__publicField(this, "innerSelectedIndex", 0);
__privateAdd(this, _wheelEndTimer, -1);
__privateAdd(this, _moveFunc, null);
__privateAdd(this, _innerClickToSelectConfig, void 0);
__privateAdd(this, _innerMouseWheelConfig, void 0);
__privateAdd(this, _innerScrollShape, null);
__privateAdd(this, _changeTrigger, "");
this.rootNode = rootNode;
this.options = options;
__privateSet(this, _canPointer, options.usePointerEvents ? "PointerEvent" in window && (!Safari || IOS) : false);
__privateSet(this, _momentumConfig, options.momentum === false ? false : { ...DefaultMomentumConfig, ...options.momentum });
__privateSet(this, _innerClickToSelectConfig, __privateMethod(this, _getClickToSelectConfig, getClickToSelectConfig_fn).call(this, options.clickToSelect));
__privateSet(this, _innerMouseWheelConfig, __privateMethod(this, _getMouseWheelConfig, getMouseWheelConfig_fn).call(this, options.mouseWheel));
__privateMethod(this, _init, init_fn).call(this);
__privateMethod(this, _initEvents, initEvents_fn).call(this);
__privateSet(this, _changeTrigger, "init");
this.setIndex(this.options.selectedIndex);
const { items, itemHeight } = this.metaInfo;
if (items) {
__privateSet(this, _innerScrollShape, __privateMethod(this, _getScrollShape, getScrollShape_fn).call(this, options.scrollShape, items));
if (__privateGet(this, _innerScrollShape)) {
__privateGet(this, _innerScrollShape).call(this, __privateGet(this, _currentY), itemHeight, items);
} else {
flatItems(items);
}
}
}
setIndex(event, duration = 0) {
var _a;
if (!((_a = this.metaInfo.items) == null ? void 0 : _a.length))
return this.innerSelectedIndex;
__privateSet(this, _changeTrigger, __privateGet(this, _changeTrigger) || "api");
const validIndex = validSelectedIndex(
event,
this.metaInfo.items.length - 1
);
if (this.innerSelectedIndex !== validIndex) {
__privateMethod(this, _transitionDuration, transitionDuration_fn).call(this, { duration });
const y = __privateMethod(this, _findYByIndex, findYByIndex_fn).call(this, validIndex);
__privateMethod(this, _transitionY, transitionY_fn).call(this, y);
__privateMethod(this, _handleChange, handleChange_fn).call(this, validIndex);
}
return validIndex;
}
destroy() {
var _a, _b, _c, _d, _e;
__privateMethod(this, _cleanup, cleanup_fn).call(this);
(_a = __privateGet(this, _startEventController)) == null ? void 0 : _a.abort();
__privateSet(this, _startEventController, null);
(_b = __privateGet(this, _transitionEndEventController)) == null ? void 0 : _b.abort();
__privateSet(this, _transitionEndEventController, null);
(_c = __privateGet(this, _wheelEventController)) == null ? void 0 : _c.abort();
__privateSet(this, _wheelEventController, null);
__privateSet(this, _changeTrigger, "");
this.metaInfo = defaultMeta;
__privateMethod(this, _transitionDuration, transitionDuration_fn).call(this);
__privateMethod(this, _transitionY, transitionY_fn).call(this, 0);
(_e = (_d = __privateGet(this, _moveFunc)) == null ? void 0 : _d.cancel) == null ? void 0 : _e.call(_d);
__privateSet(this, _moveFunc, null);
}
}
_inTransition = new WeakMap();
_currentY = new WeakMap();
_ownDocument = new WeakMap();
_dragStartInfo = new WeakMap();
_moveEventController = new WeakMap();
_endEventController = new WeakMap();
_startEventController = new WeakMap();
_transitionEndEventController = new WeakMap();
_wheelEventController = new WeakMap();
_canPointer = new WeakMap();
_momentumConfig = new WeakMap();
_wheelEndTimer = new WeakMap();
_moveFunc = new WeakMap();
_innerClickToSelectConfig = new WeakMap();
_innerMouseWheelConfig = new WeakMap();
_innerScrollShape = new WeakMap();
_changeTrigger = new WeakMap();
_getClickToSelectConfig = new WeakSet();
getClickToSelectConfig_fn = function(data) {
if (data) {
return data === true ? {
...DefaultClickToSelectConfig,
enable: true
} : {
...DefaultClickToSelectConfig,
...data,
enable: true
};
}
return {
...DefaultClickToSelectConfig,
enable: false
};
};
_getMouseWheelConfig = new WeakSet();
getMouseWheelConfig_fn = function(data) {
if (data) {
return data === true ? {
...DefaultMouseWheelConfig,
enable: true
} : {
...DefaultMouseWheelConfig,
...data,
enable: true
};
}
return {
...DefaultMouseWheelConfig,
enable: false
};
};
_getScrollShape = new WeakSet();
getScrollShape_fn = function(scrollShape, items) {
let strategy = ScrollShapeStrategies.flat;
if ((items == null ? void 0 : items.length) > 3) {
strategy = isFunction(scrollShape) ? scrollShape : ScrollShapeStrategies[scrollShape];
}
return strategy;
};
_init = new WeakSet();
init_fn = function() {
const itemContainer = this.rootNode.firstElementChild;
const firstItem = itemContainer == null ? void 0 : itemContainer.firstElementChild;
if (__privateGet(this, _inTransition) || !firstItem)
return;
const maxScrollY = 0;
const minScrollY = -itemContainer.offsetHeight + firstItem.offsetHeight;
this.metaInfo = {
itemContainer,
items: itemContainer.children,
listContainerHeight: itemContainer.offsetHeight,
itemHeight: firstItem.offsetHeight,
maxScrollY,
minScrollY,
limitMinScrollY: minScrollY - this.options.moveThreshold,
limitMaxScrollY: maxScrollY + this.options.moveThreshold
};
};
_initEvents = new WeakSet();
initEvents_fn = function() {
var _a, _b, _c, _d, _e;
__privateSet(this, _startEventController, new AbortController());
if (__privateGet(this, _canPointer)) {
this.rootNode.addEventListener(
"pointerdown",
__privateMethod(this, _startHandler, startHandler_fn).bind(this),
{
signal: (_a = __privateGet(this, _startEventController)) == null ? void 0 : _a.signal
}
);
} else {
this.rootNode.addEventListener(
"touchstart",
__privateMethod(this, _startHandler, startHandler_fn).bind(this),
{
signal: (_b = __privateGet(this, _startEventController)) == null ? void 0 : _b.signal
}
);
this.rootNode.addEventListener(
"mousedown",
__privateMethod(this, _startHandler, startHandler_fn).bind(this),
{
signal: (_c = __privateGet(this, _startEventController)) == null ? void 0 : _c.signal
}
);
}
__privateSet(this, _transitionEndEventController, new AbortController());
this.metaInfo.itemContainer.addEventListener(
"transitionend",
__privateMethod(this, _transitionEndHandler, transitionEndHandler_fn).bind(this),
{
signal: (_d = __privateGet(this, _transitionEndEventController)) == null ? void 0 : _d.signal
}
);
const { enable, duration } = __privateGet(this, _innerClickToSelectConfig);
if (enable) {
const { itemClassName } = this.options;
this.rootNode.addEventListener("click", (event) => {
if (__privateGet(this, _inTransition))
return;
const itemNode = closest(
event.target,
"." + itemClassName
);
if (itemNode) {
const i = index(itemNode, "." + itemClassName);
__privateSet(this, _changeTrigger, "click");
this.setIndex(i, duration);
}
});
}
if (__privateGet(this, _innerMouseWheelConfig).enable) {
__privateSet(this, _wheelEventController, new AbortController());
this.rootNode.addEventListener(
"wheel",
(event) => {
event.preventDefault();
event.stopPropagation();
__privateMethod(this, _wheelHandler, wheelHandler_fn).call(this, event);
},
{
signal: (_e = __privateGet(this, _wheelEventController)) == null ? void 0 : _e.signal
}
);
}
};
_wheelHandler = new WeakSet();
wheelHandler_fn = function(event) {
const { deltaY, deltaMode } = event;
let wheelY = 0;
if (deltaMode === 1) {
wheelY = -deltaY * __privateGet(this, _innerMouseWheelConfig).speed;
} else {
wheelY = -deltaY;
}
const { limitMinScrollY, limitMaxScrollY, minScrollY, maxScrollY } = this.metaInfo;
let newY = __privateGet(this, _currentY) + wheelY;
if (exceedBoundary(newY, minScrollY, maxScrollY)) {
newY = clamp(
__privateGet(this, _currentY) + wheelY / 3,
limitMinScrollY,
limitMaxScrollY
);
}
__privateMethod(this, _transitionDuration, transitionDuration_fn).call(this, { duration: TransitionDurationForWheel });
__privateMethod(this, _transitionY, transitionY_fn).call(this, newY);
__privateMethod(this, _wheelEndDetector, wheelEndDetector_fn).call(this);
};
_wheelEndDetector = new WeakSet();
wheelEndDetector_fn = function() {
window.clearTimeout(__privateGet(this, _wheelEndTimer));
__privateSet(this, _wheelEndTimer, window.setTimeout(() => {
const { index: index2, y } = __privateMethod(this, _findNearestItem, findNearestItem_fn).call(this, __privateGet(this, _currentY));
const { limitMinScrollY, limitMaxScrollY } = this.metaInfo;
const newY = clamp(y, limitMinScrollY, limitMaxScrollY);
__privateMethod(this, _transitionDuration, transitionDuration_fn).call(this, { duration: TransitionDurationForFix });
__privateMethod(this, _transitionY, transitionY_fn).call(this, newY);
__privateSet(this, _changeTrigger, "wheel");
__privateMethod(this, _handleChange, handleChange_fn).call(this, index2);
}, __privateGet(this, _innerMouseWheelConfig).discreteTime));
};
_handleChange = new WeakSet();
handleChange_fn = function(index2) {
__privateMethod(this, _saveIndex, saveIndex_fn).call(this, index2);
this.options.onChange(index2, __privateGet(this, _changeTrigger));
};
_startHandler = new WeakSet();
startHandler_fn = function(event) {
var _a, _b;
const clientCoordinate = getClientCoordinateFromEvent(event);
if (__privateGet(this, _dragStartInfo) || !clientCoordinate) {
return;
}
if (this.options.onlyLeftButton && event.type === "mousedown" && event.button !== 0) {
return;
}
__privateMethod(this, _transitionDuration, transitionDuration_fn).call(this);
(_b = (_a = __privateGet(this, _moveFunc)) == null ? void 0 : _a.cancel) == null ? void 0 : _b.call(_a);
__privateSet(this, _moveFunc, null);
this.rootNode.classList.add(DragStartCls);
__privateSet(this, _dragStartInfo, {
time: Date.now(),
startY: clientCoordinate.clientY,
pointY: clientCoordinate.clientY
});
const target = event.target;
__privateSet(this, _ownDocument, target.ownerDocument);
__privateSet(this, _moveEventController, new AbortController());
__privateSet(this, _endEventController, new AbortController());
if (__privateGet(this, _canPointer)) {
this.rootNode.setPointerCapture(event.pointerId);
__privateGet(this, _ownDocument).addEventListener(
"pointermove",
__privateMethod(this, _moveHandler, moveHandler_fn).bind(this),
{
signal: __privateGet(this, _moveEventController).signal
}
);
__privateGet(this, _ownDocument).addEventListener(
"pointerup",
__privateMethod(this, _endHandler, endHandler_fn).bind(this),
{
signal: __privateGet(this, _endEventController).signal
}
);
__privateGet(this, _ownDocument).addEventListener(
"pointercancel",
__privateMethod(this, _endHandler, endHandler_fn).bind(this),
{
signal: __privateGet(this, _endEventController).signal
}
);
} else {
__privateGet(this, _ownDocument).addEventListener(
"mousemove",
__privateMethod(this, _moveHandler, moveHandler_fn).bind(this),
{
signal: __privateGet(this, _moveEventController).signal
}
);
__privateGet(this, _ownDocument).addEventListener(
"touchmove",
__privateMethod(this, _moveHandler, moveHandler_fn).bind(this),
{
signal: __privateGet(this, _moveEventController).signal
}
);
__privateGet(this, _ownDocument).addEventListener(
"mouseup",
__privateMethod(this, _endHandler, endHandler_fn).bind(this),
{
signal: __privateGet(this, _endEventController).signal
}
);
__privateGet(this, _ownDocument).addEventListener(
"touchend",
__privateMethod(this, _endHandler, endHandler_fn).bind(this),
{
signal: __privateGet(this, _endEventController).signal
}
);
__privateGet(this, _ownDocument).addEventListener(
"touchcancel",
__privateMethod(this, _endHandler, endHandler_fn).bind(this),
{
signal: __privateGet(this, _endEventController).signal
}
);
}
};
_moveHandler = new WeakSet();
moveHandler_fn = function(event) {
const clientCoordinate = getClientCoordinateFromEvent(event);
if (!this.rootNode || !__privateGet(this, _dragStartInfo) || !clientCoordinate)
return;
this.rootNode.classList.remove(DragStartCls);
this.rootNode.classList.add(DraggingCls);
const { pointY } = __privateGet(this, _dragStartInfo);
const deltaY = clientCoordinate.clientY - pointY;
__privateGet(this, _dragStartInfo).pointY = clientCoordinate.clientY;
const { minScrollY, maxScrollY, limitMinScrollY, limitMaxScrollY } = this.metaInfo;
let newY = __privateGet(this, _currentY) + deltaY;
if (exceedBoundary(newY, minScrollY, maxScrollY)) {
newY = clamp(
__privateGet(this, _currentY) + deltaY / 3,
limitMinScrollY,
limitMaxScrollY
);
}
__privateMethod(this, _transitionY, transitionY_fn).call(this, newY);
};
_endHandler = new WeakSet();
endHandler_fn = function(event) {
if (!this.rootNode || !__privateGet(this, _dragStartInfo))
return;
if (__privateGet(this, _canPointer)) {
this.rootNode.releasePointerCapture(event.pointerId);
}
let selectedIndex = 0;
let duration = TransitionDurationForFix;
let newY = __privateGet(this, _currentY);
const { limitMinScrollY, limitMaxScrollY, minScrollY, maxScrollY } = this.metaInfo;
const fixedY = __privateMethod(this, _fixedBoundaryPosition, fixedBoundaryPosition_fn).call(this);
let isMoment = false;
if (fixedY) {
newY = fixedY;
} else {
const clientCoordinate = getClientCoordinateFromEvent(event);
const { time, startY, pointY } = __privateGet(this, _dragStartInfo);
const endTime = Date.now();
const timeDiff = endTime - time;
const absDistY = Math.abs(Math.round(clientCoordinate.clientY) - startY);
if (__privateGet(this, _momentumConfig) && timeDiff < __privateGet(this, _momentumConfig).time && absDistY > __privateGet(this, _momentumConfig).distance) {
isMoment = true;
const destination = momentum(
pointY,
startY,
timeDiff,
__privateGet(this, _momentumConfig)
);
duration = __privateGet(this, _momentumConfig).duration;
newY = __privateGet(this, _currentY) + destination;
if (exceedBoundary(newY, minScrollY, maxScrollY)) {
duration = TransitionDurationForFix;
}
}
}
const { index: index2, y } = __privateMethod(this, _findNearestItem, findNearestItem_fn).call(this, newY);
newY = y;
selectedIndex = index2;
if (newY !== __privateGet(this, _currentY)) {
__privateSet(this, _changeTrigger, "drag");
newY = clamp(newY, limitMinScrollY, limitMaxScrollY);
if (isMoment) {
__privateMethod(this, _transitionDuration, transitionDuration_fn).call(this, { duration, setProp: false });
__privateSet(this, _moveFunc, requestMove({
startPoi: __privateGet(this, _currentY),
duration,
destPoi: newY,
onRunning: (y2) => {
__privateMethod(this, _transitionY, transitionY_fn).call(this, y2);
},
onEnd: () => {
__privateMethod(this, _transitionY, transitionY_fn).call(this, y);
}
}));
} else {
__privateMethod(this, _transitionDuration, transitionDuration_fn).call(this, { duration });
__privateMethod(this, _transitionY, transitionY_fn).call(this, newY);
}
__privateMethod(this, _handleChange, handleChange_fn).call(this, selectedIndex);
}
__privateMethod(this, _cleanup, cleanup_fn).call(this);
};
_transitionEndHandler = new WeakSet();
transitionEndHandler_fn = function() {
const fixedY = __privateMethod(this, _fixedBoundaryPosition, fixedBoundaryPosition_fn).call(this);
if (fixedY === null) {
__privateMethod(this, _transitionDuration, transitionDuration_fn).call(this);
} else {
__privateMethod(this, _transitionDuration, transitionDuration_fn).call(this, { duration: TransitionDurationForFix });
__privateMethod(this, _transitionY, transitionY_fn).call(this, fixedY);
}
};
_saveIndex = new WeakSet();
saveIndex_fn = function(event) {
this.innerSelectedIndex = event;
};
_findNearestItem = new WeakSet();
findNearestItem_fn = function(event) {
var _a;
if (!((_a = this.metaInfo.items) == null ? void 0 : _a.length))
return { index: -1, y: 0 };
const { itemHeight, items } = this.metaInfo;
let index2 = -1;
let newY = 0;
const validEvent = Math.min(0, event);
index2 = clamp(
Math.round(Math.abs(validEvent) / itemHeight),
0,
items.length - 1
);
newY = index2 * itemHeight * (validEvent / (Math.abs(validEvent) || 1));
return {
index: index2,
y: newY
};
};
_findYByIndex = new WeakSet();
findYByIndex_fn = function(index2) {
var _a;
if (!((_a = this.metaInfo.items) == null ? void 0 : _a.length))
return 0;
const { itemHeight } = this.metaInfo;
return -index2 * itemHeight;
};
_fixedBoundaryPosition = new WeakSet();
fixedBoundaryPosition_fn = function() {
let result = null;
const { minScrollY, maxScrollY } = this.metaInfo;
if (exceedBoundary(__privateGet(this, _currentY), minScrollY, maxScrollY)) {
result = __privateGet(this, _currentY) > maxScrollY ? maxScrollY : minScrollY;
}
return result;
};
_transitionDuration = new WeakSet();
transitionDuration_fn = function(event) {
const duration = (event == null ? void 0 : event.duration) || 0;
const setProp = (event == null ? void 0 : event.setProp) === false ? false : true;
__privateSet(this, _inTransition, duration > 0);
if (setProp) {
this.rootNode.style.setProperty(
"--picker-transit-duration",
`${duration}ms`
);
}
};
_transitionY = new WeakSet();
transitionY_fn = function(y) {
if (!this.rootNode || __privateGet(this, _currentY) === y)
return;
this.rootNode.style.setProperty("--picker-transit-y", `${y}px`);
__privateSet(this, _currentY, y);
const { items, itemHeight } = this.metaInfo;
if (__privateGet(this, _innerScrollShape)) {
__privateGet(this, _innerScrollShape).call(this, y, itemHeight, items);
}
};
_cleanup = new WeakSet();
cleanup_fn = function() {
var _a, _b;
this.rootNode.classList.remove(DragStartCls);
this.rootNode.classList.remove(DraggingCls);
__privateSet(this, _dragStartInfo, null);
(_a = __privateGet(this, _moveEventController)) == null ? void 0 : _a.abort();
__privateSet(this, _moveEventController, null);
(_b = __privateGet(this, _endEventController)) == null ? void 0 : _b.abort();
__privateSet(this, _endEventController, null);
};
class Picker {
constructor(root, options) {
__privateAdd(this, _initPicker);
__privateAdd(this, _initPickerView);
__privateAdd(this, _handleChange2);
__privateAdd(this, _cleanup2);
__publicField(this, "rootNode");
__publicField(this, "innerOptions");
__privateAdd(this, _pickerViewInstances, []);
this.rootNode = typeof root === "string" ? document.querySelector(root) : root;
if (!this.rootNode) {
throw new Error("root node not found");
}
this.innerOptions = {
...DefaultOptions,
...options
};
__privateMethod(this, _initPicker, initPicker_fn).call(this);
}
setIndexes(event) {
this.innerOptions.selectedIndexes = event;
const validIndexes = [];
event.forEach((item, index2) => {
if (__privateGet(this, _pickerViewInstances)[index2]) {
validIndexes.push(__privateGet(this, _pickerViewInstances)[index2].setIndex(item));
}
});
return validIndexes;
}
refreshAll(value) {
const viewNodes = this.rootNode.querySelectorAll(".mobile-picker-view");
if (viewNodes.length) {
this.innerOptions.selectedIndexes.length = viewNodes.length;
__privateGet(this, _pickerViewInstances).forEach((item) => item.destroy());
__privateGet(this, _pickerViewInstances).length = viewNodes.length;
return this.refreshColumns(__privateGet(this, _pickerViewInstances).map((_, index2) => index2), value);
}
return __privateMethod(this, _cleanup2, cleanup_fn2).call(this);
}
refreshColumns(indexes, value) {
const viewNodes = this.rootNode.querySelectorAll(".mobile-picker-view");
if (viewNodes.length) {
indexes.forEach((index2) => {
const viewNode = viewNodes[index2];
if (viewNode && __privateGet(this, _pickerViewInstances)[index2]) {
__privateGet(this, _pickerViewInstances)[index2].destroy();
__privateGet(this, _pickerViewInstances)[index2] = __privateMethod(this, _initPickerView, initPickerView_fn).call(this, viewNode, value || this.innerOptions.selectedIndexes, index2);
}
});
this.innerOptions.selectedIndexes = __privateGet(this, _pickerViewInstances).map((item) => item.innerSelectedIndex);
return this.innerOptions.selectedIndexes;
}
return __privateMethod(this, _cleanup2, cleanup_fn2).call(this);
}
}
_pickerViewInstances = new WeakMap();
_initPicker = new WeakSet();
initPicker_fn = function(value) {
var _a, _b;
const viewNodes = this.rootNode.querySelectorAll(".mobile-picker-view");
this.innerOptions.selectedIndexes.length = viewNodes.length;
viewNodes.forEach((item, index2) => {
const viewInstance = __privateMethod(this, _initPickerView, initPickerView_fn).call(this, item, value || this.innerOptions.selectedIndexes, index2);
__privateGet(this, _pickerViewInstances).push(viewInstance);
});
this.innerOptions.selectedIndexes = __privateGet(this, _pickerViewInstances).map((item) => item.innerSelectedIndex);
const itemHeight = ((_b = (_a = __privateGet(this, _pickerViewInstances)[0]) == null ? void 0 : _a.metaInfo) == null ? void 0 : _b.itemHeight) || 0;
if (itemHeight > 0) {
this.rootNode.style.setProperty(
"--picker-item-height",
`${itemHeight}px`
);
}
};
_initPickerView = new WeakSet();
initPickerView_fn = function(node, value, index2) {
const viewInstance = new PickerView(node, {
...this.innerOptions,
selectedIndex: value[index2],
onChange: (event, trigger) => {
__privateMethod(this, _handleChange2, handleChange_fn2).call(this, event, trigger, index2);
}
});
return viewInstance;
};
_handleChange2 = new WeakSet();
handleChange_fn2 = function(event, trigger, index2) {
var _a, _b;
this.innerOptions.selectedIndexes[index2] = event;
(_b = (_a = this.innerOptions).onChange) == null ? void 0 : _b.call(_a, this.innerOptions.selectedIndexes, trigger, index2);
};
_cleanup2 = new WeakSet();
cleanup_fn2 = function() {
this.innerOptions.selectedIndexes = [];
__privateGet(this, _pickerViewInstances).forEach((item) => item.destroy());
__privateSet(this, _pickerViewInstances, []);
return [];
};
export { Picker };