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