-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
216 lines (191 loc) · 6.92 KB
/
script.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
210
211
212
213
214
215
216
window.onload = function () {
if (!getQueryVariable("e") || !getQueryVariable("d")) {
document.getElementById("content").innerHTML = `
<form method="GET">
<p><label for="event">What happened?</label></p>
<p><textarea class="userText" id="userEvent" rows="2" name="e" required></textarea></p>
<p><label for="timesince">When did it happen?</label></p>
<p><input class="userText" id="userDate" type="text" autocomplete="off" pattern="[0-9]{1,2}-[0-9]{1,2}-[0-9]{4}" placeholder="M-D-YYYY" name="d" required></p>
<p><input id="generateLinkButton" type="submit" value="Generate Link"></p>
</form>
`;
} else {
document.getElementById("event").innerHTML = getQueryVariable("e")
.replace(/\+/g, " ")
.replace(/%20/g, " ")
.replace(/%21/g, "!")
.replace(/%22/g, `"`)
.replace(/%23/g, "#")
.replace(/%24/g, "$")
.replace(/%C2%A3/g, "£")
.replace(/%25/g, "%")
.replace(/%26/g, "&")
.replace(/%27/g, "'")
.replace(/%28/g, "(")
.replace(/%29/g, ")")
.replace(/%2A/g, "*")
.replace(/%2B/g, "+")
.replace(/%2C/g, ",")
.replace(/%2D/g, "-")
.replace(/%2E/g, ".")
.replace(/%2F/g, "/")
.replace(/%3A/g, ":")
.replace(/%3B/g, ";")
.replace(/%3D/g, "=")
.replace(/%3F/g, "?")
.replace(/%40/g, "@")
.replace(/%5B/g, "[")
.replace(/%5C/g, "\\")
.replace(/%5F/g, "_")
.replace(/%7B/g, "{")
.replace(/%7D/g, "}");
document.getElementById("timesince").innerHTML =
timeSinceDate(getQueryVariable("u"), getQueryVariable("d"), getQueryVariable("t"));
}
document.onkeydown = function (event) {
if (event.key == 'Escape' && document.getElementById("about").classList.contains("show")) {
toggleAbout();
}
};
document.getElementById("fadeInWrapper").classList.add("fadein");
}
function getQueryVariable(variable) {
var query = window.location.search.substring(1);
var vars = query.split("&");
for (var i = 0; i < vars.length; i++) {
var pair = vars[i].split("=");
if (pair[0] == variable) { return pair[1]; }
}
return (false);
}
function timeSinceDate(timeUnit, userDate, tenth) {
if (timeUnit == false) {
timeUnit = "days"
}
const oneDay = 24 * 60 * 60 * 1000; // hours*minutes*seconds*milliseconds
const oneWeek = oneDay * 7
const oneYear = oneDay * 365
const oneMonth = oneYear / 12
const dateSplit = userDate.split("-")
// subtract 1 because Date() function has 0-indexed months
var userMonth = dateSplit[0] - 1
var userDay = dateSplit[1]
var userYear = dateSplit[2]
var firstDate = new Date(userYear, userMonth, userDay);
var secondDate = Date.now();
var diffDays = Math.abs((firstDate - secondDate) / oneDay);
var unitPlural
switch (timeUnit) {
case "days":
if (tenth) {
return roundTenth(diffDays) + " days"
} else {
var num = Math.round(diffDays)
if (num == 1) {
unitPlural = " day"
} else {
unitPlural = " days"
}
return num + unitPlural
}
case "weeks":
if (tenth) {
return roundTenth(diffDays / 7) + " weeks"
} else {
var num = Math.round(diffDays / 7)
if (num == 1) {
unitPlural = " week"
} else {
unitPlural = " weeks"
}
return num + unitPlural
}
case "months":
if (tenth) {
return roundTenth(diffDays / 30) + " months"
} else {
var num = Math.round(diffDays / 30)
if (num == 1) {
unitPlural = " month"
} else {
unitPlural = " months"
}
return num + unitPlural
}
case "years":
if (tenth) {
return roundTenth(diffDays / 365) + " years"
} else {
var num = Math.round(diffDays / 365)
if (num == 1) {
unitPlural = " year"
} else {
unitPlural = " years"
}
return num + unitPlural
}
}
}
function toggleAbout() {
if (document.getElementById("about").classList.contains("show")) {
document.getElementById("about").classList.remove("show");
document.getElementById("aboutwrapper").classList.remove("show");
} else {
document.getElementById("about").classList.add("show");
document.getElementById("aboutwrapper").classList.add("show");
}
}
function roundTenth(number) {
var rounded = Math.round(number * 10) / 10
return rounded.toFixed(1)
}
function cycleUnit(leftOrRight) {
var unit = getQueryVariable("u");
var allUnits = ["days", "weeks", "months", "years"];
var unitIndex;
allUnits.forEach(function (item, index, arr) {
if (unit == item) {
unitIndex = index;
} else if (unit == false) {
unitIndex = 0;
}
})
if (leftOrRight == "left") {
var newUnitIndex;
if (unitIndex == 0) {
newUnitIndex = 3;
} else {
newUnitIndex = unitIndex - 1;
}
} else if (leftOrRight == "right") {
var newUnitIndex;
if (unitIndex == 3) {
newUnitIndex = 0;
} else {
newUnitIndex = unitIndex + 1;
}
}
var newUnit = allUnits[newUnitIndex];
var newQueryString = "?e=" + getQueryVariable("e") +
"&d=" + getQueryVariable("d") +
"&u=" + newUnit;
if (getQueryVariable("t")) {
newQueryString += "&t=t"
}
history.replaceState(null, '', '/' + newQueryString)
document.getElementById("timesince").innerHTML =
timeSinceDate(newUnit, getQueryVariable("d"), getQueryVariable("t"));
}
function cycleDecimal() {
if (!getQueryVariable("t") || !getQueryVariable("t") == "f") {
var newQueryString = window.location.search + "&t=t";
history.replaceState(null, '', '/' + newQueryString)
document.getElementById("timesince").innerHTML =
timeSinceDate(getQueryVariable("u"), getQueryVariable("d"), "t");
} else {
var newQueryString = window.location.search.replace("&t=t", "");
history.replaceState(null, '', '/' + newQueryString)
document.getElementById("timesince").innerHTML =
timeSinceDate(getQueryVariable("u"), getQueryVariable("d"), false);
}
}