summaryrefslogtreecommitdiff
path: root/static/scripts/theme.js
blob: 4bfa68e88ed179ed1b8e423bb8cbe0c1dba8c83a (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
var local_storage = window.localStorage;

window.onload = () => {
  let theme = local_storage.getItem('theme');
  let switch_theme = document.getElementById('theme-switch');

  if(theme == null){
    local_storage.setItem('theme', 'dark');
    switch_theme.checked = true;
  }
  else{
    if(theme == 'dark'){
      switch_theme.checked = true;
    }
    else{
      switch_theme.checked = false;

      let theme = document.getElementById('theme-css');
      let href = theme.getAttribute('href');

      href = href.replace('dark.css', 'light.css');
      theme.setAttribute('href', href);
    }
  }

}

function setTheme(){
  let switch_theme = document.getElementById('theme-switch');

  if(switch_theme.checked == true){
    local_storage.setItem('theme', 'dark');
  }
  else{
    local_storage.setItem('theme', 'light');
  }
}

// toggles between both themes, and then calls set theme to actually set it persistently.
function toggleTheme(){
  let theme = document.getElementById('theme-css');
  let href = theme.getAttribute('href');

  if(href.endsWith('dark.css')){
    href = href.replace('dark.css', 'light.css');
  }
  else if (href.endsWith('light.css')){
    href = href.replace('light.css', 'dark.css');
  }
  else{
    console.log('Wrong replacement.');
  }

  theme.setAttribute('href', href);
  setTheme();
}