Sfoglia il codice sorgente

View::updateNowIndicator. called upon window resize

Adam Shaw 10 anni fa
parent
commit
c14d356e87
1 ha cambiato i file con 49 aggiunte e 40 eliminazioni
  1. 49 40
      src/common/View.js

+ 49 - 40
src/common/View.js

@@ -48,9 +48,12 @@ var View = FC.View = Class.extend({
 	// document handlers, bound to `this` object
 	documentMousedownProxy: null, // TODO: doesn't work with touch
 
-	// for refresh timing of now indicator
-	nowIndicatorTimeoutID: null,
-	nowIndicatorIntervalID: null,
+	// now indicator
+	isNowIndicatorRendered: null,
+	initialNowDate: null, // result first getNow call
+	initialNowQueriedMs: null, // ms time the getNow was called
+	nowIndicatorTimeoutID: null, // for refresh timing of now indicator
+	nowIndicatorIntervalID: null, // "
 
 
 	constructor: function(calendar, type, options, intervalDuration) {
@@ -337,10 +340,7 @@ var View = FC.View = Class.extend({
 		this.renderDates();
 		this.updateSize();
 		this.renderBusinessHours(); // might need coordinates, so should go after updateSize()
-
-		if (this.opt('nowIndicator')) {
-			this.startNowIndicator();
-		}
+		this.startNowIndicator();
 	},
 
 
@@ -442,34 +442,42 @@ var View = FC.View = Class.extend({
 	// TODO: somehow do this for the current whole day's background too
 	startNowIndicator: function() {
 		var _this = this;
-		var unit = this.getNowIndicatorUnit();
-		var initialNow; // result first getNow call
-		var initialNowQueried; // ms time of then getNow was called
+		var unit;
+		var update;
 		var delay; // ms wait value
 
-		// rerenders the now indicator, computing the new current time from the amount of time that has passed
-		// since the initial getNow call.
-		function update() {
-			_this.unrenderNowIndicator();
-			_this.renderNowIndicator(
-				initialNow.clone().add(new Date() - initialNowQueried) // add ms
-			);
+		if (this.opt('nowIndicator')) {
+			unit = this.getNowIndicatorUnit();
+			if (unit) {
+				update = proxy(this, 'updateNowIndicator'); // bind to `this`
+
+				this.initialNowDate = this.calendar.getNow();
+				this.initialNowQueriedMs = +new Date();
+				this.renderNowIndicator(this.initialNowDate);
+				this.isNowIndicatorRendered = true;
+
+				// wait until the beginning of the next interval
+				delay = this.initialNowDate.clone().startOf(unit).add(1, unit) - this.initialNowDate;
+				this.nowIndicatorTimeoutID = setTimeout(function() {
+					_this.nowIndicatorTimeoutID = null;
+					update();
+					delay = +moment.duration(1, unit);
+					delay = Math.max(100, delay); // prevent too frequent
+					_this.nowIndicatorIntervalID = setInterval(update, delay); // update every interval
+				}, delay);
+			}
 		}
+	},
 
-		if (unit) {
-			initialNow = this.calendar.getNow();
-			initialNowQueried = +new Date();
-			this.renderNowIndicator(initialNow);
 
-			// wait until the beginning of the next interval
-			delay = initialNow.clone().startOf(unit).add(1, unit) - initialNow;
-			this.nowIndicatorTimeoutID = setTimeout(function() {
-				this.nowIndicatorTimeoutID = null;
-				update();
-				delay = +moment.duration(1, unit);
-				delay = Math.max(100, delay); // prevent too frequent
-				this.nowIndicatorIntervalID = setInterval(update, delay); // update every interval
-			}, delay);
+	// rerenders the now indicator, computing the new current time from the amount of time that has passed
+	// since the initial getNow call.
+	updateNowIndicator: function() {
+		if (this.isNowIndicatorRendered) {
+			this.unrenderNowIndicator();
+			this.renderNowIndicator(
+				this.initialNowDate.clone().add(new Date() - this.initialNowQueriedMs) // add ms
+			);
 		}
 	},
 
@@ -477,19 +485,19 @@ var View = FC.View = Class.extend({
 	// Immediately unrenders the view's current time indicator and stops any re-rendering timers.
 	// Won't cause side effects if indicator isn't rendered.
 	stopNowIndicator: function() {
-		var cleared = false;
+		if (this.isNowIndicatorRendered) {
 
-		if (this.nowIndicatorTimeoutID) {
-			clearTimeout(this.nowIndicatorTimeoutID);
-			cleared = true;
-		}
-		if (this.nowIndicatorIntervalID) {
-			clearTimeout(this.nowIndicatorIntervalID);
-			cleared = true;
-		}
+			if (this.nowIndicatorTimeoutID) {
+				clearTimeout(this.nowIndicatorTimeoutID);
+				this.nowIndicatorTimeoutID = null;
+			}
+			if (this.nowIndicatorIntervalID) {
+				clearTimeout(this.nowIndicatorIntervalID);
+				this.nowIndicatorIntervalID = null;
+			}
 
-		if (cleared) { // is the indicator currently display?
 			this.unrenderNowIndicator();
+			this.isNowIndicatorRendered = false;
 		}
 	},
 
@@ -527,6 +535,7 @@ var View = FC.View = Class.extend({
 
 		this.updateHeight(isResize);
 		this.updateWidth(isResize);
+		this.updateNowIndicator();
 
 		if (isResize) {
 			this.setScroll(scrollState);