toolbar-parse.ts 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. import { ViewSpec, ViewSpecHash } from './structs/view-spec'
  2. import { Calendar } from './Calendar'
  3. import { Theme } from './theme/Theme'
  4. import { mapHash } from './util/object'
  5. export interface ToolbarModel {
  6. [sectionName: string]: ToolbarWidget[][]
  7. }
  8. export interface ToolbarWidget {
  9. buttonName: string
  10. buttonClick?: any
  11. buttonIcon?: any
  12. buttonText?: any
  13. }
  14. // TODO: make separate parsing of headerToolbar/footerToolbar part of options-processing system
  15. export function parseToolbars(
  16. options: any,
  17. optionOverrides: any,
  18. theme: Theme,
  19. viewSpecs: ViewSpecHash,
  20. calendar: Calendar
  21. ) {
  22. let viewsWithButtons: string[] = []
  23. let headerToolbar = options.headerToolbar ? parseToolbar(options.headerToolbar, options, optionOverrides, theme, viewSpecs, calendar, viewsWithButtons) : null
  24. let footerToolbar = options.footerToolbar ? parseToolbar(options.footerToolbar, options, optionOverrides, theme, viewSpecs, calendar, viewsWithButtons) : null
  25. return { headerToolbar, footerToolbar, viewsWithButtons }
  26. }
  27. function parseToolbar(
  28. sectionStrHash: { [sectionName: string]: string },
  29. options: any,
  30. optionOverrides: any,
  31. theme: Theme,
  32. viewSpecs: ViewSpecHash,
  33. calendar: Calendar,
  34. viewsWithButtons: string[] // dump side effects
  35. ) : ToolbarModel {
  36. return mapHash(sectionStrHash, (sectionStr) => parseSection(sectionStr, options, optionOverrides, theme, viewSpecs, calendar, viewsWithButtons))
  37. }
  38. /*
  39. BAD: querying icons and text here. should be done at render time
  40. */
  41. function parseSection(
  42. sectionStr: string,
  43. options: any,
  44. optionOverrides: any,
  45. theme: Theme,
  46. viewSpecs: ViewSpecHash,
  47. calendar: Calendar,
  48. viewsWithButtons: string[] // dump side effects
  49. ): ToolbarWidget[][] {
  50. let isRtl = options.direction === 'rtl'
  51. let calendarCustomButtons = options.customButtons || {}
  52. let calendarButtonTextOverrides = optionOverrides.buttonText || {}
  53. let calendarButtonText = options.buttonText || {}
  54. let sectionSubstrs = sectionStr ? sectionStr.split(' ') : []
  55. return sectionSubstrs.map((buttonGroupStr, i): ToolbarWidget[] => {
  56. return buttonGroupStr.split(',').map((buttonName, j): ToolbarWidget => {
  57. if (buttonName === 'title') {
  58. return { buttonName }
  59. } else {
  60. let customButtonProps
  61. let viewSpec: ViewSpec
  62. let buttonClick
  63. let buttonIcon // only one of these will be set
  64. let buttonText // "
  65. if ((customButtonProps = calendarCustomButtons[buttonName])) {
  66. buttonClick = function(ev: UIEvent) {
  67. if (customButtonProps.click) {
  68. customButtonProps.click.call(ev.target, ev) // TODO: correct to use `target`?
  69. }
  70. };
  71. (buttonIcon = theme.getCustomButtonIconClass(customButtonProps)) ||
  72. (buttonIcon = theme.getIconClass(buttonName, isRtl)) ||
  73. (buttonText = customButtonProps.text)
  74. } else if ((viewSpec = viewSpecs[buttonName])) {
  75. viewsWithButtons.push(buttonName)
  76. buttonClick = function() {
  77. calendar.changeView(buttonName)
  78. };
  79. (buttonText = viewSpec.buttonTextOverride) ||
  80. (buttonIcon = theme.getIconClass(buttonName, isRtl)) ||
  81. (buttonText = viewSpec.buttonTextDefault)
  82. } else if (calendar[buttonName]) { // a calendar method
  83. buttonClick = function() {
  84. calendar[buttonName]()
  85. };
  86. (buttonText = calendarButtonTextOverrides[buttonName]) ||
  87. (buttonIcon = theme.getIconClass(buttonName, isRtl)) ||
  88. (buttonText = calendarButtonText[buttonName])
  89. // ^ everything else is considered default
  90. }
  91. return { buttonName, buttonClick, buttonIcon, buttonText }
  92. }
  93. })
  94. })
  95. }