home *** CD-ROM | disk | FTP | other *** search
/ GameStar 2005 October / Gamestar_77_2005-10_dvd.iso / Programy / nsb-install-8-0.exe / chrome / comm.jar / content / communicator / contentAreaClick.js < prev    next >
Encoding:
JavaScript  |  2005-07-29  |  11.1 KB  |  301 lines

  1. /* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* ***** BEGIN LICENSE BLOCK *****
  3.  * Version: NPL 1.1/GPL 2.0/LGPL 2.1
  4.  *
  5.  * The contents of this file are subject to the Netscape Public License
  6.  * Version 1.1 (the "License"); you may not use this file except in
  7.  * compliance with the License. You may obtain a copy of the License at
  8.  * http://www.mozilla.org/NPL/
  9.  *
  10.  * Software distributed under the License is distributed on an "AS IS" basis,
  11.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  12.  * for the specific language governing rights and limitations under the
  13.  * License.
  14.  *
  15.  * The Original Code is mozilla.org code.
  16.  *
  17.  * The Initial Developer of the Original Code is 
  18.  * Netscape Communications Corporation.
  19.  * Portions created by the Initial Developer are Copyright (C) 1998
  20.  * the Initial Developer. All Rights Reserved.
  21.  *
  22.  * Contributor(s):
  23.  *   Alec Flett      <alecf@netscape.com>
  24.  *   Ben Goodger     <ben@netscape.com>
  25.  *   Mike Pinkerton  <pinkerton@netscape.com>
  26.  *   Blake Ross      <blakeross@telocity.com>
  27.  *
  28.  * Alternatively, the contents of this file may be used under the terms of
  29.  * either the GNU General Public License Version 2 or later (the "GPL"), or 
  30.  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  31.  * in which case the provisions of the GPL or the LGPL are applicable instead
  32.  * of those above. If you wish to allow use of your version of this file only
  33.  * under the terms of either the GPL or the LGPL, and not to allow others to
  34.  * use your version of this file under the terms of the NPL, indicate your
  35.  * decision by deleting the provisions above and replace them with the notice
  36.  * and other provisions required by the GPL or the LGPL. If you do not delete
  37.  * the provisions above, a recipient may use your version of this file under
  38.  * the terms of any one of the NPL, the GPL or the LGPL.
  39.  *
  40.  * ***** END LICENSE BLOCK ***** */
  41.  
  42. /*
  43.  * - [ Dependencies ] ---------------------------------------------------------
  44.  *  utilityOverlay.js:
  45.  *    - gatherTextUnder
  46.  */
  47.  
  48.   var pref = null;
  49.   pref = Components.classes["@mozilla.org/preferences-service;1"]
  50.                    .getService(Components.interfaces.nsIPrefBranch);
  51.  
  52.   // Prefill a single text field
  53.   function prefillTextBox(target) {
  54.  
  55.     // obtain values to be used for prefilling
  56.     var walletService = Components.classes["@mozilla.org/wallet/wallet-service;1"].getService(Components.interfaces.nsIWalletService);
  57.     var value = walletService.WALLET_PrefillOneElement(window._content, target);
  58.     if (value) {
  59.  
  60.       // result is a linear sequence of values, each preceded by a separator character
  61.       // convert linear sequence of values into an array of values
  62.       var separator = value[0];
  63.       var valueList = value.substring(1, value.length).split(separator);
  64.  
  65.       target.value = valueList[0];
  66. /*
  67.  * Following code is a replacement for above line.  In the case of multiple values, it
  68.  * presents the user with a dialog containing a list from which he can select the value
  69.  * he wants.  However it is being commented out for now because of several problems, namely
  70.  *
  71.  *   1. There is no easy way to put localizable strings for the title and message of
  72.  *      the dialog without introducing a .properties file which currently doesn't exist
  73.  *   2. Using blank title and messages as shown below have a problem because a zero-length
  74.  *      title is being displayed as some garbage characters (which is why the code below
  75.  *      has a title of " " instead of "").  This is a separate bug which will have to be
  76.  *      investigated further.
  77.  *   3. The current wallet tables present alternate values for items such as shipping
  78.  *      address -- namely billing address and home address.  Up until now, these alternate
  79.  *      values have never been a problem because the preferred value is always first and is
  80.  *      all the user sees when doing a prefill.  However now he will be presented with a
  81.  *      list showing all these values and asking him to pick one, even though the wallet
  82.  *      code was clearly able to determine that he meant shipping address and not billing
  83.  *      address.
  84.  *   4. There is a relatively long delay before the dialog come up whereas values are
  85.  *      filled in quickly when no dialog is involved.
  86.  *
  87.  * Once this feature is checked in, a separate bug will be opened asking that the above
  88.  * problems be examined and this dialog turned on
  89.  
  90.       if (valueList.length == 1) {
  91.         // only one value, use it for prefilling
  92.         target.value = valueList[0];
  93.       } else {
  94.  
  95.         // more than one value, have user select the one he wants
  96.         var promptService = Components.classes["@mozilla.org/embedcomp/prompt-service;1"].getService();
  97.         promptService = promptService.QueryInterface(Components.interfaces.nsIPromptService);
  98.         var position = {};
  99.         var title = " ";
  100.         var message = "";
  101.         var ok =
  102.           promptService.select
  103.             (window, title, message, valueList.length, valueList, position)
  104.         if (ok) {
  105.           target.value = valueList[position.value];
  106.         }
  107.       }
  108.  
  109.  * End of commented out code
  110.  */
  111.     }
  112.   }
  113.   
  114.   function hrefForClickEvent(event)
  115.   {
  116.     var target = event.target;
  117.     var linkNode;
  118.  
  119.     var local_name = target.localName;
  120.  
  121.     if (local_name) {
  122.       local_name = local_name.toLowerCase();
  123.     }
  124.     
  125.     var isKeyPress = (event.type == "keypress");
  126.  
  127.     switch (local_name) {
  128.       case "a":
  129.       case "area":
  130.       case "link":
  131.         if (target.hasAttribute("href")) 
  132.           linkNode = target;
  133.         break;
  134.       case "input":
  135.         if ((event.target.type == "text") // text field
  136.             && !isKeyPress       // not a key event
  137.             && event.detail == 2 // double click
  138.             && event.button == 0 // left mouse button
  139.             && event.target.value.length == 0) { // no text has been entered
  140.           prefillTextBox(target); // prefill the empty text field if possible
  141.         }
  142.         break;
  143.       default:
  144.         linkNode = event.originalTarget;
  145.         while (linkNode && !(linkNode instanceof HTMLAnchorElement))
  146.           linkNode = linkNode.parentNode;
  147.         // <a> cannot be nested.  So if we find an anchor without an
  148.         // href, there is no useful <a> around the target
  149.         if (linkNode && !linkNode.hasAttribute("href"))
  150.           linkNode = null;
  151.         break;
  152.     }
  153.     var href;
  154.     if (linkNode) {
  155.       href = new XPCNativeWrapper(linkNode, "href").href;
  156.     } else {
  157.       // Try simple XLink
  158.       linkNode = target;
  159.       while (linkNode) {
  160.         if (linkNode.nodeType == Node.ELEMENT_NODE) {
  161.           var wrapper = new XPCNativeWrapper(linkNode, "getAttributeNS()");
  162.           href = wrapper.getAttributeNS("http://www.w3.org/1999/xlink", "href");
  163.           break;
  164.         }
  165.         linkNode = linkNode.parentNode;
  166.       }
  167.       if (href && href != "") {
  168.         var baseURI = new XPCNativeWrapper(linkNode, "baseURI").baseURI;
  169.         href = makeURLAbsolute(baseURI, href);
  170.       }
  171.     }
  172.     return href;
  173.   }
  174.  
  175.   // Called whenever the user clicks in the content area,
  176.   // except when left-clicking on links (special case)
  177.   // should always return true for click to go through
  178.   function contentAreaClick(event) 
  179.   {
  180.     if (!event.isTrusted) {
  181.       return true;
  182.     }
  183.  
  184.     var isKeyPress = (event.type == "keypress");
  185.     var href = hrefForClickEvent(event);
  186.     if (href) {
  187.       if (isKeyPress) {
  188.         openNewTabWith(href, true, event.shiftKey);
  189.         event.preventBubble();
  190.       }
  191.       else {
  192.         handleLinkClick(event, href, null);
  193.       }
  194.       return true;
  195.     }
  196.  
  197.     if (pref && !isKeyPress && event.button == 1 &&
  198.         !event.getPreventDefault()
  199.         pref.getBoolPref("middlemouse.contentLoadURL")) {
  200.       if (middleMousePaste(event)) {
  201.         event.preventBubble();
  202.       }
  203.     }
  204.     return true;
  205.   }
  206.  
  207.   function openNewTabOrWindow(event, href, sendReferrer)
  208.   {
  209.     // should we open it in a new tab?
  210.     if (pref && pref.getBoolPref("browser.tabs.opentabfor.middleclick")) {
  211.       openNewTabWith(href, sendReferrer, event.shiftKey);
  212.       event.preventBubble();
  213.       return true;
  214.     }
  215.  
  216.     // should we open it in a new window?
  217.     if (pref && pref.getBoolPref("middlemouse.openNewWindow")) {
  218.       openNewWindowWith(href, sendReferrer);
  219.       event.preventBubble();
  220.       return true;
  221.     }
  222.  
  223.     // let someone else deal with it
  224.     return false;
  225.   }
  226.  
  227.   function handleLinkClick(event, href, linkNode)
  228.   {
  229.     // Make sure we are allowed to open this URL
  230.     urlSecurityCheck(href, document);
  231.  
  232.     switch (event.button) {                                   
  233.       case 0:                                                         // if left button clicked
  234.         if (event.metaKey || event.ctrlKey) {                         // and meta or ctrl are down
  235.           if (openNewTabOrWindow(event, href, true))
  236.             return true;
  237.         } 
  238.         var saveModifier = true;
  239.         if (pref) {
  240.           try {
  241.             saveModifier = pref.getBoolPref("ui.key.saveLink.shift");
  242.           }
  243.           catch(ex) {            
  244.           }
  245.         }
  246.         saveModifier = saveModifier ? event.shiftKey : event.altKey;
  247.           
  248.         if (saveModifier) {                                           // if saveModifier is down
  249.           saveURL(href, linkNode ? gatherTextUnder(linkNode) : "");
  250.           return true;
  251.         }
  252.         if (event.altKey)                                             // if alt is down
  253.           return true;                                                // do nothing
  254.         return false;
  255.       case 1:                                                         // if middle button clicked
  256.         if (openNewTabOrWindow(event, href, true))
  257.           return true;
  258.         break;
  259.     }
  260.     return false;
  261.   }
  262.  
  263.   function middleMousePaste( event )
  264.   {
  265.     var url = readFromClipboard();
  266.     if (!url)
  267.       return false;
  268.     url = getShortcutOrURI(url);
  269.  
  270.     // On ctrl-middleclick, open in new window or tab.  Do not send referrer.
  271.     if (event.ctrlKey) {
  272.       // fix up our pasted URI in case it is malformed.
  273.       const nsIURIFixup = Components.interfaces.nsIURIFixup;
  274.       if (!gURIFixup)
  275.         gURIFixup = Components.classes["@mozilla.org/docshell/urifixup;1"]
  276.                               .getService(nsIURIFixup);
  277.  
  278.       url = gURIFixup.createFixupURI(url, nsIURIFixup.FIXUP_FLAGS_MAKE_ALTERNATE_URI).spec;
  279.  
  280.       return openNewTabOrWindow(event, url, false);
  281.     }
  282.  
  283.     // If ctrl wasn't down, then just load the url in the current win/tab.
  284.     if (url != "about:blank") {
  285.       gURLBar.value = url;
  286.     }
  287.     loadURI(url);
  288.     event.preventBubble();
  289.     return true;
  290.   }
  291.  
  292.   function makeURLAbsolute( base, url ) 
  293.   {
  294.     // Construct nsIURL.
  295.     var ioService = Components.classes["@mozilla.org/network/io-service;1"]
  296.                   .getService(Components.interfaces.nsIIOService);
  297.     var baseURI  = ioService.newURI(base, null, null);
  298.  
  299.     return ioService.newURI(baseURI.resolve(url), null, null).spec;
  300.   }
  301.