-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.js
1 lines (1 loc) · 17.1 KB
/
main.js
1
async function registerSW(){if("serviceWorker"in navigator)try{console.log("Service worker made it"),await navigator.serviceWorker.register("./serviceworker.js")}catch(e){console.log("Service worker couldn't make it")}}var user_place,user_state;function getLocation(){navigator.geolocation?navigator.geolocation.getCurrentPosition(showPosition,showError):x.innerHTML="Geolocation is not supported by this browser."}function showError(e){switch(document.getElementById("error_location").style.display="block",document.getElementById("wrapper").style.display="none",e.code){case e.PERMISSION_DENIED:console.log("User denied the request for Geolocation."),document.getElementById("loc_error_message").innerHTML=" User denied the request for Geolocation.";break;case e.POSITION_UNAVAILABLE:console.log("Location information is unavailable."),document.getElementById("loc_error_message").innerHTML=" Location information is unavailable.";break;case e.TIMEOUT:console.log("The request to get user location timed out."),document.getElementById("loc_error_message").innerHTML=" The request to get user location timed out.";break;case e.UNKNOWN_ERROR:console.log("An unknown error occurred."),document.getElementById("loc_error_message").innerHTML=" An unknown error occurred."}}function get_services(e){fetch("https://api.covid19india.org/resources/resources.json").then(function(e){return e.json()}).then(function(t){var n=t.resources.length,o=0,r=document.getElementById("area_services"),d=0;for(o=0;o<n-1;o++)if(t.resources[o].city.toLowerCase()==e.toLowerCase()){var a=document.createElement("li"),c=document.createElement("a"),s=t.resources[o].category+" - "+t.resources[o].phonenumber;c.appendChild(document.createTextNode(s)),a.style.color="teal",c.setAttribute("href",t.resources[o].contact),a.appendChild(c),r.appendChild(a),d++}d>0?(document.getElementById("area_services_load").innerHTML="Available services in your area <br>(tap to open)",document.getElementById("service_available").style.display="block"):document.getElementById("area_services_load").innerHTML="No serives registered at this moment"})}function showPosition(e){var t=e.coords.latitude,n=e.coords.longitude;console.log("STAY SAFE <3"),console.log("A PROJECT FOR THE COMMUNITY, BY THE COMMUNITY :)");var o={async:!0,crossDomain:!0,url:"https://us1.locationiq.com/v1/reverse.php?key=59503131102f5b&lat="+t+"&lon="+n+"&format=json",method:"GET"};$.ajax(o).done(function(e){var t,n,o=document.getElementsByClassName("user_city");e.address.state_district?(o[0].innerHTML=e.address.state_district,t=e.address.state_district):(o[0].innerHTML=e.address.city,t=e.address.city),document.getElementsByClassName("user_region")[0].innerHTML=e.address.state,n=e.address.state,document.getElementsByClassName("user_country")[0].innerHTML=e.address.country,get_services(e.address.city),fetch("./plotting/excep.json").then(function(e){return e.json()}).then(function(e){var r=0;for(r=0;r<e.length;r++)if(e[r].district==t){t=e[r].district2,o[0].innerHTML=t;break}card1_info(t,n)})}),mapboxgl.accessToken="pk.eyJ1IjoiYWF5dXNoc2FpbmkiLCJhIjoiY2s4Zzl0cm0zMGFlNTNwbm9tZXJ5cThydiJ9.c8-bbolyM9LbA0zyO16wwg";var r=new mapboxgl.Map({container:"map",style:"mapbox://styles/mapbox/dark-v10",zoom:9,center:[n,t]});r.addControl(new mapboxgl.FullscreenControl);var d={width:200,height:200,data:new Uint8Array(16e4),onAdd:function(){var e=document.createElement("canvas");e.width=this.width,e.height=this.height,this.context=e.getContext("2d")},render:function(){var e=performance.now()%1e3/1e3,t=70*e+30,n=this.context;return n.clearRect(0,0,this.width,this.height),n.beginPath(),n.arc(this.width/2,this.height/2,t,0,2*Math.PI),n.fillStyle="rgba(255, 200, 200,"+(1-e)+")",n.fill(),n.beginPath(),n.arc(this.width/2,this.height/2,30,0,2*Math.PI),n.fillStyle="rgba(255, 100, 100, 1)",n.strokeStyle="white",n.lineWidth=2+4*(1-e),n.fill(),n.stroke(),this.data=n.getImageData(0,0,this.width,this.height).data,r.triggerRepaint(),!0}};r.on("load",function(){r.addImage("pulsing-dot",d,{pixelRatio:2}),r.addSource("points",{type:"geojson",data:{type:"FeatureCollection",features:[{type:"Feature",geometry:{type:"Point",coordinates:[n,t]}}]}}),r.addLayer({id:"points",type:"symbol",source:"points",layout:{"icon-image":"pulsing-dot"}}),r.addSource("earthquakes",{type:"geojson",data:"./plotting/map.geojson?ver=2.99607",cluster:!0,clusterMaxZoom:14,clusterRadius:50}),r.addLayer({id:"clusters",type:"circle",source:"earthquakes",filter:["has","point_count"],paint:{"circle-color":["step",["get","point_count"],"rgba(29, 245, 195, 0.75)",100,"rgba(246, 168, 33, 0.77)",250,"rgba(245, 105, 29, 0.77)",600,"rgba(255, 15, 15, 0.84)"],"circle-radius":["step",["get","point_count"],20,100,30,750,40]}});document.getElementById("menu");r.addLayer({id:"cluster-count",type:"symbol",source:"earthquakes",filter:["has","point_count"],layout:{"text-field":"{point_count_abbreviated}","text-font":["DIN Offc Pro Medium","Arial Unicode MS Bold"],"text-size":12}}),r.addLayer({id:"unclustered-point",type:"circle",source:"earthquakes",filter:["!",["has","point_count"]],paint:{"circle-color":"#1df5c3","circle-radius":10,"circle-stroke-width":4,"circle-stroke-color":"#19c29b"}}),r.on("mouseenter","clusters",function(){r.getCanvas().style.cursor="pointer"}),r.on("mouseleave","clusters",function(){r.getCanvas().style.cursor=""});var e=new mapboxgl.Popup({closeButton:!1,closeOnClick:!1});r.on("mouseenter","unclustered-point",function(t){var n=t.features[0].geometry.coordinates.slice();r.getCanvas().style.cursor="pointer",e.setLngLat(n).setHTML("One Patient Registered Here").addTo(r)}),r.on("mouseleave","unclustered-point",function(){r.getCanvas().style.cursor="",e.remove()})})}function card1_info(e,t){fetch("https://api.covid19india.org/v2/state_district_wise.json").then(function(e){return e.json()}).then(function(n){e=e.replace(/\s/g,"").toLowerCase();var o=0,r=0,d=0;for(o=0;o<n.length;o++)if(n[o].state==t)for(r=0;r<n[o].districtData.length;r++){if(n[o].districtData[r].district.toLowerCase().replace(/\s/g,"")==e){d=1,document.getElementById("user_place_cnf").innerHTML=n[o].districtData[r].confirmed,document.getElementById("user_place_cnf").style.color="coral",document.getElementById("user_place_cnf").style.fontWeight="bold",document.getElementById("user_place_cnf").style.fontSize="1.12em",document.getElementById("user_place_act").innerHTML=n[o].districtData[r].active,document.getElementById("user_place_act").style.color="#ff7043",document.getElementById("user_place_act").style.fontWeight="bold",document.getElementById("user_place_act").style.fontSize="1.12em",document.getElementById("user_place_rec").innerHTML=n[o].districtData[r].recovered,document.getElementById("user_place_rec").style.color="#1de9b6",document.getElementById("user_place_rec").style.fontWeight="bold",document.getElementById("user_place_rec").style.fontSize="1.12em",document.getElementById("user_place_ded").innerHTML=n[o].districtData[r].deceased,document.getElementById("user_place_ded").style.color="#e53935",document.getElementById("user_place_ded").style.fontWeight="bold",document.getElementById("user_place_ded").style.fontSize="1.12em",n[o].districtData[r].delta.confirmed<1?document.getElementById("cnf_daily_dist").style.display="none":(document.getElementById("increased_count_district_cnf").innerHTML=n[o].districtData[r].delta.confirmed,document.getElementById("cnf_arrow_district").innerHTML="arrow_upward",document.getElementById("cnf_arrow_district").style.color="red"),n[o].districtData[r].delta.recovered<1?document.getElementById("rec_daily_dist").style.display="none":(document.getElementById("increased_count_district_rec").innerHTML=n[o].districtData[r].delta.recovered,document.getElementById("rec_arrow_district").innerHTML="arrow_upward",document.getElementById("rec_arrow_district").style.color="aqua");break}d=0}0==d&&(document.getElementById("user_place_cnf").innerHTML=0,document.getElementById("user_place_act").innerHTML=0,document.getElementById("user_place_rec").innerHTML=0,document.getElementById("user_place_ded").innerHTML=0)}),card2_info(t)}function card2_info(e){fetch("https://api.covid19india.org/data.json").then(function(e){return e.json()}).then(function(t){var n=1;for(t.statewise[0].deltaconfirmed<1?document.getElementById("cnt_sig").style.display="none":(document.getElementById("increased_count").innerHTML=t.statewise[0].deltaconfirmed,document.getElementById("cnf_arrow_home").innerHTML="arrow_upward",document.getElementById("cnf_arrow_home").style.color="red"),n=1;n<t.statewise.length;n++)t.statewise[n].state==e&&(document.getElementById("user_state_cnf").innerHTML=t.statewise[n].confirmed,document.getElementById("user_state_cnf").style.color="coral",document.getElementById("user_state_cnf").style.fontWeight="bold",document.getElementById("user_state_cnf").style.fontSize="1.12em",document.getElementById("user_state_act").innerHTML=t.statewise[n].active,document.getElementById("user_state_act").style.color="#ff7043",document.getElementById("user_state_act").style.fontWeight="bold",document.getElementById("user_state_act").style.fontSize="1.12em",document.getElementById("user_state_rec").innerHTML=t.statewise[n].recovered,document.getElementById("user_state_rec").style.color="#1de9b6",document.getElementById("user_state_rec").style.fontWeight="bold",document.getElementById("user_state_rec").style.fontSize="1.12em",document.getElementById("user_state_ded").innerHTML=t.statewise[n].deaths,document.getElementById("user_state_ded").style.color="#e53935 ",document.getElementById("user_state_ded").style.fontWeight="bold",document.getElementById("user_state_ded").style.fontSize="1.12em",t.statewise[n].deltaconfirmed<1?document.getElementById("state_cnf_sig").style.display="none":(document.getElementById("increased_count_state_c").innerHTML=t.statewise[n].deltaconfirmed,document.getElementById("cnf_arrow_state").innerHTML="arrow_upward",document.getElementById("cnf_arrow_state").style.color="red"),t.statewise[n].deltarecovered<1?document.getElementById("state_cnf_sig").style.display="none":(document.getElementById("increased_count_state_r").innerHTML=t.statewise[n].deltarecovered,document.getElementById("rec_arrow_state").innerHTML="arrow_upward",document.getElementById("rec_arrow_state").style.color="aqua"));document.getElementById("user_country_cnf").innerHTML=t.statewise[0].confirmed,document.getElementById("user_country_cnf").style.color="coral",document.getElementById("user_country_cnf").style.fontWeight="bold",document.getElementById("user_country_cnf").style.fontSize="1.12em",document.getElementById("user_country_act").innerHTML=t.statewise[0].active,document.getElementById("user_country_act").style.color="#ff7043",document.getElementById("user_country_act").style.fontWeight="bold",document.getElementById("user_country_act").style.fontSize="1.12em",document.getElementById("user_country_rec").innerHTML=t.statewise[0].recovered,document.getElementById("user_country_rec").style.color="#1de9b6",document.getElementById("user_country_rec").style.fontWeight="bold",document.getElementById("user_country_rec").style.fontSize="1.12em",document.getElementById("user_country_ded").innerHTML=t.statewise[0].deaths,document.getElementById("user_country_ded").style.color="#e53935 ",document.getElementById("user_country_ded").style.fontWeight="bold",document.getElementById("user_country_ded").style.fontSize="1.12em"})}function load_analytics(){get_data1(),get_data2()}window.addEventListener("load",()=>{registerSW()}),document.addEventListener("DOMContentLoaded",function(){var e=document.querySelectorAll(".sidenav");M.Sidenav.init(e,{edge:"right"})});const x_axis_rec=[],y_axis_rec=[],x_axis_ded=[],y_axis_ded=[];function draw(e,t,n,o){Chart.defaults.global.defaultFontColor="#969696";var r=e,d=t,a=n,c=o;document.getElementById("load").style.display="none",new Chart(document.getElementById("line-chart_cnf"),{type:"line",data:{labels:r,datasets:[{data:d,label:"Daily confirmed cases in India",borderColor:"#de7e00",backgroundColor:"#edaf5c",fill:!0}]}}),new Chart(document.getElementById("line-chart_cnf2"),{type:"line",data:{labels:r,datasets:[{data:d,label:"Daily confirmed cases in India",borderColor:"#de7e00",backgroundColor:"#edaf5c",fill:!0}]}}),document.getElementById("load_rec").style.display="none",new Chart(document.getElementById("line-chart_rec"),{type:"line",data:{labels:r,datasets:[{data:a,label:"Daily recovered cases in India",borderColor:"#09bd78",backgroundColor:"#71e3b7",fill:!0}]}}),new Chart(document.getElementById("line-chart_rec2"),{type:"line",data:{labels:r,datasets:[{data:a,label:"Daily recovered cases in India",borderColor:"#09bd78",backgroundColor:"#71e3b7",fill:!0}]}}),document.getElementById("load_ded").style.display="none",new Chart(document.getElementById("line-chart_ded"),{type:"line",data:{labels:r,datasets:[{data:c,label:"Daily death cases in India",borderColor:"#db3030",backgroundColor:"#e38686",fill:!0}]}}),new Chart(document.getElementById("line-chart_ded2"),{type:"line",data:{labels:r,datasets:[{data:c,label:"Daily death cases in India",borderColor:"#db3030",backgroundColor:"#e38686",fill:!0}]}})}function draw2(e,t,n,o){x_axis_state=e,y_axis_state=t,y_axis_state_death=n,y_axis_state_rec=o,document.getElementById("load_state").style.display="none",new Chart(document.getElementById("line-chart_dedm"),{type:"horizontalBar",data:{labels:x_axis_state,datasets:[{data:y_axis_state,label:"No. of cases in each state",borderColor:"#a900e6",backgroundColor:"#cf7bed",fill:!0}]}}),new Chart(document.getElementById("line-chart_dedm2"),{type:"bar",data:{labels:x_axis_state,datasets:[{data:y_axis_state,label:"No. of cases in each state",borderColor:"#a900e6",backgroundColor:"#cf7bed",fill:!0}]}})}function get_data1(){fetch("https://api.covid19india.org/data.json").then(function(e){return e.json()}).then(function(e){var t,n,o=[],r=[],d=[],a=[],c=e.cases_time_series.length,s=0;for(s=0;s<e.cases_time_series.length;s++)o[s]=e.cases_time_series[s].date,r[s]=Number(e.cases_time_series[s].dailyconfirmed),d[s]=Number(e.cases_time_series[s].dailyrecovered),a[s]=Number(e.cases_time_series[s].dailydeceased);t=e.cases_time_series[c-1].totalconfirmed,today_count_rec=e.cases_time_series[c-1].totalrecovered,today_count_ded=e.cases_time_series[c-1].totaldeceased;var i=e.cases_time_series.length;document.getElementById("data_update_date1").innerHTML=e.cases_time_series[i-1].date,document.getElementById("data_update_date2").innerHTML=e.cases_time_series[i-1].date,document.getElementById("data_update_date3").innerHTML=e.cases_time_series[i-1].date;var l=today_count_rec/t*100,u=today_count_ded/t*100;l=l.toString().slice(0,6),u=u.toString().slice(0,6),document.getElementById("recovery_rate").innerHTML=l,document.getElementById("death_rate").innerHTML=u;var m=e.tested.length;""==e.tested[m-1].totalindividualstested?document.getElementById("icmr_individuals").innerHTML="N/A":document.getElementById("icmr_individuals").innerHTML=e.tested[m-1].totalindividualstested,document.getElementById("icmr_samples").innerHTML=e.tested[m-1].totalsamplestested,n=e.cases_time_series[c-2].totalconfirmed,yesterday_count_rec=e.cases_time_series[c-2].totalrecovered,yesterday_count_ded=e.cases_time_series[c-2].totaldeceased;var _=t-n,y=today_count_rec-yesterday_count_rec,g=today_count_ded-yesterday_count_ded;document.getElementById("cnf_no").innerHTML=t,document.getElementById("rec_no").innerHTML=today_count_rec,document.getElementById("ded_no").innerHTML=today_count_ded,draw(o,r,d,a),document.getElementById("cnf_no").style.color="coral",document.getElementById("rec_no").style.color="#1de9b6",document.getElementById("ded_no").style.color="#ff3838",_>0?(document.getElementById("daily_cnf_arrow").innerHTML="arrow_upward",document.getElementById("daily_cnf_arrow").style.color="red",document.getElementById("count_diff").innerHTML=_):(document.getElementById("daily_cnf_arrow").innerHTML="arrow_downward",document.getElementById("daily_cnf_arrow").style.color="green",document.getElementById("count_diff").innerHTML=_),y>0?(document.getElementById("daily_rec_arrow").innerHTML="arrow_upward",document.getElementById("daily_rec_arrow").style.color="#07b536",document.getElementById("count_diff_rec").innerHTML=y):(document.getElementById("daily_rec_arrow").innerHTML="arrow_downward",document.getElementById("daily_rec_arrow").style.color="red",document.getElementById("count_diff_rec").innerHTML=y),g>0?(document.getElementById("daily_ded_arrow").innerHTML="arrow_upward",document.getElementById("daily_ded_arrow").style.color="red",document.getElementById("count_diff_ded").innerHTML=g):(document.getElementById("daily_ded_arrow").innerHTML="arrow_downward",document.getElementById("daily_ded_arrow").style.color="#07b536",document.getElementById("count_diff_ded").innerHTML=g)})}function get_data2(){fetch("https://api.covid19india.org/data.json").then(function(e){return e.json()}).then(function(e){var t,n=[],o=[],r=[],d=[];for(t=1;t<e.statewise.length;t++)n[t]=e.statewise[t].state,o[t]=Number(e.statewise[t].confirmed),r[t]=Number(e.statewise[t].deaths),d[t]=Number(e.statewise[t].recovered);draw2(n,o,r,d)})}function get_world_data(){fetch("https://covid19.mathdro.id/api/").then(function(e){return e.json()}).then(function(e){document.getElementById("cnf_world").innerHTML=e.confirmed.value,document.getElementById("rec_world").innerHTML=e.recovered.value,document.getElementById("ded_world").innerHTML=e.deaths.value})}get_world_data();