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

293 lines
11 KiB
Vue
Raw Normal View History

2016-12-12 10:37:52 +08:00
<template>
2016-12-16 14:15:11 +08:00
<div :class="classes">
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')"
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"
v-show="currentView === 'date'">{{ monthLabel }}</span>
2016-12-15 20:16:58 +08:00
<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"
: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
2016-12-15 20:16:58 +08:00
v-ref:year-table
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
2016-12-15 20:16:58 +08:00
v-ref:month-table
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
v-ref:time-picker
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
import { initTimeDate } 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;
this.year = newVal.getFullYear();
this.month = newVal.getMonth();
}
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);
},
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
}
this.year = this.date.getFullYear();
this.month = this.date.getMonth();
2016-12-15 20:16:58 +08:00
},
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';
},
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;
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') {
this.$emit('on-pick', new Date(value.getTime()));
2016-12-15 20:16:58 +08:00
this.date.setFullYear(value.getFullYear());
this.date.setMonth(value.getMonth());
this.date.setDate(value.getDate());
}
this.resetDate();
},
handleTimePick (date) {
this.handleDatePick(date);
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();
}
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>