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_ = 100; | |
13 | |
14 /** | |
15 * @private | |
16 * The maximum drag scroll velocity in pixels. | |
Bernhard Bauer
2015/02/12 10:31:51
Pixels isn't really a unit of velocity, pixels per
| |
17 */ | |
18 ViewportScroller.MAX_DRAG_SCROLL_VELOCITY_ = 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 } | |
36 | |
37 ViewportScroller.prototype = { | |
38 /** | |
39 * @private | |
40 * Start scrolling the page by |scrollVelocity_| every |DRAG_TIMER_INTERVAL|. | |
41 */ | |
42 startDragScrollTimer_: function() { | |
43 if (this.timerId_ === null) { | |
44 this.timerId_ = | |
45 this.window_.setInterval(this.dragScrollPage_.bind(this), | |
46 ViewportScroller.DRAG_TIMER_INTERVAL_); | |
47 } | |
48 }, | |
49 | |
50 /** | |
51 * @private | |
52 * Stops the drag scroll timer if it is active. | |
53 */ | |
54 stopDragScrollTimer_: function() { | |
55 if (this.timerId_ !== null) { | |
56 this.window_.clearInterval(this.timerId_); | |
57 this.timerId_ = null; | |
58 } | |
59 }, | |
60 | |
61 /** | |
62 * @private | |
63 * Scrolls the viewport by the current scroll velocity. | |
64 */ | |
65 dragScrollPage_: function() { | |
66 var position = this.viewport_.position; | |
67 position.y += this.scrollVelocity_.y; | |
68 position.x += this.scrollVelocity_.x; | |
69 this.viewport_.position = position; | |
70 }, | |
71 | |
72 /** | |
73 * @private | |
74 * Calculate the velocity to scroll while dragging using the distance of the | |
75 * cursor outside the viewport. | |
76 * @param {Object} event The mousemove event. | |
77 * @return {Object} Object with x and y direction scroll velocity. | |
78 */ | |
79 calculateVelocity_: function(event) { | |
80 var x = Math.min(Math.max(-event.offsetX, | |
81 event.offsetX - this.plugin_.offsetWidth, 0), | |
82 ViewportScroller.MAX_DRAG_SCROLL_VELOCITY_) * | |
83 Math.sign(event.offsetX); | |
84 var y = Math.min(Math.max(-event.offsetY, | |
85 event.offsetY - this.plugin_.offsetHeight, 0), | |
86 ViewportScroller.MAX_DRAG_SCROLL_VELOCITY_) * | |
87 Math.sign(event.offsetY); | |
88 return { | |
89 x: x, | |
90 y: y | |
91 }; | |
92 }, | |
93 | |
94 /** | |
95 * @private | |
96 * Handles mousemove events. It updates the scroll velocity and starts and | |
97 * stops timer based on scroll velocity. | |
98 * @param {Object} event The mousemove event. | |
99 */ | |
100 onMousemove_: function(event) { | |
101 this.scrollVelocity_ = this.calculateVelocity_(event); | |
102 if (!this.scrollVelocity_.x && !this.scrollVelocity_.y) | |
103 this.stopDragScrollTimer_(); | |
104 else if (!this.timerId_) | |
105 this.startDragScrollTimer_(); | |
106 }, | |
107 | |
108 /** | |
109 * Sets whether to scroll the viewport when the mouse is outside the | |
110 * viewport. | |
111 * @param {boolean} isSelecting Represents selection status. | |
112 */ | |
113 setEnableScrolling: function(isSelecting) { | |
114 if (isSelecting) { | |
115 if (!this.mousemoveCallback_) | |
116 this.mousemoveCallback_ = this.onMousemove_.bind(this); | |
117 this.plugin_.addEventListener('mousemove', this.mousemoveCallback_, | |
118 false); | |
119 } else { | |
120 this.stopDragScrollTimer_(); | |
121 if (this.mousemoveCallback_) { | |
122 this.plugin_.removeEventListener('mousemove', this.mousemoveCallback_, | |
123 false); | |
124 } | |
125 } | |
126 } | |
127 }; | |
OLD | NEW |