import { Vue, Prop, Component, Watch, Ref } from "vue-property-decorator"; import moment from "moment-jalaali"; import VuePersianDatetimePicker from "vue-persian-datetime-picker"; import Icon from "@/components/Icon/index.vue"; import TextField from "@/components/TextField/index.vue"; import { DateMoment, Attributes, DatePickerValue } from "@/types"; import Button from "@/components/Button/index.vue"; @Component({ components: { TextField, Icon, VuePersianDatetimePicker, Button, }, }) export default class DatePicker extends Vue { @Prop({ type: Boolean, default: false }) range?: boolean; @Prop({ type: Boolean, default: false }) disableSingle?: boolean; @Prop({ type: Boolean, default: false }) disableStart?: boolean; @Prop({ type: Boolean, default: false }) disableEnd?: boolean; @Prop({ type: Boolean, default: false }) preview?: boolean; @Prop({ type: [String, Array] }) value!: DatePickerValue; @Prop({ type: Boolean }) dropdown?: boolean; @Prop({ type: Boolean }) button?: boolean; @Prop({ type: String }) align?: boolean; @Prop({ type: Boolean, default: false }) max?: boolean; @Prop({ type: String, default: 'date'}) type?: string; @Prop({ type: Boolean, default: false }) min?: boolean; @Ref("datePicker") readonly datePickerRef!: HTMLElement; @Ref("pickerBtn") readonly buttonPickerRef!: HTMLElement; isDrpShow: null | boolean = null; buttonText = moment().format("jD jMMMM"); today = moment().subtract(1, "days").format("jYYYY-jMM-jDD"); minToday = moment().format("jYYYY-jMM-jDD"); mounted() { /** * Open dropdown in input focus */ const inputRef = this.$refs.input; if (inputRef) { const { $el }: any = this.$refs.input; const elInput = $el.querySelector("input"); elInput.addEventListener("focus", () => { this.isDrpShow = true; }); } if (this.value) { this.onModelChange(this.value); } document.documentElement.addEventListener("click", this.outsideClick, true); } outsideClick(e: Event): void { const target = e.target as HTMLInputElement; const el = target.closest("#date-picker-op"); if (!el) { this.isDrpShow = false; } } get model(): string | string[] { return this.getModel(); } set model(value: string | string[]) { const result = value || ""; this.$emit("input", result); } getModel(): DatePickerValue { let result: DatePickerValue = this.value; const validDateHandler = (value: string) => moment(value, "YYYY-MM-DD", true).isValid(); if (validDateHandler(this.value[0])) { if (moment(this.value[0]).isAfter(this.value[1])) { result = [this.value[1], this.value[0]]; } } return result || ""; } highlightToday(formatted: string, dateMoment: DateMoment): Attributes { const attributes = {} as Attributes; if (dateMoment.format("YYYY/MM/DD") === moment().format("YYYY/MM/DD")) { attributes.class = "is-today"; } return attributes; } toggleDropdown() { if (this.isDrpShow) { this.isDrpShow = false; } else { this.isDrpShow = true; } } /** * Close dropdown after value is chosen */ @Watch("model") onModelChange(value: DatePickerValue) { console.log('here') if (value) { const compare = () => { const yesterday = moment().subtract(1, "days").format("jYYYY-jMM-jDD"); const valueYear = moment(this.value, "jYYYY/jM/jD").format("jYYYY"); const currentYear = moment().format("jYYYY"); if (value === yesterday) { return this.$i18n.t("dashboard.chart.yesterday"); } if (valueYear === currentYear) { return moment(this.value, "jYYYY/jM/jD").format("jD jMMMM"); } return moment(this.value, "jYYYY/jM/jD").format("jD jMMMM jYYYY"); }; this.isDrpShow = false; this.buttonText = compare().toString(); } } }