OLD | NEW |
---|---|
(Empty) | |
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 | |
3 // found in the LICENSE file. | |
4 | |
Bernhard Bauer
2015/02/12 09:24:18
'use strict'; ?
Deepak
2015/02/12 10:13:49
actualy @Sam asked me to remove this earlier.
Anyw
Bernhard Bauer
2015/02/12 10:31:51
You mean in https://codereview.chromium.org/814573
Sam McNally
2015/02/12 22:32:01
Yes, just the extra empty line.
| |
5 /** | |
6 * @private | |
7 * The period of time to wait between updating the viewport position by the | |
8 * scroll velocity. | |
9 */ | |
10 ViewportScroller.DRAG_TIMER_INTERVAL_ = 100; | |
Bernhard Bauer
2015/02/12 09:24:18
Please add a unit.
Deepak
2015/02/12 10:13:49
Done.
Bernhard Bauer
2015/02/12 10:31:51
Please also add that to the variable name as a pre
| |
11 | |
12 /** | |
13 * @private | |
14 * The maximum drag scroll velocity. | |
15 */ | |
16 ViewportScroller.MAX_DRAG_SCROLL_VELOCITY_ = 100; | |
Bernhard Bauer
2015/02/12 09:24:18
Please add a unit here as well.
Deepak
2015/02/12 10:13:49
Done.
| |
17 | |
18 /** | |
19 * Creates a new ViewportScroller. | |
20 * A ViewportScroller scrolls the page in response to drag selection with the | |
21 * mouse. | |
22 * @param {Object} viewport The viewport info of the page. | |
23 * @param {Object} plugin The PDF plugin element. | |
24 * @param {Object} window The window containing the viewer. | |
25 */ | |
26 function ViewportScroller(viewport, plugin, window) { | |
27 this.viewport_ = viewport; | |
28 this.plugin_ = plugin; | |
29 this.window_ = window; | |
30 this.mousemoveCallback_ = null; | |
31 this.timerId_ = null; | |
32 this.scrollVelocity_ = null; | |
33 } | |
34 | |
35 ViewportScroller.prototype = { | |
36 /** | |
37 * @private | |
38 * Start scrolling the page by |scrollVelocity_| every |DRAG_TIMER_INTERVAL|. | |
39 */ | |
40 startDragScrollTimer_: function() { | |
41 if (this.timerId_ === null) { | |
42 this.timerId_ = | |
43 this.window_.setInterval(this.dragScrollPage_.bind(this), | |
44 ViewportScroller.DRAG_TIMER_INTERVAL_); | |
45 } | |
46 }, | |
47 | |
48 /** | |
49 * @private | |
50 * Stops the drag scroll timer if it is active. | |
51 */ | |
52 stopDragScrollTimer_: function() { | |
53 if (this.timerId_ !== null) { | |
54 this.window_.clearInterval(this.timerId_); | |
55 this.timerId_ = null; | |
56 } | |
57 }, | |
58 | |
59 /** | |
60 * @private | |
61 * Scrolls the viewport by the current scroll velocity. | |
62 */ | |
63 dragScrollPage_: function() { | |
64 var position = this.viewport_.position; | |
65 position.y += this.scrollVelocity_.y; | |
Bernhard Bauer
2015/02/12 09:24:18
If the timer doesn't fire after exactly the |DRAG_
Deepak
2015/02/12 10:13:49
I am sorry, I don't understood this comment comple
Bernhard Bauer
2015/02/12 10:31:51
If we assume we are scrolling at maximum velocity,
| |
66 position.x += this.scrollVelocity_.x; | |
67 this.viewport_.position = position; | |
68 }, | |
69 | |
70 /** | |
71 * @private | |
72 * Calculate the velocity to scroll while dragging using the distance of the | |
73 * cursor outside the viewport. | |
74 * @param {Object} event The mousemove event. | |
75 * @return {Object} Object with x and y direction scroll velocity. | |
76 */ | |
77 calculateVelocity_: function(event) { | |
78 var x = Math.min(Math.max(-event.offsetX, | |
79 event.offsetX - this.plugin_.offsetWidth, 0), | |
80 ViewportScroller.MAX_DRAG_SCROLL_VELOCITY_) * | |
81 Math.sign(event.offsetX); | |
82 var y = Math.min(Math.max(-event.offsetY, | |
83 event.offsetY - this.plugin_.offsetHeight, 0), | |
84 ViewportScroller.MAX_DRAG_SCROLL_VELOCITY_) * | |
85 Math.sign(event.offsetY); | |
86 return { | |
87 x: x, | |
88 y: y | |
89 }; | |
90 }, | |
91 | |
92 /** | |
93 * @private | |
94 * Handles mousemove events. It updates the scroll velocity and starts and | |
95 * stops timer based on scroll velocity. | |
96 * @param {Object} event The mousemove event. | |
97 */ | |
98 onMousemove_: function(event) { | |
99 this.scrollVelocity_ = this.calculateVelocity_(event); | |
100 if (!this.scrollVelocity_.x && !this.scrollVelocity_.y) | |
101 this.stopDragScrollTimer_(); | |
102 else if (!this.timerId_) | |
103 this.startDragScrollTimer_(); | |
104 }, | |
105 | |
106 /** | |
107 * Sets whether to scroll the viewport when the mouse is outside the | |
108 * viewport. | |
109 * @param {boolean} isSelecting Represents selection status. | |
110 */ | |
111 setEnableScrolling: function(isSelecting) { | |
112 if (isSelecting) { | |
113 if (!this.mousemoveCallback_) | |
114 this.mousemoveCallback_ = this.onMousemove_.bind(this); | |
115 this.plugin_.addEventListener('mousemove', this.mousemoveCallback_, | |
116 false); | |
117 } else { | |
118 this.stopDragScrollTimer_(); | |
119 if (this.mousemoveCallback_) { | |
120 this.plugin_.removeEventListener('mousemove', this.mousemoveCallback_, | |
121 false); | |
122 } | |
123 } | |
124 } | |
125 }; | |
OLD | NEW |