]> Softwares of Agnibho - pdosage.git/blob - src/routine.js
Show dosage rate
[pdosage.git] / src / routine.js
1 /**********************************************************************
2 * Title: PDosage
3 * Description: Pediatric Calculator
4 * Author: Agnibho Mondal
5 * Website: http://code.agnibho.com
6 **********************************************************************
7 Copyright (c) 2016 Agnibho Mondal
8 All rights reserved
9 **********************************************************************
10 This file is part of PDosage.
11
12 PDosage is free software: you can redistribute it and/or modify
13 it under the terms of the GNU General Public License as published by
14 the Free Software Foundation, either version 3 of the License, or
15 (at your option) any later version.
16
17 PDosage is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
21
22 You should have received a copy of the GNU General Public License
23 along with PDosage. If not, see <http://www.gnu.org/licenses/>.
24 **********************************************************************/
25
26 $(document).ready(function(){
27
28 //Remove loader
29 $(".loader").remove();
30 $(".container").fadeIn();
31
32 //Insert version code
33 $(".version").text(VERSION);
34 $(".data-ver").text(JSON.parse(localStorage.getItem("pdosage_data")).version);
35
36 //Update copyright
37 $(".copyright").each(function(){
38 if(new Date().getFullYear()>$(this).data("start")){
39 $(this).text($(this).data("start")+"-"+new Date().getFullYear());
40 }
41 else{
42 $(this).text(new Date().getFullYear());
43 }
44 });
45
46 //Emit input on form reset
47 $("input[type='reset']").on("click", function(e){
48 this.form.reset();
49 $(this.form).find("input, select, textarea").each(function(){
50 this.dispatchEvent(new Event("input"));
51 });
52 });
53
54 //Notifications
55 $(window).resize(function(){
56 $("#notify").width($(".container").width()-20);
57 });
58 $(window).scroll(function(){
59 $("#notify").width($(".container").width()-20);
60 });
61
62 //Parse app info from server
63 $.get("https://code.agnibho.com/pdosage/info.json", function(data){
64 var vCurr=VERSION.split(".").map(Number);
65 var vLtst=data.latest.split(".").map(Number);
66
67 if(isBiggerThan(data.latest, VERSION)){
68 $("#notify").slideDown();
69 $("#notify").width($(".container").width()-20);
70 $("#notify-text").text("A new version of PDosage is available.");
71 if(document.URL.indexOf("http://")==-1 && document.URL.indexOf("https://")==-1){
72 if(/(android)/i.test(navigator.userAgent)){
73 $("#notify-link").attr("href", data.apk);
74 $("#notify-link").text("Download");
75 }
76 else{
77 $("#notify-link").attr("href", data.url);
78 $("#notify-link").text("Load");
79 }
80 }
81 else{
82 $("#notify-link").attr("href", data.url);
83 $("#notify-link").text("Load");
84 }
85 }
86
87 try{
88 if(data.data.latest>JSON.parse(localStorage.getItem("pdosage_data")).version){
89 $.get(data.data.src, function(d){
90 localStorage.setItem("pdosage_data", JSON.stringify(d));
91 });
92 }
93 }
94 catch(e){}
95 });
96
97 //Compare versions
98 function isBiggerThan(v1, v2){
99 while(v1.length<v2.length){
100 v1.push(0);
101 }
102 while(v2.length<v1.length){
103 v2.push(0);
104 }
105 for(var i=0; i<v1.length; i++){
106 if(v1[i]>v2[i]){
107 return true;
108 }
109 }
110 return false;
111 }
112 });