Use datalist instead of home-rolled autocomplete
[brackets.git] / frontend / index.js
1 const sleep = (ms) => new Promise(resolve => setTimeout(resolve, ms));
2
3 /**
4 * Safari iOS pls
5 */
6 const formSubmitPolyfill = (form, callback) => {
7 if (form.requestSubmit) {
8 form.requestSubmit();
9 } else {
10 callback();
11 }
12 }
13
14 const createGenreListWithClickEvent = (genreList, genres, callback) => {
15 genreList.append(...genres.map((genre) => {
16 const name = genre["name"];
17 const option = document.createElement("option");
18 option.setAttribute("data-name", name);
19 option.addEventListener("click", callback);
20 option.appendChild(document.createTextNode(name));
21 return option;
22 }));
23 }
24
25 const relativeToCtx = (x1, x2, y1, y2, ctx) => {
26 const newX = x1 + x2;
27 const newY = y1 + y2;
28 ctx.lineTo(newX, newY);
29 return [newX, newY];
30 }
31
32 /**
33 * Return (x,y) of canvas
34 */
35 const getDimensions = (canvas) => [canvas.width, canvas.height];
36
37 /**
38 * Return (x,y) midpoint of canvas
39 */
40 const getCenter = (canvas) => getDimensions(canvas).map((dim) => dim / 2);
41
42 const getRectangleDimensionsUnbound = (canvas, xScale, yScale) => {
43 [width, height] = getDimensions(canvas);
44 return [width * xScale, height * yScale];
45 }
46
47 const getRectangleDimensions = (canvas) => getRectangleDimensionsUnbound(canvas, .5, .1);
48
49 /**
50 * Draw champion box and clear background.
51 */
52 const drawWinner = (canvas) => {
53 const ctx = canvas.getContext("2d");
54 const [width, height] = getDimensions(canvas);
55 const [mid_x, mid_y] = getCenter(canvas);
56 const [rect_width, rect_height] = getRectangleDimensions(canvas);
57 ctx.strokeRect(mid_x - rect_width / 2, mid_y - rect_height / 2, rect_width, rect_height);
58 ctx.clearRect(mid_x - rect_width / 2, mid_y - rect_height / 2, rect_width, rect_height);
59 }
60
61 /**
62 * Draws a path in context from the current location to a point xDist * left, yDist * up away from it.
63 * @param ctx RenderingContext
64 * @param x, y float current location
65 * @param x, y float distance away
66 * @param up, left (-1|1) directions
67 * @return [newNodeX, newNodeY]
68 */
69 const drawBranchFrom = (ctx, x, y, xDist, yDist, left, up) => {
70 const newX = x + (xDist * left)
71 const newY = y + (yDist * up)
72 ctx.lineTo(x, newY);
73 ctx.lineTo(newX, newY);
74 ctx.stroke();
75 return [newX, newY];
76 }
77
78 const drawArtistOnCtx = (ctx, artistName, x, y) => {
79 ctx.font = "20px sans serif";
80 ctx.strokeText(artistName, x, y);
81 }
82
83 /**
84 * Draws paths to the terminal nodes of a round
85 * @param x, y the point representation of the start of the branch
86 * @param baseCallback a callback that needs the terminal x,y context
87 */
88 const drawMatchup = (canvas, x, y, iter, maxIter, left, artists, baseCallback) => {
89 if (iter === maxIter) {
90 return baseCallback(x, y);
91 }
92 const ctx = canvas.getContext("2d");
93 ctx.direction = left === -1 ? "ltr" : "rtl";
94 const [width, height] = getDimensions(canvas);
95
96 const drawBranchUp = (xDist, yDist) => drawBranchFrom(ctx, x, y, xDist, yDist, left, -1);
97 const drawBranchDown = (xDist, yDist) => drawBranchFrom(ctx, x, y, xDist, yDist, left, 1);
98 const drawArtist = (artistName, x, y) => drawArtistOnCtx(ctx, artistName, x, y);
99 const drawArtist1 = (x, y) => drawArtist(artists.shift()["name"], x, y);
100 const drawArtist2 = (x, y) => drawArtist(artists.pop()["name"], x, y);
101
102 ctx.beginPath();
103 ctx.moveTo(x, y);
104 const branchDistances = [width / (5 * (iter + 1)), height / (9 * (iter + 1))];
105 drawMatchup(
106 canvas,
107 ...drawBranchUp(...branchDistances),
108 iter + 1,
109 maxIter,
110 left,
111 artists,
112 drawArtist1,
113 );
114
115 ctx.moveTo(x, y);
116 drawMatchup(
117 canvas,
118 ...drawBranchDown(...branchDistances),
119 iter + 1,
120 maxIter,
121 left,
122 artists,
123 drawArtist2,
124 );
125 }
126
127 const drawBracket = (canvas, artists, genre) => {
128 const context = canvas.getContext("2d");
129 context.clearRect(0, 0, canvas.width, canvas.height);
130 drawWinner(canvas);
131 const [mid_x, mid_y] = getCenter(canvas);
132 context.font = '28px sans serif'
133 context.textAlign = "center";
134 context.strokeText(genre.toUpperCase(), mid_x, 40);
135 context.textAlign = "start";
136 const [rect_width, rect_height] = getRectangleDimensions(canvas);
137 const groups = 4;
138 const rounds = Math.floor(Math.log2(artists.length / groups));
139 for (let group = 1; group <= groups; group++) {
140 drawMatchup(
141 canvas,
142 mid_x + (Math.pow(-1, group) * (rect_width / 6)),
143 mid_y + (Math.pow(-1, Math.floor(group / 2)) * (rect_height * 2.5)),
144 0,
145 rounds,
146 Math.pow(-1, group),
147 artists,
148 (x, y) => console.log("hello")
149 );
150 }
151 }
152
153 window.onload = () => {
154 const lStorage = window.localStorage;
155 const genreList = document.getElementById("genre-list");
156 const genreInput = document.getElementById("genre-input");
157 const genreForm = document.getElementById("genre-form");
158 const canvas = document.getElementById("bracket");
159
160 const formSubmitAction = () => {
161 fetch(encodeURI(`http://api.brackets.jacobcasper.com/artist/genre?genre_name=${genreInput.value}`))
162 .then((response) => response.json())
163 .then((data) => drawBracket(canvas, data.slice(0, 33), genreInput.value));
164 }
165
166 const genreFormSubmitPolyfill = () => formSubmitPolyfill(genreList, formSubmitAction)
167
168 const createGenreList = (genreList, genres) => {
169 return createGenreListWithClickEvent(genreList, genres, (e) => {
170 genreInput.value = e.target.innerText;
171 genreFormSubmitPolyfill()
172 })
173 }
174 let genres = JSON.parse(lStorage.getItem("genres"))
175 if (genres === null) {
176 fetch("http://api.brackets.jacobcasper.com/genre")
177 .then((response) => response.text())
178 .then((text) => {
179 window.localStorage.setItem("genres", text);
180 genres = JSON.parse(text);
181 createGenreList(genreList, genres);
182 });
183 } else {
184 createGenreList(genreList, genres);
185 }
186
187 genreForm.addEventListener("submit", (e) => {
188 e.preventDefault();
189 formSubmitAction()
190 });
191
192 genreInput.addEventListener("change", (e) => {
193 genreFormSubmitPolyfill()
194 });
195
196
197 canvas.width = window.innerWidth;
198 canvas.height = window.innerHeight;
199 drawBracket(canvas, ['dummy', 'dummy', 'dum' ,'dumhy'], "");
200
201 const bgImg = new Image();
202 bgImg.onload = () => {
203 const ctx = canvas.getContext("2d");
204 //ctx.mozImageSmoothingEnabled = false;
205 //ctx.webkitImageSmoothingEnabled = false;
206 //ctx.msImageSmoothingEnabled = false;
207 //ctx.imageSmoothingEnabled = false;
208 ctx.drawImage(
209 bgImg,
210 0,
211 0,
212 (canvas.width / bgImg.width) * bgImg.width,
213 (canvas.height / bgImg.height) * bgImg.height
214 );
215 }
216
217 const upload = document.getElementById("image-upload");
218 upload.addEventListener("change", (e) => {
219 bgImg.src = URL.createObjectURL(e.target.files[0]);
220 });
221 }