123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503 |
- <template>
- <table
- cellspacing="0"
- cellpadding="0"
- class="el-date-table"
- @click="handleClick"
- @mousemove="handleMouseMove"
- :class="{ 'is-week-mode': selectionMode === 'week' }">
- <tbody>
- <tr>
- <th v-if="showWeekNumber">{{ t('el.datepicker.week') }}</th>
- <th v-for="(week, key) in WEEKS" :key="key">{{ t('el.datepicker.weeks.' + week) }}</th>
- </tr>
- <tr
- class="el-date-table__row"
- v-for="(row, key) in rows"
- :class="{ current: isWeekActive(row[1]) }"
- :key="key">
- <td
- v-for="(cell, key) in row"
- :class="getCellClasses(cell)"
- :key="key">
- <div>
- <span>
- {{ cell.text }}
- </span>
- </div>
- </td>
- </tr>
- </tbody>
- </table>
- </template>
- <script>
- import { getFirstDayOfMonth, getDayCountOfMonth, getWeekNumber, getStartDateOfMonth, nextDate, isDate } from '../util';
- import { hasClass } from 'element-ui/src/utils/dom';
- import Locale from 'element-ui/src/mixins/locale';
- const WEEKS = ['sun', 'mon', 'tue', 'wed', 'thu', 'fri', 'sat'];
- const clearHours = function(time) {
- const cloneDate = new Date(time);
- cloneDate.setHours(0, 0, 0, 0);
- return cloneDate.getTime();
- };
- export default {
- mixins: [Locale],
- props: {
- firstDayOfWeek: {
- default: 7,
- type: Number,
- validator: val => val >= 1 && val <= 7
- },
- value: {},
- defaultValue: {
- validator(val) {
- // either: null, valid Date object, Array of valid Date objects
- return val === null || isDate(val) || (Array.isArray(val) && val.every(isDate));
- }
- },
- date: {},
- selectionMode: {
- default: 'day'
- },
- showWeekNumber: {
- type: Boolean,
- default: false
- },
- disabledDate: {},
- selectedDate: {
- type: Array
- },
- minDate: {},
- maxDate: {},
- rangeState: {
- default() {
- return {
- endDate: null,
- selecting: false,
- row: null,
- column: null
- };
- }
- }
- },
- computed: {
- offsetDay() {
- const week = this.firstDayOfWeek;
- // 周日为界限,左右偏移的天数,3217654 例如周一就是 -1,目的是调整前两行日期的位置
- return week > 3 ? 7 - week : -week;
- },
- WEEKS() {
- const week = this.firstDayOfWeek;
- return WEEKS.concat(WEEKS).slice(week, week + 7);
- },
- year() {
- return this.date.getFullYear();
- },
- month() {
- return this.date.getMonth();
- },
- startDate() {
- return getStartDateOfMonth(this.year, this.month);
- },
- rows() {
- // TODO: refactory rows / getCellClasses
- const date = new Date(this.year, this.month, 1);
- let day = getFirstDayOfMonth(date); // day of first day
- const dateCountOfMonth = getDayCountOfMonth(date.getFullYear(), date.getMonth());
- const dateCountOfLastMonth = getDayCountOfMonth(date.getFullYear(), (date.getMonth() === 0 ? 11 : date.getMonth() - 1));
- day = (day === 0 ? 7 : day);
- const offset = this.offsetDay;
- const rows = this.tableRows;
- let count = 1;
- let firstDayPosition;
- const startDate = this.startDate;
- const disabledDate = this.disabledDate;
- const selectedDate = this.selectedDate || this.value;
- const now = clearHours(new Date());
- for (let i = 0; i < 6; i++) {
- const row = rows[i];
- if (this.showWeekNumber) {
- if (!row[0]) {
- row[0] = { type: 'week', text: getWeekNumber(nextDate(startDate, i * 7 + 1)) };
- }
- }
- for (let j = 0; j < 7; j++) {
- let cell = row[this.showWeekNumber ? j + 1 : j];
- if (!cell) {
- cell = { row: i, column: j, type: 'normal', inRange: false, start: false, end: false };
- }
- cell.type = 'normal';
- const index = i * 7 + j;
- const time = nextDate(startDate, index - offset).getTime();
- cell.inRange = time >= clearHours(this.minDate) && time <= clearHours(this.maxDate);
- cell.start = this.minDate && time === clearHours(this.minDate);
- cell.end = this.maxDate && time === clearHours(this.maxDate);
- const isToday = time === now;
- if (isToday) {
- cell.type = 'today';
- }
- if (i >= 0 && i <= 1) {
- if (j + i * 7 >= (day + offset)) {
- cell.text = count++;
- if (count === 2) {
- firstDayPosition = i * 7 + j;
- }
- } else {
- cell.text = dateCountOfLastMonth - (day + offset - j % 7) + 1 + i * 7;
- cell.type = 'prev-month';
- }
- } else {
- if (count <= dateCountOfMonth) {
- cell.text = count++;
- if (count === 2) {
- firstDayPosition = i * 7 + j;
- }
- } else {
- cell.text = count++ - dateCountOfMonth;
- cell.type = 'next-month';
- }
- }
- let newDate = new Date(time);
- cell.disabled = typeof disabledDate === 'function' && disabledDate(newDate);
- cell.selected = Array.isArray(selectedDate) &&
- selectedDate.filter(date => date.toString() === newDate.toString())[0];
- this.$set(row, this.showWeekNumber ? j + 1 : j, cell);
- }
- if (this.selectionMode === 'week') {
- const start = this.showWeekNumber ? 1 : 0;
- const end = this.showWeekNumber ? 7 : 6;
- const isWeekActive = this.isWeekActive(row[start + 1]);
- row[start].inRange = isWeekActive;
- row[start].start = isWeekActive;
- row[end].inRange = isWeekActive;
- row[end].end = isWeekActive;
- }
- }
- rows.firstDayPosition = firstDayPosition;
- return rows;
- }
- },
- watch: {
- 'rangeState.endDate'(newVal) {
- this.markRange(newVal);
- },
- minDate(newVal, oldVal) {
- if (newVal && !oldVal) {
- this.rangeState.selecting = true;
- this.markRange(newVal);
- } else if (!newVal) {
- this.rangeState.selecting = false;
- this.markRange(newVal);
- } else {
- this.markRange();
- }
- },
- maxDate(newVal, oldVal) {
- if (newVal && !oldVal) {
- this.rangeState.selecting = false;
- this.markRange(newVal);
- }
- }
- },
- data() {
- return {
- tableRows: [ [], [], [], [], [], [] ]
- };
- },
- methods: {
- cellMatchesDate(cell, date) {
- const value = new Date(date);
- return this.year === value.getFullYear() &&
- this.month === value.getMonth() &&
- Number(cell.text) === value.getDate();
- },
- getCellClasses(cell) {
- const selectionMode = this.selectionMode;
- const defaultValue = this.defaultValue ? Array.isArray(this.defaultValue) ? this.defaultValue : [this.defaultValue] : [];
- let classes = [];
- if ((cell.type === 'normal' || cell.type === 'today') && !cell.disabled) {
- classes.push('available');
- if (cell.type === 'today') {
- classes.push('today');
- }
- } else {
- classes.push(cell.type);
- }
- if (cell.type === 'normal' && defaultValue.some(date => this.cellMatchesDate(cell, date))) {
- classes.push('default');
- }
- if (selectionMode === 'day' && (cell.type === 'normal' || cell.type === 'today') && this.cellMatchesDate(cell, this.value)) {
- classes.push('current');
- }
- if (cell.inRange && ((cell.type === 'normal' || cell.type === 'today') || this.selectionMode === 'week')) {
- classes.push('in-range');
- if (cell.start) {
- classes.push('start-date');
- }
- if (cell.end) {
- classes.push('end-date');
- }
- }
- if (cell.disabled) {
- classes.push('disabled');
- }
- if (cell.selected) {
- classes.push('selected');
- }
- return classes.join(' ');
- },
- getDateOfCell(row, column) {
- const offsetFromStart = row * 7 + (column - (this.showWeekNumber ? 1 : 0)) - this.offsetDay;
- return nextDate(this.startDate, offsetFromStart);
- },
- isWeekActive(cell) {
- if (this.selectionMode !== 'week') return false;
- const newDate = new Date(this.year, this.month, 1);
- const year = newDate.getFullYear();
- const month = newDate.getMonth();
- if (cell.type === 'prev-month') {
- newDate.setMonth(month === 0 ? 11 : month - 1);
- newDate.setFullYear(month === 0 ? year - 1 : year);
- }
- if (cell.type === 'next-month') {
- newDate.setMonth(month === 11 ? 0 : month + 1);
- newDate.setFullYear(month === 11 ? year + 1 : year);
- }
- newDate.setDate(parseInt(cell.text, 10));
- const valueYear = isDate(this.value) ? this.value.getFullYear() : null;
- return year === valueYear && getWeekNumber(newDate) === getWeekNumber(this.value);
- },
- markRange(maxDate) {
- const startDate = this.startDate;
- if (!maxDate) {
- maxDate = this.maxDate;
- }
- const rows = this.rows;
- const minDate = this.minDate;
- for (let i = 0, k = rows.length; i < k; i++) {
- const row = rows[i];
- for (let j = 0, l = row.length; j < l; j++) {
- if (this.showWeekNumber && j === 0) continue;
- const cell = row[j];
- const index = i * 7 + j + (this.showWeekNumber ? -1 : 0);
- const time = nextDate(startDate, index - this.offsetDay).getTime();
- if (maxDate && maxDate < minDate) {
- cell.inRange = minDate && time >= clearHours(maxDate) && time <= clearHours(minDate);
- cell.start = maxDate && time === clearHours(maxDate.getTime());
- cell.end = minDate && time === clearHours(minDate.getTime());
- } else {
- cell.inRange = minDate && time >= clearHours(minDate) && time <= clearHours(maxDate);
- cell.start = minDate && time === clearHours(minDate.getTime());
- cell.end = maxDate && time === clearHours(maxDate.getTime());
- }
- }
- }
- },
- handleMouseMove(event) {
- if (!this.rangeState.selecting) return;
- this.$emit('changerange', {
- minDate: this.minDate,
- maxDate: this.maxDate,
- rangeState: this.rangeState
- });
- let target = event.target;
- if (target.tagName === 'SPAN') {
- target = target.parentNode.parentNode;
- }
- if (target.tagName === 'DIV') {
- target = target.parentNode;
- }
- if (target.tagName !== 'TD') return;
- const column = target.cellIndex;
- const row = target.parentNode.rowIndex - 1;
- const { row: oldRow, column: oldColumn } = this.rangeState;
- if (oldRow !== row || oldColumn !== column) {
- this.rangeState.row = row;
- this.rangeState.column = column;
- this.rangeState.endDate = this.getDateOfCell(row, column);
- }
- },
- handleClick(event) {
- let target = event.target;
- if (target.tagName === 'SPAN') {
- target = target.parentNode.parentNode;
- }
- if (target.tagName === 'DIV') {
- target = target.parentNode;
- }
- if (target.tagName !== 'TD') return;
- if (hasClass(target, 'disabled') || hasClass(target, 'week')) return;
- const selectionMode = this.selectionMode;
- if (selectionMode === 'week') {
- target = target.parentNode.cells[1];
- }
- let year = Number(this.year);
- let month = Number(this.month);
- const cellIndex = target.cellIndex;
- const rowIndex = target.parentNode.rowIndex;
- const cell = this.rows[rowIndex - 1][cellIndex];
- const text = cell.text;
- const className = target.className;
- const newDate = new Date(year, month, 1);
- if (className.indexOf('prev') !== -1) {
- if (month === 0) {
- year = year - 1;
- month = 11;
- } else {
- month = month - 1;
- }
- newDate.setFullYear(year);
- newDate.setMonth(month);
- } else if (className.indexOf('next') !== -1) {
- if (month === 11) {
- year = year + 1;
- month = 0;
- } else {
- month = month + 1;
- }
- newDate.setFullYear(year);
- newDate.setMonth(month);
- }
- newDate.setDate(parseInt(text, 10));
- if (this.selectionMode === 'range') {
- if (this.minDate && this.maxDate) {
- const minDate = new Date(newDate.getTime());
- const maxDate = null;
- this.$emit('pick', { minDate, maxDate }, false);
- this.rangeState.selecting = true;
- this.markRange(this.minDate);
- this.$nextTick(() => {
- this.handleMouseMove(event);
- });
- } else if (this.minDate && !this.maxDate) {
- if (newDate >= this.minDate) {
- const maxDate = new Date(newDate.getTime());
- this.rangeState.selecting = false;
- this.$emit('pick', {
- minDate: this.minDate,
- maxDate
- });
- } else {
- const minDate = new Date(newDate.getTime());
- this.rangeState.selecting = false;
- this.$emit('pick', { minDate, maxDate: this.minDate });
- }
- } else if (!this.minDate) {
- const minDate = new Date(newDate.getTime());
- this.$emit('pick', { minDate, maxDate: this.maxDate }, false);
- this.rangeState.selecting = true;
- this.markRange(this.minDate);
- }
- } else if (selectionMode === 'day') {
- this.$emit('pick', newDate);
- } else if (selectionMode === 'week') {
- const weekNumber = getWeekNumber(newDate);
- const value = newDate.getFullYear() + 'w' + weekNumber;
- this.$emit('pick', {
- year: newDate.getFullYear(),
- week: weekNumber,
- value: value,
- date: newDate
- });
- } else if (selectionMode === 'dates') {
- let selectedDate = this.selectedDate;
- if (!cell.selected) {
- selectedDate.push(newDate);
- } else {
- selectedDate.forEach((date, index) => {
- if (date.toString() === newDate.toString()) {
- selectedDate.splice(index, 1);
- }
- });
- }
- this.$emit('select', selectedDate);
- }
- }
- }
- };
- </script>
|