ViewSpecManager.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. var ViewSpecManager = Class.extend({
  2. _calendar: null, // avoid
  3. optionsManager: null,
  4. viewSpecCache: null, // cache of view definitions (initialized in Calendar.js)
  5. constructor: function(optionsManager, _calendar) {
  6. this.optionsManager = optionsManager;
  7. this._calendar = _calendar;
  8. this.clearCache();
  9. },
  10. clearCache: function() {
  11. this.viewSpecCache = {};
  12. },
  13. // Gets information about how to create a view. Will use a cache.
  14. getViewSpec: function(viewType) {
  15. var cache = this.viewSpecCache;
  16. return cache[viewType] || (cache[viewType] = this.buildViewSpec(viewType));
  17. },
  18. // Given a duration singular unit, like "week" or "day", finds a matching view spec.
  19. // Preference is given to views that have corresponding buttons.
  20. getUnitViewSpec: function(unit) {
  21. var viewTypes;
  22. var i;
  23. var spec;
  24. if ($.inArray(unit, unitsDesc) != -1) {
  25. // put views that have buttons first. there will be duplicates, but oh well
  26. viewTypes = this._calendar.header.getViewsWithButtons(); // TODO: include footer as well?
  27. $.each(FC.views, function(viewType) { // all views
  28. viewTypes.push(viewType);
  29. });
  30. for (i = 0; i < viewTypes.length; i++) {
  31. spec = this.getViewSpec(viewTypes[i]);
  32. if (spec) {
  33. if (spec.singleUnit == unit) {
  34. return spec;
  35. }
  36. }
  37. }
  38. }
  39. },
  40. // Builds an object with information on how to create a given view
  41. buildViewSpec: function(requestedViewType) {
  42. var viewOverrides = this.optionsManager.overrides.views || {};
  43. var specChain = []; // for the view. lowest to highest priority
  44. var defaultsChain = []; // for the view. lowest to highest priority
  45. var overridesChain = []; // for the view. lowest to highest priority
  46. var viewType = requestedViewType;
  47. var spec; // for the view
  48. var overrides; // for the view
  49. var durationInput;
  50. var duration;
  51. var unit;
  52. // iterate from the specific view definition to a more general one until we hit an actual View class
  53. while (viewType) {
  54. spec = fcViews[viewType];
  55. overrides = viewOverrides[viewType];
  56. viewType = null; // clear. might repopulate for another iteration
  57. if (typeof spec === 'function') { // TODO: deprecate
  58. spec = { 'class': spec };
  59. }
  60. if (spec) {
  61. specChain.unshift(spec);
  62. defaultsChain.unshift(spec.defaults || {});
  63. durationInput = durationInput || spec.duration;
  64. viewType = viewType || spec.type;
  65. }
  66. if (overrides) {
  67. overridesChain.unshift(overrides); // view-specific option hashes have options at zero-level
  68. durationInput = durationInput || overrides.duration;
  69. viewType = viewType || overrides.type;
  70. }
  71. }
  72. spec = mergeProps(specChain);
  73. spec.type = requestedViewType;
  74. if (!spec['class']) {
  75. return false;
  76. }
  77. // fall back to top-level `duration` option
  78. durationInput = durationInput ||
  79. this.optionsManager.dynamicOverrides.duration ||
  80. this.optionsManager.overrides.duration;
  81. if (durationInput) {
  82. duration = moment.duration(durationInput);
  83. if (duration.valueOf()) { // valid?
  84. unit = computeDurationGreatestUnit(duration, durationInput);
  85. spec.duration = duration;
  86. spec.durationUnit = unit;
  87. // view is a single-unit duration, like "week" or "day"
  88. // incorporate options for this. lowest priority
  89. if (duration.as(unit) === 1) {
  90. spec.singleUnit = unit;
  91. overridesChain.unshift(viewOverrides[unit] || {});
  92. }
  93. }
  94. }
  95. spec.defaults = mergeOptions(defaultsChain);
  96. spec.overrides = mergeOptions(overridesChain);
  97. this.buildViewSpecOptions(spec);
  98. this.buildViewSpecButtonText(spec, requestedViewType);
  99. return spec;
  100. },
  101. // Builds and assigns a view spec's options object from its already-assigned defaults and overrides
  102. buildViewSpecOptions: function(spec) {
  103. var optionsManager = this.optionsManager;
  104. spec.options = mergeOptions([ // lowest to highest priority
  105. Calendar.defaults, // global defaults
  106. spec.defaults, // view's defaults (from ViewSubclass.defaults)
  107. optionsManager.dirDefaults,
  108. optionsManager.localeDefaults, // locale and dir take precedence over view's defaults!
  109. optionsManager.overrides, // calendar's overrides (options given to constructor)
  110. spec.overrides, // view's overrides (view-specific options)
  111. optionsManager.dynamicOverrides // dynamically set via setter. highest precedence
  112. ]);
  113. populateInstanceComputableOptions(spec.options);
  114. },
  115. // Computes and assigns a view spec's buttonText-related options
  116. buildViewSpecButtonText: function(spec, requestedViewType) {
  117. var optionsManager = this.optionsManager;
  118. // given an options object with a possible `buttonText` hash, lookup the buttonText for the
  119. // requested view, falling back to a generic unit entry like "week" or "day"
  120. function queryButtonText(options) {
  121. var buttonText = options.buttonText || {};
  122. return buttonText[requestedViewType] ||
  123. // view can decide to look up a certain key
  124. (spec.buttonTextKey ? buttonText[spec.buttonTextKey] : null) ||
  125. // a key like "month"
  126. (spec.singleUnit ? buttonText[spec.singleUnit] : null);
  127. }
  128. // highest to lowest priority
  129. spec.buttonTextOverride =
  130. queryButtonText(optionsManager.dynamicOverrides) ||
  131. queryButtonText(optionsManager.overrides) || // constructor-specified buttonText lookup hash takes precedence
  132. spec.overrides.buttonText; // `buttonText` for view-specific options is a string
  133. // highest to lowest priority. mirrors buildViewSpecOptions
  134. spec.buttonTextDefault =
  135. queryButtonText(optionsManager.localeDefaults) ||
  136. queryButtonText(optionsManager.dirDefaults) ||
  137. spec.defaults.buttonText || // a single string. from ViewSubclass.defaults
  138. queryButtonText(Calendar.defaults) ||
  139. (spec.duration ? this._calendar.humanizeDuration(spec.duration) : null) || // like "3 days"
  140. requestedViewType; // fall back to given view name
  141. }
  142. });