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