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