OptionsManager.ts 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. import * as $ from 'jquery'
  2. import { firstDefined } from './util'
  3. import { globalDefaults, rtlDefaults, mergeOptions } from './options'
  4. import { localeOptionHash, populateInstanceComputableOptions } from './locale'
  5. import Model from './common/Model'
  6. export default class OptionsManager extends Model {
  7. _calendar: any // avoid
  8. dirDefaults: any // option defaults related to LTR or RTL
  9. localeDefaults: any // option defaults related to current locale
  10. overrides: any // option overrides given to the fullCalendar constructor
  11. dynamicOverrides: any // options set with dynamic setter method. higher precedence than view overrides.
  12. constructor(_calendar, overrides) {
  13. super()
  14. this._calendar = _calendar;
  15. this.overrides = $.extend({}, overrides); // make a copy
  16. this.dynamicOverrides = {};
  17. this.compute();
  18. }
  19. add(newOptionHash) { // was setOptions
  20. var optionCnt = 0;
  21. var optionName;
  22. this.recordOverrides(newOptionHash); // will trigger this model's watchers
  23. for (optionName in newOptionHash) {
  24. optionCnt++;
  25. }
  26. // special-case handling of single option change.
  27. // if only one option change, `optionName` will be its name.
  28. if (optionCnt === 1) {
  29. if (optionName === 'height' || optionName === 'contentHeight' || optionName === 'aspectRatio') {
  30. this._calendar.updateViewSize(true); // isResize=true
  31. return;
  32. }
  33. else if (optionName === 'defaultDate') {
  34. return; // can't change date this way. use gotoDate instead
  35. }
  36. else if (optionName === 'businessHours') {
  37. return; // this model already reacts to this
  38. }
  39. else if (optionName === 'timezone') {
  40. this._calendar.view.flash('initialEvents');
  41. return;
  42. }
  43. }
  44. // catch-all. rerender the header and footer and rebuild/rerender the current view
  45. this._calendar.renderHeader();
  46. this._calendar.renderFooter();
  47. // even non-current views will be affected by this option change. do before rerender
  48. // TODO: detangle
  49. this._calendar.viewsByType = {};
  50. this._calendar.reinitView();
  51. }
  52. // Computes the flattened options hash for the calendar and assigns to `this.options`.
  53. // Assumes this.overrides and this.dynamicOverrides have already been initialized.
  54. compute() {
  55. var locale, localeDefaults;
  56. var isRTL, dirDefaults;
  57. var rawOptions;
  58. locale = firstDefined( // explicit locale option given?
  59. this.dynamicOverrides.locale,
  60. this.overrides.locale
  61. );
  62. localeDefaults = localeOptionHash[locale];
  63. if (!localeDefaults) { // explicit locale option not given or invalid?
  64. locale = globalDefaults.locale;
  65. localeDefaults = localeOptionHash[locale] || {};
  66. }
  67. isRTL = firstDefined( // based on options computed so far, is direction RTL?
  68. this.dynamicOverrides.isRTL,
  69. this.overrides.isRTL,
  70. localeDefaults.isRTL,
  71. globalDefaults.isRTL
  72. );
  73. dirDefaults = isRTL ? rtlDefaults : {};
  74. this.dirDefaults = dirDefaults;
  75. this.localeDefaults = localeDefaults;
  76. rawOptions = mergeOptions([ // merge defaults and overrides. lowest to highest precedence
  77. globalDefaults, // global defaults
  78. dirDefaults,
  79. localeDefaults,
  80. this.overrides,
  81. this.dynamicOverrides
  82. ]);
  83. populateInstanceComputableOptions(rawOptions); // fill in gaps with computed options
  84. this.reset(rawOptions);
  85. }
  86. // stores the new options internally, but does not rerender anything.
  87. recordOverrides(newOptionHash) {
  88. var optionName;
  89. for (optionName in newOptionHash) {
  90. this.dynamicOverrides[optionName] = newOptionHash[optionName];
  91. }
  92. this._calendar.viewSpecManager.clearCache(); // the dynamic override invalidates the options in this cache, so just clear it
  93. this.compute(); // this.options needs to be recomputed after the dynamic override
  94. }
  95. }