Albirew/nyaa-pantsu
Archivé
1
0
Bifurcation 0

Maskot to cookie (#857)

* Put the mascot into the cookie as well, major js cleanup

* Update en-us.all.json

* removed redundant translation
Cette révision appartient à :
Steindór 2017-05-31 02:21:35 +00:00 révisé par alucard0134
Parent 0d70ed3038
révision ffbbacb84a
8 fichiers modifiés avec 53 ajouts et 56 suppressions

Voir le fichier

@ -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

Voir le fichier

@ -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")
}
}

Voir le fichier

@ -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)

Voir le fichier

@ -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),

Voir le fichier

@ -57,15 +57,17 @@
<div class="content container center">
{{block "content" .}}{{call $.T "nothing_here"}}{{end}}
</div>
<div id="mascot" class="hide-xs" onclick="playVoice();"></div>
{{ if eq .Theme "tomorrow" }}
<audio id="explosion" hidden preload="none">
<source src="https://megumin.love/sounds/explosion.mp3" type="audio/mpeg">
</audio>
{{ else }}
<audio id="nyanpassu" hidden preload="none">
<source src="https://a.doko.moe/sewang.mp3" type="audio/mpeg">
</audio>
{{if ne .Mascot "hide"}}
<div id="mascot" class="hide-xs" onclick="playVoice();"></div>
{{ if eq .Theme "tomorrow" }}
<audio id="explosion" hidden preload="none">
<source src="https://megumin.love/sounds/explosion.mp3" type="audio/mpeg">
</audio>
{{ else }}
<audio id="nyanpassu" hidden preload="none">
<source src="https://a.doko.moe/sewang.mp3" type="audio/mpeg">
</audio>
{{end}}
{{end}}
<div class="pagination">
{{ genNav .Navigation .URL 15 }}
@ -73,7 +75,6 @@
<footer id="footer">
<div class="container footer center">
<div class="footer-opt">
<button class="form-input btn" id="mascotKeepHide" onclick="toggleMascot(this)" value="hide"><span class="oi" data-glyph="eye"></span> {{ call $.T "mascot" }}</button>
<p><a href="/settings">{{ call $.T "change_settings" }}</a></p>
</div>
<span><i>Powered by <a href="#">NyaaPantsu</a></i></span>

Voir le fichier

@ -20,10 +20,16 @@
<option value=""{{ if eq $.Theme "" }} selected{{end}}>{{call $.T "theme_none"}}</option>
</select>
</br>
<h3>{{call $.T "mascot"}}</h3>
<select id="mascot-selector" name="mascot" class="form-input">
{{call $.T "mascot"}}
<option value="show" {{ if eq $.Mascot "show" }} selected{{end}}>{{call $.T "show"}}</option>
<option value="hide" {{ if eq $.Mascot "hide" }} selected{{end}}>{{call $.T "hide"}}</option>
</select>
</br>
<p>{{call $.T "cookies"}}</p>
<button type="submit" class="form-input btn">{{call $.T "save_changes"}}</button>
</div>
</form>
<div style="padding-bottom: 1em"></div>
</div>

Voir le fichier

@ -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"
}
]

Voir le fichier

@ -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)
}