darktheme.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. /*
  2. Dark Theme Toggle Manager
  3. This script is used to manage the dark theme toggle button in the header of the website.
  4. It will change the theme of the website to dark mode when the toggle is clicked and back to light mode when clicked again.
  5. Must be included just after the start of body tag in the HTML file.
  6. */
  7. function _whiteThemeHandleApplyChange(){
  8. $(".menubar .logo").attr("src", "img/logo.svg");
  9. }
  10. function _darkThemeHandleApplyChange(){
  11. $(".menubar .logo").attr("src", "img/logo_white.svg");
  12. }
  13. //Check if the theme is dark, must be done before the body is loaded to prevent flickering
  14. function setDarkTheme(isDarkTheme = false){
  15. if (isDarkTheme){
  16. $("body").addClass("darkTheme");
  17. $("#themeColorButton").html(`<i class="ui sun icon"></i>`);
  18. localStorage.setItem("theme", "dark");
  19. //Check if the page is still loading, if not change the logo
  20. if (document.readyState == "complete"){
  21. _darkThemeHandleApplyChange();
  22. }else{
  23. //Wait for the page to load and then change the logo
  24. $(document).ready(function(){
  25. _darkThemeHandleApplyChange();
  26. });
  27. }
  28. }else{
  29. $("body").removeClass("darkTheme")
  30. $("#themeColorButton").html(`<i class="ui moon icon"></i>`);
  31. localStorage.setItem("theme", "light");
  32. //By default the page is white theme. So no need to change the logo if page is still loading
  33. if (document.readyState == "complete"){
  34. //Switching back to light theme
  35. _whiteThemeHandleApplyChange();
  36. }
  37. }
  38. }
  39. if (localStorage.getItem("theme") == "dark"){
  40. setDarkTheme(true);
  41. }else{
  42. setDarkTheme(false);
  43. }