home *** CD-ROM | disk | FTP | other *** search
/ GameStar 2005 October / Gamestar_77_2005-10_dvd.iso / Programy / nsb-install-8-0.exe / chrome / toolkit.jar / content / mozapps / autofill / SavePrompt.js < prev    next >
Text File  |  2005-07-29  |  14KB  |  417 lines

  1. /* ***** BEGIN LICENSE BLOCK *****
  2.  * Version: NPL 1.1/GPL 2.0/LGPL 2.1
  3.  *
  4.  * The contents of this file are subject to the Netscape Public License
  5.  * Version 1.1 (the "License"); you may not use this file except in
  6.  * compliance with the License. You may obtain a copy of the License at
  7.  * http://www.mozilla.org/NPL/
  8.  *
  9.  * Software distributed under the License is distributed on an "AS IS" basis,
  10.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  11.  * for the specific language governing rights and limitations under the
  12.  * License.
  13.  *
  14.  * The Original Code is Mozilla Communicator client code.
  15.  *
  16.  * The Initial Developer of the Original Code is 
  17.  * Netscape Communications Corporation.
  18.  * Portions created by the Initial Developer are Copyright (C) 1998
  19.  * the Initial Developer. All Rights Reserved.
  20.  *
  21.  * Contributor(s):
  22.  *  Alec Flett  <alecf@netscape.com>
  23.  *  Ben Goodger <ben@netscape.com>
  24.  *  Blake Ross  <blakeross@telocity.com>
  25.  *  Joe Hewitt <hewitt@netscape.com>
  26.  *
  27.  * Alternatively, the contents of this file may be used under the terms of
  28.  * either the GNU General Public License Version 2 or later (the "GPL"), or 
  29.  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  30.  * in which case the provisions of the GPL or the LGPL are applicable instead
  31.  * of those above. If you wish to allow use of your version of this file only
  32.  * under the terms of either the GPL or the LGPL, and not to allow others to
  33.  * use your version of this file under the terms of the NPL, indicate your
  34.  * decision by deleting the provisions above and replace them with the notice
  35.  * and other provisions required by the GPL or the LGPL. If you do not delete
  36.  * the provisions above, a recipient may use your version of this file under
  37.  * the terms of any one of the NPL, the GPL or the LGPL.
  38.  *
  39.  * ***** END LICENSE BLOCK ***** */
  40.  
  41. // parameters to gCommonDialogParam.Get() are defined in nsPIPromptService.idl
  42. var gCommonDialogParam = 
  43.   window.arguments[0].QueryInterface(Components.interfaces.nsIDialogParamBlock);
  44.   
  45. function showControls()
  46. {
  47.   // This is called before onload fires, so we can't be certain that any elements
  48.   // in the document have their bindings ready, so don't call any methods/properties
  49.   // here on xul elements that come from xbl bindings.
  50.   
  51.   
  52.   //Passcard name field
  53.   initTextbox("name",4,6,false);
  54.   
  55. }
  56.  
  57. function setLabelForNode(aNode, aLabel, aIsLabelFlag)
  58. {
  59.   // This is for labels with possible accesskeys
  60.   var accessKeyIndex;
  61.   if (/^\&[^\&]/.test(aLabel)) { // access key is at the start
  62.    accessKeyIndex = 0;
  63.   } else {
  64.     accessKeyIndex = aLabel.search(/[^\&]\&[^\&]/) + 1;
  65.     if (accessKeyIndex == 0) {
  66.       accessKeyIndex = -1; // magic value for no accesskey
  67.     }
  68.   }
  69.  
  70.   // If a character has an & before it, then it should become an accesskey
  71.   if (accessKeyIndex >= 0 && accessKeyIndex < aLabel.length - 1) {
  72.     // This will also cause the accesskey attribute to be set via xbl
  73.     aNode.accessKey = aLabel.charAt(accessKeyIndex + 1);
  74.     // Set the label to the string without the &
  75.     aLabel = aLabel.substr(0, accessKeyIndex) + 
  76.              aLabel.substr(accessKeyIndex + 1);
  77.   }
  78.   aLabel = aLabel.replace(/\&\&/g, "&");
  79.   if (aIsLabelFlag) {    // Set text for <label> element
  80.     aNode.setAttribute("value", aLabel);
  81.   } else {    // Set text for other xul elements
  82.     aNode.label = aLabel;
  83.   }
  84. }
  85.  
  86. function savePromptOnLoad()
  87. {
  88.   // set the window title
  89.   window.title = gCommonDialogParam.GetString(12);
  90.  
  91.   // set the number of command buttons
  92.   var nButtons = gCommonDialogParam.GetInt(2);
  93.   var dialog = document.documentElement;
  94.   switch (nButtons) {
  95.     case 1:
  96.       dialog.getButton("cancel").hidden = true;
  97.       break;
  98.     case 4:
  99.       dialog.getButton("extra2").hidden = false;
  100.     case 3:
  101.       dialog.getButton("extra1").hidden = false;
  102.   }
  103.  
  104.   // display the main text
  105.   var messageText = gCommonDialogParam.GetString(0);
  106.   var messageParent = document.getElementById("info.box");
  107.   var messageParagraphs = messageText.split("\n");
  108.  
  109.   for (var i = 0; i < messageParagraphs.length; i++) {
  110.     var descriptionNode = document.createElement("description");
  111.     var text = document.createTextNode(messageParagraphs[i]);
  112.     descriptionNode.appendChild(text);
  113.     messageParent.appendChild(descriptionNode);
  114.   }
  115.  
  116.   setElementText("info.header", gCommonDialogParam.GetString(3), true);
  117.  
  118.   // set the icon
  119.   var iconElement = document.getElementById("info.icon");
  120.   var iconClass = gCommonDialogParam.GetString(2);
  121.   if (!iconClass)
  122.     iconClass = "alert-icon";
  123.   iconElement.setAttribute("class", iconElement.getAttribute("class") + " " + iconClass);
  124.  
  125.   switch (nButtons) {
  126.     case 4:
  127.       setLabelForNode(document.documentElement.getButton("extra2"), gCommonDialogParam.GetString(11));
  128.       // fall through
  129.     case 3:
  130.       setLabelForNode(document.documentElement.getButton("extra1"), gCommonDialogParam.GetString(10));
  131.       // fall through
  132.     default:
  133.     case 2:
  134.       var string = gCommonDialogParam.GetString(9);
  135.       if (string)
  136.         setLabelForNode(document.documentElement.getButton("cancel"), string);
  137.       // fall through
  138.     case 1:
  139.       string = gCommonDialogParam.GetString(8);
  140.       if (string)
  141.         setLabelForNode(document.documentElement.getButton("accept"), string);
  142.       break;
  143.   }
  144.  
  145.   // set default result to cancelled
  146.   gCommonDialogParam.SetInt(0, 1); 
  147.  
  148.   // initialize the checkbox
  149.   setCheckbox(gCommonDialogParam.GetString(1), gCommonDialogParam.GetInt(1));
  150.   
  151.   //initialize the radiobuttons
  152.   setRadiobutton("radio0",gCommonDialogParam.GetString(13));
  153.   setRadiobutton("radio1",gCommonDialogParam.GetString(14));
  154.   
  155.   //set radiogroup
  156.   setRadiogroup(0);
  157.   
  158.   //set dropdown list
  159.   setDropdown(gCommonDialogParam.GetString(16));
  160.   
  161.   if (gCommonDialogParam.GetInt(3) == 0) // If no text fields
  162.   {
  163.     var dButton;
  164.     var defaultButton = gCommonDialogParam.GetInt(5);
  165.     switch (defaultButton) {
  166.       case 3:
  167.         dButton = document.documentElement.getButton("extra2");
  168.         break;
  169.       case 2:
  170.         dButton = document.documentElement.getButton("extra1");
  171.         break;
  172.       case 1:
  173.         dButton = document.documentElement.getButton("cancel");
  174.         break;
  175.       default:
  176.       case 0:
  177.         dButton = document.documentElement.getButton("accept");
  178.         break;
  179.     }
  180.     // move the default attribute and focus from the accept button
  181.     // to the one specified in the dialog params
  182.     document.documentElement.getButton("accept").setAttribute("default",false);
  183.     dButton.setAttribute("default", true);
  184.     dButton.focus();
  185.   }
  186.  
  187.   if (gCommonDialogParam.GetInt(6) != 0) // delay button enable
  188.   {
  189.     var delayInterval = 2000;
  190.     try {
  191.       var prefs = Components.classes["@mozilla.org/preferences-service;1"]
  192.                   .getService(Components.interfaces.nsIPrefBranch);
  193.       delayInterval = prefs.getIntPref("security.dialog_enable_delay");
  194.     } catch (e) {}
  195.  
  196.     document.documentElement.getButton("accept").disabled = true;
  197.     document.documentElement.getButton("extra1").disabled = true;
  198.     document.documentElement.getButton("extra2").disabled = true;
  199.  
  200.     setTimeout(commonDialogReenableButtons, delayInterval);
  201.   }
  202.  
  203.   getAttention();
  204. }
  205.  
  206. function commonDialogReenableButtons()
  207. {
  208.   document.documentElement.getButton("accept").disabled = false;
  209.   document.documentElement.getButton("extra1").disabled = false;
  210.   document.documentElement.getButton("extra2").disabled = false;
  211. }
  212.  
  213. function initTextbox(aName, aLabelIndex, aValueIndex, aAlwaysLabel)
  214. {
  215.   unHideElementById(aName+"Container");
  216.  
  217.   var label = aLabelIndex < 0 ? "" : gCommonDialogParam.GetString(aLabelIndex);
  218.   if (label || aAlwaysLabel && !label)
  219.     setElementText(aName+"Label", label);
  220.     
  221.   var value = aValueIndex < 0 ? "" : gCommonDialogParam.GetString(aValueIndex);
  222.   var textbox = document.getElementById(aName + "Textbox");
  223.   textbox.setAttribute("value", value);
  224. }
  225.  
  226. function setElementText(aElementID, aValue, aChildNodeFlag)
  227. {
  228.   var element = document.getElementById(aElementID);
  229.   if (!aChildNodeFlag && element) {
  230.     setLabelForNode(element, aValue, true);
  231.   } else if (aChildNodeFlag && element) {
  232.     element.appendChild(document.createTextNode(aValue));
  233.   }
  234. }
  235.  
  236. function setCheckbox(aChkMsg, aChkValue)
  237. {
  238.   dump("*******setting checkbox, message = " + aChkMsg);
  239.   if (aChkMsg) {
  240.     // XXX Would love to use hidden instead of collapsed, but the checkbox
  241.     // fails to size itself properly when I do this.
  242.     document.getElementById("checkboxContainer").removeAttribute("collapsed");
  243.     
  244.     var checkboxElement = document.getElementById("checkbox");
  245.     setLabelForNode(checkboxElement, aChkMsg);
  246.     checkboxElement.checked = aChkValue > 0;
  247.   }
  248. }
  249.  
  250. function setRadiobutton(aRBId, aRBMsg)
  251. {
  252.   if (aRBMsg) {
  253.     
  254.     document.getElementById(aRBId+"Container").removeAttribute("collapsed");
  255.     var rbElement = document.getElementById(aRBId);
  256.     setLabelForNode(rbElement, aRBMsg);
  257.   }
  258. }
  259.  
  260. function setDropdown(aDDMsg)
  261. {  
  262.   document.getElementById("dropdownContainer").removeAttribute("collapsed");
  263.   var ddElement = document.getElementById("dropdownLabel");
  264.   setLabelForNode(ddElement,aDDMsg,true);
  265.   //ddElement.value = aDDMsg;
  266.   
  267.   dump("*****DURGA****");
  268.   dump("dropdown label = " + aDDMsg);
  269.   
  270.   var stringBundle = document.getElementById("strings");
  271.   
  272.   var dropdownlist = document.getElementById("dropdownMenulist");
  273.   dropdownlist.appendItem(stringBundle.getString("displayPromptText"),0);
  274.   dropdownlist.appendItem(stringBundle.getString("fillPasswordText"),1);
  275.   dropdownlist.appendItem(stringBundle.getString("autologinText"),2);
  276.   dropdownlist.appendItem(stringBundle.getString("doNothingText"),3);
  277.   
  278.   dropdownlist.selectedIndex = gCommonDialogParam.GetInt(10);
  279.   gCommonDialogParam.SetInt(10, dropdownlist.selectedItem.value);
  280. }
  281.  
  282. function setRadiogroup(aRBIndex)
  283. {
  284.   if (aRBIndex) {   
  285.     var rbGroupElement = document.getElementById("radiogroup");
  286.     rbGroupElement.selectedIndex = aRBIndex;
  287.     
  288.     gCommonDialogParam.SetInt(7, 1);
  289.     gCommonDialogParam.SetInt(8, 0);
  290.   }
  291. }
  292.  
  293. function unHideElementById(aElementID)
  294. {
  295.   var element = document.getElementById(aElementID);
  296.   element.hidden = false;
  297. }
  298.  
  299. function hideElementById(aElementID)
  300. {
  301.   var element = document.getElementById(aElementID)
  302.   element.hidden = true;
  303. }
  304.  
  305. function isVisible(aElementId)
  306. {
  307.   return document.getElementById(aElementId).hasAttribute("hidden");
  308. }
  309.  
  310. function onCheckboxClick(aCheckboxElement)
  311. {
  312.   gCommonDialogParam.SetInt(1, aCheckboxElement.checked);
  313.   dump("checkbox value = " + gCommonDialogParam.GetInt(1));
  314.   
  315.   //check if protect with master password and master password is not set
  316.   if(gCommonDialogParam.GetInt(1) && !IsMasterPasswordSet() && gCommonDialogParam.GetInt(7))  
  317.   {
  318.     // if master password already exists, pass empty string to use default text
  319.       var dialogTitle = GetMasterPasswordText();
  320.       var obj = new Object( );
  321.       obj.res = "";
  322.           
  323.       dump("************calling mp***********");
  324.       
  325.       window.openDialog("chrome://browser/content/pref/pref-masterpass.xul",
  326.           "_blank", "chrome,dialog,modal", dialogTitle,obj);
  327.                             
  328.       dump("*******save prompt obj.res = " + obj.res);
  329.       
  330.       if(obj.res != "ok")
  331.       {
  332.           dump("*****saveprompt not ok*********");
  333.           setCheckbox(gCommonDialogParam.GetString(1), 0);
  334.           gCommonDialogParam.SetInt(1, 0);
  335.       }
  336.   }
  337. }
  338.  
  339. function onDropdownClick(aDropdownElement)
  340. {
  341.   gCommonDialogParam.SetInt(10, aDropdownElement.selectedItem.value);
  342.   dump("option selected = " + gCommonDialogParam.GetInt(10));
  343. }
  344.  
  345. function onRadio1Selected()
  346. {
  347.   document.getElementById("nameLabel").disabled = true;
  348.   document.getElementById("nameTextbox").disabled = true;
  349.   document.getElementById("checkbox").disabled = true;
  350.   document.getElementById("dropdownLabel").disabled = true;
  351.   document.getElementById("dropdownMenulist").disabled = true;
  352.   gCommonDialogParam.SetInt(7, 0);
  353.   gCommonDialogParam.SetInt(8, 1);
  354.   dump("radio button 1 selected = " + gCommonDialogParam.GetInt(8));
  355. }
  356.  
  357. function onRadio0Selected()
  358. {
  359.   document.getElementById("nameLabel").removeAttribute("disabled");
  360.   document.getElementById("nameTextbox").removeAttribute("disabled");
  361.   document.getElementById("checkbox").removeAttribute("disabled");
  362.   document.getElementById("dropdownLabel").removeAttribute("disabled");
  363.   document.getElementById("dropdownMenulist").removeAttribute("disabled");
  364.   gCommonDialogParam.SetInt(7, 1);
  365.   gCommonDialogParam.SetInt(8, 0);
  366.   dump("radio button 0 selected = " + gCommonDialogParam.GetInt(7));
  367. }
  368.  
  369. function savePromptOnAccept()
  370. {
  371.     dump('savePromptOnAccept()\n');
  372.   gCommonDialogParam.SetInt(0, 0); // say that ok was pressed
  373.  
  374.   var numTextBoxes = gCommonDialogParam.GetInt(3);
  375.   var textboxIsPassword1 = gCommonDialogParam.GetInt(4) == 1;
  376.   
  377.   //set passcard name
  378.   gCommonDialogParam.SetString(6, document.getElementById("nameTextbox").value);
  379.   
  380.   dump("value in name textbox = " + gCommonDialogParam.GetString(6));
  381.   
  382.   gCommonDialogParam.SetInt(10, document.getElementById("dropdownMenulist").selectedItem.value);
  383.  
  384.   if (numTextBoxes >= 1) {
  385.     var editField1;
  386.     if (textboxIsPassword1)
  387.       editField1 = document.getElementById("password1Textbox");
  388.     else
  389.       editField1 = document.getElementById("loginTextbox");
  390.     gCommonDialogParam.SetString(6, editField1.value);
  391.   }
  392.  
  393.   if (numTextBoxes == 2) {
  394.     var editField2;
  395.     if (textboxIsPassword1)
  396.       // we had two password fields
  397.       editField2 = document.getElementById("password2Textbox");
  398.     else
  399.       // we only had one password field (and one login field)
  400.       editField2 = document.getElementById("password1Textbox");
  401.     gCommonDialogParam.SetString(7, editField2.value);
  402.   }
  403.   return true;
  404. }
  405.  
  406. function savePromptOnExtra1()
  407. {
  408.   gCommonDialogParam.SetInt(0, 2);
  409.   window.close();
  410. }
  411.  
  412. function savePromptOnExtra2()
  413. {
  414.   gCommonDialogParam.SetInt(0, 3);
  415.   window.close();
  416. }
  417.