home *** CD-ROM | disk | FTP | other *** search
/ Chip 2002 January / 01_02.iso / software / netscape62win / browser.xpi / bin / chrome / comm.jar / content / wallet / walletOverlay.js < prev    next >
Encoding:
JavaScript  |  2001-08-21  |  12.2 KB  |  341 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.     // Set the disabled attribute of specified item.
  25.     //   If the value is false, then it removes the attribute
  26.     function setDisabledAttr(id, val) {
  27.       var elem = document.getElementById(id);
  28.       if (elem) {
  29.         if (val) {
  30.           elem.setAttribute("disabled", val);
  31.         } else {
  32.           elem.removeAttribute("disabled");
  33.         }
  34.       }
  35.     }
  36.  
  37.     // Show/hide one item (specified via name or the item element itself).
  38.     function showItem(itemOrId, show) {
  39.       var item = null;
  40.       if (itemOrId.constructor == String) {
  41.         // Argument specifies item id.
  42.         item = document.getElementById(itemOrId);
  43.       } else {
  44.         // Argument is the item itself.
  45.         item = itemOrId;
  46.       }
  47.       if (item) {
  48.         var styleIn = item.getAttribute("style");
  49.         var styleOut = styleIn;
  50.         if (show) {
  51.           // Remove style="display:none;".
  52.           styleOut = styleOut.replace("display:none;", "");
  53.  
  54.         } else {
  55.           // Set style="display:none;".
  56.           if (styleOut.indexOf("display:none;") == -1) {
  57.             // Add style the first time we need to.
  58.             styleOut += "display:none;";
  59.           }
  60.         }
  61.         // Only set style if it's different.
  62.         if (styleIn != styleOut) {
  63.           item.setAttribute("style", styleOut);
  64.         }
  65.       }
  66.     }
  67.  
  68. /* form toolbar is out
  69.     var firstTime = true;
  70.  
  71.     function initToolbarItems() {
  72.  
  73.       // This routine determines whether or not to display the form-manager toolbar and,
  74.       // if so, which buttons on the toolbar are to be enabled.  We need to reexecute
  75.       // this routine whenever the form-manager dialog finishes because saved values might
  76.       // have been added/removed which could affect the enable/disable state of the buttons.
  77.  
  78.       if (firstTime) {
  79.         // Force initToolbarItems to be executed upon return from viewing prefs.
  80.         //   This is necessary in case the form-manager dialog was invoked from the
  81.         //   pref panel.  See next block of code for more details.
  82.         var pref = document.getElementById("menu_preferences");
  83.         if (pref) {
  84.           oncommand = pref.getAttribute("oncommand");
  85.           pref.setAttribute("oncommand", oncommand+";initToolbarItems()");
  86.           firstTime = false;
  87.         }
  88.       }
  89.  
  90.       // get the form-manager toolbar
  91.       var cmd_viewformToolbar = document.getElementById("cmd_viewformtoolbar");
  92.       if (!cmd_viewformToolbar) {
  93.         // This happens when you access the form-manager dialog from the prefs panel
  94.         // Not sure yet how to get access to items in navigator in that case
  95.         // So instead we will execute initToolbarItems when edit->prefs returns (that's
  96.         // what above block of code involving firstTime accomplished.
  97.         return;
  98.       }
  99.  
  100.       // keep form toolbar hidden if checkbox in view menu so indicates
  101.       var checkValue = cmd_viewformToolbar.getAttribute("checked");
  102.       if (checkValue == "false") {
  103.         showItem("formToolbar", false);
  104.         return;
  105.       }
  106.  
  107.       // hide form toolbar if three or less text elements in form
  108.       var prefillState = getState(prefill, 3);
  109.       showItem("formToolbar", (prefillState != hide));
  110.  
  111.       // enable prefill button if there is at least one saved value for the form
  112.       setDisabledAttr("formPrefill", (prefillState == disable));
  113.     }
  114. */
  115.  
  116.     function formShow() {
  117.       window.openDialog(
  118.           "chrome://communicator/content/wallet/WalletViewer.xul",
  119.           "_blank",
  120.           "chrome,titlebar,modal=yes,resizable=yes");
  121. /* form toolbar is out
  122.        initToolbarItems(); // need to redetermine which buttons in form toolbar to enable
  123. */
  124.     }
  125.  
  126.     // Capture the values that are filled in on the form being displayed.
  127.     function formCapture() {
  128.       var walletService = Components.classes["@mozilla.org/wallet/wallet-service;1"].getService(Components.interfaces.nsIWalletService);
  129.       walletService.WALLET_RequestToCapture(window._content);
  130.     }
  131.  
  132.     // Prefill the form being displayed.
  133.     function formPrefill() {
  134.       var walletService = Components.classes["@mozilla.org/wallet/wallet-service;1"].getService(Components.interfaces.nsIWalletService);
  135.       walletService.WALLET_Prefill(false, window._content);
  136.       window.openDialog("chrome://communicator/content/wallet/WalletPreview.xul",
  137.                         "_blank", "chrome,modal=yes,dialog=yes,all, width=504, height=436");
  138.     }
  139.  
  140. /*
  141.     // Prefill the form being displayed without bringing up the preview window.
  142.     function formQuickPrefill() {
  143.       gWalletService.WALLET_Prefill(true, window._content);
  144.     }
  145. */
  146.  
  147.     var hide = -1;
  148.     var disable = 0;
  149.     var enable = 1;
  150.  
  151.     var capture = 0;
  152.     var prefill = 1;
  153.  
  154.     var elementCount;
  155.  
  156.     // Walk through the DOM to determine how a capture or prefill item is to appear.
  157.     //   arguments:
  158.     //      captureOrPrefill = capture, prefill
  159.     //   returned value:
  160.     //      hide, disable, enable
  161.     function getStateFromFormsArray(formsArray, captureOrPrefill, threshhold) {
  162.       if (!formsArray) {
  163.         return hide;
  164.       }
  165.  
  166.       var form;
  167.       var bestState = 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 enable;
  194. //              }
  195.               }
  196.             } catch(e) {
  197.               // there is no crypto pref so database could not possible be encrypted
  198.             }
  199.             
  200.             if (bestState == hide) {
  201.               bestState = disable;
  202.             }
  203.             var value;
  204.  
  205.             // obtain saved values if any and store in array called valueList
  206.             var valueList;
  207.             var valueSequence = gWalletService.WALLET_PrefillOneElement
  208.               (window._content, elementsArray[element]);
  209.             // result is a linear sequence of values, each preceded by a separator character
  210.             // convert linear sequence of values into an array of values
  211.             if (valueSequence) {
  212.               var separator = valueSequence[0];
  213.               valueList = valueSequence.substring(1, valueSequence.length).split(separator);
  214.             }
  215.  
  216.             // see if there's a value on screen (capture case) or a saved value (prefill case) 
  217.             if (captureOrPrefill == capture) {
  218.               // in capture case, see if element has a value on the screen which is not saved
  219.               value = elementsArray[element].value;
  220.               if (valueSequence && value) {
  221.                 for (var i=0; i<valueList.length; i++) {
  222.                   if (value == valueList[i]) {
  223.                     value = null;
  224.                     break;
  225.                   }
  226.                 }
  227.               }
  228.             } else {
  229.               // in prefill case, see if element has a saved value
  230.               if (valueSequence) {
  231.                 value = valueList[0];
  232.               }
  233.             }
  234.  
  235.             if (value) {
  236.               // at least one text (or select) element has a value,
  237.               //    in which case the capture or prefill item is to appear in menu
  238.               bestState = enable;
  239.               if (elementCount > threshhold) {
  240.                 return enable;
  241.               }
  242.             }
  243.           } 
  244.         }
  245.       }
  246.       // if we got here, then there was no element with a value or too few elements
  247.       return bestState;
  248.     }
  249.  
  250.     var bestState;
  251.  
  252.     function stateFoundInFormsArray(formsArray, captureOrPrefill, threshhold) {
  253.       state =
  254.         getStateFromFormsArray(formsArray, captureOrPrefill, threshhold);
  255.       if (state == enable) {
  256.         if (elementCount > threshhold) {
  257.           bestState = enable;
  258.           return true;
  259.         }
  260.         bestState = enable;
  261.       } else if (state == disable && bestState == hide) {
  262.         bestState = disable;
  263.         return false;
  264.       }
  265.     }
  266.  
  267.     // Walk through the DOM to determine how capture or prefill item is to appear.
  268.     //   arguments:
  269.     //      captureOrPrefill = capture, prefill
  270.     //   returned value:
  271.     //      hide, disable, enable
  272.  
  273.     function getState(captureOrPrefill, threshhold) {
  274.       stateFound(window.content, captureOrPrefill, threshhold);
  275.       return bestState;
  276.     }
  277.  
  278.     function stateFound(content, captureOrPrefill, threshhold) {
  279.       bestState = hide;
  280.       if (!content || !content.document) {
  281.         return false;
  282.       }
  283.       var document = content.document;
  284.       if (!("forms" in document)) {
  285.         // this will occur if document is xul document instead of html document for example
  286.         return false;
  287.       }
  288.  
  289.       // test for wallet service being available
  290.       if (gWalletService == -1)
  291.         gWalletService = Components.classes["@mozilla.org/wallet/wallet-service;1"]
  292.                                    .getService(Components.interfaces.nsIWalletService);
  293.       if (!gWalletService) {
  294.         return true;
  295.       }
  296.  
  297.       var state;
  298.       elementCount = 0;
  299.  
  300.       // process frames if any
  301.       var formsArray;
  302.       var framesArray = content.frames;
  303.       if (framesArray.length != 0) {
  304.         var frame;
  305.         for (frame=0; frame<framesArray.length; ++frame) {
  306.  
  307.           // recursively process each frame for additional documents
  308.           if (stateFound(framesArray[frame], captureOrPrefill, threshhold)) {
  309.             return true;
  310.           }
  311.  
  312.           // process the document of this frame
  313.           var frameDocument = framesArray[frame].document;
  314.           if (frameDocument) {
  315.  
  316.             if (stateFoundInFormsArray(frameDocument.forms, captureOrPrefill, threshhold)) {
  317.               gIsEncrypted = -1;
  318.               return true;
  319.             }
  320.           }
  321.         }
  322.       }
  323.  
  324.       // process top-level document
  325.       gIsEncrypted = -1;
  326.       if (stateFoundInFormsArray(document.forms, captureOrPrefill, threshhold)) {
  327.         return true;
  328.       }
  329.  
  330.       // if we got here, then there was no text (or select) element with a value
  331.       // or there were too few text (or select) elements
  332.       if (elementCount > threshhold) {
  333.         // no text (or select) element with a value
  334.         return false;
  335.       }
  336.  
  337.       // too few text (or select) elements
  338.       bestState = hide;
  339.       return false;
  340.     }
  341.