-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplatser.js
130 lines (99 loc) · 3.14 KB
/
platser.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
function getNewListWithAjax(awesome, input) {
console.log('inside getNewListWithAjax');
let ajax = new XMLHttpRequest();
ajax.open("GET", "https://www.smhi.se/wpt-a/backend_tendayforecast_nextgen/geo/autocomplete/places/" + input.value, true);
ajax.onload = function () {
let list = buildListFromLocations(JSON.parse(ajax.responseText));
// Update awesome with new list
awesome.list = list;
awesome.evaluate();
};
ajax.send();
}
function buildListFromLocations(locations) {
let list = [];
Object.values(locations).forEach(function (location) {
// Set munincipality as default region
let region = location.municipality;
// If no municipality exists for this place, set country as region
if (region === undefined) {
region = location.country;
}
// Create a place label
let fullplace = location.place;
// Only add region if different
if (location.place != region) {
fullplace += ', ' + region;
}
// Add lat/lon to data object
data = {
"name": fullplace,
"geoname": location.place,
"lat": location.lat,
"lon": location.lon
}
// Add element to list
list.push({
label: fullplace,
value: data
});
});
return list;
}
function comparePlaces(a, b) {
if (a > b) return 1;
if (b > a) return -1;
return 0;
}
/*
*
* Create and configure the autocomplete input-box
*
*/
try {
let myInput = document.querySelector("#plats-search input");
let myOptions = {
"minChars": 1,
"autoFirst": true,
"filter": Awesomplete.FILTER_STARTSWITH
};
let myAwesome = new Awesomplete(myInput, myOptions);
myInput.addEventListener('input', function () {
getNewListWithAjax(myAwesome, myInput);
});
myInput.addEventListener('awesomplete-select', function (obj) {
console.log("obj", obj.text);
// Create a new cookie with new selected location data as first object
addPlace(obj.text.value);
// change value to label (otherwise data-Object is displayed in input box)
obj.text.value = obj.text.label
//alert("Wait");
// Go back to Weather-page
location.assign("index.html");
});
console.log("Done");
} catch (err) {
console.log(err);
// Clear cookie if that is the problem
deletePlaces();
}
function displayRecentSearches() {
let recentSearchesDiv = document.getElementById("recent-searches");
let places = getPlaces();
if (places.length > 0) {
let recentSearchesHTML = "";
places.forEach(place => {
recentSearchesHTML += `<div class="recent-search-item" onclick="selectRecentPlace('${place.name}', '${place.lat}', '${place.lon}')">${place.name}</div>`;
});
recentSearchesDiv.innerHTML = recentSearchesHTML;
}
}
function selectRecentPlace(name, lat, lon) {
let selectedPlace = {
name: name,
lat: lat,
lon: lon
};
addPlace(selectedPlace);
location.assign("index.html");
}