Skip to content

Commit

Permalink
Merge pull request #386 from treadpit/develop
Browse files Browse the repository at this point in the history
feat: add plugin of holidays
  • Loading branch information
todrfu authored Oct 12, 2020
2 parents d6d97cd + 84c524e commit ac9a14c
Show file tree
Hide file tree
Showing 6 changed files with 248 additions and 3 deletions.
36 changes: 34 additions & 2 deletions docs/v2/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -268,10 +268,12 @@ calendar.switchView('month').then(() => {});
## 日期范围选择
> 调用此方法默认打开 `chooseAreaMode` 配置,非连续性日期选择请调用 `setSelectedDates()`
> 只支持单个连续时间段
::: tip 提示 👇
调用此方法默认打开 `chooseAreaMode` 配置,非连续性日期选择请调用 `setSelectedDates()`
:::
```js
// 当连续时间为单天时
calendar.chooseDateArea(['2020-10-12']);
Expand All @@ -280,6 +282,36 @@ calendar.chooseDateArea(['2020-10-12']);
calendar.chooseDateArea(['2020-10-20', '2020-10-30'])
```
# 需引入插件: plugins/holidays/index.js
::: tip 提示 👇
节假日数据来源:[国务院办公厅_政府信息公开专栏](http://www.gov.cn/zhengce/content/2019-11/21/content_5454164.htm),目前仅支持了 `2020年份` 的数据,其他数据可按需自行补充 `/plugins/holidays/holidays-map.js`
:::
## 显示法定节假日班/休情况
配置日历config:
```js {2}
calendarConfig: {
showHolidays: true,
... // 更多配置待接入
}
```
## 获取当前年份节假日信息
```js
calendar.getHolidaysOfCurrentYear()
```
## 获取指定年份节假日信息
```js
const year = 2020
calendar.getHolidaysOfYear(year)
```
# 需引入插件: plugins/solarLunar.js
## 获取指定日期农历信息
Expand Down
1 change: 1 addition & 0 deletions docs/v2/guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ const conf = {
markToday: '', // 当天日期展示不使用默认数字,用特殊文字标记
takeoverTap: true, // 是否完全接管日期点击事件(日期不会选中)
chooseAreaMode: true, // 开启日期范围选择模式,该模式下只可选择时间段
showHolidays: true, // 显示法定节假日班/休情况,需引入holidays插件
highlightToday: true, // 是否高亮显示当天,区别于选中样式(初始化时当天高亮并不代表已选中当天)
defaultDate: '2018-3-6', // 默认选中指定某天,如需选中需配置 autoChoosedWhenJump: true
preventSwipe: true, // 是否禁用日历滑动切换月份
Expand Down
6 changes: 6 additions & 0 deletions src/component/v2/index.wxml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@
class="date-desc {{config.theme}}_date-desc date-desc-bottom {{(item.choosed || item.isToday) ? 'date-desc-bottom-always' : ''}} {{item.disable ? config.theme + '_date-desc-disable' : ''}}">
{{item.lunar.Term || item.lunar.IDayCn}}
</view>
<view
wx:if="{{config.showHolidays && item.label && !item.choosed}}"
class="date-desc {{config.theme + '_date-desc-lunar'}} date-desc-bottom"
style="background-color: {{item.label ? '' : item.color}}; color: {{item.color}}">
{{item.label}}
</view>
<view
wx:if="{{item.showTodoLabel && !calendar.todoLabelCircle}}"
class="{{item.todoText ? 'date-desc' : config.theme + '_todo-dot todo-dot'}} {{config.showLunar ? config.theme + '_date-desc-lunar' : ''}} {{calendar.todoLabelPos === 'bottom' ? 'date-desc-bottom todo-dot-bottom' : 'date-desc-top todo-dot-top'}} {{calendar.showLabelAlways && item.choosed && calendar.todoLabelPos === 'bottom' ? 'date-desc-bottom-always todo-dot-bottom-always' : ''}} {{calendar.showLabelAlways && item.choosed && calendar.todoLabelPos === 'top' ? 'date-desc-top-always todo-dot-top-always' : ''}}"
Expand Down
91 changes: 91 additions & 0 deletions src/component/v2/plugins/holidays/holidays-map.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/* *
@Author: drfu*
@Description: 数据来源于国务院办公厅关于2020年部分节假日安排的通知(国办发明电〔2019〕16号)_政府信息公开专栏,http://www.gov.cn/zhengce/content/2019-11/21/content_5454164.htm
@Date: 2020-10-12 14:29:45*
* @Last Modified by: drfu
* @Last Modified time: 2020-10-12 14:30:31
*/

export default {
2020: {
1: {
1: {
type: 'holiday',
name: '元旦',
text: '休'
},
19: {
type: 'work',
name: '调班',
text: '班'
},
'24-30': {
type: 'holiday',
name: '春节',
text: '休'
}
},
2: {
1: {
type: 'work',
name: '调班',
text: '班'
}
},
4: {
'4-6': {
type: 'holiday',
name: '清明节',
text: '休'
},
26: {
type: 'work',
name: '调班',
text: '班'
}
},
5: {
'1-5': {
type: 'holiday',
name: '劳动节',
text: '休'
},
9: {
type: 'work',
name: '调班',
text: '班'
}
},
6: {
'25-27': {
type: 'holiday',
name: '端午节',
text: '休'
},
28: {
type: 'work',
name: '调班',
text: '班'
}
},
9: {
27: {
type: 'work',
name: '调班',
text: '班'
}
},
10: {
'1-8': {
type: 'holiday',
name: '国庆节/中秋节',
text: '休'
},
10: {
type: 'work',
name: '调班',
text: '班'
}
}
}
}
112 changes: 112 additions & 0 deletions src/component/v2/plugins/holidays/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
/* *
@Author: drfu*
@Description: 显示法定节假日班/休情况
@Date: 2020-10-12 14:29:45*
* @Last Modified by: drfu
* @Last Modified time: 2020-10-12 14:30:51
*/

import holidays from './holidays-map'
import { dateUtil, getCalendarData, logger } from '../../utils/index'

function inHolidays({ year, month }, { start, end, current }) {
const getTimeStamp = dateUtil.getTimeStamp
const startTimestamp = getTimeStamp({
year,
month,
date: start
})
const endTimestamp = getTimeStamp({
year,
month,
date: end
})
const currentDateTimestamp = getTimeStamp({
year,
month,
date: current
})
if (
currentDateTimestamp >= startTimestamp &&
currentDateTimestamp <= endTimestamp
) {
return true
}
return false
}

export default () => {
return {
name: 'holidays',
beforeRender(calendarData = {}, calendarConfig = {}) {
let { dates = [] } = calendarData
if (calendarConfig.showHolidays) {
dates = dates.map(d => {
let item = { ...d }
const { year, month, date } = item
const hasHolidaysOfThisMonth = holidays[year] && holidays[year][month]
if (hasHolidaysOfThisMonth) {
const holidayDate = hasHolidaysOfThisMonth[date]
if (holidayDate) {
item.label = holidayDate.text
} else {
const holidayKeys = Object.keys(
hasHolidaysOfThisMonth
).filter(item => item.includes('-'))
let target = ''
for (let v of holidayKeys) {
const [start, end] = v.split('-')
if (+d.date >= +start && +d.date <= +end) {
target = v
break
}
}
const [start, end] = target.split('-')
const isInHolidays = inHolidays(
{
year,
month
},
{
start,
end,
current: date
}
)
if (isInHolidays) {
item.label = hasHolidaysOfThisMonth[target].text
}
}
}
return item
})
}
return {
calendarData: {
...calendarData,
dates: dates
},
calendarConfig
}
},
methods(component) {
return {
getHolidaysOfCurrentYear() {
const calendar = getCalendarData('calendar', component)
const { curYear } = calendar
return this.methods(component).getHolidaysOfYear(curYear)
},
getHolidaysOfYear(year) {
if (!year) return logger.warn('getHolidaysOfCurrentYear() 入参错误')
if (!holidays[year]) {
logger.warn('未匹配到当前年份节假日信息,请自行补充')
return {
err: 'not match'
}
}
return holidays[year]
}
}
}
}
}
5 changes: 4 additions & 1 deletion src/pages/calendarV2/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import selectable from '../../component/v2/plugins/selectable'
import solarLunar from '../../component/v2/plugins/solarLunar/index'
import timeRange from '../../component/v2/plugins/time-range'
import week from '../../component/v2/plugins/week'
import holidays from '../../component/v2/plugins/holidays/index'
import plugin from '../../component/v2/plugins/index'

plugin
Expand All @@ -11,11 +12,13 @@ plugin
.use(selectable)
.use(week)
.use(timeRange)
.use(holidays)

const conf = {
data: {
calendarConfig: {
theme: 'elegant'
// theme: 'elegant',
// showHolidays: true,
// chooseAreaMode: true
// defaultDate: '2020-9-8',
// autoChoosedWhenJump: true
Expand Down

0 comments on commit ac9a14c

Please sign in to comment.