1 # -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 # ***** BEGIN LICENSE BLOCK *****
3 # Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 # The contents of this file are subject to the Mozilla Public License Version
6 # 1.1 (the "License"); you may not use this file except in compliance with
7 # the License. You may obtain a copy of the License at
8 # http://www.mozilla.org/MPL/
10 # Software distributed under the License is distributed on an "AS IS" basis,
11 # WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 # for the specific language governing rights and limitations under the
15 # The Original Code is the Firefox Preferences System.
17 # The Initial Developer of the Original Code is
19 # Portions created by the Initial Developer are Copyright (C) 2005
20 # the Initial Developer. All Rights Reserved.
23 # Ben Goodger <ben@mozilla.org>
24 # Adrian Havill <havill@redhat.com>
25 # Steffen Wilberg <steffen.wilberg@web.de>
27 # Alternatively, the contents of this file may be used under the terms of
28 # either the GNU General Public License Version 2 or later (the "GPL"), or
29 # the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
30 # in which case the provisions of the GPL or the LGPL are applicable instead
31 # of those above. If you wish to allow use of your version of this file only
32 # under the terms of either the GPL or the LGPL, and not to allow others to
33 # use your version of this file under the terms of the MPL, indicate your
34 # decision by deleting the provisions above and replace them with the notice
35 # and other provisions required by the GPL or the LGPL. If you do not delete
36 # the provisions above, a recipient may use your version of this file under
37 # the terms of any one of the MPL, the GPL or the LGPL.
39 # ***** END LICENSE BLOCK *****
43 _availableLanguagesList : [],
44 _acceptLanguages : { },
46 _selectedItemID :
null,
50 if (!this._availableLanguagesList.length)
51 this._loadAvailableLanguages();
54 get _activeLanguages()
56 return document.getElementById(
"activeLanguages");
59 get _availableLanguages()
61 return document.getElementById(
"availableLanguages");
64 _loadAvailableLanguages:
function ()
71 var bundleAccepted = document.getElementById(
"bundleAccepted");
72 var bundleRegions = document.getElementById(
"bundleRegions");
73 var bundleLanguages = document.getElementById(
"bundleLanguages");
74 var bundlePreferences = document.getElementById(
"bundlePreferences");
76 function LanguageInfo(
aName, aABCD, aIsVisible)
84 var
strings = bundleAccepted.strings;
85 while (strings.hasMoreElements()) {
86 var currString = strings.getNext();
87 if (!(currString instanceof Components.interfaces.nsIPropertyElement))
90 var
property = currString.key.split(
".");
91 if (property[1] ==
"accept") {
92 var abCD =
property[0];
93 var abCDPairs = abCD.split(
"-");
94 var useABCDFormat = abCDPairs.length > 1;
95 var ab = useABCDFormat ? abCDPairs[0] : abCD;
96 var cd = useABCDFormat ? abCDPairs[1] :
"";
100 language = bundleLanguages.getString(ab);
102 catch (e) {
continue; };
107 region = bundleRegions.getString(cd);
109 catch (e) {
continue; }
114 name = bundlePreferences.getFormattedString(
"languageRegionCodeFormat",
115 [language, region, abCD]);
117 name = bundlePreferences.getFormattedString(
"languageCodeFormat",
121 var
isVisible = currString.value ==
"true" &&
122 (!(abCD in this._acceptLanguages) || !this._acceptLanguages[abCD]);
123 var li =
new LanguageInfo(name, abCD, isVisible);
124 this._availableLanguagesList.push(li);
129 this._buildAvailableLanguageList();
132 _buildAvailableLanguageList:
function ()
134 var availableLanguagesPopup = document.getElementById(
"availableLanguagesPopup");
135 while (availableLanguagesPopup.hasChildNodes())
136 availableLanguagesPopup.removeChild(availableLanguagesPopup.firstChild);
139 this._availableLanguagesList.sort(
function (a, b) {
140 return a.name.localeCompare(b.name);
144 for (var
i = 0;
i < this._availableLanguagesList.length; ++
i) {
145 var abCD = this._availableLanguagesList[
i].abcd;
146 if (this._availableLanguagesList[
i].
isVisible &&
147 (!(abCD in this._acceptLanguages) || !this._acceptLanguages[abCD])) {
148 var menuitem = document.createElement(
"menuitem");
149 menuitem.id = this._availableLanguagesList[
i].abcd;
150 availableLanguagesPopup.appendChild(menuitem);
151 menuitem.setAttribute(
"label", this._availableLanguagesList[
i].
name);
156 readAcceptLanguages:
function ()
158 while (this._activeLanguages.hasChildNodes())
159 this._activeLanguages.removeChild(
this._activeLanguages.firstChild);
161 var selectedIndex = 0;
162 var preference = document.getElementById(
"intl.accept_languages");
163 if (preference.value ==
"")
165 var languages = preference.value.toLowerCase().split(/\s*,\s*/);
166 for (var
i = 0;
i < languages.length; ++
i) {
167 var
name = this._getLanguageName(languages[
i]);
169 name =
"[" + languages[
i] +
"]";
170 var listitem = document.createElement(
"listitem");
171 listitem.id = languages[
i];
172 if (languages[i] == this._selectedItemID)
174 this._activeLanguages.appendChild(listitem);
175 listitem.setAttribute(
"label", name);
179 this._acceptLanguages[languages[
i]] =
true;
182 if (this._activeLanguages.childNodes.length > 0) {
183 this._activeLanguages.ensureIndexIsVisible(selectedIndex);
184 this._activeLanguages.selectedIndex = selectedIndex;
190 writeAcceptLanguages:
function ()
195 onAvailableLanguageSelect:
function ()
197 var addButton = document.getElementById(
"addButton");
198 addButton.disabled =
false;
200 this._availableLanguages.removeAttribute(
"accesskey");
203 addLanguage:
function ()
205 var selectedID = this._availableLanguages.selectedItem.id;
206 var preference = document.getElementById(
"intl.accept_languages");
207 var arrayOfPrefs = preference.value.toLowerCase().split(/\s*,\s*/);
208 for (var
i = 0;
i < arrayOfPrefs.length; ++
i ){
209 if (arrayOfPrefs[
i] == selectedID)
213 this._selectedItemID = selectedID;
215 if (preference.value ==
"")
216 preference.value = selectedID;
218 preference.value +=
"," + selectedID;
220 this._acceptLanguages[selectedID] =
true;
221 this._availableLanguages.selectedItem =
null;
224 this._buildAvailableLanguageList();
226 this._availableLanguages.setAttribute(
"label", this._availableLanguages.getAttribute(
"label2"));
229 removeLanguage:
function ()
232 var languagesArray = [];
233 for (var
i = 0;
i < this._activeLanguages.childNodes.length; ++
i) {
234 var item = this._activeLanguages.childNodes[
i];
236 languagesArray.push(item.id);
238 this._acceptLanguages[item.id] =
false;
240 var
string = languagesArray.join(
",");
243 var selection = this._activeLanguages.selectedItems;
244 var lastSelected = selection[selection.length-1];
245 var selectItem = lastSelected.nextSibling || lastSelected.previousSibling;
246 selectItem = selectItem ? selectItem.id :
null;
248 this._selectedItemID = selectItem;
251 var preference = document.getElementById(
"intl.accept_languages");
252 preference.value =
string;
254 this._buildAvailableLanguageList();
257 _getLanguageName:
function (aABCD)
259 if (!this._availableLanguagesList.length)
260 this._loadAvailableLanguages();
261 for (var
i = 0;
i < this._availableLanguagesList.length; ++
i) {
262 if (aABCD == this._availableLanguagesList[
i].abcd)
263 return this._availableLanguagesList[
i].name;
270 var
selectedItem = this._activeLanguages.selectedItems[0];
271 var previousItem = selectedItem.previousSibling;
274 for (var
i = 0;
i < this._activeLanguages.childNodes.length; ++
i) {
275 var item = this._activeLanguages.childNodes[
i];
276 string += (
i == 0 ?
"" :
",");
277 if (item.id == previousItem.id)
278 string += selectedItem.id;
279 else if (item.id == selectedItem.id)
280 string += previousItem.id;
285 this._selectedItemID = selectedItem.id;
288 var preference = document.getElementById(
"intl.accept_languages");
289 preference.value =
string;
292 moveDown:
function ()
294 var
selectedItem = this._activeLanguages.selectedItems[0];
295 var nextItem = selectedItem.nextSibling;
298 for (var
i = 0;
i < this._activeLanguages.childNodes.length; ++
i) {
299 var item = this._activeLanguages.childNodes[
i];
300 string += (
i == 0 ?
"" :
",");
301 if (item.id == nextItem.id)
302 string += selectedItem.id;
303 else if (item.id == selectedItem.id)
304 string += nextItem.id;
309 this._selectedItemID = selectedItem.id;
312 var preference = document.getElementById(
"intl.accept_languages");
313 preference.value =
string;
316 onLanguageSelect:
function ()
318 var upButton = document.getElementById(
"up");
319 var downButton = document.getElementById(
"down");
320 var removeButton = document.getElementById(
"remove");
321 switch (this._activeLanguages.selectedCount) {
323 upButton.disabled = downButton.disabled = removeButton.disabled =
true;
326 upButton.disabled = this._activeLanguages.selectedIndex == 0;
327 downButton.disabled = this._activeLanguages.selectedIndex == this._activeLanguages.childNodes.length - 1;
328 removeButton.disabled =
false;
331 upButton.disabled =
true;
332 downButton.disabled =
true;
333 removeButton.disabled =
false;
function isVisible(element)
_getSelectedPageStyle s i