diff --git a/htdocs/lib/FrequencyDisplay.js b/htdocs/lib/FrequencyDisplay.js index ae50fe7..aecc03f 100644 --- a/htdocs/lib/FrequencyDisplay.js +++ b/htdocs/lib/FrequencyDisplay.js @@ -1,4 +1,10 @@ function FrequencyDisplay(element) { + this.prefixes = { + 'k': 3, + 'M': 6, + 'G': 9, + 'T': 12 + }; this.element = $(element); this.digits = []; this.precision = 4; @@ -9,13 +15,23 @@ function FrequencyDisplay(element) { FrequencyDisplay.prototype.setupElements = function() { this.displayContainer = $('
'); this.digitContainer = $(''); - this.displayContainer.html([this.digitContainer, $(' MHz')]); + this.unitContainer = $(' Hz'); + this.displayContainer.html([this.digitContainer, this.unitContainer]); this.element.html(this.displayContainer); }; +FrequencyDisplay.prototype.getPrefix = function() { + var me = this; + return Object.keys(me.prefixes).filter(function(key){ + return me.prefixes[key] == me.exponent; + })[0] || ""; +}; + FrequencyDisplay.prototype.setFrequency = function(freq) { this.frequency = freq; - var formatted = (freq / 1e6).toLocaleString( + this.exponent = Math.floor(Math.log10(this.frequency) / 3) * 3; + + var formatted = (freq / 10 ** this.exponent).toLocaleString( undefined, {maximumFractionDigits: this.precision, minimumFractionDigits: this.precision} ); @@ -36,6 +52,7 @@ FrequencyDisplay.prototype.setFrequency = function(freq) { while (this.digits.length > formatted.length) { this.digits.pop().remove(); } + this.unitContainer.text(' ' + this.getPrefix() + 'Hz'); }; FrequencyDisplay.prototype.setFrequencyPrecision = function(precision) { @@ -68,7 +85,7 @@ TuneableFrequencyDisplay.prototype.setupEvents = function() { var index = me.digitContainer.find('.digit').index(e.target); if (index < 0) return; - var delta = 10 ** (Math.floor(Math.max(6, Math.log10(me.frequency))) - index); + var delta = 10 ** (Math.floor(Math.max(me.exponent, Math.log10(me.frequency))) - index); if (e.originalEvent.deltaY > 0) delta *= -1; var newFrequency = me.frequency + delta; diff --git a/htdocs/lib/settings/ExponentialInput.js b/htdocs/lib/settings/ExponentialInput.js index 65bf1ce..603651d 100644 --- a/htdocs/lib/settings/ExponentialInput.js +++ b/htdocs/lib/settings/ExponentialInput.js @@ -1,9 +1,9 @@ $.fn.exponentialInput = function() { - var suffixes = { - "K": 3, - "M": 6, - "G": 9, - "T": 12 + var prefixes = { + 'K': 3, + 'M': 6, + 'G': 9, + 'T': 12 }; this.each(function(){ @@ -26,9 +26,9 @@ $.fn.exponentialInput = function() { $input.on('keydown', function(e) { var c = String.fromCharCode(e.which); - if (c in suffixes) { - currentExponent = suffixes[c]; - $exponent.val(suffixes[c]); + if (c in prefixes) { + currentExponent = prefixes[c]; + $exponent.val(prefixes[c]); } });