Bläddra i källkod

currentDate/massageCurrentDate refactor

Adam Shaw 9 år sedan
förälder
incheckning
eaef24ff35
3 ändrade filer med 36 tillägg och 27 borttagningar
  1. 4 3
      src/Calendar.js
  2. 30 22
      src/common/View.js
  3. 2 2
      src/util.js

+ 4 - 3
src/Calendar.js

@@ -721,9 +721,6 @@ function Calendar_constructor(element, overrides) {
 
 
 		if (currentView) {
 		if (currentView) {
 
 
-			// in case the view should render a period of time that is completely hidden
-			t.currentDate = currentView.massageCurrentDate(t.currentDate);
-
 			if (elementVisible()) {
 			if (elementVisible()) {
 
 
 				if (forcedScroll) {
 				if (forcedScroll) {
@@ -732,6 +729,10 @@ function Calendar_constructor(element, overrides) {
 
 
 				currentView.setDate(t.currentDate);
 				currentView.setDate(t.currentDate);
 
 
+				// TODO: make setDate return the revised date.
+				// Difficult because of the pseudo-async nature, promises.
+				t.currentDate = currentView.currentDate;
+
 				if (forcedScroll) {
 				if (forcedScroll) {
 					currentView.releaseScroll();
 					currentView.releaseScroll();
 				}
 				}

+ 30 - 22
src/common/View.js

@@ -22,6 +22,7 @@ var View = FC.View = Class.extend(EmitterMixin, ListenerMixin, {
 	eventRenderQueue: null,
 	eventRenderQueue: null,
 
 
 	viewSpecDuration: null,
 	viewSpecDuration: null,
+	currentDate: null,
 
 
 	// range the view is formally responsible for (moments)
 	// range the view is formally responsible for (moments)
 	// may be different from start/end. for example, a month view might have 1st-31st, excluding padded dates
 	// may be different from start/end. for example, a month view might have 1st-31st, excluding padded dates
@@ -160,6 +161,7 @@ var View = FC.View = Class.extend(EmitterMixin, ListenerMixin, {
 			this.renderRange = ranges.renderRange;
 			this.renderRange = ranges.renderRange;
 			this.visibleRange = ranges.visibleRange;
 			this.visibleRange = ranges.visibleRange;
 			this.dateIncrement = ranges.dateIncrement;
 			this.dateIncrement = ranges.dateIncrement;
+			this.currentDate = ranges.date;
 
 
 			// DEPRECATED, but we need to keep it updated
 			// DEPRECATED, but we need to keep it updated
 			// TODO: run automated tests with this commented out
 			// TODO: run automated tests with this commented out
@@ -178,7 +180,7 @@ var View = FC.View = Class.extend(EmitterMixin, ListenerMixin, {
 	},
 	},
 
 
 
 
-	resolveRangesForDate: function(date) {
+	resolveRangesForDate: function(date, direction) {
 		var validRange = this.buildValidRange(date);
 		var validRange = this.buildValidRange(date);
 		var customVisibleRange = this.buildCustomVisibleRange(date);
 		var customVisibleRange = this.buildCustomVisibleRange(date);
 		var currentRangeDuration = moment.duration(1, 'day'); // with default value
 		var currentRangeDuration = moment.duration(1, 'day'); // with default value
@@ -193,6 +195,14 @@ var View = FC.View = Class.extend(EmitterMixin, ListenerMixin, {
 			currentRange = customVisibleRange;
 			currentRange = customVisibleRange;
 			renderRange = customVisibleRange;
 			renderRange = customVisibleRange;
 
 
+			// if the view displays a single day or smaller
+			if (customVisibleRange.end.diff(customVisibleRange.start, 'days', true) <= 1) {
+				if (this.isHiddenDay(date)) {
+					date = this.skipHiddenDays(date, direction);
+					date.startOf('day');
+				}
+			}
+
 			currentRangeUnit = computeIntervalUnit(
 			currentRangeUnit = computeIntervalUnit(
 				customVisibleRange.start,
 				customVisibleRange.start,
 				customVisibleRange.end
 				customVisibleRange.end
@@ -202,6 +212,14 @@ var View = FC.View = Class.extend(EmitterMixin, ListenerMixin, {
 			currentRangeDuration = this.viewSpecDuration || currentRangeDuration;
 			currentRangeDuration = this.viewSpecDuration || currentRangeDuration;
 			currentRangeUnit = computeIntervalUnit(currentRangeDuration);
 			currentRangeUnit = computeIntervalUnit(currentRangeDuration);
 
 
+			// if the view displays a single day or smaller
+			if (currentRangeDuration.as('days') <= 1) {
+				if (this.isHiddenDay(date)) {
+					date = this.skipHiddenDays(date, direction);
+					date.startOf('day');
+				}
+			}
+
 			currentRange = this.computeCurrentRange(date, currentRangeDuration, currentRangeUnit);
 			currentRange = this.computeCurrentRange(date, currentRangeDuration, currentRangeUnit);
 			renderRange = this.computeRenderRange(currentRange);
 			renderRange = this.computeRenderRange(currentRange);
 		}
 		}
@@ -212,6 +230,8 @@ var View = FC.View = Class.extend(EmitterMixin, ListenerMixin, {
 			visibleRange = constrainRange(visibleRange, currentRange);
 			visibleRange = constrainRange(visibleRange, currentRange);
 		}
 		}
 
 
+		date = constrainDateToRange(date, visibleRange);
+
 		dateIncrementInput = this.opt('dateIncrement'); // TODO: util for getting date options
 		dateIncrementInput = this.opt('dateIncrement'); // TODO: util for getting date options
 		dateIncrement = (dateIncrementInput ? moment.duration(dateIncrementInput) : null) ||
 		dateIncrement = (dateIncrementInput ? moment.duration(dateIncrementInput) : null) ||
 			currentRangeDuration;
 			currentRangeDuration;
@@ -222,7 +242,8 @@ var View = FC.View = Class.extend(EmitterMixin, ListenerMixin, {
 			currentRangeUnit: currentRangeUnit,
 			currentRangeUnit: currentRangeUnit,
 			visibleRange: visibleRange,
 			visibleRange: visibleRange,
 			renderRange: renderRange,
 			renderRange: renderRange,
-			dateIncrement: dateIncrement
+			dateIncrement: dateIncrement,
+			date: date // the revised date
 		};
 		};
 	},
 	},
 
 
@@ -278,32 +299,19 @@ var View = FC.View = Class.extend(EmitterMixin, ListenerMixin, {
 
 
 	// Computes the new date when the user hits the prev button, given the current date
 	// Computes the new date when the user hits the prev button, given the current date
 	computePrevDate: function(date) {
 	computePrevDate: function(date) {
-		return this.massageCurrentDate(
-			date.clone().startOf(this.currentRangeUnit).subtract(this.dateIncrement), -1
-		);
+		var prevDate = date.clone().startOf(this.currentRangeUnit).subtract(this.dateIncrement);
+		var ranges = this.resolveRangesForDate(prevDate, -1);
+
+		return ranges.date;
 	},
 	},
 
 
 
 
 	// Computes the new date when the user hits the next button, given the current date
 	// Computes the new date when the user hits the next button, given the current date
 	computeNextDate: function(date) {
 	computeNextDate: function(date) {
-		return this.massageCurrentDate(
-			date.clone().startOf(this.currentRangeUnit).add(this.dateIncrement)
-		);
-	},
-
-
-	// Given an arbitrarily calculated current date of the calendar, returns a date that is ensured to be completely
-	// visible. `direction` is optional and indicates which direction the current date was being
-	// incremented or decremented (1 or -1).
-	massageCurrentDate: function(date, direction) {
-		if (this.currentRangeAs('days') <= 1) { // if the view displays a single day or smaller
-			if (this.isHiddenDay(date)) {
-				date = this.skipHiddenDays(date, direction);
-				date.startOf('day');
-			}
-		}
+		var nextDate = date.clone().startOf(this.currentRangeUnit).add(this.dateIncrement);
+		var ranges = this.resolveRangesForDate(nextDate, 1);
 
 
-		return date;
+		return ranges.date;
 	},
 	},
 
 
 
 

+ 2 - 2
src/util.js

@@ -709,7 +709,7 @@ function isRangesEqual(range0, range1) {
 }
 }
 
 
 
 
-/*function constrainDateToRange(date, range) {
+function constrainDateToRange(date, range) {
 	date = date.clone();
 	date = date.clone();
 
 
 	if (range.start) {
 	if (range.start) {
@@ -721,7 +721,7 @@ function isRangesEqual(range0, range1) {
 	}
 	}
 
 
 	return date;
 	return date;
-}*/
+}
 
 
 
 
 function minMoment(mom1, mom2) {
 function minMoment(mom1, mom2) {