X-Git-Url: https://code.agnibho.com/repo?a=blobdiff_plain;ds=sidebyside;f=dietsurvey%2Fscript.js;fp=dietsurvey%2Fscript.js;h=96a84bea6714b83a1efb64fb86f5f8c1475edaaa;hb=e852388debf20fdb3047dd9fe4751f63d0b2833b;hp=0000000000000000000000000000000000000000;hpb=491fb507c42930847967357e6830964813cd592e;p=dietsurvey.git diff --git a/dietsurvey/script.js b/dietsurvey/script.js new file mode 100644 index 0000000..96a84be --- /dev/null +++ b/dietsurvey/script.js @@ -0,0 +1,656 @@ +/********************************************************************** + * Title: Diet-Survey + * Description: Application for calculating nutrient intake + * Author: Agnibho Mondal + * Website: http://code.agnibho.com + ********************************************************************** + Copyright (c) 2013-2015 Agnibho Mondal + All rights reserved + ********************************************************************** + This file is part of Diet-Survey. + + Diet-Survey is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + Diet-Survey is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with Diet-Survey. If not, see . + **********************************************************************/ +var foodlist=[]; +var customfood=[]; +var family={}; +family.members=[]; +var foods=[]; +var total={}; + + +$(document).ready(function(){ + $("#js-check").hide(); + enum_date(); + $("#date-month").change(function(e){ + adjust_day($(e.target).val()); + }); + + $("#final").hide(); + + $("#block-wait").modal("show"); + $.get("ajax.php?initdata=true", function(data, state){ + $("#block-wait").modal("hide"); + if(state=="success"){ + init=JSON.parse(data); + foodlist=init.list; + customfood=init.custom; + for(i=0; i"+foodlist[i]+""); + } + } + else{ + $("#connect-fail").modal("show"); + } + }); + + $("#edit-member").on("show.bs.modal", function(){ + $("#member-name").val(""); + $("#member-age").val(""); + $("[name='member-sex']").removeProp("checked"); + $("#member-female-npnl").prop("checked", "true"); + $("#member-work-box").hide(); + $("#female-special").hide(); + }); + $("#member-age").change(function(){ + adjust_member(); + }); + $("[name='member-sex']").change(function(){ + adjust_member(); + }); + $("#save-member").click(function(){ + mem={}; + mem.name=$("#member-name").val(); + if(mem.name.length==0){ + alert("Name can't be empty"); + return; + } + mem.age=$("#member-age").val(); + if(mem.age=="" || isNaN(mem.age) || mem.age<0 || mem.age>100){ + alert("Please enter correct age"); + return; + } + if($("#member-sex-male").is(":checked")){ + mem.sex="Male"; + } + else if($("#member-sex-female").is(":checked")){ + mem.sex="Female"; + } + else{ + alert("Please select sex"); + return; + } + if(mem.age>=18){ + mem.work=$("#member-work").val(); + if(mem.age<=45 && mem.sex=="Female"){ + if($("#member-female-npnl").is(":checked")){ + mem.state="npnl"; + } + if($("#member-female-pregnant").is(":checked")){ + mem.state="preg"; + } + if($("#member-female-lactating-lt6").is(":checked")){ + mem.state="lactb6"; + } + if($("#member-female-lactating-gt6").is(":checked")){ + mem.state="lacto6"; + } + } + } + family.members.push(mem); + show_members(); + $("#edit-member").modal("hide"); + }); + $("#family-members").on("click", ".remove-member", function(e){ + family.members.splice($(e.target).data("id"), 1); + show_members(); + }); + + $("#edit-food").on("show.bs.modal", function(){ + $("#food-name").val(""); + $("#food-amount").val(""); + }); + + $("#add-food").click(function(){ + item={}; + item.name=$("#food-name").val(); + item.amount=$("#food-amount").val(); + if(item.name==""){ + alert("Please select a food"); + return; + } + if(item.amount=="" || isNaN(item.amount) || item.amount<=0){ + alert("Please enter correct amount"); + return; + } + foods.push(item); + show_foods(); + $("#edit-food").modal("hide"); + }); + $("#launch-custom").click(function(){ + $("#edit-food").modal("hide"); + $("#customize-food").modal("show"); + }); + $("#food-list").on("click", ".remove-food", function(e){ + foods.splice($(e.target).data("id"), 1); + show_foods(); + }); + + $("#customize-food").on("show.bs.modal", function(){ + show_custom_foods(); + }); + $("#custom-save").click(function(){ + save_custom_food(); + }); + $("#custom-existing").on("click", ".custom-delete", function(e){ + del_id=$(e.target).data("id"); + $("#customize-food :input, #customize-food :button").prop("disabled", "disabled"); + $.post("ajax.php", {"delete_custom_food":del_id}, function(data, state){ + if(state=="success" && JSON.parse(data).flag){ + customfood.splice(del_id, 1); + show_custom_foods(); + } + else{ + alert("Server sync failed"); + } + $("#customize-food :input, #customize-food :button").removeProp("disabled", "disabled"); + }); + }); + + $("#get-result").click(function(){ + family.head=$("#family-head").val(); + family.date=[$("#date-day").val(), $("#date-month").val(), $("#date-year").val()]; + if(family.head.length==0){ + alert("Please enter the name of the head of the family"); + return; + } + else if(family.members.length==0){ + alert("Please add family members"); + } + else if(foods.length==0){ + alert("Please add consumed foods"); + } + retrieve(); + }); + +}); + +function retrieve(){ + compact={}; + compact.subjects=[]; + compact.foods=[]; + local=[]; + $.extend(true, compact.subjects, family.members); + $.extend(true, compact.foods, foods); + for(i=0; itotal.intake.energy){ + total.diff.energy.state="-"; + total.diff.energy.value=total.requirement.energy-total.intake.energy + } + else{ + total.diff.energy.state="+"; + total.diff.energy.value=total.intake.energy-total.requirement.energy + } + if(total.requirement.protein>total.intake.protein){ + total.diff.protein.state="-"; + total.diff.protein.value=total.requirement.protein-total.intake.protein + } + else{ + total.diff.protein.state="+"; + total.diff.protein.value=total.intake.protein-total.requirement.protein + } + if(total.requirement.iron>total.intake.iron){ + total.diff.iron.state="-"; + total.diff.iron.value=total.requirement.iron-total.intake.iron + } + else{ + total.diff.iron.state="+"; + total.diff.iron.value=total.intake.iron-total.requirement.iron + } + if(total.requirement.vitA>total.intake.vitA){ + total.diff.vitA.state="-"; + total.diff.vitA.value=total.requirement.vitA-total.intake.vitA + } + else{ + total.diff.vitA.state="+"; + total.diff.vitA.value=total.intake.vitA-total.requirement.vitA + } + if(total.requirement.thiamine>total.intake.thiamine){ + total.diff.thiamine.state="-"; + total.diff.thiamine.value=total.requirement.thiamine-total.intake.thiamine + } + else{ + total.diff.thiamine.state="+"; + total.diff.thiamine.value=total.intake.thiamine-total.requirement.thiamine + } + if(total.requirement.riboflavin>total.intake.riboflavin){ + total.diff.riboflavin.state="-"; + total.diff.riboflavin.value=total.requirement.riboflavin-total.intake.riboflavin + } + else{ + total.diff.riboflavin.state="+"; + total.diff.riboflavin.value=total.intake.riboflavin-total.requirement.riboflavin + } + if(total.requirement.vitC>total.intake.vitC){ + total.diff.vitC.state="-"; + total.diff.vitC.value=total.requirement.vitC-total.intake.vitC + } + else{ + total.diff.vitC.state="+"; + total.diff.vitC.value=total.intake.vitC-total.requirement.vitC + } +} + + +function adjust_member(){ + age=$("#member-age").val(); + fem=false; + if($("#member-sex-female").is(":checked")){ + fem=true; + } + if(age>=18){ + $("#member-work-box").show(); + if(fem && age<=45){ + $("#female-special").show(); + } + else{ + $("#female-special").hide(); + } + } + else{ + $("#member-work-box").hide(); + $("#female-special").hide(); + } +} + +function show_members(){ + $("#family-members").html(""); + for(i=0; iFood NameAmount (gm or ml)Remove'); + for(i=0; iNameEnergyProteinIronVitamin AThiamineRiboflavinVitamin CDelete"); + for(i=0; i'+customfood[i].name+''+customfood[i].energy+''+customfood[i].protein+''+customfood[i].iron+''+customfood[i].vitA+''+customfood[i].thiamine+''+customfood[i].riboflavin+''+customfood[i].vitC+''); + } +} +function save_custom_food(){ + sv={}; + sv.name=$("#custom-name").val(); + sv.energy=$("#custom-energy").val(); + sv.protein=$("#custom-protein").val(); + sv.iron=$("#custom-iron").val(); + sv.vitA=$("#custom-vitA").val(); + sv.thiamine=$("#custom-thiamine").val(); + sv.riboflavin=$("#custom-riboflavin").val(); + sv.vitC=$("#custom-vitC").val(); + if(sv.name==""){ + alert("Please enter name"); + return; + } + else if(!foodlist.indexOf(sv.name)==-1){ + alert("The food is already present"); + return; + } + else if(!(check(sv.energy) && check(sv.protein) && check(sv.iron) && check(sv.vitA) && check(sv.thiamine) && check(sv.riboflavin) && check(sv.vitC))){ + alert("Please enter proper nutritional values"); + return; + } + $("#customize-food :input, #customize-food :button").prop("disabled", "disabled"); + $.post("ajax.php", sv, function(data, state){ + if(state=="success" && JSON.parse(data).flag){ + customfood.push(sv); + foodlist.push(sv.name); + show_custom_foods(); + for(i=0; i"+foodlist[i]+""); + } + } + else{ + alert("Server sync failed"); + } + $("#customize-food :input, #customize-food :button").removeProp("disabled"); + }); + + function check(val){ + if(val=="" || isNaN(val) || val<0){ + return false; + } + else{ + return true; + } + } +} + +function show_result(){ + $("#initial").hide(); + $("#final").slideDown(); + + $("#show-hof").text(family.head); + $("#show-date").text(family.date[0]+"-"+family.date[1]+"-"+family.date[2]); + + $("#show-members").html("NameAgeSex"); + $("#show-member-points").html(""); + for(i=0; i"+family.members[i].name+""+family.members[i].age+" years"+family.members[i].sex+""); + if(family.members[i].age>=18){ + $("#show-member-points").append('
  • '+family.members[i].name+' is a '+family.members[i].work+' worker
  • '); + if(family.members[i].age<=45 && family.members[i].sex=="Female"){ + if(family.members[i].state=="pregnant"){ + $("#show-member-points").append('
  • '+family.members[i].name+' is pregnant
  • '); + } + else if(family.members[i].state=="lactb6"){ + $("#show-member-points").append('
  • '+family.members[i].name+' is lactating (below 6 months)
  • '); + } + if(family.members[i].state=="lacto6"){ + $("#show-member-points").append('
  • '+family.members[i].name+' is lactating (over 6 months)
  • '); + } + } + } + } + + $("#show-foods").html("NameAmount (mg or ml)"); + for(i=0; i"+foods[i].name+""+foods[i].amount+""); + } + + $("#show-requirement").html("MemberEnergyProteinIronVitamin AThiamineRiboflavinVitamin C"); + for(i=0; i"+family.members[i].name+""+family.members[i].rda.energy.toFixed(2)+" kcal"+family.members[i].rda.protein.toFixed(2)+" gm"+family.members[i].rda.iron.toFixed(2)+" mg"+family.members[i].rda.vitA.toFixed(2)+" μg"+family.members[i].rda.thiamine.toFixed(2)+" mg"+family.members[i].rda.riboflavin.toFixed(2)+" mg"+family.members[i].rda.vitC.toFixed(2)+" mg"); + } + $("#show-requirement").append("Total"+total.requirement.energy.toFixed(2)+" kcal"+total.requirement.protein.toFixed(2)+" gm"+total.requirement.iron.toFixed(2)+" mg"+total.requirement.vitA.toFixed(2)+" μg"+total.requirement.thiamine.toFixed(2)+" mg"+total.requirement.riboflavin.toFixed(2)+" mg"+total.requirement.vitC.toFixed(2)+" mg"); + + $("#show-value").html("FoodEnergyProteinIronVitamin AThiamineRiboflavinVitamin C"); + for(i=0; i"+foods[i].name+""+foods[i].intake.energy.toFixed(2)+""+foods[i].intake.protein.toFixed(2)+""+foods[i].intake.iron.toFixed(2)+""+foods[i].intake.vitA.toFixed(2)+""+foods[i].intake.thiamine.toFixed(2)+""+foods[i].intake.riboflavin.toFixed(2)+""+foods[i].intake.vitC.toFixed(2)+""); + } + $("#show-value").append("Total"+total.intake.energy.toFixed(2)+""+total.intake.protein.toFixed(2)+""+total.intake.iron.toFixed(2)+""+total.intake.vitA.toFixed(2)+""+total.intake.thiamine.toFixed(2)+""+total.intake.riboflavin.toFixed(2)+""+total.intake.vitC.toFixed(2)+""); + + $("#show-compare").html("NutrientRequirementIntakeDeficeiency / Excess"); + $("#show-summary").html(""); + if(total.diff.energy.state=="+"){ + $("#show-compare").append("Energy"+total.requirement.energy.toFixed(2)+""+total.intake.energy.toFixed(2)+""+total.diff.energy.value.toFixed(2)+" excess"); + $("#show-summary").append("
  • Energy consumption is excess by "+((total.diff.energy.value*100)/total.requirement.energy).toFixed(2)+"%
  • "); + } + else{ + $("#show-compare").append("Energy"+total.requirement.energy.toFixed(2)+""+total.intake.energy.toFixed(2)+""+total.diff.energy.value.toFixed(2)+" deficient"); + $("#show-summary").append("
  • Energy consumption is deficient by "+((total.diff.energy.value*100)/total.requirement.energy).toFixed(2)+"%
  • "); + } + if(total.diff.protein.state=="+"){ + $("#show-compare").append("Protein"+total.requirement.protein.toFixed(2)+""+total.intake.protein.toFixed(2)+""+total.diff.protein.value.toFixed(2)+" excess"); + $("#show-summary").append("
  • Protein consumption is excess by "+((total.diff.protein.value*100)/total.requirement.protein).toFixed(2)+"%
  • "); + } + else{ + $("#show-compare").append("Protein"+total.requirement.protein.toFixed(2)+""+total.intake.protein.toFixed(2)+""+total.diff.protein.value.toFixed(2)+" deficient"); + $("#show-summary").append("
  • Protein consumption is deficient by "+((total.diff.protein.value*100)/total.requirement.protein).toFixed(2)+"%
  • "); + } + if(total.diff.iron.state=="+"){ + $("#show-compare").append("Iron"+total.requirement.iron.toFixed(2)+""+total.intake.iron.toFixed(2)+""+total.diff.iron.value.toFixed(2)+" excess"); + $("#show-summary").append("
  • Iron consumption is excess by "+((total.diff.iron.value*100)/total.requirement.iron).toFixed(2)+"%
  • "); + } + else{ + $("#show-compare").append("Iron"+total.requirement.iron.toFixed(2)+""+total.intake.iron.toFixed(2)+""+total.diff.iron.value.toFixed(2)+" deficient"); + $("#show-summary").append("
  • Iron consumption is deficient by "+((total.diff.iron.value*100)/total.requirement.iron).toFixed(2)+"%
  • "); + } + if(total.diff.vitA.state=="+"){ + $("#show-compare").append("Vitamin A"+total.requirement.vitA.toFixed(2)+""+total.intake.vitA.toFixed(2)+""+total.diff.vitA.value.toFixed(2)+" excess"); + $("#show-summary").append("
  • Vitamin A consumption is excess by "+((total.diff.vitA.value*100)/total.requirement.vitA).toFixed(2)+"%
  • "); + } + else{ + $("#show-compare").append("Vitamin A"+total.requirement.vitA.toFixed(2)+""+total.intake.vitA.toFixed(2)+""+total.diff.vitA.value.toFixed(2)+" deficient"); + $("#show-summary").append("
  • Vitamin A consumption is deficient by "+((total.diff.vitA.value*100)/total.requirement.vitA).toFixed(2)+"%
  • "); + } + if(total.diff.thiamine.state=="+"){ + $("#show-compare").append("Thiamine"+total.requirement.thiamine.toFixed(2)+""+total.intake.thiamine.toFixed(2)+""+total.diff.thiamine.value.toFixed(2)+" excess"); + $("#show-summary").append("
  • Thiamine consumption is excess by "+((total.diff.thiamine.value*100)/total.requirement.thiamine).toFixed(2)+"%
  • "); + } + else{ + $("#show-compare").append("Thiamine"+total.requirement.thiamine.toFixed(2)+""+total.intake.thiamine.toFixed(2)+""+total.diff.thiamine.value.toFixed(2)+" deficient"); + $("#show-summary").append("
  • Thiamine consumption is deficient by "+((total.diff.thiamine.value*100)/total.requirement.thiamine).toFixed(2)+"%
  • "); + } + if(total.diff.riboflavin.state=="+"){ + $("#show-compare").append("Riboflavin"+total.requirement.riboflavin.toFixed(2)+""+total.intake.riboflavin.toFixed(2)+""+total.diff.riboflavin.value.toFixed(2)+" excess"); + $("#show-summary").append("
  • Riboflavin consumption is excess by "+((total.diff.riboflavin.value*100)/total.requirement.riboflavin).toFixed(2)+"%
  • "); + } + else{ + $("#show-compare").append("Riboflavin"+total.requirement.riboflavin.toFixed(2)+""+total.intake.riboflavin.toFixed(2)+""+total.diff.riboflavin.value.toFixed(2)+" deficient"); + $("#show-summary").append("
  • Riboflavin consumption is deficient by "+((total.diff.riboflavin.value*100)/total.requirement.riboflavin).toFixed(2)+"%
  • "); + } + if(total.diff.vitC.state=="+"){ + $("#show-compare").append("Vitamin C"+total.requirement.vitC.toFixed(2)+""+total.intake.vitC.toFixed(2)+""+total.diff.vitC.value.toFixed(2)+" excess"); + $("#show-summary").append("
  • Vitamin C consumption is excess by "+((total.diff.vitC.value*100)/total.requirement.vitC).toFixed(2)+"%
  • "); + } + else{ + $("#show-compare").append("Vitamin C"+total.requirement.vitC.toFixed(2)+""+total.intake.vitC.toFixed(2)+""+total.diff.vitC.value.toFixed(2)+" deficient"); + $("#show-summary").append("
  • Vitamin C consumption is deficient by "+((total.diff.vitC.value*100)/total.requirement.vitC).toFixed(2)+"%
  • "); + } +} + +function enum_date(){ + for(i=2010; i<=2020; i++){ + if(i==2014){ + $("#date-year").append(""); + } + else{ + $("#date-year").append(""); + } + } + for(i=1; i<=12; i++){ + if(i==10){ + $("#date-month").append(""); + } + else{ + $("#date-month").append(""); + } + } + adjust_day(10); +} + +function adjust_day(month){ + lim=31; + if(month==2){ + lim=28; + } + else if(month==4||month==6||month==9||month==11){ + lim=30; + } + day=$("#date-day").val(); + $("#date-day").html(""); + if(day>lim){ + day=lim; + } + for(i=1; i<=lim; i++){ + if(i==day){ + $("#date-day").append(""); + } + else{ + $("#date-day").append(""); + } + } +} + +function display_member(mem, i){ + html='
    ' + +'
    ' + +'
    ' + +'
    ' + +'
    ' + +'
    ' + +'
    ' + +'
    ' + +'
    ' + +'' + +'
    ' + +'

    '+mem.name+'

    ' + +'
    ' + +'
    ' + +'
    ' + +'' + +'
    ' + +'

    '+mem.age+'

    ' + +'
    ' + +'
    ' + +'
    ' + +'' + +'
    ' + +'

    '+mem.sex+'

    ' + +'
    ' + +'
    '; + if(mem.age>=18){ + html=html+'
    ' + +'' + +'
    ' + +'

    '+mem.work+'

    ' + +'
    ' + +'
    ' + } + html=html+'
    ' + +'
    '; + if(mem.age>=18 && mem.age<=45 && mem.sex=="Female"){ + if(mem.state=="npnl"){ + state=""; + } + else if(mem.state=="preg"){ + state="Pregnant"; + } + else if(mem.state=="lactb6"){ + state="Lactating, below 6 months"; + } + else if(mem.state=="lacto6"){ + state="Lactating, over 6 months"; + } + if(state!=""){ + html=html+''; + } + } + html=html+'
    '; + return html; +} + +function display_food(item, i){ + html=''+item.name+''+item.amount+''; + return html; +} + +function draw_bar_diagram(){ + data={ + labels: ["Energy", "Protein", "Iron", "Vitamine A", "Thiamine", "Riboflavin", "Vitamin C"], + datasets: [ + { + label: "dataset", + fillColor: "rgba(151,187,205,0.5)", + strokeColor: "rgba(151,187,205,0.8)", + highlightFill: "rgba(151,187,205,0.75)", + highlightStroke: "rgba(151,187,205,1)", + data: [parseFloat(((total.intake.energy*100)/total.requirement.energy).toFixed(2)), parseFloat(((total.intake.protein*100)/total.requirement.protein).toFixed(2)), parseFloat(((total.intake.iron*100)/total.requirement.iron).toFixed(2)), parseFloat(((total.intake.vitA*100)/total.requirement.vitA).toFixed(2)), parseFloat(((total.intake.thiamine*100)/total.requirement.thiamine).toFixed(2)), parseFloat(((total.intake.riboflavin*100)/total.requirement.riboflavin).toFixed(2)), parseFloat(((total.intake.vitC*100)/total.requirement.vitC).toFixed(2))] + } + ] + }; + + + var ctx=$("#bar-diagram").get(0).getContext("2d"); + var diagram=new Chart(ctx).Bar(data); +}