| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112 |
- import { ViewSpec, ViewSpecHash } from './structs/view-spec'
- import { Calendar } from './Calendar'
- import { Theme } from './theme/Theme'
- import { mapHash } from './util/object'
- export interface ToolbarModel {
- [sectionName: string]: ToolbarWidget[][]
- }
- export interface ToolbarWidget {
- buttonName: string
- buttonClick?: any
- buttonIcon?: any
- buttonText?: any
- }
- // TODO: make separate parsing of headerToolbar/footerToolbar part of options-processing system
- export function parseToolbars(
- options: any,
- optionOverrides: any,
- theme: Theme,
- viewSpecs: ViewSpecHash,
- calendar: Calendar
- ) {
- let viewsWithButtons: string[] = []
- let headerToolbar = options.headerToolbar ? parseToolbar(options.headerToolbar, options, optionOverrides, theme, viewSpecs, calendar, viewsWithButtons) : null
- let footerToolbar = options.footerToolbar ? parseToolbar(options.footerToolbar, options, optionOverrides, theme, viewSpecs, calendar, viewsWithButtons) : null
- return { headerToolbar, footerToolbar, viewsWithButtons }
- }
- function parseToolbar(
- sectionStrHash: { [sectionName: string]: string },
- options: any,
- optionOverrides: any,
- theme: Theme,
- viewSpecs: ViewSpecHash,
- calendar: Calendar,
- viewsWithButtons: string[] // dump side effects
- ) : ToolbarModel {
- return mapHash(sectionStrHash, (sectionStr) => parseSection(sectionStr, options, optionOverrides, theme, viewSpecs, calendar, viewsWithButtons))
- }
- /*
- BAD: querying icons and text here. should be done at render time
- */
- function parseSection(
- sectionStr: string,
- options: any,
- optionOverrides: any,
- theme: Theme,
- viewSpecs: ViewSpecHash,
- calendar: Calendar,
- viewsWithButtons: string[] // dump side effects
- ): ToolbarWidget[][] {
- let isRtl = options.direction === 'rtl'
- let calendarCustomButtons = options.customButtons || {}
- let calendarButtonTextOverrides = optionOverrides.buttonText || {}
- let calendarButtonText = options.buttonText || {}
- let sectionSubstrs = sectionStr ? sectionStr.split(' ') : []
- return sectionSubstrs.map((buttonGroupStr, i): ToolbarWidget[] => {
- return buttonGroupStr.split(',').map((buttonName, j): ToolbarWidget => {
- if (buttonName === 'title') {
- return { buttonName }
- } else {
- let customButtonProps
- let viewSpec: ViewSpec
- let buttonClick
- let buttonIcon // only one of these will be set
- let buttonText // "
- if ((customButtonProps = calendarCustomButtons[buttonName])) {
- buttonClick = function(ev: UIEvent) {
- if (customButtonProps.click) {
- customButtonProps.click.call(ev.target, ev) // TODO: correct to use `target`?
- }
- };
- (buttonIcon = theme.getCustomButtonIconClass(customButtonProps)) ||
- (buttonIcon = theme.getIconClass(buttonName, isRtl)) ||
- (buttonText = customButtonProps.text)
- } else if ((viewSpec = viewSpecs[buttonName])) {
- viewsWithButtons.push(buttonName)
- buttonClick = function() {
- calendar.changeView(buttonName)
- };
- (buttonText = viewSpec.buttonTextOverride) ||
- (buttonIcon = theme.getIconClass(buttonName, isRtl)) ||
- (buttonText = viewSpec.buttonTextDefault)
- } else if (calendar[buttonName]) { // a calendar method
- buttonClick = function() {
- calendar[buttonName]()
- };
- (buttonText = calendarButtonTextOverrides[buttonName]) ||
- (buttonIcon = theme.getIconClass(buttonName, isRtl)) ||
- (buttonText = calendarButtonText[buttonName])
- // ^ everything else is considered default
- }
- return { buttonName, buttonClick, buttonIcon, buttonText }
- }
- })
- })
- }
|