@gez/date-time-kit
Version:
121 lines (120 loc) • 4.03 kB
JavaScript
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;
import { Ele as PopoverEle } from "../popover/index.mjs";
import {
clearupPopEleAttrSync2Parent,
isPopoverAttrKey,
parentPopAttrSync2PopEle,
popEleAttrSync2Parent,
popoverAttrKeys
} from "../popover/attr-sync-helper.mjs";
import {
BaseEle
} from "./base.mjs";
import { selectorCss } from "./css.mjs";
import { selectorHtml } from "./html.mjs";
export { granularityList, colOrderList } from "./base.mjs";
export class Ele extends BaseEle {
constructor() {
super(...arguments);
__publicField(this, "_render", super._genRenderFn(() => {
if (!this.isConnected) return;
const { currentTime, _minmaxGran } = this;
this.millisecond = +currentTime;
Object.assign(this._els.dateEcho, {
currentTime,
minGranularity: _minmaxGran.min,
maxGranularity: _minmaxGran.max
});
}));
__publicField(this, "_onPopoverChange", (e) => {
if (!(e.target instanceof PopoverEle)) return;
this._els.dateEcho.active = e.detail;
if (!e.detail) return this._render();
this.scrollToCurrentItem();
});
__publicField(this, "_onDoneBtnClick", (_e) => {
const oldTime = new Date(this.currentTime);
const newTime = new Date(this.millisecond);
oldTime.setFullYear(newTime.getFullYear());
oldTime.setMonth(newTime.getMonth());
oldTime.setDate(newTime.getDate());
const time = oldTime;
this.currentTime = time;
this.dispatchEvent("select-time", time);
this._render();
this.open = false;
});
}
static get observedAttributes() {
return [
...super.observedAttributes,
"current-time",
...popoverAttrKeys
];
}
get _staticEls() {
return {
...super._staticEls,
popover: this.$0(_a || (_a = __template(["dt-popover"]))),
dateEcho: this.$0(_b || (_b = __template(["dt-echo"])))
};
}
set open(v) {
this.toggleAttribute("pop-open", v);
}
get open() {
return this.hasAttribute("pop-open");
}
connectedCallback() {
if (!super.connectedCallback()) return;
this._render();
const { _els } = this;
popEleAttrSync2Parent(this, _els.popover);
this._bindEvt(_els.popover)("open-change", this._onPopoverChange);
this._bindEvt(_c || (_c = __template(["button"])))("click", this._onDoneBtnClick);
}
disconnectedCallback() {
clearupPopEleAttrSync2Parent(this);
return super.disconnectedCallback();
}
_onAttrChanged(name, oldValue, newValue) {
super._onAttrChanged(name, oldValue, newValue);
if (name === "millisecond") return;
if (isPopoverAttrKey(name)) {
parentPopAttrSync2PopEle(
name,
oldValue,
newValue,
this._els.popover
);
return;
}
this._render();
}
/** 当前日期,带时分秒,`select-time` 事件抛出时,时分秒来自这里,年月日来自 `millisecond`。 */
get currentTime() {
const v = this._getAttr("current-time", "" + Date.now());
return new Date(Number.isNaN(+v) ? v : +v);
}
set currentTime(val) {
const v = new Date(val);
if (Number.isNaN(+v)) return;
this.setAttribute("current-time", +v + "");
}
/** 日期回显格式化函数。设置为 `null` 则重置为默认值 */
get dateFormatter() {
return this._els.dateEcho.dateFormatter;
}
set dateFormatter(fn) {
this._els.dateEcho.dateFormatter = fn;
}
}
__publicField(Ele, "tagName", "dt-yyyymmdd-selector");
__publicField(Ele, "_style", selectorCss);
__publicField(Ele, "_template", selectorHtml);
Ele.define();