Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
michael@0 | 2 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 5 | |
michael@0 | 6 | var gLanguagesDialog = { |
michael@0 | 7 | |
michael@0 | 8 | _availableLanguagesList : [], |
michael@0 | 9 | _acceptLanguages : { }, |
michael@0 | 10 | |
michael@0 | 11 | _selectedItemID : null, |
michael@0 | 12 | |
michael@0 | 13 | init: function () |
michael@0 | 14 | { |
michael@0 | 15 | if (!this._availableLanguagesList.length) |
michael@0 | 16 | this._loadAvailableLanguages(); |
michael@0 | 17 | }, |
michael@0 | 18 | |
michael@0 | 19 | get _activeLanguages() |
michael@0 | 20 | { |
michael@0 | 21 | return document.getElementById("activeLanguages"); |
michael@0 | 22 | }, |
michael@0 | 23 | |
michael@0 | 24 | get _availableLanguages() |
michael@0 | 25 | { |
michael@0 | 26 | return document.getElementById("availableLanguages"); |
michael@0 | 27 | }, |
michael@0 | 28 | |
michael@0 | 29 | _loadAvailableLanguages: function () |
michael@0 | 30 | { |
michael@0 | 31 | // This is a parser for: resource://gre/res/language.properties |
michael@0 | 32 | // The file is formatted like so: |
michael@0 | 33 | // ab[-cd].accept=true|false |
michael@0 | 34 | // ab = language |
michael@0 | 35 | // cd = region |
michael@0 | 36 | var bundleAccepted = document.getElementById("bundleAccepted"); |
michael@0 | 37 | var bundleRegions = document.getElementById("bundleRegions"); |
michael@0 | 38 | var bundleLanguages = document.getElementById("bundleLanguages"); |
michael@0 | 39 | var bundlePreferences = document.getElementById("bundlePreferences"); |
michael@0 | 40 | |
michael@0 | 41 | function LanguageInfo(aName, aABCD, aIsVisible) |
michael@0 | 42 | { |
michael@0 | 43 | this.name = aName; |
michael@0 | 44 | this.abcd = aABCD; |
michael@0 | 45 | this.isVisible = aIsVisible; |
michael@0 | 46 | } |
michael@0 | 47 | |
michael@0 | 48 | // 1) Read the available languages out of language.properties |
michael@0 | 49 | var strings = bundleAccepted.strings; |
michael@0 | 50 | while (strings.hasMoreElements()) { |
michael@0 | 51 | var currString = strings.getNext(); |
michael@0 | 52 | if (!(currString instanceof Components.interfaces.nsIPropertyElement)) |
michael@0 | 53 | break; |
michael@0 | 54 | |
michael@0 | 55 | var property = currString.key.split("."); // ab[-cd].accept |
michael@0 | 56 | if (property[1] == "accept") { |
michael@0 | 57 | var abCD = property[0]; |
michael@0 | 58 | var abCDPairs = abCD.split("-"); // ab[-cd] |
michael@0 | 59 | var useABCDFormat = abCDPairs.length > 1; |
michael@0 | 60 | var ab = useABCDFormat ? abCDPairs[0] : abCD; |
michael@0 | 61 | var cd = useABCDFormat ? abCDPairs[1] : ""; |
michael@0 | 62 | if (ab) { |
michael@0 | 63 | var language = ""; |
michael@0 | 64 | try { |
michael@0 | 65 | language = bundleLanguages.getString(ab); |
michael@0 | 66 | } |
michael@0 | 67 | catch (e) { continue; }; |
michael@0 | 68 | |
michael@0 | 69 | var region = ""; |
michael@0 | 70 | if (useABCDFormat) { |
michael@0 | 71 | try { |
michael@0 | 72 | region = bundleRegions.getString(cd); |
michael@0 | 73 | } |
michael@0 | 74 | catch (e) { continue; } |
michael@0 | 75 | } |
michael@0 | 76 | |
michael@0 | 77 | var name = ""; |
michael@0 | 78 | if (useABCDFormat) |
michael@0 | 79 | name = bundlePreferences.getFormattedString("languageRegionCodeFormat", |
michael@0 | 80 | [language, region, abCD]); |
michael@0 | 81 | else |
michael@0 | 82 | name = bundlePreferences.getFormattedString("languageCodeFormat", |
michael@0 | 83 | [language, abCD]); |
michael@0 | 84 | |
michael@0 | 85 | if (name && abCD) { |
michael@0 | 86 | var isVisible = currString.value == "true" && |
michael@0 | 87 | (!(abCD in this._acceptLanguages) || !this._acceptLanguages[abCD]); |
michael@0 | 88 | var li = new LanguageInfo(name, abCD, isVisible); |
michael@0 | 89 | this._availableLanguagesList.push(li); |
michael@0 | 90 | } |
michael@0 | 91 | } |
michael@0 | 92 | } |
michael@0 | 93 | } |
michael@0 | 94 | this._buildAvailableLanguageList(); |
michael@0 | 95 | }, |
michael@0 | 96 | |
michael@0 | 97 | _buildAvailableLanguageList: function () |
michael@0 | 98 | { |
michael@0 | 99 | var availableLanguagesPopup = document.getElementById("availableLanguagesPopup"); |
michael@0 | 100 | while (availableLanguagesPopup.hasChildNodes()) |
michael@0 | 101 | availableLanguagesPopup.removeChild(availableLanguagesPopup.firstChild); |
michael@0 | 102 | |
michael@0 | 103 | // Sort the list of languages by name |
michael@0 | 104 | this._availableLanguagesList.sort(function (a, b) { |
michael@0 | 105 | return a.name.localeCompare(b.name); |
michael@0 | 106 | }); |
michael@0 | 107 | |
michael@0 | 108 | // Load the UI with the data |
michael@0 | 109 | for (var i = 0; i < this._availableLanguagesList.length; ++i) { |
michael@0 | 110 | var abCD = this._availableLanguagesList[i].abcd; |
michael@0 | 111 | if (this._availableLanguagesList[i].isVisible && |
michael@0 | 112 | (!(abCD in this._acceptLanguages) || !this._acceptLanguages[abCD])) { |
michael@0 | 113 | var menuitem = document.createElement("menuitem"); |
michael@0 | 114 | menuitem.id = this._availableLanguagesList[i].abcd; |
michael@0 | 115 | availableLanguagesPopup.appendChild(menuitem); |
michael@0 | 116 | menuitem.setAttribute("label", this._availableLanguagesList[i].name); |
michael@0 | 117 | } |
michael@0 | 118 | } |
michael@0 | 119 | }, |
michael@0 | 120 | |
michael@0 | 121 | readAcceptLanguages: function () |
michael@0 | 122 | { |
michael@0 | 123 | while (this._activeLanguages.hasChildNodes()) |
michael@0 | 124 | this._activeLanguages.removeChild(this._activeLanguages.firstChild); |
michael@0 | 125 | |
michael@0 | 126 | var selectedIndex = 0; |
michael@0 | 127 | var preference = document.getElementById("intl.accept_languages"); |
michael@0 | 128 | if (preference.value == "") |
michael@0 | 129 | return undefined; |
michael@0 | 130 | var languages = preference.value.toLowerCase().split(/\s*,\s*/); |
michael@0 | 131 | for (var i = 0; i < languages.length; ++i) { |
michael@0 | 132 | var name = this._getLanguageName(languages[i]); |
michael@0 | 133 | if (!name) |
michael@0 | 134 | name = "[" + languages[i] + "]"; |
michael@0 | 135 | var listitem = document.createElement("listitem"); |
michael@0 | 136 | listitem.id = languages[i]; |
michael@0 | 137 | if (languages[i] == this._selectedItemID) |
michael@0 | 138 | selectedIndex = i; |
michael@0 | 139 | this._activeLanguages.appendChild(listitem); |
michael@0 | 140 | listitem.setAttribute("label", name); |
michael@0 | 141 | |
michael@0 | 142 | // Hash this language as an "Active" language so we don't |
michael@0 | 143 | // show it in the list that can be added. |
michael@0 | 144 | this._acceptLanguages[languages[i]] = true; |
michael@0 | 145 | } |
michael@0 | 146 | |
michael@0 | 147 | if (this._activeLanguages.childNodes.length > 0) { |
michael@0 | 148 | this._activeLanguages.ensureIndexIsVisible(selectedIndex); |
michael@0 | 149 | this._activeLanguages.selectedIndex = selectedIndex; |
michael@0 | 150 | } |
michael@0 | 151 | |
michael@0 | 152 | return undefined; |
michael@0 | 153 | }, |
michael@0 | 154 | |
michael@0 | 155 | writeAcceptLanguages: function () |
michael@0 | 156 | { |
michael@0 | 157 | return undefined; |
michael@0 | 158 | }, |
michael@0 | 159 | |
michael@0 | 160 | onAvailableLanguageSelect: function () |
michael@0 | 161 | { |
michael@0 | 162 | var addButton = document.getElementById("addButton"); |
michael@0 | 163 | addButton.disabled = false; |
michael@0 | 164 | |
michael@0 | 165 | this._availableLanguages.removeAttribute("accesskey"); |
michael@0 | 166 | }, |
michael@0 | 167 | |
michael@0 | 168 | addLanguage: function () |
michael@0 | 169 | { |
michael@0 | 170 | var selectedID = this._availableLanguages.selectedItem.id; |
michael@0 | 171 | var preference = document.getElementById("intl.accept_languages"); |
michael@0 | 172 | var arrayOfPrefs = preference.value.toLowerCase().split(/\s*,\s*/); |
michael@0 | 173 | for (var i = 0; i < arrayOfPrefs.length; ++i ){ |
michael@0 | 174 | if (arrayOfPrefs[i] == selectedID) |
michael@0 | 175 | return; |
michael@0 | 176 | } |
michael@0 | 177 | |
michael@0 | 178 | this._selectedItemID = selectedID; |
michael@0 | 179 | |
michael@0 | 180 | if (preference.value == "") |
michael@0 | 181 | preference.value = selectedID; |
michael@0 | 182 | else { |
michael@0 | 183 | arrayOfPrefs.unshift(selectedID); |
michael@0 | 184 | preference.value = arrayOfPrefs.join(","); |
michael@0 | 185 | } |
michael@0 | 186 | |
michael@0 | 187 | this._acceptLanguages[selectedID] = true; |
michael@0 | 188 | this._availableLanguages.selectedItem = null; |
michael@0 | 189 | |
michael@0 | 190 | // Rebuild the available list with the added item removed... |
michael@0 | 191 | this._buildAvailableLanguageList(); |
michael@0 | 192 | |
michael@0 | 193 | this._availableLanguages.setAttribute("label", this._availableLanguages.getAttribute("label2")); |
michael@0 | 194 | }, |
michael@0 | 195 | |
michael@0 | 196 | removeLanguage: function () |
michael@0 | 197 | { |
michael@0 | 198 | // Build the new preference value string. |
michael@0 | 199 | var languagesArray = []; |
michael@0 | 200 | for (var i = 0; i < this._activeLanguages.childNodes.length; ++i) { |
michael@0 | 201 | var item = this._activeLanguages.childNodes[i]; |
michael@0 | 202 | if (!item.selected) |
michael@0 | 203 | languagesArray.push(item.id); |
michael@0 | 204 | else |
michael@0 | 205 | this._acceptLanguages[item.id] = false; |
michael@0 | 206 | } |
michael@0 | 207 | var string = languagesArray.join(","); |
michael@0 | 208 | |
michael@0 | 209 | // Get the item to select after the remove operation completes. |
michael@0 | 210 | var selection = this._activeLanguages.selectedItems; |
michael@0 | 211 | var lastSelected = selection[selection.length-1]; |
michael@0 | 212 | var selectItem = lastSelected.nextSibling || lastSelected.previousSibling; |
michael@0 | 213 | selectItem = selectItem ? selectItem.id : null; |
michael@0 | 214 | |
michael@0 | 215 | this._selectedItemID = selectItem; |
michael@0 | 216 | |
michael@0 | 217 | // Update the preference and force a UI rebuild |
michael@0 | 218 | var preference = document.getElementById("intl.accept_languages"); |
michael@0 | 219 | preference.value = string; |
michael@0 | 220 | |
michael@0 | 221 | this._buildAvailableLanguageList(); |
michael@0 | 222 | }, |
michael@0 | 223 | |
michael@0 | 224 | _getLanguageName: function (aABCD) |
michael@0 | 225 | { |
michael@0 | 226 | if (!this._availableLanguagesList.length) |
michael@0 | 227 | this._loadAvailableLanguages(); |
michael@0 | 228 | for (var i = 0; i < this._availableLanguagesList.length; ++i) { |
michael@0 | 229 | if (aABCD == this._availableLanguagesList[i].abcd) |
michael@0 | 230 | return this._availableLanguagesList[i].name; |
michael@0 | 231 | } |
michael@0 | 232 | return ""; |
michael@0 | 233 | }, |
michael@0 | 234 | |
michael@0 | 235 | moveUp: function () |
michael@0 | 236 | { |
michael@0 | 237 | var selectedItem = this._activeLanguages.selectedItems[0]; |
michael@0 | 238 | var previousItem = selectedItem.previousSibling; |
michael@0 | 239 | |
michael@0 | 240 | var string = ""; |
michael@0 | 241 | for (var i = 0; i < this._activeLanguages.childNodes.length; ++i) { |
michael@0 | 242 | var item = this._activeLanguages.childNodes[i]; |
michael@0 | 243 | string += (i == 0 ? "" : ","); |
michael@0 | 244 | if (item.id == previousItem.id) |
michael@0 | 245 | string += selectedItem.id; |
michael@0 | 246 | else if (item.id == selectedItem.id) |
michael@0 | 247 | string += previousItem.id; |
michael@0 | 248 | else |
michael@0 | 249 | string += item.id; |
michael@0 | 250 | } |
michael@0 | 251 | |
michael@0 | 252 | this._selectedItemID = selectedItem.id; |
michael@0 | 253 | |
michael@0 | 254 | // Update the preference and force a UI rebuild |
michael@0 | 255 | var preference = document.getElementById("intl.accept_languages"); |
michael@0 | 256 | preference.value = string; |
michael@0 | 257 | }, |
michael@0 | 258 | |
michael@0 | 259 | moveDown: function () |
michael@0 | 260 | { |
michael@0 | 261 | var selectedItem = this._activeLanguages.selectedItems[0]; |
michael@0 | 262 | var nextItem = selectedItem.nextSibling; |
michael@0 | 263 | |
michael@0 | 264 | var string = ""; |
michael@0 | 265 | for (var i = 0; i < this._activeLanguages.childNodes.length; ++i) { |
michael@0 | 266 | var item = this._activeLanguages.childNodes[i]; |
michael@0 | 267 | string += (i == 0 ? "" : ","); |
michael@0 | 268 | if (item.id == nextItem.id) |
michael@0 | 269 | string += selectedItem.id; |
michael@0 | 270 | else if (item.id == selectedItem.id) |
michael@0 | 271 | string += nextItem.id; |
michael@0 | 272 | else |
michael@0 | 273 | string += item.id; |
michael@0 | 274 | } |
michael@0 | 275 | |
michael@0 | 276 | this._selectedItemID = selectedItem.id; |
michael@0 | 277 | |
michael@0 | 278 | // Update the preference and force a UI rebuild |
michael@0 | 279 | var preference = document.getElementById("intl.accept_languages"); |
michael@0 | 280 | preference.value = string; |
michael@0 | 281 | }, |
michael@0 | 282 | |
michael@0 | 283 | onLanguageSelect: function () |
michael@0 | 284 | { |
michael@0 | 285 | var upButton = document.getElementById("up"); |
michael@0 | 286 | var downButton = document.getElementById("down"); |
michael@0 | 287 | var removeButton = document.getElementById("remove"); |
michael@0 | 288 | switch (this._activeLanguages.selectedCount) { |
michael@0 | 289 | case 0: |
michael@0 | 290 | upButton.disabled = downButton.disabled = removeButton.disabled = true; |
michael@0 | 291 | break; |
michael@0 | 292 | case 1: |
michael@0 | 293 | upButton.disabled = this._activeLanguages.selectedIndex == 0; |
michael@0 | 294 | downButton.disabled = this._activeLanguages.selectedIndex == this._activeLanguages.childNodes.length - 1; |
michael@0 | 295 | removeButton.disabled = false; |
michael@0 | 296 | break; |
michael@0 | 297 | default: |
michael@0 | 298 | upButton.disabled = true; |
michael@0 | 299 | downButton.disabled = true; |
michael@0 | 300 | removeButton.disabled = false; |
michael@0 | 301 | } |
michael@0 | 302 | } |
michael@0 | 303 | }; |
michael@0 | 304 |