-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
113 lines (90 loc) · 2.59 KB
/
index.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
const getElement = (selector) => {
return document.querySelector(selector);
};
const setTextContent = ({ selector, value }) => {
const element = getElement(selector);
element.textContent = value;
return element;
};
const createSemanticDate = ({ day, month, year }) => {
// Month counts 0-11 for Date. Input the expected month
return new Date(year, month - 1, day);
};
const languageContentSwitch = ({ en, zh, ja }) => {
const language = getElement("html").getAttribute("lang");
if (language.startsWith("zh")) {
return zh;
} else if (language.startsWith("ja")) {
return ja;
}
return en;
};
const setDaysToGo = () => {
const dateNow = new Date();
const weddingDate = createSemanticDate({ day: 26, month: 11, year: 2022 });
const daysUntil = Math.round((weddingDate - dateNow) / (24 * 60 * 60 * 1000));
const selector = "#days-to-go";
const element = getElement(selector);
// Change the value based on date calculation
if (daysUntil > 0) {
element.textContent = languageContentSwitch({
en: `${daysUntil} days to go!`,
zh: `还有${daysUntil}天!`,
ja: `あと${daysUntil}日!`,
});
} else if (daysUntil === 0) {
element.textContent = languageContentSwitch({
en: `Today's the day!`,
zh: `今天的日子!`,
ja: `今日がその日です!`,
});
}
// Remove hidden class
element.classList.remove("invisible");
};
const carouselImageLoad = () => {
const glider = document.querySelector(".glider");
const images = glider.querySelectorAll("img[data-src]");
for (let i = 0; i < images.length; i++) {
const image = images?.[i];
if (!image) {
continue;
}
image.src = image.getAttribute("data-src");
image.removeAttribute("data-src");
}
};
const inviteGuest = ({ name }) => {
if (!name) {
return;
}
const value = languageContentSwitch({
en: `Invite you, ${name}, to our wedding`,
zh: `邀请你,${name},参加我们的婚礼`,
ja: `${name}さんを私たちの結婚式にご招待`,
});
console.log({ name });
value && setTextContent({ selector: "#invitation", value });
};
const init = () => {
const urlParams = new URLSearchParams(window.location.search);
const name = urlParams.get("invitation");
setDaysToGo();
// Relies on Glider being present
new Glider(document.querySelector(".glider"), {
slidesToShow: 1,
dots: "#dots",
draggable: true,
arrows: {
prev: ".glider-prev",
next: ".glider-next",
},
});
setTimeout(carouselImageLoad, 1000);
inviteGuest({ name });
};
try {
init();
} catch (e) {
console.log(e);
}