akaunting/resources/assets/js/components/AkauntingDate.vue

190 lines
4.7 KiB
Vue
Raw Normal View History

2019-11-16 10:21:14 +03:00
<template>
<base-input :label="title"
:name="name"
:class="[
{'readonly': readonly},
{'disabled': disabled},
2021-06-07 14:42:36 +03:00
{'hidden-year': hiddenYear},
2021-06-18 11:35:36 +03:00
{'data-value-min': dataValueMin},
formClasses
]"
:footer-error="formError"
:prependIcon="icon"
:readonly="readonly"
:disabled="disabled"
2021-06-07 14:42:36 +03:00
@focus="focus"
>
2019-11-16 10:21:14 +03:00
<flat-picker slot-scope="{focus, blur}"
2021-05-25 19:52:39 +03:00
:name="dataName"
@on-open="focus"
@on-close="blur"
2021-02-27 16:54:39 +03:00
:config="dateConfig"
class="form-control datepicker"
v-model="real_model"
@input="change"
:readonly="readonly"
:disabled="disabled">
2019-11-16 10:21:14 +03:00
</flat-picker>
</base-input>
</template>
<script>
import flatPicker from "vue-flatpickr-component";
import "flatpickr/dist/flatpickr.css";
export default {
name: 'akaunting-date',
components: {
flatPicker
},
props: {
title: {
type: String,
default: '',
description: "Modal header title"
},
2021-05-25 19:52:39 +03:00
dataName: {
type: String,
default: '',
description: "Modal header title"
},
2019-11-16 10:21:14 +03:00
placeholder: {
type: String,
default: '',
description: "Modal header title"
},
readonly: {
type: Boolean,
default: false,
description: "Input readonly status"
},
2021-07-08 15:14:33 +03:00
period: {
type: Number,
description: "Payment period"
},
disabled: {
type: Boolean,
default: false,
description: "Input disabled status"
},
2019-11-16 10:21:14 +03:00
formClasses: null,
formError: null,
name: null,
2020-03-20 17:24:32 +03:00
value: {
default: null,
description: "Input value defalut"
},
model: {
default: null,
description: "Input model defalut"
},
2021-02-27 16:54:39 +03:00
dateConfig: {
type: Object,
default: function () {
return {
allowInput: true,
altFormat: "d M Y",
altInput: true,
dateFormat: "Y-m-d",
wrap: true,
};
},
description: "FlatPckr date configuration"
},
2019-11-16 10:21:14 +03:00
icon: {
type: String,
description: "Prepend icon (left)"
},
locale: {
type: String,
default: 'en',
2021-06-07 14:42:36 +03:00
},
hiddenYear: {
type: [Boolean, String]
2021-06-18 11:35:36 +03:00
},
dataValueMin: {
type: [Boolean, String, Date]
2019-11-16 10:21:14 +03:00
}
},
data() {
return {
2021-05-25 19:52:39 +03:00
real_model: '',
}
},
created() {
if (this.locale !== 'en') {
const lang = require(`flatpickr/dist/l10n/${this.locale}.js`).default[this.locale];
2021-02-23 22:28:18 +03:00
2021-03-18 11:43:38 +03:00
this.dateConfig.locale = lang;
2019-11-16 10:21:14 +03:00
}
this.real_model = this.value;
},
2019-11-16 10:21:14 +03:00
mounted() {
if (this.model) {
this.real_model = this.model;
}
this.$emit('interface', this.real_model);
2019-11-16 10:21:14 +03:00
},
methods: {
change() {
this.$emit('interface', this.real_model);
this.$emit('change', this.real_model);
2021-06-07 14:42:36 +03:00
},
focus() {
2021-06-07 14:48:34 +03:00
let date_wrapper_html = document.querySelectorAll('.numInputWrapper');
2021-06-07 14:57:39 +03:00
if(this.hiddenYear) {
2021-06-07 14:48:34 +03:00
date_wrapper_html.forEach((wrapper) => {
2021-06-07 14:57:39 +03:00
wrapper.classList.add('hidden-year-flatpickr');
2021-06-07 14:48:34 +03:00
});
2021-06-07 14:57:39 +03:00
} else {
date_wrapper_html.forEach((wrapper) => {
wrapper.classList.remove('hidden-year-flatpickr');
});
}
},
addDays(dateInput) {
if(!this.period) return;
const dateString = new Date(dateInput);
const aMillisec = 86400000;
const dateInMillisecs = dateString.getTime();
const settingPaymentTermInMs = parseInt(this.period) * aMillisec;
const prospectedDueDate = new Date(dateInMillisecs + settingPaymentTermInMs);
return prospectedDueDate;
},
2021-05-25 19:52:39 +03:00
},
2021-05-25 22:19:49 +03:00
2021-05-25 19:52:39 +03:00
watch: {
value: function(val) {
this.real_model = val;
},
dateConfig: function() {
if(this.dateConfig.minDate) {
if(this.real_model < this.dateConfig.minDate){
this.real_model = this.addDays(this.dateConfig.minDate);
}
}
2021-06-18 11:35:36 +03:00
},
2019-11-16 10:21:14 +03:00
}
}
</script>
2021-06-07 14:42:36 +03:00
<style>
.hidden-year-flatpickr {
display: none !important;
}
</style>