-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
162 lines (150 loc) · 4.66 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
import * as flaggy from "./flaggy.mjs";
const socialPosts = [];
const postOrder = [];
function $(id) {
return document.getElementById(id);
}
function stringHash(str) {
// if you think this is a hash you are mistaken
let num = 0;
for (let i = 0; i < 6; i += 1) {
num += str.charCodeAt(i) * 256 ** i;
}
return num;
}
function clearPosts() {
$("postDisplay").remove();
const postDisplayDiv = document.createElement("div");
postDisplayDiv.id = "postDisplay";
postDisplayDiv.classList.add("post-display");
$("socialDiv").appendChild(postDisplayDiv);
}
function shufflePosts() {
const integers = [];
for (let i = 0; i < socialPosts.length; i += 1) {
integers.push(i);
}
while (integers.length > 0) {
const position = Math.floor(Math.random() * integers.length);
postOrder.push(integers.splice(position, 1));
}
}
function generatePost() {
if (postOrder.length <= 0) {
shufflePosts();
}
const postData = socialPosts[postOrder.pop()];
const salt = Math.floor(Math.random() * 9999989);
const containerDiv = $("postDisplay");
for (let i = 0; i < postData.length; i += 1) {
const postContainer = document.createElement("div");
const textContainer = document.createElement("div");
const avatarContainer = document.createElement("div");
const postHead = document.createElement("div");
const postText = document.createElement("p");
const username = document.createElement("span");
const time = document.createElement("time");
const spacer = document.createElement("span");
postContainer.classList.add("post");
username.classList.add("username");
textContainer.classList.add("post-text");
const flag = flaggy.createAvatar(stringHash(postData[i].user) + salt);
avatarContainer.classList.add("avatar");
const timeObj = new Date(postData[i].timestamp);
const timeString = timeObj.toLocaleString();
spacer.textContent = " ";
postText.textContent = postData[i].content;
username.textContent = postData[i].user;
time.textContent = timeString;
avatarContainer.appendChild(flag);
postContainer.appendChild(avatarContainer);
postHead.appendChild(username);
postHead.appendChild(spacer);
postHead.appendChild(time);
textContainer.appendChild(postHead);
if ("imageSrc" in postData[i]) {
const image = document.createElement("img");
image.src = postData[i].imageSrc;
image.classList.add("image-post");
postText.appendChild(image);
}
textContainer.appendChild(postText);
postContainer.appendChild(textContainer);
containerDiv.appendChild(postContainer);
}
}
function clearFlags() {
$("flagGrid").remove();
const flagGrid = document.createElement("div");
flagGrid.id = "flagGrid";
flagGrid.classList.add("flag-grid");
$("justFlagsDiv").appendChild(flagGrid);
}
function generateFlagGrid() {
const seed = Math.floor(Math.random() * 9999989);
const flagGrid = $("flagGrid");
for (let i = 0; i < 36; i += 1) {
const container = document.createElement("div");
container.classList.add("flag");
const flag = flaggy.createAvatar(seed + i);
flag.setAttribute("transform", "scale(1.12)");
container.append(flag);
flagGrid.append(container);
}
}
function addListeners() {
$("selectDisplayFlags").addEventListener("click", () => {
$("justFlagsDiv").classList.remove("hidden");
$("socialDiv").classList.add("hidden");
});
$("selectDisplaySocial").addEventListener("click", () => {
$("justFlagsDiv").classList.add("hidden");
$("socialDiv").classList.remove("hidden");
});
$("generateNewPost").addEventListener("click", () => {
clearPosts();
generatePost();
});
$("generateFlagGrid").addEventListener("click", () => {
clearFlags();
generateFlagGrid();
});
}
//function testy() {
// const seed = bigChungus(169);
// const cols = selectColors(seed);
// const test = setBg(seed, cols[0]);
// const width = test.width.baseVal.value;
// const height = test.height.baseVal.value;
// const group = document.createElementNS(svgNameSpace, "g");
// const rhom = drawRect(width * 0.4, 0.943 * width * 0.4, cols[1]);
// rhom.setAttribute(
// "transform",
// `
// skewX(19.5)`
// );
// group.setAttribute(
// "transform",
// `
// translate(${width / 2}, ${height / 2})
// rotate(-35.25)`
// );
// group.appendChild(rhom);
// test.appendChild(group);
// $("test").appendChild(test);
//}
//
function loadPosts() {
fetch("assets/posts.json")
.then((response) => response.json())
.then((json) => {
json.forEach((post) => socialPosts.push(post));
generatePost();
});
}
function main() {
addListeners();
loadPosts();
generateFlagGrid();
}
main();