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 / abCardOverlay.js < prev    next >
Text File  |  2001-02-14  |  13KB  |  382 lines

  1. /* -*- Mode: Java; tab-width: 4; 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.org code.
  14.  *
  15.  * The Initial Developer of the Original Code is Netscape
  16.  * Communications Corporation.  Portions created by Netscape are
  17.  * Copyright (C) 1998 Netscape Communications Corporation. All
  18.  * Rights Reserved.
  19.  *
  20.  * Contributor(s): 
  21.  */
  22.  
  23. var editCard;
  24. var gOnSaveListeners = new Array;
  25.  
  26. var Bundle = srGetStrBundle("chrome://messenger/locale/addressbook/addressBook.properties");
  27.  
  28. function OnLoadNewCard()
  29. {
  30.     InitEditCard();
  31.  
  32.     doSetOKCancel(NewCardOKButton, 0);
  33.     
  34.     var cardproperty = Components.classes["@mozilla.org/addressbook/cardproperty;1"].createInstance();
  35.     cardproperty = cardproperty.QueryInterface(Components.interfaces.nsIAbCard);
  36.  
  37.     editCard.card = cardproperty;
  38.     editCard.okCallback = 0;
  39.     editCard.titleProperty = "newCardTitle"
  40.  
  41.     if (window.arguments && window.arguments[0])
  42.     {
  43.         if ( window.arguments[0].selectedAB )
  44.             editCard.selectedAB = window.arguments[0].selectedAB;
  45.         else
  46.             editCard.selectedAB = "abdirectory://abook.mab";
  47.  
  48.     // we may have been given properties to pre-initialize the window with....
  49.     // we'll fill these in here...
  50.     if (window.arguments[0].primaryEmail)
  51.       editCard.card.primaryEmail = window.arguments[0].primaryEmail;
  52.     if (window.arguments[0].displayName)
  53.       editCard.card.displayName = window.arguments[0].displayName;
  54.     }
  55.  
  56.     // set popup with address book names
  57.     var abPopup = document.getElementById('abPopup');
  58.     if ( abPopup )
  59.     {
  60.         var menupopup = document.getElementById('abPopup-menupopup');
  61.         
  62.         if ( editCard.selectedAB && menupopup && menupopup.childNodes )
  63.         {
  64.             for ( var index = menupopup.childNodes.length - 1; index >= 0; index-- )
  65.             {
  66.                 if ( menupopup.childNodes[index].getAttribute('data') == editCard.selectedAB )
  67.                 {
  68.                     abPopup.value = menupopup.childNodes[index].getAttribute('value');
  69.                     abPopup.data = menupopup.childNodes[index].getAttribute('data');
  70.                     break;
  71.                 }
  72.             }
  73.         }
  74.     }
  75.  
  76.     GetCardValues(editCard.card, document);
  77.  
  78.     //// FIX ME - looks like we need to focus on both the text field and the tab widget
  79.     //// probably need to do the same in the addressing widget
  80.     
  81.     // focus on first name
  82.     var firstName = document.getElementById('FirstName');
  83.     if ( firstName )
  84.         firstName.focus();
  85.     moveToAlertPosition();
  86. }
  87.  
  88.  
  89. function OnLoadEditCard()
  90. {
  91.     InitEditCard();
  92.     
  93.     doSetOKCancel(EditCardOKButton, 0);
  94.  
  95.     editCard.titleProperty = "editCardTitle";
  96.  
  97.     if (window.arguments && window.arguments[0])
  98.     {
  99.         if ( window.arguments[0].card )
  100.             editCard.card = window.arguments[0].card;
  101.         if ( window.arguments[0].okCallback )
  102.             editCard.okCallback = window.arguments[0].okCallback;
  103.         if ( window.arguments[0].abURI )
  104.             editCard.abURI = window.arguments[0].abURI;
  105.     }
  106.             
  107.     // set global state variables
  108.     // if first or last name entered, disable generateDisplayName
  109.     if ( editCard.generateDisplayName && (editCard.card.firstName.length +
  110.                                           editCard.card.lastName.length +
  111.                                           editCard.card.displayName.length > 0) )
  112.     {
  113.         editCard.generateDisplayName = false;
  114.     }
  115.     
  116.     GetCardValues(editCard.card, document);
  117.  
  118.     var displayName = editCard.card.displayName;
  119.     top.window.title = Bundle.formatStringFromName(editCard.titleProperty,
  120.                                                    [ displayName ], 1);
  121. }
  122.  
  123. function RegisterSaveListener(func)
  124. {
  125.   var length = gOnSaveListeners.length;
  126.   gOnSaveListeners[length] = func;  
  127. }
  128.  
  129. function CallSaveListeners()
  130. {
  131.     for ( var i = 0; i < gOnSaveListeners.length; i++ )
  132.         gOnSaveListeners[i]();
  133. }
  134.  
  135. function InitEditCard()
  136. {
  137.     // create editCard object that contains global variables for editCard.js
  138.     editCard = new Object;
  139.     
  140.     // get pointer to nsIPref object
  141.     var prefs = Components.classes["@mozilla.org/preferences;1"];
  142.     if ( prefs )
  143.     {
  144.         prefs = prefs.getService();
  145.         if ( prefs )
  146.         {
  147.             prefs = prefs.QueryInterface(Components.interfaces.nsIPref);
  148.             editCard.prefs = prefs;
  149.         }
  150.     }
  151.             
  152.     // get specific prefs that editCard will need
  153.     if ( prefs )
  154.     {
  155.         try {
  156.             editCard.displayLastNameFirst = prefs.GetBoolPref("mail.addr_book.displayName.lastnamefirst");
  157.             editCard.generateDisplayName = prefs.GetBoolPref("mail.addr_book.displayName.autoGeneration");
  158.             editCard.lastFirstSeparator = ", ";
  159.             editCard.firstLastSeparator = " ";
  160.         }
  161.         catch (ex) {
  162.             dump("failed to get pref\n");
  163.         }
  164.     }
  165. }
  166.  
  167. function NewCardOKButton()
  168. {
  169.     var popup = document.getElementById('abPopup');
  170.     if ( popup )
  171.     {
  172.         var uri = popup.getAttribute('data');
  173.         
  174.         // FIX ME - hack to avoid crashing if no ab selected because of blank option bug from template
  175.         // should be able to just remove this if we are not seeing blank lines in the ab popup
  176.         if ( !uri )
  177.             return false;  // don't close window
  178.         // -----
  179.         
  180.         if ( editCard.card )
  181.         {
  182.             SetCardValues(editCard.card, document);
  183.             editCard.card.addCardToDatabase(uri);
  184.               CallSaveListeners();
  185.         }
  186.     }    
  187.     
  188.     return true;    // close the window
  189. }
  190.  
  191. function EditCardOKButton()
  192. {
  193.     SetCardValues(editCard.card, document);
  194.     
  195.     editCard.card.editCardToDatabase(editCard.abURI);
  196.       CallSaveListeners();
  197.     
  198.     // callback to allow caller to update
  199.     if ( editCard.okCallback )
  200.         editCard.okCallback();
  201.     
  202.     return true;    // close the window
  203. }
  204.  
  205.  
  206. // Move the data from the cardproperty to the dialog
  207. function GetCardValues(cardproperty, doc)
  208. {
  209.     if ( cardproperty )
  210.     {
  211.         doc.getElementById('FirstName').value = cardproperty.firstName;
  212.         doc.getElementById('LastName').value = cardproperty.lastName;
  213.         doc.getElementById('DisplayName').value = cardproperty.displayName;
  214.         doc.getElementById('NickName').value = cardproperty.nickName;
  215.         
  216.         doc.getElementById('PrimaryEmail').value = cardproperty.primaryEmail;
  217.         doc.getElementById('SecondEmail').value = cardproperty.secondEmail;
  218.  
  219.         var checkbox = doc.getElementById('SendPlainText');
  220.         if (checkbox)
  221.         {
  222.             if (cardproperty.sendPlainText)
  223.                 checkbox.checked = true;
  224.             else
  225.                 checkbox.removeAttribute('checked', 'false');
  226.         }
  227.         doc.getElementById('WorkPhone').value = cardproperty.workPhone;
  228.         doc.getElementById('HomePhone').value = cardproperty.homePhone;
  229.         doc.getElementById('FaxNumber').value = cardproperty.faxNumber;
  230.         doc.getElementById('PagerNumber').value = cardproperty.pagerNumber;
  231.         doc.getElementById('CellularNumber').value = cardproperty.cellularNumber;
  232.  
  233.         doc.getElementById('HomeAddress').value = cardproperty.homeAddress;
  234.         doc.getElementById('HomeAddress2').value = cardproperty.homeAddress2;
  235.         doc.getElementById('HomeCity').value = cardproperty.homeCity;
  236.         doc.getElementById('HomeState').value = cardproperty.homeState;
  237.         doc.getElementById('HomeZipCode').value = cardproperty.homeZipCode;
  238.         doc.getElementById('HomeCountry').value = cardproperty.homeCountry;
  239.         doc.getElementById('WebPage2').value = cardproperty.webPage2;
  240.  
  241.         doc.getElementById('JobTitle').value = cardproperty.jobTitle;
  242.         doc.getElementById('Department').value = cardproperty.department;
  243.         doc.getElementById('Company').value = cardproperty.company;
  244.         doc.getElementById('WorkAddress').value = cardproperty.workAddress;
  245.         doc.getElementById('WorkAddress2').value = cardproperty.workAddress2;
  246.         doc.getElementById('WorkCity').value = cardproperty.workCity;
  247.         doc.getElementById('WorkState').value = cardproperty.workState;
  248.         doc.getElementById('WorkZipCode').value = cardproperty.workZipCode;
  249.         doc.getElementById('WorkCountry').value = cardproperty.workCountry;
  250.         doc.getElementById('WebPage1').value = cardproperty.webPage1;
  251.  
  252.         doc.getElementById('Custom1').value = cardproperty.custom1;
  253.         doc.getElementById('Custom2').value = cardproperty.custom2;
  254.         doc.getElementById('Custom3').value = cardproperty.custom3;
  255.         doc.getElementById('Custom4').value = cardproperty.custom4;
  256.         doc.getElementById('Notes').value = cardproperty.notes;
  257.     }
  258. }
  259.  
  260.  
  261. // Move the data from the dialog to the cardproperty to be stored in the database
  262. function SetCardValues(cardproperty, doc)
  263. {
  264.     if (cardproperty)
  265.     {
  266.         cardproperty.firstName = doc.getElementById('FirstName').value;
  267.         cardproperty.lastName = doc.getElementById('LastName').value;
  268.         cardproperty.displayName = doc.getElementById('DisplayName').value;
  269.         cardproperty.nickName = doc.getElementById('NickName').value;
  270.         
  271.         cardproperty.primaryEmail = doc.getElementById('PrimaryEmail').value;
  272.         cardproperty.secondEmail = doc.getElementById('SecondEmail').value;
  273.  
  274.         var checkbox = doc.getElementById('SendPlainText');
  275.         if (checkbox)
  276.             cardproperty.sendPlainText = checkbox.checked;
  277.         
  278.         cardproperty.workPhone = doc.getElementById('WorkPhone').value;
  279.         cardproperty.homePhone = doc.getElementById('HomePhone').value;
  280.         cardproperty.faxNumber = doc.getElementById('FaxNumber').value;
  281.         cardproperty.pagerNumber = doc.getElementById('PagerNumber').value;
  282.         cardproperty.cellularNumber = doc.getElementById('CellularNumber').value;
  283.  
  284.         cardproperty.homeAddress = doc.getElementById('HomeAddress').value;
  285.         cardproperty.homeAddress2 = doc.getElementById('HomeAddress2').value;
  286.         cardproperty.homeCity = doc.getElementById('HomeCity').value;
  287.         cardproperty.homeState = doc.getElementById('HomeState').value;
  288.         cardproperty.homeZipCode = doc.getElementById('HomeZipCode').value;
  289.         cardproperty.homeCountry = doc.getElementById('HomeCountry').value;
  290.         cardproperty.webPage2 = CleanUpWebPage(doc.getElementById('WebPage2').value);
  291.  
  292.         cardproperty.jobTitle = doc.getElementById('JobTitle').value;
  293.         cardproperty.department = doc.getElementById('Department').value;
  294.         cardproperty.company = doc.getElementById('Company').value;
  295.         cardproperty.workAddress = doc.getElementById('WorkAddress').value;
  296.         cardproperty.workAddress2 = doc.getElementById('WorkAddress2').value;
  297.         cardproperty.workCity = doc.getElementById('WorkCity').value;
  298.         cardproperty.workState = doc.getElementById('WorkState').value;
  299.         cardproperty.workZipCode = doc.getElementById('WorkZipCode').value;
  300.         cardproperty.workCountry = doc.getElementById('WorkCountry').value;
  301.         cardproperty.webPage1 = CleanUpWebPage(doc.getElementById('WebPage1').value);
  302.  
  303.         cardproperty.custom1 = doc.getElementById('Custom1').value;
  304.         cardproperty.custom2 = doc.getElementById('Custom2').value;
  305.         cardproperty.custom3 = doc.getElementById('Custom3').value;
  306.         cardproperty.custom4 = doc.getElementById('Custom4').value;
  307.         cardproperty.notes = doc.getElementById('Notes').value;
  308.     }
  309. }
  310.  
  311. function CleanUpWebPage(webPage)
  312. {
  313.     // no :// yet so we should add something
  314.     if ( webPage.length && webPage.search("://") == -1 )
  315.     {
  316.         // check for missing / on http://
  317.         if ( webPage.substr(0, 6) == "http:/" )
  318.             return( "http://" + webPage.substr(6) );
  319.         else
  320.             return( "http://" + webPage );
  321.     }
  322.     else
  323.         return(webPage);
  324. }
  325.  
  326.  
  327. function NewCardCancelButton()
  328. {
  329.     top.window.close();
  330. }
  331.  
  332. function EditCardCancelButton()
  333. {
  334.     top.window.close();
  335. }
  336.  
  337. function GenerateDisplayName()
  338. {
  339.     if ( editCard.generateDisplayName )
  340.     {
  341.         var displayName;
  342.         
  343.         var firstNameField = document.getElementById('FirstName');
  344.         var lastNameField = document.getElementById('LastName');
  345.         var displayNameField = document.getElementById('DisplayName');
  346.  
  347.         /* todo: i18N work todo here */
  348.         /* this used to be XP_GetString(MK_ADDR_FIRST_LAST_SEP) */
  349.         
  350.         var separator = "";
  351.         if ( lastNameField.value && firstNameField.value )
  352.         {
  353.             if ( editCard.displayLastNameFirst )
  354.                  separator = editCard.lastFirstSeparator;
  355.             else
  356.                  separator = editCard.firstLastSeparator;
  357.         }
  358.         
  359.         if ( editCard.displayLastNameFirst )
  360.             displayName = lastNameField.value + separator + firstNameField.value;
  361.         else
  362.             displayName = firstNameField.value + separator + lastNameField.value;
  363.             
  364.         displayNameField.value = displayName;
  365.         top.window.title = Bundle.formatStringFromName(editCard.titleProperty,
  366.                                                        [ displayName ], 1);
  367.     }
  368. }
  369.  
  370. function DisplayNameChanged()
  371. {
  372.     // turn off generateDisplayName if the user changes the display name
  373.     editCard.generateDisplayName = false;
  374.  
  375.     var displayName = document.getElementById('DisplayName').value;
  376.     var title = Bundle.formatStringFromName(editCard.titleProperty,
  377.                                             [ displayName ], 1);
  378.     if ( top.window.title != title )
  379.         top.window.title = title;
  380. }
  381.  
  382.