home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 35 Internet / 35-Internet.zip / mozil06.zip / bin / chrome / messenger.jar / content / messenger / addressbook / abSelectAddressesDialog.js < prev    next >
Text File  |  2001-02-14  |  9KB  |  289 lines

  1. /* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
  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.  * Contributor(s):
  20.  * Paul Hangas <hangas@netscape.com>
  21.  * Alec Flett <alecf@netscape.com>
  22.  */
  23.  
  24. var addressbook = 0;
  25. var composeWindow = 0;
  26. var msgCompFields = 0;
  27. var editCardCallback = 0;
  28. var gDialogResultsPaneSelectionChanged = 0;
  29.  
  30. var Bundle = srGetStrBundle("chrome://messenger/locale/addressbook/addressBook.properties");
  31.  
  32. // localization strings
  33. var prefixTo = Bundle.GetStringFromName("prefixTo") + ": ";
  34. var prefixCc = Bundle.GetStringFromName("prefixCc") + ": ";
  35. var prefixBcc = Bundle.GetStringFromName("prefixBcc") + ": ";
  36.  
  37. function OnLoadSelectAddress()
  38. {
  39.     InitCommonJS();
  40.  
  41.     var toAddress="", ccAddress="", bccAddress="";
  42.  
  43.     doSetOKCancel(SelectAddressOKButton, 0);
  44.  
  45.     top.addressbook = Components.classes["@mozilla.org/addressbook;1"].createInstance();
  46.     top.addressbook = top.addressbook.QueryInterface(Components.interfaces.nsIAddressBook);
  47.  
  48.     top.gDialogResultsPaneSelectionChanged = DialogResultsPaneSelectionChanged;
  49.  
  50.     // look in arguments[0] for parameters
  51.     if (window.arguments && window.arguments[0])
  52.     {
  53.         // keep parameters in global for later
  54.         if ( window.arguments[0].composeWindow )
  55.             top.composeWindow = window.arguments[0].composeWindow;
  56.         if ( window.arguments[0].msgCompFields )
  57.             top.msgCompFields = window.arguments[0].msgCompFields;
  58.         if ( window.arguments[0].toAddress )
  59.             toAddress = window.arguments[0].toAddress;
  60.         if ( window.arguments[0].ccAddress )
  61.             ccAddress = window.arguments[0].ccAddress;
  62.         if ( window.arguments[0].bccAddress )
  63.             bccAddress = window.arguments[0].bccAddress;
  64.             
  65.         dump("onload top.composeWindow: " + top.composeWindow + "\n");
  66.         dump("onload toAddress: " + toAddress + "\n");
  67.  
  68.         // put the addresses into the bucket
  69.         AddAddressFromComposeWindow(toAddress, prefixTo);
  70.         AddAddressFromComposeWindow(ccAddress, prefixCc);
  71.         AddAddressFromComposeWindow(bccAddress, prefixBcc);
  72.     }
  73.     
  74.     SelectFirstAddressBook();
  75.     
  76.     DialogResultsPaneSelectionChanged();
  77.     DialogBucketPaneSelectionChanged();
  78. }
  79.  
  80. function AddAddressFromComposeWindow(addresses, prefix)
  81. {
  82.     if ( addresses )
  83.     {
  84.         var addressArray = addresses.split(",");
  85.         
  86.         for ( var index = 0; index < addressArray.length; index++ )
  87.         {
  88.             // remove leading spaces
  89.             while ( addressArray[index][0] == " " )
  90.                 addressArray[index] = addressArray[index].substring(1, addressArray[index].length);
  91.             
  92.             AddAddressIntoBucket(prefix + addressArray[index]);
  93.         }
  94.     }
  95. }
  96.  
  97.  
  98. function SelectAddressOKButton()
  99. {
  100.     var body = document.getElementById('bucketBody');
  101.     var item, row, cell, text, colon,email;
  102.     var toAddress="", ccAddress="", bccAddress="", emptyEmail="";
  103.  
  104.     for ( var index = 0; index < body.childNodes.length; index++ )
  105.     {
  106.         item = body.childNodes[index];
  107.         if ( item.childNodes && item.childNodes.length )
  108.         {
  109.             row = item.childNodes[0];
  110.             if (  row.childNodes &&  row.childNodes.length )
  111.             {
  112.                 cell = row.childNodes[0];
  113.                 text = cell.getAttribute('value');
  114.                 email = cell.getAttribute('email');
  115.                 if ( text )
  116.                 {
  117.                     switch ( text[0] )
  118.                     {
  119.                         case prefixTo[0]:
  120.                             if ( toAddress )
  121.                                 toAddress += ", ";
  122.                             toAddress += text.substring(prefixTo.length, text.length);
  123.                             break;
  124.                         case prefixCc[0]:
  125.                             if ( ccAddress )
  126.                                 ccAddress += ", ";
  127.                             ccAddress += text.substring(prefixCc.length, text.length);
  128.                             break;
  129.                         case prefixBcc[0]:
  130.                             if ( bccAddress )
  131.                                 bccAddress += ", ";
  132.                             bccAddress += text.substring(prefixBcc.length, text.length);
  133.                             break;
  134.                     }
  135.                 }
  136.                 if(!email)
  137.                 {
  138.                     if (emptyEmail)
  139.                         emptyEmail +=", ";
  140.                         emptyEmail += text.substring(prefixTo.length, text.length-2);
  141.                 }
  142.             }
  143.         }
  144.     }
  145.     if(emptyEmail)
  146.     {    
  147.         var Bundle = srGetStrBundle("chrome://messenger/locale/addressbook/addressBook.properties");
  148.         var alertText = Bundle.GetStringFromName("emptyEmailCard");
  149.         alert(alertText + emptyEmail);
  150.         return false;
  151.     }
  152.     // reset the UI in compose window
  153.     msgCompFields.SetTo(toAddress);
  154.     msgCompFields.SetCc(ccAddress);
  155.     msgCompFields.SetBcc(bccAddress);
  156.     top.composeWindow.CompFields2Recipients(top.msgCompFields);
  157.     
  158.     return true;
  159. }
  160.  
  161. function SelectAddressToButton()
  162. {
  163.     AddSelectedAddressesIntoBucket(prefixTo);
  164. }
  165.  
  166. function SelectAddressCcButton()
  167. {
  168.     AddSelectedAddressesIntoBucket(prefixCc);
  169. }
  170.  
  171. function SelectAddressBccButton()
  172. {
  173.     AddSelectedAddressesIntoBucket(prefixBcc);
  174. }
  175.  
  176. function AddSelectedAddressesIntoBucket(prefix)
  177. {
  178.     var item, uri, rdf, cardResource, card, address;
  179.     var email ="";
  180.     rdf = Components.classes["@mozilla.org/rdf/rdf-service;1"].getService();
  181.     rdf = rdf.QueryInterface(Components.interfaces.nsIRDFService);
  182.  
  183.     if ( resultsTree && resultsTree.selectedItems && resultsTree.selectedItems.length )
  184.     {
  185.         for ( item = 0; item < resultsTree.selectedItems.length; item++ )
  186.         {
  187.             uri = resultsTree.selectedItems[item].getAttribute('id');
  188.             cardResource = rdf.GetResource(uri);
  189.             card = cardResource.QueryInterface(Components.interfaces.nsIAbCard);
  190.             if (card.isMailList)
  191.             {
  192.                 address = prefix + "\"" + card.name + "\" <" + card.name + ">";
  193.                 email = card.name;
  194.             }
  195.             else 
  196.             {
  197.                 address = prefix + "\"" + card.name + "\" <" + card.primaryEmail + ">";
  198.                 email = card.primaryEmail;
  199.             }
  200.             AddAddressIntoBucket(address,email);
  201.         }
  202.     }    
  203. }
  204.  
  205. function AddAddressIntoBucket(address,email)
  206. {
  207.     var body = document.getElementById("bucketBody");
  208.     
  209.     var item = document.createElement('treeitem');
  210.     var row = document.createElement('treerow');
  211.     var cell = document.createElement('treecell');
  212.     cell.setAttribute('value', address);
  213.     cell.setAttribute('email',email);
  214.     
  215.     row.appendChild(cell);
  216.     item.appendChild(row);
  217.     body.appendChild(item);
  218. }
  219.  
  220. function RemoveSelectedFromBucket()
  221. {
  222.     var bucketTree = document.getElementById("addressBucket");
  223.     if ( bucketTree )
  224.     {
  225.         var body = document.getElementById("bucketBody");
  226.         
  227.         if ( body && bucketTree.selectedItems && bucketTree.selectedItems.length )
  228.         {
  229.             for ( var item = bucketTree.selectedItems.length - 1; item >= 0; item-- )
  230.                 body.removeChild(bucketTree.selectedItems[item]);
  231.         }    
  232.     }    
  233. }
  234.  
  235. /* Function: DialogResultsPaneSelectionChanged()
  236.  * Callers : OnLoadSelectAddress(), abCommon.js:ResultsPaneSelectionChange()
  237.  * -------------------------------------------------------------------------
  238.  * This function is used to grab the selection state of the results tree to maintain
  239.  * the appropriate enabled/disabled states of the "Edit", "To:", "CC:", and "Bcc:" buttons.
  240.  * If an entry is selected in the results Tree, then the "disabled" attribute is removed.
  241.  * Otherwise, if nothing is selected, "disabled" is set to true.
  242.  */
  243.  
  244. function DialogResultsPaneSelectionChanged()
  245. {
  246.     var resultsTree = document.getElementById("resultsTree");
  247.     var editButton = document.getElementById("edit");
  248.     var toButton = document.getElementById("toButton");
  249.     var ccButton = document.getElementById("ccButton");
  250.     var bccButton = document.getElementById("bccButton");
  251.     
  252.     if (editButton && toButton && ccButton && bccButton && resultsTree && resultsTree.selectedItems && resultsTree.selectedItems.length) 
  253.     {
  254.         editButton.removeAttribute("disabled");
  255.         toButton.removeAttribute("disabled");
  256.         ccButton.removeAttribute("disabled");
  257.         bccButton.removeAttribute("disabled");
  258.     } 
  259.     else 
  260.     { 
  261.         editButton.setAttribute("disabled", "true");
  262.         toButton.setAttribute("disabled", "true");
  263.         ccButton.setAttribute("disabled", "true");
  264.         bccButton.setAttribute("disabled", "true");
  265.     }
  266. }
  267.  
  268. /* Function: DialogBucketPaneSelectionChanged()
  269.  * Callers : OnLoadSelectAddress(), abSelectAddressesDialog.xul:id="addressBucket"
  270.  * -------------------------------------------------------------------------------
  271.  * This function is used to grab the selection state of the bucket tree to maintain
  272.  * the appropriate enabled/disabled states of the "Remove" button.
  273.  * If an entry is selected in the bucket Tree, then the "disabled" attribute is removed.
  274.  * Otherwise, if nothing is selected, "disabled" is set to true.
  275.  */
  276.  
  277. function DialogBucketPaneSelectionChanged()
  278. {
  279.     var bucketTree = document.getElementById("addressBucket");
  280.     var removeButton = document.getElementById("remove");
  281.     
  282.     if (removeButton && bucketTree && bucketTree.selectedItems && bucketTree.selectedItems.length)
  283.         removeButton.removeAttribute("disabled");
  284.     else 
  285.         removeButton.setAttribute("disabled", "true");
  286. }
  287.  
  288.  
  289.