-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscripttemp.js
237 lines (200 loc) · 9.99 KB
/
scripttemp.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
<script>
let map;
let markersLayer;
document.addEventListener('DOMContentLoaded', () => {
initializeMap();
const searchButton = document.getElementById('searchButton');
const searchInput = document.getElementById('searchInput');
searchButton.addEventListener('click', () => {
const query = searchInput.value.trim();
if (query) {
populateItems(query);
}
});
searchInput.addEventListener('keydown', (event) => {
if (event.key === 'Enter') {
event.preventDefault();
searchButton.click();
}
});
document.getElementById('nextButton').addEventListener('click', showNextValue);
document.getElementById('prevButton').addEventListener('click', showPreviousValue);
document.addEventListener('keydown', (event) => {
if (event.key === 'ArrowDown' || event.key === 'ArrowRight') {
navigateItems('next');
} else if (event.key === 'ArrowUp' || event.key === 'ArrowLeft') {
navigateItems('previous');
}
});
document.querySelectorAll('.component-header-top select').forEach(select => {
select.addEventListener('change', (event) => {
const selectedView = event.target.value;
const component = event.target.closest('.component');
if (selectedView === 'Close') {
component.remove();
} else {
replaceComponentContent(component, selectedView);
}
});
});
// Add event listener for help button
document.getElementById('showHelpButton').addEventListener('click', () => {
const helpSection = document.getElementById('helpSection');
if (helpSection.style.display === 'none') {
helpSection.style.display = 'block';
document.getElementById('showHelpButton').textContent = 'Hide Help';
} else {
helpSection.style.display = 'none';
document.getElementById('showHelpButton').textContent = 'Show Help';
}
});
});
const SPREADSHEET_ID = 'your_spreadsheet_id'; // Update with your actual spreadsheet ID
const RANGE = 'Dataset!D2:D'; // Adjust range if necessary
async function loadSheetData() {
const url = `/api/sheet-data?spreadsheetId=${SPREADSHEET_ID}&range=${RANGE}`;
try {
const response = await fetch(url);
const data = await response.json();
values = data.values ? data.values.map(row => row[0]) : [];
currentIndex = 0;
displayCurrentValue();
} catch (error) {
console.error('Error loading sheet data:', error);
}
}
function displayCurrentValue() {
const searchInput = document.getElementById('searchInput');
if (values.length > 0) {
searchInput.value = values[currentIndex];
} else {
searchInput.value = 'No data available';
}
}
function showNextValue() {
if (values.length > 0) {
currentIndex = (currentIndex + 1) % values.length;
displayCurrentValue();
}
}
function showPreviousValue() {
if (values.length > 0) {
currentIndex = (currentIndex - 1 + values.length) % values.length;
displayCurrentValue();
}
}
function initializeMap() {
map = L.map('map').setView([51.505, -0.09], 13);
L.tileLayer('https://{s}.basemaps.cartocdn.com/light_all/{z}/{x}/{y}{r}.png', {
attribution: '© <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors © <a href="https://carto.com/attributions">CARTO</a>',
}).addTo(map);
// Use L.featureGroup for markers to enable getBounds()
markersLayer = L.featureGroup().addTo(map);
}
async function fetchWikidataItems(query) {
try {
const sparqlQuery = `
SELECT ?item ?itemLabel ?itemDescription (SAMPLE(?image) AS ?image) (SAMPLE(?coord) AS ?coord) WHERE {
SERVICE wikibase:mwapi {
bd:serviceParam wikibase:endpoint "www.wikidata.org";
wikibase:api "EntitySearch";
mwapi:search "${query}";
mwapi:language "en";
mwapi:limit "8".
?item wikibase:apiOutputItem mwapi:item.
?item wikibase:apiOutputItemLabel mwapi:label.
}
OPTIONAL { ?item wdt:P18 ?image. }
OPTIONAL { ?item wdt:P625 ?coord. }
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
} GROUP BY ?item ?itemLabel ?itemDescription LIMIT 8`;
const url = `https://query.wikidata.org/sparql?query=${encodeURIComponent(sparqlQuery)}&format=json`;
const response = await fetch(url);
const data = await response.json();
return data.results.bindings;
} catch (error) {
console.error('Error fetching Wikidata items:', error);
return [];
}
}
function createItemElement(item) {
const itemElement = document.createElement('div');
itemElement.className = 'item';
itemElement.addEventListener('click', () => {
document.querySelectorAll('.item').forEach(el => el.classList.remove('selected'));
itemElement.classList.add('selected');
const qid = item.item.value.split('/').pop();
const url = `https://wikidocumentaries-demo.wmcloud.org/${qid}`;
const projectUrl = document.getElementById('projectUrl');
if (projectUrl) {
projectUrl.href = url;
projectUrl.textContent = url;
}
const iframe = document.getElementById('projectIframe');
if (iframe) {
iframe.src = url;
iframe.onload = () => {
// Avoid cross-origin issues; skip iframe content manipulation
};
}
});
const img = document.createElement('img');
img.src = item.image ? item.image.value : 'https://via.placeholder.com/50';
img.alt = 'QID Image';
const details = document.createElement('div');
details.className = 'item-details';
details.innerHTML = `<span>${item.itemLabel.value}</span><br><span>${item.itemDescription ? item.itemDescription.value : ''}</span>`;
itemElement.appendChild(img);
itemElement.appendChild(details);
return itemElement;
}
async function populateItems(query) {
const resultsContainer = document.querySelector('.results');
resultsContainer.innerHTML = '';
const items = await fetchWikidataItems(query);
items.forEach(item => {
const itemElement = createItemElement(item);
resultsContainer.appendChild(itemElement);
});
if (items.length > 0) {
resultsContainer.firstChild.classList.add('selected');
const firstQid = items[0].item.value.split('/').pop();
const url = `https://wikidocumentaries-demo.wmcloud.org/${firstQid}`;
const projectUrl = document.getElementById('projectUrl');
projectUrl.href = url;
projectUrl.textContent = url;
const iframe = document.getElementById('projectIframe');
iframe.src = url;
} else {
const noResultsMessage = document.createElement('div');
noResultsMessage.textContent = 'No results found';
resultsContainer.appendChild(noResultsMessage);
}
}
function replaceComponentContent(component, selectedView) {
const headerBottom = component.querySelector('.component-header-bottom');
const body = component.querySelector('.component-body');
headerBottom.innerHTML = getTemplate(selectedView + 'HeaderBottom');
body.innerHTML = generateComponentBody(selectedView);
}
function navigateItems(direction) {
const items = Array.from(document.querySelectorAll('.item'));
if (items.length === 0) return;
const selectedIndex = items.findIndex(item => item.classList.contains('selected'));
let newIndex;
if (direction === 'next') {
newIndex = (selectedIndex + 1) % items.length;
} else if (direction === 'previous') {
newIndex = (selectedIndex - 1 + items.length) % items.length;
}
items[selectedIndex].classList.remove('selected');
items[newIndex].classList.add('selected');
const qid = items[newIndex].querySelector('.item-details').textContent.split('/').pop();
const url = `https://wikidocumentaries-demo.wmcloud.org/${qid}`;
const projectUrl = document.getElementById('projectUrl');
projectUrl.href = url;
projectUrl.textContent = url;
const iframe = document.getElementById('projectIframe');
iframe.src = url;
}
</script>