diff --git a/Wave/Components/App.razor b/Wave/Components/App.razor index a251d54..3cce66d 100644 --- a/Wave/Components/App.razor +++ b/Wave/Components/App.razor @@ -1,7 +1,7 @@ @using System.Globalization @using Microsoft.AspNetCore.Localization - + @@ -46,6 +46,8 @@ [CascadingParameter] public HttpContext? HttpContext { get; set; } + private string? UserTheme { get; set; } + protected override void OnInitialized() { HttpContext?.Response.Cookies.Append( CookieRequestCultureProvider.DefaultCookieName, @@ -57,5 +59,10 @@ IsEssential = true, SameSite = SameSiteMode.Strict }); + + if (HttpContext?.Request.Cookies.ContainsKey(".Wave.Theme") is true) { + HttpContext.Request.Cookies.TryGetValue(".Wave.Theme", out string? theme); + UserTheme = theme; + } } } \ No newline at end of file diff --git a/Wave/Components/Layout/MainLayout.razor b/Wave/Components/Layout/MainLayout.razor index 0fa5a09..0206b22 100644 --- a/Wave/Components/Layout/MainLayout.razor +++ b/Wave/Components/Layout/MainLayout.razor @@ -59,11 +59,13 @@ - + @@ -76,6 +78,3 @@ - - - \ No newline at end of file diff --git a/Wave/Controllers/ThemeController.cs b/Wave/Controllers/ThemeController.cs new file mode 100644 index 0000000..d7edf1e --- /dev/null +++ b/Wave/Controllers/ThemeController.cs @@ -0,0 +1,23 @@ +using Microsoft.AspNetCore.Mvc; + +namespace Wave.Controllers; + +[ApiController] +[Route("/theme")] +public class ThemeController : ControllerBase { + [HttpGet("{theme}")] + public IActionResult SetLanguage(string? theme, string returnUrl = "") { + if (theme is null) { + Response.Cookies.Delete(".Wave.Theme"); + } else { + Response.Cookies.Append(".Wave.Theme", theme, new CookieOptions { + Expires = DateTimeOffset.UtcNow.AddYears(1), + IsEssential = true, + SameSite = SameSiteMode.Strict + }); + } + + return LocalRedirect(string.IsNullOrWhiteSpace(returnUrl) ? "/" : returnUrl); + } + +} \ No newline at end of file diff --git a/Wave/wwwroot/lib/theme-change/LICENSE b/Wave/wwwroot/lib/theme-change/LICENSE deleted file mode 100644 index 7177316..0000000 --- a/Wave/wwwroot/lib/theme-change/LICENSE +++ /dev/null @@ -1,21 +0,0 @@ -MIT License - -Copyright (c) 2022 Pouya Saadeghi - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in all -copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -SOFTWARE. diff --git a/Wave/wwwroot/lib/theme-change/index.min.js b/Wave/wwwroot/lib/theme-change/index.min.js deleted file mode 100644 index 9960082..0000000 --- a/Wave/wwwroot/lib/theme-change/index.min.js +++ /dev/null @@ -1 +0,0 @@ -function themeToggle(){var toggleEl=document.querySelector("[data-toggle-theme]");(function(theme=localStorage.getItem("theme")){if(localStorage.getItem("theme")){document.documentElement.setAttribute("data-theme",theme);if(toggleEl){[...document.querySelectorAll("[data-toggle-theme]")].forEach(el=>{el.classList.add(toggleEl.getAttribute("data-act-class"))})}}})();if(toggleEl){[...document.querySelectorAll("[data-toggle-theme]")].forEach(el=>{el.addEventListener("click",function(){var themesList=el.getAttribute("data-toggle-theme");if(themesList){var themesArray=themesList.split(",");if(document.documentElement.getAttribute("data-theme")==themesArray[0]){if(themesArray.length==1){document.documentElement.removeAttribute("data-theme");localStorage.removeItem("theme")}else{document.documentElement.setAttribute("data-theme",themesArray[1]);localStorage.setItem("theme",themesArray[1])}}else{document.documentElement.setAttribute("data-theme",themesArray[0]);localStorage.setItem("theme",themesArray[0])}}[...document.querySelectorAll("[data-toggle-theme]")].forEach(el=>{el.classList.toggle(this.getAttribute("data-act-class"))})})})}}function themeBtn(){(function(theme=localStorage.getItem("theme")){if(theme!=undefined&&theme!=""){if(localStorage.getItem("theme")&&localStorage.getItem("theme")!=""){document.documentElement.setAttribute("data-theme",theme);var btnEl=document.querySelector("[data-set-theme='"+theme.toString()+"']");if(btnEl){[...document.querySelectorAll("[data-set-theme]")].forEach(el=>{el.classList.remove(el.getAttribute("data-act-class"))});if(btnEl.getAttribute("data-act-class")){btnEl.classList.add(btnEl.getAttribute("data-act-class"))}}}else{var btnEl=document.querySelector("[data-set-theme='']");if(btnEl.getAttribute("data-act-class")){btnEl.classList.add(btnEl.getAttribute("data-act-class"))}}}})();[...document.querySelectorAll("[data-set-theme]")].forEach(el=>{el.addEventListener("click",function(){document.documentElement.setAttribute("data-theme",this.getAttribute("data-set-theme"));localStorage.setItem("theme",document.documentElement.getAttribute("data-theme"));[...document.querySelectorAll("[data-set-theme]")].forEach(el=>{el.classList.remove(el.getAttribute("data-act-class"))});if(el.getAttribute("data-act-class")){el.classList.add(el.getAttribute("data-act-class"))}})})}function themeSelect(){(function(theme=localStorage.getItem("theme")){if(localStorage.getItem("theme")){document.documentElement.setAttribute("data-theme",theme);var optionToggler=document.querySelector("select[data-choose-theme] [value='"+theme.toString()+"']");if(optionToggler){[...document.querySelectorAll("select[data-choose-theme] [value='"+theme.toString()+"']")].forEach(el=>{el.selected=true})}}})();if(document.querySelector("select[data-choose-theme]")){[...document.querySelectorAll("select[data-choose-theme]")].forEach(el=>{el.addEventListener("change",function(){document.documentElement.setAttribute("data-theme",this.value);localStorage.setItem("theme",document.documentElement.getAttribute("data-theme"));[...document.querySelectorAll("select[data-choose-theme] [value='"+localStorage.getItem("theme")+"']")].forEach(el=>{el.selected=true})})})}}function themeChange(attach=true){if(attach===true){document.addEventListener("DOMContentLoaded",function(event){themeToggle();themeSelect();themeBtn()})}else{themeToggle();themeSelect();themeBtn()}}if(typeof exports!="undefined"){module.exports={themeChange:themeChange}}else{themeChange()} \ No newline at end of file