Selaa lähdekoodia

ViewSpecManager

Adam Shaw 8 vuotta sitten
vanhempi
sitoutus
b901d625a2
4 muutettua tiedostoa jossa 27 lisäystä ja 9 poistoa
  1. 6 4
      src/Calendar.js
  2. 18 3
      src/Calendar.view-spec.js
  3. 1 1
      src/OptionsManager.js
  4. 2 1
      src/Toolbar.js

+ 6 - 4
src/Calendar.js

@@ -7,6 +7,7 @@ var Calendar = FC.Calendar = Class.extend(EmitterMixin, ListenerMixin, {
 	theme: null,
 	constraints: null,
 	optionsManager: null,
+	viewSpecManager: null,
 	businessHourGenerator: null,
 	loadingLevel: 0, // number of simultaneous loading tasks
 
@@ -19,9 +20,9 @@ var Calendar = FC.Calendar = Class.extend(EmitterMixin, ListenerMixin, {
 
 		this.el = el;
 		this.viewsByType = {};
-		this.viewSpecCache = {};
 
 		this.optionsManager = new OptionsManager(this, overrides);
+		this.viewSpecManager = new ViewSpecManager(this.optionsManager, this);
 		this.initMomentInternals(); // needs to happen after options hash initialized
 		this.initCurrentDate();
 		this.initEventManager();
@@ -116,7 +117,7 @@ var Calendar = FC.Calendar = Class.extend(EmitterMixin, ListenerMixin, {
 
 	// Given a view name for a custom view or a standard view, creates a ready-to-go View object
 	instantiateView: function(viewType) {
-		var spec = this.getViewSpec(viewType);
+		var spec = this.viewSpecManager.getViewSpec(viewType);
 
 		return new spec['class'](this, spec);
 	},
@@ -124,7 +125,7 @@ var Calendar = FC.Calendar = Class.extend(EmitterMixin, ListenerMixin, {
 
 	// Returns a boolean about whether the view is okay to instantiate at some point
 	isValidViewType: function(viewType) {
-		return Boolean(this.getViewSpec(viewType));
+		return Boolean(this.viewSpecManager.getViewSpec(viewType));
 	},
 
 
@@ -152,7 +153,8 @@ var Calendar = FC.Calendar = Class.extend(EmitterMixin, ListenerMixin, {
 		var spec;
 
 		viewType = viewType || 'day'; // day is default zoom
-		spec = this.getViewSpec(viewType) || this.getUnitViewSpec(viewType);
+		spec = this.viewSpecManager.getViewSpec(viewType) ||
+			this.viewSpecManager.getUnitViewSpec(viewType);
 
 		this.currentDate = newDate.clone();
 		this.renderView(spec ? spec.type : null);

+ 18 - 3
src/Calendar.view-spec.js

@@ -1,9 +1,24 @@
 
-Calendar.mixin({
+var ViewSpecManager = Class.extend({
 
+	_calendar: null, // avoid
+	optionsManager: null,
 	viewSpecCache: null, // cache of view definitions (initialized in Calendar.js)
 
 
+	constructor: function(optionsManager, _calendar) {
+		this.optionsManager = optionsManager;
+		this._calendar = _calendar;
+
+		this.clearCache();
+	},
+
+
+	clearCache: function() {
+		this.viewSpecCache = {};
+	},
+
+
 	// Gets information about how to create a view. Will use a cache.
 	getViewSpec: function(viewType) {
 		var cache = this.viewSpecCache;
@@ -22,7 +37,7 @@ Calendar.mixin({
 		if ($.inArray(unit, unitsDesc) != -1) {
 
 			// put views that have buttons first. there will be duplicates, but oh well
-			viewTypes = this.header.getViewsWithButtons(); // TODO: include footer as well?
+			viewTypes = this._calendar.header.getViewsWithButtons(); // TODO: include footer as well?
 			$.each(FC.views, function(viewType) { // all views
 				viewTypes.push(viewType);
 			});
@@ -160,7 +175,7 @@ Calendar.mixin({
 			queryButtonText(optionsManager.dirDefaults) ||
 			spec.defaults.buttonText || // a single string. from ViewSubclass.defaults
 			queryButtonText(Calendar.defaults) ||
-			(spec.duration ? this.humanizeDuration(spec.duration) : null) || // like "3 days"
+			(spec.duration ? this._calendar.humanizeDuration(spec.duration) : null) || // like "3 days"
 			requestedViewType; // fall back to given view name
 	}
 

+ 1 - 1
src/OptionsManager.js

@@ -108,7 +108,7 @@ var OptionsManager = Model.extend({
 			this.dynamicOverrides[optionName] = newOptionHash[optionName];
 		}
 
-		this._calendar.viewSpecCache = {}; // the dynamic override invalidates the options in this cache, so just clear it
+		this._calendar.viewSpecManager.clearCache(); // the dynamic override invalidates the options in this cache, so just clear it
 		this.compute(); // this.options needs to be recomputed after the dynamic override
 	}
 

+ 2 - 1
src/Toolbar.js

@@ -59,6 +59,7 @@ function Toolbar(calendar, toolbarOptions) {
 	function renderSection(position) {
 		var theme = calendar.theme;
 		var optionsManager = calendar.optionsManager;
+		var viewSpecManager = calendar.viewSpecManager;
 		var sectionEl = $('<div class="fc-' + position + '"/>');
 		var buttonStr = toolbarOptions.layout[position];
 		var calendarCustomButtons = optionsManager.get('customButtons') || {};
@@ -97,7 +98,7 @@ function Toolbar(calendar, toolbarOptions) {
 							(buttonIcon = theme.getIconClass(buttonName)) ||
 							(buttonText = customButtonProps.text); // jshint ignore:line
 						}
-						else if ((viewSpec = calendar.getViewSpec(buttonName))) {
+						else if ((viewSpec = viewSpecManager.getViewSpec(buttonName))) {
 							viewsWithButtons.push(buttonName);
 							buttonClick = function() {
 								calendar.changeView(buttonName);