iview/src/components/date-picker/panel/date.vue

239 lines
8.6 KiB
Vue
Raw Normal View History

2016-12-12 10:37:52 +08:00
<template>
2016-12-12 20:34:28 +08:00
<div :class="[prefixCls + '-body-wrapper']">
<div :class="[prefixCls + '-sidebar']" v-if="shortcuts">
<div
:class="[prefixCls + '-shortcut']"
v-for="shortcut in shortcuts"
@click="handleShortcutClick(shortcut)">{{ shortcut.text }}</div>
</div>
<div :class="[prefixCls + '-body']">
<div :class="[datePrefixCls + '-header']" v-show="currentView !== 'time'">
<span
:class="iconBtnCls('prev', '-double')"
2016-12-15 20:16:58 +08:00
@click="prevYear"><Icon type="ios-arrow-left"></Icon></span>
2016-12-12 20:34:28 +08:00
<span
:class="iconBtnCls('prev')"
@click="prevMonth"
2016-12-15 20:16:58 +08:00
v-show="currentView === 'date'"><Icon type="ios-arrow-left"></Icon></span>
2016-12-12 20:34:28 +08:00
<span
:class="[datePrefixCls + '-header-label']"
2016-12-15 20:16:58 +08:00
@click="showYearPicker">{{ yearLabel }}</span>
2016-12-12 20:34:28 +08:00
<span
:class="[datePrefixCls + '-header-label']"
@click="showMonthPicker"
2016-12-15 20:16:58 +08:00
v-show="currentView === 'date'">{{ month + 1 + '月' }}</span>
<span
:class="iconBtnCls('next', '-double')"
@click="nextYear"><Icon type="ios-arrow-right"></Icon></span>
2016-12-12 20:34:28 +08:00
<span
:class="iconBtnCls('next')"
@click="nextMonth"
2016-12-15 20:16:58 +08:00
v-show="currentView === 'date'"><Icon type="ios-arrow-right"></Icon></span>
2016-12-12 20:34:28 +08:00
</div>
<div :class="[prefixCls + '-content']">
<date-table
2016-12-14 23:08:57 +08:00
v-show="currentView === 'date'"
:year="year"
:month="month"
:date="date"
:value="value"
:week="week"
:selection-mode="selectionMode"
2016-12-15 20:16:58 +08:00
:disabled-date="disabledDate"
@on-pick="handleDatePick"></date-table>
2016-12-12 20:34:28 +08:00
<year-table
2016-12-15 20:16:58 +08:00
v-ref:year-table
v-show="currentView === 'year'"
:year="year"
:date="date"
:disabled-date="disabledDate"
@on-pick="handleYearPick"></year-table>
2016-12-12 20:34:28 +08:00
<month-table
2016-12-15 20:16:58 +08:00
v-ref:month-table
v-show="currentView === 'month'"
:month="month"
:date="date"
:disabled-date="disabledDate"
@on-pick="handleMonthPick"></month-table>
2016-12-12 20:34:28 +08:00
</div>
</div>
</div>
2016-12-12 10:37:52 +08:00
</template>
<script>
2016-12-15 20:16:58 +08:00
import Icon from '../../icon/icon.vue';
2016-12-12 20:34:28 +08:00
import DateTable from '../base/date-table.vue';
import YearTable from '../base/year-table.vue';
import MonthTable from '../base/month-table.vue';
2016-12-15 20:16:58 +08:00
import { formatDate, parseDate } from '../util';
2016-12-12 20:34:28 +08:00
const prefixCls = 'ivu-picker-panel';
const datePrefixCls = 'ivu-date-picker';
2016-12-12 10:37:52 +08:00
export default {
2016-12-15 20:16:58 +08:00
components: { Icon, DateTable, YearTable, MonthTable },
2016-12-12 10:37:52 +08:00
data () {
2016-12-12 20:34:28 +08:00
return {
prefixCls: prefixCls,
datePrefixCls: datePrefixCls,
shortcuts: [],
2016-12-14 23:08:57 +08:00
currentView: 'date',
date: new Date(),
value: '',
showTime: false,
selectionMode: 'day',
visible: false,
disabledDate: '',
year: null,
month: null,
week: null,
showWeekNumber: false,
timePickerVisible: false
}
},
computed: {
2016-12-15 20:16:58 +08:00
yearLabel () {
const year = this.year;
if (!year) return '';
if (this.currentView === 'year') {
const startYear = Math.floor(year / 10) * 10;
return `${startYear}年 - ${startYear + 9}`;
}
return `${year}`
}
2016-12-14 23:08:57 +08:00
},
watch: {
value (newVal) {
2016-12-15 20:16:58 +08:00
if (!newVal) return;
2016-12-14 23:08:57 +08:00
newVal = new Date(newVal);
if (!isNaN(newVal)) {
// todo
// if (typeof this.disabledDate === 'function' && this.disabledDate(new Date(newVal))) return;
this.date = newVal;
this.year = newVal.getFullYear();
this.month = newVal.getMonth();
// this.$emit('on-pick', newVal, true);
}
2016-12-12 20:34:28 +08:00
}
2016-12-12 10:37:52 +08:00
},
2016-12-12 20:34:28 +08:00
methods: {
2016-12-15 20:16:58 +08:00
handleClear() {
this.date = new Date();
this.$emit('on-pick', '');
},
2016-12-12 20:34:28 +08:00
handleShortcutClick (shortcut) {
},
iconBtnCls (direction, type = '') {
return [
`${prefixCls}-icon-btn`,
`${datePrefixCls}-${direction}-btn`,
`${datePrefixCls}-${direction}-btn-arrow${type}`,
]
},
2016-12-15 20:16:58 +08:00
resetDate () {
this.date = new Date(this.date);
},
2016-12-12 20:34:28 +08:00
prevYear () {
2016-12-15 20:16:58 +08:00
if (this.currentView === 'year') {
this.$refs.yearTable.prevTenYear();
} else {
this.year--;
this.date.setFullYear(this.year);
this.resetDate();
}
2016-12-12 20:34:28 +08:00
},
nextYear () {
2016-12-15 20:16:58 +08:00
if (this.currentView === 'year') {
this.$refs.yearTable.nextTenYear();
} else {
this.year++;
this.date.setFullYear(this.year);
this.resetDate();
}
2016-12-12 20:34:28 +08:00
},
prevMonth () {
2016-12-15 20:16:58 +08:00
this.month--;
if (this.month < 0) {
this.month = 11;
this.year--;
}
2016-12-12 20:34:28 +08:00
},
nextMonth () {
2016-12-15 20:16:58 +08:00
this.month++;
if (this.month > 11) {
this.month = 0;
this.year++;
}
2016-12-12 20:34:28 +08:00
},
showYearPicker () {
2016-12-15 20:16:58 +08:00
this.currentView = 'year';
2016-12-12 20:34:28 +08:00
},
showMonthPicker () {
2016-12-15 20:16:58 +08:00
this.currentView = 'month';
},
handleYearPick(year, close = true) {
this.year = year;
if (!close) return;
2016-12-12 20:34:28 +08:00
2016-12-15 20:16:58 +08:00
this.date.setFullYear(year);
if (this.selectionMode === 'year') {
this.$emit('on-pick', new Date(year));
} else {
this.currentView = 'month';
}
this.resetDate();
},
handleMonthPick (month) {
this.month = month;
const selectionMode = this.selectionMode;
if (selectionMode !== 'month') {
this.date.setMonth(month);
this.currentView = 'date';
this.resetDate();
} else {
this.date.setMonth(month);
this.year && this.date.setFullYear(this.year);
this.resetDate();
const value = new Date(this.date.getFullYear(), month, 1);
this.$emit('on-pick', value);
}
},
handleDatePick (value) {
if (this.selectionMode === 'day') {
if (!this.showTime) {
this.$emit('on-pick', new Date(value.getTime()));
}
this.date.setFullYear(value.getFullYear());
this.date.setMonth(value.getMonth());
this.date.setDate(value.getDate());
}
this.resetDate();
},
resetView() {
if (this.selectionMode === 'month') {
this.currentView = 'month';
} else if (this.selectionMode === 'year') {
this.currentView = 'year';
} else {
this.currentView = 'date';
}
this.year = this.date.getFullYear();
this.month = this.date.getMonth();
2016-12-12 20:34:28 +08:00
}
},
2016-12-14 23:08:57 +08:00
compiled () {
if (this.selectionMode === 'month') {
this.currentView = 'month';
}
if (this.date && !this.year) {
this.year = this.date.getFullYear();
this.month = this.date.getMonth();
}
2016-12-12 20:34:28 +08:00
}
2016-12-12 10:37:52 +08:00
}
</script>