-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
163 lines (147 loc) · 5.54 KB
/
app.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
class DigLibApp {
constructor() {
this.featuredDiv = document.querySelector('.featured');
//this.itemDiv = document.querySelector('.item');
this.timelineDiv = document.querySelector('.timeline');
this.init();
}
async init() {
if ('IntersectionObserver' in window) {
this.setupNavIntersectionObserver();
}
this.addLoadingIndicatorDelay();
//check for filename; run featured and timeline functions based on HTML page
if (fileName === 'browse.html' || fileName === 'index.html' || !fileName || fileName.trim === '') {
await this.loadFeatured();
await this.loadTimeline();
}
//await this.loadItem();
}
addLoadingIndicatorDelay() {
//only show spinner if we're delayed more than 1s
setTimeout(() => {
Array.from(document.querySelectorAll('.loader')).forEach(loader => {
loader.removeAttribute('hidden');
});
}, 1000);
}
setupNavIntersectionObserver() {
const nav = document.querySelector('nav');
const header = document.querySelector('header');
const callback = entries => {
entries.forEach(entry => {
if (entry.isIntersecting) {
[nav, header].forEach(e => e.classList.remove('fixed'));
} else {
[nav, header].forEach(e => e.classList.add('fixed'));
}
});
};
const observer = new IntersectionObserver(callback, {
threshold: [0, 1]
});
observer.observe(header);
}
async loadFeatured() {
this.featured = await this.fetchJSON('./items.json');
//check for filename; run unique featured functions based on HTML page
if (fileName === 'index.html' || !fileName || fileName.trim === '') {
this.featuredDiv.innerHTML = this.featured.sort(() => Math.random() - 0.5).slice(0,4).sort((a, b) => parseFloat(a.item.originInfo_dateIssued) - parseFloat(b.item.originInfo_dateIssued)).map(this.toFeatureBlock).join('\n');
} else {
this.featuredDiv.innerHTML = this.featured.sort((a, b) => parseFloat(a.item.originInfo_dateIssued) - parseFloat(b.item.originInfo_dateIssued)).map(this.toFeatureBlock).join('\n');
}
}
async loadTimeline() {
const rawTimeline = await this.fetchJSON('./items.json');
this.timeline = rawTimeline.map(this.addObjectDetails, this);
//check for filename; run unique timeline functions based on HTML page
if (fileName === 'index.html' || !fileName || fileName.trim === '') {
this.timelineDiv.innerHTML = this.timeline.sort(() => Math.random() - 0.5).slice(0,4).sort((a, b) => parseFloat(a.item.originInfo_dateIssued) - parseFloat(b.item.originInfo_dateIssued)).map(this.toTimelineBlock).join('\n');
} else {
this.timelineDiv.innerHTML = this.timeline.sort((a, b) => parseFloat(a.item.originInfo_dateIssued) - parseFloat(b.item.originInfo_dateIssued)).map(this.toTimelineBlock).join('\n');
}
}
/*
async loadItem(id) {
const id = getQueryVariable('id');
const rawItems = await this.fetchJSON('./items.json');
this.item = rawItems.map(this.addObjectDetails, this);
this.itemDiv.innerHTML = this.item
.map(this.toItemBlock)
.join('\n');
}
*/
toFeatureBlock(items) {
return `
<div class="feature-item">
<a href="./item.html?id=${items.item.recordInfo_recordIdentifier}">
<img src="${items.item.identifier}" width="160" height="120" loading="lazy" alt="${items.item.titleInfo_title}">
<div>${items.item.originInfo_dateIssued}</div>
</a>
</div>`;
}
/*
toItemBlock(items) {
return `
<h2>Item: ${items.item.titleInfo_title}</h2>
<li>
<img alt="${items.item.titleInfo_title}" src="${items.item.identifier}" />
<p>${items.item.titleInfo_title}</p><p>${items.item.abstract}</p>
<p>${items.item.extension}</p>
<p>${items.item.originInfo_dateIssued}</p>
</li>`;
}
*/
toTimelineBlock(items) {
return `
<div class="timeline-item ${items.item.genre}">
<div class="title-and-date">
<div class="date">${items.item.originInfo_dateIssued}</div>
<div class="title-and-creator">
<div class="title">${items.item.titleInfo_title}</div>
<div class="creator">${
items.item.record ? item.record.item.titleInfo_title : ' '
}</div>
</div>
</div>
<p class="description">${items.item.abstract}</p>
</div>
`;
}
addObjectDetails(item) {
if (item.recordInfo_recordIdentifier) {
return Object.assign({}, item, {
record: this.objects.find(s => s.id === item.recordInfo_recordIdentifier)
});
}
return Object.assign({}, item);
}
async fetchJSON(url) {
const response = await fetch(url);
if (!response.ok) {
throw new Error(response.status); // 404
}
return response.json();
}
}
//get siteUrl and filename; add to global scope
//current use: run featured/timeline functions based on HTML page
const url = new URL(window.location);
const siteUrl = `${url.protocol}//${url.host}${url.pathname.substring(0,url.pathname.lastIndexOf("/")+1)}`;
const fileName = url.pathname.substring(url.pathname.lastIndexOf('/')+1);
window.addEventListener('load', e => {
new DigLibApp();
registerSW();
});
async function registerSW() {
if ('serviceWorker' in navigator) {
try {
await navigator.serviceWorker.register('./serviceworker.js');
console.log('ServiceWorker registration succeeded.');
} catch (e) {
console.log('ServiceWorker registration failed.');
}
} else {
document.querySelector('.alert').removeAttribute('hidden');
}
}