UNPKG

@gez/date-time-kit

Version:

166 lines (165 loc) 5.59 kB
var __freeze = Object.freeze; 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); var __template = (cooked, raw) => __freeze(__defProp(cooked, "raw", { value: __freeze(raw || cooked.slice()) })); var _a, _b, _c, _d, _e, _f, _g, _h; import { granHelper } from "../../utils/index.mjs"; import { Ele as NumListEle } from "../num-list/index.mjs"; import { UiBase } from "../web-component-base/index.mjs"; import { baseCss } from "./css.mjs"; import { baseHtml } from "./html.mjs"; export const granularityList = granHelper.date.list; export const colOrderList = ["ymd", "ydm", "myd", "mdy", "dym", "dmy"]; export class BaseEle extends UiBase { constructor() { super(...arguments); __publicField(this, "_renderCols", super._genRenderFn(() => { const { _els } = this; for (const c of this.colOrder) { _els["".concat(c, "Col")].remove(); _els.cols.appendChild(_els["".concat(c, "Col")]); } })); __publicField(this, "_updateGranularity", super._genRenderFn(() => { const { _minmaxGran, _els } = this; const [maxG, minG] = granHelper.date.idx( _minmaxGran.max, _minmaxGran.min ); const show = (el, condition) => !(el.style.display = condition ? "" : "none"); show( _els.cols, [ show(_els.yCol, maxG >= 2 && minG <= 2), show(_els.mCol, maxG >= 1 && minG <= 1), show(_els.dCol, maxG >= 0 && minG <= 0) ].some((v) => v) ); })); __publicField(this, "_updateColsValue", super._genRenderFn(() => { const { millisecond, _els } = this; const date = new Date(millisecond); if (Number.isNaN(+date)) return; _els.yList.currentNum = date.getFullYear(); _els.mList.currentNum = date.getMonth() + 1; _els.dList.maxNum = new Date( date.getFullYear(), date.getMonth() + 1, 0 ).getDate(); _els.dList.currentNum = date.getDate(); })); __publicField(this, "_onColsSelect", ({ target, detail: { newNum } }) => { if (!(target instanceof NumListEle)) return; target.currentNum = newNum; const oldMs = this.millisecond; const newMs = this._getMsFromEle(); this.millisecond = newMs; this.dispatchEvent( "change", { oldMs, newMs: this.millisecond }, true ); }); } static get observedAttributes() { return [ ...super.observedAttributes, "millisecond", "max-granularity", "min-granularity", "col-order" ]; } /** 当前选中日期,不带时分秒,用户点击列表内数字后会更新该值 */ get millisecond() { return Math.floor(+this._getAttr("millisecond", "0")); } set millisecond(v) { if (!Number.isSafeInteger(v)) return; this.setAttribute("millisecond", "" + Math.floor(v)); } get maxGranularity() { return this._getAttr("max-granularity", "year"); } set maxGranularity(v) { if (!granularityList.includes(v)) return; this.setAttribute("max-granularity", v); } get minGranularity() { return this._getAttr("min-granularity", "day"); } set minGranularity(v) { if (!granularityList.includes(v)) return; this.setAttribute("min-granularity", v); } get colOrder() { return this._getAttr("col-order", "dmy"); } set colOrder(v) { if (!colOrderList.includes(v)) return; this.setAttribute("col-order", v); } get _staticEls() { return { ...super._staticEls, cols: this.$0(_a || (_a = __template([".cols"]))), yCol: this.$0(_b || (_b = __template([".col.year"]))), mCol: this.$0(_c || (_c = __template([".col.month"]))), dCol: this.$0(_d || (_d = __template([".col.day"]))), lists: this.$(_e || (_e = __template(["dt-num-list"]))), yList: this.$0(_f || (_f = __template(["dt-num-list.year"]))), mList: this.$0(_g || (_g = __template(["dt-num-list.month"]))), dList: this.$0(_h || (_h = __template(["dt-num-list.day"]))) }; } get _minmaxGran() { const [min, max] = granHelper.date.minmax( this.minGranularity, this.maxGranularity ); return { min, max }; } scrollToCurrentItem() { this._els.lists.forEach((e) => e.scrollToCurrent()); } connectedCallback() { if (!super.connectedCallback()) return; const { _els } = this; _els.mList.formatter = _els.dList.formatter = (i) => (i < 10 ? "0" : "") + i; this._renderCols(); this._updateGranularity(); this._updateColsValue(); this._bindEvt(_els.lists)("select-num", this._onColsSelect); return true; } _onAttrChanged(name, oldValue, newValue) { super._onAttrChanged(name, oldValue, newValue); if (name === "max-granularity" || name === "min-granularity") this._updateGranularity(); else if (name === "col-order") this._renderCols(); else if (name === "millisecond") this._updateColsValue(); } _getMsFromEle() { const { _els } = this; const month = _els.mList.currentNum; const date = new Date( _els.yList.currentNum, month - 1, _els.dList.currentNum ); if (date.getMonth() + 1 !== month) { date.setDate(0); } return +date; } } __publicField(BaseEle, "_style", baseCss); __publicField(BaseEle, "_template", baseHtml);