home *** CD-ROM | disk | FTP | other *** search
/ GameStar 2005 October / Gamestar_77_2005-10_dvd.iso / Programy / nsb-install-8-0.exe / chrome / messenger.jar / content / messenger / addressbook / abSelectAddressesDialog.js < prev    next >
Encoding:
JavaScript  |  2005-07-29  |  13.0 KB  |  454 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.  *
  20.  * Contributor(s):
  21.  *   Paul Hangas <hangas@netscape.com>
  22.  *   Alec Flett <alecf@netscape.com>
  23.  *   Seth Spitzer <sspitzer@netscape.com>
  24.  */
  25.  
  26. var addressbook = 0;
  27. var composeWindow = 0;
  28. var msgCompFields = 0;
  29. var editCardCallback = 0;
  30.  
  31. var gAddressBookBundle;
  32.  
  33. var gSearchInput;
  34. var gSearchTimer = null;
  35. var gQueryURIFormat = null;
  36.  
  37. // localization strings
  38. var prefixTo;
  39. var prefixCc;
  40. var prefixBcc;
  41.  
  42. var gToButton;
  43. var gCcButton;
  44. var gBccButton;
  45.  
  46. var gActivatedButton;
  47.  
  48. var gDragService = Components.classes["@mozilla.org/widget/dragservice;1"].getService();
  49. gDragService = gDragService.QueryInterface(Components.interfaces.nsIDragService);
  50.  
  51. var gSelectAddressesAbViewListener = {
  52.     onSelectionChanged: function() {
  53.     ResultsPaneSelectionChanged();
  54.   },
  55.   onCountChanged: function(total) {
  56.     // do nothing
  57.   }
  58. };
  59.  
  60. function GetAbViewListener()
  61. {
  62.   return gSelectAddressesAbViewListener;
  63. }
  64.  
  65. function OnLoadSelectAddress()
  66. {
  67.   gAddressBookBundle = document.getElementById("bundle_addressBook");
  68.   prefixTo = gAddressBookBundle.getString("prefixTo") + ": ";
  69.   prefixCc = gAddressBookBundle.getString("prefixCc") + ": ";
  70.   prefixBcc = gAddressBookBundle.getString("prefixBcc") + ": ";
  71.  
  72.   InitCommonJS();
  73.  
  74.   UpgradeAddressBookResultsPaneUI("mailnews.ui.select_addresses_results.version");
  75.  
  76.   var toAddress="", ccAddress="", bccAddress="";
  77.  
  78.   doSetOKCancel(SelectAddressOKButton, 0);
  79.  
  80.   top.addressbook = Components.classes["@mozilla.org/addressbook;1"].createInstance(Components.interfaces.nsIAddressBook);
  81.  
  82.   // look in arguments[0] for parameters
  83.   if (window.arguments && window.arguments[0])
  84.   {
  85.     // keep parameters in global for later
  86.     if ( window.arguments[0].composeWindow )
  87.       top.composeWindow = window.arguments[0].composeWindow;
  88.     if ( window.arguments[0].msgCompFields )
  89.       top.msgCompFields = window.arguments[0].msgCompFields;
  90.     if ( window.arguments[0].toAddress )
  91.       toAddress = window.arguments[0].toAddress;
  92.     if ( window.arguments[0].ccAddress )
  93.       ccAddress = window.arguments[0].ccAddress;
  94.     if ( window.arguments[0].bccAddress )
  95.       bccAddress = window.arguments[0].bccAddress;
  96.  
  97.     // put the addresses into the bucket
  98.     AddAddressFromComposeWindow(toAddress, prefixTo);
  99.     AddAddressFromComposeWindow(ccAddress, prefixCc);
  100.     AddAddressFromComposeWindow(bccAddress, prefixBcc);
  101.   }
  102.  
  103.   gSearchInput = document.getElementById("searchInput");
  104.   SearchInputChanged();
  105.  
  106.   SelectFirstAddressBookMenulist();
  107.  
  108.   DialogBucketPaneSelectionChanged();
  109.   
  110.   var workPhoneCol = document.getElementById("WorkPhone");
  111.   workPhoneCol.setAttribute("hidden", "true");
  112.   
  113.   var companyCol = document.getElementById("Company");
  114.   companyCol.setAttribute("hidden", "true");
  115.  
  116.   gToButton = document.getElementById("toButton");
  117.   gCcButton = document.getElementById("ccButton");
  118.   gBccButton = document.getElementById("bccButton");
  119.  
  120.   var abResultsTree = document.getElementById("abResultsTree");
  121.   abResultsTree.focus();
  122.  
  123.   gActivatedButton = gToButton;
  124.  
  125.   document.documentElement.addEventListener("keypress", OnReturnHit, true);
  126. }
  127.  
  128. function OnUnloadSelectAddress()
  129. {
  130.   CloseAbView();
  131. }
  132.  
  133. function AddAddressFromComposeWindow(addresses, prefix)
  134. {
  135.   if ( addresses )
  136.   {
  137.     var addressArray = addresses.split(",");
  138.  
  139.     for ( var index = 0; index < addressArray.length; index++ )
  140.     {
  141.       // remove leading spaces
  142.       while ( addressArray[index][0] == " " )
  143.         addressArray[index] = addressArray[index].substring(1, addressArray[index].length);
  144.  
  145.       AddAddressIntoBucket(prefix + addressArray[index]);
  146.     }
  147.   }
  148. }
  149.  
  150. function SelectAddressOKButton()
  151. {
  152.   var body = document.getElementById('bucketBody');
  153.   var item, row, cell, text, colon,email;
  154.   var toAddress="", ccAddress="", bccAddress="", emptyEmail="";
  155.  
  156.   for ( var index = 0; index < body.childNodes.length; index++ )
  157.   {
  158.     item = body.childNodes[index];
  159.     if ( item.childNodes && item.childNodes.length )
  160.     {
  161.       row = item.childNodes[0];
  162.       if (  row.childNodes &&  row.childNodes.length )
  163.       {
  164.         cell = row.childNodes[0];
  165.         text = cell.getAttribute('label');
  166.         email = cell.getAttribute('email');
  167.         if ( text )
  168.         {
  169.           switch ( text[0] )
  170.           {
  171.             case prefixTo[0]:
  172.               if ( toAddress )
  173.                 toAddress += ", ";
  174.               toAddress += text.substring(prefixTo.length, text.length);
  175.               break;
  176.             case prefixCc[0]:
  177.               if ( ccAddress )
  178.                 ccAddress += ", ";
  179.               ccAddress += text.substring(prefixCc.length, text.length);
  180.               break;
  181.             case prefixBcc[0]:
  182.               if ( bccAddress )
  183.                 bccAddress += ", ";
  184.               bccAddress += text.substring(prefixBcc.length, text.length);
  185.               break;
  186.           }
  187.         }
  188.         if(!email)
  189.         {
  190.           if (emptyEmail)
  191.             emptyEmail +=", ";
  192.             emptyEmail += text.substring(prefixTo.length, text.length-2);
  193.         }
  194.       }
  195.     }
  196.   }
  197.   if(emptyEmail)
  198.   {
  199.     var alertText = gAddressBookBundle.getString("emptyEmailCard");
  200.     alert(alertText + emptyEmail);
  201.     return false;
  202.   }
  203.   // reset the UI in compose window
  204.   msgCompFields.to = toAddress;
  205.   msgCompFields.cc = ccAddress;
  206.   msgCompFields.bcc = bccAddress;
  207.   top.composeWindow.CompFields2Recipients(top.msgCompFields);
  208.  
  209.   return true;
  210. }
  211.  
  212. function SelectAddressToButton()
  213. {
  214.   AddSelectedAddressesIntoBucket(prefixTo);
  215.   gActivatedButton = gToButton;
  216. }
  217.  
  218. function SelectAddressCcButton()
  219. {
  220.   AddSelectedAddressesIntoBucket(prefixCc);
  221.   gActivatedButton = gCcButton;
  222. }
  223.  
  224. function SelectAddressBccButton()
  225. {
  226.   AddSelectedAddressesIntoBucket(prefixBcc);
  227.   gActivatedButton = gBccButton;
  228. }
  229.  
  230. function AddSelectedAddressesIntoBucket(prefix)
  231. {
  232.   var cards = GetSelectedAbCards();
  233.   var count = cards.length;
  234.  
  235.   for (var i = 0; i < count; i++) {
  236.     AddCardIntoBucket(prefix, cards[i]);
  237.   }
  238. }
  239.  
  240. function AddCardIntoBucket(prefix, card)
  241. {
  242.   var address = prefix + GenerateAddressFromCard(card);
  243.   if (card.isMailList) {
  244.     AddAddressIntoBucket(address, card.displayName);
  245.     }
  246.   else {
  247.     AddAddressIntoBucket(address, card.primaryEmail);
  248.   }
  249. }
  250.  
  251. function AddAddressIntoBucket(address,email)
  252. {
  253.   var body = document.getElementById("bucketBody");
  254.  
  255.   var item = document.createElement('treeitem');
  256.   var row = document.createElement('treerow');
  257.   var cell = document.createElement('treecell');
  258.   cell.setAttribute('label', address);
  259.   cell.setAttribute('email',email);
  260.  
  261.   row.appendChild(cell);
  262.   item.appendChild(row);
  263.   body.appendChild(item);
  264. }
  265.  
  266. function RemoveSelectedFromBucket()
  267. {
  268.   var bucketTree = document.getElementById("addressBucket");
  269.   if ( bucketTree )
  270.   {
  271.     var body = document.getElementById("bucketBody");
  272.     var selection = bucketTree.view.selection;
  273.     var rangeCount = selection.getRangeCount();
  274.  
  275.     for (var i = rangeCount-1; i >= 0; --i)
  276.     {
  277.       var start = {}, end = {};
  278.       selection.getRangeAt(i,start,end);
  279.       for (var j = end.value; j >= start.value; --j)
  280.       {
  281.         var item = bucketTree.contentView.getItemAtIndex(j);
  282.         body.removeChild(item);
  283.       }
  284.     }
  285.   }
  286. }
  287.  
  288. /* Function: ResultsPaneSelectionChanged()
  289.  * Callers : OnLoadSelectAddress(), abCommon.js:ResultsPaneSelectionChanged()
  290.  * -------------------------------------------------------------------------
  291.  * This function is used to grab the selection state of the results tree to maintain
  292.  * the appropriate enabled/disabled states of the "Edit", "To:", "CC:", and "Bcc:" buttons.
  293.  * If an entry is selected in the results Tree, then the "disabled" attribute is removed.
  294.  * Otherwise, if nothing is selected, "disabled" is set to true.
  295.  */
  296.  
  297. function ResultsPaneSelectionChanged()
  298. {;
  299.   var editButton = document.getElementById("edit");
  300.   var toButton = document.getElementById("toButton");
  301.   var ccButton = document.getElementById("ccButton");
  302.   var bccButton = document.getElementById("bccButton");
  303.  
  304.   var numSelected = GetNumSelectedCards();
  305.   if (numSelected > 0)
  306.   {
  307.     if (numSelected == 1)
  308.     editButton.removeAttribute("disabled");
  309.     else
  310.       editButton.setAttribute("disabled", "true");
  311.  
  312.     toButton.removeAttribute("disabled");
  313.     ccButton.removeAttribute("disabled");
  314.     bccButton.removeAttribute("disabled");
  315.   }
  316.   else
  317.   {
  318.     editButton.setAttribute("disabled", "true");
  319.     toButton.setAttribute("disabled", "true");
  320.     ccButton.setAttribute("disabled", "true");
  321.     bccButton.setAttribute("disabled", "true");
  322.   }
  323. }
  324.  
  325. /* Function: DialogBucketPaneSelectionChanged()
  326.  * Callers : OnLoadSelectAddress(), abSelectAddressesDialog.xul:id="addressBucket"
  327.  * -------------------------------------------------------------------------------
  328.  * This function is used to grab the selection state of the bucket tree to maintain
  329.  * the appropriate enabled/disabled states of the "Remove" button.
  330.  * If an entry is selected in the bucket Tree, then the "disabled" attribute is removed.
  331.  * Otherwise, if nothing is selected, "disabled" is set to true.
  332.  */
  333.  
  334. function DialogBucketPaneSelectionChanged()
  335. {
  336.   var bucketTree = document.getElementById("addressBucket");
  337.   var removeButton = document.getElementById("remove");
  338.  
  339.   removeButton.disabled = bucketTree.view.selection.count == 0;
  340. }
  341.  
  342. function AbResultsPaneDoubleClick(card)
  343. {
  344.   AddCardIntoBucket(prefixTo, card);
  345. }
  346.  
  347. function OnClickedCard(card)
  348. {
  349.   // in the select address dialog, do nothing on click
  350. }
  351.  
  352. function UpdateCardView()
  353. {
  354.   // in the select address dialog, do nothing
  355. }
  356.  
  357. function DragOverBucketPane(event)
  358. {
  359.   var dragSession = gDragService.getCurrentSession();
  360.  
  361.   if (dragSession.isDataFlavorSupported("text/x-moz-address"))
  362.     dragSession.canDrop = true;
  363. }
  364.  
  365. function DropOnBucketPane(event)
  366. {
  367.   var dragSession = gDragService.getCurrentSession();
  368.   var trans;
  369.   
  370.   try {
  371.     trans = Components.classes["@mozilla.org/widget/transferable;1"].createInstance(Components.interfaces.nsITransferable);
  372.     trans.addDataFlavor("text/x-moz-address");
  373.   }
  374.   catch (ex) {
  375.     return;
  376.   }
  377.  
  378.   for ( var i = 0; i < dragSession.numDropItems; ++i )
  379.   {
  380.     dragSession.getData ( trans, i );
  381.     var dataObj = new Object();
  382.     var bestFlavor = new Object();
  383.     var len = new Object();
  384.     trans.getAnyTransferData ( bestFlavor, dataObj, len );
  385.     if ( dataObj )  
  386.       dataObj = dataObj.value.QueryInterface(Components.interfaces.nsISupportsString);
  387.     if ( !dataObj ) 
  388.       continue;
  389.  
  390.     // pull the address out of the data object
  391.     var address = dataObj.data.substring(0, len.value);
  392.     if (!address)
  393.       continue;
  394.  
  395.     AddAddressIntoBucket(prefixTo + address, address);
  396.   }
  397. }
  398.  
  399. function OnReturnHit(event)
  400. {  
  401.   if (event.keyCode == 13) {
  402.     var focusedElement = document.commandDispatcher.focusedElement;
  403.     if (focusedElement && (focusedElement.id == "addressBucket"))
  404.       return;
  405.     event.preventBubble();
  406.     if (focusedElement && (focusedElement.id == "abResultsTree"))
  407.       gActivatedButton.doCommand();
  408.   }
  409. }
  410.  
  411.  
  412. function onEnterInSearchBar()
  413. {
  414.   var selectedNode = abList.selectedItem;
  415.  
  416.   if (!selectedNode)
  417.     return;
  418.  
  419.   if (!gQueryURIFormat) {
  420.     gQueryURIFormat = gPrefs.getComplexValue("mail.addr_book.quicksearchquery.format", 
  421.                                               Components.interfaces.nsIPrefLocalizedString).data;
  422.   }
  423.   
  424.   var sortColumn = selectedNode.getAttribute("sortColumn");
  425.   var sortDirection = selectedNode.getAttribute("sortDirection");
  426.   var searchURI = selectedNode.getAttribute("id");
  427.  
  428.   if (gSearchInput.value != "") {
  429.     searchURI += gQueryURIFormat.replace(/@V/g, encodeURIComponent(gSearchInput.value));
  430.   }
  431.  
  432.   if (!sortColumn.Length)
  433.     sortColumn = "GeneratedName";
  434.   SetAbView(searchURI, true, sortColumn, sortDirection);
  435.   
  436.   SelectFirstCard();
  437. }
  438.  
  439. function SelectFirstAddressBookMenulist()
  440. {
  441.   ChangeDirectoryByURI(abList.selectedItem.id);
  442.   return;
  443. }
  444.  
  445. function DirPaneSelectionChangeMenulist()
  446. {
  447.   if (abList && abList.selectedItem) {
  448.     if (gSearchInput.value && (gSearchInput.value != "")) 
  449.       onEnterInSearchBar();
  450.     else
  451.       ChangeDirectoryByURI(abList.selectedItem.id);
  452.   }
  453. }
  454.