-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
209 lines (188 loc) · 6.27 KB
/
main.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
const PROJECT_CLEANUP_DELAY = 3000;
const FLICKITY_DIST = "https://unpkg.com/flickity@2/dist/flickity.pkgd.min.js";
const FLICKITY_CSS_DIST = "https://unpkg.com/flickity@2/dist/flickity.min.css";
const FLICKITY_BG_LAZY_LOAD_DIST = "https://npmcdn.com/[email protected]/bg-lazyload.js";
const FLICKITY_FULLSCREEN_DIST = "https://unpkg.com/flickity-fullscreen@1/fullscreen.js";
const FLICKITY_FULLSCREEN_CSS_DIST = "https://unpkg.com/flickity-fullscreen@1/fullscreen.css";
// TODO vimeo support should be conditional
const VIMEO_DIST = "https://player.vimeo.com/api/player.js";
const imageLoadHandler = (event) => {
const image = event.target;
image.classList.add("loaded");
event.target.removeEventListener("load", imageLoadHandler);
}
// restoring back image src attributes
for (let i = 0; i < document.images.length; i++) {
const image = document.images[i];
const dataSrc = image.getAttribute("data-src");
if (dataSrc) {
image.addEventListener("load", imageLoadHandler);
image.src = image.getAttribute("data-src");
image.removeAttribute("data-src");
}
}
const miniatures = document.getElementById("project-miniatures");
const projects = document.getElementById("projects");
function show(element) {
element.classList.remove("hidden");
element.classList.add("fade-in");
}
const loadJs = (src, extender) => new Promise((resolve, reject) => {
const script = document.createElement("script");
script.onload = () => resolve();
script.onerror = (e) => reject(e);
if (extender) {
extender(script);
}
script.src = src;
document.head.appendChild(script);
});
const loadCss = (src) => new Promise((resolve, reject) => {
const firstStyle = document.head.querySelector("style");
const link = document.createElement("link");
link.onload = () => resolve();
link.onerror = (e) => reject(e);
link.rel = "stylesheet";
link.href = src;
document.head.insertBefore(link, firstStyle);
});
function newPlayer(element) {
element.player = new Vimeo.Player(element, {
id: element.getAttribute("data-video-id"),
responsive: true,
byline: false,
title: false,
color: "ffffff"
});
return element.player;
}
function setVisibility(button, visibility) {
const value = visibility ? "visible" : "hidden";
button.element.style.visibility = value;
}
function initialize() {
const flickityMiniatures = new Flickity(miniatures, {
bgLazyLoad: 2,
freeScroll: true,
pageDots: false,
});
var selectedProject = null;
const projectCleaner = (projectToClean) => {
if (!projectToClean) {
return;
}
setTimeout(() => {
if (projectToClean != selectedProject) {
cleanUpProject(projectToClean);
}
}, PROJECT_CLEANUP_DELAY);
}
const projectExpander = document.createElement("div");
projectExpander.id = "project-expander";
projects.insertAdjacentElement("afterend", projectExpander);
const hashChangeHandler = (e) => {
const hash = location.hash;
if (!hash) return;
const project = projects.querySelector(hash);
if (!project) return;
flickityMiniatures.selectCell("[href='" + hash + "']");
const height = project.clientHeight + "px";
initializeProject(project);
projects.style.height = height;
projectExpander.style.height = height;
projectCleaner(selectedProject);
selectedProject = project;
};
window.addEventListener("hashchange", hashChangeHandler, false);
document.querySelectorAll(".hidden").forEach(element => show(element));
flickityMiniatures.resize();
initializeProjects();
root.style.setProperty("--z-index-background", -2);
let hash = window.location.hash;
if (hash) {
hashChangeHandler();
if (selectedProject) {
selectedProject.scrollIntoView();
}
}
}
function initializeProjects() {
projects.querySelectorAll(".flickity").forEach(flickityElement => {
const flickity = new Flickity(flickityElement, {
adaptiveHeight: true
});
flickityElement.flickity = flickity;
flickityElement.imageLoadHandler = (e) => flickity.resize();
flickityElement.querySelectorAll("img.medium").forEach(image => {
image.addEventListener("load", flickity.imageLoadHandler);
});
});
}
function initializeProject(project) {
const media = project.querySelector(".media");
const description = project.querySelector(".description");
initializeMedia(project.querySelector(".media"));
if (typeof initializeCustomProject === "function") {
initializeCustomProject(project);
}
}
function cleanUpProject(project) {
const media = project.querySelector(".media");
const description = project.querySelector(".description");
cleanUpMedia(media);
if (typeof cleanUpCustomProject === "function") {
cleanUpCustomProject(project);
}
}
function initializeMedia(media) {
media.querySelectorAll(":scope > .flickity").forEach(flickityElement => {
const flickity = flickityElement.flickity;
flickityElement.querySelectorAll(".vimeo-video").forEach(video => {
function showButtons(visibility) {
setVisibility(flickity.prevButton, visibility);
setVisibility(flickity.nextButton, visibility);
}
const player = newPlayer(video);
player.ready().then(() => flickity.resize());
player.on("playing", () => showButtons(false));
player.on("pause", () => showButtons(true));
player.on("ended", () => showButtons(true));
player.on("loaded", () => {
video.classList.add("loaded");
flickity.resize();
});
flickity.on("change", (index) => { player.pause(); });
});
});
media.querySelectorAll(":scope > .vimeo-video").forEach(video => {
newPlayer(video);
});
}
function cleanUpMedia(media) {
videos = media.querySelectorAll(".vimeo-video");
videos.forEach(video => {
if (video.player) {
video.player.destroy().then(() => {
video.player = null;
video.textContent = "";
});
}
video.classList.remove("loaded");
});
}
Promise.all(
[
loadJs(FLICKITY_DIST).then(() => Promise.all([
loadJs(FLICKITY_FULLSCREEN_DIST),
loadJs(FLICKITY_BG_LAZY_LOAD_DIST)
])),
loadJs(VIMEO_DIST),
loadCss(FLICKITY_CSS_DIST),
loadCss(FLICKITY_FULLSCREEN_CSS_DIST)
].concat(
scriptsToLoad.map(x => {
loadJs(x[0], x[1]);
}),
cssToLoad.map(src => loadCss(src))
)
).then(() => initialize());