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.
5 // This file provides the ScrollAction object, which scrolls a page
6 // to the bottom or for a specified distance:
7 // 1. var action = new __ScrollAction(callback, opt_distance_func)
8 // 2. action.start(scroll_options)
12 var MAX_SCROLL_LENGTH_TIME_MS = 6250;
14 function ScrollGestureOptions(opt_options) {
16 this.element_ = opt_options.element;
17 this.left_start_ratio_ = opt_options.left_start_ratio;
18 this.top_start_ratio_ = opt_options.top_start_ratio;
19 this.direction_ = opt_options.direction;
20 this.speed_ = opt_options.speed;
21 this.gesture_source_type_ = opt_options.gesture_source_type;
23 this.element_ = document.body;
24 this.left_start_ratio_ = 0.5;
25 this.top_start_ratio_ = 0.5;
26 this.direction_ = 'down';
28 this.gesture_source_type_ = chrome.gpuBenchmarking.DEFAULT_INPUT;
32 function supportedByBrowser() {
33 return !!(window.chrome &&
34 chrome.gpuBenchmarking &&
35 chrome.gpuBenchmarking.smoothScrollBy);
38 // This class scrolls a page from the top to the bottom once.
40 // The page is scrolled down by a single scroll gesture.
41 function ScrollAction(opt_callback, opt_distance_func) {
44 this.beginMeasuringHook = function() {}
45 this.endMeasuringHook = function() {}
47 this.callback_ = opt_callback;
48 this.distance_func_ = opt_distance_func;
51 ScrollAction.prototype.getScrollDistance_ = function() {
52 if (this.distance_func_)
53 return this.distance_func_();
55 if (this.options_.direction_ == 'down') {
57 // clientHeight is "special" for the body element.
58 if (this.element_ == document.body)
59 clientHeight = window.innerHeight;
61 clientHeight = this.element_.clientHeight;
63 return this.element_.scrollHeight -
64 this.element_.scrollTop -
66 } else if (this.options_.direction_ == 'up') {
67 return this.element_.scrollTop;
68 } else if (this.options_.direction_ == 'right') {
70 // clientWidth is "special" for the body element.
71 if (this.element_ == document.body)
72 clientWidth = window.innerWidth;
74 clientWidth = this.element_.clientWidth;
76 return this.element_.scrollWidth - this.element_.scrollLeft - clientWidth;
77 } else if (this.options_.direction_ == 'left') {
78 return this.element_.scrollLeft;
82 ScrollAction.prototype.start = function(opt_options) {
83 this.options_ = new ScrollGestureOptions(opt_options);
84 // Assign this.element_ here instead of constructor, because the constructor
85 // ensures this method will be called after the document is loaded.
86 this.element_ = this.options_.element_;
87 requestAnimationFrame(this.startGesture_.bind(this));
90 ScrollAction.prototype.startGesture_ = function() {
91 this.beginMeasuringHook();
93 var max_scroll_length_pixels = (MAX_SCROLL_LENGTH_TIME_MS / 1000) *
95 var distance = Math.min(max_scroll_length_pixels,
96 this.getScrollDistance_());
98 var rect = __GestureCommon_GetBoundingVisibleRect(this.options_.element_);
100 rect.left + rect.width * this.options_.left_start_ratio_;
102 rect.top + rect.height * this.options_.top_start_ratio_;
103 chrome.gpuBenchmarking.smoothScrollBy(
104 distance, this.onGestureComplete_.bind(this), start_left, start_top,
105 this.options_.gesture_source_type_, this.options_.direction_,
106 this.options_.speed_);
109 ScrollAction.prototype.onGestureComplete_ = function() {
110 this.endMeasuringHook();
117 window.__ScrollAction = ScrollAction;
118 window.__ScrollAction_SupportedByBrowser = supportedByBrowser;