2024-05-02 15:54:10 +02:00
|
|
|
// #### Gauge
|
|
|
|
|
|
|
|
// The Gauge object encapsulates the behavior
|
|
|
|
// of simple gauge. Most of the implementation
|
|
|
|
// is in the CSS rules, but we do have a bit
|
|
|
|
// of JavaScript to set or read the gauge value
|
|
|
|
|
|
|
|
function Gauge(el) {
|
|
|
|
|
|
|
|
// ##### Private Properties and Attributes
|
|
|
|
|
|
|
|
var element, // Containing element for the info component
|
|
|
|
data, // `.gauge__data` element
|
|
|
|
needle, // `.gauge__needle` element
|
|
|
|
value = 0.0, // Current gauge value from 0 to 1
|
2024-05-02 22:39:18 +02:00
|
|
|
prop, // Style for transform
|
|
|
|
valueLabel; // `.gauge__label--spacer` element
|
2024-05-02 15:54:10 +02:00
|
|
|
|
|
|
|
// ##### Private Methods and Functions
|
|
|
|
|
|
|
|
var setElement = function(el) {
|
|
|
|
// Keep a reference to the various elements and sub-elements
|
|
|
|
element = el;
|
2024-05-03 00:33:23 +02:00
|
|
|
data = element.querySelector('.gauge__data');
|
|
|
|
needle = element.querySelector('.gauge__needle');
|
|
|
|
valueLabel = element.querySelector('.gauge__label--spacer');
|
2024-05-02 22:39:18 +02:00
|
|
|
|
2024-05-03 00:33:23 +02:00
|
|
|
//valueLabel = element.getElementsByTagName('span'); //valueLabel = element.querySelector('span.gauge__label--spacer');
|
2024-05-02 22:39:18 +02:00
|
|
|
|
2024-05-02 15:54:10 +02:00
|
|
|
};
|
|
|
|
|
2024-05-02 22:39:18 +02:00
|
|
|
var setValue = function(x, max, unit) {
|
|
|
|
percentage = x * 100 / max;
|
|
|
|
value = percentage / 100;
|
|
|
|
var turns = -0.5 + (value * 0.5);
|
2024-05-03 00:33:23 +02:00
|
|
|
data.style[prop] = 'rotate(' + turns + 'turn)';
|
|
|
|
needle.style[prop] = 'rotate(' + turns + 'turn)';
|
2024-05-02 22:39:18 +02:00
|
|
|
valueLabel.textContent = x + unit;
|
|
|
|
/*
|
|
|
|
for (var i = 0; i < valueLabel.children.length; i++) {
|
|
|
|
window.alert(valueLabel.children[i].className);
|
2024-05-03 00:33:23 +02:00
|
|
|
if (valueLabel.children[i].className == 'gauge__label--spacer') {
|
2024-05-02 22:39:18 +02:00
|
|
|
window.alert(valueLabel.children[i]);
|
|
|
|
valueLabel.children[i].textContent = x + unit;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*/
|
2024-05-02 15:54:10 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
// ##### Object to be Returned
|
|
|
|
|
|
|
|
function exports() { };
|
|
|
|
|
|
|
|
// ##### Public API Methods
|
|
|
|
|
|
|
|
exports.element = function(el) {
|
|
|
|
if (!arguments.length) { return element; }
|
|
|
|
setElement(el);
|
|
|
|
return this;
|
|
|
|
};
|
|
|
|
|
2024-05-02 22:39:18 +02:00
|
|
|
exports.value = function(x, max=100, unit='%') {
|
2024-05-02 15:54:10 +02:00
|
|
|
if (!arguments.length) { return value; }
|
2024-05-02 22:39:18 +02:00
|
|
|
setValue(x, max, unit);
|
2024-05-02 15:54:10 +02:00
|
|
|
return this;
|
|
|
|
};
|
|
|
|
|
|
|
|
// ##### Initialization
|
|
|
|
|
2024-05-03 00:33:23 +02:00
|
|
|
var body = document.getElementsByTagName('body')[0];
|
|
|
|
['webkitTransform', 'mozTransform', 'msTransform', 'oTransform', 'transform'].
|
2024-05-02 15:54:10 +02:00
|
|
|
forEach(function(p) {
|
2024-05-03 00:33:23 +02:00
|
|
|
if (typeof body.style[p] !== 'undefined') { prop = p; }
|
2024-05-02 15:54:10 +02:00
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
if (arguments.length) {
|
|
|
|
setElement(el);
|
|
|
|
}
|
|
|
|
|
|
|
|
return exports;
|
|
|
|
|
|
|
|
};
|