Selaa lähdekoodia

BusinessHourGenerator

Adam Shaw 8 vuotta sitten
vanhempi
sitoutus
2e20b7d554

+ 1 - 1
src.json

@@ -59,7 +59,7 @@
     "models/ComponentFootprint.js",
     "models/EventManager.js",
     "models/EventPeriod.js",
-    "models/BusinessHours.js",
+    "models/BusinessHourGenerator.js",
     "models/event/EventDefParser.js",
     "models/event/EventDef.js",
     "models/event/SingleEventDef.js",

+ 18 - 15
src/Calendar.constraints.js

@@ -141,24 +141,10 @@ Calendar.prototype.isFootprintWithinConstraints = function(componentFootprint, c
 
 
 Calendar.prototype.constraintValToFootprints = function(constraintVal, isAllDay) {
-	var businessHours;
 	var eventInstances;
 
 	if (constraintVal === 'businessHours') {
-
-		// TODO: is it good rely on view for this?
-		if (this.view) {
-			businessHours = this.view.get('businessHours');
-			if (businessHours) {
-				return eventFootprintsToComponentFootprints(
-					this.eventRangesToEventFootprints(
-						businessHours.getAllEventRanges(isAllDay)
-					)
-				);
-			}
-		}
-
-		return [];
+		return this.buildCurrentBusinessFootprints(isAllDay);
 	}
 	else if (typeof constraintVal === 'object') {
 		eventInstances = this.parseEventDefToInstances(constraintVal); // handles recurring events
@@ -178,6 +164,23 @@ Calendar.prototype.constraintValToFootprints = function(constraintVal, isAllDay)
 };
 
 
+// returns ComponentFootprint[]
+// uses current view's range
+Calendar.prototype.buildCurrentBusinessFootprints = function(isAllDay) {
+	var view = this.view;
+	var businessHourGenerator = view.get('businessHourGenerator');
+	var unzonedRange = view.get('dateProfile').activeUnzonedRange;
+	var eventInstanceGroup = businessHourGenerator.buildEventInstanceGroup(isAllDay, unzonedRange);
+
+	if (eventInstanceGroup) {
+		return this.eventInstancesToFootprints(eventInstanceGroup.eventInstances);
+	}
+	else {
+		return [];
+	}
+};
+
+
 // conversion util
 Calendar.prototype.eventInstancesToFootprints = function(eventInstances) {
 	return eventFootprintsToComponentFootprints(

+ 15 - 5
src/View.js

@@ -130,7 +130,7 @@ var View = FC.View = InteractiveDateComponent.extend({
 
 		// TODO: not best place for this
 		// TODO: better way of forwarding options from calendar -> view
-		this.calendar.optionsModel.watch('viewRawBusinessHours' + this.uid, [ 'businessHours' ], function(deps) {
+		this.calendar.optionsModel.watch('viewRawBusinessHours' + this.uid, [ '?businessHours' ], function(deps) {
 			_this.set('rawBusinessHours', deps.businessHours);
 		}, function() {
 			_this.unset('rawBusinessHours');
@@ -907,15 +907,25 @@ var View = FC.View = InteractiveDateComponent.extend({
 // responsible for populating data that DateComponent relies on
 
 
-View.watch('businessHours', [ 'rawBusinessHours', 'dateProfile' ], function(deps) {
-	return new BusinessHours(
+View.watch('businessHourGenerator', [ 'rawBusinessHours' ], function(deps) {
+	return new BusinessHourGenerator(
 		deps.rawBusinessHours,
-		deps.dateProfile.activeUnzonedRange,
-		this.calendar
+		this.calendar // TODO: untangle
 	);
 });
 
 
+View.watch('businessHours', [ 'businessHourGenerator', 'dateProfile' ], function(deps) {
+	var businessHourGenerator = deps.businessHourGenerator;
+	var unzonedRange = deps.dateProfile.activeUnzonedRange;
+
+	return {
+		allDay: businessHourGenerator.buildEventInstanceGroup(true, unzonedRange),
+		timed: businessHourGenerator.buildEventInstanceGroup(false, unzonedRange)
+	};
+});
+
+
 View.watch('initialEvents', [ 'dateProfile' ], function(deps) {
 	return this.fetchInitialEvents(deps.dateProfile);
 });

+ 4 - 10
src/basic/DayGrid.js

@@ -5,6 +5,7 @@
 var DayGrid = FC.DayGrid = InteractiveDateComponent.extend(StandardInteractionsMixin, DayTableMixin, {
 
 	eventRendererClass: DayGridEventRenderer,
+	businessHourRendererClass: BusinessHourRenderer,
 	helperRendererClass: DayGridHelperRenderer,
 	fillRendererClass: DayGridFillRenderer,
 
@@ -24,6 +25,8 @@ var DayGrid = FC.DayGrid = InteractiveDateComponent.extend(StandardInteractionsM
 	// Relies on the view's colCnt and rowCnt. In the future, this component should probably be self-sufficient.
 	isRigid: false,
 
+	hasAllDayBusinessHours: true,
+
 
 	constructor: function(view) {
 		this.view = view; // do first, for opt calls during initialization
@@ -391,15 +394,6 @@ var DayGrid = FC.DayGrid = InteractiveDateComponent.extend(StandardInteractionsM
 	unrenderEventResize: function() {
 		this.unrenderHighlight();
 		this.helperRenderer.unrender();
-	},
-
-
-	/* Business Hours
-	------------------------------------------------------------------------------------------------------------------*/
-
-
-	businessHourRendererClass: BusinessHourRenderer.extend({
-		isAllDay: true // TODO: config param on component?
-	})
+	}
 
 });

+ 22 - 2
src/component/DateComponent.js

@@ -18,6 +18,8 @@ var DateComponent = Component.extend({
 
 	hitsNeededDepth: 0, // necessary because multiple callers might need the same hits
 
+	hasAllDayBusinessHours: false, // TODO: unify with largeUnit and isTimeScale?
+
 	dateMessageAggregator: null,
 	eventMessageAggregator: null,
 
@@ -193,9 +195,27 @@ var DateComponent = Component.extend({
 
 
 	// Renders business-hours onto the view. Assumes updateSize has already been called.
-	renderBusinessHours: function(businessHours) {
+	renderBusinessHours: function(businessHourPayload) {
+		var dateProfile = this.get('dateProfile');
+		var eventInstanceGroup = businessHourPayload[this.hasAllDayBusinessHours ? 'allDay' : 'timed'];
+		var eventFootprints;
+
+		if (eventInstanceGroup) {
+			eventFootprints = this.eventRangesToEventFootprints(
+				eventInstanceGroup.sliceRenderRanges(dateProfile.activeUnzonedRange)
+			);
+		}
+		else {
+			eventFootprints = [];
+		}
+
+		this.renderBusinessHourEventFootprints(eventFootprints);
+	},
+
+
+	renderBusinessHourEventFootprints: function(eventFootprints) {
 		if (this.businessHourRenderer) {
-			this.businessHourRenderer.render(businessHours);
+			this.businessHourRenderer.renderEventFootprints(eventFootprints);
 		}
 	},
 

+ 0 - 9
src/component/renderers/BusinessHourRenderer.js

@@ -1,7 +1,6 @@
 
 var BusinessHourRenderer = Class.extend({
 
-	isAllDay: false, // subclasses can config
 	component: null,
 	fillRenderer: null,
 	segs: null,
@@ -18,14 +17,6 @@ var BusinessHourRenderer = Class.extend({
 	},
 
 
-	render: function(businessHours) {
-		var eventRanges = businessHours.sliceRenderRanges(this.isAllDay);
-		var eventFootprints = this.component.eventRangesToEventFootprints(eventRanges);
-
-		this.renderEventFootprints(eventFootprints);
-	},
-
-
 	renderEventFootprints: function(eventFootprints) {
 		var segs = this.component.eventFootprintsToSegs(eventFootprints);
 

+ 5 - 35
src/models/BusinessHours.js → src/models/BusinessHourGenerator.js

@@ -8,55 +8,25 @@ var BUSINESS_HOUR_EVENT_DEFAULTS = {
 };
 
 
-var BusinessHours = Class.extend({
+var BusinessHourGenerator = Class.extend({
 
 	rawComplexDef: null,
-	unzonedRange: null,
-	calendar: null, // for eventRangesToEventFootprints AND anonymous EventSource
-	cache: null,
+	calendar: null, // for anonymous EventSource
 
 
-	constructor: function(rawComplexDef, unzonedRange, calendar) {
+	constructor: function(rawComplexDef, calendar) {
 		this.rawComplexDef = rawComplexDef;
-		this.unzonedRange = unzonedRange;
 		this.calendar = calendar;
-		this.cache = {};
 	},
 
 
-	getAllEventRanges: function(isAllDay) {
-		var key = 'getAllEventRanges' + (isAllDay ? 1 : 0);
-		var instanceGroup;
-
-		if (!this.cache[key]) {
-			instanceGroup = this.buildInstanceGroup(isAllDay);
-			this.cache[key] = instanceGroup ? instanceGroup.getAllEventRanges() : [];
-		}
-
-		return this.cache[key];
-	},
-
-
-	sliceRenderRanges: function(isAllDay) {
-		var key = 'sliceRenderRanges' + (isAllDay ? 1 : 0);
-		var instanceGroup;
-
-		if (!this.cache[key]) {
-			instanceGroup = this.buildInstanceGroup(isAllDay);
-			this.cache[key] = instanceGroup ? instanceGroup.sliceRenderRanges(this.unzonedRange) : [];
-		}
-
-		return this.cache[key];
-	},
-
-
-	buildInstanceGroup: function(isAllDay) {
+	buildEventInstanceGroup: function(isAllDay, unzonedRange) {
 		var eventDefs = this.buildEventDefs(isAllDay);
 		var eventInstanceGroup;
 
 		if (eventDefs.length) {
 			eventInstanceGroup = new EventInstanceGroup(
-				eventDefsToEventInstances(eventDefs, this.unzonedRange)
+				eventDefsToEventInstances(eventDefs, unzonedRange)
 			);
 
 			// so that inverse-background rendering can happen even when no eventRanges in view

+ 2 - 0
src/models/event-source/EventSource.js

@@ -20,6 +20,8 @@ var EventSource = Class.extend(ParsableModelMixin, {
 	eventDataTransform: null, // optional function
 
 
+	// can we do away with calendar? at least for the abstract?
+	// useful for buildEventDef
 	constructor: function(calendar) {
 		this.calendar = calendar;
 		this.className = [];