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 / abMailListDialog.js < prev    next >
Text File  |  2001-02-14  |  16KB  |  630 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.org code.
  13.  *
  14.  * The Initial Developer of the Original Code is Netscape
  15.  * Communications Corporation.  Portions created by Netscape are
  16.  * Copyright (C) 1998 Netscape Communications Corporation. All
  17.  * Rights Reserved.
  18.  *
  19.  * Contributor(s): 
  20.  */
  21.  
  22. top.MAX_RECIPIENTS = 1;
  23. var inputElementType = "";
  24.  
  25. var mailList;
  26. var parentURI;
  27. var editList;
  28. var hitReturnInList = false;
  29. var oldListName = "";
  30.  
  31. function handleKeyPress(element, event)
  32. {
  33.     if (event.keyCode == 13) 
  34.     {
  35.         hitReturnInList = true;
  36.         awReturnHit(element);
  37.     }
  38. }
  39.  
  40. function mailingListExists(listname)
  41. {
  42.     var addressbook = Components.classes["@mozilla.org/addressbook;1"].createInstance(Components.interfaces.nsIAddressBook);
  43.     if (addressbook.mailListNameExists(listname))
  44.     {
  45.         var strBundle = srGetStrBundle("chrome://messenger/locale/addressbook/addressBook.properties");
  46.         var alertText = strBundle.GetStringFromName("mailListNameExists");
  47.         alert(alertText);
  48.         return true;
  49.     }
  50.     return false;
  51. }
  52.  
  53. function GetListValue(mailList, doAdd)
  54. {
  55.     mailList.listName = document.getElementById('ListName').value;
  56.  
  57.     if (mailList.listName.length == 0)
  58.     {
  59.         var strBundle = srGetStrBundle("chrome://messenger/locale/addressbook/addressBook.properties");
  60.         var alertText = strBundle.GetStringFromName("emptyListName");
  61.         alert(alertText);
  62.         return false;
  63.     }
  64.     else 
  65.     {
  66.         listname = mailList.listName;
  67.         listname = listname.toLowerCase();
  68.         oldListName = oldListName.toLowerCase();
  69.         if (doAdd == true)
  70.         {
  71.             if (mailingListExists(listname))
  72.                 return false;
  73.         }
  74.         else if (oldListName != listname)
  75.         {
  76.             if (mailingListExists(listname))
  77.                 return false;
  78.         }
  79.     }
  80.  
  81.     mailList.listNickName = document.getElementById('ListNickName').value;
  82.     mailList.description = document.getElementById('ListDescription').value;
  83.     
  84.     var oldTotal = mailList.addressLists.Count();
  85.     var i = 1;
  86.     var pos = 0;
  87.     while ((inputField = awGetInputElement(i)))
  88.     {
  89.         fieldValue = inputField.value;
  90.         if (doAdd || (doAdd == false && pos >= oldTotal))
  91.             var cardproperty = Components.classes["@mozilla.org/addressbook/cardproperty;1"].createInstance();
  92.         else
  93.             var cardproperty = mailList.addressLists.GetElementAt(pos);
  94.  
  95.         if (fieldValue == "")
  96.         {
  97.             if (doAdd == false && cardproperty) 
  98.                 mailList.removeEmailAddressAt(pos);
  99.         }
  100.         else if (cardproperty)
  101.         {
  102.             cardproperty = cardproperty.QueryInterface(Components.interfaces.nsIAbCard);
  103.             if (cardproperty)
  104.             {
  105.                 var beginpos = fieldValue.search('<');
  106.                 var endpos = fieldValue.search('>');
  107.                 if (beginpos != -1)
  108.                 {
  109.                     beginpos++;
  110.                     var newValue = fieldValue.slice(beginpos, endpos);
  111.                     cardproperty.primaryEmail = newValue;
  112.                 }
  113.                 else
  114.                     cardproperty.primaryEmail = fieldValue;
  115.                 if (doAdd || (doAdd == false && pos >= oldTotal))
  116.                     mailList.addressLists.AppendElement(cardproperty);
  117.                 pos++;
  118.             }
  119.         }
  120.         i++;
  121.     }
  122.     if (doAdd == false && i < oldTotal)
  123.     {
  124.         for (j = i; j < oldTotal; j++)
  125.             mailList.addressLists.RemoveElementAt(j);
  126.     }
  127.     return true;
  128. }
  129.  
  130. function MailListOKButton()
  131. {
  132.     if (hitReturnInList)
  133.     {
  134.         hitReturnInList = false;
  135.         return false;
  136.     }
  137.     var popup = document.getElementById('abPopup');
  138.     if ( popup )
  139.     {
  140.         var uri = popup.getAttribute('data');
  141.         
  142.         // FIX ME - hack to avoid crashing if no ab selected because of blank option bug from template
  143.         // should be able to just remove this if we are not seeing blank lines in the ab popup
  144.         if ( !uri )
  145.             return false;  // don't close window
  146.         // -----
  147.         
  148.         //Add mailing list to database
  149.         mailList = Components.classes["@mozilla.org/addressbook/directoryproperty;1"].createInstance();
  150.         mailList = mailList.QueryInterface(Components.interfaces.nsIAbDirectory);
  151.  
  152.         if (GetListValue(mailList, true))
  153.             mailList.addMailListToDatabase(uri);
  154.         else
  155.             return false;
  156.     }        
  157.     return true;    // close the window
  158. }
  159.  
  160. function OnLoadMailList()
  161. {
  162.     doSetOKCancel(MailListOKButton, 0);
  163.     
  164.     if (window.arguments && window.arguments[0])
  165.     {
  166.         if ( window.arguments[0].selectedAB )
  167.             selectedAB = window.arguments[0].selectedAB;
  168.         else
  169.             selectedAB = "abdirectory://abook.mab";
  170.     }
  171.  
  172.     // set popup with address book names
  173.     var abPopup = document.getElementById('abPopup');
  174.     if ( abPopup )
  175.     {
  176.         var menupopup = document.getElementById('abPopup-menupopup');
  177.         
  178.         if ( selectedAB && menupopup && menupopup.childNodes )
  179.         {
  180.             for ( var index = menupopup.childNodes.length - 1; index >= 0; index-- )
  181.             {
  182.                 if ( menupopup.childNodes[index].getAttribute('data') == selectedAB )
  183.                 {
  184.                     abPopup.value = menupopup.childNodes[index].getAttribute('value');
  185.                     abPopup.data = menupopup.childNodes[index].getAttribute('data');
  186.                     break;
  187.                 }
  188.             }
  189.         }
  190.     }
  191.     
  192.     AppendnewRowAndSetFocus();
  193.  
  194.     // focus on first name
  195.     var listName = document.getElementById('ListName');
  196.     if ( listName )
  197.         listName.focus();
  198.     moveToAlertPosition();
  199. }
  200.  
  201. function EditListOKButton()
  202. {
  203.     if (hitReturnInList)
  204.     {
  205.         hitReturnInList = false;
  206.         return false;
  207.     }
  208.     //Add mailing list to database
  209.     if (GetListValue(editList, false))
  210.     {
  211.         editList.editMailListToDatabase(parentURI);
  212.         return true;    // close the window
  213.     }
  214.     else
  215.         return false;    
  216. }
  217.  
  218. function OnLoadEditList()
  219. {
  220.     doSetOKCancel(EditListOKButton, 0);
  221.     
  222.     parentURI  = window.arguments[0].abURI;
  223.     var listUri  = window.arguments[0].listURI;
  224.  
  225.     var rdf = Components.classes["@mozilla.org/rdf/rdf-service;1"].getService();
  226.     rdf = rdf.QueryInterface(Components.interfaces.nsIRDFService);
  227.     editList = rdf.GetResource(listUri);
  228.     editList = editList.QueryInterface(Components.interfaces.nsIAbDirectory);
  229.  
  230.     document.getElementById('ListName').value = editList.listName;
  231.     document.getElementById('ListNickName').value = editList.listNickName;
  232.     document.getElementById('ListDescription').value = editList.description;
  233.     oldListName = editList.listName;
  234.  
  235.     if (editList.addressLists)
  236.     {
  237.         var total = editList.addressLists.Count();
  238. dump("*** editList.Count = "+total+"\n");
  239.         if (total)
  240.         {
  241.             var treeChildren = document.getElementById('addressList');
  242.             var newTreeChildrenNode = treeChildren.cloneNode(false);
  243.             var templateNode = treeChildren.firstChild;    
  244.  
  245.             top.MAX_RECIPIENTS = 0;
  246.             for ( var i = 0;  i < total; i++ )
  247.             {
  248.                 var card = editList.addressLists.GetElementAt(i);
  249.                 card = card.QueryInterface(Components.interfaces.nsIAbCard);
  250.                 var address;
  251.                 if (card.name.length)
  252.                     address = card.name + " <" + card.primaryEmail + ">";
  253.                 else
  254.                     address = card.primaryEmail;
  255.                 SetInputValue(address, newTreeChildrenNode, templateNode);
  256.             }
  257.             var parent = treeChildren.parentNode; 
  258.             parent.replaceChild(newTreeChildrenNode, treeChildren);
  259.         } 
  260.     }
  261.  
  262.     AppendnewRowAndSetFocus();
  263.  
  264.     // focus on first name
  265.     var listName = document.getElementById('ListName');
  266.     if ( listName )
  267.         listName.focus();
  268.     moveToAlertPosition();
  269. }
  270.  
  271. function AppendnewRowAndSetFocus()
  272. {
  273.     var lastInput = awGetInputElement(top.MAX_RECIPIENTS);
  274.     if ( lastInput && lastInput.value ) 
  275.         awAppendNewRow(true);
  276.     else
  277.         awSetFocus(top.MAX_RECIPIENTS, lastInput);
  278. }
  279.  
  280. function SetInputValue(inputValue, parentNode, templateNode)
  281. {
  282.     top.MAX_RECIPIENTS++;
  283.  
  284.     var newNode = templateNode.cloneNode(true);
  285.     parentNode.appendChild(newNode); // we need to insert the new node before we set the value of the select element!
  286.  
  287.     var input = newNode.getElementsByTagName(awInputElementName());
  288.     if ( input && input.length == 1 )
  289.     {
  290.         //We need to set the value using both setAttribute and .value else we will
  291.         // loose the content when the field is not visible. See bug 37435
  292.         input[0].setAttribute("value", inputValue);
  293.         input[0].value = inputValue;
  294.         input[0].setAttribute("id", "address#" + top.MAX_RECIPIENTS);
  295.     }
  296. }
  297.  
  298. function awNotAnEmptyArea(event)
  299. {
  300.     //This is temporary until i figure out how to ensure to always having an empty space after the last row
  301.     dump("awNotAnEmptyArea\n");
  302.  
  303.     var lastInput = awGetInputElement(top.MAX_RECIPIENTS);
  304.     if ( lastInput && lastInput.value )
  305.         awAppendNewRow(false);
  306.  
  307.     event.preventBubble();
  308. }
  309.  
  310. function awClickEmptySpace(targ, setFocus)
  311. {
  312.     if (targ.localName != 'treechildren')
  313.         return;
  314.  
  315.     dump("awClickEmptySpace\n");
  316.     var lastInput = awGetInputElement(top.MAX_RECIPIENTS);
  317.  
  318.     if ( lastInput && lastInput.value )
  319.         awAppendNewRow(setFocus);
  320.     else
  321.         if (setFocus)
  322.             awSetFocus(top.MAX_RECIPIENTS, lastInput);
  323. }
  324.  
  325. function awReturnHit(inputElement)
  326. {
  327.     dump("***** awReturnHit\n");
  328.     var row = awGetRowByInputElement(inputElement);
  329.     
  330.     if ( inputElement.value )
  331.     {
  332.         var nextInput = awGetInputElement(row+1);
  333.         if ( !nextInput )
  334.             awAppendNewRow(true);
  335.         else
  336.             awSetFocus(row+1, nextInput);
  337.     }
  338. }
  339.  
  340. function awInputChanged(inputElement)
  341. {
  342.     dump("awInputChanged\n");
  343. //    AutoCompleteAddress(inputElement);
  344.  
  345.     //Do we need to add a new row?
  346.     var lastInput = awGetInputElement(top.MAX_RECIPIENTS);
  347.     if ( lastInput && lastInput.value && !top.doNotCreateANewRow)
  348.         awAppendNewRow(false);
  349.     top.doNotCreateANewRow = false;
  350. }
  351.  
  352. function awInputElementName()
  353. {
  354.     if (inputElementType == "")
  355.         inputElementType = document.getElementById("address#1").localName;
  356.     return inputElementType;
  357. }
  358.  
  359. function awAppendNewRow(setFocus)
  360. {
  361.     var body = document.getElementById('addressList');
  362.     var treeitem1 = awGetTreeItem(1);
  363.     
  364.     if ( body && treeitem1 )
  365.     {
  366.         var newNode = awCopyNode(treeitem1, body, 0);
  367.         top.MAX_RECIPIENTS++;
  368.  
  369.         var input = newNode.getElementsByTagName(awInputElementName());
  370.         if ( input && input.length == 1 )
  371.         {
  372.             input[0].setAttribute("value", "");
  373.             input[0].setAttribute("id", "address#" + top.MAX_RECIPIENTS);
  374.         }
  375.         // focus on new input widget
  376.         if (setFocus && input )
  377.             awSetFocus(top.MAX_RECIPIENTS, input[0]);
  378.     }
  379. }
  380.  
  381.  
  382. // functions for accessing the elements in the addressing widget
  383.  
  384. function awGetInputElement(row)
  385. {
  386.     return document.getElementById("address#" + row);
  387. }
  388.  
  389. function awGetTreeRow(row)
  390. {
  391.     var body = document.getElementById('addressList');
  392.     
  393.     if ( body && row > 0)
  394.     {
  395.         var treerows = body.getElementsByTagName('treerow');
  396.         if ( treerows && treerows.length >= row )
  397.             return treerows[row-1];
  398.     }
  399.     return 0;
  400. }
  401.  
  402. function awGetTreeItem(row)
  403. {
  404.     var body = document.getElementById('addressList');
  405.     
  406.     if ( body && row > 0)
  407.     {
  408.         var treeitems = body.getElementsByTagName('treeitem');
  409.         if ( treeitems && treeitems.length >= row )
  410.             return treeitems[row-1];
  411.     }
  412.     return 0;
  413. }
  414.  
  415. function awGetRowByInputElement(inputElement)
  416. {
  417.     if ( inputElement )
  418.     {
  419.         var treerow;
  420.         var inputElementTreerow = inputElement.parentNode.parentNode;
  421.         
  422.         if ( inputElementTreerow )
  423.         {
  424.             for ( var row = 1;  (treerow = awGetTreeRow(row)); row++ )
  425.             {
  426.                 if ( treerow == inputElementTreerow )
  427.                 {
  428.                     return row;
  429.                 }
  430.             }
  431.         }
  432.     }
  433.     return 0;
  434. }
  435.  
  436.  
  437. // Copy Node - copy this node and insert ahead of the (before) node.  Append to end if before=0
  438. function awCopyNode(node, parentNode, beforeNode)
  439. {
  440.     var newNode = node.cloneNode(true);
  441.     
  442.     if ( beforeNode )
  443.         parentNode.insertBefore(newNode, beforeNode);
  444.     else
  445.         parentNode.appendChild(newNode);
  446.  
  447.     return newNode;
  448. }
  449.  
  450. // remove row
  451.  
  452. function awRemoveRow(row)
  453. {
  454.     var body = document.getElementById('addressList');
  455.     
  456.     awRemoveNodeAndChildren(body, awGetTreeItem(row));
  457.  
  458.     top.MAX_RECIPIENTS--;
  459. }
  460.  
  461. function awRemoveNodeAndChildren(parent, nodeToRemove)
  462. {
  463.     // children of nodes
  464.     var childNode;
  465.     
  466.     while ( nodeToRemove.childNodes && nodeToRemove.childNodes.length )
  467.     {
  468.         childNode = nodeToRemove.childNodes[0];
  469.     
  470.         awRemoveNodeAndChildren(nodeToRemove, childNode);
  471.     }
  472.     
  473.     parent.removeChild(nodeToRemove);
  474.  
  475. }
  476.  
  477. function awSetFocus(row, inputElement)
  478. {
  479.     top.awRow = row;
  480.     top.awInputElement = inputElement;
  481.     top.awFocusRetry = 0;
  482.     setTimeout("_awSetFocus();", 0);
  483. }
  484.  
  485. function _awSetFocus()
  486. {
  487.     var tree = document.getElementById('addressListTree');
  488.     try
  489.     {
  490.         var theNewRow = awGetTreeRow(top.awRow);
  491.         //temporary patch for bug 26344
  492. //        awFinishCopyNode(theNewRow);
  493.  
  494.         tree.ensureElementIsVisible(theNewRow);
  495.         top.awInputElement.focus();
  496.     }
  497.     catch(ex)
  498.     {
  499.         top.awFocusRetry ++;
  500.         if (top.awFocusRetry < 8)
  501.         {
  502.             dump("_awSetFocus failed, try it again...\n");
  503.             setTimeout("_awSetFocus();", 0);
  504.         }
  505.         else
  506.             dump("_awSetFocus failed, forget about it!\n");
  507.     }
  508. }
  509.  
  510.  
  511. //temporary patch for bug 26344 & 26528
  512. function awFinishCopyNode(node)
  513. {
  514.     msgCompose.ResetNodeEventHandlers(node);
  515.     return;
  516. }
  517.  
  518.  
  519. function awFinishCopyNodes()
  520. {
  521.     var treeChildren = document.getElementById('addressList');
  522.     awFinishCopyNode(treeChildren);
  523. }
  524.  
  525.  
  526. function awTabFromRecipient(element, event)
  527. {
  528.     //If we are le last element in the tree, we don't want to create a new row.
  529.     if (element == awGetInputElement(top.MAX_RECIPIENTS))
  530.         top.doNotCreateANewRow = true;
  531. }
  532.  
  533. function awGetNumberOfRecipients()
  534. {
  535.     return top.MAX_RECIPIENTS;
  536. }
  537.  
  538. function DragOverTree(event)
  539. {
  540.     var validFlavor = false;
  541.     var dragSession = null;
  542.     var retVal = true;
  543.  
  544.     var dragService = Components.classes["@mozilla.org/widget/dragservice;1"].getService();
  545.     if (dragService) 
  546.         dragService = dragService.QueryInterface(Components.interfaces.nsIDragService);
  547.     if (!dragService)    return(false);
  548.  
  549.     dragSession = dragService.getCurrentSession();
  550.     if (!dragSession)    return(false);
  551.  
  552.     if (dragSession.isDataFlavorSupported("text/nsabcard"))    validFlavor = true;
  553.     //XXX other flavors here...
  554.  
  555.     // touch the attribute on the rowgroup to trigger the repaint with the drop feedback.
  556.     if (validFlavor)
  557.     {
  558.         //XXX this is really slow and likes to refresh N times per second.
  559.         var rowGroup = event.target.parentNode.parentNode;
  560.         rowGroup.setAttribute ( "dd-triggerrepaint", 0 );
  561.         dragSession.canDrop = true;
  562.         // necessary??
  563.         retVal = false; // do not propagate message
  564.     }
  565.     return(retVal);
  566. }
  567.  
  568. function DropOnAddressListTree(event)
  569. {
  570.     var rdf = Components.classes["@mozilla.org/rdf/rdf-service;1"].getService();
  571.     if (rdf)   
  572.         rdf = rdf.QueryInterface(Components.interfaces.nsIRDFService);
  573.     if (!rdf) return(false);
  574.  
  575.     var dragService = Components.classes["@mozilla.org/widget/dragservice;1"].getService();
  576.     if (dragService) 
  577.         dragService = dragService.QueryInterface(Components.interfaces.nsIDragService);
  578.     if (!dragService)    return(false);
  579.     
  580.     var dragSession = dragService.getCurrentSession();
  581.     if ( !dragSession )    return(false);
  582.  
  583.     var trans = Components.classes["@mozilla.org/widget/transferable;1"].createInstance(Components.interfaces.nsITransferable);
  584.     if ( !trans ) return(false);
  585.     trans.addDataFlavor("text/nsabcard");
  586.  
  587.     for ( var i = 0; i < dragSession.numDropItems; ++i )
  588.     {
  589.         dragSession.getData ( trans, i );
  590.         dataObj = new Object();
  591.         bestFlavor = new Object();
  592.         len = new Object();
  593.         trans.getAnyTransferData ( bestFlavor, dataObj, len );
  594.         if ( dataObj )    dataObj = dataObj.value.QueryInterface(Components.interfaces.nsISupportsWString);
  595.         if ( !dataObj )    continue;
  596.  
  597.         // pull the URL out of the data object
  598.         var sourceID = dataObj.data.substring(0, len.value);
  599.         if (!sourceID)    continue;
  600.  
  601.         var cardResource = rdf.GetResource(sourceID);
  602.         var card = cardResource.QueryInterface(Components.interfaces.nsIAbCard);
  603.         
  604.         if (card.isMailList)
  605.             DropListAddress(card.name); 
  606.         else
  607.         {
  608.             var address;
  609.             if (card.name.length)
  610.                 address = card.name + " <" + card.primaryEmail + ">";
  611.             else
  612.                 address = card.primaryEmail;
  613.             DropListAddress(address); 
  614.         }
  615.         
  616.     }
  617.  
  618.     return(false);
  619. }
  620.  
  621. function DropListAddress(address) 
  622.     awClickEmptySpace(true);    //that will automatically set the focus on a new available row, and make sure is visible 
  623.     if (top.MAX_RECIPIENTS == 0)
  624.         top.MAX_RECIPIENTS = 1;
  625.     var lastInput = awGetInputElement(top.MAX_RECIPIENTS); 
  626.     lastInput.value = address; 
  627.     awAppendNewRow(true); 
  628. }
  629.