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

268 lines
10 KiB
Vue
Raw Normal View History

2016-12-12 10:37:52 +08:00
<template>
2017-09-21 08:22:05 +02:00
<div :class="classes" @mousedown.prevent>
2016-12-19 14:44:07 +08:00
<div :class="[prefixCls + '-sidebar']" v-if="shortcuts.length">
2016-12-12 20:34:28 +08:00
<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')"
2017-09-26 16:57:01 +02:00
@click="changeYear(-1)"><Icon type="ios-arrow-left"></Icon></span>
2016-12-12 20:34:28 +08:00
<span
:class="iconBtnCls('prev')"
2017-09-26 16:57:01 +02:00
@click="changeMonth(-1)"
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"
v-show="currentView === 'date'">{{ monthLabel }}</span>
2016-12-15 20:16:58 +08:00
<span
:class="iconBtnCls('next', '-double')"
2017-09-26 16:57:01 +02:00
@click="changeYear(+1)"><Icon type="ios-arrow-right"></Icon></span>
2016-12-12 20:34:28 +08:00
<span
:class="iconBtnCls('next')"
2017-09-26 16:57:01 +02:00
@click="changeMonth(+1)"
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"
:selection-mode="selectionMode"
2016-12-15 20:16:58 +08:00
:disabled-date="disabledDate"
2016-12-22 09:18:11 +08:00
@on-pick="handleDatePick"
@on-pick-click="handlePickClick"></date-table>
2016-12-12 20:34:28 +08:00
<year-table
ref="yearTable"
2016-12-15 20:16:58 +08:00
v-show="currentView === 'year'"
:year="year"
:date="date"
2016-12-16 09:18:35 +08:00
:selection-mode="selectionMode"
2016-12-15 20:16:58 +08:00
:disabled-date="disabledDate"
2016-12-22 09:18:11 +08:00
@on-pick="handleYearPick"
@on-pick-click="handlePickClick"></year-table>
2016-12-12 20:34:28 +08:00
<month-table
ref="monthTable"
2016-12-15 20:16:58 +08:00
v-show="currentView === 'month'"
:month="month"
:date="date"
2016-12-16 09:18:35 +08:00
:selection-mode="selectionMode"
2016-12-15 20:16:58 +08:00
:disabled-date="disabledDate"
2016-12-22 09:18:11 +08:00
@on-pick="handleMonthPick"
@on-pick-click="handlePickClick"></month-table>
<time-picker
ref="timePicker"
show-date
v-show="currentView === 'time'"
@on-pick="handleTimePick"
@on-pick-click="handlePickClick"></time-picker>
2016-12-12 20:34:28 +08:00
</div>
<Confirm
v-if="confirm"
:show-time="showTime"
:is-time="isTime"
@on-pick-toggle-time="handleToggleTime"
@on-pick-clear="handlePickClear"
@on-pick-success="handlePickSuccess"></Confirm>
2016-12-12 20:34:28 +08:00
</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';
import TimePicker from './time.vue';
import Confirm from '../base/confirm.vue';
2016-12-12 20:34:28 +08:00
2016-12-19 14:44:07 +08:00
import Mixin from './mixin';
import Locale from '../../../mixins/locale';
2016-12-19 14:44:07 +08:00
2017-09-26 16:57:01 +02:00
import { initTimeDate, siblingMonth } 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 {
name: 'DatePicker',
mixins: [ Mixin, Locale ],
components: { Icon, DateTable, YearTable, MonthTable, TimePicker, Confirm },
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: initTimeDate(),
2016-12-14 23:08:57 +08:00
value: '',
showTime: false,
selectionMode: 'day',
disabledDate: '',
year: null,
month: null,
confirm: false,
isTime: false,
format: 'yyyy-MM-dd'
2016-12-25 22:49:42 +08:00
};
2016-12-14 23:08:57 +08:00
},
computed: {
2016-12-16 14:15:11 +08:00
classes () {
return [
`${prefixCls}-body-wrapper`,
{
[`${prefixCls}-with-sidebar`]: this.shortcuts.length
}
2016-12-25 22:49:42 +08:00
];
2016-12-16 14:15:11 +08:00
},
2016-12-15 20:16:58 +08:00
yearLabel () {
const tYear = this.t('i.datepicker.year');
2016-12-15 20:16:58 +08:00
const year = this.year;
if (!year) return '';
if (this.currentView === 'year') {
const startYear = Math.floor(year / 10) * 10;
return `${startYear}${tYear} - ${startYear + 9}${tYear}`;
2016-12-15 20:16:58 +08:00
}
return `${year}${tYear}`;
},
monthLabel () {
const month = this.month + 1;
return this.t(`i.datepicker.month${month}`);
2016-12-15 20:16:58 +08:00
}
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)) {
this.date = newVal;
2017-09-26 16:57:01 +02:00
this.setMonthYear(newVal);
2016-12-14 23:08:57 +08:00
}
if (this.showTime) this.$refs.timePicker.value = newVal;
},
date (val) {
if (this.showTime) this.$refs.timePicker.date = val;
},
format (val) {
if (this.showTime) this.$refs.timePicker.format = val;
},
currentView (val) {
if (val === 'time') this.$refs.timePicker.updateScroll();
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-19 22:18:57 +08:00
resetDate () {
this.date = new Date(this.date);
},
2017-09-26 16:57:01 +02:00
setMonthYear(date){
this.month = date.getMonth();
this.year = date.getFullYear();
},
handleClear () {
2016-12-15 20:16:58 +08:00
this.date = new Date();
this.$emit('on-pick', '');
if (this.showTime) this.$refs.timePicker.handleClear();
2016-12-15 20:16:58 +08:00
},
resetView (reset = false) {
if (this.currentView !== 'time' || reset) {
if (this.selectionMode === 'month') {
this.currentView = 'month';
} else if (this.selectionMode === 'year') {
this.currentView = 'year';
} else {
this.currentView = 'date';
}
2016-12-19 21:09:57 +08:00
}
2017-09-26 16:57:01 +02:00
this.setMonthYear(this.date);
2017-01-13 20:32:24 +08:00
if (reset) this.isTime = false;
2016-12-15 20:16:58 +08:00
},
2017-09-26 16:57:01 +02:00
changeYear(dir){
2016-12-15 20:16:58 +08:00
if (this.currentView === 'year') {
2017-09-26 16:57:01 +02:00
this.$refs.yearTable[dir == 1 ? 'nextTenYear' : 'prevTenYear']();
2016-12-15 20:16:58 +08:00
} else {
2017-09-26 16:57:01 +02:00
this.year+= dir;
this.date = siblingMonth(this.date, dir * 12);
2016-12-15 20:16:58 +08:00
}
2016-12-12 20:34:28 +08:00
},
2017-09-26 16:57:01 +02:00
changeMonth(dir){
this.date = siblingMonth(this.date, dir);
this.setMonthYear(this.date);
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';
},
handleToggleTime () {
if (this.currentView === 'date') {
this.currentView = 'time';
this.isTime = true;
} else if (this.currentView === 'time') {
this.currentView = 'date';
this.isTime = false;
}
},
2016-12-15 20:16:58 +08:00
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') {
2016-12-15 23:41:06 +08:00
this.$emit('on-pick', new Date(year, 0, 1));
2016-12-15 20:16:58 +08:00
} else {
this.currentView = 'month';
}
this.resetDate();
},
handleMonthPick (month) {
this.month = month;
2017-09-26 16:57:01 +02:00
this.date.setMonth(month);
if (this.selectionMode !== 'month') {
2016-12-15 20:16:58 +08:00
this.currentView = 'date';
this.resetDate();
} else {
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') {
this.$emit('on-pick', new Date(value.getTime()));
2017-09-26 16:57:01 +02:00
this.date = new Date(value);
2016-12-15 20:16:58 +08:00
}
},
handleTimePick (date) {
this.handleDatePick(date);
2016-12-12 20:34:28 +08:00
}
},
mounted () {
2016-12-14 23:08:57 +08:00
if (this.selectionMode === 'month') {
this.currentView = 'month';
}
if (this.date && !this.year) {
2017-09-26 16:57:01 +02:00
this.setMonthYear(this.date);
2016-12-14 23:08:57 +08:00
}
if (this.showTime) {
// todo 这里可能有问题,并不能进入到这里,但不影响正常使用
this.$refs.timePicker.date = this.date;
this.$refs.timePicker.value = this.value;
this.$refs.timePicker.format = this.format;
this.$refs.timePicker.showDate = true;
}
2016-12-12 20:34:28 +08:00
}
2016-12-25 22:49:42 +08:00
};
</script>