summaryrefslogtreecommitdiff
path: root/src/app/bootstrap.ts
blob: 88ce1cc109089873f3a72cae0dd773da37380d57 (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
type TouchPoint = {
  x: number;
  y: number;
};

type TouchData = {
  touching: boolean;
  trace: TouchPoint[];
};

const tags = ['/', '/proxies', '/rules', '/connections', '/configs', '/logs'];
const touchData: TouchData = { touching: false, trace: [] };

export function registerAppBootstrap(rootEl: HTMLElement | null) {
  if (!rootEl) return;

  rootEl.addEventListener('touchstart', onTouchStart, { passive: true });
  rootEl.addEventListener('touchmove', onTouchMove, false);
  rootEl.addEventListener('touchend', onTouchEnd, false);

  // eslint-disable-next-line no-console
  console.log('Checkout the repo: https://github.com/MetaCubeX/yacd');
  // eslint-disable-next-line no-console
  console.log('Version:', __VERSION__);
}

function onTouchStart(evt: TouchEvent) {
  if (evt.touches.length !== 1) {
    touchData.touching = false;
    touchData.trace = [];
    return;
  }

  touchData.touching = true;
  touchData.trace = [{ x: evt.touches[0].screenX, y: evt.touches[0].screenY }];
}

function onTouchMove(evt: TouchEvent) {
  if (!touchData.touching) return;

  touchData.trace.push({
    x: evt.touches[0].screenX,
    y: evt.touches[0].screenY,
  });
}

function onTouchEnd() {
  if (!touchData.touching) return;

  const trace = touchData.trace;
  touchData.touching = false;
  touchData.trace = [];
  handleTouch(trace);
}

function handleTouch(trace: TouchPoint[]) {
  const start = trace[0];
  const end = trace[trace.length - 1];
  const tag = window.location.hash.slice(1);
  const index = tags.indexOf(tag);

  // eslint-disable-next-line no-console
  console.log(index, tag, tags.length);

  if (index === 3) return;

  if (end.x - start.x > 200 && index > 0) {
    window.location.hash = tags[index - 1];
  } else if (end.x - start.x < -200 && index < tags.length - 1) {
    window.location.hash = tags[index + 1];
    if (index === -1) {
      window.location.hash = tags[index + 2];
    }
  }
}