-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
343 lines (296 loc) · 11.9 KB
/
script.js
File metadata and controls
343 lines (296 loc) · 11.9 KB
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
(function(){
const btn = document.getElementById('menuButton');
const drawer = document.getElementById('drawer');
const closeBtn = document.getElementById('drawerClose');
const scrim = document.getElementById('scrim');
const focusableSelector = 'a[href], button:not([disabled]), [tabindex]:not([tabindex="-1"])';
let lastFocus = null;
function openDrawer(){
lastFocus = document.activeElement;
drawer.classList.add('open');
scrim.classList.add('open');
drawer.setAttribute('aria-hidden', 'false');
btn.setAttribute('aria-expanded', 'true');
const firstLink = drawer.querySelector('.drawer-link');
if (firstLink) firstLink.focus();
document.body.style.overflow = 'hidden';
}
function closeDrawer(){
drawer.classList.remove('open');
scrim.classList.remove('open');
drawer.setAttribute('aria-hidden', 'true');
btn.setAttribute('aria-expanded', 'false');
document.body.style.overflow = '';
if (lastFocus && typeof lastFocus.focus === 'function') lastFocus.focus();
}
btn.addEventListener('click', () => {
const expanded = btn.getAttribute('aria-expanded') === 'true';
expanded ? closeDrawer() : openDrawer();
});
if (closeBtn) closeBtn.addEventListener('click', closeDrawer);
if (scrim) scrim.addEventListener('click', closeDrawer);
// Close on ESC
document.addEventListener('keydown', (e) => {
if (e.key === 'Escape' && drawer.classList.contains('open')) closeDrawer();
});
// Close after clicking a link
drawer.querySelectorAll('a').forEach(a => a.addEventListener('click', closeDrawer));
// Focus trap
document.addEventListener('keydown', (e) => {
if (!drawer.classList.contains('open')) return;
if (e.key !== 'Tab') return;
const focusables = drawer.querySelectorAll(focusableSelector);
if (!focusables.length) return;
const first = focusables[0];
const last = focusables[focusables.length - 1];
if (e.shiftKey && document.activeElement === first) {
e.preventDefault();
last.focus();
} else if (!e.shiftKey && document.activeElement === last) {
e.preventDefault();
first.focus();
}
});
// Footer year
const y = document.getElementById('year');
if (y) y.textContent = String(new Date().getFullYear());
})();
/* ===== Scrolling rails + accordions ===== */
(function(){
function initRail(opts){
const rail = document.querySelector(opts.railSel);
const prev = document.querySelector(opts.prevSel);
const next = document.querySelector(opts.nextSel);
if (!rail) return;
function step(){
const card = rail.querySelector(opts.cardSel);
if (!card) return 260;
const gap = opts.gap ?? 12;
return card.getBoundingClientRect().width + gap;
}
function update(){
if (!prev || !next) return;
const max = rail.scrollWidth - rail.clientWidth;
const x = rail.scrollLeft;
prev.disabled = x <= 2;
next.disabled = x >= max - 2;
}
function scrollByDir(dir){
rail.scrollBy({ left: dir * step(), behavior: "smooth" });
}
if (prev) prev.addEventListener("click", () => scrollByDir(-1));
if (next) next.addEventListener("click", () => scrollByDir(1));
rail.addEventListener("scroll", update, { passive: true });
window.addEventListener("resize", update);
update();
// Desktop nicety: wheel vertical scroll moves the rail horizontally
rail.addEventListener("wheel", (e) => {
if (!e.shiftKey && Math.abs(e.deltaY) > Math.abs(e.deltaX)) {
rail.scrollLeft += e.deltaY;
e.preventDefault();
}
}, { passive: false });
}
// Project cards rail
initRail({
railSel: "[data-wg-rail]",
prevSel: "[data-wg-scroll='prev']",
nextSel: "[data-wg-scroll='next']",
cardSel: ".wg-project-card",
gap: 12
});
// Tool/Community/Platform rail
initRail({
railSel: "[data-tcp-rail]",
prevSel: "[data-tcp-scroll='prev']",
nextSel: "[data-tcp-scroll='next']",
cardSel: ".tcp-card",
gap: 12
});
// Attributions rail
initRail({
railSel: "[data-attr-rail]",
prevSel: "[data-attr-scroll='prev']",
nextSel: "[data-attr-scroll='next']",
cardSel: ".attr-card",
gap: 12
});
// Accordion behavior for project cards: only one <details> open at a time
const detailsEls = Array.from(document.querySelectorAll(".wg-project-card__details"));
detailsEls.forEach((d) => {
d.addEventListener("toggle", () => {
if (!d.open) return;
detailsEls.forEach((other) => {
if (other !== d) other.open = false;
});
});
});
})();
/* ===== Shared Calendar (event cards) ===== */
(function(){
const rail = document.querySelector('[data-cal-rail]');
if(!rail) return;
// Enable rail arrows (reuse initRail pattern)
(function initRail(){
const prev = document.querySelector('[data-cal-scroll="prev"]');
const next = document.querySelector('[data-cal-scroll="next"]');
const cardSel = '.wg-project-card';
const gap = 12;
function step(){
const card = rail.querySelector(cardSel);
if (!card) return 260;
return card.getBoundingClientRect().width + gap;
}
function update(){
if(!prev || !next) return;
const max = rail.scrollWidth - rail.clientWidth;
const x = rail.scrollLeft;
prev.disabled = x <= 2;
next.disabled = x >= max - 2;
}
window.__calRailUpdate = update;
function scrollByDir(dir){
rail.scrollBy({ left: dir * step(), behavior: 'smooth' });
// refresh after layout settles
window.setTimeout(update, 250);
}
if(prev) prev.addEventListener('click', ()=> scrollByDir(-1));
if(next) next.addEventListener('click', ()=> scrollByDir(1));
rail.addEventListener('scroll', update, { passive:true });
window.addEventListener('resize', update);
update();
rail.addEventListener('wheel', (e)=>{
if(!e.shiftKey && Math.abs(e.deltaY) > Math.abs(e.deltaX)){
rail.scrollLeft += e.deltaY;
e.preventDefault();
}
}, { passive:false });
})();
const sourceChips = document.getElementById('calSourceChips');
const tagChips = document.getElementById('calTagChips');
const meta = document.getElementById('calMeta');
const SOURCE = {
GOAT: { label:'GOAT', icon:'https://www.google.com/s2/favicons?domain=goatech.org&sz=64' },
GIAA: { label:'GIAA', icon:'https://www.google.com/s2/favicons?domain=gia-agroecology.org&sz=64' },
AKC: { label:'Ag Knowledge Concordance', icon:'https://www.google.com/s2/favicons?domain=agricultural-knowledge-concordance.github.io&sz=64' },
FARMHACK: { label:'Farm Hack', icon:'https://www.google.com/s2/favicons?domain=farmhack.org&sz=64' }
};
const state = { tag:'' };
let data = [];
function escapeHtml(s){
return (s ?? '').toString()
.replaceAll('&','&')
.replaceAll('<','<')
.replaceAll('>','>')
.replaceAll('"','"')
.replaceAll("'",''');
}
function fmtRange(startIso, endIso){
const start = new Date(startIso);
const end = new Date(endIso || startIso);
if (Number.isNaN(start.getTime())) return '';
const optsDate = { weekday:'short', month:'short', day:'numeric', year:'numeric' };
const optsTime = { hour:'2-digit', minute:'2-digit' };
const sameDay = start.toDateString() === end.toDateString();
const d = start.toLocaleDateString(undefined, optsDate);
const t1 = start.toLocaleTimeString(undefined, optsTime);
const t2 = endIso ? end.toLocaleTimeString(undefined, optsTime) : '';
return sameDay ? `${d} • ${t1}${t2 ? '–' + t2 : ''}` : `${d} • ${t1} → ${end.toLocaleDateString(undefined, optsDate)} • ${t2}`;
}
function monthKey(iso){
const d = new Date(iso);
return d.toLocaleString(undefined, { month:'short', year:'numeric' });
}
function makeChip(label, onClick){
const b = document.createElement('button');
b.className = 'cal-chip';
b.type = 'button';
b.textContent = label;
b.setAttribute('aria-pressed','false');
b.addEventListener('click', onClick);
return b;
}
function updateChips(){
document.querySelectorAll('.cal-chip').forEach(ch => {
const txt = ch.textContent || '';
let pressed = false;
if (txt === 'All tags') pressed = state.tag === '';
for (const k of Object.keys(SOURCE)){
}
if (state.tag && txt === state.tag) pressed = true;
ch.setAttribute('aria-pressed', pressed ? 'true' : 'false');
});
}
function matches(ev){
if (state.tag && !(ev.tags || []).includes(state.tag)) return false;
const now = Date.now();
const end = new Date(ev.end || ev.start).getTime();
return end >= now - 365*24*3600*1000; // show ~last year + upcoming
}
function card(ev){
const src = SOURCE[ev.source] || { label: ev.source || 'Event', icon:'' };
const tagList = (ev.tags || []).filter(t => t !== ev.source).slice(0,3);
const tagPills = tagList.map(t => `<span class="ec-pill">${escapeHtml(t)}</span>`).join('');
const startDate = new Date(ev.start);
const endDate = ev.end ? new Date(ev.end) : null;
const isMultiDay = endDate && startDate.toDateString() !== endDate.toDateString();
const dateFmt = { weekday:'short', month:'short', day:'numeric', year:'numeric' };
const startStr = Number.isNaN(startDate.getTime()) ? '' : startDate.toLocaleDateString(undefined, dateFmt);
const endStr = isMultiDay ? endDate.toLocaleDateString(undefined, dateFmt) : '';
const dateStr = isMultiDay ? `${startStr} → ${endStr}` : startStr;
const where = ev.location ? ` • ${ev.location}` : '';
return `
<article class="ec">
<div class="ec__inner">
${src.icon ? `<img class="ec__icon" src="${src.icon}" alt="${escapeHtml(src.label)} logo" loading="lazy" />` : '<div class="ec__icon ec__icon--empty"></div>'}
<div class="ec__body">
<h4 class="ec__title">${escapeHtml(ev.title || 'Untitled event')}</h4>
<p class="ec__when">${escapeHtml(dateStr)}${escapeHtml(where)}</p>
<div class="ec__pills">
<span class="ec-pill ec-pill--src">${escapeHtml(src.label)}</span>
${tagPills}
</div>
</div>
</div>
<details class="ec__details">
<summary class="ec__summary"><span>Details</span><span class="ec__chev" aria-hidden="true">▾</span></summary>
<div class="ec__expand">
${ev.description ? `<p class="ec__desc">${escapeHtml(ev.description)}</p>` : ''}
<div class="ec__links">
${ev.url ? `<a class="ec__link" href="${escapeHtml(ev.url)}" target="_blank" rel="noopener">Event link ↗</a>` : ''}
<a class="ec__link" href="calendar.html">Month view ↗</a>
</div>
</div>
</details>
</article>`;
}
function render(){
updateChips();
const list = data.filter(matches).sort((a,b)=> new Date(a.start).getTime() - new Date(b.start).getTime());
meta.textContent = `${list.length} upcoming events shown` + (state.tag ? ` • ${state.tag}` : '');
// For this homepage rail, show the next ~18 events (still scrollable)
const subset = list.slice(0, 18);
rail.innerHTML = subset.map(card).join('') || `<div class="muted" style="padding: 10px 6px;">No upcoming events match your filters.</div>`;
if (window.__calRailUpdate) window.__calRailUpdate();
// No expand/collapse in new card design
}
function buildFilters(){
const tags = [...new Set(data.flatMap(e=>e.tags || []))].sort();
tagChips.innerHTML = '';
tagChips.appendChild(makeChip('All tags', ()=>{ state.tag = ''; render(); }));
tags.slice(0, 10).forEach(t => {
tagChips.appendChild(makeChip(t, ()=>{ state.tag = (state.tag===t) ? '' : t; render(); }));
});
}
fetch('data/events.json', { cache:'no-store' })
.then(r => r.ok ? r.json() : Promise.reject(new Error('events.json not found')))
.then(json => {
data = Array.isArray(json) ? json : (json.items || []);
buildFilters();
render();
})
.catch(err => {
console.error(err);
if (meta) meta.textContent = 'Events feed could not be loaded yet.';
});
})();