-
Notifications
You must be signed in to change notification settings - Fork 20
/
index.js
427 lines (407 loc) · 14.7 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
import {Closest} from './pos.js';
const container = document.querySelector('.container');
const priceBox = document.querySelector('.price-box');
const change = document.querySelector('.change');
const st = document.getElementById("map");
const getLoc = document.getElementById("getlocation");
const near = document.getElementById('nearby');
const pageContainer = document.getElementById("page-container");
const mapContainer = document.getElementById("map");
const shareWeb = document.getElementById('Web-share');
// Create variables to get the fuel prices and their changes
let petrolPrice , dieselPrice , petrolChange , dieselChange;
// Create variable to get the selected State and City name
let selectedState , selectedCity;
let cords=[];
let rslt=[];//state data
let datac=[];//city data of select state
let slent;
let clent;
let corlent;
let b0,b1,b2,b3;
let markers=L.layerGroup();
const map = L.map('map').setView([25, 75], 3);
const tiles = L.tileLayer('https://tile.openstreetmap.org/{z}/{x}/{y}.png', {
maxZoom: 19,
attribution: '© <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a>'
}).addTo(map);
// var darkModeTileLayer = new L.StamenTileLayer("terrain");
async function getcsv(url,splitter='\r\n')
{
return await fetch(url)
.then(response => response.text())
.then( data => {
let loc=[];
const rows = data.split(splitter);
const headers = rows[0].split(',');
let len=rows.length;
for (let i = 1; i < rows.length; i++) {
const row = rows[i].split(',');
if (row.length === headers.length) {
const obj = {};
for (let j = 0; j < headers.length; j++) {
obj[headers[j]] = row[j];
}
loc.push(obj);
}
}return [loc,len];
})
};
async function bharat()
{ map.removeLayer(markers);
map.setView([20, 77], 3)
container.style.height = '590px';
container.style.marginTop = '1px';
st.style.display='block';
map.invalidateSize();
markers=L.layerGroup();let mar;
let order=Closest (cords,[8.058382,77.544730],"Tamil Nadu",markers);
for(let i=0;i<707;i++)
{ map.invalidateSize();
if(parseFloat(order[1][i]["circ"])<300)
{mar=L.circleMarker([order[1][i]["lat"],order[1][i]["long"]],{
radius:7,
color: '#2a176f',
fillColor: '#2a176f'
}).bindPopup("#2a176f").openPopup();
markers.addLayer(mar);
markers.addTo(map);}
else if(parseFloat(order[1][i]["dist"])<1200)
{mar=L.circleMarker([order[1][i]["lat"],order[1][i]["long"]],{
radius:7,
color: '#138808',
fillColor: '#138808'
}).bindPopup("#138808").openPopup();
markers.addLayer(mar);
markers.addTo(map);}
else if(parseFloat(order[1][i]["dist"])<2200)
{mar=L.circleMarker([order[1][i]["lat"],order[1][i]["long"]],{
radius:7,
color: '#ffffff',
fillColor: '#ffffff'
}).bindPopup("#ffffff").openPopup();
markers.addLayer(mar);
markers.addTo(map);}
else
{mar=L.circleMarker([order[1][i]["lat"],order[1][i]["long"]],{
radius:7,
color: '#f0630e',
fillColor: '#f0630e'
}).bindPopup("#f0630e").openPopup();
markers.addLayer(mar);
markers.addTo(map);}
await new Promise(r => setTimeout(r, 0.01));
}
}
async function getdata(st,ct)
{let i;
for(i=0;i<slent-2;i++)
{
if(rslt[i]["State"].toLowerCase()==(st).toLowerCase())
{var dat=[],clt;
await getcsv('https://rapid-wave-c8e3.redfor14314.workers.dev/https://raw.githubusercontent.com/Fuelish/FuelishCLI/main/assets/'+st+'.csv').then(data =>
{
dat=data[0];
clt=data[1];
})
for(i=0;i<clt;i++)
{
if(dat[i]["City"].toLowerCase()==(ct).toLowerCase())
{return dat[i];break;}
}
break;
}
}
}
async function genmark(gs,gc,lat,lon,out)
{
markers=L.layerGroup();let mar;
mar=L.circleMarker([lat,lon],{
radius:5,
color: 'cyan',
fillColor: 'cyan'
}).bindPopup("Idhar hain aap").openPopup();
markers.addLayer(mar);
let pp,dp;
getdata(gs,gc).then(data=>{
pp=parseFloat(data["Price(P)"]).toFixed(2);
dp=parseFloat(data["Price(D)"]).toFixed(2);
});
let pp1,dp1;
for(let i=0;i<8;i++)
{ await getdata(out[1][i]["State"],out[1][i]["City"]).then(data=>{
pp1=parseFloat(data["Price(P)"]).toFixed(2);
dp1=parseFloat(data["Price(D)"]).toFixed(2);
if((pp1-pp)<=0 && (dp1-dp)<=0)
{
mar=L.circleMarker([out[1][i]["lat"],out[1][i]["long"]],{
data:String(out[1][i]["State"]+'-'+out[1][i]["City"]),
radius:7,
color: '#72ff72',
fillColor: '#72ff72'
}).bindPopup(out[1][i]["State"]+'<p>'+out[1][i]["City"]+'</p>').openPopup().on('click',clicky);
}
else if((pp1-pp)>0 && (dp1-dp)>0)
{
mar=L.circleMarker([out[1][i]["lat"],out[1][i]["long"]],{
data:String(out[1][i]["State"]+'-'+out[1][i]["City"]),
radius:7,
color: '#EE3E3E',
fillColor: '#EE3E3E'
}).bindPopup(out[1][i]["State"]+'<p>'+out[1][i]["City"]+'</p>').openPopup().on('click',clicky);
}
else
{
mar=L.circleMarker([out[1][i]["lat"],out[1][i]["long"]],{
data:String(out[1][i]["State"]+'-'+out[1][i]["City"]),
radius:7,
color: 'orange',
fillColor: 'orange'
}).bindPopup(out[1][i]["State"]+'<p>'+out[1][i]["City"]+'</p>').openPopup().on('click',clicky);
}
markers.addLayer(mar);
markers.addTo(map);
})
}
}
async function clicky(data)
{
near.classList.add('fadeIn');
let txt=data.target.options.data;
const ar=txt.split("-");
const npr = document.getElementById('npr');
getdata(ar[0],ar[1]).then(data=>{
npr.innerHTML="";
npr.innerText=data["City"];
let pp=parseFloat(document.getElementById("pp").innerText).toFixed(2);
let dp=parseFloat(document.getElementById("dp").innerText).toFixed(2);
let pp1=parseFloat(data["Price(P)"]).toFixed(2);
let dp1=parseFloat(data["Price(D)"]).toFixed(2);
if((pp-pp1)>=0)
npr.innerHTML+="<p style='color: #72ff72'>"+"Petrol : "+data["Price(P)"]+"("+(pp1-pp).toFixed(2)+")"+"</p>";
else
npr.innerHTML+="<p style='color: #EE3E3E'>"+"Petrol : "+data["Price(P)"]+"("+(pp1-pp).toFixed(2)+")"+"</p>";
if((dp-dp1)>=0)
npr.innerHTML+="<p style='color: #72ff72'>"+"Diesel : "+data["Price(D)"]+"("+(dp1-dp).toFixed(2)+")"+"</p>";
else
npr.innerHTML+="<p style='color: #EE3E3E'>"+"Diesel : "+data["Price(D)"]+"("+(dp1-dp).toFixed(2)+")"+"</p>";
});
}
getLoc.addEventListener('click', event => {
document.getElementById("getlocation");
let gcity,gstate;
if ('geolocation' in navigator) {
near.classList.remove('fadeIn');
navigator.geolocation.getCurrentPosition( pos => {
const latitude = pos.coords.latitude;
const longitude = pos.coords.longitude;
console.log(latitude, longitude);
fetch('https://nominatim.openstreetmap.org/reverse?format=jsonv2&lat='+latitude+'&lon='+longitude)
.then(response => response.json())
.then(async data => {
let out=Closest(cords,[latitude,longitude],data["address"]["state"],markers);
[gcity,gstate]=out[0];
document.getElementById("state").value=gstate;
console.log(document.getElementById("state").value);
await func(1,gcity);
genmark(gstate,gcity,latitude,longitude,out);
});
}, error => {
document.getElementById("getlocation");
console.log("Geolocation request denied by user", error.code);
});
} else {
document.getElementById("getlocation");
console.log("Geolocation is not supported.");
}
});
window.onload=async ()=>{
let status=0;
while(status!=2){
await getcsv('https://rapid-wave-c8e3.redfor14314.workers.dev/https://raw.githubusercontent.com/Fuelish/FuelishCLI/main/State.csv')
.then(data =>{
rslt=data[0];
slent=data[1];
status=1;
for(let x=0;x<slent-2;x++)
{
document.getElementById("state").innerHTML+="<option value='"+rslt[x]["State"]+"'>"+rslt[x]["State"]+"</option>";
}
});
await getcsv('https://rapid-wave-c8e3.redfor14314.workers.dev/https://raw.githubusercontent.com/Fuelish/FuelishCLI/main/src/Citycord.csv')
.then(data =>{cords=data[0];corlent=data[1];status=2;});
console.log(status);
}
}
async function getcord(city)
{ let arr=[];let i,bl,bound=[];
arr=await fetch('https://nominatim.openstreetmap.org/search.php?q='+city.replace(/ /g, '+')+'&format=jsonv2')
.then(response => response.json())
.then(data => {
return data[0]["boundingbox"];
})
.catch(error => {return [0,0,0,0]});
if(arr[0]==0)
{ console.log("b",arr);
await getcsv('https://raw.githubusercontent.com/Doofenshmirtz-Evil-Incorp/FuelishCLI/main/src/bound.csv','\n')
.then(data =>{bound=data[0];})
.then(data =>{
bl=bound.length;
for(i=0;i<bl-1;i++)
{
if(bound[i]["City"].toLowerCase()==(city.split('+')).slice(-1)[0].toLowerCase())
{
arr=[bound[i]["s"],bound[i]["n"],bound[i]["w"],bound[i]["e"]];
break;
}
}
}
);
}
return arr;
};
async function cfunc(mode=0)
{
map.removeLayer(markers);
var found=0;
let i;near.classList.remove('fadeIn');
// Make the whatsapp icon visible when state and city get selected
document.getElementById('social-sharing-icon').classList.remove('hide-the-icon');
for(i=0;i<clent;i++)
{
if(datac[i]["City"].toLowerCase()==(city.value).toLowerCase())
{
selectedCity = datac[i]["City"];
priceBox.style.display = '';
change.style.display = '';
priceBox.classList.add('fadeIn');
change.classList.add('fadeIn');
container.style.height = '590px';
container.style.marginTop = '1px';
st.style.display='block';
[b0,b1,b2,b3]=await getcord(state.value.replace(/ /g, '+')+"+"+datac[i]["City"]);
map.fitBounds([
[b1, b2],
[b0, b3]
]);
const ele1=document.getElementById("pp");
ele1.innerText=datac[i]["Price(P)"];
petrolPrice = ele1.innerText;
const ele2=document.getElementById("dp");
ele2.innerText=datac[i]["Price(D)"];
dieselPrice = ele2.innerText;
const ele3=document.getElementById("cp");
if((datac[i]["Change(P)"]).charAt(0)==="+")
{
ele3.style.color='#EE3E3E';
}
else if((datac[i]["Change(P)"]).charAt(0)==="-")
{
ele3.style.color='#72ff72';
}
else
{
ele3.style.color='#72ff72';
}
ele3.innerText=datac[i]["Change(P)"];
petrolChange = ele3.innerText;
const ele4=document.getElementById("cd");
if((datac[i]["Change(D)"]).charAt(0)==="+")
{
ele4.style.color='#EE3E3E';
}
else if((datac[i]["Change(D)"]).charAt(0)==="-")
{
ele4.style.color='#72ff72';
}
else
{
ele4.style.color='#72ff72';
}
ele4.innerText=datac[i]["Change(D)"];
dieselChange = ele4.innerText;
found=1;
document.getElementById("getlocation");
if(mode==0)
{
for(let j=0;j<corlent;j++)
{
if(cords[j]["City"]==city.value)
{
let out=Closest(cords,[cords[j]["lat"],cords[j]["long"]],state.value,markers);
genmark(state.value,city.value,cords[j]["lat"],cords[j]["long"],out);
break;
}
}
}
break;
}
}
map.invalidateSize();
}
// Share button using WebShare API
shareWeb.addEventListener("click", async () => {
let location = `Fuel price in ${selectedCity} city, ${selectedState}`;
let fuelInfo = `Petrol price : ${petrolPrice}\nChange in petrol : ${petrolChange}\n\nDiesel price : ${dieselPrice}\nChange in diesel : ${dieselChange}`;
const shareData = {
title: "Fuelish",
text: `Check your fuel prices on Fuelish!\n${location}\n${fuelInfo}\n`,
url: "https://fuelish.dtho.xyz",
};
try {
await navigator.share(shareData);
console.log("Data was shared successfully");
} catch (err) {
console.error("Share failed:", err.message);
}
});
async function func(mode=0,gcity)
{
if(state.value=="")
{return;}
let i;
for(i=0;i<slent-2;i++)
{
if(rslt[i]["State"].toLowerCase()==(state.value).toLowerCase())
{
[b0,b1,b2,b3]=await getcord(state.value);
map.fitBounds([
[b1, b2],
[b0, b3]
]);
selectedState = rslt[i]["State"];
document.getElementById("city").disabled=false;
document.getElementById("city").innerHTML="<option value='' selected disabled>Select a city</option>";
getcsv('https://rapid-wave-c8e3.redfor14314.workers.dev/https://raw.githubusercontent.com/Fuelish/FuelishCLI/main/assets/'+rslt[i]["State"]+'.csv')
.then(data=>
{
datac=[];
clent=data[1];
datac=data[0];
for(let x=0;x<clent-2;x++)
if(mode==1 && datac[x]["City"]==gcity)
{
document.getElementById("city").innerHTML+="<option selected value='"+datac[x]["City"]+"'>"+datac[x]["City"]+"</option>";
}
else
{document.getElementById("city").innerHTML+="<option value='"+datac[x]["City"]+"'>"+datac[x]["City"]+"</option>";}
if(mode==1)
{
cfunc(1);
}
});
break;
}
}
map.invalidateSize();
}
document.getElementById('state').addEventListener('change',func);
document.getElementById('city').addEventListener('change', function(){cfunc(0);});
document.getElementById('animation').addEventListener('click',bharat);
priceBox.addEventListener("animationend", function() {
map.invalidateSize();
map.fitBounds([
[b1, b2],
[b0, b3]
]);
});