home *** CD-ROM | disk | FTP | other *** search
/ Freelog 115 / FreelogNo115-MaiJuin2013.iso / Internet / AvantBrowser / asetup.exe / _data / webkit / chrome.dll / 0 / BINDATA / 610 < prev    next >
Encoding:
Text File  |  2013-04-03  |  3.7 KB  |  113 lines

  1. // Copyright (c) 2012 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4.  
  5. /**
  6.  * @fileoverview A collection of utility methods for UberPage and its contained
  7.  *     pages.
  8.  */
  9.  
  10. cr.define('uber', function() {
  11.  
  12.   /**
  13.    * Fixed position header elements on the page to be shifted by handleScroll.
  14.    * @type {NodeList}
  15.    */
  16.   var headerElements;
  17.  
  18.   /**
  19.    * This should be called by uber content pages when DOM content has loaded.
  20.    */
  21.   function onContentFrameLoaded() {
  22.     headerElements = document.getElementsByTagName('header');
  23.     document.addEventListener('scroll', handleScroll);
  24.  
  25.     // Trigger the scroll handler to tell the navigation if our page started
  26.     // with some scroll (happens when you use tab restore).
  27.     handleScroll();
  28.  
  29.     window.addEventListener('message', handleWindowMessage);
  30.   }
  31.  
  32.   /**
  33.    * Handles scroll events on the document. This adjusts the position of all
  34.    * headers and updates the parent frame when the page is scrolled.
  35.    * @private
  36.    */
  37.   function handleScroll() {
  38.     var offset = document.body.scrollLeft * -1;
  39.     for (var i = 0; i < headerElements.length; i++)
  40.       headerElements[i].style.webkitTransform = 'translateX(' + offset + 'px)';
  41.  
  42.     invokeMethodOnParent('adjustToScroll', document.body.scrollLeft);
  43.   };
  44.  
  45.   /**
  46.    * Handles 'message' events on window.
  47.    * @param {Event} e The message event.
  48.    */
  49.   function handleWindowMessage(e) {
  50.     if (e.data.method === 'frameSelected')
  51.       handleFrameSelected();
  52.     else if (e.data.method === 'mouseWheel')
  53.       handleMouseWheel(e.data.params);
  54.   }
  55.  
  56.   /**
  57.    * This is called when a user selects this frame via the navigation bar
  58.    * frame (and is triggered via postMessage() from the uber page).
  59.    * @private
  60.    */
  61.   function handleFrameSelected() {
  62.     document.body.scrollLeft = 0;
  63.   }
  64.  
  65.   /**
  66.    * Called when a user mouse wheels (or trackpad scrolls) over the nav frame.
  67.    * The wheel event is forwarded here and we scroll the body.
  68.    * There's no way to figure out the actual scroll amount for a given delta.
  69.    * It differs for every platform and even initWebKitWheelEvent takes a
  70.    * pixel amount instead of a wheel delta. So we just choose something
  71.    * reasonable and hope no one notices the difference.
  72.    * @param {Object} params A structure that holds wheel deltas in X and Y.
  73.    */
  74.   function handleMouseWheel(params) {
  75.     document.body.scrollTop -= params.deltaY * 49 / 120;
  76.     document.body.scrollLeft -= params.deltaX * 49 / 120;
  77.   }
  78.  
  79.   /**
  80.    * Invokes a method on the parent window (UberPage). This is a convenience
  81.    * method for API calls into the uber page.
  82.    * @param {String} method The name of the method to invoke.
  83.    * @param {Object=} opt_params Optional property bag of parameters to pass to
  84.    *     the invoked method.
  85.    * @private
  86.    */
  87.   function invokeMethodOnParent(method, opt_params) {
  88.     if (window.location == window.parent.location)
  89.       return;
  90.  
  91.     invokeMethodOnWindow(window.parent, method, opt_params, 'chrome://chrome');
  92.   }
  93.  
  94.   /**
  95.    * Invokes a method on the target window.
  96.    * @param {String} method The name of the method to invoke.
  97.    * @param {Object=} opt_params Optional property bag of parameters to pass to
  98.    *     the invoked method.
  99.    * @param {String=} opt_url The origin of the target window.
  100.    * @private
  101.    */
  102.   function invokeMethodOnWindow(targetWindow, method, opt_params, opt_url) {
  103.     var data = {method: method, params: opt_params};
  104.     targetWindow.postMessage(data, opt_url ? opt_url : '*');
  105.   }
  106.  
  107.   return {
  108.     invokeMethodOnParent: invokeMethodOnParent,
  109.     invokeMethodOnWindow: invokeMethodOnWindow,
  110.     onContentFrameLoaded: onContentFrameLoaded,
  111.   };
  112. });
  113.