mine-h5-ui
Version:
一款轻量级、模块化基于 Vue3.x 的 H5 前端 UI 组件库 👍
116 lines (115 loc) • 4.2 kB
JavaScript
import { ref as g, computed as N } from "vue";
import { IsLeapyear as S } from "../MeAPI/function.js";
const H = (h) => {
const x = h.maxDate.getFullYear(), p = h.minDate.getFullYear(), r = g([
{
id: 1,
list: Array.from({ length: x - p + 1 }, (t, e) => p + e)
},
{
id: 2,
list: 12
},
{
id: 3,
list: 31
},
{
id: 4,
list: 24
},
{
id: 5,
list: 60
}
]), y = {
"year-month": [1, 2],
"month-day": [2, 3],
time: [4, 5],
datetime: [1, 2, 3, 4, 5]
}[h.type] ?? [1, 2, 3], u = g(y), Y = { length: y.length }, c = g(Array.from(Y, () => 88)), v = Array.from(Y);
y.forEach((t, e) => {
const { list: s } = r.value[t - 1];
v[e] = typeof s == "number" ? +![3, 4].includes(e) : s[0];
});
let m = 0, d = 0, T = 0;
const w = g(0), A = N(() => (t) => t < 10 ? `0${t}` : t), C = (t, e) => [4, 5].includes(t) ? e - 1 : e, O = (t) => {
w.value = t, setTimeout(() => {
w.value = 0;
}, t);
}, I = (t) => {
const e = u.value.indexOf(t);
let s = c.value[e];
const o = r.value[2].list - 1;
s > 88 ? s = 88 : s < 88 - o * 44 && (s = 88 - o * 44), c.value[e] = s, O(400);
const n = Math.abs(s / 44 - 2);
v[u.value.indexOf(3)] = n + 1;
}, b = (t, e) => {
const s = Math.abs(t / 44 - 2), { list: o } = r.value[e - 1];
let n = 0;
if ([4, 5].includes(e) ? n = typeof o == "number" ? s : o[s] : n = typeof o == "number" ? s + 1 : o[s], e === 2 && u.value.includes(3)) {
if (n === 2) {
const f = u.value.includes(1) ? v[0] : (/* @__PURE__ */ new Date()).getFullYear();
r.value[2].list = S(f) ? 29 : 28;
} else
r.value[2].list = n > 7 && n % 2 !== 0 || n <= 7 && n % 2 === 0 ? 30 : 31;
I(e + 1);
}
v[u.value.indexOf(e)] = n;
};
return { show: u, currentValue: v, listData: r, distance: c, duration: w, filterNumber: A, getCurNum: C, onTouchstart: (t, e) => {
m = t.changedTouches[0].clientY, d = c.value[u.value.indexOf(e)], T = +/* @__PURE__ */ new Date();
}, onTouchmove: (t, e) => {
const o = t.changedTouches[0].clientY - m, n = d + o;
c.value[u.value.indexOf(e)] = n;
}, onTouchend: (t, e) => {
const s = u.value.indexOf(e);
let o = c.value[s];
const n = +/* @__PURE__ */ new Date(), f = +((t.changedTouches[0].clientY - m) / (n - T)).toFixed(2);
if (f > 0.12 || f < -0.12) {
const l = Math.ceil(f * 400);
o += l;
}
const i = o % 44;
if (i !== 0) {
let l = 0;
i > 0 ? l = i > 44 / 2 ? o + (44 - i) : o - i : l = i < -44 / 2 ? o - (44 + i) : o - i;
const { list: a } = r.value[e - 1], D = typeof a == "number" ? a - 1 : a.length - 1;
l > 88 ? l = 88 : l < 88 - D * 44 && (l = 88 - D * 44), c.value[s] = l, O(400), b(l, e);
}
}, onMousedown: (t, e) => {
m = t.clientY, d = c.value[u.value.indexOf(e)], T = +/* @__PURE__ */ new Date(), document.onmousemove = (s) => {
const n = s.clientY - m, f = d + n;
c.value[u.value.indexOf(e)] = f;
}, document.onmouseup = (s) => {
const o = u.value.indexOf(e);
let n = c.value[o];
const f = +/* @__PURE__ */ new Date(), i = +((s.clientY - m) / (f - T)).toFixed(2);
if (i > 0.12 || i < -0.12) {
const a = Math.ceil(i * 400);
n += a;
}
const l = n % 44;
if (l !== 0) {
let a = 0;
l > 0 ? a = l > 44 / 2 ? n + (44 - l) : n - l : a = l < -44 / 2 ? n - (44 + l) : n - l;
const { list: D } = r.value[e - 1], M = typeof D == "number" ? D - 1 : D.length - 1;
a > 88 ? a = 88 : a < 88 - M * 44 && (a = 88 - M * 44), c.value[o] = a, O(400), b(a, e);
}
document.onmousemove = null, document.onmouseup = null;
};
} };
}, L = ({ props: h, emit: x, currentValue: p, modelValue: r }) => ({ onCancel: () => {
x("cancel");
}, onSure: () => {
const u = p.reduce((Y, c, v) => {
const m = c < 10 ? `0${c}` : c;
let d;
return h.type === "time" ? d = ":" : h.type === "datetime" ? d = v < 3 ? "-" : v === 3 ? " " : ":" : d = "-", Y + d + m;
}, "");
r.value = u.slice(1), x("sure", p);
} });
export {
L as useBtns,
H as useHandMove
};