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 /** | |
6 * Creates a new ViewportScroller. | |
7 * A ViewportScroller scrolls the page in response to drag selection with the | |
8 * mouse. | |
9 * @param {Object} viewport The viewport info of the page. | |
10 * @param {Object} plugin The plugin object of PDF. | |
raymes
2015/02/12 06:22:31
nit "The PDF plugin element."
Deepak
2015/02/12 08:28:32
Done.
| |
11 * @param {Object} window The window object for PDF. | |
raymes
2015/02/12 06:22:31
nit: "The window containing the viewer."
Deepak
2015/02/12 08:28:32
Done.
| |
12 */ | |
13 function ViewportScroller(viewport, plugin, window) { | |
14 this.viewport_ = viewport; | |
15 this.plugin_ = plugin; | |
16 this.window_ = window; | |
17 this.mousemoveCallback_ = null; | |
18 this.timerId_ = null; | |
19 this.scrollVelocity_ = null; | |
20 // The period of time to wait between updating the viewport position by the | |
21 // scroll velocity. | |
22 this.DRAG_TIMER_INTERVAL = 100; | |
Sam McNally
2015/02/12 06:27:44
I meant add a trailing underscore, not turn them i
Deepak
2015/02/12 08:28:32
Done.
| |
23 // The maximum drag scroll velocity. | |
24 this.MAX_DRAG_SCROLL_VELOCITY = 100; | |
25 } | |
26 | |
27 ViewportScroller.prototype = { | |
28 /** | |
29 * @private | |
30 * Start scrolling the page by |scrollVelocity_| every |DRAG_TIMER_INTERVAL|. | |
31 */ | |
32 startDragScrollTimer_: function() { | |
33 if (this.timerId_ === null) { | |
34 this.timerId_ = this.window_.setInterval(this.dragScrollPage_.bind(this), | |
35 this.DRAG_TIMER_INTERVAL); | |
36 } | |
37 }, | |
38 | |
39 /** | |
40 * @private | |
41 * Stops the drag scroll timer if it is active. | |
42 */ | |
43 stopDragScrollTimer_: function() { | |
44 if (this.timerId_ !== null) { | |
45 this.window_.clearInterval(this.timerId_); | |
46 this.timerId_ = null; | |
47 } | |
48 }, | |
49 | |
50 /** | |
51 * @private | |
52 * Scrolls the viewport by the current scroll velocity. | |
53 */ | |
54 dragScrollPage_: function() { | |
55 var position = this.viewport_.position; | |
56 position.y += this.scrollVelocity_.y; | |
57 position.x += this.scrollVelocity_.x; | |
58 this.viewport_.position = position; | |
59 }, | |
60 | |
61 /** | |
62 * @private | |
63 * Calculate the velocity to scroll while dragging using the distance of the | |
64 * cursor outside the viewport. | |
Sam McNally
2015/02/12 06:27:45
@param for event
Deepak
2015/02/12 08:28:32
Done.
| |
65 * @return {Object} Object with x and y direction scroll velocity. | |
66 */ | |
67 calculateVelocity_: function(event) { | |
68 var x = Math.min(Math.max(-event.offsetX, | |
69 event.offsetX - this.plugin_.offsetWidth, 0), | |
70 this.MAX_DRAG_SCROLL_VELOCITY) * | |
71 Math.sign(event.offsetX); | |
72 var y = Math.min(Math.max(-event.offsetY, | |
73 event.offsetY - this.plugin_.offsetHeight, 0), | |
74 this.MAX_DRAG_SCROLL_VELOCITY) * | |
75 Math.sign(event.offsetY); | |
76 return { | |
77 x: x, | |
78 y: y | |
79 }; | |
80 }, | |
81 | |
82 /** | |
83 * @private | |
84 * Handles mousemove events. It updates the scroll velocity and starts and | |
85 * stops timer based on scroll velocity. | |
86 * @param {Object} event The mousemove event. | |
87 */ | |
88 onMousemove_: function(event) { | |
89 this.scrollVelocity_ = this.calculateVelocity_(event); | |
90 if (!this.scrollVelocity_.x && !this.scrollVelocity_.y) | |
91 this.stopDragScrollTimer_(); | |
92 else if (!this.timerId_) | |
93 this.startDragScrollTimer_(); | |
94 }, | |
95 | |
96 /** | |
97 * Sets whether to scroll the viewport when the mouse is outside the | |
98 * viewport. | |
99 * @param {boolean} isSelecting Represents selection status. | |
100 */ | |
101 setEnableScrolling: function(isSelecting) { | |
102 if (isSelecting) { | |
103 if (!this.mousemoveCallback_) | |
104 this.mousemoveCallback_ = this.onMousemove_.bind(this); | |
105 this.plugin_.addEventListener('mousemove', this.mousemoveCallback_, | |
106 false); | |
107 } else { | |
108 this.stopDragScrollTimer_(); | |
109 if (this.mousemoveCallback_) { | |
110 this.plugin_.removeEventListener('mousemove', this.mousemoveCallback_, | |
111 false); | |
112 } | |
113 } | |
114 } | |
115 }; | |
OLD | NEW |