home *** CD-ROM | disk | FTP | other *** search
/ ftp.swcp.com / ftp.swcp.com.zip / ftp.swcp.com / mac / mozilla-macos9-1.3.1.sea.bin / Mozilla1.3.1 / Chrome / comm.jar / content / wallet / walletOverlay.js < prev    next >
Text File  |  2003-06-08  |  14KB  |  369 lines

  1. /* -*- Mode: Java; tab-width: 4; c-basic-offset: 4; -*-
  2.  * 
  3.  * The contents of this file are subject to the Netscape Public License
  4.  * Version 1.0 (the "NPL"); you may not use this file except in
  5.  * compliance with the NPL.  You may obtain a copy of the NPL at
  6.  * http://www.mozilla.org/NPL/
  7.  *
  8.  * Software distributed under the NPL is distributed on an "AS IS" basis,
  9.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
  10.  * for the specific language governing rights and limitations under the
  11.  * NPL.
  12.  *
  13.  * The Initial Developer of this code under the NPL is Netscape
  14.  * Communications Corporation.  Portions created by Netscape are
  15.  * Copyright (C) 1998 Netscape Communications Corporation.  All Rights
  16.  * Reserved.
  17.  *
  18.  * Contributor(s):
  19.  */
  20.  
  21.     var gIsEncrypted = -1;
  22.     var gWalletService = -1;
  23.  
  24.     // states that the capture and prefill menu items can be in
  25.     const hide = -1;   // don't show menu item
  26.     const disable = 0; // show menu item but gray it out
  27.     const enable = 1;  // show menu item normally
  28.  
  29.     // Set the disabled attribute of specified item.
  30.     //   If the value is false, then it removes the attribute
  31.     function setDisabledAttr(id, val) {
  32.       var elem = document.getElementById(id);
  33.       if (elem) {
  34.         if (val) {
  35.           elem.setAttribute("disabled", val);
  36.         } else {
  37.           elem.removeAttribute("disabled");
  38.         }
  39.       }
  40.     }
  41.  
  42.     // Show/hide one item (specified via name or the item element itself).
  43.     function showItem(itemOrId, show) {
  44.       var item = null;
  45.       if (itemOrId.constructor == String) {
  46.         // Argument specifies item id.
  47.         item = document.getElementById(itemOrId);
  48.       } else {
  49.         // Argument is the item itself.
  50.         item = itemOrId;
  51.       }
  52.       if (item) {
  53.         var styleIn = item.getAttribute("style");
  54.         var styleOut = styleIn;
  55.         if (show) {
  56.           // Remove style="display:none;".
  57.           styleOut = styleOut.replace("display:none;", "");
  58.  
  59.         } else {
  60.           // Set style="display:none;".
  61.           if (styleOut.indexOf("display:none;") == -1) {
  62.             // Add style the first time we need to.
  63.             styleOut += "display:none;";
  64.           }
  65.         }
  66.         // Only set style if it's different.
  67.         if (styleIn != styleOut) {
  68.           item.setAttribute("style", styleOut);
  69.         }
  70.       }
  71.     }
  72.  
  73. /* form toolbar is out
  74.     var firstTime = true;
  75.  
  76.     function initToolbarItems() {
  77.  
  78.       // This routine determines whether or not to display the form-manager toolbar and,
  79.       // if so, which buttons on the toolbar are to be enabled.  We need to reexecute
  80.       // this routine whenever the form-manager dialog finishes because saved values might
  81.       // have been added/removed which could affect the enable/disable state of the buttons.
  82.  
  83.       if (firstTime) {
  84.         // Force initToolbarItems to be executed upon return from viewing prefs.
  85.         //   This is necessary in case the form-manager dialog was invoked from the
  86.         //   pref panel.  See next block of code for more details.
  87.         var pref = document.getElementById("menu_preferences");
  88.         if (pref) {
  89.           oncommand = pref.getAttribute("oncommand");
  90.           pref.setAttribute("oncommand", oncommand+";initToolbarItems()");
  91.           firstTime = false;
  92.         }
  93.       }
  94.  
  95.       // get the form-manager toolbar
  96.       var cmd_viewformToolbar = document.getElementById("cmd_viewformtoolbar");
  97.       if (!cmd_viewformToolbar) {
  98.         // This happens when you access the form-manager dialog from the prefs panel
  99.         // Not sure yet how to get access to items in navigator in that case
  100.         // So instead we will execute initToolbarItems when edit->prefs returns (that's
  101.         // what above block of code involving firstTime accomplished.
  102.         return;
  103.       }
  104.  
  105.       // keep form toolbar hidden if checkbox in view menu so indicates
  106.       var checkValue = cmd_viewformToolbar.getAttribute("checked");
  107.       if (checkValue == "false") {
  108.         showItem("formToolbar", false);
  109.         return;
  110.       }
  111.  
  112.       // hide form toolbar if three or less text elements in form
  113.       var state = getState(3);
  114.       showItem("formToolbar", (state.prefill != hide));
  115.  
  116.       // enable prefill button if there is at least one saved value for the form
  117.       setDisabledAttr("formPrefill", (state.prefill == disable));
  118.     }
  119. */
  120.  
  121.     function formShow() {
  122.       window.openDialog(
  123.           "chrome://communicator/content/wallet/WalletViewer.xul",
  124.           "_blank",
  125.           "chrome,titlebar,resizable=yes");
  126. /* form toolbar is out
  127.        initToolbarItems(); // need to redetermine which buttons in form toolbar to enable
  128. */
  129.     }
  130.  
  131.     // Capture the values that are filled in on the form being displayed.
  132.     function formCapture() {
  133.       var walletService = Components.classes["@mozilla.org/wallet/wallet-service;1"].getService(Components.interfaces.nsIWalletService);
  134.       walletService.WALLET_RequestToCapture(window._content);
  135.     }
  136.  
  137.     // Prefill the form being displayed.
  138.     function formPrefill() {
  139.       var walletService = Components.classes["@mozilla.org/wallet/wallet-service;1"].getService(Components.interfaces.nsIWalletService);
  140.       try {
  141.         walletService.WALLET_Prefill(false, window._content);
  142.         window.openDialog("chrome://communicator/content/wallet/WalletPreview.xul",
  143.                           "_blank", "chrome,modal=yes,dialog=yes,all, width=504, height=436");
  144.       } catch(e) {
  145.       }
  146.     }
  147.  
  148. /*
  149.     // Prefill the form being displayed without bringing up the preview window.
  150.     function formQuickPrefill() {
  151.       gWalletService.WALLET_Prefill(true, window._content);
  152.     }
  153. */
  154.  
  155.     var elementCount;
  156.  
  157.     // Walk through the DOM to determine how a capture or prefill item is to appear.
  158.     //   returned value:
  159.     //      hide, disable, enable for .capture and .prefill
  160.     function getStateFromFormsArray(content, threshhold) {
  161.       var formsArray = content.document.forms;
  162.       if (!formsArray) {
  163.         return {capture: hide, prefill: hide};
  164.       }
  165.  
  166.       var form;
  167.       var bestState = {capture: hide, prefill: hide};
  168.  
  169.       for (form=0; form<formsArray.length; form++) {
  170.         var elementsArray = formsArray[form].elements;
  171.         var element;
  172.         for (element=0; element<elementsArray.length; element++) {
  173.           var type = elementsArray[element].type;
  174.           if ((type=="") || (type=="text") || (type=="select-one")) {
  175.             // we have a form with at least one text or select element
  176.             if (type != "select-one") {
  177.               elementCount++;
  178.             }
  179.  
  180.             /* If database is encrypted and user has not yet supplied master password,
  181.              * we won't be able to access the data.  In that case, enable the item rather
  182.              * than asking user for password at this time.  Otherwise you'll be asking for
  183.              * the password whenever user clicks on edit menu or context menu
  184.              */
  185.             try {
  186.               if (gIsEncrypted == -1)
  187.                 gIsEncrypted = this.pref.getBoolPref("wallet.crypto");
  188.               if (gIsEncrypted) {
  189.                 // database is encrypted, see if it is still locked
  190. //              if (locked) { -- there's currently no way to make such a test
  191.                   // it's encrypted and locked, we lose
  192.                   elementCount = threshhold+1;
  193.                   return {capture: enable, prefill: enable};
  194. //              }
  195.               }
  196.             } catch(e) {
  197.               // there is no crypto pref so database could not possible be encrypted
  198.             }
  199.  
  200.             // since we know there is at least one text or select element, we can unhide
  201.             // the menu items.  That means doing nothing if state is already "enable" but
  202.             // changing state to "disable" if it is currently "hide".
  203.             for (var j in bestState) {
  204.               if (bestState[j] == hide) {
  205.                 bestState[j] = disable;
  206.               }
  207.             }
  208.             
  209.             var value;
  210.  
  211.             // obtain saved values if any and store in array called valueList
  212.             var valueList;
  213.             var valueSequence = gWalletService.WALLET_PrefillOneElement
  214.               (content, elementsArray[element]);
  215.             // result is a linear sequence of values, each preceded by a separator character
  216.             // convert linear sequence of values into an array of values
  217.             if (valueSequence) {
  218.               var separator = valueSequence[0];
  219.               valueList = valueSequence.substring(1, valueSequence.length).split(separator);
  220.             }
  221.  
  222.             // in capture case, see if element has a value on the screen which is not saved
  223.             value = elementsArray[element].value;
  224.             if (valueSequence && value) {
  225.               for (var i=0; i<valueList.length; i++) {
  226.                 if (value == valueList[i]) {
  227.                   value = null;
  228.                   break;
  229.                 }
  230.               }
  231.             }
  232.             if (value) {
  233.               // at least one text (or select) element has a value,
  234.               //    in which case the capture item is to appear in menu
  235.               bestState.capture = enable;
  236.             }
  237.  
  238.             // in prefill case, see if element has a saved value
  239.             if (valueSequence) {
  240.               value = valueList[0];
  241.               if (value) {
  242.                 // at least one text (or select) element has a value,
  243.                 //    in which case the prefill item is to appear in menu
  244.                 bestState.prefill = enable;
  245.               }
  246.             }
  247.  
  248.             // see if we've gone far enough
  249.             if ((bestState.capture == enable) && (bestState.prefill == enable) &&
  250.                 (elementCount > threshhold)) {
  251.               return bestState;
  252.             }
  253.           } 
  254.         }
  255.       }
  256.       // if we got here, then there was no element with a value or too few elements
  257.       return bestState;
  258.     }
  259.  
  260.     var bestState;
  261.  
  262.     function stateFoundInFormsArray(content, threshhold) {
  263.       var rv = {capture: false, prefill: false};
  264.       var state = getStateFromFormsArray(content, threshhold);
  265.       for (var i in state) {
  266.         if (state[i] == enable) {
  267.           bestState[i] = enable;
  268.           if (elementCount > threshhold) {
  269.             rv[i] = true;
  270.           }
  271.         } else if (state[i] == disable && bestState[i] == hide) {
  272.           bestState[i] = disable;
  273.         }
  274.       }
  275.       return rv;
  276.     }
  277.  
  278.     // Walk through the DOM to determine how capture or prefill item is to appear.
  279.     //   returned value:
  280.     //      hide, disable, enable for .capture and .prefill
  281.  
  282.     function getState(threshhold) {
  283.       bestState = {capture: hide, prefill: hide};
  284.       elementCount = 0;
  285.       stateFound(window.content, threshhold);
  286.       return bestState;
  287.     }
  288.  
  289.     function stateFound(content, threshhold) {
  290.       var captureStateFound = false;
  291.       var prefillStateFound = false;
  292.       if (!content || !content.document) {
  293.         return {capture: false, prefill: false};
  294.       }
  295.       var document = content.document;
  296.       if (!("forms" in document)) {
  297.         // this will occur if document is xul document instead of html document for example
  298.         return {capture: false, prefill: false};
  299.       }
  300.  
  301.       // test for wallet service being available
  302.       if (gWalletService == -1)
  303.         gWalletService = Components.classes["@mozilla.org/wallet/wallet-service;1"]
  304.                                    .getService(Components.interfaces.nsIWalletService);
  305.       if (!gWalletService) {
  306.         return {capture: true, prefill: true};
  307.       }
  308.  
  309.       // process frames if any
  310.       var framesArray = content.frames;
  311.       var rv;
  312.       if (framesArray.length != 0) {
  313.         var frame;
  314.         for (frame=0; frame<framesArray.length; ++frame) {
  315.  
  316.           // recursively process each frame for additional documents
  317.           rv = stateFound(framesArray[frame], threshhold);
  318.           captureStateFound |= rv.capture; prefillStateFound |= rv.prefill;
  319.           if (captureStateFound && prefillStateFound) {
  320.             return {capture: true, prefill: true};
  321.           }
  322.  
  323.           // process the document of this frame
  324.           var frameContent = framesArray[frame];
  325.           if (frameContent.document) {
  326.             rv = stateFoundInFormsArray(frameContent, threshhold);
  327.             captureStateFound |= rv.capture; prefillStateFound |= rv.prefill;
  328.             if (captureStateFound && prefillStateFound) {
  329.               gIsEncrypted = -1;
  330.               return {capture: true, prefill: true};
  331.             }
  332.           }
  333.         }
  334.       }
  335.  
  336.       // process top-level document
  337.       gIsEncrypted = -1;
  338.       rv = stateFoundInFormsArray(content, threshhold);
  339.       captureStateFound |= rv.capture; prefillStateFound |= rv.prefill;
  340.       if (captureStateFound && prefillStateFound) {
  341.         return {capture: true, prefill: true};
  342.       }
  343.  
  344.       // if we got here, then there was no text (or select) element with a value
  345.       // or there were too few text (or select) elements
  346.       if (elementCount > threshhold) {
  347.         // no text (or select) element with a value
  348.         return rv;
  349.       }
  350.  
  351.       // too few text (or select) elements
  352.       bestState = {capture: hide, prefill: hide};
  353.       return rv;
  354.     }
  355.  
  356.     // display a Wallet Dialog
  357.     function WalletDialog(which) {
  358.       switch( which ) {
  359.         case "walletsites":
  360.           window.openDialog("chrome://communicator/content/wallet/SignonViewer.xul",
  361.                             "_blank","chrome,resizable=no","W"); 
  362.           break;
  363.         case "wallet":
  364.         default:
  365.           formShow();
  366.           break;
  367.       }
  368.     }
  369.