home *** CD-ROM | disk | FTP | other *** search
/ GameStar 2005 October / Gamestar_77_2005-10_dvd.iso / Programy / nsb-install-8-0.exe / chrome / browser.jar / content / browser / pref / pref-languages.js < prev    next >
Encoding:
JavaScript  |  2005-07-29  |  12.4 KB  |  448 lines

  1. /* ***** BEGIN LICENSE BLOCK *****
  2.  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  3.  *
  4.  * The contents of this file are subject to the Mozilla Public License Version
  5.  * 1.1 (the "License"); you may not use this file except in compliance with
  6.  * the License. You may obtain a copy of the License at
  7.  * http://www.mozilla.org/MPL/
  8.  *
  9.  * Software distributed under the License is distributed on an "AS IS" basis,
  10.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  11.  * for the specific language governing rights and limitations under the
  12.  * License.
  13.  *
  14.  * The Original Code is mozilla.org code.
  15.  *
  16.  * The Initial Developer of the Original Code is
  17.  * Netscape Communications Corporation.
  18.  * Portions created by the Initial Developer are Copyright (C) 2000
  19.  * the Initial Developer. All Rights Reserved.
  20.  *
  21.  * Contributor(s):
  22.  *   Adrian Havill <havill@redhat.com>
  23.  *   Steffen Wilberg <steffen.wilberg@web.de>
  24.  *
  25.  * Alternatively, the contents of this file may be used under the terms of
  26.  * either the GNU General Public License Version 2 or later (the "GPL"), or
  27.  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  28.  * in which case the provisions of the GPL or the LGPL are applicable instead
  29.  * of those above. If you wish to allow use of your version of this file only
  30.  * under the terms of either the GPL or the LGPL, and not to allow others to
  31.  * use your version of this file under the terms of the MPL, indicate your
  32.  * decision by deleting the provisions above and replace them with the notice
  33.  * and other provisions required by the GPL or the LGPL. If you do not delete
  34.  * the provisions above, a recipient may use your version of this file under
  35.  * the terms of any one of the MPL, the GPL or the LGPL.
  36.  *
  37.  * ***** END LICENSE BLOCK ***** */
  38.  
  39. //GLOBALS
  40.  
  41. //locale bundles
  42. var gRegionsBundle;
  43. var gLanguagesBundle;
  44. var gAcceptedBundle;
  45. var gPrefLangBundle;
  46.  
  47. //dictionary of all supported locales
  48. var gAvailLanguageDict;
  49.  
  50. //XUL listbox handles
  51. var gAvailableLanguages;
  52. var gActiveLanguages;
  53.  
  54. // label and accesskey of the available_languages menulist
  55. var gSelectLabel;
  56. var gSelectAccesskey;
  57.  
  58. //XUL window pref window interface object
  59. var gPrefString = new String();
  60.  
  61. //Reg expression for splitting multiple pref values
  62. var gSeparatorRe = /\s*,\s*/;
  63.  
  64. // PrefWindow
  65. var gPrefWindow;
  66.  
  67.  
  68. function Init()
  69. {
  70.   gRegionsBundle   = document.getElementById("bundle_regions");
  71.   gLanguagesBundle = document.getElementById("bundle_languages");
  72.   gPrefLangBundle  = document.getElementById("bundle_prefLang");
  73.   gAcceptedBundle  = document.getElementById("bundle_accepted");
  74.   ReadAvailableLanguages();
  75.  
  76.   try {
  77.     window.opener.top.initPanel(window.location.href, window);
  78.   }
  79.   catch(ex) { } //pref service backup
  80.  
  81.   gActiveLanguages = document.getElementById("active_languages");
  82.   gPrefString = gActiveLanguages.getAttribute("prefvalue");
  83.   LoadActiveLanguages();
  84.  
  85.   gPrefWindow = window.opener.parent.hPrefWindow;
  86.   gAvailableLanguages = document.getElementById("available_languages");
  87.   SelectLanguage();
  88.  
  89.   LoadAvailableLanguages();
  90.   gSelectLabel = gAvailableLanguages.label;
  91.   gSelectAccesskey = gAvailableLanguages.getAttribute("accesskey");
  92. }
  93.  
  94.  
  95. function onLanguagesDialogOK()
  96. {
  97.   gPrefWindow.wsm.savePageData(window.location.href, window);
  98.   return true;
  99. }
  100.  
  101.  
  102. function ReadAvailableLanguages()
  103. {
  104.   gAvailLanguageDict   = new Array();
  105.   var visible = new String();
  106.   var str = new String();
  107.   var i =0;
  108.  
  109.   var gAcceptedBundleEnum = gAcceptedBundle.stringBundle.getSimpleEnumeration();
  110.  
  111.   var curItem;
  112.   var stringName;
  113.   var stringNameProperty;
  114.  
  115.   while (gAcceptedBundleEnum.hasMoreElements()) {
  116.  
  117.     //progress through the bundle
  118.     curItem = gAcceptedBundleEnum.getNext();
  119.  
  120.     //"unpack" the item, nsIPropertyElement is now partially scriptable
  121.     curItem = curItem.QueryInterface(Components.interfaces.nsIPropertyElement);
  122.  
  123.     //dump string name (key)
  124.     stringName = curItem.key;
  125.     stringNameProperty = stringName.split(".");
  126.  
  127.     if (stringNameProperty[1] == "accept") {
  128.  
  129.       //dump the UI string (value)
  130.       visible   = curItem.value;
  131.  
  132.       //if (visible == "true") {
  133.  
  134.         str = stringNameProperty[0];
  135.         var stringLangRegion = stringNameProperty[0].split("-");
  136.  
  137.         if (stringLangRegion[0]) {
  138.           var tit;
  139.           var language;
  140.           var region;
  141.           var use_region_format = false;
  142.  
  143.           try {
  144.             language = gLanguagesBundle.getString(stringLangRegion[0]);
  145.           }
  146.           catch (ex) {
  147.             language = "";
  148.           }
  149.  
  150.           if (stringLangRegion.length > 1) {
  151.  
  152.             try {
  153.               region = gRegionsBundle.getString(stringLangRegion[1]);
  154.               use_region_format = true;
  155.             }
  156.             catch (ex) { }
  157.           }
  158.  
  159.           if (use_region_format) {
  160.             tit = gPrefLangBundle.stringBundle.formatStringFromName("languageRegionCodeFormat",
  161.                                                                     [language, region, str], 3);
  162.           } else {
  163.             tit = gPrefLangBundle.stringBundle.formatStringFromName("languageCodeFormat",
  164.                                                                     [language, str], 2);
  165.           }
  166.  
  167.         } //if language
  168.  
  169.         if (str && tit) {
  170.  
  171.           gAvailLanguageDict[i] = new Array(3);
  172.           gAvailLanguageDict[i][0] = tit;
  173.           gAvailLanguageDict[i][1] = str;
  174.           gAvailLanguageDict[i][2] = visible;
  175.           i++;
  176.  
  177.         } // if str && tit
  178.       //} //if visible
  179.     } //if accepted
  180.   } //while
  181.   gAvailLanguageDict.sort( // sort on first element
  182.     function (a, b) {
  183.       if (a[0] < b[0]) return -1;
  184.       if (a[0] > b[0]) return  1;
  185.       return 0;
  186.     });
  187. } //ReadAvailableLanguages
  188.  
  189.  
  190. function LoadAvailableLanguages()
  191. {
  192.   if (gAvailLanguageDict)
  193.     for (var i = 0; i < gAvailLanguageDict.length; i++)
  194.       if (gAvailLanguageDict[i][2] == "true")
  195.         AddMenuOrListItem(document, gAvailableLanguages.menupopup, "menuitem", gAvailLanguageDict[i][1], gAvailLanguageDict[i][0]);
  196. }
  197.  
  198.  
  199. function LoadActiveLanguages()
  200. {
  201.   if (gPrefString) {
  202.     var arrayOfPrefs = gPrefString.split(gSeparatorRe);
  203.  
  204.     for (var i = 0; i < arrayOfPrefs.length; i++) {
  205.       var str = arrayOfPrefs[i];
  206.       var tit = GetLanguageTitle(str);
  207.  
  208.       if (str) {
  209.         if (!tit)
  210.            tit = "[" + str + "]";
  211.         AddMenuOrListItem(document, gActiveLanguages, "listitem", str, tit);
  212.       } //if
  213.     } //for
  214.   } //if
  215. }
  216.  
  217.  
  218. function LangAlreadyActive(langId)
  219. {
  220.   var found = false;
  221.   try {
  222.     var arrayOfPrefs = gPrefString.split(gSeparatorRe);
  223.  
  224.     if (arrayOfPrefs)
  225.       for (var i = 0; i < arrayOfPrefs.length; i++) {
  226.       if (arrayOfPrefs[i] == langId) {
  227.         found = true;
  228.         break;
  229.       }
  230.     }
  231.  
  232.     return found;
  233.   }
  234.  
  235.   catch(ex){
  236.     return false;
  237.   }
  238. } //LangAlreadyActive
  239.  
  240.  
  241. function SelectAvailableLanguage()
  242. {
  243.     var selItem  = gAvailableLanguages.selectedItem;
  244.     var languageId   = selItem.getAttribute("id");
  245.     var addButton = document.getElementById("add");
  246.  
  247.     // since we're not displaying "select" anymore, don't underline some random "s"
  248.     gAvailableLanguages.removeAttribute("accesskey");
  249.  
  250.     // if the langauge is not already active, activate the "add" button
  251.     if (!LangAlreadyActive(languageId))
  252.       addButton.disabled = false;
  253.     else
  254.       addButton.disabled = true;
  255. }
  256.  
  257. function AddAvailableLanguage()
  258. {
  259.   var selItem  = gAvailableLanguages.selectedItem;
  260.   var languageId   = selItem.getAttribute("id");
  261.   var languageName = selItem.getAttribute("label");
  262.   var addButton = document.getElementById("add");
  263.  
  264.   if (!LangAlreadyActive(languageId))
  265.     AddMenuOrListItem(document, gActiveLanguages, "listitem", languageId, languageName);
  266.  
  267.   // restore the "select" label and accesskey, disable the "add" button again
  268.   gAvailableLanguages.label = gSelectLabel;
  269.   gAvailableLanguages.setAttribute("accesskey", gSelectAccesskey);
  270.   addButton.disabled = true;
  271.  
  272.   // select the item we just added in the active_languages listbox
  273.   var lastItem = gActiveLanguages.lastChild;
  274.   gActiveLanguages.selectItem(lastItem);
  275.   gActiveLanguages.ensureElementIsVisible(lastItem);
  276.  
  277.   UpdateSavePrefString();
  278. }
  279.  
  280.  
  281. function RemoveActiveLanguage()
  282. {
  283.   var nextNode = null;
  284.   var numSelected = gActiveLanguages.selectedItems.length;
  285.   var deleted_all = false;
  286.  
  287.   while (gActiveLanguages.selectedItems.length > 0) {
  288.     var selectedNode = gActiveLanguages.selectedItems[0];
  289.  
  290.     nextNode = selectedNode.nextSibling;
  291.     if (!nextNode && selectedNode.previousSibling)
  292.       nextNode = selectedNode.previousSibling;
  293.  
  294.     gActiveLanguages.removeChild(selectedNode);
  295.   } //while
  296.  
  297.   if (nextNode)
  298.     gActiveLanguages.selectItem(nextNode)
  299.  
  300.   UpdateSavePrefString();
  301. }
  302.  
  303.  
  304. function GetLanguageTitle(id)
  305. {
  306.  
  307.   if (gAvailLanguageDict)
  308.     for (var j = 0; j < gAvailLanguageDict.length; j++) {
  309.  
  310.       if ( gAvailLanguageDict[j][1] == id) {
  311.         //title =
  312.         return gAvailLanguageDict[j][0];
  313.       }
  314.     }
  315.   return "";
  316. }
  317.  
  318.  
  319. function AddMenuOrListItem(doc, listbox, type, langID, langTitle)
  320. {
  321.   try {  //let's beef up our error handling for languages without label / title
  322.  
  323.     // Create a listitem for the new Language
  324.     var item = doc.createElement(type);
  325.  
  326.     // Copy over the attributes
  327.     item.setAttribute("label", langTitle);
  328.     item.setAttribute("id", langID);
  329.     listbox.appendChild(item);
  330.  
  331.   }
  332.   catch (ex) { }
  333. }
  334.  
  335.  
  336. function UpdateSavePrefString()
  337. {
  338.   var num_languages = 0;
  339.   gPrefString = "";
  340.  
  341.   for (var item = gActiveLanguages.firstChild; item != null; item = item.nextSibling) {
  342.  
  343.     var languageId = item.getAttribute("id");
  344.  
  345.     if (languageId.length > 1) {
  346.       num_languages++;
  347.  
  348.       //separate >1 languages by commas
  349.       if (num_languages > 1) {
  350.         gPrefString = gPrefString + "," + " " + languageId;
  351.       } else {
  352.         gPrefString = languageId;
  353.       } //if
  354.     } //if
  355.   }//for
  356.  
  357.   gActiveLanguages.setAttribute("prefvalue", gPrefString);
  358. }
  359.  
  360.  
  361. function MoveUp() {
  362.  
  363.   if (gActiveLanguages.selectedItems.length == 1) {
  364.     var selected = gActiveLanguages.selectedItems[0];
  365.     var before = selected.previousSibling
  366.     if (before) {
  367.       before.parentNode.insertBefore(selected, before);
  368.       gActiveLanguages.selectItem(selected);
  369.       gActiveLanguages.ensureElementIsVisible(selected);
  370.     }
  371.   }
  372.  
  373.   if (gActiveLanguages.selectedIndex == 0)
  374.   {
  375.     // selected item is first
  376.     var moveUp = document.getElementById("up");
  377.     moveUp.disabled = true;
  378.   }
  379.  
  380.   if (gActiveLanguages.childNodes.length > 1)
  381.   {
  382.     // more than one item so we can move selected item back down
  383.     var moveDown = document.getElementById("down");
  384.     moveDown.disabled = false;
  385.   }
  386.  
  387.   UpdateSavePrefString();
  388. } //MoveUp
  389.  
  390.  
  391. function MoveDown() {
  392.  
  393.   if (gActiveLanguages.selectedItems.length == 1) {
  394.     var selected = gActiveLanguages.selectedItems[0];
  395.     if (selected.nextSibling) {
  396.       if (selected.nextSibling.nextSibling) {
  397.         gActiveLanguages.insertBefore(selected, selected.nextSibling.nextSibling);
  398.       }
  399.       else {
  400.         gActiveLanguages.appendChild(selected);
  401.       }
  402.       gActiveLanguages.selectItem(selected);
  403.     }
  404.   }
  405.  
  406.   if (gActiveLanguages.selectedIndex == gActiveLanguages.childNodes.length - 1)
  407.   {
  408.     // selected item is last
  409.     var moveDown = document.getElementById("down");
  410.     moveDown.disabled = true;
  411.   }
  412.  
  413.   if (gActiveLanguages.childNodes.length > 1)
  414.   {
  415.     // more than one item so we can move selected item back up
  416.     var moveUp = document.getElementById("up");
  417.     moveUp.disabled = false;
  418.   }
  419.  
  420.   UpdateSavePrefString();
  421.  
  422. } //MoveDown
  423.  
  424.  
  425. function SelectLanguage() {
  426.   if (gActiveLanguages.selectedItems.length) {
  427.     document.getElementById("remove").disabled = false;
  428.  
  429.     var selected = gActiveLanguages.selectedItems[0];
  430.     document.getElementById("down").disabled = !selected.nextSibling;
  431.     document.getElementById("up").disabled = !selected.previousSibling;
  432.   }
  433.   else {
  434.     document.getElementById("remove").disabled = true;
  435.     document.getElementById("down").disabled = true;
  436.     document.getElementById("up").disabled = true;
  437.   }
  438.  
  439.   if (gPrefWindow.getPrefIsLocked(document.getElementById("up").getAttribute("prefstring")))
  440.     document.getElementById("up").disabled = true;
  441.   if (gPrefWindow.getPrefIsLocked(document.getElementById("down").getAttribute("prefstring")))
  442.     document.getElementById("down").disabled = true;
  443.   if (gPrefWindow.getPrefIsLocked(document.getElementById("remove").getAttribute("prefstring")))
  444.     document.getElementById("remove").disabled = true;
  445.   if (gPrefWindow.getPrefIsLocked(gAvailableLanguages.getAttribute("prefstring")))
  446.     gAvailableLanguages.disabled = true; // the "add" button just stays disabled
  447. } // SelectLanguage
  448.