2021-02-27 21:15:19 +00:00
|
|
|
$.fn.frequencyInput = function() {
|
2021-02-27 21:30:48 +00:00
|
|
|
var suffixes = {
|
|
|
|
"K": 3,
|
|
|
|
"M": 6,
|
|
|
|
"G": 9,
|
|
|
|
"T": 12
|
|
|
|
};
|
|
|
|
|
2021-02-27 21:15:19 +00:00
|
|
|
this.each(function(){
|
|
|
|
var $group = $(this);
|
|
|
|
var currentExponent = 0;
|
|
|
|
$input = $group.find('input');
|
|
|
|
|
|
|
|
var setExponent = function() {
|
|
|
|
var newExponent = parseInt($exponent.val());
|
|
|
|
var delta = currentExponent - newExponent;
|
|
|
|
$input.val(parseFloat($input.val()) * 10 ** delta);
|
|
|
|
currentExponent = newExponent;
|
|
|
|
};
|
|
|
|
|
2021-02-27 21:30:48 +00:00
|
|
|
$input.on('keydown', function(e) {
|
|
|
|
var c = String.fromCharCode(e.which);
|
|
|
|
if (c in suffixes) {
|
|
|
|
currentExponent = suffixes[c];
|
|
|
|
$exponent.val(suffixes[c]);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2021-02-27 21:15:19 +00:00
|
|
|
$exponent = $group.find('select.frequency-exponent');
|
|
|
|
$exponent.on('change', setExponent);
|
|
|
|
|
|
|
|
// calculate initial exponent
|
|
|
|
$exponent.val(Math.floor(Math.log10($input.val()) / 3) * 3);
|
|
|
|
setExponent();
|
|
|
|
})
|
|
|
|
};
|