Add dropdown to the bottom of the page (needs JS)
Cette révision appartient à :
Parent
7808615c6a
révision
5dcd3892f4
3 fichiers modifiés avec 62 ajouts et 7 suppressions
|
@ -47,3 +47,36 @@ for(var i in list) {
|
|||
var date = new Date(e.innerText);
|
||||
e.innerText = date.toDateString() + " " + date.toLocaleTimeString();
|
||||
}
|
||||
|
||||
function loadLanguages() {
|
||||
var xhr = new XMLHttpRequest();
|
||||
xhr.onreadystatechange = function() {
|
||||
if (xhr.readyState == 4 && xhr.status == 200) {
|
||||
var selector = document.getElementById("bottom_language_selector");
|
||||
selector.hidden = false
|
||||
/* Response format is
|
||||
* { "current": "(user current language)",
|
||||
* "languages": {
|
||||
* "(language_code)": "(language_name"),
|
||||
* }} */
|
||||
var response = JSON.parse(xhr.responseText);
|
||||
for (var language in response.languages) {
|
||||
if (!response.languages.hasOwnProperty(language)) continue;
|
||||
|
||||
var opt = document.createElement("option")
|
||||
opt.value = language
|
||||
opt.innerHTML = response.languages[language]
|
||||
if (language == response.current) {
|
||||
opt.selected = true
|
||||
}
|
||||
|
||||
selector.appendChild(opt)
|
||||
}
|
||||
}
|
||||
}
|
||||
xhr.open("GET", "/language?format=json", true)
|
||||
xhr.send()
|
||||
}
|
||||
|
||||
loadLanguages();
|
||||
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
package router;
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
|
||||
"github.com/ewhal/nyaa/util/languages"
|
||||
|
@ -8,16 +9,31 @@ import (
|
|||
"github.com/gorilla/mux"
|
||||
)
|
||||
|
||||
type LanguagesJSONResponse struct {
|
||||
Current string `json:"current"`
|
||||
Languages map[string]string `json:"languages"`
|
||||
}
|
||||
|
||||
func SeeLanguagesHandler(w http.ResponseWriter, r *http.Request) {
|
||||
_, Tlang := languages.GetTfuncAndLanguageFromRequest(r, "en-us")
|
||||
availableLanguages := languages.GetAvailableLanguages()
|
||||
|
||||
clv := ChangeLanguageVariables{NewSearchForm(), Navigation{}, Tlang.Tag, availableLanguages, GetUser(r), r.URL, mux.CurrentRoute(r)}
|
||||
languages.SetTranslationFromRequest(changeLanguageTemplate, r, "en-us")
|
||||
err := changeLanguageTemplate.ExecuteTemplate(w, "index.html", clv)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
format := r.URL.Query().Get("format")
|
||||
if format == "json" {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
err := json.NewEncoder(w).Encode(LanguagesJSONResponse{Tlang.Tag, availableLanguages})
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
} else {
|
||||
clv := ChangeLanguageVariables{NewSearchForm(), Navigation{}, Tlang.Tag, availableLanguages, GetUser(r), r.URL, mux.CurrentRoute(r)}
|
||||
languages.SetTranslationFromRequest(changeLanguageTemplate, r, "en-us")
|
||||
err := changeLanguageTemplate.ExecuteTemplate(w, "index.html", clv)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -95,7 +95,13 @@
|
|||
<footer>
|
||||
Powered by NyaaPantsu
|
||||
</footer>
|
||||
<center><a href="{{ .URL.Parse "/language" }}">{{ T "change_language" }}</a></center>
|
||||
|
||||
<form method="POST" action="{{ .URL.Parse "/language" }}" id="bottom_language_selector_form">
|
||||
<select id="bottom_language_selector" name="language" onchange="javascript:document.getElementById('bottom_language_selector_form').submit()" hidden class="form-control"></select>
|
||||
</form>
|
||||
<noscript>
|
||||
<center><a href="{{ .URL.Parse "/language" }}">{{ T "change_language" }}</a></center>
|
||||
</noscript>
|
||||
|
||||
<!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
|
||||
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
|
||||
|
|
Référencer dans un nouveau ticket