From a4c840daab2c2062f78833b99737020473575e92 Mon Sep 17 00:00:00 2001 From: Saeid Date: Thu, 30 Jul 2020 12:51:41 +0430 Subject: [PATCH] fix: persian Date --- .../Global/components/Inputs/PersianDate.vue | 91 ++++++++++++++++--- .../js/Global/plugins/globalComponent.js | 2 + resources/js/Global/utils/date/cron.js | 2 +- 3 files changed, 80 insertions(+), 15 deletions(-) diff --git a/resources/js/Global/components/Inputs/PersianDate.vue b/resources/js/Global/components/Inputs/PersianDate.vue index 4ac6ec5..0bd067b 100644 --- a/resources/js/Global/components/Inputs/PersianDate.vue +++ b/resources/js/Global/components/Inputs/PersianDate.vue @@ -14,15 +14,16 @@ v-model="dateShow" locale="fa,en" :locale-config="localeConfig" - :format="displayFormat" - @change="$emit('input', $event.format(format))" + :format="defaultDisplayFormat" + :inputFormat="defaultInputFormat" + @change="$emit('input', $event.format(defaultFormat))" :type="type" :auto-submit="true" :element="'my-custom-date-' + uniqueId" :editable="true" :show="show" - :min="min" - :max="max" + :min="minimum" + :max="maximum" @close="show = false" :color="color" /> @@ -38,24 +39,30 @@ }, props: { color: {default: 'black'}, + defaultDate: { + default: 'now', + type: String, + validator: (val) => ['now', 'empty'].includes(val) + }, min: {default: null}, max: {default: null}, - displayFormat: {default: 'jDD jMMMM jYYYY ساعت HH:mm'}, - format: {default: 'YYYY-MM-DD HH:mm'}, - type: {default: 'datetime'}, + displayFormat: {default: null}, + inputFormat: {default: null}, + format: {default: null}, + type: { + default: 'datetime', + type: String, + validator: (val) => ['datetime', 'time', 'date'].includes(val) + }, label: {default: 'در تاریخ '}, value: {default: null}, }, data: () => ({ localeConfig: { fa: { - displayFormat: 'jYYYY/jMM/jDD HH:mm', - inputFormat: 'YYYY-MM-DD HH:mm', lang: {label: 'شمسی'} }, en: { - displayFormat: 'YYYY/MM/DD HH:mm', - inputFormat: 'YYYY-MM-DD HH:mm', lang: {label: 'Gregorian'} } }, @@ -66,12 +73,68 @@ dateShow: { get() { if (this.value) { - return moment(new Date(this.value)).format(this.displayFormat); + return moment(new Date(this.value)).format(this.defaultDisplayFormat); + } else { + if (this.defaultDate === 'now' ) { + this.$emit('input', moment(new Date()).format(this.defaultFormat)); + return moment(new Date()).format(this.defaultDisplayFormat); + } else { + return ''; + } + } - return moment(new Date()).format(this.displayFormat); }, set(value) { - + } + }, + defaultDisplayFormat() { + if (!this.displayFormat) { + if (this.type === 'datetime') { + return 'jDD jMMMM jYYYY ساعت HH:mm'; + } else if(this.type === 'date') { + return 'jDD jMMMM jYYYY'; + } else if(this.type === 'time') { + return 'HH:mm'; + } + } + return this.displayFormat + }, + defaultInputFormat() { + if (!this.inputFormat) { + if (this.type === 'datetime') { + return 'YYYY-MM-DD HH:mm'; + } else if(this.type === 'date') { + return 'YYYY-MM-DD'; + } else if(this.type === 'time') { + return 'HH:mm'; + } + } + return this.inputFormat + }, + defaultFormat() { + if (!this.format) { + if (this.type === 'datetime') { + return 'YYYY-MM-DD HH:mm'; + } else if(this.type === 'date') { + return 'YYYY-MM-DD'; + } else if(this.type === 'time') { + return 'HH:mm'; + } + } + return this.format + }, + minimum() { + if (this.min === 'now') { + return moment(new Date()).format(this.defaultInputFormat); + } else { + return this.min; + } + }, + maximum() { + if (this.max === 'now') { + return moment(new Date()).format(this.defaultInputFormat); + } else { + return this.max; } }, } diff --git a/resources/js/Global/plugins/globalComponent.js b/resources/js/Global/plugins/globalComponent.js index 93516bf..c3615be 100644 --- a/resources/js/Global/plugins/globalComponent.js +++ b/resources/js/Global/plugins/globalComponent.js @@ -74,6 +74,8 @@ Vue.component("we-hint-text", HintText); import HintBlock from "@Global/components/Blocks/Hint.vue"; Vue.component("hint-block", HintBlock); +import PersianDate from '@Global/components/Inputs/PersianDate' +Vue.component("we-persian-date", PersianDate); diff --git a/resources/js/Global/utils/date/cron.js b/resources/js/Global/utils/date/cron.js index 1b437f4..69c9551 100644 --- a/resources/js/Global/utils/date/cron.js +++ b/resources/js/Global/utils/date/cron.js @@ -1,7 +1,7 @@ export default class Cron { constructor(options) { - this.date_time = options['date_time'] ? options.date : ''; + this.date_time = options['date_time'] ? options.date_time : ''; this.date = options['date'] ? options.date : ''; this.time = options['time'] ? options.time : ''; this.cron = options['cron'] ? options.cron : '';