From ffbbacb84a5d67435e65e52fffa0c401aa32941d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Steind=C3=B3r?= Date: Wed, 31 May 2017 02:21:35 +0000 Subject: [PATCH] Maskot to cookie (#857) * Put the mascot into the cookie as well, major js cleanup * Update en-us.all.json * removed redundant translation --- model/user.go | 1 + public/js/main.js | 45 ++------------------------- router/publicSettingsHandler.go | 3 ++ router/template_variables.go | 2 ++ templates/index.html | 21 +++++++------ templates/public_settings.html | 8 ++++- translations/en-us.all.json | 14 ++++++++- util/publicSettings/publicSettings.go | 15 ++++++++- 8 files changed, 53 insertions(+), 56 deletions(-) diff --git a/model/user.go b/model/user.go index 940aeacc..8016fa76 100644 --- a/model/user.go +++ b/model/user.go @@ -32,6 +32,7 @@ type User struct { APITokenExpiry time.Time `gorm:"column:api_token_expiry"` Language string `gorm:"column:language"` Theme string `gorm:"column:theme"` + Mascot string `gorm:"column:mascot"` UserSettings string `gorm:"column:settings"` // TODO: move this to PublicUser diff --git a/public/js/main.js b/public/js/main.js index 0751bfa7..cac3c61c 100644 --- a/public/js/main.js +++ b/public/js/main.js @@ -1,16 +1,6 @@ var explosion = document.getElementById("explosion"); var nyanpassu = document.getElementById("nyanpassu"); -function toggleNightMode() { - var night = localStorage.getItem("night"); - if(night == "true") { - document.getElementsByTagName("head")[0].removeChild(darkStyleLink); - } else { - document.getElementsByTagName("head")[0].appendChild(darkStyleLink); - } - localStorage.setItem("night", (night == "true") ? "false" : "true"); -} - // Switches between themes when a new one is selected function switchThemes(){ themeName = document.getElementById("theme-selector").value @@ -31,18 +21,6 @@ function switchThemes(){ head.appendChild(newTheme); } - -function toggleMascot(btn) { - var state= btn.value; - if (state == "hide") { - document.getElementById("mascot").className = "hide"; - btn.value = "show"; - } else { - document.getElementById("mascot").className = ""; - btn.value = "hide"; - } -} - // Used by spoiler tags function toggleLayer(elem) { if (elem.classList.contains("hide")) @@ -70,7 +48,9 @@ function parseAllDates() { e.innerText = new Date(e.innerText).toLocaleString(lang); } } + parseAllDates(); + /*Fixed-Navbar offset fix*/ document.addEventListener("DOMContentLoaded", function(event) { var shiftWindow = function() { scrollBy(0, -70) }; @@ -86,14 +66,6 @@ document.addEventListener("DOMContentLoaded", function(event) { document.getElementsByClassName("search-box")[0].style.width = ""; document.getElementsByClassName("h-user")[0].style.display = "inline-block"; }); - - // Keep mascot hiding choice - var hideMascot = (localStorage.getItem("hide_mascot") == "true") - if (hideMascot) { - var btn = document.getElementById("mascotKeepHide"); - document.getElementById("mascot").className = "hide"; - btn.value = "show"; - } }); function playVoice() { @@ -105,16 +77,3 @@ function playVoice() { nyanpassu.play(); } } - -function toggleMascot(btn) { - var state= btn.value; - if (state == "hide") { - document.getElementById("mascot").className = "hide"; - btn.value = "show"; - localStorage.setItem("hide_mascot", "true") - } else { - document.getElementById("mascot").className = ""; - btn.value = "hide"; - localStorage.setItem("hide_mascot", "false") - } -} diff --git a/router/publicSettingsHandler.go b/router/publicSettingsHandler.go index eb6d8fb1..aa361fe3 100644 --- a/router/publicSettingsHandler.go +++ b/router/publicSettingsHandler.go @@ -46,6 +46,7 @@ func SeePublicSettingsHandler(w http.ResponseWriter, r *http.Request) { func ChangePublicSettingsHandler(w http.ResponseWriter, r *http.Request) { theme := r.FormValue("theme") lang := r.FormValue("language") + mascot := r.FormValue("mascot") availableLanguages := publicSettings.GetAvailableLanguages() defer r.Body.Close() @@ -59,12 +60,14 @@ func ChangePublicSettingsHandler(w http.ResponseWriter, r *http.Request) { if user.ID > 0 { user.Language = lang user.Theme = theme + user.Mascot = mascot // I don't know if I should use this... userService.UpdateUserCore(&user) } // Set cookie http.SetCookie(w, &http.Cookie{Name: "lang", Value: lang, Expires: timeHelper.FewDaysLater(365)}) http.SetCookie(w, &http.Cookie{Name: "theme", Value: theme, Expires: timeHelper.FewDaysLater(365)}) + http.SetCookie(w, &http.Cookie{Name: "mascot", Value: mascot, Expires: timeHelper.FewDaysLater(365)}) url, _ := Router.Get("home").URL() http.Redirect(w, r, url.String(), http.StatusSeeOther) diff --git a/router/template_variables.go b/router/template_variables.go index 05ef1401..1c4c2780 100644 --- a/router/template_variables.go +++ b/router/template_variables.go @@ -101,6 +101,7 @@ type commonTemplateVariables struct { Search searchForm T publicSettings.TemplateTfunc Theme string + Mascot string User *model.User URL *url.URL // for parsing URL in templates Route *mux.Route // for getting current route in templates @@ -152,6 +153,7 @@ func newCommonVariables(r *http.Request) commonTemplateVariables { Search: newSearchForm(), T: publicSettings.GetTfuncFromRequest(r), Theme: publicSettings.GetThemeFromRequest(r), + Mascot: publicSettings.GetMascotFromRequest(r), User: getUser(r), URL: r.URL, Route: mux.CurrentRoute(r), diff --git a/templates/index.html b/templates/index.html index e31211a9..781dfea6 100755 --- a/templates/index.html +++ b/templates/index.html @@ -57,15 +57,17 @@
{{block "content" .}}{{call $.T "nothing_here"}}{{end}}
-
- {{ if eq .Theme "tomorrow" }} - - {{ else }} - + {{if ne .Mascot "hide"}} +
+ {{ if eq .Theme "tomorrow" }} + + {{ else }} + + {{end}} {{end}} diff --git a/translations/en-us.all.json b/translations/en-us.all.json index be973c4f..c5906bfb 100644 --- a/translations/en-us.all.json +++ b/translations/en-us.all.json @@ -973,7 +973,7 @@ }, { "id": "change_settings", - "translation": "Change Settings" + "translation": "Change Appearance/Language" }, { "id": "mascot", @@ -994,5 +994,17 @@ { "id": "mark_as_hidden", "translation": "Mark as Hidden" + }, + { + "id": "cookies", + "translation": "By cliking save you consent to our use of cookies" + }, + { + "id": "show", + "translation": "Show" + }, + { + "id": "hide", + "translation": "Hide" } ] diff --git a/util/publicSettings/publicSettings.go b/util/publicSettings/publicSettings.go index 67d598e8..105b71f8 100644 --- a/util/publicSettings/publicSettings.go +++ b/util/publicSettings/publicSettings.go @@ -146,10 +146,23 @@ func GetThemeFromRequest(r *http.Request) string { return "" } +// GetThemeFromRequest: Gets the user selected theme from the request +func GetMascotFromRequest(r *http.Request) string { + user, _ := getCurrentUser(r) + if user.ID > 0 { + return user.Mascot + } + cookie, err := r.Cookie("mascot") + if err == nil { + return cookie.Value + } + return "show" +} + + func getCurrentUser(r *http.Request) (model.User, error) { if userRetriever == nil { return model.User{}, errors.New("failed to get current user: no user retriever set") } - return userRetriever.RetrieveCurrentUser(r) }