]> Softwares of Agnibho - pdosage.git/blob - src/routine.js
Typo correction
[pdosage.git] / src / routine.js
1 /**********************************************************************
2 * Title: DietSurvey
3 * Description: Nutritional Assessment App
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 DietSurvey.
11
12 DietSurvey 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 DietSurvey 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 DietSurvey. If not, see <http://www.gnu.org/licenses/>.
24 **********************************************************************/
25
26 $.ajaxSetup({cache:false});
27
28 $(document).ready(function(){
29
30 //Remove loader
31 $(".loader").remove();
32 $(".container").fadeIn();
33
34 //Insert version code
35 $(".version").text(VERSION);
36 try{
37 $(".data-ver").text(JSON.parse(localStorage.getItem(STORAGE)).version);
38 } catch(e){}
39
40 //Update copyright
41 $(".copyright").each(function(){
42 if(new Date().getFullYear()>$(this).data("start")){
43 $(this).text($(this).data("start")+"-"+new Date().getFullYear());
44 }
45 else{
46 $(this).text(new Date().getFullYear());
47 }
48 });
49
50 //Emit input on form reset
51 $("input[type='reset']").on("click", function(e){
52 this.form.reset();
53 $(this.form).find("input, select, textarea").each(function(){
54 this.dispatchEvent(new Event("input"));
55 });
56 });
57
58 //Change focus after number input
59 $(".jump-focus").on("input", function(){
60 if($(this).val().length == $(this).prop("maxlength")){
61 var all=$("input").toArray();
62 var i=all.indexOf(this)+1;
63 $(all[i]).focus().select();
64 }
65 });
66
67 //Defocus after input finished
68 $(".stop-focus").on("input", function(){
69 if($(this).val().length == $(this).prop("maxlength")){
70 $(this).blur();
71 var target=$($(this).data("ref"));
72 console.log($(target).offset());
73 $("html, body").animate({
74 scrollTop: $(target).offset().top
75 }, 1000);
76 }
77 });
78
79 //Use custom datepicker if Firefox
80 if($(".datepicker").length && navigator.userAgent.indexOf("Firefox")!=-1){
81 $(".datepicker").datepicker({
82 format:"yyyy-mm-dd",
83 autoclose:true
84 }).on("changeDate", function(){
85 this.dispatchEvent(new Event("input"));
86 });
87 }
88
89 //Notifications
90 $(window).resize(function(){
91 $("#notify").width($(".container").width()-20);
92 });
93 $(window).scroll(function(){
94 $("#notify").width($(".container").width()-20);
95 });
96 //Get data from server
97 $.get(INFO_URL, function(data){
98 var vCurr=VERSION.split(".").map(Number);
99 var vLtst=data.latest.split(".").map(Number);
100 //Define version comparator
101 function isBiggerThan(v1, v2){
102 while(v1.length<v2.length){
103 v1.push(0);
104 }
105 while(v2.length<v1.length){
106 v2.push(0);
107 }
108 for(var i=0; i<v1.length; i++){
109 if(v1[i]>v2[i]){
110 return true;
111 }
112 }
113 return false;
114 }
115 //Compare versions
116 if(NOTIFY && isBiggerThan(data.latest, VERSION)){
117 $("#notify").slideDown();
118 $("#notify").width($(".container").width()-20);
119 $("#notify-text").text("A new version of "+NAME+" is available.");
120 if(document.URL.indexOf("http://")==-1 && document.URL.indexOf("https://")==-1){
121 if(/(android)/i.test(navigator.userAgent)){
122 $("#notify-link").attr("href", data.apk);
123 $("#notify-link").text("Download");
124 }
125 else{
126 $("#notify-link").attr("href", data.url);
127 $("#notify-link").text("Load");
128 }
129 }
130 else{
131 $("#notify-link").attr("href", data.url);
132 $("#notify-link").text("Load");
133 }
134 }
135 //Update app data
136 try{
137 if(data.data.latest>JSON.parse(localStorage.getItem(STORAGE)).version){
138 $.get(data.data.src, function(d){
139 localStorage.setItem(STORAGE, JSON.stringify(d));
140 });
141 }
142 }
143 catch(e){}
144 });
145 });