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 / editor / EdDictionary.js < prev    next >
Text File  |  2003-06-08  |  5KB  |  202 lines

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