-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathwikidaheim.js
179 lines (163 loc) · 4.59 KB
/
wikidaheim.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
import {
WIKIDAHEIM_ENDPOINT /*, WIKIDAHEIM_FEEDBACK_ENDPOINT*/,
} from "/src/config";
import boundaries from "../config/boundaries_mapped.json";
const listCategories = () => {
return fetch(
`${WIKIDAHEIM_ENDPOINT}?format=json&action=query&type=structure`,
{
method: "get",
}
)
.then((res) => {
if (!res.ok) throw Error(res.statusText);
return res.json();
})
.then((json) => {
return json;
});
};
const getTownData = (location, categories, getWikiData) => {
const categoriesString = categories.join("|");
let locationQuery = "";
if (location.wikidata) {
locationQuery = `wikidata=${location.wikidata}`;
} else if (location.iso && !location.name.includes("Wien")) {
locationQuery = `gemeindekennzahl=${location.iso}`;
} else {
locationQuery = `latitude=${location.latitude}&longitude=${location.longitude}`;
}
return fetch(
`${WIKIDAHEIM_ENDPOINT}?format=json&action=query&type=data&${locationQuery}&categories=${categoriesString}&wiki=${+getWikiData}`,
{
method: "get",
}
)
.then((res) => {
if (!res.ok) throw Error(res.statusText);
return res.json();
})
.then((json) => {
const res = json;
const mod = {
selectedCats: categories,
};
// check if there were categories returned. if yes fix an API bug
if ("categories" in json) {
// check if the deep-nesting bug exists
if (json.categories.length === 1 && Array.isArray(json.categories[0])) {
mod.categories = json.categories[0];
}
}
return Object.assign({}, res, mod);
});
};
/*
const getFeedbackFormToken = () => {
return fetch(`${WIKIDAHEIM_FEEDBACK_ENDPOINT}?token=new`, {
method: "get",
})
.then((res) => {
if (!res.ok) throw Error(res.statusText);
return res.json();
})
.then((json) => {
return json.token;
});
};
const submitFeedbackForm = (token, subject, message) => {
const formData = new FormData();
formData.append("token", token);
formData.append("subject", subject);
formData.append("message", message);
return fetch(`${WIKIDAHEIM_FEEDBACK_ENDPOINT}`, {
method: "post",
body: formData,
})
.then((res) => {
if (!res.ok) throw Error(res.statusText);
return res.json();
})
.then((json) => {
return json;
});
};
*/
const search = (query, lang, maxResults = 10) => {
// helper function to make string lower case,
// replace "sankt" with "st." and remove umlauts
const normalizeQueryString = (name) =>
name.toLowerCase().replace("sankt", "st.");
const normalizedQuery = normalizeQueryString(query);
const filtered = boundaries.filter((town) => {
if (normalizedQuery.toLowerCase() === "wien") {
return town.unit_code.slice(0, 1) == 9;
} else {
return normalizeQueryString(town.name).includes(normalizedQuery);
}
});
const mapped = filtered
// remove duplicates from mapbox (due to multiple boundaries per town from municipality fusion)
.filter(
(town, index) =>
filtered.findIndex((e) => e.unit_code === town.unit_code) === index
)
// sort by name - and move matches staring with query to top
.sort((a, b) => {
const aName = normalizeQueryString(a.name);
const bName = normalizeQueryString(b.name);
if (
aName.startsWith(normalizedQuery) &&
!bName.startsWith(normalizedQuery)
)
return -1;
if (
!aName.startsWith(normalizedQuery) &&
bName.startsWith(normalizedQuery)
)
return 1;
if (aName < bName) return -1;
if (aName > bName) return 1;
return 0;
})
// slice to max results
.slice(0, maxResults)
// map to match legacy mapbox format
.map((town) => ({
id: "place." + town.unit_code,
properties: {
wikidata: town.wikidata,
},
text: town.name,
place_name: town.name,
geometry: {
type: "Point",
coordinates: town.centroid,
},
context: [
{
id: "region." + String(town.unit_code).slice(0, 1),
text: {
1: "Burgenland",
2: "Kärnten",
3: "Niederösterreich",
4: "Oberösterreich",
5: "Salzburg",
6: "Steiermark",
7: "Tirol",
8: "Vorarlberg",
9: "Wien",
}[String(town.unit_code).slice(0, 1)],
},
],
}));
return Promise.resolve(mapped);
};
const wikidaheimAPI = {
listCategories,
getTownData,
//getFeedbackFormToken,
//submitFeedbackForm,
search,
};
export default wikidaheimAPI;