@ -4,16 +4,16 @@
* @ Date : 2020 - 10 - 08 21 : 22 : 09 *
* @ Date : 2020 - 10 - 08 21 : 22 : 09 *
* @ Last Modified by : drfu
* @ Last Modified by : drfu
* @ Last Modified time : 2020 - 10 - 12 14 : 39 : 45
* @ Last Modified time : 2020 - 10 - 12 14 : 39 : 45
* * /
* /
import { calcJumpData } from '../core'
import { renderCalendar } from '../render'
import { renderCalendar } from '../render'
import {
import {
dateUtil ,
getCalendarConfig ,
getCalendarConfig ,
getCalendarData ,
getCalendarData ,
logger ,
logger ,
dateUtil
} from '../utils/index'
} from '../utils/index'
import { calcJumpData } from '../core'
/ * *
/ * *
* 当月第一周所有日期
* 当月第一周所有日期
@ -21,7 +21,7 @@ import { calcJumpData } from '../core'
function firstWeekInMonth (
function firstWeekInMonth (
target = { } ,
target = { } ,
calendarDates = [ ] ,
calendarDates = [ ] ,
calendarConfig = { }
calendarConfig = { } ,
) {
) {
const { firstDayOfWeek } = calendarConfig
const { firstDayOfWeek } = calendarConfig
const firstDayOfWeekIsMon = firstDayOfWeek === 'Mon'
const firstDayOfWeekIsMon = firstDayOfWeek === 'Mon'
@ -54,10 +54,11 @@ function lastWeekInMonth(target = {}, calendarDates = [], calendarConfig = {}) {
* 判断目标日期是否在某些指定日历内
* 判断目标日期是否在某些指定日历内
* /
* /
function dateIsInDatesRange ( target , dates ) {
function dateIsInDatesRange ( target , dates ) {
if ( ! target || ! dates || ! dates . length ) return false
if ( ! target || ! dates || ! dates . length )
return false
const targetDateStr = dateUtil . toTimeStr ( target )
const targetDateStr = dateUtil . toTimeStr ( target )
let rst = false
let rst = false
for ( le t date of dates ) {
for ( cons t date of dates ) {
const dateStr = dateUtil . toTimeStr ( date )
const dateStr = dateUtil . toTimeStr ( date )
if ( dateStr === targetDateStr ) {
if ( dateStr === targetDateStr ) {
rst = true
rst = true
@ -73,17 +74,17 @@ function getDatesWhenTargetInFirstWeek(target, firstWeekDates) {
const prevMonthInfo = dateUtil . getPrevMonthInfo ( { year , month } )
const prevMonthInfo = dateUtil . getPrevMonthInfo ( { year , month } )
let lastMonthDatesCount = dateUtil . getDatesCountOfMonth (
let lastMonthDatesCount = dateUtil . getDatesCountOfMonth (
prevMonthInfo . year ,
prevMonthInfo . year ,
prevMonthInfo . month
prevMonthInfo . month ,
)
)
le t dates = firstWeekDates
cons t dates = firstWeekDates
le t firstWeekCount = firstWeekDates . length
cons t firstWeekCount = firstWeekDates . length
for ( let i = 0 ; i < 7 - firstWeekCount ; i ++ ) {
for ( let i = 0 ; i < 7 - firstWeekCount ; i ++ ) {
const week = dateUtil . getDayOfWeek ( + year , + month , lastMonthDatesCount )
const week = dateUtil . getDayOfWeek ( + year , + month , lastMonthDatesCount )
dates . unshift ( {
dates . unshift ( {
year : prevMonthInfo . year ,
year : prevMonthInfo . year ,
month : prevMonthInfo . month ,
month : prevMonthInfo . month ,
date : lastMonthDatesCount ,
date : lastMonthDatesCount ,
week
week ,
} )
} )
lastMonthDatesCount -= 1
lastMonthDatesCount -= 1
}
}
@ -93,15 +94,15 @@ function getDatesWhenTargetInFirstWeek(target, firstWeekDates) {
function getDatesWhenTargetInLastWeek ( target , lastWeekDates ) {
function getDatesWhenTargetInLastWeek ( target , lastWeekDates ) {
const { year , month } = target
const { year , month } = target
const prevMonthInfo = dateUtil . getNextMonthInfo ( { year , month } )
const prevMonthInfo = dateUtil . getNextMonthInfo ( { year , month } )
le t dates = lastWeekDates
cons t dates = lastWeekDates
le t lastWeekCount = lastWeekDates . length
cons t lastWeekCount = lastWeekDates . length
for ( let i = 0 ; i < 7 - lastWeekCount ; i ++ ) {
for ( let i = 0 ; i < 7 - lastWeekCount ; i ++ ) {
const week = dateUtil . getDayOfWeek ( + year , + month , i + 1 )
const week = dateUtil . getDayOfWeek ( + year , + month , i + 1 )
dates . push ( {
dates . push ( {
year : prevMonthInfo . year ,
year : prevMonthInfo . year ,
month : prevMonthInfo . month ,
month : prevMonthInfo . month ,
date : i + 1 ,
date : i + 1 ,
week
week ,
} )
} )
}
}
return dates
return dates
@ -115,23 +116,27 @@ function getDates(target, calendarDates = [], calendarConfig = {}) {
if ( firstDayOfWeekIsMon ) {
if ( firstDayOfWeekIsMon ) {
const startIdx = date - ( targetDay || 7 )
const startIdx = date - ( targetDay || 7 )
return calendarDates . splice ( startIdx , 7 )
return calendarDates . splice ( startIdx , 7 )
} else {
}
else {
const startIdx = date - targetDay - 1
const startIdx = date - targetDay - 1
return calendarDates . splice ( startIdx , 7 )
return calendarDates . splice ( startIdx , 7 )
}
}
}
}
function getTargetWeekDates ( target , calendarConfig ) {
function getTargetWeekDates ( target , calendarConfig ) {
if ( ! target ) return
if ( ! target )
return
const { year , month } = target
const { year , month } = target
const calendarDates = dateUtil . calcDates ( year , month )
const calendarDates = dateUtil . calcDates ( year , month )
const firstWeekDates = firstWeekInMonth ( target , calendarDates , calendarConfig )
const firstWeekDates = firstWeekInMonth ( target , calendarDates , calendarConfig )
const lastWeekDates = lastWeekInMonth ( target , calendarDates , calendarConfig )
const lastWeekDates = lastWeekInMonth ( target , calendarDates , calendarConfig )
if ( dateIsInDatesRange ( target , firstWeekDates ) ) {
if ( dateIsInDatesRange ( target , firstWeekDates ) ) {
return getDatesWhenTargetInFirstWeek ( target , firstWeekDates )
return getDatesWhenTargetInFirstWeek ( target , firstWeekDates )
} else if ( dateIsInDatesRange ( target , lastWeekDates ) ) {
}
else if ( dateIsInDatesRange ( target , lastWeekDates ) ) {
return getDatesWhenTargetInLastWeek ( target , lastWeekDates )
return getDatesWhenTargetInLastWeek ( target , lastWeekDates )
} else {
}
else {
return getDates ( target , calendarDates , calendarConfig )
return getDates ( target , calendarDates , calendarConfig )
}
}
}
}
@ -157,42 +162,44 @@ function calculateFirstDateOfCurrentWeek(calendarData = {}) {
function calculateNextWeekDates ( calendarData = { } ) {
function calculateNextWeekDates ( calendarData = { } ) {
let { curYear , curMonth } = calendarData
let { curYear , curMonth } = calendarData
let calendarDates = [ ]
let calendarDates = [ ]
le t lastDateInThisWeek = calculateLastDateOfCurrentWeek ( calendarData )
cons t lastDateInThisWeek = calculateLastDateOfCurrentWeek ( calendarData )
const { year : LYear , month : LMonth } = lastDateInThisWeek
const { year : LYear , month : LMonth } = lastDateInThisWeek
if ( curYear !== LYear || curMonth !== LMonth ) {
if ( curYear !== LYear || curMonth !== LMonth ) {
calendarDates = dateUtil . calcDates ( LYear , LMonth )
calendarDates = dateUtil . calcDates ( LYear , LMonth )
curYear = LYear
curYear = LYear
curMonth = LMonth
curMonth = LMonth
} else {
}
else {
calendarDates = dateUtil . calcDates ( curYear , curMonth )
calendarDates = dateUtil . calcDates ( curYear , curMonth )
}
}
const lastDateInThisMonth = dateUtil . getDatesCountOfMonth ( curYear , curMonth )
const lastDateInThisMonth = dateUtil . getDatesCountOfMonth ( curYear , curMonth )
const count = lastDateInThisMonth - lastDateInThisWeek . date
const count = lastDateInThisMonth - lastDateInThisWeek . date
const lastDateIdx = calendarDates . findIndex (
const lastDateIdx = calendarDates . findIndex (
date => dateUtil . toTimeStr ( date ) === dateUtil . toTimeStr ( lastDateInThisWeek )
date => dateUtil . toTimeStr ( date ) === dateUtil . toTimeStr ( lastDateInThisWeek ) ,
)
)
const startIdx = lastDateIdx + 1
const startIdx = lastDateIdx + 1
if ( count >= 7 ) {
if ( count >= 7 ) {
return {
return {
dates : calendarDates . splice ( startIdx , 7 ) ,
dates : calendarDates . splice ( startIdx , 7 ) ,
year : curYear ,
year : curYear ,
month : curMonth
month : curMonth ,
}
}
}
} else {
else {
const nextMonth = dateUtil . getNextMonthInfo ( {
const nextMonth = dateUtil . getNextMonthInfo ( {
year : curYear ,
year : curYear ,
month : curMonth
month : curMonth ,
} )
} )
const { year , month } = nextMonth || { }
const { year , month } = nextMonth || { }
const calendarDatesOfNextMonth = dateUtil . calcDates ( year , month )
const calendarDatesOfNextMonth = dateUtil . calcDates ( year , month )
const remainDatesOfThisMonth = calendarDates . splice ( startIdx )
const remainDatesOfThisMonth = calendarDates . splice ( startIdx )
const patchDatesOfNextMonth = calendarDatesOfNextMonth . splice (
const patchDatesOfNextMonth = calendarDatesOfNextMonth . splice (
0 ,
0 ,
7 - remainDatesOfThisMonth . length
7 - remainDatesOfThisMonth . length ,
)
)
return {
return {
dates : [ ... remainDatesOfThisMonth , ... patchDatesOfNextMonth ] ,
dates : [ ... remainDatesOfThisMonth , ... patchDatesOfNextMonth ] ,
... nextMonth
... nextMonth ,
}
}
}
}
}
}
@ -202,43 +209,45 @@ function calculateNextWeekDates(calendarData = {}) {
* /
* /
function calculatePrevWeekDates ( calendarData = { } ) {
function calculatePrevWeekDates ( calendarData = { } ) {
let { curYear , curMonth } = calendarData
let { curYear , curMonth } = calendarData
le t firstDateInThisWeek = calculateFirstDateOfCurrentWeek ( calendarData )
cons t firstDateInThisWeek = calculateFirstDateOfCurrentWeek ( calendarData )
let calendarDates = [ ]
let calendarDates = [ ]
const { year : FYear , month : FMonth } = firstDateInThisWeek
const { year : FYear , month : FMonth } = firstDateInThisWeek
if ( curYear !== FYear || curMonth !== FMonth ) {
if ( curYear !== FYear || curMonth !== FMonth ) {
calendarDates = dateUtil . calcDates ( FYear , FMonth )
calendarDates = dateUtil . calcDates ( FYear , FMonth )
curYear = FYear
curYear = FYear
curMonth = FMonth
curMonth = FMonth
} else {
}
else {
calendarDates = dateUtil . calcDates ( curYear , curMonth )
calendarDates = dateUtil . calcDates ( curYear , curMonth )
}
}
const firstDateIdx = calendarDates . findIndex (
const firstDateIdx = calendarDates . findIndex (
date => dateUtil . toTimeStr ( date ) === dateUtil . toTimeStr ( firstDateInThisWeek )
date => dateUtil . toTimeStr ( date ) === dateUtil . toTimeStr ( firstDateInThisWeek ) ,
)
)
if ( firstDateIdx - 7 >= 0 ) {
if ( firstDateIdx - 7 >= 0 ) {
const startIdx = firstDateIdx - 7
const startIdx = firstDateIdx - 7
return {
return {
dates : calendarDates . splice ( startIdx , 7 ) ,
dates : calendarDates . splice ( startIdx , 7 ) ,
year : curYear ,
year : curYear ,
month : curMonth
month : curMonth ,
}
}
}
} else {
else {
const prevMonth = dateUtil . getPrevMonthInfo ( {
const prevMonth = dateUtil . getPrevMonthInfo ( {
year : curYear ,
year : curYear ,
month : curMonth
month : curMonth ,
} )
} )
const { year , month } = prevMonth || { }
const { year , month } = prevMonth || { }
const calendarDatesOfPrevMonth = dateUtil . calcDates ( year , month )
const calendarDatesOfPrevMonth = dateUtil . calcDates ( year , month )
const remainDatesOfThisMonth = calendarDates . splice (
const remainDatesOfThisMonth = calendarDates . splice (
0 ,
0 ,
firstDateInThisWeek . date - 1
firstDateInThisWeek . date - 1 ,
)
)
const patchDatesOfPrevMonth = calendarDatesOfPrevMonth . splice (
const patchDatesOfPrevMonth = calendarDatesOfPrevMonth . splice (
- ( 7 - remainDatesOfThisMonth . length )
- ( 7 - remainDatesOfThisMonth . length ) ,
)
)
return {
return {
dates : [ ... patchDatesOfPrevMonth , ... remainDatesOfThisMonth ] ,
dates : [ ... patchDatesOfPrevMonth , ... remainDatesOfThisMonth ] ,
... prevMonth
... prevMonth ,
}
}
}
}
}
}
@ -250,30 +259,30 @@ export default () => {
const { initializedWeekMode , selectedDates } = calendarData
const { initializedWeekMode , selectedDates } = calendarData
if ( calendarConfig . weekMode && ! initializedWeekMode ) {
if ( calendarConfig . weekMode && ! initializedWeekMode ) {
const { defaultDate } = calendarConfig
const { defaultDate } = calendarConfig
const target =
const target
( selectedDates && selectedDates [ 0 ] ) ||
= ( selectedDates && selectedDates [ 0 ] )
( defaultDate && dateUtil . transformDateRow2Dict ( defaultDate ) ) ||
|| ( defaultDate && dateUtil . transformDateRow2Dict ( defaultDate ) )
dateUtil . todayFMD ( )
|| dateUtil . todayFMD ( )
const waitRenderData = this . methods (
const waitRenderData = this . methods (
component
component ,
) . _ _calcDatesWhenSwitchView ( 'week' , target )
) . _ _calcDatesWhenSwitchView ( 'week' , target )
const { data , config } = waitRenderData || { }
const { data , config } = waitRenderData || { }
const setSelectDates = this . methods (
const setSelectDates = this . methods (
component
component ,
) . _ _selectTargetDateWhenJump ( target , data . dates , config )
) . _ _selectTargetDateWhenJump ( target , data . dates , config )
return {
return {
calendarData : {
calendarData : {
... data ,
... data ,
... setSelectDates ,
... setSelectDates ,
weeksCh : dateUtil . getWeekHeader ( calendarConfig . firstDayOfWeek ) ,
weeksCh : dateUtil . getWeekHeader ( calendarConfig . firstDayOfWeek ) ,
initializedWeekMode : true
initializedWeekMode : true ,
} ,
} ,
calendarConfig
calendarConfig ,
}
}
}
}
return {
return {
calendarData ,
calendarData ,
calendarConfig
calendarConfig ,
}
}
} ,
} ,
onSwitchCalendar ( target = { } , calendarData = { } , component ) {
onSwitchCalendar ( target = { } , calendarData = { } , component ) {
@ -284,7 +293,8 @@ export default () => {
if ( calendarConfig . weekMode ) {
if ( calendarConfig . weekMode ) {
if ( direction === 'left' ) {
if ( direction === 'left' ) {
waitRenderData = calculateNextWeekDates ( calendarData )
waitRenderData = calculateNextWeekDates ( calendarData )
} else {
}
else {
waitRenderData = calculatePrevWeekDates ( calendarData )
waitRenderData = calculatePrevWeekDates ( calendarData )
}
}
const { dates , year , month } = waitRenderData
const { dates , year , month } = waitRenderData
@ -292,7 +302,7 @@ export default () => {
... calendarData ,
... calendarData ,
dates ,
dates ,
curYear : year || curYear ,
curYear : year || curYear ,
curMonth : month || curMonth
curMonth : month || curMonth ,
}
}
}
}
return calendarData
return calendarData
@ -304,8 +314,8 @@ export default () => {
const weekDates = dates . map ( ( date , idx ) => {
const weekDates = dates . map ( ( date , idx ) => {
const tmp = { ... date }
const tmp = { ... date }
tmp . id = idx
tmp . id = idx
const isTarget =
const isTarget
dateUtil . toTimeStr ( target ) === dateUtil . toTimeStr ( tmp )
= dateUtil . toTimeStr ( target ) === dateUtil . toTimeStr ( tmp )
if ( isTarget && ! target . choosed && config . autoChoosedWhenJump ) {
if ( isTarget && ! target . choosed && config . autoChoosedWhenJump ) {
tmp . choosed = true
tmp . choosed = true
selectedDate = tmp
selectedDate = tmp
@ -314,7 +324,7 @@ export default () => {
} )
} )
return {
return {
dates : weekDates ,
dates : weekDates ,
selectedDates : [ selectedDate ]
selectedDates : [ selectedDate ] ,
}
}
} ,
} ,
_ _calcDatesForWeekMode ( target , config = { } , calendarData = { } ) {
_ _calcDatesForWeekMode ( target , config = { } , calendarData = { } ) {
@ -328,31 +338,31 @@ export default () => {
nextMonthGrids : null ,
nextMonthGrids : null ,
dates : weekDates ,
dates : weekDates ,
curYear : year ,
curYear : year ,
curMonth : month
curMonth : month ,
} ,
} ,
config : {
config : {
... config ,
... config ,
weekMode : true
weekMode : true ,
}
} ,
}
}
} ,
} ,
_ _calcDatesForMonthMode ( target , config = { } , calendarData = { } ) {
_ _calcDatesForMonthMode ( target , config = { } , calendarData = { } ) {
const { year , month } = target || { }
const { year , month } = target || { }
const waitRenderData = calcJumpData ( {
const waitRenderData = calcJumpData ( {
dateInfo : target ,
dateInfo : target ,
config
config ,
} )
} )
return {
return {
data : {
data : {
... calendarData ,
... calendarData ,
... waitRenderData ,
... waitRenderData ,
curYear : year ,
curYear : year ,
curMonth : month
curMonth : month ,
} ,
} ,
config : {
config : {
... config ,
... config ,
weekMode : false
weekMode : false ,
}
} ,
}
}
} ,
} ,
/ * *
/ * *
@ -369,18 +379,20 @@ export default () => {
selectedDates = [ ] ,
selectedDates = [ ] ,
dates = [ ] ,
dates = [ ] ,
curYear ,
curYear ,
curMonth
curMonth ,
} = existCalendarData
} = existCalendarData
const currentMonthSelected = selectedDates . filter (
const currentMonthSelected = selectedDates . filter (
item => curYear === + item . year || curMonth === + item . month
item => curYear === + item . year || curMonth === + item . month ,
)
)
let jumpTarget = { }
let jumpTarget = { }
if ( target ) {
if ( target ) {
jumpTarget = target
jumpTarget = target
} else {
}
else {
if ( currentMonthSelected . length ) {
if ( currentMonthSelected . length ) {
jumpTarget = currentMonthSelected . pop ( )
jumpTarget = currentMonthSelected . pop ( )
} else {
}
else {
jumpTarget = dates [ 0 ]
jumpTarget = dates [ 0 ]
}
}
}
}
@ -388,45 +400,47 @@ export default () => {
return this . methods ( component ) . _ _calcDatesForWeekMode (
return this . methods ( component ) . _ _calcDatesForWeekMode (
jumpTarget ,
jumpTarget ,
calendarConfig ,
calendarConfig ,
existCalendarData
existCalendarData ,
)
)
} else {
}
else {
return this . methods ( component ) . _ _calcDatesForMonthMode (
return this . methods ( component ) . _ _calcDatesForMonthMode (
jumpTarget ,
jumpTarget ,
calendarConfig ,
calendarConfig ,
existCalendarData
existCalendarData ,
)
)
}
}
} ,
} ,
weekModeJump : dateInfo => {
weekModeJump : ( dateInfo ) => {
const target = dateInfo || dateUtil . todayFMD ( )
const target = dateInfo || dateUtil . todayFMD ( )
const existCalendarData = getCalendarData ( 'calendar' , component ) || { }
const existCalendarData = getCalendarData ( 'calendar' , component ) || { }
const waitRenderData = this . methods (
const waitRenderData = this . methods (
component
component ,
) . _ _calcDatesWhenSwitchView ( 'week' , target )
) . _ _calcDatesWhenSwitchView ( 'week' , target )
const { data , config } = waitRenderData || { }
const { data , config } = waitRenderData || { }
const setSelectDates = this . methods (
const setSelectDates = this . methods (
component
component ,
) . _ _selectTargetDateWhenJump ( target , data . dates , config )
) . _ _selectTargetDateWhenJump ( target , data . dates , config )
return renderCalendar . call (
return renderCalendar . call (
component ,
component ,
{
{
... existCalendarData ,
... existCalendarData ,
... data ,
... data ,
... setSelectDates
... setSelectDates ,
} ,
} ,
config
config ,
)
)
} ,
} ,
switchView : ( view , target ) => {
switchView : ( view , target ) => {
const waitRenderData = this . methods (
const waitRenderData = this . methods (
component
component ,
) . _ _calcDatesWhenSwitchView ( view , target )
) . _ _calcDatesWhenSwitchView ( view , target )
const { data , config } = waitRenderData || { }
const { data , config } = waitRenderData || { }
if ( ! data ) return logger . warn ( '当前状态不能切换为周视图' )
if ( ! data )
return logger . warn ( '当前状态不能切换为周视图' )
return renderCalendar . call ( component , data , config )
return renderCalendar . call ( component , data , config )
} ,
}
}
}
} ,
}
}
}
}
}