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