2020-07-22 07:33:57 +00:00
|
|
|
import { inject } 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-03-18 06:52:02 +00:00
|
|
|
import { initDefaultProps } from '../_util/props-util';
|
2020-09-30 02:47:18 +00:00
|
|
|
import { defaultConfigProvider } from '../config-provider';
|
2020-03-07 11:45:13 +00:00
|
|
|
|
|
|
|
const { Option } = Select;
|
|
|
|
|
|
|
|
function getMonthsLocale(value) {
|
|
|
|
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;
|
|
|
|
}
|
2018-03-13 14:40:13 +00:00
|
|
|
|
|
|
|
export const HeaderProps = {
|
|
|
|
prefixCls: PropTypes.string,
|
|
|
|
locale: PropTypes.any,
|
|
|
|
fullscreen: PropTypes.boolean,
|
|
|
|
yearSelectOffset: PropTypes.number,
|
|
|
|
yearSelectTotal: PropTypes.number,
|
|
|
|
type: PropTypes.string,
|
|
|
|
value: PropTypes.any,
|
2018-04-06 12:56:19 +00:00
|
|
|
validRange: PropTypes.array,
|
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
|
|
|
|
|
|
|
export default {
|
2020-07-22 07:33:57 +00:00
|
|
|
name: 'CalendarHeader',
|
|
|
|
inheritAttrs: false,
|
2018-03-13 14:40:13 +00:00
|
|
|
props: initDefaultProps(HeaderProps, {
|
|
|
|
yearSelectOffset: 10,
|
|
|
|
yearSelectTotal: 20,
|
|
|
|
}),
|
2020-07-22 07:33:57 +00:00
|
|
|
setup() {
|
|
|
|
return {
|
2020-09-30 02:47:18 +00:00
|
|
|
configProvider: inject('configProvider', defaultConfigProvider),
|
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: {
|
2019-03-13 05:34:40 +00:00
|
|
|
getYearSelectElement(prefixCls, year) {
|
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
|
|
|
},
|
|
|
|
|
2019-03-13 05:34:40 +00:00
|
|
|
getMonthSelectElement(prefixCls, month, months) {
|
|
|
|
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
|
|
|
},
|
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
onYearChange(year) {
|
|
|
|
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
|
|
|
},
|
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
onMonthChange(month) {
|
|
|
|
const newValue = this.value.clone();
|
|
|
|
newValue.month(parseInt(month, 10));
|
|
|
|
this.$emit('valueChange', newValue);
|
2018-03-13 14:40:13 +00:00
|
|
|
},
|
|
|
|
|
2020-03-07 11:45:13 +00:00
|
|
|
onInternalTypeChange(e) {
|
2020-07-22 07:33:57 +00:00
|
|
|
this.triggerTypeChange(e.target.value);
|
2020-03-07 11:45:13 +00:00
|
|
|
},
|
|
|
|
|
2020-07-22 07:33:57 +00:00
|
|
|
triggerTypeChange(val) {
|
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-07-22 07:33:57 +00:00
|
|
|
triggerValueChange() {
|
2020-03-07 11:45:13 +00:00
|
|
|
this.$emit('valueChange', ...arguments);
|
|
|
|
},
|
2020-07-22 07:33:57 +00:00
|
|
|
saveCalendarHeaderNode(node) {
|
|
|
|
this.calendarHeaderNode = node;
|
|
|
|
},
|
2020-03-07 11:45:13 +00:00
|
|
|
headerRenderCustom(headerRender) {
|
|
|
|
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-07-22 07:33:57 +00:00
|
|
|
<div class={`${prefixCls}-header`} ref={this.saveCalendarHeaderNode}>
|
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
|
|
|
},
|
2019-01-12 03:33:27 +00:00
|
|
|
};
|