home *** CD-ROM | disk | FTP | other *** search
/ ftp.swcp.com / ftp.swcp.com.zip / ftp.swcp.com / mac / mozilla-macos9-1.3.1.sea.bin / Mozilla1.3.1 / Chrome / comm.jar / content / communicator / pref / pref-charset.js < prev    next >
Text File  |  2003-06-08  |  9KB  |  341 lines

  1. //get prefInt services
  2.  
  3. var availCharsetDict     = [];
  4. var prefInt              = null; //Preferences Interface
  5. var pref_string_title    = "";
  6. var pref_string_content  = "";
  7.  
  8. function Init()
  9. {
  10.   var applicationArea = "";
  11.  
  12.   if ("arguments" in window && window.arguments[0])
  13.     applicationArea = window.arguments[0];
  14.  
  15.   prefInt = Components.classes["@mozilla.org/preferences;1"];
  16.  
  17.   if (prefInt) {
  18.     prefInt = prefInt.getService(Components.interfaces.nsIPref);
  19.  
  20.     if (applicationArea.indexOf("mail") != -1) {
  21.       pref_string_title = "intl.charsetmenu.mailedit";
  22.     } else {
  23.     //default is the browser
  24.       pref_string_title = "intl.charsetmenu.browser.static";
  25.     }
  26.  
  27.     pref_string_content = prefInt.getLocalizedUnicharPref(pref_string_title);
  28.  
  29.     AddRemoveLatin1('add');
  30.   }
  31.  
  32.   LoadAvailableCharSets();
  33.   LoadActiveCharSets();
  34. }
  35.  
  36.  
  37. function readRDFString(aDS,aRes,aProp) 
  38. {
  39.   var n = aDS.GetTarget(aRes, aProp, true);
  40.   if (n)
  41.     return n.QueryInterface(Components.interfaces.nsIRDFLiteral).Value;
  42.   else 
  43.     return "";
  44. }
  45.  
  46.  
  47. function LoadAvailableCharSets()
  48. {
  49.   try {
  50.     var available_charsets_listbox = document.getElementById('available_charsets');
  51.     var rdf=Components.classes["@mozilla.org/rdf/rdf-service;1"].getService(Components.interfaces.nsIRDFService); 
  52.     var kNC_Root = rdf.GetResource("NC:DecodersRoot");
  53.     var kNC_name = rdf.GetResource("http://home.netscape.com/NC-rdf#Name");
  54.     var rdfDataSource = rdf.GetDataSource("rdf:charset-menu"); 
  55.     var rdfContainer = Components.classes["@mozilla.org/rdf/container;1"].getService(Components.interfaces.nsIRDFContainer);
  56.  
  57.     rdfContainer.Init(rdfDataSource, kNC_Root);
  58.     var availableCharsets = rdfContainer.GetElements();
  59.     var charset;
  60.  
  61.     for (var i = 0; i < rdfContainer.GetCount(); i++) {
  62.       charset = availableCharsets.getNext().QueryInterface(Components.interfaces.nsIRDFResource);
  63.       availCharsetDict[i] = new Array(2);
  64.       availCharsetDict[i][0] = readRDFString(rdfDataSource, charset, kNC_name);
  65.       availCharsetDict[i][1] = charset.Value;
  66.  
  67.       AddListItem(document,
  68.                   available_charsets_listbox,
  69.                   availCharsetDict[i][1],
  70.                   availCharsetDict[i][0]);
  71.     }
  72.   }
  73.   catch (e) {}
  74. }
  75.  
  76.  
  77. function GetCharSetTitle(id)
  78. {
  79.   if (availCharsetDict) {
  80.     for (var j = 0; j < availCharsetDict.length; j++) {
  81.       if (availCharsetDict[j][1] == id) {
  82.         return availCharsetDict[j][0];
  83.       }
  84.     }
  85.   }
  86.   return '';
  87. }
  88.  
  89. function AddRemoveLatin1(action)
  90. {
  91.   var arrayOfPrefs = [];
  92.   arrayOfPrefs = pref_string_content.split(', ');
  93.  
  94.   if (arrayOfPrefs.length > 0) {
  95.     for (var i = 0; i < arrayOfPrefs.length; i++) {
  96.       if (arrayOfPrefs[i] == 'ISO-8859-1') {
  97.         if (action == 'remove') {
  98.           arrayOfPrefs[i] = arrayOfPrefs[arrayOfPrefs.length-1];
  99.           arrayOfPrefs.length = arrayOfPrefs.length - 1;
  100.         }
  101.  
  102.         pref_string_content = arrayOfPrefs.join(', ');
  103.         return;
  104.       }
  105.     }
  106.  
  107.     if (action == 'add') {
  108.       arrayOfPrefs[arrayOfPrefs.length] = 'ISO-8859-1';
  109.       pref_string_content = arrayOfPrefs.join(', ');
  110.     }
  111.   }
  112. }
  113.  
  114.  
  115. function LoadActiveCharSets()
  116. {
  117.   var active_charsets = document.getElementById('active_charsets');
  118.   var arrayOfPrefs = [];
  119.   var str;
  120.   var tit;
  121.  
  122.   arrayOfPrefs = pref_string_content.split(', ');
  123.  
  124.   if (arrayOfPrefs.length > 0) {
  125.     for (var i = 0; i < arrayOfPrefs.length; i++) {
  126.       str = arrayOfPrefs[i];
  127.       tit = GetCharSetTitle(str);
  128.       if (str && tit)
  129.         AddListItem(document, active_charsets, str, tit);
  130.     }
  131.   }
  132. }
  133.  
  134.  
  135. function SelectAvailableCharset()
  136. {
  137.   //Remove the selection in the active charsets list
  138.   var active_charsets = document.getElementById('active_charsets');
  139.  
  140.   if (active_charsets.selectedItems.length > 0)
  141.     active_charsets.clearSelection();
  142.  
  143.   update_buttons();
  144. } //SelectAvailableCharset
  145.  
  146.  
  147.  
  148. function SelectActiveCharset()
  149. {
  150.   //Remove the selection in the available charsets list
  151.   var available_charsets = document.getElementById('available_charsets');
  152.  
  153.   if (available_charsets.selectedItems.length > 0)
  154.       available_charsets.clearSelection();
  155.  
  156.   update_buttons();
  157. } //SelectActiveCharset
  158.  
  159.  
  160. function enable_save()
  161. {
  162.   var save_button = document.documentElement.getButton("accept");
  163.   save_button.removeAttribute('disabled');
  164. }
  165.  
  166.  
  167. function update_buttons()
  168. {
  169.   var available_charsets = document.getElementById('available_charsets');
  170.   var active_charsets = document.getElementById('active_charsets');
  171.   var remove_button = document.getElementById('remove_button');
  172.   var add_button = document.getElementById('add_button');
  173.   var up_button = document.getElementById('up_button');
  174.   var down_button = document.getElementById('down_button');
  175.  
  176.   var activeCharsetSelected = (active_charsets.selectedItems.length > 0);
  177.   remove_button.disabled = !activeCharsetSelected;
  178.  
  179.   if (activeCharsetSelected) {
  180.     up_button.disabled = !(active_charsets.selectedItems[0].previousSibling);
  181.     down_button.disabled = !(active_charsets.selectedItems[0].nextSibling);
  182.   }
  183.   else {
  184.     up_button.disabled = true;
  185.     down_button.disabled = true;
  186.   }
  187.  
  188.   add_button.disabled = (available_charsets.selectedItems.length == 0);
  189. }
  190.  
  191.  
  192.  
  193. function AddAvailableCharset()
  194. {
  195.   var active_charsets = document.getElementById('active_charsets');
  196.   var available_charsets = document.getElementById('available_charsets');
  197.  
  198.   for (var nodeIndex=0; nodeIndex < available_charsets.selectedItems.length;  nodeIndex++)
  199.   {
  200.     var selItem =  available_charsets.selectedItems[nodeIndex];
  201.     
  202.     var charsetname  = selItem.label;
  203.     var charsetid = selItem.id;
  204.     var already_active = false;
  205.  
  206.     for (var item = active_charsets.firstChild; item != null; item = item.nextSibling) {
  207.       if (charsetid == item.id) {
  208.         already_active = true;
  209.         break;
  210.       }//if
  211.  
  212.     }//for
  213.  
  214.     if (already_active == false) {
  215.       AddListItem(document, active_charsets, charsetid, charsetname);
  216.     }//if
  217.  
  218.   }//for
  219.  
  220.   available_charsets.clearSelection();
  221.   enable_save();
  222.  
  223. } //AddAvailableCharset
  224.  
  225.  
  226.  
  227. function RemoveActiveCharset()
  228. {
  229.   var listbox = document.getElementById('active_charsets');
  230.   var nextNode = null;
  231.   var numSelected = listbox.selectedItems.length;
  232.   var deleted_all = false;
  233.  
  234.   while (listbox.selectedItems.length > 0) {
  235.     var selectedNode = listbox.selectedItems[0];
  236.     dump(selectedNode + " - " + listbox.selectedItems.length + "\n");
  237.     nextNode = selectedNode.nextSibling;
  238.  
  239.     if (!nextNode) {
  240.       if (selectedNode.previousSibling)
  241.         nextNode = selectedNode.previousSibling;
  242.     }
  243.  
  244.     listbox.removeChild(selectedNode);
  245.   } //while
  246.  
  247.   if (nextNode) {
  248.     listbox.selectItem(nextNode)
  249.   } else {
  250.     //listbox.clearSelection();
  251.   }
  252.  
  253.   enable_save();
  254. } //RemoveActiveCharset
  255.  
  256.  
  257.  
  258. function Save()
  259. {
  260.   // Iterate through the 'active charsets  tree to collect the charsets
  261.   // that the user has chosen.
  262.  
  263.   var active_charsets = document.getElementById('active_charsets');
  264.  
  265.   var charsetid    = "";
  266.   var num_charsets = 0;
  267.   var pref_string_content = '';
  268.  
  269.   for (var item = active_charsets.firstChild; item != null; item = item.nextSibling) {
  270.     charsetid = item.id;
  271.  
  272.     if (charsetid.length > 1) {
  273.       num_charsets++;
  274.  
  275.       //separate >1 charsets by commas
  276.       if (num_charsets > 1)
  277.         pref_string_content = pref_string_content + ", " + charsetid;
  278.       else
  279.         pref_string_content = charsetid;
  280.     }
  281.   }
  282.  
  283.   try
  284.   {
  285.     if (prefInt) {
  286.       prefInt.SetCharPref(pref_string_title, pref_string_content);
  287.       window.close();
  288.     }
  289.   }
  290.   catch(ex) {
  291.     confirm('exception' + ex);
  292.   }
  293.   return true;
  294. } //Save
  295.  
  296.  
  297. function MoveUp() {
  298.   var listbox = document.getElementById('active_charsets');
  299.   if (listbox.selectedItems.length == 1) {
  300.     var selected = listbox.selectedItems[0];
  301.     var before = selected.previousSibling
  302.     if (before) {
  303.       listbox.insertBefore(selected, before);
  304.       listbox.selectItem(selected);
  305.       listbox.ensureElementIsVisible(selected);
  306.     }
  307.   }
  308.  
  309.   enable_save();
  310. } //MoveUp
  311.  
  312.  
  313.  
  314. function MoveDown() {
  315.   var listbox = document.getElementById('active_charsets');
  316.   if (listbox.selectedItems.length == 1) {
  317.     var selected = listbox.selectedItems[0];
  318.     if (selected.nextSibling) {
  319.       if (selected.nextSibling.nextSibling)
  320.         listbox.insertBefore(selected, selected.nextSibling.nextSibling);
  321.       else
  322.         selected.parentNode.appendChild(selected);
  323.       listbox.selectItem(selected);
  324.     }
  325.   }
  326.  
  327.   enable_save();
  328. } //MoveDown
  329.  
  330. function AddListItem(doc, listbox, ID, UIstring)
  331. {
  332.   // Create a treerow for the new item
  333.   var item = doc.createElement('listitem');
  334.  
  335.   // Copy over the attributes
  336.   item.setAttribute('label', UIstring);
  337.   item.setAttribute('id', ID);
  338.  
  339.   listbox.appendChild(item);
  340. }
  341.