diff --git a/lib/timeline/TimeStep.js b/lib/timeline/TimeStep.js index cef93adc..a6a1dec9 100644 --- a/lib/timeline/TimeStep.js +++ b/lib/timeline/TimeStep.js @@ -34,7 +34,7 @@ function TimeStep(start, end, minimumStep, hiddenDates) { this._end = new Date(); this.autoScale = true; - this.scale = TimeStep.SCALE.DAY; + this.scale = 'day'; this.step = 1; // initialize the range @@ -50,25 +50,6 @@ function TimeStep(start, end, minimumStep, hiddenDates) { } } -// enum scale -TimeStep.SCALE = { - MILLISECOND: 1, - SECOND: 2, - MINUTE: 3, - HOUR: 4, - DAY: 5, - WEEKDAY: 6, - MONTH: 7, - YEAR: 8 -}; - -// array with lower case scale names -TimeStep.SCALE_INDEX = {}; -Object.keys(TimeStep.SCALE).forEach(function (key) { - var index = TimeStep.SCALE[key]; - TimeStep.SCALE_INDEX[index] = key.toLowerCase(); -}); - // Time formatting TimeStep.FORMAT = { minorLabels: { @@ -133,29 +114,29 @@ TimeStep.prototype.roundToMinor = function() { // IMPORTANT: we have no breaks in this switch! (this is no bug) //noinspection FallthroughInSwitchStatementJS switch (this.scale) { - case TimeStep.SCALE.YEAR: + case 'year': this.current.setFullYear(this.step * Math.floor(this.current.getFullYear() / this.step)); this.current.setMonth(0); - case TimeStep.SCALE.MONTH: this.current.setDate(1); - case TimeStep.SCALE.DAY: // intentional fall through - case TimeStep.SCALE.WEEKDAY: this.current.setHours(0); - case TimeStep.SCALE.HOUR: this.current.setMinutes(0); - case TimeStep.SCALE.MINUTE: this.current.setSeconds(0); - case TimeStep.SCALE.SECOND: this.current.setMilliseconds(0); - //case TimeStep.SCALE.MILLISECOND: // nothing to do for milliseconds + case 'month': this.current.setDate(1); + case 'day': // intentional fall through + case 'weekday': this.current.setHours(0); + case 'hour': this.current.setMinutes(0); + case 'minute': this.current.setSeconds(0); + case 'second': this.current.setMilliseconds(0); + //case 'millisecond': // nothing to do for milliseconds } if (this.step != 1) { // round down to the first minor value that is a multiple of the current step size switch (this.scale) { - case TimeStep.SCALE.MILLISECOND: this.current.setMilliseconds(this.current.getMilliseconds() - this.current.getMilliseconds() % this.step); break; - case TimeStep.SCALE.SECOND: this.current.setSeconds(this.current.getSeconds() - this.current.getSeconds() % this.step); break; - case TimeStep.SCALE.MINUTE: this.current.setMinutes(this.current.getMinutes() - this.current.getMinutes() % this.step); break; - case TimeStep.SCALE.HOUR: this.current.setHours(this.current.getHours() - this.current.getHours() % this.step); break; - case TimeStep.SCALE.WEEKDAY: // intentional fall through - case TimeStep.SCALE.DAY: this.current.setDate((this.current.getDate()-1) - (this.current.getDate()-1) % this.step + 1); break; - case TimeStep.SCALE.MONTH: this.current.setMonth(this.current.getMonth() - this.current.getMonth() % this.step); break; - case TimeStep.SCALE.YEAR: this.current.setFullYear(this.current.getFullYear() - this.current.getFullYear() % this.step); break; + case 'millisecond': this.current.setMilliseconds(this.current.getMilliseconds() - this.current.getMilliseconds() % this.step); break; + case 'second': this.current.setSeconds(this.current.getSeconds() - this.current.getSeconds() % this.step); break; + case 'minute': this.current.setMinutes(this.current.getMinutes() - this.current.getMinutes() % this.step); break; + case 'hour': this.current.setHours(this.current.getHours() - this.current.getHours() % this.step); break; + case 'weekday': // intentional fall through + case 'day': this.current.setDate((this.current.getDate()-1) - (this.current.getDate()-1) % this.step + 1); break; + case 'month': this.current.setMonth(this.current.getMonth() - this.current.getMonth() % this.step); break; + case 'year': this.current.setFullYear(this.current.getFullYear() - this.current.getFullYear() % this.step); break; default: break; } } @@ -179,34 +160,34 @@ TimeStep.prototype.next = function() { // (end of March and end of October) if (this.current.getMonth() < 6) { switch (this.scale) { - case TimeStep.SCALE.MILLISECOND: + case 'millisecond': this.current = new Date(this.current.valueOf() + this.step); break; - case TimeStep.SCALE.SECOND: this.current = new Date(this.current.valueOf() + this.step * 1000); break; - case TimeStep.SCALE.MINUTE: this.current = new Date(this.current.valueOf() + this.step * 1000 * 60); break; - case TimeStep.SCALE.HOUR: + case 'second': this.current = new Date(this.current.valueOf() + this.step * 1000); break; + case 'minute': this.current = new Date(this.current.valueOf() + this.step * 1000 * 60); break; + case 'hour': this.current = new Date(this.current.valueOf() + this.step * 1000 * 60 * 60); // in case of skipping an hour for daylight savings, adjust the hour again (else you get: 0h 5h 9h ... instead of 0h 4h 8h ...) var h = this.current.getHours(); this.current.setHours(h - (h % this.step)); break; - case TimeStep.SCALE.WEEKDAY: // intentional fall through - case TimeStep.SCALE.DAY: this.current.setDate(this.current.getDate() + this.step); break; - case TimeStep.SCALE.MONTH: this.current.setMonth(this.current.getMonth() + this.step); break; - case TimeStep.SCALE.YEAR: this.current.setFullYear(this.current.getFullYear() + this.step); break; + case 'weekday': // intentional fall through + case 'day': this.current.setDate(this.current.getDate() + this.step); break; + case 'month': this.current.setMonth(this.current.getMonth() + this.step); break; + case 'year': this.current.setFullYear(this.current.getFullYear() + this.step); break; default: break; } } else { switch (this.scale) { - case TimeStep.SCALE.MILLISECOND: this.current = new Date(this.current.valueOf() + this.step); break; - case TimeStep.SCALE.SECOND: this.current.setSeconds(this.current.getSeconds() + this.step); break; - case TimeStep.SCALE.MINUTE: this.current.setMinutes(this.current.getMinutes() + this.step); break; - case TimeStep.SCALE.HOUR: this.current.setHours(this.current.getHours() + this.step); break; - case TimeStep.SCALE.WEEKDAY: // intentional fall through - case TimeStep.SCALE.DAY: this.current.setDate(this.current.getDate() + this.step); break; - case TimeStep.SCALE.MONTH: this.current.setMonth(this.current.getMonth() + this.step); break; - case TimeStep.SCALE.YEAR: this.current.setFullYear(this.current.getFullYear() + this.step); break; + case 'millisecond': this.current = new Date(this.current.valueOf() + this.step); break; + case 'second': this.current.setSeconds(this.current.getSeconds() + this.step); break; + case 'minute': this.current.setMinutes(this.current.getMinutes() + this.step); break; + case 'hour': this.current.setHours(this.current.getHours() + this.step); break; + case 'weekday': // intentional fall through + case 'day': this.current.setDate(this.current.getDate() + this.step); break; + case 'month': this.current.setMonth(this.current.getMonth() + this.step); break; + case 'year': this.current.setFullYear(this.current.getFullYear() + this.step); break; default: break; } } @@ -214,14 +195,14 @@ TimeStep.prototype.next = function() { if (this.step != 1) { // round down to the correct major value switch (this.scale) { - case TimeStep.SCALE.MILLISECOND: if(this.current.getMilliseconds() < this.step) this.current.setMilliseconds(0); break; - case TimeStep.SCALE.SECOND: if(this.current.getSeconds() < this.step) this.current.setSeconds(0); break; - case TimeStep.SCALE.MINUTE: if(this.current.getMinutes() < this.step) this.current.setMinutes(0); break; - case TimeStep.SCALE.HOUR: if(this.current.getHours() < this.step) this.current.setHours(0); break; - case TimeStep.SCALE.WEEKDAY: // intentional fall through - case TimeStep.SCALE.DAY: if(this.current.getDate() < this.step+1) this.current.setDate(1); break; - case TimeStep.SCALE.MONTH: if(this.current.getMonth() < this.step) this.current.setMonth(0); break; - case TimeStep.SCALE.YEAR: break; // nothing to do for year + case 'millisecond': if(this.current.getMilliseconds() < this.step) this.current.setMilliseconds(0); break; + case 'second': if(this.current.getSeconds() < this.step) this.current.setSeconds(0); break; + case 'minute': if(this.current.getMinutes() < this.step) this.current.setMinutes(0); break; + case 'hour': if(this.current.getHours() < this.step) this.current.setHours(0); break; + case 'weekday': // intentional fall through + case 'day': if(this.current.getDate() < this.step+1) this.current.setDate(1); break; + case 'month': if(this.current.getMonth() < this.step) this.current.setMonth(0); break; + case 'year': break; // nothing to do for year default: break; } } @@ -295,35 +276,35 @@ TimeStep.prototype.setMinimumStep = function(minimumStep) { var stepMillisecond= (1); // find the smallest step that is larger than the provided minimumStep - if (stepYear*1000 > minimumStep) {this.scale = TimeStep.SCALE.YEAR; this.step = 1000;} - if (stepYear*500 > minimumStep) {this.scale = TimeStep.SCALE.YEAR; this.step = 500;} - if (stepYear*100 > minimumStep) {this.scale = TimeStep.SCALE.YEAR; this.step = 100;} - if (stepYear*50 > minimumStep) {this.scale = TimeStep.SCALE.YEAR; this.step = 50;} - if (stepYear*10 > minimumStep) {this.scale = TimeStep.SCALE.YEAR; this.step = 10;} - if (stepYear*5 > minimumStep) {this.scale = TimeStep.SCALE.YEAR; this.step = 5;} - if (stepYear > minimumStep) {this.scale = TimeStep.SCALE.YEAR; this.step = 1;} - if (stepMonth*3 > minimumStep) {this.scale = TimeStep.SCALE.MONTH; this.step = 3;} - if (stepMonth > minimumStep) {this.scale = TimeStep.SCALE.MONTH; this.step = 1;} - if (stepDay*5 > minimumStep) {this.scale = TimeStep.SCALE.DAY; this.step = 5;} - if (stepDay*2 > minimumStep) {this.scale = TimeStep.SCALE.DAY; this.step = 2;} - if (stepDay > minimumStep) {this.scale = TimeStep.SCALE.DAY; this.step = 1;} - if (stepDay/2 > minimumStep) {this.scale = TimeStep.SCALE.WEEKDAY; this.step = 1;} - if (stepHour*4 > minimumStep) {this.scale = TimeStep.SCALE.HOUR; this.step = 4;} - if (stepHour > minimumStep) {this.scale = TimeStep.SCALE.HOUR; this.step = 1;} - if (stepMinute*15 > minimumStep) {this.scale = TimeStep.SCALE.MINUTE; this.step = 15;} - if (stepMinute*10 > minimumStep) {this.scale = TimeStep.SCALE.MINUTE; this.step = 10;} - if (stepMinute*5 > minimumStep) {this.scale = TimeStep.SCALE.MINUTE; this.step = 5;} - if (stepMinute > minimumStep) {this.scale = TimeStep.SCALE.MINUTE; this.step = 1;} - if (stepSecond*15 > minimumStep) {this.scale = TimeStep.SCALE.SECOND; this.step = 15;} - if (stepSecond*10 > minimumStep) {this.scale = TimeStep.SCALE.SECOND; this.step = 10;} - if (stepSecond*5 > minimumStep) {this.scale = TimeStep.SCALE.SECOND; this.step = 5;} - if (stepSecond > minimumStep) {this.scale = TimeStep.SCALE.SECOND; this.step = 1;} - if (stepMillisecond*200 > minimumStep) {this.scale = TimeStep.SCALE.MILLISECOND; this.step = 200;} - if (stepMillisecond*100 > minimumStep) {this.scale = TimeStep.SCALE.MILLISECOND; this.step = 100;} - if (stepMillisecond*50 > minimumStep) {this.scale = TimeStep.SCALE.MILLISECOND; this.step = 50;} - if (stepMillisecond*10 > minimumStep) {this.scale = TimeStep.SCALE.MILLISECOND; this.step = 10;} - if (stepMillisecond*5 > minimumStep) {this.scale = TimeStep.SCALE.MILLISECOND; this.step = 5;} - if (stepMillisecond > minimumStep) {this.scale = TimeStep.SCALE.MILLISECOND; this.step = 1;} + if (stepYear*1000 > minimumStep) {this.scale = 'year'; this.step = 1000;} + if (stepYear*500 > minimumStep) {this.scale = 'year'; this.step = 500;} + if (stepYear*100 > minimumStep) {this.scale = 'year'; this.step = 100;} + if (stepYear*50 > minimumStep) {this.scale = 'year'; this.step = 50;} + if (stepYear*10 > minimumStep) {this.scale = 'year'; this.step = 10;} + if (stepYear*5 > minimumStep) {this.scale = 'year'; this.step = 5;} + if (stepYear > minimumStep) {this.scale = 'year'; this.step = 1;} + if (stepMonth*3 > minimumStep) {this.scale = 'month'; this.step = 3;} + if (stepMonth > minimumStep) {this.scale = 'month'; this.step = 1;} + if (stepDay*5 > minimumStep) {this.scale = 'day'; this.step = 5;} + if (stepDay*2 > minimumStep) {this.scale = 'day'; this.step = 2;} + if (stepDay > minimumStep) {this.scale = 'day'; this.step = 1;} + if (stepDay/2 > minimumStep) {this.scale = 'weekday'; this.step = 1;} + if (stepHour*4 > minimumStep) {this.scale = 'hour'; this.step = 4;} + if (stepHour > minimumStep) {this.scale = 'hour'; this.step = 1;} + if (stepMinute*15 > minimumStep) {this.scale = 'minute'; this.step = 15;} + if (stepMinute*10 > minimumStep) {this.scale = 'minute'; this.step = 10;} + if (stepMinute*5 > minimumStep) {this.scale = 'minute'; this.step = 5;} + if (stepMinute > minimumStep) {this.scale = 'minute'; this.step = 1;} + if (stepSecond*15 > minimumStep) {this.scale = 'second'; this.step = 15;} + if (stepSecond*10 > minimumStep) {this.scale = 'second'; this.step = 10;} + if (stepSecond*5 > minimumStep) {this.scale = 'second'; this.step = 5;} + if (stepSecond > minimumStep) {this.scale = 'second'; this.step = 1;} + if (stepMillisecond*200 > minimumStep) {this.scale = 'millisecond'; this.step = 200;} + if (stepMillisecond*100 > minimumStep) {this.scale = 'millisecond'; this.step = 100;} + if (stepMillisecond*50 > minimumStep) {this.scale = 'millisecond'; this.step = 50;} + if (stepMillisecond*10 > minimumStep) {this.scale = 'millisecond'; this.step = 10;} + if (stepMillisecond*5 > minimumStep) {this.scale = 'millisecond'; this.step = 5;} + if (stepMillisecond > minimumStep) {this.scale = 'millisecond'; this.step = 1;} }; /** @@ -335,7 +316,7 @@ TimeStep.prototype.setMinimumStep = function(minimumStep) { TimeStep.prototype.snap = function(date) { var clone = new Date(date.valueOf()); - if (this.scale == TimeStep.SCALE.YEAR) { + if (this.scale == 'year') { var year = clone.getFullYear() + Math.round(clone.getMonth() / 12); clone.setFullYear(Math.round(year / this.step) * this.step); clone.setMonth(0); @@ -345,7 +326,7 @@ TimeStep.prototype.snap = function(date) { clone.setSeconds(0); clone.setMilliseconds(0); } - else if (this.scale == TimeStep.SCALE.MONTH) { + else if (this.scale == 'month') { if (clone.getDate() > 15) { clone.setDate(1); clone.setMonth(clone.getMonth() + 1); @@ -360,7 +341,7 @@ TimeStep.prototype.snap = function(date) { clone.setSeconds(0); clone.setMilliseconds(0); } - else if (this.scale == TimeStep.SCALE.DAY) { + else if (this.scale == 'day') { //noinspection FallthroughInSwitchStatementJS switch (this.step) { case 5: @@ -373,7 +354,7 @@ TimeStep.prototype.snap = function(date) { clone.setSeconds(0); clone.setMilliseconds(0); } - else if (this.scale == TimeStep.SCALE.WEEKDAY) { + else if (this.scale == 'weekday') { //noinspection FallthroughInSwitchStatementJS switch (this.step) { case 5: @@ -386,7 +367,7 @@ TimeStep.prototype.snap = function(date) { clone.setSeconds(0); clone.setMilliseconds(0); } - else if (this.scale == TimeStep.SCALE.HOUR) { + else if (this.scale == 'hour') { switch (this.step) { case 4: clone.setMinutes(Math.round(clone.getMinutes() / 60) * 60); break; @@ -395,7 +376,7 @@ TimeStep.prototype.snap = function(date) { } clone.setSeconds(0); clone.setMilliseconds(0); - } else if (this.scale == TimeStep.SCALE.MINUTE) { + } else if (this.scale == 'minute') { //noinspection FallthroughInSwitchStatementJS switch (this.step) { case 15: @@ -410,7 +391,7 @@ TimeStep.prototype.snap = function(date) { } clone.setMilliseconds(0); } - else if (this.scale == TimeStep.SCALE.SECOND) { + else if (this.scale == 'second') { //noinspection FallthroughInSwitchStatementJS switch (this.step) { case 15: @@ -424,7 +405,7 @@ TimeStep.prototype.snap = function(date) { clone.setMilliseconds(Math.round(clone.getMilliseconds() / 500) * 500); break; } } - else if (this.scale == TimeStep.SCALE.MILLISECOND) { + else if (this.scale == 'millisecond') { var step = this.step > 5 ? this.step / 2 : 1; clone.setMilliseconds(Math.round(clone.getMilliseconds() / step) * step); } @@ -441,14 +422,14 @@ TimeStep.prototype.isMajor = function() { if (this.switchedYear == true) { this.switchedYear = false; switch (this.scale) { - case TimeStep.SCALE.YEAR: - case TimeStep.SCALE.MONTH: - case TimeStep.SCALE.WEEKDAY: - case TimeStep.SCALE.DAY: - case TimeStep.SCALE.HOUR: - case TimeStep.SCALE.MINUTE: - case TimeStep.SCALE.SECOND: - case TimeStep.SCALE.MILLISECOND: + case 'year': + case 'month': + case 'weekday': + case 'day': + case 'hour': + case 'minute': + case 'second': + case 'millisecond': return true; default: return false; @@ -457,12 +438,12 @@ TimeStep.prototype.isMajor = function() { else if (this.switchedMonth == true) { this.switchedMonth = false; switch (this.scale) { - case TimeStep.SCALE.WEEKDAY: - case TimeStep.SCALE.DAY: - case TimeStep.SCALE.HOUR: - case TimeStep.SCALE.MINUTE: - case TimeStep.SCALE.SECOND: - case TimeStep.SCALE.MILLISECOND: + case 'weekday': + case 'day': + case 'hour': + case 'minute': + case 'second': + case 'millisecond': return true; default: return false; @@ -471,10 +452,10 @@ TimeStep.prototype.isMajor = function() { else if (this.switchedDay == true) { this.switchedDay = false; switch (this.scale) { - case TimeStep.SCALE.MILLISECOND: - case TimeStep.SCALE.SECOND: - case TimeStep.SCALE.MINUTE: - case TimeStep.SCALE.HOUR: + case 'millisecond': + case 'second': + case 'minute': + case 'hour': return true; default: return false; @@ -482,20 +463,20 @@ TimeStep.prototype.isMajor = function() { } switch (this.scale) { - case TimeStep.SCALE.MILLISECOND: + case 'millisecond': return (this.current.getMilliseconds() == 0); - case TimeStep.SCALE.SECOND: + case 'second': return (this.current.getSeconds() == 0); - case TimeStep.SCALE.MINUTE: + case 'minute': return (this.current.getHours() == 0) && (this.current.getMinutes() == 0); - case TimeStep.SCALE.HOUR: + case 'hour': return (this.current.getHours() == 0); - case TimeStep.SCALE.WEEKDAY: // intentional fall through - case TimeStep.SCALE.DAY: + case 'weekday': // intentional fall through + case 'day': return (this.current.getDate() == 1); - case TimeStep.SCALE.MONTH: + case 'month': return (this.current.getMonth() == 0); - case TimeStep.SCALE.YEAR: + case 'year': return false; default: return false; @@ -514,8 +495,7 @@ TimeStep.prototype.getLabelMinor = function(date) { date = this.current; } - var scaleName = TimeStep.SCALE_INDEX[this.scale]; - var format = TimeStep.FORMAT.minorLabels[scaleName]; + var format = TimeStep.FORMAT.minorLabels[this.scale]; return (format && format.length > 0) ? moment(date).format(format) : ''; }; @@ -530,8 +510,7 @@ TimeStep.prototype.getLabelMajor = function(date) { date = this.current; } - var scaleName = TimeStep.SCALE_INDEX[this.scale]; - var format = TimeStep.FORMAT.majorLabels[scaleName]; + var format = TimeStep.FORMAT.majorLabels[this.scale]; return (format && format.length > 0) ? moment(date).format(format) : ''; };