Newer
Older

Jan-Hendrik Willms
committed
<input type="hidden" :name="name" :value="returnValue">
<input type="text"
ref="visibleInput"
class="visible_input"
v-bind="$attrs"

Jan-Hendrik Willms
committed
v-on="$listeners"
:placeholder="placeholder">

Jan-Hendrik Willms
committed
import RestrictedDatesHelper from '../../assets/javascripts/lib/RestrictedDatesHelper';

Jan-Hendrik Willms
committed
name: 'Datepicker',
inheritAttrs: false,
props: {
name: {
type: String,
required: false
},

Jan-Hendrik Willms
committed
value: [Date, String, Number],
mindate: [Date, Number, String],
maxdate: [Date, Number, String],
placeholder: String,
disableHolidays: {
type: Boolean,
default: false,

Jan-Hendrik Willms
committed
emitDate: {
type: Boolean,
default: false,

Jan-Hendrik Willms
committed
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
returnAs: {
type: String,
default: 'localized',
validator(value) {
return ['localized', 'unix', 'iso'].includes(value);
}
}
},
computed: {
input() {
return $(this.$refs.visibleInput);
},
parameters() {
let params = {
onSelect: () => {
this.setUnixTimestamp();
},
maxDate: this.convertInputToNativeDate(this.maxdate),
minDate: this.convertInputToNativeDate(this.mindate),
};
if (this.disableHolidays) {
params.beforeShowDay = (date) => {
RestrictedDatesHelper.loadRestrictedDatesByYear(date.getFullYear()).then(
() => this.input.datepicker('refresh'),
() => null
);
const {reason, lock} = RestrictedDatesHelper.isDateRestricted(date);
return [!lock, lock ? 'ui-datepicker-is-locked' : null, reason];
};
}
return params;
},
returnValue() {
if (this.returnAs === 'unix') {
return this.convertInputToUnixTimestamp(this.value);
}
if (this.returnAs === 'iso') {
return this.convertInputToNativeDate(this.value).toISOString();
}
return this.convertInputToNativeDate(this.value).toLocaleDateString(String.locale);

Jan-Hendrik Willms
committed
convertInputToNativeDate(input) {
if (input instanceof Date) {
return input;
}
if (input === 'today') {
return new Date();
}
return input ? new Date(input * 1000) : null;
},
convertInputToUnixTimestamp(input) {
if (input instanceof Date) {
return Math.floor(input.getTime() / 1000);
}
if (!isNaN(parseInt(input, 10))) {
return parseInt(input, 10);
}
return input;
},

Jan-Hendrik Willms
committed
let date = this.input.datepicker('getDate');
this.$emit('input', this.emitDate ? date : Math.floor(date.getTime() / 1000));

Jan-Hendrik Willms
committed
let value = this.convertInputToUnixTimestamp(this.value);
if (Number.isInteger(value)) {
let date = new Date(value * 1000);

Jan-Hendrik Willms
committed
this.input.val(date.toLocaleDateString());

Jan-Hendrik Willms
committed
this.input.val(value);

Jan-Hendrik Willms
committed
this.input.datepicker(this.parameters);

Jan-Hendrik Willms
committed
maxdate(current) {
this.input.datepicker(
'option',
'maxDate',
this.convertInputToNativeDate(current)
);

Jan-Hendrik Willms
committed
mindate(current) {
this.input.datepicker(
'option',
'minDate',
this.convertInputToNativeDate(current)
);
},
value(current, previous) {
if (current.toISOString() !== previous.toISOString()) {
this.input.datepicker('setDate', current);
this.input.datepicker('refresh');
}