Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | |
|
Sam McNally
2015/02/11 06:59:13
Let's call this viewport_scroller.js.
Deepak
2015/02/11 10:29:56
Done.
| |
| 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 | |
|
Sam McNally
2015/02/11 06:59:13
Remove.
Deepak
2015/02/11 10:29:55
Done.
| |
| 8 /** | |
| 9 * The period of time to wait between updating the viewport position by the | |
| 10 * scroll velocity. | |
| 11 */ | |
| 12 ViewportScroller.DRAG_TIMER_INTERVAL = 100; | |
|
Sam McNally
2015/02/11 06:59:13
I think we can make these private.
Deepak
2015/02/11 10:29:55
Done.
| |
| 13 | |
| 14 /** | |
| 15 * The maximum drag scroll velocity when we have selection and drag mouse out | |
|
Sam McNally
2015/02/11 06:59:13
The maximum drag scroll velocity.
Deepak
2015/02/11 10:29:56
Done.
| |
| 16 * of viewport. | |
| 17 */ | |
| 18 ViewportScroller.MAX_SCROLL_VELOCITY = 100; | |
| 19 | |
| 20 /** | |
| 21 * Creates a new ViewportScroller for scrolling viewport when have selection | |
|
Sam McNally
2015/02/11 06:59:13
Creates a new ViewportScroller.
A ViewportScrolle
Deepak
2015/02/11 10:29:56
Done.
| |
| 22 * and mouse is dragging out of the viewport. | |
| 23 * @param {Object} viewport The viewport info of the page. | |
| 24 * @param {Object} plugin The plugin object of PDF. | |
| 25 * @param {Object} window The window object for PDF. | |
| 26 */ | |
| 27 function ViewportScroller(viewport, plugin, window) { | |
| 28 this.viewport_ = viewport; | |
| 29 this.plugin_ = plugin; | |
| 30 this.window_ = window; | |
| 31 this.mousemoveCallback_ = null; | |
| 32 this.timerId_ = null; | |
| 33 this.scrollVelocity_ = null; | |
| 34 } | |
| 35 | |
| 36 ViewportScroller.prototype = { | |
| 37 | |
| 38 /** | |
| 39 * @private | |
| 40 * Start the timer to call dragScrollPage_ function at regular interval until | |
|
Sam McNally
2015/02/11 06:59:12
Start scrolling the page by |scrollVelocity_| ever
Deepak
2015/02/11 10:29:55
Done.
| |
| 41 * the timer is stopped. | |
| 42 */ | |
| 43 startDragScrollTimer_: function() { | |
| 44 this.timerId_ = this.window_.setInterval( | |
|
Sam McNally
2015/02/11 06:59:13
if (this.timerId === null)
Deepak
2015/02/11 10:29:56
Done.
Deepak
2015/02/11 10:29:56
Done.
| |
| 45 this.dragScrollPage_.bind(this), ViewportScroller.DRAG_TIMER_INTERVAL); | |
| 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; | |
| 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 * @return {Object} Object with x and y direction scroll velocity. | |
| 75 */ | |
| 76 calculateVelocity_: function(event) { | |
| 77 var x = Math.min(Math.max(-event.offsetX, | |
| 78 event.offsetX - this.plugin_.offsetWidth, 0), | |
| 79 ViewportScroller.MAX_SCROLL_VELOCITY) * | |
| 80 Math.sign(event.offsetX); | |
| 81 var y = Math.min(Math.max(-event.offsetY, | |
| 82 event.offsetY - this.plugin_.offsetHeight, 0), | |
| 83 ViewportScroller.MAX_SCROLL_VELOCITY) * | |
| 84 Math.sign(event.offsetY); | |
| 85 return { | |
| 86 x: x, | |
| 87 y: y | |
| 88 }; | |
| 89 }, | |
| 90 | |
| 91 /** | |
| 92 * @private | |
| 93 * Handles mousemove events. It updates the scroll velocity and starts and | |
| 94 * stops timer based on scroll velocity. | |
| 95 * @param {Object} event The mousemove event. | |
| 96 */ | |
| 97 selectionDragListener_: function(event) { | |
|
Sam McNally
2015/02/11 06:59:13
onMousemove_
Deepak
2015/02/11 10:29:55
Done.
| |
| 98 this.scrollVelocity_ = this.calculateVelocity_(event); | |
| 99 if (!this.scrollVelocity_.x && !this.scrollVelocity_.y) | |
| 100 this.stopDragScrollTimer_(); | |
| 101 else if (!this.timerId_) | |
| 102 this.startDragScrollTimer_(); | |
| 103 }, | |
| 104 | |
| 105 /** | |
| 106 * @private | |
|
Sam McNally
2015/02/11 06:59:13
Remove.
Deepak
2015/02/11 10:29:55
Done.
| |
| 107 * Function to add and remove callback function when mousemove event comes. | |
|
Sam McNally
2015/02/11 06:59:12
Sets whether to scroll the viewport when the mouse
Deepak
2015/02/11 10:29:55
Done.
| |
| 108 * @param {boolean} isSelecting Represents selection status. | |
| 109 */ | |
| 110 viewportScroll: function(isSelecting) { | |
|
Sam McNally
2015/02/11 06:59:13
How about calling this setEnableScrolling?
Deepak
2015/02/11 10:29:56
Done.
| |
| 111 if (isSelecting) { | |
| 112 if (!this.mousemoveCallback_) | |
| 113 this.mousemoveCallback_ = this.selectionDragListener_.bind(this); | |
| 114 this.plugin_.addEventListener('mousemove', this.mousemoveCallback_, | |
| 115 false); | |
| 116 } else { | |
| 117 this.stopDragScrollTimer_(); | |
| 118 if (this.mousemoveCallback_) { | |
| 119 this.plugin_.removeEventListener('mousemove', this.mousemoveCallback_, | |
| 120 false); | |
| 121 } | |
| 122 } | |
| 123 } | |
| 124 }; | |
| OLD | NEW |