Repository navigation
Expand file tree
/
Copy pathworks.js
More file actions
262 lines (228 loc) · 8.91 KB
/
Copy pathworks.js
File metadata and controls
262 lines (228 loc) · 8.91 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
const works = window.ALEKSI_WORKS || [];
let exhibitionInteractionCleanup = null;
function escapeHtml(value) {
return String(value || '').replace(/[&<>"']/g, (character) => ({
'&': '&',
'<': '<',
'>': '>',
'"': '"',
"'": '''
}[character]));
}
function safeLocalHref(value, fallback = './works.html') {
const candidate = String(value || '').trim();
if (
!candidate
|| candidate.startsWith('//')
|| /^[a-z][a-z\d+.-]*:/i.test(candidate)
|| /[\u0000-\u001f\u007f\\]/.test(candidate)
) {
return fallback;
}
if (/^(?:\.\/|\/(?!\/)|\?|#)/.test(candidate)) return candidate;
try {
const normalized = new URL(candidate, 'http://local.invalid/');
const normalizedRelative = `${normalized.pathname.slice(1)}${normalized.search}${normalized.hash}`;
return normalized.origin === 'http://local.invalid' && normalizedRelative === candidate
? candidate
: fallback;
} catch {
return fallback;
}
}
function renderNavigation() {
const nav = document.querySelector('.desktop-nav');
const items = window.ALEKSI_SITE?.navigation || (window.ALEKSI_CONTENT && window.ALEKSI_CONTENT.nav);
if (!nav || !Array.isArray(items)) return;
const current = window.location.pathname.split('/').pop() || 'works.html';
nav.innerHTML = items.map((item) => {
const href = item.href || './index.html';
const label = escapeHtml(item.label || '');
const file = href.replace('./', '').split('#')[0].split('?')[0] || 'index.html';
const active = file === current;
return `<a class="nav-link${active ? ' is-active' : ''}" href="${href}"${active ? ' aria-current="page"' : ''}>${label}</a>`;
}).join('');
}
function workTitleDisplay(work) {
return work.titleDisplay || work.shortTitle || work.title || '';
}
function desktopSlot(index, count, width) {
const columns = Math.min(7, Math.max(4, Math.floor(width / 190)));
const rows = Math.ceil(count / columns);
const column = index % columns;
const row = Math.floor(index / columns);
const usableWidth = Math.min(width - 80, 1240);
const gapX = columns > 1 ? usableWidth / (columns - 1) : 0;
const x = -usableWidth / 2 + column * gapX;
const rowGap = rows > 1 ? 270 : 0;
const y = (row - (rows - 1) / 2) * rowGap + (column % 2 ? -24 : 18);
const rotationPattern = [-6, 4, -3, 5, -5, 3, -2];
return { x, y, rotate: rotationPattern[index % rotationPattern.length] };
}
function renderExhibitionWorks(items = works) {
const stage = document.querySelector('[data-exhibition-stage]');
if (!stage) return;
stage.innerHTML = items.map((work, index) => {
const title = workTitleDisplay(work);
const summary = work.cardSummary || work.summary || work.intro || '';
const source = work.source || '来源待复核';
const image = work.thumb;
const ctaLabel = '进入作品';
const ctaHref = safeLocalHref(work.detailUrl || work.href, './works.html');
const loading = index < 2 ? 'eager' : 'lazy';
return `
<article
class="exhibition-card"
data-work-card
data-id="${escapeHtml(work.slug)}"
>
<div class="exhibition-card__media">
<img
class="exhibition-card__img"
src="${escapeHtml(image)}"
alt="${escapeHtml(work.alt || work.title)}"
loading="${loading}"
decoding="async"
>
</div>
<div class="exhibition-card__body">
<p class="exhibition-card__source">${escapeHtml(source)}</p>
<h2 class="exhibition-card__title">${escapeHtml(title)}</h2>
<p class="exhibition-card__summary">${escapeHtml(summary)}</p>
<div class="exhibition-card__actions">
<button type="button" class="exhibition-card__toggle" aria-expanded="false" aria-label="聚焦作品:${escapeHtml(title)}">聚焦</button>
<a class="exhibition-card__cta" href="${escapeHtml(ctaHref)}">${ctaLabel}</a>
</div>
</div>
</article>
`;
}).join('');
bindImageFallbacks(stage);
bindExhibitionInteractions(stage);
positionDesktopCards(stage);
}
function bindImageFallbacks(stage) {
stage.querySelectorAll('.exhibition-card__img').forEach((image) => {
image.addEventListener('error', () => {
image.closest('[data-work-card]')?.classList.add('has-image-fallback');
image.removeAttribute('src');
}, { once: true });
});
}
function positionDesktopCards(stage) {
const cards = Array.from(stage.querySelectorAll('[data-work-card]'));
const isDesktop = window.matchMedia('(min-width: 1180px)').matches;
stage.style.removeProperty('--stage-height');
cards.forEach((card, index) => {
if (!isDesktop) {
card.style.removeProperty('--x');
card.style.removeProperty('--y');
card.style.removeProperty('--rotate');
card.style.removeProperty('--stack');
return;
}
const slot = desktopSlot(index, cards.length, stage.clientWidth);
card.style.setProperty('--x', `${slot.x}px`);
card.style.setProperty('--y', `${slot.y}px`);
card.style.setProperty('--rotate', `${slot.rotate}deg`);
card.style.setProperty('--stack', `${cards.length - index}`);
});
if (isDesktop) {
const columns = Math.min(7, Math.max(4, Math.floor(stage.clientWidth / 190)));
const rows = Math.ceil(cards.length / columns);
stage.style.setProperty('--stage-height', `${rows * 270 + 180}px`);
}
}
function applySelection(stage, activeId) {
const cards = Array.from(stage.querySelectorAll('[data-work-card]'));
stage.classList.toggle('has-active-card', Boolean(activeId));
cards.forEach((card) => {
const active = card.dataset.id === activeId;
const muted = Boolean(activeId) && !active;
const toggle = card.querySelector('.exhibition-card__toggle');
const title = card.querySelector('.exhibition-card__title')?.textContent.trim() || '';
card.classList.toggle('is-active', active);
card.classList.toggle('is-muted', muted);
if (toggle) {
toggle.setAttribute('aria-expanded', String(active));
toggle.setAttribute('aria-label', `${active ? '取消聚焦' : '聚焦作品'}:${title}`);
toggle.textContent = active ? '取消聚焦' : '聚焦';
}
});
}
function bindExhibitionInteractions(stage) {
if (exhibitionInteractionCleanup) exhibitionInteractionCleanup();
let activeId = null;
let resizeTimer = 0;
let observer = null;
let cleanedUp = false;
const toggleCard = (card) => {
activeId = activeId === card.dataset.id ? null : card.dataset.id;
applySelection(stage, activeId);
};
const clearSelection = () => {
if (!activeId) return;
activeId = null;
applySelection(stage, activeId);
};
const handleStageClick = (event) => {
if (event.target === stage) {
clearSelection();
return;
}
const card = event.target.closest('[data-work-card]');
if (!card) return;
if (event.target.closest('.exhibition-card__toggle')) {
toggleCard(card);
return;
}
if (event.target.closest('a, button')) return;
toggleCard(card);
};
const handleDocumentKeydown = (event) => {
if (event.key === 'Escape') clearSelection();
};
const schedulePositionUpdate = () => {
window.clearTimeout(resizeTimer);
resizeTimer = window.setTimeout(() => positionDesktopCards(stage), 80);
};
const cleanup = () => {
if (cleanedUp) return;
cleanedUp = true;
window.clearTimeout(resizeTimer);
stage.removeEventListener('click', handleStageClick);
document.removeEventListener('keydown', handleDocumentKeydown);
window.removeEventListener('resize', schedulePositionUpdate);
window.removeEventListener('pagehide', cleanup);
if (observer) observer.disconnect();
if (exhibitionInteractionCleanup === cleanup) exhibitionInteractionCleanup = null;
};
stage.addEventListener('click', handleStageClick);
document.addEventListener('keydown', handleDocumentKeydown);
window.addEventListener('pagehide', cleanup);
if ('ResizeObserver' in window) {
observer = new ResizeObserver(schedulePositionUpdate);
observer.observe(stage);
} else {
window.addEventListener('resize', schedulePositionUpdate);
}
exhibitionInteractionCleanup = cleanup;
return cleanup;
}
function renderIndex() {
const index = document.querySelector('[data-works-index]');
if (!index) return;
index.innerHTML = works.map((work, itemIndex) => `
<a class="index-row work-index-row" href="${escapeHtml(safeLocalHref(work.detailUrl || work.href, './works.html'))}" data-reveal>
<span class="index-row-kicker">${String(itemIndex + 1).padStart(2, '0')} / ${escapeHtml(work.date)}</span>
<strong class="index-row-title">${escapeHtml(work.title)}</strong>
<p class="index-row-copy">${escapeHtml(work.source || '来源待复核')} / ${escapeHtml(work.medium || work.category)}</p>
<em class="row-action">进入作品 →</em>
</a>
`).join('');
}
document.addEventListener('DOMContentLoaded', () => {
renderNavigation();
renderExhibitionWorks();
renderIndex();
});