summaryrefslogtreecommitdiff
path: root/client/simple/src/js/main/preferences.ts
blob: 1db676753ec726f3fea4945445e840d29856715f (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
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
// SPDX-License-Identifier: AGPL-3.0-or-later

import { http, listen, settings } from "../toolkit.ts";
import { assertElement } from "../util/assertElement.ts";

let engineDescriptions: Record<string, [string, string]> | undefined;

const loadEngineDescriptions = async (): Promise<void> => {
  if (engineDescriptions) return;
  try {
    const res = await http("GET", "engine_descriptions.json");
    engineDescriptions = await res.json();
  } catch (error) {
    console.error("Error fetching engineDescriptions:", error);
  }
  if (!engineDescriptions) return;

  for (const [engine_name, [description, source]] of Object.entries(engineDescriptions)) {
    const elements = document.querySelectorAll<HTMLElement>(`[data-engine-name="${engine_name}"] .engine-description`);
    const sourceText = ` (<i>${settings.translations?.Source}:&nbsp;${source}</i>)`;

    for (const element of elements) {
      element.innerHTML = description + sourceText;
    }
  }
};

const toggleEngines = (enable: boolean, engineToggles: NodeListOf<HTMLInputElement>): void => {
  for (const engineToggle of engineToggles) {
    // check if element visible, so that only engines of the current category are modified
    if (engineToggle.offsetParent) {
      engineToggle.checked = !enable;
    }
  }
};

const engineElements: NodeListOf<HTMLElement> = document.querySelectorAll<HTMLElement>("[data-engine-name]");
for (const engineElement of engineElements) {
  listen("mouseenter", engineElement, loadEngineDescriptions);
}

const engineToggles: NodeListOf<HTMLInputElement> = document.querySelectorAll<HTMLInputElement>(
  "tbody input[type=checkbox][class~=checkbox-onoff]"
);

const enableAllEngines: NodeListOf<HTMLElement> = document.querySelectorAll<HTMLElement>(".enable-all-engines");
for (const engine of enableAllEngines) {
  listen("click", engine, () => toggleEngines(true, engineToggles));
}

const disableAllEngines: NodeListOf<HTMLElement> = document.querySelectorAll<HTMLElement>(".disable-all-engines");
for (const engine of disableAllEngines) {
  listen("click", engine, () => toggleEngines(false, engineToggles));
}

listen("click", "#copy-hash", async function (this: HTMLElement) {
  const target = this.parentElement?.querySelector<HTMLPreElement>("pre");
  assertElement(target);

  if (window.isSecureContext) {
    await navigator.clipboard.writeText(target.innerText);
  } else {
    const selection = window.getSelection();
    if (selection) {
      const range = document.createRange();
      range.selectNodeContents(target);
      selection.removeAllRanges();
      selection.addRange(range);
      document.execCommand("copy");
    }
  }

  if (this.dataset.copiedText) {
    this.innerText = this.dataset.copiedText;
  }
});