aboutsummaryrefslogtreecommitdiff
path: root/webAO/dom/updateBackgroundPreview.ts
blob: bc2803b09ed8239b4f1fca8af565afae1f30bc97 (plain)
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
import { client } from "../client";
import { AO_HOST } from "../client/aoHost";
import fileExists from "../utils/fileExists";
import transparentPng from "../constants/transparentPng";

const tryBackgroundUrls = async (url: string) => {
  for (let i = 0; i < client.background_extensions.length; i++) {
    const extension = client.background_extensions[i];
    const fullFileUrl = url + extension;
    const exists = await fileExists(fullFileUrl);
    if (exists) {
      return fullFileUrl;
    }
  }
  return transparentPng;
};
export default tryBackgroundUrls;

/**
 * Update background preview.
 */
export function updateBackgroundPreview() {
  const background_select = <HTMLSelectElement>(
    document.getElementById("bg_select")
  );
  const background_filename = <HTMLInputElement>(
    document.getElementById("bg_filename")
  );
  const background_preview = <HTMLImageElement>(
    document.getElementById("bg_preview")
  );

  if (background_select.selectedIndex === 0) {
    background_filename.style.display = "initial";
  } else {
    background_filename.style.display = "none";
  }
  tryBackgroundUrls(
    `${AO_HOST}background/${encodeURI(
      background_select.value,
    )}/defenseempty`,
  ).then((resp) => {
    background_preview.src = resp;
  });
}
window.updateBackgroundPreview = updateBackgroundPreview;