OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 'use strict'; | 5 'use strict'; |
6 | 6 |
7 /** | 7 /** |
8 * @private | 8 * @private |
9 * The period of time in milliseconds to wait between updating the viewport | 9 * The period of time in milliseconds to wait between updating the viewport |
10 * position by the scroll velocity. | 10 * position by the scroll velocity. |
(...skipping 26 matching lines...) Expand all Loading... |
37 } | 37 } |
38 | 38 |
39 ViewportScroller.prototype = { | 39 ViewportScroller.prototype = { |
40 /** | 40 /** |
41 * @private | 41 * @private |
42 * Start scrolling the page by |scrollVelocity_| every | 42 * Start scrolling the page by |scrollVelocity_| every |
43 * |DRAG_TIMER_INTERVAL_MS_|. | 43 * |DRAG_TIMER_INTERVAL_MS_|. |
44 */ | 44 */ |
45 startDragScrollTimer_: function() { | 45 startDragScrollTimer_: function() { |
46 if (this.timerId_ === null) { | 46 if (this.timerId_ === null) { |
47 this.timerId_ = | 47 this.timerId_ = this.window_.setInterval( |
48 this.window_.setInterval(this.dragScrollPage_.bind(this), | 48 this.dragScrollPage_.bind(this), |
49 ViewportScroller.DRAG_TIMER_INTERVAL_MS_); | 49 ViewportScroller.DRAG_TIMER_INTERVAL_MS_); |
50 this.lastFrameTime_ = Date.now(); | 50 this.lastFrameTime_ = Date.now(); |
51 } | 51 } |
52 }, | 52 }, |
53 | 53 |
54 /** | 54 /** |
55 * @private | 55 * @private |
56 * Stops the drag scroll timer if it is active. | 56 * Stops the drag scroll timer if it is active. |
57 */ | 57 */ |
58 stopDragScrollTimer_: function() { | 58 stopDragScrollTimer_: function() { |
59 if (this.timerId_ !== null) { | 59 if (this.timerId_ !== null) { |
60 this.window_.clearInterval(this.timerId_); | 60 this.window_.clearInterval(this.timerId_); |
61 this.timerId_ = null; | 61 this.timerId_ = null; |
62 this.lastFrameTime_ = 0; | 62 this.lastFrameTime_ = 0; |
63 } | 63 } |
64 }, | 64 }, |
65 | 65 |
66 /** | 66 /** |
67 * @private | 67 * @private |
68 * Scrolls the viewport by the current scroll velocity. | 68 * Scrolls the viewport by the current scroll velocity. |
69 */ | 69 */ |
70 dragScrollPage_: function() { | 70 dragScrollPage_: function() { |
71 var position = this.viewport_.position; | 71 var position = this.viewport_.position; |
72 var currentFrameTime = Date.now(); | 72 var currentFrameTime = Date.now(); |
73 var timeAdjustment = (currentFrameTime - this.lastFrameTime_) / | 73 var timeAdjustment = (currentFrameTime - this.lastFrameTime_) / |
74 ViewportScroller.DRAG_TIMER_INTERVAL_MS_; | 74 ViewportScroller.DRAG_TIMER_INTERVAL_MS_; |
75 position.y += (this.scrollVelocity_.y * timeAdjustment); | 75 position.y += (this.scrollVelocity_.y * timeAdjustment); |
76 position.x += (this.scrollVelocity_.x * timeAdjustment); | 76 position.x += (this.scrollVelocity_.x * timeAdjustment); |
77 this.viewport_.position = position; | 77 this.viewport_.position = position; |
78 this.lastFrameTime_ = currentFrameTime; | 78 this.lastFrameTime_ = currentFrameTime; |
79 }, | 79 }, |
80 | 80 |
81 /** | 81 /** |
82 * @private | 82 * @private |
83 * Calculate the velocity to scroll while dragging using the distance of the | 83 * Calculate the velocity to scroll while dragging using the distance of the |
84 * cursor outside the viewport. | 84 * cursor outside the viewport. |
85 * @param {Object} event The mousemove event. | 85 * @param {Object} event The mousemove event. |
86 * @return {Object} Object with x and y direction scroll velocity. | 86 * @return {Object} Object with x and y direction scroll velocity. |
87 */ | 87 */ |
88 calculateVelocity_: function(event) { | 88 calculateVelocity_: function(event) { |
89 var x = Math.min(Math.max(-event.offsetX, | 89 var x = |
90 event.offsetX - this.plugin_.offsetWidth, 0), | 90 Math.min( |
91 ViewportScroller.MAX_DRAG_SCROLL_DISTANCE_) * | 91 Math.max( |
92 Math.sign(event.offsetX); | 92 -event.offsetX, event.offsetX - this.plugin_.offsetWidth, 0), |
93 var y = Math.min(Math.max(-event.offsetY, | 93 ViewportScroller.MAX_DRAG_SCROLL_DISTANCE_) * |
94 event.offsetY - this.plugin_.offsetHeight, 0), | 94 Math.sign(event.offsetX); |
95 ViewportScroller.MAX_DRAG_SCROLL_DISTANCE_) * | 95 var y = |
96 Math.sign(event.offsetY); | 96 Math.min( |
97 return { | 97 Math.max( |
98 x: x, | 98 -event.offsetY, event.offsetY - this.plugin_.offsetHeight, 0), |
99 y: y | 99 ViewportScroller.MAX_DRAG_SCROLL_DISTANCE_) * |
100 }; | 100 Math.sign(event.offsetY); |
| 101 return {x: x, y: y}; |
101 }, | 102 }, |
102 | 103 |
103 /** | 104 /** |
104 * @private | 105 * @private |
105 * Handles mousemove events. It updates the scroll velocity and starts and | 106 * Handles mousemove events. It updates the scroll velocity and starts and |
106 * stops timer based on scroll velocity. | 107 * stops timer based on scroll velocity. |
107 * @param {Object} event The mousemove event. | 108 * @param {Object} event The mousemove event. |
108 */ | 109 */ |
109 onMousemove_: function(event) { | 110 onMousemove_: function(event) { |
110 this.scrollVelocity_ = this.calculateVelocity_(event); | 111 this.scrollVelocity_ = this.calculateVelocity_(event); |
111 if (!this.scrollVelocity_.x && !this.scrollVelocity_.y) | 112 if (!this.scrollVelocity_.x && !this.scrollVelocity_.y) |
112 this.stopDragScrollTimer_(); | 113 this.stopDragScrollTimer_(); |
113 else if (!this.timerId_) | 114 else if (!this.timerId_) |
114 this.startDragScrollTimer_(); | 115 this.startDragScrollTimer_(); |
115 }, | 116 }, |
116 | 117 |
117 /** | 118 /** |
118 * Sets whether to scroll the viewport when the mouse is outside the | 119 * Sets whether to scroll the viewport when the mouse is outside the |
119 * viewport. | 120 * viewport. |
120 * @param {boolean} isSelecting Represents selection status. | 121 * @param {boolean} isSelecting Represents selection status. |
121 */ | 122 */ |
122 setEnableScrolling: function(isSelecting) { | 123 setEnableScrolling: function(isSelecting) { |
123 if (isSelecting) { | 124 if (isSelecting) { |
124 if (!this.mousemoveCallback_) | 125 if (!this.mousemoveCallback_) |
125 this.mousemoveCallback_ = this.onMousemove_.bind(this); | 126 this.mousemoveCallback_ = this.onMousemove_.bind(this); |
126 this.plugin_.addEventListener('mousemove', this.mousemoveCallback_, | 127 this.plugin_.addEventListener( |
127 false); | 128 'mousemove', this.mousemoveCallback_, false); |
128 } else { | 129 } else { |
129 this.stopDragScrollTimer_(); | 130 this.stopDragScrollTimer_(); |
130 if (this.mousemoveCallback_) { | 131 if (this.mousemoveCallback_) { |
131 this.plugin_.removeEventListener('mousemove', this.mousemoveCallback_, | 132 this.plugin_.removeEventListener( |
132 false); | 133 'mousemove', this.mousemoveCallback_, false); |
133 } | 134 } |
134 } | 135 } |
135 } | 136 } |
136 }; | 137 }; |
OLD | NEW |