2021-06-26 09:35:40 +08:00
|
|
|
import type { PropType } from 'vue';
|
|
|
|
import { defineComponent, inject } from 'vue';
|
2019-01-12 11:33:27 +08:00
|
|
|
import Select from '../select';
|
|
|
|
import { Group, Button } from '../radio';
|
|
|
|
import PropTypes from '../_util/vue-types';
|
2020-09-30 10:47:18 +08:00
|
|
|
import { defaultConfigProvider } from '../config-provider';
|
2021-06-26 09:35:40 +08:00
|
|
|
import type { VueNode } from '../_util/type';
|
|
|
|
import type moment from 'moment';
|
|
|
|
import type { RadioChangeEvent } from '../radio/interface';
|
2020-03-07 19:45:13 +08:00
|
|
|
|
2020-10-19 22:19:33 +08:00
|
|
|
function getMonthsLocale(value: moment.Moment): string[] {
|
2020-03-07 19:45:13 +08:00
|
|
|
const current = value.clone();
|
|
|
|
const localeData = value.localeData();
|
|
|
|
const months = [];
|
|
|
|
for (let i = 0; i < 12; i++) {
|
|
|
|
current.month(i);
|
|
|
|
months.push(localeData.monthsShort(current));
|
|
|
|
}
|
|
|
|
return months;
|
|
|
|
}
|
2020-10-13 19:02:03 +08:00
|
|
|
export interface RenderHeader {
|
|
|
|
value: moment.Moment;
|
|
|
|
onChange?: (value: moment.Moment) => void;
|
|
|
|
type: string;
|
|
|
|
onTypeChange: (type: string) => void;
|
|
|
|
}
|
|
|
|
export type HeaderRender = (headerRender: RenderHeader) => VueNode;
|
2018-03-13 22:40:13 +08:00
|
|
|
export const HeaderProps = {
|
|
|
|
prefixCls: PropTypes.string,
|
|
|
|
locale: PropTypes.any,
|
2020-10-12 18:46:05 +08:00
|
|
|
fullscreen: PropTypes.looseBool,
|
2018-03-13 22:40:13 +08:00
|
|
|
yearSelectOffset: PropTypes.number,
|
|
|
|
yearSelectTotal: PropTypes.number,
|
|
|
|
type: PropTypes.string,
|
2020-10-13 19:02:03 +08:00
|
|
|
value: {
|
2020-10-19 16:43:10 +08:00
|
|
|
type: Object as PropType<moment.Moment>,
|
2020-10-13 19:02:03 +08:00
|
|
|
},
|
|
|
|
validRange: {
|
2020-10-19 16:43:10 +08:00
|
|
|
type: Array as PropType<moment.Moment[]>,
|
2020-10-13 19:02:03 +08:00
|
|
|
},
|
2020-03-07 19:45:13 +08:00
|
|
|
headerRender: PropTypes.func,
|
2020-07-22 15:33:57 +08:00
|
|
|
onValueChange: PropTypes.func,
|
|
|
|
onTypeChange: PropTypes.func,
|
2019-01-12 11:33:27 +08:00
|
|
|
};
|
2018-03-13 22:40:13 +08:00
|
|
|
|
2020-10-13 19:02:03 +08:00
|
|
|
export default defineComponent({
|
2020-07-22 15:33:57 +08:00
|
|
|
name: 'CalendarHeader',
|
|
|
|
inheritAttrs: false,
|
2020-10-13 19:02:03 +08:00
|
|
|
props: {
|
|
|
|
...HeaderProps,
|
|
|
|
yearSelectOffset: PropTypes.number.def(10),
|
|
|
|
yearSelectTotal: PropTypes.number.def(20),
|
|
|
|
},
|
2020-07-22 15:33:57 +08:00
|
|
|
setup() {
|
|
|
|
return {
|
2020-09-30 10:47:18 +08:00
|
|
|
configProvider: inject('configProvider', defaultConfigProvider),
|
2020-10-19 16:43:10 +08:00
|
|
|
calendarHeaderNode: undefined,
|
2020-07-22 15:33:57 +08:00
|
|
|
};
|
2019-03-13 13:34:40 +08:00
|
|
|
},
|
2020-07-22 15:33:57 +08:00
|
|
|
// private calendarHeaderNode: HTMLDivElement;
|
2018-03-13 22:40:13 +08:00
|
|
|
methods: {
|
2020-10-13 19:02:03 +08:00
|
|
|
getYearSelectElement(prefixCls: string, year: number) {
|
2020-03-07 19:45:13 +08:00
|
|
|
const { yearSelectOffset, yearSelectTotal, locale = {}, fullscreen, validRange } = this;
|
2019-01-12 11:33:27 +08:00
|
|
|
let start = year - yearSelectOffset;
|
|
|
|
let end = start + yearSelectTotal;
|
2018-04-06 20:56:19 +08:00
|
|
|
if (validRange) {
|
2019-01-12 11:33:27 +08:00
|
|
|
start = validRange[0].get('year');
|
|
|
|
end = validRange[1].get('year') + 1;
|
2018-04-06 20:56:19 +08:00
|
|
|
}
|
2020-10-19 22:19:33 +08:00
|
|
|
const suffix = locale && locale.year === '年' ? '年' : '';
|
|
|
|
const options: { label: string; value: number }[] = [];
|
2018-03-13 22:40:13 +08:00
|
|
|
for (let index = start; index < end; index++) {
|
2020-10-19 22:19:33 +08:00
|
|
|
options.push({ label: `${index}${suffix}`, value: index });
|
2018-03-13 22:40:13 +08:00
|
|
|
}
|
|
|
|
return (
|
|
|
|
<Select
|
2020-10-19 22:19:33 +08:00
|
|
|
size={fullscreen ? undefined : 'small'}
|
2018-03-13 22:40:13 +08:00
|
|
|
class={`${prefixCls}-year-select`}
|
|
|
|
onChange={this.onYearChange}
|
2020-10-24 23:52:51 +08:00
|
|
|
value={year}
|
2020-10-19 22:19:33 +08:00
|
|
|
options={options}
|
2020-07-23 15:15:38 +08:00
|
|
|
getPopupContainer={() => this.calendarHeaderNode}
|
2020-10-19 22:19:33 +08:00
|
|
|
></Select>
|
2019-01-12 11:33:27 +08:00
|
|
|
);
|
2018-03-13 22:40:13 +08:00
|
|
|
},
|
|
|
|
|
2020-10-19 22:19:33 +08:00
|
|
|
getMonthSelectElement(prefixCls: string, month: number, months: string[]) {
|
2019-03-13 13:34:40 +08:00
|
|
|
const { fullscreen, validRange, value } = this;
|
2019-01-12 11:33:27 +08:00
|
|
|
let start = 0;
|
2021-04-10 22:15:52 +08:00
|
|
|
let end = 11;
|
2018-04-06 20:56:19 +08:00
|
|
|
if (validRange) {
|
2019-01-12 11:33:27 +08:00
|
|
|
const [rangeStart, rangeEnd] = validRange;
|
|
|
|
const currentYear = value.get('year');
|
2018-04-06 20:56:19 +08:00
|
|
|
if (rangeEnd.get('year') === currentYear) {
|
2019-01-12 11:33:27 +08:00
|
|
|
end = rangeEnd.get('month') + 1;
|
2019-03-13 13:34:40 +08:00
|
|
|
}
|
|
|
|
if (rangeStart.get('year') === currentYear) {
|
2019-01-12 11:33:27 +08:00
|
|
|
start = rangeStart.get('month');
|
2018-04-06 20:56:19 +08:00
|
|
|
}
|
|
|
|
}
|
2020-10-19 22:19:33 +08:00
|
|
|
const options: { label: string; value: number }[] = [];
|
|
|
|
for (let index = start; index <= end; index += 1) {
|
|
|
|
options.push({
|
|
|
|
label: months[index],
|
|
|
|
value: index,
|
|
|
|
});
|
2018-03-13 22:40:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Select
|
2020-10-19 22:19:33 +08:00
|
|
|
size={fullscreen ? undefined : 'small'}
|
2018-03-13 22:40:13 +08:00
|
|
|
class={`${prefixCls}-month-select`}
|
2020-10-19 22:19:33 +08:00
|
|
|
value={month}
|
|
|
|
options={options}
|
2018-03-13 22:40:13 +08:00
|
|
|
onChange={this.onMonthChange}
|
2020-07-22 15:33:57 +08:00
|
|
|
getPopupContainer={() => this.calendarHeaderNode}
|
2020-10-19 22:19:33 +08:00
|
|
|
></Select>
|
2019-01-12 11:33:27 +08:00
|
|
|
);
|
2018-03-13 22:40:13 +08:00
|
|
|
},
|
|
|
|
|
2020-10-19 16:43:10 +08:00
|
|
|
onYearChange(year: string) {
|
2019-01-12 11:33:27 +08:00
|
|
|
const { value, validRange } = this;
|
|
|
|
const newValue = value.clone();
|
|
|
|
newValue.year(parseInt(year, 10));
|
2018-04-06 20:56:19 +08:00
|
|
|
// switch the month so that it remains within range when year changes
|
|
|
|
if (validRange) {
|
2019-01-12 11:33:27 +08:00
|
|
|
const [start, end] = validRange;
|
|
|
|
const newYear = newValue.get('year');
|
|
|
|
const newMonth = newValue.get('month');
|
2018-04-06 20:56:19 +08:00
|
|
|
if (newYear === end.get('year') && newMonth > end.get('month')) {
|
2019-01-12 11:33:27 +08:00
|
|
|
newValue.month(end.get('month'));
|
2018-04-06 20:56:19 +08:00
|
|
|
}
|
|
|
|
if (newYear === start.get('year') && newMonth < start.get('month')) {
|
2019-01-12 11:33:27 +08:00
|
|
|
newValue.month(start.get('month'));
|
2018-04-06 20:56:19 +08:00
|
|
|
}
|
|
|
|
}
|
2019-01-12 11:33:27 +08:00
|
|
|
this.$emit('valueChange', newValue);
|
2018-03-13 22:40:13 +08:00
|
|
|
},
|
|
|
|
|
2020-10-19 16:43:10 +08:00
|
|
|
onMonthChange(month: string) {
|
2019-01-12 11:33:27 +08:00
|
|
|
const newValue = this.value.clone();
|
|
|
|
newValue.month(parseInt(month, 10));
|
|
|
|
this.$emit('valueChange', newValue);
|
2018-03-13 22:40:13 +08:00
|
|
|
},
|
|
|
|
|
2020-11-13 10:51:12 +08:00
|
|
|
onInternalTypeChange(e: RadioChangeEvent) {
|
|
|
|
this.triggerTypeChange(e.target.value);
|
2020-03-07 19:45:13 +08:00
|
|
|
},
|
|
|
|
|
2020-10-19 16:43:10 +08:00
|
|
|
triggerTypeChange(val: string) {
|
2020-03-07 19:45:13 +08:00
|
|
|
this.$emit('typeChange', val);
|
2018-03-13 22:40:13 +08:00
|
|
|
},
|
2020-03-07 19:45:13 +08:00
|
|
|
getMonthYearSelections(getPrefixCls) {
|
|
|
|
const { prefixCls: customizePrefixCls, type, value } = this.$props;
|
|
|
|
|
|
|
|
const prefixCls = getPrefixCls('fullcalendar', customizePrefixCls);
|
|
|
|
const yearReactNode = this.getYearSelectElement(prefixCls, value.year());
|
|
|
|
const monthReactNode =
|
|
|
|
type === 'month'
|
|
|
|
? this.getMonthSelectElement(prefixCls, value.month(), getMonthsLocale(value))
|
|
|
|
: null;
|
|
|
|
return {
|
|
|
|
yearReactNode,
|
|
|
|
monthReactNode,
|
|
|
|
};
|
|
|
|
},
|
|
|
|
|
|
|
|
getTypeSwitch() {
|
|
|
|
const { locale = {}, type, fullscreen } = this.$props;
|
|
|
|
const size = fullscreen ? 'default' : 'small';
|
|
|
|
return (
|
|
|
|
<Group onChange={this.onInternalTypeChange} value={type} size={size}>
|
|
|
|
<Button value="month">{locale.month}</Button>
|
|
|
|
<Button value="year">{locale.year}</Button>
|
|
|
|
</Group>
|
|
|
|
);
|
|
|
|
},
|
2020-10-19 16:43:10 +08:00
|
|
|
triggerValueChange(...args: any[]) {
|
|
|
|
this.$emit('valueChange', ...args);
|
2020-03-07 19:45:13 +08:00
|
|
|
},
|
2020-10-13 19:02:03 +08:00
|
|
|
saveCalendarHeaderNode(node: HTMLElement) {
|
2020-07-22 15:33:57 +08:00
|
|
|
this.calendarHeaderNode = node;
|
|
|
|
},
|
2020-10-13 19:02:03 +08:00
|
|
|
headerRenderCustom(headerRender: HeaderRender) {
|
2020-03-07 19:45:13 +08:00
|
|
|
const { type, value } = this.$props;
|
|
|
|
return headerRender({
|
|
|
|
value,
|
|
|
|
type: type || 'month',
|
2020-07-22 15:33:57 +08:00
|
|
|
onChange: this.triggerValueChange,
|
|
|
|
onTypeChange: this.triggerTypeChange,
|
2020-03-07 19:45:13 +08:00
|
|
|
});
|
|
|
|
},
|
2018-03-13 22:40:13 +08:00
|
|
|
},
|
|
|
|
|
2019-01-12 11:33:27 +08:00
|
|
|
render() {
|
2020-03-07 19:45:13 +08:00
|
|
|
const { prefixCls: customizePrefixCls, headerRender } = this;
|
2019-09-11 22:35:25 +08:00
|
|
|
const getPrefixCls = this.configProvider.getPrefixCls;
|
2019-03-13 13:34:40 +08:00
|
|
|
const prefixCls = getPrefixCls('fullcalendar', customizePrefixCls);
|
2020-03-07 19:45:13 +08:00
|
|
|
const typeSwitch = this.getTypeSwitch();
|
|
|
|
const { yearReactNode, monthReactNode } = this.getMonthYearSelections(getPrefixCls);
|
|
|
|
return headerRender ? (
|
|
|
|
this.headerRenderCustom(headerRender)
|
|
|
|
) : (
|
2020-10-13 19:02:03 +08:00
|
|
|
<div class={`${prefixCls}-header`} ref={this.saveCalendarHeaderNode as any}>
|
2020-03-07 19:45:13 +08:00
|
|
|
{yearReactNode}
|
|
|
|
{monthReactNode}
|
2018-03-13 22:40:13 +08:00
|
|
|
{typeSwitch}
|
|
|
|
</div>
|
2019-01-12 11:33:27 +08:00
|
|
|
);
|
2018-03-13 22:40:13 +08:00
|
|
|
},
|
2020-10-13 19:02:03 +08:00
|
|
|
});
|