home *** CD-ROM | disk | FTP | other *** search
/ Chip 2002 January / 01_02.iso / software / netscape62win / browser.xpi / bin / chrome / comm.jar / content / communicator / contentAreaClick.js < prev    next >
Encoding:
JavaScript  |  2001-04-09  |  7.2 KB  |  182 lines

  1. /* -*- Mode: Java; tab-width: 2; 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.  * Contributors:
  21.  * 
  22.  *   Alec Flett      <alecf@netscape.com>
  23.  *   Ben Goodger     <ben@netscape.com>
  24.  *   Mike Pinkerton  <pinkerton@netscape.com>
  25.  *   Blake Ross      <blakeross@telocity.com>
  26.  */
  27.  
  28.   var pref = null;
  29.   pref = Components.classes["@mozilla.org/preferences;1"];
  30.   pref = pref.getService();
  31.   pref = pref.QueryInterface(Components.interfaces.nsIPref);
  32.  
  33.   // Prefill a single text field
  34.   function prefillTextBox(target) {
  35.  
  36.     // obtain values to be used for prefilling
  37.     var walletService = Components.classes["@mozilla.org/wallet/wallet-service;1"].getService(Components.interfaces.nsIWalletService);
  38.     var value = walletService.WALLET_PrefillOneElement(window._content, target);
  39.     if (value) {
  40.  
  41.       // result is a linear sequence of values, each preceded by a separator character
  42.       // convert linear sequence of values into an array of values
  43.       var separator = value[0];
  44.       var valueList = value.substring(1, value.length).split(separator);
  45.  
  46.       target.value = valueList[0];
  47. /*
  48.  * Following code is a replacement for above line.  In the case of multiple values, it
  49.  * presents the user with a dialog containing a list from which he can select the value
  50.  * he wants.  However it is being commented out for now because of several problems, namely
  51.  *
  52.  *   1. There is no easy way to put localizable strings for the title and message of
  53.  *      the dialog without introducing a .properties file which currently doesn't exist
  54.  *   2. Using blank title and messages as shown below have a problem because a zero-length
  55.  *      title is being displayed as some garbage characters (which is why the code below
  56.  *      has a title of " " instead of "").  This is a separate bug which will have to be
  57.  *      investigated further.
  58.  *   3. The current wallet tables present alternate values for items such as shipping
  59.  *      address -- namely billing address and home address.  Up until now, these alternate
  60.  *      values have never been a problem because the preferred value is always first and is
  61.  *      all the user sees when doing a prefill.  However now he will be presented with a
  62.  *      list showing all these values and asking him to pick one, even though the wallet
  63.  *      code was clearly able to determine that he meant shipping address and not billing
  64.  *      address.
  65.  *   4. There is a relatively long delay before the dialog come up whereas values are
  66.  *      filled in quickly when no dialog is involved.
  67.  *
  68.  * Once this feature is checked in, a separate bug will be opened asking that the above
  69.  * problems be examined and this dialog turned on
  70.  
  71.       if (valueList.length == 1) {
  72.         // only one value, use it for prefilling
  73.         target.value = valueList[0];
  74.       } else {
  75.  
  76.         // more than one value, have user select the one he wants
  77.         var promptService = Components.classes["@mozilla.org/embedcomp/prompt-service;1"].getService();
  78.         promptService = promptService.QueryInterface(Components.interfaces.nsIPromptService);
  79.         var position = {};
  80.         var title = " ";
  81.         var message = "";
  82.         var ok =
  83.           promptService.select
  84.             (window, title, message, valueList.length, valueList, position)
  85.         if (ok) {
  86.           target.value = valueList[position.value];
  87.         }
  88.       }
  89.  
  90.  * End of commented out code
  91.  */
  92.     }
  93.   }
  94.   
  95.   // Called whenever the user clicks in the content area,
  96.   // except when left-clicking on links (special case)
  97.   // should always return true for click to go through
  98.   function contentAreaClick(event) 
  99.   {
  100.     var target = event.target;
  101.     var linkNode;
  102.     switch (target.localName.toLowerCase()) {
  103.       case "a":
  104.         linkNode = target;
  105.         break;
  106.       case "area":
  107.         if (target.href) 
  108.           linkNode = target;
  109.         break;
  110.       case "input":
  111.         if ((event.target.type.toLowerCase() == "text" || event.target.type == "") // text field
  112.             && event.detail == 2 // double click
  113.             && event.button == 0 // left mouse button
  114.             && event.target.value.length == 0) { // no text has been entered
  115.           prefillTextBox(target); // prefill the empty text field if possible
  116.         }
  117.         break;
  118.       default:
  119.         linkNode = findParentNode(event.originalTarget, "a");
  120.         break;
  121.     }
  122.     if (linkNode) {
  123.       handleLinkClick(event, linkNode.href);
  124.       return true;
  125.     }
  126.     if (pref && event.button == 1 &&
  127.         !findParentNode(event.originalTarget, "scrollbar") &&
  128.         pref.GetBoolPref("middlemouse.contentLoadURL")) {
  129.       if (middleMousePaste()) {
  130.         event.preventBubble();
  131.       }
  132.     }
  133.     return true;
  134.   }
  135.  
  136.   function handleLinkClick(event, href)
  137.   {
  138.     switch (event.button) {                                   
  139.       case 0:                                                         // if left button clicked
  140.         if (event.metaKey || event.ctrlKey) {                         // and meta or ctrl are down
  141.           openNewWindowWith(href);                                    // open link in new window
  142.           event.preventBubble();
  143.           return true;
  144.         } 
  145.         var saveModifier = true;
  146.         if (pref) {
  147.           try {
  148.             saveModifier = pref.GetBoolPref("ui.key.saveLink.shift");
  149.           }
  150.           catch(ex) {            
  151.           }
  152.         }
  153.         saveModifier = saveModifier ? event.shiftKey : event.metaKey;
  154.           
  155.         if (saveModifier) {                                           // if saveModifier is down
  156.           savePage(href);                                             // save the link
  157.           return true;
  158.         }
  159.         if (event.altKey)                                             // if alt is down
  160.           return true;                                                // do nothing
  161.         return false;
  162.       case 1:                                                         // if middle button clicked
  163.         if (pref && pref.GetBoolPref("middlemouse.openNewWindow")) {  // and the pref is on
  164.           openNewWindowWith(href);                                    // open link in new window
  165.           event.preventBubble();
  166.           return true;
  167.         }
  168.         break;
  169.     }
  170.     return false;
  171.   }
  172.  
  173.   function middleMousePaste()
  174.   {
  175.     var url = readFromClipboard();
  176.     if (url) {
  177.       loadURI(getShortcutOrURI(url));
  178.       return true;
  179.     }
  180.     return false;
  181.   }
  182.