languages.js
Go to the documentation of this file.
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
4 #
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/
9 #
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
13 # License.
14 #
15 # The Original Code is the Firefox Preferences System.
16 #
17 # The Initial Developer of the Original Code is
18 # Ben Goodger.
19 # Portions created by the Initial Developer are Copyright (C) 2005
20 # the Initial Developer. All Rights Reserved.
21 #
22 # Contributor(s):
23 # Ben Goodger <ben@mozilla.org>
24 # Adrian Havill <havill@redhat.com>
25 # Steffen Wilberg <steffen.wilberg@web.de>
26 #
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.
38 #
39 # ***** END LICENSE BLOCK *****
40 
42 
43  _availableLanguagesList : [],
44  _acceptLanguages : { },
45 
46  _selectedItemID : null,
47 
48  init: function ()
49  {
50  if (!this._availableLanguagesList.length)
51  this._loadAvailableLanguages();
52  },
53 
54  get _activeLanguages()
55  {
56  return document.getElementById("activeLanguages");
57  },
58 
59  get _availableLanguages()
60  {
61  return document.getElementById("availableLanguages");
62  },
63 
64  _loadAvailableLanguages: function ()
65  {
66  // This is a parser for: resource://gre/res/language.properties
67  // The file is formatted like so:
68  // ab[-cd].accept=true|false
69  // ab = language
70  // cd = region
71  var bundleAccepted = document.getElementById("bundleAccepted");
72  var bundleRegions = document.getElementById("bundleRegions");
73  var bundleLanguages = document.getElementById("bundleLanguages");
74  var bundlePreferences = document.getElementById("bundlePreferences");
75 
76  function LanguageInfo(aName, aABCD, aIsVisible)
77  {
78  this.name = aName;
79  this.abcd = aABCD;
80  this.isVisible = aIsVisible;
81  }
82 
83  // 1) Read the available languages out of language.properties
84  var strings = bundleAccepted.strings;
85  while (strings.hasMoreElements()) {
86  var currString = strings.getNext();
87  if (!(currString instanceof Components.interfaces.nsIPropertyElement))
88  break;
89 
90  var property = currString.key.split("."); // ab[-cd].accept
91  if (property[1] == "accept") {
92  var abCD = property[0];
93  var abCDPairs = abCD.split("-"); // ab[-cd]
94  var useABCDFormat = abCDPairs.length > 1;
95  var ab = useABCDFormat ? abCDPairs[0] : abCD;
96  var cd = useABCDFormat ? abCDPairs[1] : "";
97  if (ab) {
98  var language = "";
99  try {
100  language = bundleLanguages.getString(ab);
101  }
102  catch (e) { continue; };
103 
104  var region = "";
105  if (useABCDFormat) {
106  try {
107  region = bundleRegions.getString(cd);
108  }
109  catch (e) { continue; }
110  }
111 
112  var name = "";
113  if (useABCDFormat)
114  name = bundlePreferences.getFormattedString("languageRegionCodeFormat",
115  [language, region, abCD]);
116  else
117  name = bundlePreferences.getFormattedString("languageCodeFormat",
118  [language, abCD]);
119 
120  if (name && abCD) {
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);
125  }
126  }
127  }
128  }
129  this._buildAvailableLanguageList();
130  },
131 
132  _buildAvailableLanguageList: function ()
133  {
134  var availableLanguagesPopup = document.getElementById("availableLanguagesPopup");
135  while (availableLanguagesPopup.hasChildNodes())
136  availableLanguagesPopup.removeChild(availableLanguagesPopup.firstChild);
137 
138  // Sort the list of languages by name
139  this._availableLanguagesList.sort(function (a, b) {
140  return a.name.localeCompare(b.name);
141  });
142 
143  // Load the UI with the data
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);
152  }
153  }
154  },
155 
156  readAcceptLanguages: function ()
157  {
158  while (this._activeLanguages.hasChildNodes())
159  this._activeLanguages.removeChild(this._activeLanguages.firstChild);
160 
161  var selectedIndex = 0;
162  var preference = document.getElementById("intl.accept_languages");
163  if (preference.value == "")
164  return undefined;
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]);
168  if (!name)
169  name = "[" + languages[i] + "]";
170  var listitem = document.createElement("listitem");
171  listitem.id = languages[i];
172  if (languages[i] == this._selectedItemID)
173  selectedIndex = i;
174  this._activeLanguages.appendChild(listitem);
175  listitem.setAttribute("label", name);
176 
177  // Hash this language as an "Active" language so we don't
178  // show it in the list that can be added.
179  this._acceptLanguages[languages[i]] = true;
180  }
181 
182  if (this._activeLanguages.childNodes.length > 0) {
183  this._activeLanguages.ensureIndexIsVisible(selectedIndex);
184  this._activeLanguages.selectedIndex = selectedIndex;
185  }
186 
187  return undefined;
188  },
189 
190  writeAcceptLanguages: function ()
191  {
192  return undefined;
193  },
194 
195  onAvailableLanguageSelect: function ()
196  {
197  var addButton = document.getElementById("addButton");
198  addButton.disabled = false;
199 
200  this._availableLanguages.removeAttribute("accesskey");
201  },
202 
203  addLanguage: function ()
204  {
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)
210  return;
211  }
212 
213  this._selectedItemID = selectedID;
214 
215  if (preference.value == "")
216  preference.value = selectedID;
217  else
218  preference.value += "," + selectedID;
219 
220  this._acceptLanguages[selectedID] = true;
221  this._availableLanguages.selectedItem = null;
222 
223  // Reuild the available list with the added item removed...
224  this._buildAvailableLanguageList();
225 
226  this._availableLanguages.setAttribute("label", this._availableLanguages.getAttribute("label2"));
227  },
228 
229  removeLanguage: function ()
230  {
231  // Build the new preference value string.
232  var languagesArray = [];
233  for (var i = 0; i < this._activeLanguages.childNodes.length; ++i) {
234  var item = this._activeLanguages.childNodes[i];
235  if (!item.selected)
236  languagesArray.push(item.id);
237  else
238  this._acceptLanguages[item.id] = false;
239  }
240  var string = languagesArray.join(",");
241 
242  // Get the item to select after the remove operation completes.
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;
247 
248  this._selectedItemID = selectItem;
249 
250  // Update the preference and force a UI rebuild
251  var preference = document.getElementById("intl.accept_languages");
252  preference.value = string;
253 
254  this._buildAvailableLanguageList();
255  },
256 
257  _getLanguageName: function (aABCD)
258  {
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;
264  }
265  return "";
266  },
267 
268  moveUp: function ()
269  {
270  var selectedItem = this._activeLanguages.selectedItems[0];
271  var previousItem = selectedItem.previousSibling;
272 
273  var string = "";
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;
281  else
282  string += item.id;
283  }
284 
285  this._selectedItemID = selectedItem.id;
286 
287  // Update the preference and force a UI rebuild
288  var preference = document.getElementById("intl.accept_languages");
289  preference.value = string;
290  },
291 
292  moveDown: function ()
293  {
294  var selectedItem = this._activeLanguages.selectedItems[0];
295  var nextItem = selectedItem.nextSibling;
296 
297  var string = "";
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;
305  else
306  string += item.id;
307  }
308 
309  this._selectedItemID = selectedItem.id;
310 
311  // Update the preference and force a UI rebuild
312  var preference = document.getElementById("intl.accept_languages");
313  preference.value = string;
314  },
315 
316  onLanguageSelect: function ()
317  {
318  var upButton = document.getElementById("up");
319  var downButton = document.getElementById("down");
320  var removeButton = document.getElementById("remove");
321  switch (this._activeLanguages.selectedCount) {
322  case 0:
323  upButton.disabled = downButton.disabled = removeButton.disabled = true;
324  break;
325  case 1:
326  upButton.disabled = this._activeLanguages.selectedIndex == 0;
327  downButton.disabled = this._activeLanguages.selectedIndex == this._activeLanguages.childNodes.length - 1;
328  removeButton.disabled = false;
329  break;
330  default:
331  upButton.disabled = true;
332  downButton.disabled = true;
333  removeButton.disabled = false;
334  }
335  }
336 };
337 
var gLanguagesDialog
Definition: languages.js:41
function isVisible(element)
var language
Definition: Info.js:44
var strings
Definition: Info.js:46
_window init
Definition: FeedWriter.js:1144
var selectedItem
Definition: FeedWriter.js:1261
return null
Definition: FeedWriter.js:1143
_updateCookies aName
_getSelectedPageStyle s i