blob: 4ab8209c706fd3454dfb1e4091fe8c6cc410b25e [file] [log] [blame]
Adrià3fb18982014-12-14 18:02:01 +01001var isoLangs = {"af":{"name":"Afrikaans","nativeName":"Afrikaans"},"sq":{"name":"Albanian","nativeName":"Shqip"},"ar":{"name":"Arabic","nativeName":"\u0639\u0631\u0628\u064a"},"hy":{"name":"Armenian","nativeName":"\u0540\u0561\u0575\u0565\u0580\u0567\u0576"},"az":{"name":"Azerbaijani","nativeName":"\u0622\u0630\u0631\u0628\u0627\u06cc\u062c\u0627\u0646 \u062f\u06cc\u0644\u06cc"},"eu":{"name":"Basque","nativeName":"Euskara"},"be":{"name":"Belarusian","nativeName":"\u0411\u0435\u043b\u0430\u0440\u0443\u0441\u043a\u0430\u044f"},"bg":{"name":"Bulgarian","nativeName":"\u0411\u044a\u043b\u0433\u0430\u0440\u0441\u043a\u0438"},"ca":{"name":"Catalan","nativeName":"Catal\u00e0"},"ny":{"name":"Chichewa","nativeName":"chiChe\u0175a"},"zh-CN":{"name":"Chinese (Simplified)","nativeName":"\u4e2d\u6587\u7b80\u4f53"},"zh-TW":{"name":"Chinese (Traditional)","nativeName":"\u4e2d\u6587\u7e41\u9ad4"},"hr":{"name":"Croatian","nativeName":"Hrvatski"},"cs":{"name":"Czech","nativeName":"\u010ce\u0161tina"},"da":{"name":"Danish","nativeName":"Dansk"},"nl":{"name":"Dutch","nativeName":"Nederlands"},"en":{"name":"English","nativeName":"English"},"et":{"name":"Estonian","nativeName":"Eesti keel"},"tl":{"name":"Filipino","nativeName":"Filipino"},"fi":{"name":"Finnish","nativeName":"Suomi"},"fr":{"name":"French","nativeName":"Fran\u00e7ais"},"gl":{"name":"Galician","nativeName":"Galego"},"ka":{"name":"Georgian","nativeName":"\u10e5\u10d0\u10e0\u10d7\u10e3\u10da\u10d8"},"de":{"name":"German","nativeName":"Deutsch"},"el":{"name":"Greek","nativeName":"\u0395\u03bb\u03bb\u03b7\u03bd\u03b9\u03ba\u03ac"},"ht":{"name":"Haitian Creole","nativeName":"Krey\u00f2l ayisyen"},"iw":{"name":"Hebrew","nativeName":"\u05e2\u05d1\u05e8\u05d9\u05ea"},"hi":{"name":"Hindi","nativeName":"\u0939\u093f\u0928\u094d\u0926\u0940"},"hu":{"name":"Hungarian","nativeName":"Magyar"},"is":{"name":"Icelandic","nativeName":"\u00cdslenska"},"id":{"name":"Indonesian","nativeName":"Bahasa Indonesia"},"ga":{"name":"Irish","nativeName":"Gaeilge"},"it":{"name":"Italian","nativeName":"Italiano"},"ja":{"name":"Japanese","nativeName":"\u65e5\u672c\u8a9e"},"kk":{"name":"Kazakh","nativeName":"\u049a\u0430\u0437\u0430\u049b \u0442\u0456\u043b\u0456"},"ko":{"name":"Korean","nativeName":"\ud55c\uad6d\uc5b4"},"lv":{"name":"Latvian","nativeName":"Latvie\u0161u"},"lt":{"name":"Lithuanian","nativeName":"Lietuvi\u0173 kalba"},"mk":{"name":"Macedonian","nativeName":"\u041c\u0430\u043a\u0435\u0434\u043e\u043d\u0441\u043a\u0438"},"mg":{"name":"Malagasy","nativeName":"Malagasy fiteny"},"ms":{"name":"Malay","nativeName":"Malay"},"ml":{"name":"Malayalam","nativeName":"\u0d2e\u0d32\u0d2f\u0d3e\u0d33\u0d02"},"mt":{"name":"Maltese","nativeName":"Malti"},"my":{"name":"Myanmar (Burmese)","nativeName":"\u1017\u1019\u102c\u1005\u102c"},"no":{"name":"Norwegian","nativeName":"Norsk"},"fa":{"name":"Persian","nativeName":"\u0641\u0627\u0631\u0633\u06cc"},"pl":{"name":"Polish","nativeName":"Polski"},"pt":{"name":"Portuguese","nativeName":"Portugu\u00eas"},"ro":{"name":"Romanian","nativeName":"Rom\u00e2n\u0103"},"ru":{"name":"Russian","nativeName":"\u0420\u0443\u0441\u0441\u043a\u0438\u0439"},"sr":{"name":"Serbian","nativeName":"\u0421\u0440\u043f\u0441\u043a\u0438"},"st":{"name":"Sesotho","nativeName":"Sesotho"},"si":{"name":"Sinhala","nativeName":"\u0dc3\u0dd2\u0d82\u0dc4\u0dbd"},"sk":{"name":"Slovak","nativeName":"Sloven\u010dina"},"sl":{"name":"Slovenian","nativeName":"Slovensko"},"es":{"name":"Spanish","nativeName":"Espa\u00f1ol"},"su":{"name":"Sundanese","nativeName":"Basa Sunda"},"sw":{"name":"Swahili","nativeName":"Kiswahili"},"sv":{"name":"Swedish","nativeName":"Svenska"},"tg":{"name":"Tajik","nativeName":"\u0442\u043e\u04b7\u0438\u043a\u04e3"},"th":{"name":"Thai","nativeName":"\u0e44\u0e17\u0e22"},"tr":{"name":"Turkish","nativeName":"T\u00fcrk\u00e7e"},"uk":{"name":"Ukrainian","nativeName":"\u0423\u043a\u0440\u0430\u0457\u043d\u0441\u044c\u043a\u0430"},"ur":{"name":"Urdu","nativeName":"\u0627\u0631\u062f\u0648"},"uz":{"name":"Uzbek","nativeName":"zbek"},"vi":{"name":"Vietnamese","nativeName":"Ti\u1ebfng Vi\u1ec7t"},"cy":{"name":"Welsh","nativeName":"Cymraeg"},"yi":{"name":"Yiddish","nativeName":"\u05d9\u05d9\u05b4\u05d3\u05d9\u05e9"}}, sortable;
Adrià6ae49e72014-11-09 22:21:19 +01002
3function $(selector) {
4 return document.querySelector(selector);
5}
6
7function $all(selector) {
8 return document.querySelectorAll(selector);
9}
10
11function isEmpty(obj) {
12 return Object.keys(obj).length === 0;
13}
14
15function i18n() {
16 $("#welcome").innerHTML = chrome.i18n.getMessage("options_welcome");
17 $("#introduction").innerHTML = chrome.i18n.getMessage("options_introduction");
18 //$("#languageselectheader").innerHTML = chrome.i18n.getMessage("options_languageselectheader");
19 $("#otheroptionsheader").innerHTML = chrome.i18n.getMessage("options_otheroptionsheader");
20 $("#varioustabs_label").innerHTML = chrome.i18n.getMessage("options_tabsoption_1");
21 $("#uniquetab_label").innerHTML = chrome.i18n.getMessage("options_tabsoption_2");
22 $("#panel_label").innerHTML = chrome.i18n.getMessage("options_tabsoption_3");
Adriàf0e90592014-12-06 16:21:14 +010023 $("#languages_add").innerHTML = chrome.i18n.getMessage("options_addlanguage_addbutton");
Adrià6ae49e72014-11-09 22:21:19 +010024 $("#save").innerHTML = chrome.i18n.getMessage("options_savebutton");
Adriàf0e90592014-12-06 16:21:14 +010025 $("#credits").innerHTML = chrome.i18n.getMessage("options_credits");
26 $("#credits_dialog h3").innerHTML = chrome.i18n.getMessage("options_credits");
27 $("#languages_add_ok").innerHTML = chrome.i18n.getMessage("options_ok");
28 $("#languages_add_cancel").innerHTML = chrome.i18n.getMessage("options_cancel");
29 $("#credits_ok").innerHTML = chrome.i18n.getMessage("options_ok");
30 $("#languages_add_dialog h3").innerHTML = chrome.i18n.getMessage("options_addlanguage");
31 $("#language_label").innerHTML = chrome.i18n.getMessage("options_language_label");
Adrià6ae49e72014-11-09 22:21:19 +010032}
33
34function print_list_modal() {
35 $("#select_language").innerHTML = "";
36 var heysortable = sortable.toArray();
37 for (var language in isoLangs) {
38 if (!inArray(language, heysortable)) {
39 var el = document.createElement('option');
40 el.setAttribute('value', language);
41 el.innerText = isoLangs[language]["name"]+" ("+isoLangs[language]["nativeName"]+")";
42 $("#select_language").appendChild(el);
43 }
44 }
45}
46
47function init() {
48 i18n();
49 chrome.storage.sync.get(null, function(items) {
50 // If no settings are set
51 if (isEmpty(items)) {
52 items = {'translateinto': {}, 'uniquetab': ''};
53 chrome.storage.sync.set({'translateinto': {}, 'uniquetab': ''});
54 }
55
56 // Check the checkbox of the window opening
57 if (items.uniquetab === "yep")
58 $("#uniquetab").checked = true;
59 if (items.uniquetab === "")
60 $("#varioustabs").checked = true;
61 if (items.uniquetab === "panel")
62 $("#panel").checked = true;
63
64 // Add event listeners for certain buttons and links
65 $("#panelsflag").addEventListener('click', function() { event.preventDefault(); chrome.tabs.create({url: 'chrome://flags/#enable-panels'}); });
66 $("#save").addEventListener('click', function() {
67 save_options();
68 });
69
70 // Print selected language list
71 var languages = items.translateinto;
72
73 if (languages) {
74 for (var language_id in languages) {
75 var language = languages[language_id];
76 var el = document.createElement('li');
77 el.setAttribute('data-language', language);
78 el.setAttribute('data-id', language);
79 el.innerHTML = isoLangs[language]["name"]+" ("+isoLangs[language]["nativeName"]+")"+"<span data-language='"+language+"' class='delete'>x</span>";
80 $("#languages").appendChild(el);
81 $("#languages li[data-language="+language+"] .delete").addEventListener('click', function() {
82 $("#languages").removeChild($("li[data-language="+this.getAttribute("data-language")+"]"));
83 print_list_modal();
84 });
85 }
86 }
87
88 // Initiate Sortable
89 sortable = new Sortable($("#languages"), {
90 animation: 150
91 });
92
93 // Handling The Dialog
Adriàd5eae422014-12-06 15:16:44 +010094 $("#languages_add").addEventListener('click', function() { $("dialog#languages_add_dialog").showModal(); $("#select_language").focus(); });
Adriàe96a76c2014-12-06 14:59:17 +010095 $("#languages_add_cancel").addEventListener('click', function() { $("dialog#languages_add_dialog").close(); });
Adrià6ae49e72014-11-09 22:21:19 +010096 $("#languages_add_ok").addEventListener('click', function() {
97 var el = document.createElement('li');
98 var language = $("#select_language").value;
99 if (inArray(language, sortable.toArray())) {
100 return;
101 }
102 el.setAttribute('data-language', language);
103 el.setAttribute('data-id', language);
104 el.innerHTML = isoLangs[language]["name"]+" ("+isoLangs[language]["nativeName"]+")"+"<span data-language='"+language+"' class='delete'>x</span>";
105 $("#languages").appendChild(el);
106 var selection = $("#select_language option[value="+language+"]");
107 selection.parentNode.removeChild(selection);
108 $("#languages li[data-id="+language+"] .delete").addEventListener('click', function() {
109 $("#languages").removeChild($("li[data-language="+this.getAttribute("data-language")+"]"));
110 print_list_modal();
111 });
112 $("dialog").close();
113 });
114
Adriàe96a76c2014-12-06 14:59:17 +0100115 // About credits...
116 var xhr = new XMLHttpRequest();
117 xhr.onreadystatechange = function() {
118 if (xhr.readyState == 4 && xhr.status == 200) {
119 var json = JSON.parse(xhr.responseText), printhtml = "";
120 for (var item in json) {
121 printhtml += "<div>";
122 if (json[item].url) {
Adriàf0e90592014-12-06 16:21:14 +0100123 printhtml += "<a href='"+json[item].url+"' class='homepage' target='_blank'>"+chrome.i18n.getMessage("options_credits_homepage")+"</a>";
Adriàe96a76c2014-12-06 14:59:17 +0100124 }
125 printhtml += "<h4>"+json[item].name+"</h4>";
126 if (json[item].author) {
Adriàf0e90592014-12-06 16:21:14 +0100127 printhtml += "<p class='author'>"+chrome.i18n.getMessage("options_credits_by")+" "+json[item].author;
Adriàe96a76c2014-12-06 14:59:17 +0100128 }
129 if (json[item].license) {
130 printhtml += " – "+json[item].license+"</p>";
131 } else {
132 printhtml += "</p>";
133 }
134 printhtml += "</div>";
135 }
136 $("dialog#credits_dialog .content_area").innerHTML = printhtml;
137 window.onhashchange = function() {
138 if (location.hash == "#credits") {
139 $("dialog#credits_dialog").showModal();
Adriàd5eae422014-12-06 15:16:44 +0100140 $("#credits_ok").focus();
Adriàe96a76c2014-12-06 14:59:17 +0100141 }
142 }
143 if (location.hash == "#credits") {
144 $("dialog#credits_dialog").showModal();
Adriàd5eae422014-12-06 15:16:44 +0100145 $("#credits_ok").focus();
Adriàe96a76c2014-12-06 14:59:17 +0100146 }
Adriàd5eae422014-12-06 15:16:44 +0100147 $("#credits_ok").addEventListener('click', function() { $("dialog#credits_dialog").close(); });
148 $("dialog#credits_dialog").addEventListener('close', function() { history.pushState("", document.title, window.location.pathname + window.location.search); });
Adriàe96a76c2014-12-06 14:59:17 +0100149 }
150 }
151 xhr.open("GET", "json/credits.json", true);
152 xhr.send();
153
Adrià6ae49e72014-11-09 22:21:19 +0100154 // Print language list in the modal dialog
155 print_list_modal();
156 });
157}
158
159function save_options() {
160 var languages = document.getElementById("languages");
161 var options = {"uniquetab": "", "translateinto": {}};
162
163 options.uniquetab = radio_selected("uniquetab");
164
165 var selected_languages = sortable.toArray();
166
167 var i = 0;
168 for (var language_id in selected_languages) {
169 var language = selected_languages[language_id];
170 options.translateinto[i] = language;
171 i++;
172 }
173
174 chrome.storage.sync.set(options, function() {
175 var background = chrome.extension.getBackgroundPage();
176
177 background.translator_tab = false;
178 background.translator_window = false;
179 window.close();
180 });
181
182 // We don't need the following code because the background.js is already listening to changes in chrome.sync ;-) Yeeey!
183 //chrome.extension.getBackgroundPage().createmenus(JSON.stringify(options));
184}
185
186function toObject(arr) {
187 var rv = {};
188 for (var i = 0; i < arr.length; ++i)
189 if (arr[i] !== undefined) rv[i] = arr[i];
190 return rv;
191}
192
193function radio_selected(a) {
194 var elements = document.getElementsByName(a);
195
196 for (var i=0; i<elements.length; i++)
197 if (elements[i].checked) return elements[i].value;
198}
199
200function inArray(needle, haystack) {
201 var length = haystack.length;
202 for(var i = 0; i < length; i++) {
203 if(haystack[i] == needle) return true;
204 }
205 return false;
206}
207
208window.addEventListener('load', init);