Procházet zdrojové kódy

DateProfileGenerator

Adam Shaw před 8 roky
rodič
revize
7f5d7b06e2

+ 2 - 1
src.json

@@ -39,8 +39,8 @@
     "component/DateComponent.util.js",
     "component/InteractiveDateComponent.js",
     "component/DayTableMixin.js",
-    "View.js",
     "View.date-range.js",
+    "View.js",
     "Toolbar.js",
     "Constraints.js",
     "OptionsManager.js",
@@ -85,6 +85,7 @@
     "basic/DayGridHelperRenderer.js",
     "basic/DayGrid.js",
     "basic/DayGrid.limit.js",
+    "basic/BasicViewDateProfileGenerator.js",
     "basic/BasicView.js",
     "basic/MonthView.js",
     "basic/config.js",

+ 2 - 2
src/Calendar.js

@@ -180,7 +180,7 @@ var Calendar = FC.Calendar = Class.extend(EmitterMixin, ListenerMixin, {
 
 	prev: function() {
 		var view = this.view;
-		var prevInfo = view.buildPrevDateProfile(view.get('dateProfile'));
+		var prevInfo = view.dateProfileGenerator.buildPrev(view.get('dateProfile'));
 
 		if (prevInfo.isValid) {
 			this.currentDate = prevInfo.date;
@@ -191,7 +191,7 @@ var Calendar = FC.Calendar = Class.extend(EmitterMixin, ListenerMixin, {
 
 	next: function() {
 		var view = this.view;
-		var nextInfo = view.buildNextDateProfile(view.get('dateProfile'));
+		var nextInfo = view.dateProfileGenerator.buildNext(view.get('dateProfile'));
 
 		if (nextInfo.isValid) {
 			this.currentDate = nextInfo.date;

+ 3 - 3
src/Calendar.toolbar.js

@@ -63,9 +63,9 @@ Calendar.mixin({
 	updateToolbarButtons: function(dateProfile) {
 		var now = this.getNow();
 		var view = this.view;
-		var todayInfo = view.buildDateProfile(now);
-		var prevInfo = view.buildPrevDateProfile(view.get('dateProfile'));
-		var nextInfo = view.buildNextDateProfile(view.get('dateProfile'));
+		var todayInfo = view.dateProfileGenerator.build(now);
+		var prevInfo = view.dateProfileGenerator.buildPrev(view.get('dateProfile'));
+		var nextInfo = view.dateProfileGenerator.buildNext(view.get('dateProfile'));
 
 		this.toolbarsManager.proxyCall(
 			(todayInfo.isValid && !dateProfile.currentUnzonedRange.containsDate(now)) ?

+ 42 - 21
src/View.date-range.js

@@ -1,7 +1,27 @@
 
-View.mixin({
+var DateProfileGenerator = Class.extend({
 
-	usesMinMaxTime: false, // whether minTime/maxTime will affect the activeUnzonedRange. Views must opt-in.
+	_view: null, // avoid
+
+
+	constructor: function(_view) {
+		this._view = _view;
+	},
+
+
+	opt: function(name) {
+		return this._view.opt(name);
+	},
+
+
+	trimHiddenDays: function(unzonedRange) {
+		return this._view.trimHiddenDays(unzonedRange);
+	},
+
+
+	msToUtcMoment: function(ms, forceAllDay) {
+		return this._view.calendar.msToUtcMoment(ms, forceAllDay);
+	},
 
 
 	/* Date Range Computation
@@ -9,29 +29,29 @@ View.mixin({
 
 
 	// Builds a structure with info about what the dates/ranges will be for the "prev" view.
-	buildPrevDateProfile: function(currentDateProfile) {
+	buildPrev: function(currentDateProfile) {
 		var prevDate = currentDateProfile.date.clone()
 			.startOf(currentDateProfile.currentRangeUnit)
 			.subtract(currentDateProfile.dateIncrement);
 
-		return this.buildDateProfile(prevDate, -1);
+		return this.build(prevDate, -1);
 	},
 
 
 	// Builds a structure with info about what the dates/ranges will be for the "next" view.
-	buildNextDateProfile: function(currentDateProfile) {
+	buildNext: function(currentDateProfile) {
 		var nextDate = currentDateProfile.date.clone()
 			.startOf(currentDateProfile.currentRangeUnit)
 			.add(currentDateProfile.dateIncrement);
 
-		return this.buildDateProfile(nextDate, 1);
+		return this.build(nextDate, 1);
 	},
 
 
 	// Builds a structure holding dates/ranges for rendering around the given date.
 	// Optional direction param indicates whether the date is being incremented/decremented
 	// from its previous value. decremented = -1, incremented = 1 (default).
-	buildDateProfile: function(date, direction, forceToValid) {
+	build: function(date, direction, forceToValid) {
 		var isDateAllDay = !date.hasTime();
 		var validUnzonedRange;
 		var minTime = null;
@@ -46,7 +66,7 @@ View.mixin({
 		validUnzonedRange = this.trimHiddenDays(validUnzonedRange);
 
 		if (forceToValid) {
-			date = this.calendar.msToUtcMoment(
+			date = this.msToUtcMoment(
 				validUnzonedRange.constrainDate(date), // returns MS
 				isDateAllDay
 			);
@@ -72,7 +92,7 @@ View.mixin({
 		activeUnzonedRange = activeUnzonedRange.intersect(validUnzonedRange); // might return null
 
 		if (activeUnzonedRange) {
-			date = this.calendar.msToUtcMoment(
+			date = this.msToUtcMoment(
 				activeUnzonedRange.constrainDate(date), // returns MS
 				isDateAllDay
 			);
@@ -125,25 +145,26 @@ View.mixin({
 	// Indicates the minimum/maximum dates to display.
 	// not responsible for trimming hidden days.
 	buildValidRange: function() {
-		return this.getUnzonedRangeOption('validRange', this.calendar.getNow()) ||
+		return this._view.getUnzonedRangeOption('validRange', this._view.calendar.getNow()) ||
 			new UnzonedRange(); // completely open-ended
 	},
 
 
 	// Builds a structure with info about the "current" range, the range that is
 	// highlighted as being the current month for example.
-	// See buildDateProfile for a description of `direction`.
+	// See build() for a description of `direction`.
 	// Guaranteed to have `range` and `unit` properties. `duration` is optional.
 	// TODO: accept a MS-time instead of a moment `date`?
 	buildCurrentRangeInfo: function(date, direction) {
+		var viewSpec = this._view.viewSpec;
 		var duration = null;
 		var unit = null;
 		var unzonedRange = null;
 		var dayCount;
 
-		if (this.viewSpec.duration) {
-			duration = this.viewSpec.duration;
-			unit = this.viewSpec.durationUnit;
+		if (viewSpec.duration) {
+			duration = viewSpec.duration;
+			unit = viewSpec.durationUnit;
 			unzonedRange = this.buildRangeFromDuration(date, direction, duration, unit);
 		}
 		else if ((dayCount = this.opt('dayCount'))) {
@@ -174,7 +195,7 @@ View.mixin({
 		var start = unzonedRange.getStart();
 		var end = unzonedRange.getEnd();
 
-		if (this.usesMinMaxTime) {
+		if (this._view.usesMinMaxTime) {
 
 			if (minTime < 0) {
 				start.time(0).add(minTime);
@@ -201,8 +222,8 @@ View.mixin({
 
 		// if the view displays a single day or smaller
 		if (duration.as('days') <= 1) {
-			if (this.isHiddenDay(start)) {
-				start = this.skipHiddenDays(start, direction);
+			if (this._view.isHiddenDay(start)) {
+				start = this._view.skipHiddenDays(start, direction);
 				start.startOf('day');
 			}
 		}
@@ -247,12 +268,12 @@ View.mixin({
 		}
 
 		start.startOf('day');
-		start = this.skipHiddenDays(start, direction);
+		start = this._view.skipHiddenDays(start, direction);
 
 		end = start.clone();
 		do {
 			end.add(1, 'day');
-			if (!this.isHiddenDay(end)) {
+			if (!this._view.isHiddenDay(end)) {
 				runningCount++;
 			}
 		} while (runningCount < dayCount);
@@ -265,9 +286,9 @@ View.mixin({
 	// which is a way to define the currentUnzonedRange and activeUnzonedRange at the same time.
 	// TODO: accept a MS-time instead of a moment `date`?
 	buildCustomVisibleRange: function(date) {
-		var visibleUnzonedRange = this.getUnzonedRangeOption(
+		var visibleUnzonedRange = this._view.getUnzonedRangeOption(
 			'visibleRange',
-			this.calendar.applyTimezone(date) // correct zone. also generates new obj that avoids mutations
+			this._view.calendar.applyTimezone(date) // correct zone. also generates new obj that avoids mutations
 		);
 
 		if (visibleUnzonedRange && (visibleUnzonedRange.startMs === null || visibleUnzonedRange.endMs === null)) {

+ 6 - 1
src/View.js

@@ -31,6 +31,10 @@ var View = FC.View = InteractiveDateComponent.extend({
 	nowIndicatorTimeoutID: null, // for refresh timing of now indicator
 	nowIndicatorIntervalID: null, // "
 
+	dateProfileGeneratorClass: DateProfileGenerator,
+	dateProfileGenerator: null,
+	usesMinMaxTime: false, // whether minTime/maxTime will affect the activeUnzonedRange. Views must opt-in.
+
 	// DEPRECATED
 	start: null, // use activeUnzonedRange
 	end: null, // use activeUnzonedRange
@@ -53,6 +57,7 @@ var View = FC.View = InteractiveDateComponent.extend({
 
 		this.initRenderQueue();
 		this.initHiddenDays();
+		this.dateProfileGenerator = new this.dateProfileGeneratorClass(this);
 		this.bindBaseRenderHandlers();
 		this.eventOrderSpecs = parseFieldSpecs(this.opt('eventOrder'));
 
@@ -189,7 +194,7 @@ var View = FC.View = InteractiveDateComponent.extend({
 
 	setDate: function(date) {
 		var currentDateProfile = this.get('dateProfile');
-		var newDateProfile = this.buildDateProfile(date, null, true); // forceToValid=true
+		var newDateProfile = this.dateProfileGenerator.build(date, null, true); // forceToValid=true
 
 		if (
 			!currentDateProfile ||

+ 2 - 20
src/basic/BasicView.js

@@ -6,6 +6,8 @@
 
 var BasicView = FC.BasicView = View.extend({
 
+	dateProfileGeneratorClass: BasicViewDateProfileGenerator,
+
 	scroller: null,
 
 	dayGridClass: DayGrid, // class the dayGrid will be instantiated from (overridable by subclasses)
@@ -50,26 +52,6 @@ var BasicView = FC.BasicView = View.extend({
 	},
 
 
-	// Computes the date range that will be rendered.
-	buildRenderRange: function(currentUnzonedRange, currentRangeUnit, isRangeAllDay) {
-		var renderUnzonedRange = View.prototype.buildRenderRange.apply(this, arguments); // an UnzonedRange
-		var start = this.calendar.msToUtcMoment(renderUnzonedRange.startMs, isRangeAllDay);
-		var end = this.calendar.msToUtcMoment(renderUnzonedRange.endMs, isRangeAllDay);
-
-		// year and month views should be aligned with weeks. this is already done for week
-		if (/^(year|month)$/.test(currentRangeUnit)) {
-			start.startOf('week');
-
-			// make end-of-week if not already
-			if (end.weekday()) {
-				end.add(1, 'week').startOf('week'); // exclusively move backwards
-			}
-		}
-
-		return new UnzonedRange(start, end);
-	},
-
-
 	executeDateRender: function(dateProfile) {
 		this.dayGrid.breakOnWeeks = /year|month|week/.test(dateProfile.currentRangeUnit);
 

+ 23 - 0
src/basic/BasicViewDateProfileGenerator.js

@@ -0,0 +1,23 @@
+
+var BasicViewDateProfileGenerator = DateProfileGenerator.extend({
+
+	// Computes the date range that will be rendered.
+	buildRenderRange: function(currentUnzonedRange, currentRangeUnit, isRangeAllDay) {
+		var renderUnzonedRange = DateProfileGenerator.prototype.buildRenderRange.apply(this, arguments); // an UnzonedRange
+		var start = this.msToUtcMoment(renderUnzonedRange.startMs, isRangeAllDay);
+		var end = this.msToUtcMoment(renderUnzonedRange.endMs, isRangeAllDay);
+
+		// year and month views should be aligned with weeks. this is already done for week
+		if (/^(year|month)$/.test(currentRangeUnit)) {
+			start.startOf('week');
+
+			// make end-of-week if not already
+			if (end.weekday()) {
+				end.add(1, 'week').startOf('week'); // exclusively move backwards
+			}
+		}
+
+		return new UnzonedRange(start, end);
+	}
+
+});

+ 13 - 11
src/basic/MonthView.js

@@ -2,18 +2,18 @@
 /* A month view with day cells running in rows (one-per-week) and columns
 ----------------------------------------------------------------------------------------------------------------------*/
 
-var MonthView = FC.MonthView = BasicView.extend({
 
+var MonthViewDateProfileGenerator = BasicViewDateProfileGenerator.extend({
 
 	// Computes the date range that will be rendered.
 	buildRenderRange: function(currentUnzonedRange, currentRangeUnit, isRangeAllDay) {
-		var renderUnzonedRange = BasicView.prototype.buildRenderRange.apply(this, arguments);
-		var start = this.calendar.msToUtcMoment(renderUnzonedRange.startMs, isRangeAllDay);
-		var end = this.calendar.msToUtcMoment(renderUnzonedRange.endMs, isRangeAllDay);
+		var renderUnzonedRange = BasicViewDateProfileGenerator.prototype.buildRenderRange.apply(this, arguments);
+		var start = this.msToUtcMoment(renderUnzonedRange.startMs, isRangeAllDay);
+		var end = this.msToUtcMoment(renderUnzonedRange.endMs, isRangeAllDay);
 		var rowCnt;
 
 		// ensure 6 weeks
-		if (this.isFixedWeeks()) {
+		if (this.opt('fixedWeekCount')) {
 			rowCnt = Math.ceil( // could be partial weeks due to hiddenDays
 				end.diff(start, 'weeks', true) // dontRound=true
 			);
@@ -21,7 +21,14 @@ var MonthView = FC.MonthView = BasicView.extend({
 		}
 
 		return new UnzonedRange(start, end);
-	},
+	}
+
+});
+
+
+var MonthView = FC.MonthView = BasicView.extend({
+
+	dateProfileGeneratorClass: MonthViewDateProfileGenerator,
 
 
 	// Overrides the default BasicView behavior to have special multi-week auto-height logic
@@ -36,11 +43,6 @@ var MonthView = FC.MonthView = BasicView.extend({
 	},
 
 
-	isFixedWeeks: function() {
-		return this.opt('fixedWeekCount');
-	},
-
-
 	isDateInOtherMonth: function(date, dateProfile) {
 		return date.month() !== moment.utc(dateProfile.currentUnzonedRange.startMs).month(); // TODO: optimize
 	}