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 | |
5 'use strict'; | |
6 | |
7 /** | |
8 * @private | |
9 * The period of time in milliseconds to wait between updating the viewport | |
10 * position by the scroll velocity. | |
11 */ | |
12 ViewportScroller.DRAG_TIMER_INTERVAL_MS_ = 100; | |
13 | |
14 /** | |
15 * @private | |
16 * The maximum drag scroll distance per DRAG_TIMER_INTERVAL in pixels. | |
17 */ | |
18 ViewportScroller.MAX_DRAG_SCROLL_DISTANCE_ = 100; | |
19 | |
20 /** | |
21 * Creates a new ViewportScroller. | |
22 * A ViewportScroller scrolls the page in response to drag selection with the | |
23 * mouse. | |
24 * @param {Object} viewport The viewport info of the page. | |
25 * @param {Object} plugin The PDF plugin element. | |
26 * @param {Object} window The window containing the viewer. | |
27 */ | |
28 function ViewportScroller(viewport, plugin, window) { | |
29 this.viewport_ = viewport; | |
30 this.plugin_ = plugin; | |
31 this.window_ = window; | |
32 this.mousemoveCallback_ = null; | |
33 this.timerId_ = null; | |
34 this.scrollVelocity_ = null; | |
35 this.startTime_ = 0; | |
Bernhard Bauer
2015/02/12 14:25:44
I would probably call this |lastFrameTime_|.
Deepak
2015/02/13 03:40:20
Done.
| |
36 this.newTime_ = 0; | |
37 } | |
38 | |
39 ViewportScroller.prototype = { | |
40 /** | |
41 * @private | |
42 * Start scrolling the page by |scrollVelocity_| every | |
43 * |DRAG_TIMER_INTERVAL_MS_|. | |
44 */ | |
45 startDragScrollTimer_: function() { | |
46 if (this.timerId_ === null) { | |
47 this.timerId_ = | |
48 this.window_.setInterval(this.dragScrollPage_.bind(this), | |
49 ViewportScroller.DRAG_TIMER_INTERVAL_MS_); | |
50 this.startTime_ = new Date().getTime(); | |
Bernhard Bauer
2015/02/12 14:25:44
You can use Date.now() for this.
Deepak
2015/02/13 03:40:20
Done.
| |
51 } | |
52 }, | |
53 | |
54 /** | |
55 * @private | |
56 * Stops the drag scroll timer if it is active. | |
57 */ | |
58 stopDragScrollTimer_: function() { | |
59 if (this.timerId_ !== null) { | |
60 this.window_.clearInterval(this.timerId_); | |
61 this.timerId_ = null; | |
62 this.startTime_ = 0; | |
63 } | |
64 }, | |
65 | |
66 /** | |
67 * @private | |
68 * Scrolls the viewport by the current scroll velocity. | |
69 */ | |
70 dragScrollPage_: function() { | |
71 var position = this.viewport_.position; | |
72 this.newTime_ = new Date().getTime(); | |
Bernhard Bauer
2015/02/12 14:25:44
This can be a local variable.
Deepak
2015/02/13 03:40:20
Done.
| |
73 var timeAdjustment = (this.newTime_ - this.startTime_) / | |
74 ViewportScroller.DRAG_TIMER_INTERVAL_MS_; | |
75 position.y += (this.scrollVelocity_.y * timeAdjustment); | |
76 position.x += (this.scrollVelocity_.x * timeAdjustment); | |
77 this.viewport_.position = position; | |
78 this.startTime_ = this.newTime_; | |
79 }, | |
80 | |
81 /** | |
82 * @private | |
83 * Calculate the velocity to scroll while dragging using the distance of the | |
84 * cursor outside the viewport. | |
85 * @param {Object} event The mousemove event. | |
86 * @return {Object} Object with x and y direction scroll velocity. | |
87 */ | |
88 calculateVelocity_: function(event) { | |
89 var x = Math.min(Math.max(-event.offsetX, | |
90 event.offsetX - this.plugin_.offsetWidth, 0), | |
91 ViewportScroller.MAX_DRAG_SCROLL_DISTANCE_) * | |
92 Math.sign(event.offsetX); | |
93 var y = Math.min(Math.max(-event.offsetY, | |
94 event.offsetY - this.plugin_.offsetHeight, 0), | |
95 ViewportScroller.MAX_DRAG_SCROLL_DISTANCE_) * | |
96 Math.sign(event.offsetY); | |
97 return { | |
98 x: x, | |
99 y: y | |
100 }; | |
101 }, | |
102 | |
103 /** | |
104 * @private | |
105 * Handles mousemove events. It updates the scroll velocity and starts and | |
106 * stops timer based on scroll velocity. | |
107 * @param {Object} event The mousemove event. | |
108 */ | |
109 onMousemove_: function(event) { | |
110 this.scrollVelocity_ = this.calculateVelocity_(event); | |
111 if (!this.scrollVelocity_.x && !this.scrollVelocity_.y) | |
112 this.stopDragScrollTimer_(); | |
113 else if (!this.timerId_) | |
114 this.startDragScrollTimer_(); | |
115 }, | |
116 | |
117 /** | |
118 * Sets whether to scroll the viewport when the mouse is outside the | |
119 * viewport. | |
120 * @param {boolean} isSelecting Represents selection status. | |
121 */ | |
122 setEnableScrolling: function(isSelecting) { | |
123 if (isSelecting) { | |
124 if (!this.mousemoveCallback_) | |
125 this.mousemoveCallback_ = this.onMousemove_.bind(this); | |
126 this.plugin_.addEventListener('mousemove', this.mousemoveCallback_, | |
127 false); | |
128 } else { | |
129 this.stopDragScrollTimer_(); | |
130 if (this.mousemoveCallback_) { | |
131 this.plugin_.removeEventListener('mousemove', this.mousemoveCallback_, | |
132 false); | |
133 } | |
134 } | |
135 } | |
136 }; | |
OLD | NEW |