]> Softwares of Agnibho - pdosage.git/blob - src/routine.js
Minor corrections
[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 //Disable cache
28 $.ajaxSetup({"cache": false});
29
30 //Remove loader
31 $(".loader").remove();
32 $(".container").fadeIn();
33
34 //Insert version code
35 $(".version").text(VERSION);
36 $(".data-ver").text(JSON.parse(localStorage.getItem("pdosage_data")).version);
37
38 //Update copyright
39 $(".copyright").each(function(){
40 if(new Date().getFullYear()>$(this).data("start")){
41 $(this).text($(this).data("start")+"-"+new Date().getFullYear());
42 }
43 else{
44 $(this).text(new Date().getFullYear());
45 }
46 });
47
48 //Emit input on form reset
49 $("input[type='reset']").on("click", function(e){
50 this.form.reset();
51 $(this.form).find("input, select, textarea").each(function(){
52 this.dispatchEvent(new Event("input"));
53 });
54 });
55
56 //Notifications
57 $(window).resize(function(){
58 $("#notify").width($(".container").width()-20);
59 });
60 $(window).scroll(function(){
61 $("#notify").width($(".container").width()-20);
62 });
63
64 //Parse app info from server
65 $.get("https://code.agnibho.com/pdosage/info.json", function(data){
66 var vCurr=VERSION.split(".").map(Number);
67 var vLtst=data.latest.split(".").map(Number);
68
69 if(isBiggerThan(data.latest, VERSION)){
70 $("#notify").slideDown();
71 $("#notify").width($(".container").width()-20);
72 $("#notify-text").text("A new version of PDosage is available.");
73 if(document.URL.indexOf("http://")==-1 && document.URL.indexOf("https://")==-1){
74 if(/(android)/i.test(navigator.userAgent)){
75 $("#notify-link").attr("href", data.apk);
76 $("#notify-link").text("Download");
77 }
78 else{
79 $("#notify-link").attr("href", data.url);
80 $("#notify-link").text("Load");
81 }
82 }
83 else{
84 $("#notify-link").attr("href", data.url);
85 $("#notify-link").text("Load");
86 }
87 }
88
89 try{
90 if(data.data.latest>JSON.parse(localStorage.getItem("pdosage_data")).version){
91 $.get(data.data.src, function(d){
92 localStorage.setItem("pdosage_data", JSON.stringify(d));
93 });
94 }
95 }
96 catch(e){}
97 });
98
99 //Compare versions
100 function isBiggerThan(v1, v2){
101 while(v1.length<v2.length){
102 v1.push(0);
103 }
104 while(v2.length<v1.length){
105 v2.push(0);
106 }
107 for(var i=0; i<v1.length; i++){
108 if(v1[i]>v2[i]){
109 return true;
110 }
111 }
112 return false;
113 }
114 });