bmi-calculator
Version:
Bmi calculator for jQuery
274 lines (237 loc) • 9.66 kB
JavaScript
/*!
--------------------------------
BMI calculator
--------------------------------
- https://github.com/Boele/bmi-calculator.git
- version 1.0.0
- Copyright 2016 Boele Boom
- Licensed under the MIT license
*/
(function($) {
"use strict";
var features = (function() {
/* constants */
var IDEAL_BMI_LOWER = 18.5;
var IDEAL_BMI_UPPER = 25;
/* methods */
var getInput = function(array) {
var input = {};
$.each(array, function(index, value) {
input[value] = $("input[name=" + value + "]").val();
});
return input;
};
var search = function(string, array){
if (typeof(array) === "object") {
for(var key in array) {
var value = array[key];
if(value === string) {
return true;
}
}
} else {
for (var i = 0 ; i < array.length ; i++) {
if (array[i] === string) {
return true;
}
}
}
};
var setBackground = function(selector, color, trans) {
if (trans) {
$(selector).css({
"transition": "background-color " + trans + "s ease",
"background-color": color
});
} else {
$(selector).css({
"background-color": color
});
}
};
var calculateBmi = function(height, weight) {
return weight / (Math.pow(height, 2));
};
var customaryWeight = function(lb, st) {
return st * 14 + lb;
};
var imperialHeight = function(ft, inch) {
return ft * 12 + inch;
};
var metricHeight = function(cm, m) {
return m + (cm / 100);
};
return {
/* constants */
IDEAL_BMI_LOWER: IDEAL_BMI_LOWER,
IDEAL_BMI_UPPER: IDEAL_BMI_UPPER,
/* methods */
getInput: getInput,
calculateBmi: calculateBmi,
imperialHeight: imperialHeight,
customaryWeight: customaryWeight,
metricHeight: metricHeight,
setBackground: setBackground,
search: search
};
})();
var defaults = {
colors: {
underweight: "orange",
idealweight: "green",
overweight: "red"
},
ajax: {
dataType: "JSON",
dataUrl: false
},
debug: false,
colorTransition: "0.5",
decimal: 2
};
$.fn.bmi = function(options) {
var self = $(this).selector;
var settings = $.extend({}, defaults, options);
var bmi = {
classes: [" .bmi-metric", " .bmi-imperial", " .bmi-customary"],
items: [],
origin: [],
score: $(self + " .bmi-score h1"),
scoreContainer: $(self + " .bmi-score"),
onReady: function() {
bmi.origin["name"] = $(self + " select").val();
bmi.isVisible(bmi.classes);
$(self + " select").on("change", function() {
bmi.reset();
bmi.origin["name"] = $(this).val();
bmi.toggle();
bmi.isVisible(bmi.classes);
});
$("#bmi-form input").on("keyup", function() {
bmi.update(bmi.calculate(bmi.items));
});
$("#bmi-form").submit(function(e) {
e.preventDefault();
var score = bmi.calculate(bmi.items).toFixed(settings.decimal);
if ($.isNumeric(score)) {
var input = features.getInput(bmi.items);
input["bmi"] = score;
bmi.ajax(input);
}
});
},
isVisible: function(array) {
$(array).each(function() {
if($(self + this).css("display") !== "none") {
$(self + this + " input[name]").each(function(index, value) {
bmi.items.push(value.name);
});
}
});
},
toggle: function() {
switch(bmi.origin["name"]) {
case "customary":
$(".bmi-metric").hide();
$(".bmi-imperial").hide();
$(".bmi-customary").show();;
break;
case "imperial":
$(".bmi-metric").hide();
$(".bmi-imperial").show();
$(".bmi-customary").hide();;
break;
case "metric":
$(".bmi-metric").show();
$(".bmi-imperial").hide();
$(".bmi-customary").hide();;
break;
}
},
update: function(result) {
bmi.result(bmi.scoreContainer, result);
if ($.isNumeric(result)) {
bmi.score.html(result.toFixed(settings.decimal));
} else {
bmi.reset("score");
}
},
calculate: function(array) {
var input = features.getInput(array);
$.each(input, function(measurement, data) {
if(data !== "") {
data = parseInt(data);
}
input[measurement] = data;
});
if (!features.search("", input)) {
switch (bmi.origin.name) {
case "customary":
return features.calculateBmi(features.imperialHeight(input.custFt, input.custInch),
features.customaryWeight(input.custLb, input.custSt) * 703);
case "imperial":
return features.calculateBmi(features.imperialHeight(input.impFt, input.impInch), input.impLb * 703);
case "metric":
return features.calculateBmi(features.metricHeight(input.cm, input.m), input.kg);
}
} else if (features.search("", input)) {
if (settings.debug) {
console.log(input);
}
return false;
}
},
reset: function(level) {
switch(level) {
case "score":
bmi.score.html("BMI");
bmi.scoreContainer.css({
"background-color": "white"
});;
break;
default:
bmi.items = [];
bmi.origin = [];
$(self + " input[type=number").val("");
bmi.score.html("BMI");
bmi.scoreContainer.css({
"background-color": "white"
});;
}
},
ajax: function(input) {
$.ajax({
type: "POST",
url: settings.ajax.dataUrl,
data: $.param(input),
dataType: settings.ajax.dataType,
success: function(data) {
if (settings.debug) {
console.log(data);
}
},
error: function(request, status, error) {
if (settings.debug) {
console.log(this.data);
console.log(request.responseText);
} else {
return false;
}
}
});
},
result: function(selector, bmi) {
if (bmi < features.IDEAL_BMI_LOWER) {
features.setBackground(selector, settings.colors.underweight, settings.colorTransition);
} else if (bmi > features.IDEAL_BMI_UPPER) {
features.setBackground(selector, settings.colors.overweight, settings.colorTransition);
} else if (bmi >= features.IDEAL_BMI_LOWER && bmi <= features.IDEAL_BMI_UPPER) {
features.setBackground(selector, settings.colors.idealweight, settings.colorTransition);
} else {
features.setBackground(selector, "white", settings.colorTransition);
}
}
};
$(document).ready(bmi.onReady);
};
}(jQuery));