home *** CD-ROM | disk | FTP | other *** search
/ Chip 2002 January / 01_02.iso / software / netscape62win / browser.xpi / bin / chrome / comm.jar / content / editor / EdDictionary.js < prev    next >
Encoding:
JavaScript  |  2001-05-05  |  5.3 KB  |  204 lines

  1. /* 
  2.  * The contents of this file are subject to the Netscape Public
  3.  * License Version 1.1 (the "License"); you may not use this file
  4.  * except in compliance with the License. You may obtain a copy of
  5.  * the License at http://www.mozilla.org/NPL/
  6.  *  
  7.  * Software distributed under the License is distributed on an "AS
  8.  * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
  9.  * implied. See the License for the specific language governing
  10.  * rights and limitations under the License.
  11.  *  
  12.  * The Original Code is Mozilla Communicator client code, released
  13.  * March 31, 1998.
  14.  * 
  15.  * The Initial Developer of the Original Code is Netscape
  16.  * Communications Corporation. Portions created by Netscape are
  17.  * Copyright (C) 1998-1999 Netscape Communications Corporation. All
  18.  * Rights Reserved.
  19.  * 
  20.  * Contributor(s): 
  21.  */
  22.  
  23. var gSpellChecker;
  24. var gWordToAdd;
  25.  
  26. function Startup()
  27. {
  28.   if (!InitEditorShell())
  29.     return;
  30.   dump("EditoreditorShell found for dialog\n");
  31.  
  32.   // Get the SpellChecker shell
  33.   gSpellChecker = editorShell.QueryInterface(Components.interfaces.nsIEditorSpellCheck);
  34.   if (!gSpellChecker)
  35.   {
  36.     dump("SpellChecker not found!!!\n");
  37.     window.close();
  38.     return;
  39.   }
  40.   // Create dialog object to store controls for easy access
  41.   dialog = new Object;
  42.   if (!dialog)
  43.   {
  44.     dump("Failed to create dialog object!!!\n");
  45.     Close();
  46.   }
  47.   // The word to add word is passed as the 2nd extra parameter in window.openDialog()
  48.   gWordToAdd = window.arguments[1];
  49.   
  50.   dialog.WordInput = document.getElementById("WordInput");
  51.   dialog.DictionaryList = document.getElementById("DictionaryList");
  52.   
  53.   dialog.WordInput.value = gWordToAdd;
  54.   FillDictionaryList();
  55.  
  56.   // Select the supplied word if it is already in the list
  57.   SelectWordToAddInList();
  58.   SetTextboxFocus(dialog.WordInput);
  59.  
  60.   SetWindowLocation();
  61. }
  62.  
  63. function ValidateWordToAdd()
  64. {
  65.   gWordToAdd = TrimString(dialog.WordInput.value);
  66.   if (gWordToAdd.length > 0)
  67.   {
  68.     return true;
  69.   } else {
  70.     return false;
  71.   }
  72. }    
  73.  
  74. function SelectWordToAddInList()
  75. {
  76.   for (var index = 0; index < dialog.DictionaryList.getAttribute("length"); index++)
  77.   {
  78.     if (gWordToAdd == GetTreelistValueAt(dialog.DictionaryList,index))
  79.     {
  80.       dialog.DictionaryList.selectedIndex = index;
  81.       break;
  82.     }
  83.   }
  84. }
  85.  
  86. function AddWord()
  87. {
  88.   if (ValidateWordToAdd())
  89.   {
  90.     try {
  91.       gSpellChecker.AddWordToDictionary(gWordToAdd);
  92.     }
  93.     catch (e) {
  94.       dump("Exception occured in gSpellChecker.AddWordToDictionary\nWord to add probably already existed\n");
  95.     }
  96.  
  97.     // Rebuild the dialog list
  98.     FillDictionaryList();
  99.  
  100.     SelectWordToAddInList();
  101.     dialog.WordInput.value = "";
  102.   }
  103. }
  104.  
  105. function ReplaceWord()
  106. {
  107.   if (ValidateWordToAdd())
  108.   {
  109.     var selIndex = dialog.DictionaryList.selectedIndex;
  110.     if (selIndex >= 0)
  111.     {
  112.       gSpellChecker.RemoveWordFromDictionary(GetSelectedTreelistValue(dialog.DictionaryList));
  113.       try {
  114.         // Add to the dictionary list
  115.         gSpellChecker.AddWordToDictionary(gWordToAdd);
  116.         // Just change the text on the selected item
  117.         //  instead of rebuilding the list
  118.         ReplaceStringInTreeList(dialog.DictionaryList, selIndex, gWordToAdd);
  119.       } catch (e) {
  120.         // Rebuild list and select the word - it was probably already in the list
  121.         dump("Exception occured adding word in ReplaceWord\n");
  122.         FillDictionaryList();
  123.         SelectWordToAddInList();
  124.       }
  125.     }
  126.   }
  127. }
  128.  
  129. function RemoveWord()
  130. {
  131.   var selIndex = dialog.DictionaryList.selectedIndex;
  132.   if (selIndex >= 0)
  133.   {
  134.     var word = GetSelectedTreelistValue(dialog.DictionaryList);
  135.  
  136.     // Remove word from list
  137.     RemoveSelectedTreelistItem(dialog.DictionaryList);
  138.  
  139.     // Remove from dictionary
  140.     try {
  141.       //Not working: BUG 43348
  142.       gSpellChecker.RemoveWordFromDictionary(word);
  143.     }
  144.     catch (e)
  145.     {
  146.       dump("Failed to remove word from dictionary\n");
  147.     }
  148.  
  149.     ResetSelectedItem(selIndex);
  150.   }
  151. }
  152.  
  153. function FillDictionaryList()
  154. {
  155.   var selIndex = dialog.DictionaryList.selectedIndex;
  156.  
  157.   // Clear the current contents of the list
  158.   ClearTreelist(dialog.DictionaryList);
  159.   // Get the list from the spell checker
  160.   gSpellChecker.GetPersonalDictionary()
  161.  
  162.   var haveList = false;
  163.  
  164.   // Get words until an empty string is returned
  165.   do {
  166.     var word = gSpellChecker.GetPersonalDictionaryWord();
  167.     if (word != "")
  168.     {
  169.       AppendStringToTreelist(dialog.DictionaryList, word);
  170.       haveList = true;
  171.     }
  172.   } while (word != "");
  173.   
  174.   //XXX: BUG 74467: If list is empty, tree doesn't layout to full height correctly
  175.   //     (ignores "rows" attribute) (bug is latered, so we are fixing here for now)
  176.   if (!haveList)
  177.     AppendStringToTreelist(dialog.DictionaryList, " ");
  178.  
  179.   ResetSelectedItem(selIndex);
  180. }
  181.  
  182. function ResetSelectedItem(index)
  183. {
  184.   var lastIndex = dialog.DictionaryList.getAttribute("length") - 1;
  185.   if (index > lastIndex)
  186.     index = lastIndex;
  187.  
  188.   // If we didn't have a selected item, 
  189.   //  set it to the first item
  190.   if (index == -1 && lastIndex >= 0)
  191.     index = 0;
  192.  
  193. dump("ResetSelectedItem to index="+index+"\n");
  194.  
  195.   dialog.DictionaryList.selectedIndex = index;
  196. }
  197.  
  198. function Close()
  199. {
  200.   // Close the dialog
  201.   SaveWindowLocation();
  202.   window.close();
  203. }
  204.