all repos — artbound-python @ 61c699ca6fc5947d28d19bddabb995e31c50a654

A client-server reimplementation of the administration panel for ArtBound.

artbound_python/static/script.js (view raw)

  1const MAX_WIDTH = MAX_HEIGHT = 1000,
  2	WATERMARK_SRC = "/static/res/wm.png",
  3	WATERMARK_WIDTH = 325,
  4	WATERMARK_HEIGHT = 98,
  5	IG_TEMPLATE_SRC = "/static/res/ig.png",
  6	IG_MIN_OFFSET_X = 0,
  7	IG_MIN_OFFSET_Y = 342,
  8	IG_MAX_WIDTH = 1080,
  9	IG_MAX_HEIGHT = 988,
 10
 11	WATERMARK = new Image(),
 12	IG_TEMPLATE = new Image(),
 13	last_updated_link = document.getElementById("last-updated-link"),
 14	month_input = document.getElementById("month_input"),
 15	month_div = document.getElementById("month_div"),
 16	get_button = document.getElementById("get_button"),
 17	selectall_button = document.getElementById("selectall_button"),
 18	selectnone_button = document.getElementById("selectnone_button"),
 19	controls_div = document.getElementById("controls"),
 20	opacity_range = document.getElementById("opacity_range"),
 21	content_div = document.getElementById("content"),
 22	canvas_link = document.getElementById("canvas-download"),
 23	canvas_ig = document.getElementById("instagram-canvas"),
 24	fanart_template = document.getElementById("fanart-template").innerHTML;
 25
 26let fanarts = new Array(),
 27	new_entries = 0;
 28
 29WATERMARK.src = WATERMARK_SRC;
 30IG_TEMPLATE.src = IG_TEMPLATE_SRC;
 31month_input.addEventListener("keyup", (e) => { if (e.key === "Enter") getArtworks(); });
 32
 33function addCanvasEvents(element, ctx) {
 34	function abc(e) {
 35		if (element.enabled == 0) return;
 36		if (e.button != 0) return;
 37		element.watermark.opacity = opacity_range.value;
 38		addWatermark(e, element, ctx);
 39	}
 40	if (element.clicked) {
 41		element.canvas.addEventListener('mousedown', abc);
 42		return;
 43	}
 44	element.canvas.addEventListener('mousemove', abc);
 45	element.canvas.addEventListener('mouseleave', () => {
 46		if (!element.clicked && element.enabled)
 47			setBaseImage(element.image, element.canvas, ctx);
 48	});
 49	element.canvas.addEventListener('mousedown', (e) => {
 50		element.clicked = true;
 51		element.canvas.removeEventListener('mousemove', abc);
 52		element.canvas.addEventListener('mousedown', abc);
 53	});
 54}
 55
 56function createElementFromHTML(htmlString) {
 57	const div = document.createElement('div');
 58	div.innerHTML = htmlString.trim();
 59	return div.firstChild;
 60}
 61
 62function getNewCardHtml(element) {
 63	const id = element.id,
 64		index = element.index,
 65		name = element.name,
 66		content = element.content,
 67		filename = `${('0' + element.index).slice(-2)} - ${element.name}.png`,
 68		disabled = element.enabled == 0 ? " entry-disabled" : "";
 69	const html_string = Mustache.render("{{={| |}=}}" + fanart_template, { id, index, name, content, filename, disabled });
 70	element.div = createElementFromHTML(html_string);
 71	element.canvas = element.div.getElementsByTagName("canvas")[0];
 72	const ctx = element.canvas.getContext("2d");
 73	element.image = new Image();
 74	element.image.addEventListener("load", () => {
 75		const wm_info = element.watermark;
 76		setBaseImage(element.image, element.canvas, ctx);
 77		if (element.clicked)
 78			drawWatermark(wm_info, ctx);
 79		addCanvasEvents(element, ctx);
 80	});
 81	element.image.src = element.content;
 82	return element.div;
 83}
 84
 85async function updateFanartList() {
 86	content_div.innerHTML = "";
 87	get_button.disabled = false;
 88	get_button.innerText = "Aggiungi";
 89
 90	let i = 0;
 91	for (fanart of fanarts) {
 92		if (fanart.enabled) {
 93			i++;
 94			fanart.index = i;
 95			content_div.appendChild(getNewCardHtml(fanart));
 96		}
 97	}
 98	for (fanart of fanarts) {
 99		if (!fanart.enabled) {
100			fanart.index = 0;
101			content_div.appendChild(getNewCardHtml(fanart));
102		}
103	}
104}
105
106function getFanart(id) {
107	return fanarts.find(element => element.id == id)
108}
109
110function toggleEntry(id) {
111	entry = getFanart(id);
112	if (!entry) return;
113
114	entry.enabled = !entry.enabled;
115	updateFanartList()
116}
117
118function saveCanvas(my_canvas, filename) {
119	canvas_link.href = my_canvas.toDataURL("image/png").replace("image/png", "image/octet-stream");
120	canvas_link.setAttribute("download", filename);
121	canvas_link.click()
122}
123
124function reloadEntry(id) {
125	const fanart = getFanart(id);
126	if (!fanart) return;
127
128	const old_div = fanart.div;
129	content_div.replaceChild(getNewCardHtml(fanart), old_div);
130	old_div.remove();
131}
132
133function selectAllNone(toggle) {
134	fanarts.map(x => x.enabled = toggle)
135	updateFanartList()
136}
137
138function clickCoordsToCanvas(clickX, clickY, c) {
139	const rect = c.getBoundingClientRect();
140	const x = (clickX - rect.left) * c.width / c.clientWidth;
141	const y = (clickY - rect.top) * c.height / c.clientHeight;
142	return [x, y];
143}
144
145function drawWatermark(wm_info, ctx) {
146	ctx.globalAlpha = wm_info.opacity;
147	ctx.filter = wm_info.invert;
148	ctx.drawImage(WATERMARK, wm_info.x - (WATERMARK_WIDTH / 2), wm_info.y - (WATERMARK_HEIGHT / 2), WATERMARK_WIDTH, WATERMARK_HEIGHT);
149}
150
151function addWatermark(event, element, ctx) {
152	setBaseImage(element.image, element.canvas, ctx);
153	const [x, y] = clickCoordsToCanvas(event.clientX, event.clientY, element.canvas);
154	element.watermark.x = x;
155	element.watermark.y = y
156	drawWatermark(element.watermark, ctx);
157}
158
159function updateOpacity() {
160	opacity_label.innerHTML = Math.round(opacity_range.value * 100) + '%';
161}
162
163function getFactor(img_width, img_height, max_width, max_height) {
164	return Math.min(max_width / img_width, max_height / img_height);
165}
166
167function setBaseImage(img, c, ctx) {
168	const f = getFactor(img.width, img.height, MAX_WIDTH, MAX_HEIGHT);
169	const new_width = c.width = Math.ceil(img.width * f);
170	const new_height = c.height = Math.ceil(img.height * f)
171	ctx.imageSmoothingEnabled = (f < 1);
172	ctx.drawImage(img, 0, 0, new_width, new_height);
173}
174
175function moveUpDown(id, amount) {
176	const entry = fanarts.find(element => element.id == id);
177	if (!entry) return;
178	if (!entry.enabled) return;
179	const pos = fanarts.indexOf(entry);
180	const new_pos = pos + amount;
181
182	if (new_pos <= -1 || new_pos >= fanarts.length) return;
183
184	[fanarts[pos], fanarts[new_pos]] = [fanarts[new_pos], fanarts[pos]];
185	updateFanartList();
186}
187
188function toggleInvert(id, button) {
189	const entry = fanarts.find(element => element.id == id);
190	if (!entry) return;
191	if (!entry.enabled) return;
192	entry.watermark.invert = entry.watermark.invert == '' ? 'invert(1)' : '';
193	button.innerText = entry.watermark.invert ? "⚫" : "⚪";
194	const ctx = entry.canvas.getContext('2d');
195	setBaseImage(entry.image, entry.canvas, ctx);
196	drawWatermark(entry.watermark, ctx);
197}
198
199async function postData(url = "", data = {}, contentType = "application/json") {
200	const response = await fetch(url, { method: "POST", headers: { "Content-Type": contentType }, body: JSON.stringify(data) });
201	return response.json();
202}
203
204function saveEntry(id) {
205	const entry = getFanart(id);
206	if (!entry) return;
207	if (!entry.enabled) return;
208	saveCanvas(entry.canvas, entry.canvas.getAttribute("data-filename"));
209}
210
211function saveCanvasIG(my_canvas) {
212	const f = getFactor(my_canvas.width, my_canvas.height, IG_MAX_WIDTH, IG_MAX_HEIGHT);
213	const width = Math.ceil(my_canvas.width * f);
214	const height = Math.ceil(my_canvas.height * f);
215	const offset_x = Math.round((IG_MAX_WIDTH - width) / 2);
216	const offset_y = Math.round((IG_MAX_HEIGHT - height) / 2);
217	destCtx = canvas_ig.getContext('2d');
218	destCtx.drawImage(IG_TEMPLATE, 0, 0);
219	destCtx.drawImage(my_canvas, IG_MIN_OFFSET_X + offset_x, IG_MIN_OFFSET_Y + offset_y, width, height);
220	saveCanvas(canvas_ig, "IG - " + my_canvas.getAttribute("data-filename"));
221}
222
223function saveEntryIG(id) {
224	entry = getFanart(id);
225	if (!entry) return;
226	if (!entry.enabled) return;
227	saveCanvasIG(entry.canvas);
228}
229
230function getArtworks() {
231	get_button.disabled = true;
232	get_button.innerText = "…"
233	postData("/", { month: month_input.value }).then((data) => {
234		fanarts = fanarts.concat(data);
235		controls_div.hidden = false;
236		updateOpacity();
237		updateFanartList();
238	});
239}
240
241function saveAll() {
242	const response = confirm("Vuoi davvero scaricare tutte le fanart?");
243	if (response == false) return;
244	fanarts.forEach((fanart) => {
245		if (fanart.enabled)
246			saveCanvas(fanart.canvas, fanart.canvas.getAttribute("data-filename"));
247	})
248}
249
250function saveAllIG() {
251	const response = confirm("Vuoi davvero scaricare tutte le storie per le fanart?");
252	if (response == false) return;
253	fanarts.forEach((fanart) => {
254		if (fanart.enabled)
255			saveCanvasIG(fanart.canvas);
256	});
257}
258
259function updateDb() {
260	postData("/update").then((data) => {
261		last_updated_link.innerText = data.timestamp;
262		if (data.new == 0) return;
263		new_entries += data.new;
264		last_updated_link.innerText += ` (+${new_entries})`;
265	});
266}