import { ViewSpec , ViewSpecHash } from './structs/view-spec' ; import { Theme } from './theme/Theme' ; import { CalendarApi } from './CalendarApi' ; import { CalendarOptionsRefined , CalendarOptions } from './options' ; import { ToolbarInput , ToolbarModel , ToolbarWidget , CustomButtonInput } from './toolbar-struct' ; import { formatWithOrdinals } from './util/misc' ; export function parseToolbars ( calendarOptions , calendarOptionOverrides , theme , viewSpecs , calendarApi ) { let header = calendarOptions . headerToolbar ? parseToolbar ( calendarOptions . headerToolbar , calendarOptions , calendarOptionOverrides , theme , viewSpecs , calendarApi ) : null ; let footer = calendarOptions . footerToolbar ? parseToolbar ( calendarOptions . footerToolbar , calendarOptions , calendarOptionOverrides , theme , viewSpecs , calendarApi ) : null ; return { header , footer } ; } function parseToolbar ( sectionStrHash , calendarOptions , calendarOptionOverrides , theme , viewSpecs , calendarApi ) { let sectionWidgets = { } ; let viewsWithButtons = [ ] ; let hasTitle = false ; for ( let sectionName in sectionStrHash ) { let sectionStr = sectionStrHash [ sectionName ] ; let sectionRes = parseSection ( sectionStr , calendarOptions , calendarOptionOverrides , theme , viewSpecs , calendarApi ) ; sectionWidgets [ sectionName ] = sectionRes . widgets ; viewsWithButtons . push ( ... sectionRes . viewsWithButtons ) ; hasTitle = hasTitle || sectionRes . hasTitle ; } return { sectionWidgets , viewsWithButtons , hasTitle } ; } function parseSection ( sectionStr , calendarOptions , calendarOptionOverrides , theme , viewSpecs , calendarApi ) { let isRtl = calendarOptions . direction === 'rtl' ; let calendarCustomButtons = calendarOptions . customButtons || { } ; let calendarButtonTextOverrides = calendarOptionOverrides . buttonText || { } ; let calendarButtonText = calendarOptions . buttonText || { } ; let calendarButtonHintOverrides = calendarOptionOverrides . buttonHints || { } ; let calendarButtonHints = calendarOptions . buttonHints || { } ; let sectionSubstrs = sectionStr ? sectionStr . split ( ' ' ) : [ ] ; let viewsWithButtons = [ ] ; let hasTitle = false ; let widgets = sectionSubstrs . map ( ( buttonGroupStr ) : ToolbarWidget [ ] => ( buttonGroupStr . split ( ',' ) . map ( ( buttonName ) : ToolbarWidget => { if ( buttonName === 'title' ) { hasTitle = true ; return { buttonName } ; } let customButtonProps ; let viewSpec ; let buttonClick ; let buttonIcon ; let buttonText ; let buttonHint ; if ( ( customButtonProps = calendarCustomButtons [ buttonName ] ) ) { buttonClick = ( ev ) => { if ( customButtonProps . click ) { customButtonProps . click . call ( ev . target , ev , ev . target ) ; } } ; ( buttonIcon = theme . getCustomButtonIconClass ( customButtonProps ) ) || ( buttonIcon = theme . getIconClass ( buttonName , isRtl ) ) || ( buttonText = customButtonProps . text ) ; buttonHint = customButtonProps . hint || customButtonProps . text ; } else if ( ( viewSpec = viewSpecs [ buttonName ] ) ) { viewsWithButtons . push ( buttonName ) ; buttonClick = ( ) => { calendarApi . changeView ( buttonName ) ; } ; ( buttonText = viewSpec . buttonTextOverride ) || ( buttonIcon = theme . getIconClass ( buttonName , isRtl ) ) || ( buttonText = viewSpec . buttonTextDefault ) ; let textFallback = viewSpec . buttonTextOverride || viewSpec . buttonTextDefault ; buttonHint = formatWithOrdinals ( viewSpec . buttonTitleOverride || viewSpec . buttonTitleDefault || calendarOptions . viewHint , [ textFallback , buttonName ] , textFallback ) ; } else if ( calendarApi [ buttonName ] ) { buttonClick = ( ) => { calendarApi [ buttonName ] ( ) ; } ; ( buttonText = calendarButtonTextOverrides [ buttonName ] ) || ( buttonIcon = theme . getIconClass ( buttonName , isRtl ) ) || ( buttonText = calendarButtonText [ buttonName ] ) ; if ( buttonName === 'prevYear' || buttonName === 'nextYear' ) { let prevOrNext = buttonName === 'prevYear' ? 'prev' : 'next' ; buttonHint = formatWithOrdinals ( calendarButtonHintOverrides [ prevOrNext ] || calendarButtonHints [ prevOrNext ] , [ calendarButtonText . year || 'year' , 'year' , ] , calendarButtonText [ buttonName ] ) ; } else { buttonHint = ( navUnit ) => formatWithOrdinals ( calendarButtonHintOverrides [ buttonName ] || calendarButtonHints [ buttonName ] , [ calendarButtonText [ navUnit ] || navUnit , navUnit , ] , calendarButtonText [ buttonName ] ) ; } } return { buttonName , buttonClick , buttonIcon , buttonText , buttonHint } ; } ) ) ) ; return { widgets , viewsWithButtons , hasTitle } ; }