summaryrefslogtreecommitdiff
path: root/static/scripts/theme.js
blob: 68a0cbb7f1e7ed4da5f94c38260bddb550ce757c (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
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 code_theme = document.getElementById('code-theme-css');
      let href = theme.getAttribute('href');
      let code_href = code_theme.getAttribute('href');

      href = href.replace('dark.css', 'light.css');
      code_href = code_href.replace('solarized-dark.min.css', 'solarized-light.min.css');
      theme.setAttribute('href', href);
      code_theme.setAttribute('href', code_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 code_theme = document.getElementById('code-theme-css');
  let href = theme.getAttribute('href');
  let code_href = code_theme.getAttribute('href');

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

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