| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 // This file provides the ScrollAction object, which scrolls a page | 5 // This file provides the ScrollAction object, which scrolls a page |
| 6 // to the bottom or for a specified distance: | 6 // to the bottom or for a specified distance: |
| 7 // 1. var action = new __ScrollAction(callback, opt_distance_func) | 7 // 1. var action = new __ScrollAction(callback, opt_distance_func) |
| 8 // 2. action.start(scroll_options) | 8 // 2. action.start(scroll_options) |
| 9 'use strict'; | 9 'use strict'; |
| 10 | 10 |
| (...skipping 23 matching lines...) Expand all Loading... |
| 34 chrome.gpuBenchmarking && | 34 chrome.gpuBenchmarking && |
| 35 chrome.gpuBenchmarking.smoothScrollBy); | 35 chrome.gpuBenchmarking.smoothScrollBy); |
| 36 } | 36 } |
| 37 | 37 |
| 38 // This class scrolls a page from the top to the bottom once. | 38 // This class scrolls a page from the top to the bottom once. |
| 39 // | 39 // |
| 40 // The page is scrolled down by a single scroll gesture. | 40 // The page is scrolled down by a single scroll gesture. |
| 41 function ScrollAction(opt_callback, opt_distance_func) { | 41 function ScrollAction(opt_callback, opt_distance_func) { |
| 42 var self = this; | 42 var self = this; |
| 43 | 43 |
| 44 this.beginMeasuringHook = function() {} | 44 this.beginMeasuringHook = function() {}; |
| 45 this.endMeasuringHook = function() {} | 45 this.endMeasuringHook = function() {}; |
| 46 | 46 |
| 47 this.callback_ = opt_callback; | 47 this.callback_ = opt_callback; |
| 48 this.distance_func_ = opt_distance_func; | 48 this.distance_func_ = opt_distance_func; |
| 49 } | 49 } |
| 50 | 50 |
| 51 ScrollAction.prototype.getScrollDistanceDown_ = function() { |
| 52 var clientHeight; |
| 53 // clientHeight is "special" for the body element. |
| 54 if (this.element_ == document.body) |
| 55 clientHeight = window.innerHeight; |
| 56 else |
| 57 clientHeight = this.element_.clientHeight; |
| 58 |
| 59 return this.element_.scrollHeight - |
| 60 this.element_.scrollTop - |
| 61 clientHeight; |
| 62 }; |
| 63 |
| 64 ScrollAction.prototype.getScrollDistanceUp_ = function() { |
| 65 return this.element_.scrollTop; |
| 66 }; |
| 67 |
| 68 ScrollAction.prototype.getScrollDistanceRight_ = function() { |
| 69 var clientWidth; |
| 70 // clientWidth is "special" for the body element. |
| 71 if (this.element_ == document.body) |
| 72 clientWidth = window.innerWidth; |
| 73 else |
| 74 clientWidth = this.element_.clientWidth; |
| 75 |
| 76 return this.element_.scrollWidth - this.element_.scrollLeft - clientWidth; |
| 77 }; |
| 78 |
| 79 ScrollAction.prototype.getScrollDistanceLeft_ = function() { |
| 80 return this.element_.scrollLeft; |
| 81 }; |
| 82 |
| 51 ScrollAction.prototype.getScrollDistance_ = function() { | 83 ScrollAction.prototype.getScrollDistance_ = function() { |
| 52 if (this.distance_func_) | 84 if (this.distance_func_) |
| 53 return this.distance_func_(); | 85 return this.distance_func_(); |
| 54 | 86 |
| 55 if (this.options_.direction_ == 'down') { | 87 if (this.options_.direction_ == 'down') { |
| 56 var clientHeight; | 88 return this.getScrollDistanceDown_(); |
| 57 // clientHeight is "special" for the body element. | |
| 58 if (this.element_ == document.body) | |
| 59 clientHeight = window.innerHeight; | |
| 60 else | |
| 61 clientHeight = this.element_.clientHeight; | |
| 62 | |
| 63 return this.element_.scrollHeight - | |
| 64 this.element_.scrollTop - | |
| 65 clientHeight; | |
| 66 } else if (this.options_.direction_ == 'up') { | 89 } else if (this.options_.direction_ == 'up') { |
| 67 return this.element_.scrollTop; | 90 return this.getScrollDistanceUp_(); |
| 68 } else if (this.options_.direction_ == 'right') { | 91 } else if (this.options_.direction_ == 'right') { |
| 69 var clientWidth; | 92 return this.getScrollDistanceRight_(); |
| 70 // clientWidth is "special" for the body element. | |
| 71 if (this.element_ == document.body) | |
| 72 clientWidth = window.innerWidth; | |
| 73 else | |
| 74 clientWidth = this.element_.clientWidth; | |
| 75 | |
| 76 return this.element_.scrollWidth - this.element_.scrollLeft - clientWidth; | |
| 77 } else if (this.options_.direction_ == 'left') { | 93 } else if (this.options_.direction_ == 'left') { |
| 78 return this.element_.scrollLeft; | 94 return this.getScrollDistanceLeft_(); |
| 95 } else if (this.options_.direction_ == 'upleft') { |
| 96 return Math.min(this.getScrollDistanceUp_(), |
| 97 this.getScrollDistanceLeft_()); |
| 98 } else if (this.options_.direction_ == 'upright') { |
| 99 return Math.min(this.getScrollDistanceUp_(), |
| 100 this.getScrollDistanceRight_()); |
| 101 } else if (this.options_.direction_ == 'downleft') { |
| 102 return Math.min(this.getScrollDistanceDown_(), |
| 103 this.getScrollDistanceLeft_()); |
| 104 } else if (this.options_.direction_ == 'downright') { |
| 105 return Math.min(this.getScrollDistanceDown_(), |
| 106 this.getScrollDistanceRight_()); |
| 79 } | 107 } |
| 80 } | 108 }; |
| 81 | 109 |
| 82 ScrollAction.prototype.start = function(opt_options) { | 110 ScrollAction.prototype.start = function(opt_options) { |
| 83 this.options_ = new ScrollGestureOptions(opt_options); | 111 this.options_ = new ScrollGestureOptions(opt_options); |
| 84 // Assign this.element_ here instead of constructor, because the constructor | 112 // Assign this.element_ here instead of constructor, because the constructor |
| 85 // ensures this method will be called after the document is loaded. | 113 // ensures this method will be called after the document is loaded. |
| 86 this.element_ = this.options_.element_; | 114 this.element_ = this.options_.element_; |
| 87 requestAnimationFrame(this.startGesture_.bind(this)); | 115 requestAnimationFrame(this.startGesture_.bind(this)); |
| 88 }; | 116 }; |
| 89 | 117 |
| 90 ScrollAction.prototype.startGesture_ = function() { | 118 ScrollAction.prototype.startGesture_ = function() { |
| (...skipping 19 matching lines...) Expand all Loading... |
| 110 this.endMeasuringHook(); | 138 this.endMeasuringHook(); |
| 111 | 139 |
| 112 // We're done. | 140 // We're done. |
| 113 if (this.callback_) | 141 if (this.callback_) |
| 114 this.callback_(); | 142 this.callback_(); |
| 115 }; | 143 }; |
| 116 | 144 |
| 117 window.__ScrollAction = ScrollAction; | 145 window.__ScrollAction = ScrollAction; |
| 118 window.__ScrollAction_SupportedByBrowser = supportedByBrowser; | 146 window.__ScrollAction_SupportedByBrowser = supportedByBrowser; |
| 119 })(); | 147 })(); |
| OLD | NEW |