-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsettings.js
More file actions
124 lines (110 loc) · 4.59 KB
/
settings.js
File metadata and controls
124 lines (110 loc) · 4.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
var languagePack; //The language specifc messages
var languageMap; //Map of language codes and long language names
//Load language specific messages
//Returns promise
function loadLanguagePack() {
var promise = new Promise(function (resolve, reject) {
chrome.runtime.sendMessage({"req": "get-language-pack", "context": "settings"}, function (langPack) { //Request data from background app
languagePack = langPack.lang_pack; //Store the language pack globally
resolve();
});
});
return promise;
}
//Get the current language of the extension
//Returns promise
function getCurrentLanguage() {
var promise = new Promise(function (resolve, reject) {
chrome.runtime.sendMessage({"req": "get-current-language"}, function (curLang) { //Request data from background app
resolve(curLang.current_language); //Resolve promise with the current language
});
});
return promise;
}
//Load the language map file
//Return promise
function loadLangMap() {
var promise = new Promise(function (resolve, reject) {
$.getJSON("lang/langMap.json", function (result) { //Fetch the map json file
languageMap = result; //Store the map globally
resolve();
});
});
return promise;
}
//Get a listItem for languages
//language: the long name of the language to display
//selectorTextNode: the textNode (the current language) assigned to the dropdown
//languageShortName: the short (code) name of the language
//caret: the textNode assigned to the dropdown
function getListItem(language, selectorTextNode, languageShortName) {
var listItem = document.createElement("li"); //Create the list item
var hyperlink = document.createElement("a"); //Create the language displaying "a" tag
hyperlink.href = "#"; //Point the "a" tag to void
hyperlink.onclick = function () { //Set the onclick function TODO: test if language updates work at all!
//Update current language cache
langCache = languageShortName;
//Reload page content with new language, without reloading the language list
setTimeout(function () {
loadLanguagePack().then(function () {
formatPage(true);
});
}, 1000);
//Change background language settings
chrome.runtime.sendMessage({"req": "set-current-language", "language": languageShortName});
};
hyperlink.appendChild(document.createTextNode(language)); //Display the language's name on the "a" tag
listItem.appendChild(hyperlink); //Add the "a" tag to the listItem
return listItem; //Return the listItem
}
//Load static/dynamic language specific/not specific data on this page
//skipList: true to skip loading of the language list (not language specific)
function formatPage(skipList) {
//Set skipList, when value not given to false
if (typeof(skipList) === "undefined") {
skipList = false;
}
//Load static language based items
for (var property in languagePack) {
if (languagePack.hasOwnProperty(property)) {
var element = document.getElementById(property); //Load the element, the message belongs to
element.innerText = "";
element.appendChild(document.createTextNode(languagePack[property])); //Display the message on the element
}
}
//Load dynamic content
var langSelector = document.getElementById("select_language"); //Get the dropdown
getCurrentLanguage().then(function (currentLanguage) { //Retrieve the current language
langSelector.innerText = "";
var caret = document.createElement("span"); //Create the span (for the caret)
caret.classList.add("caret"); //Apply the caret class to the span
var selectorTextNode = document.createTextNode(languageMap[currentLanguage]); //Create a textNode for the current language
langSelector.appendChild(selectorTextNode); //Load current language name to dropdown
langSelector.appendChild(caret); //Load the caret character after the current language
if (!skipList) { //If listLoading not skipped
var parentList = document.getElementById("languageList"); //Get the listElement
for (var property in languageMap) {
if (languageMap.hasOwnProperty(property)) {
parentList.appendChild(getListItem(languageMap[property], selectorTextNode, property)); //Load supported languages list
}
}
}
});
}
//Handle the back button's click event
function handleClick () {
document.getElementById("back_button").addEventListener("click", function () { //Assign the event
window.location.href = "ui.html"; //Redirect to the main page
});
}
//Start the application
var init = function () {
handleClick(); //Setup click handler
formatPage(); //Initially format the page
};
//Entry point
loadLanguagePack().then(function () { //Load language data for the page
loadLangMap().then(function () { //Load language map
init(); //Load data into page
});
});