diff --git a/src/components/base/Calendar/BaseCalendar.vue b/src/components/base/Calendar/BaseCalendar.vue index 43da3ce..88d86fc 100644 --- a/src/components/base/Calendar/BaseCalendar.vue +++ b/src/components/base/Calendar/BaseCalendar.vue @@ -1,15 +1,14 @@ @@ -57,17 +56,29 @@ export default { type: Function, default: () => {}, }, - cancel: { - type: Function, - default: () => {}, - }, }, data() { return { internalDate: moment(), - externalDate: null, - currentFormIndex: 0, - forms: ["CalendarDates", "CalendarYears", "CalendarMonths"], + currentForm: null, + forms: [ + { + component: "CalendarDates", + saveBtnLabel: "Сохранить", + saveFunction: this.saveExternalChanges, + }, + { + component: "CalendarYears", + saveBtnLabel: "Применить", + saveFunction: this.applyInternalChanges, + }, + { + component: "CalendarMonths", + saveBtnLabel: "Применить", + saveFunction: this.applyInternalChanges, + }, + ], + externalValue: null, }; }, computed: { @@ -79,9 +90,6 @@ export default { this.$emit("update:modelValue", value); }, }, - currentForm() { - return this.forms[this.currentFormIndex]; - }, internalMonth() { let formattedMonth = this.internalDate.format("MMMM"); return `${ @@ -90,38 +98,48 @@ export default { }, }, methods: { - nextForm() { - this.currentFormIndex += 1; + changeForm(form) { + this.currentForm = this.forms.find((elem) => elem.component === form); }, - previousForm() { - this.currentFormIndex -= 1; - }, - cancelChanges() { - this.cancel(); - }, - saveChanges() { - this.value = this.externalDate?.from - ? { - from: this.externalDate?.from?.clone(), - to: this.externalDate?.to?.clone(), - } - : this.externalDate.clone(); - this.save(); - }, - }, - mounted() { - if (this.value) { + changeDate() { if (this.range) { this.internalDate = this.value?.from?.clone(); - this.externalDate = { + this.externalValue = { from: this.value?.from?.clone(), to: this.value?.to?.clone(), }; } else { this.internalDate = this.value?.clone(); - this.externalDate = this.value?.clone(); + this.externalValue = this.value?.clone(); } - } + }, + cancelChanges() { + this.changeDate(); + this.changeForm("CalendarDates"); + }, + saveExternalChanges() { + if (this.range) + this.value = { + from: this.externalValue?.from?.clone(), + to: this.externalValue?.to?.clone(), + }; + else this.value = this.externalValue?.clone(); + this.save(); + }, + applyInternalChanges() { + switch (this.currentForm.component) { + case "CalendarYears": + this.changeForm("CalendarMonths"); + break; + case "CalendarMonths": + this.changeForm("CalendarDates"); + break; + } + }, + }, + mounted() { + if (this.value) this.changeDate(); + this.currentForm = this.forms[0]; }, }; diff --git a/src/components/base/Calendar/CalendarDates.vue b/src/components/base/Calendar/CalendarDates.vue index 2262142..9fc435e 100644 --- a/src/components/base/Calendar/CalendarDates.vue +++ b/src/components/base/Calendar/CalendarDates.vue @@ -8,7 +8,7 @@ :label="internalMonth" no-caps, padding="10px 12px" - @click="nextForm" + @click="switchForm" ) .arrows.flex.items-center.justify-center.gap-x-6 q-btn( @@ -53,7 +53,7 @@ export default { range: Boolean, rangeCount: Number, internalMonth: String, - nextForm: { + changeForm: { type: Function, default: () => {}, }, @@ -97,6 +97,9 @@ export default { }, }, methods: { + switchForm() { + this.changeForm("CalendarYears"); + }, previousMonth() { this.internalValue = this.internalValue.clone().subtract(1, "month"); }, diff --git a/src/components/base/Calendar/CalendarMonths.vue b/src/components/base/Calendar/CalendarMonths.vue index 3a843c8..a5e7ddd 100644 --- a/src/components/base/Calendar/CalendarMonths.vue +++ b/src/components/base/Calendar/CalendarMonths.vue @@ -1,17 +1,82 @@ - + diff --git a/src/components/base/Calendar/CalendarYears.vue b/src/components/base/Calendar/CalendarYears.vue index d2fea67..695de22 100644 --- a/src/components/base/Calendar/CalendarYears.vue +++ b/src/components/base/Calendar/CalendarYears.vue @@ -14,7 +14,7 @@ v-for="year in internalYearsList", :key="year", :id="year", - :class="activeCellClass(year), cellsClass", + :class="calculateActiveCell(year), cellsClass", @click="selectYear(year)", ) {{ year }} @@ -28,11 +28,6 @@ export default { endYear: Number, internalMonth: String, }, - data() { - return { - previousDate: null, - }; - }, computed: { internalValue: { get() { @@ -61,7 +56,7 @@ export default { }, }, methods: { - activeCellClass(year) { + calculateActiveCell(year) { return { "active-cell": parseInt(this.internalValue.format("YYYY"), 10) === year, }; @@ -69,9 +64,14 @@ export default { selectYear(year) { this.internalValue = this.internalValue.clone().year(year); }, + scrollToCurrentYear() { + document + .getElementById(parseInt(this.internalValue.format("YYYY"), 10)) + .scrollIntoView({ block: "center", behavior: "smooth" }); + }, }, mounted() { - this.previousDate = this.internalValue.clone(); + this.scrollToCurrentYear(); }, }; diff --git a/src/pages/newCalendar/components/CalendarHeader.vue b/src/pages/newCalendar/components/CalendarHeader.vue index d81579d..8fa66ae 100644 --- a/src/pages/newCalendar/components/CalendarHeader.vue +++ b/src/pages/newCalendar/components/CalendarHeader.vue @@ -31,7 +31,7 @@ ) .h-5.w-5.flex.items-center.justify-center q-icon.text.cursor-pointer(:name="calendarVisibility ? 'app:cancel' : 'app:calendar'", - :size="calendarVisibility ? '10px' : '16px'" + :size="calendarVisibility ? '10px' : '16px'", ) q-menu( :style="{'margin-top': '4px !important'}" @@ -42,11 +42,11 @@ :offset="[122, 14]" ) base-calendar( - v-model="date", + v-model="dates", range, :range-count="7", - :save="saveNewDate", - :cancel="cancelDateChanges", + :save="saveDatesChange", + :start-year="2000" ) q-btn( color="secondary", @@ -88,7 +88,7 @@ export default { value: "collapsed", }, ], - date: { + dates: { from: null, to: null, }, @@ -97,9 +97,9 @@ export default { }, computed: { currentWeek() { - return `${this.date?.from?.format( + return `${this.dates?.from?.format( "D MMMM YYYY" - )} - ${this.date?.to?.format("D MMMM YYYY")}`; + )} - ${this.dates?.to?.format("D MMMM YYYY")}`; }, ...mapState({ selectedDates: (state) => state.calendar.selectedDates, @@ -107,25 +107,28 @@ export default { }, methods: { previousWeek() { - this.date.from = this.date.from.clone().subtract(1, "week"); - this.date.to = this.date.to.clone().subtract(1, "week"); + this.dates.from = this.dates.from.clone().subtract(1, "week"); + this.dates.to = this.dates.to.clone().subtract(1, "week"); }, nextWeek() { - this.date.from = this.date.from.clone().add(1, "week"); - this.date.to = this.date.to.clone().add(1, "week"); + this.dates.from = this.dates.from.clone().add(1, "week"); + this.dates.to = this.dates.to.clone().add(1, "week"); }, ...mapActions({ changeSelectedDates: "changeSelectedDates", }), - saveNewDate() { + saveDatesChange() { this.calendarVisibility = false; }, - cancelDateChanges() { - this.calendarVisibility = false; + initializeDates() { + this.dates = { + from: this.selectedDates.from.clone(), + to: this.selectedDates.to.clone(), + }; }, }, watch: { - date: { + dates: { deep: true, handler(val) { if ( @@ -137,10 +140,7 @@ export default { }, }, mounted() { - this.date = { - from: this.selectedDates.from.clone(), - to: this.selectedDates.to.clone(), - }; + this.initializeDates(); }, };