| OLD | NEW |
| (Empty) |
| 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 // This file provides the ScrollingAction object, which scrolls a page | |
| 6 // from top to bottom: | |
| 7 // 1. var action = new __ScrollingAction(callback) | |
| 8 // 2. action.start(element_to_scroll) | |
| 9 'use strict'; | |
| 10 | |
| 11 (function() { | |
| 12 var MAX_SCROLL_LENGTH_PIXELS = 5000; | |
| 13 | |
| 14 var getTimeMs = (function() { | |
| 15 if (window.performance) | |
| 16 return (performance.now || | |
| 17 performance.mozNow || | |
| 18 performance.msNow || | |
| 19 performance.oNow || | |
| 20 performance.webkitNow).bind(window.performance); | |
| 21 else | |
| 22 return function() { return new Date().getTime(); }; | |
| 23 })(); | |
| 24 | |
| 25 var requestAnimationFrame = (function() { | |
| 26 return window.requestAnimationFrame || | |
| 27 window.webkitRequestAnimationFrame || | |
| 28 window.mozRequestAnimationFrame || | |
| 29 window.oRequestAnimationFrame || | |
| 30 window.msRequestAnimationFrame || | |
| 31 function(callback) { | |
| 32 window.setTimeout(callback, 1000 / 60); | |
| 33 }; | |
| 34 })().bind(window); | |
| 35 | |
| 36 /** | |
| 37 * Scrolls a given element down a certain amount to emulate user scrolling. | |
| 38 * Uses smooth scrolling capabilities provided by the platform, if available. | |
| 39 * @constructor | |
| 40 */ | |
| 41 function SmoothScrollDownGesture(opt_element) { | |
| 42 this.element_ = opt_element || document.body; | |
| 43 }; | |
| 44 | |
| 45 function min(a, b) { | |
| 46 if (a > b) { | |
| 47 return b; | |
| 48 } | |
| 49 return a; | |
| 50 }; | |
| 51 | |
| 52 function getBoundingVisibleRect(el) { | |
| 53 var bound = el.getBoundingClientRect(); | |
| 54 var rect = { top: bound.top, | |
| 55 left: bound.left, | |
| 56 width: bound.width, | |
| 57 height: bound.height }; | |
| 58 var outsideHeight = (rect.top + rect.height) - window.innerHeight; | |
| 59 var outsideWidth = (rect.left + rect.width) - window.innerWidth; | |
| 60 | |
| 61 if (outsideHeight > 0) { | |
| 62 rect.height -= outsideHeight; | |
| 63 } | |
| 64 if (outsideWidth > 0) { | |
| 65 rect.width -= outsideWidth; | |
| 66 } | |
| 67 return rect; | |
| 68 }; | |
| 69 | |
| 70 SmoothScrollDownGesture.prototype.start = function(distance, callback) { | |
| 71 this.callback_ = callback; | |
| 72 if (window.chrome && | |
| 73 chrome.gpuBenchmarking && | |
| 74 chrome.gpuBenchmarking.smoothScrollBy) { | |
| 75 var rect = getBoundingVisibleRect(this.element_); | |
| 76 chrome.gpuBenchmarking.smoothScrollBy(distance, function() { | |
| 77 callback(); | |
| 78 }, rect.left + rect.width / 2, rect.top + rect.height / 2); | |
| 79 return; | |
| 80 } | |
| 81 | |
| 82 var SCROLL_DELTA = 100; | |
| 83 this.element_.scrollTop += SCROLL_DELTA; | |
| 84 requestAnimationFrame(callback); | |
| 85 }; | |
| 86 | |
| 87 // This class scrolls a page from the top to the bottom once. | |
| 88 // | |
| 89 // The page is scrolled down by a set of scroll gestures. These gestures | |
| 90 // correspond to a reading gesture on that platform. | |
| 91 // | |
| 92 // start -> startPass_ -> ...scrolling... -> onGestureComplete_ -> | |
| 93 // -> startPass_ -> .. scrolling... -> onGestureComplete_ -> callback_ | |
| 94 function ScrollingAction(opt_callback) { | |
| 95 var self = this; | |
| 96 | |
| 97 this.beginMeasuringHook = function() {} | |
| 98 this.endMeasuringHook = function() {} | |
| 99 | |
| 100 this.callback_ = opt_callback; | |
| 101 } | |
| 102 | |
| 103 ScrollingAction.prototype.getRemainingScrollDistance_ = function() { | |
| 104 var clientHeight; | |
| 105 // clientHeight is "special" for the body element. | |
| 106 if (this.element_ == document.body) | |
| 107 clientHeight = window.innerHeight; | |
| 108 else | |
| 109 clientHeight = this.element_.clientHeight; | |
| 110 | |
| 111 return this.scrollHeight_ - this.element_.scrollTop - clientHeight; | |
| 112 } | |
| 113 | |
| 114 ScrollingAction.prototype.start = function(opt_element) { | |
| 115 // Assign this.element_ here instead of constructor, because the constructor | |
| 116 // ensures this method will be called after the document is loaded. | |
| 117 this.element_ = opt_element || document.body; | |
| 118 // Some pages load more content when you scroll to the bottom. Record | |
| 119 // the original element height here and only scroll to that point. | |
| 120 // -1 to allow for rounding errors on scaled viewports (like mobile). | |
| 121 this.scrollHeight_ = Math.min(MAX_SCROLL_LENGTH_PIXELS, | |
| 122 this.element_.scrollHeight - 1); | |
| 123 requestAnimationFrame(this.startPass_.bind(this)); | |
| 124 }; | |
| 125 | |
| 126 ScrollingAction.prototype.startPass_ = function() { | |
| 127 this.element_.scrollTop = 0; | |
| 128 | |
| 129 this.beginMeasuringHook(); | |
| 130 | |
| 131 this.gesture_ = new SmoothScrollDownGesture(this.element_); | |
| 132 this.gesture_.start(this.getRemainingScrollDistance_(), | |
| 133 this.onGestureComplete_.bind(this)); | |
| 134 }; | |
| 135 | |
| 136 ScrollingAction.prototype.getResults = function() { | |
| 137 return this.renderingStats_; | |
| 138 } | |
| 139 | |
| 140 ScrollingAction.prototype.onGestureComplete_ = function(timestamp) { | |
| 141 // If the scrollHeight went down, only scroll to the new scrollHeight. | |
| 142 // -1 to allow for rounding errors on scaled viewports (like mobile). | |
| 143 this.scrollHeight_ = Math.min(this.scrollHeight_, | |
| 144 this.element_.scrollHeight - 1); | |
| 145 | |
| 146 if (this.getRemainingScrollDistance_() > 0) { | |
| 147 this.gesture_.start(this.getRemainingScrollDistance_(), | |
| 148 this.onGestureComplete_.bind(this)); | |
| 149 return; | |
| 150 } | |
| 151 | |
| 152 this.endMeasuringHook(); | |
| 153 | |
| 154 // We're done. | |
| 155 if (this.callback_) | |
| 156 this.callback_(); | |
| 157 }; | |
| 158 | |
| 159 window.__ScrollingAction = ScrollingAction; | |
| 160 window.__ScrollingAction_GetBoundingVisibleRect = getBoundingVisibleRect; | |
| 161 })(); | |
| OLD | NEW |