diff --git a/lib/timeline/TimeStep.js b/lib/timeline/TimeStep.js index 1da29a85..cef93adc 100644 --- a/lib/timeline/TimeStep.js +++ b/lib/timeline/TimeStep.js @@ -50,7 +50,7 @@ function TimeStep(start, end, minimumStep, hiddenDates) { } } -/// enum scale +// enum scale TimeStep.SCALE = { MILLISECOND: 1, SECOND: 2, @@ -62,6 +62,36 @@ TimeStep.SCALE = { 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: { + 'millisecond':'SSS', + 'second': 's', + 'minute': 'HH:mm', + 'hour': 'HH:mm', + 'weekday': 'ddd D', + 'day': 'D', + 'month': 'MMM', + 'year': 'YYYY' + }, + majorLabels: { + 'millisecond':'HH:mm:ss', + 'second': 'D MMMM HH:mm', + 'minute': 'ddd D MMMM', + 'hour': 'ddd D MMMM', + 'weekday': 'MMMM YYYY', + 'day': 'MMMM YYYY', + 'month': 'YYYY', + 'year': '' + } +}; /** * Set a new range @@ -484,20 +514,11 @@ TimeStep.prototype.getLabelMinor = function(date) { date = this.current; } - switch (this.scale) { - case TimeStep.SCALE.MILLISECOND: return moment(date).format('SSS'); - case TimeStep.SCALE.SECOND: return moment(date).format('s'); - case TimeStep.SCALE.MINUTE: return moment(date).format('HH:mm'); - case TimeStep.SCALE.HOUR: return moment(date).format('HH:mm'); - case TimeStep.SCALE.WEEKDAY: return moment(date).format('ddd D'); - case TimeStep.SCALE.DAY: return moment(date).format('D'); - case TimeStep.SCALE.MONTH: return moment(date).format('MMM'); - case TimeStep.SCALE.YEAR: return moment(date).format('YYYY'); - default: return ''; - } + var scaleName = TimeStep.SCALE_INDEX[this.scale]; + var format = TimeStep.FORMAT.minorLabels[scaleName]; + return (format && format.length > 0) ? moment(date).format(format) : ''; }; - /** * Returns formatted text for the major axis label, depending on the current * date and the scale. For example when scale is MINUTE, the major scale is @@ -509,18 +530,9 @@ TimeStep.prototype.getLabelMajor = function(date) { date = this.current; } - //noinspection FallthroughInSwitchStatementJS - switch (this.scale) { - case TimeStep.SCALE.MILLISECOND:return moment(date).format('HH:mm:ss'); - case TimeStep.SCALE.SECOND: return moment(date).format('D MMMM HH:mm'); - case TimeStep.SCALE.MINUTE: - case TimeStep.SCALE.HOUR: return moment(date).format('ddd D MMMM'); - case TimeStep.SCALE.WEEKDAY: - case TimeStep.SCALE.DAY: return moment(date).format('MMMM YYYY'); - case TimeStep.SCALE.MONTH: return moment(date).format('YYYY'); - case TimeStep.SCALE.YEAR: return ''; - default: return ''; - } + var scaleName = TimeStep.SCALE_INDEX[this.scale]; + var format = TimeStep.FORMAT.majorLabels[scaleName]; + return (format && format.length > 0) ? moment(date).format(format) : ''; }; module.exports = TimeStep;