Chromium Code Reviews| Index: chrome/browser/resources/pdf/viewport_scroller.js |
| diff --git a/chrome/browser/resources/pdf/viewport_scroller.js b/chrome/browser/resources/pdf/viewport_scroller.js |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..7711f18e2dbc894cad703a5567cbeaab34582f7f |
| --- /dev/null |
| +++ b/chrome/browser/resources/pdf/viewport_scroller.js |
| @@ -0,0 +1,115 @@ |
| +// Copyright 2015 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +/** |
| + * Creates a new ViewportScroller. |
| + * A ViewportScroller scrolls the page in response to drag selection with the |
| + * mouse. |
| + * @param {Object} viewport The viewport info of the page. |
| + * @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.
|
| + * @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.
|
| + */ |
| +function ViewportScroller(viewport, plugin, window) { |
| + this.viewport_ = viewport; |
| + this.plugin_ = plugin; |
| + this.window_ = window; |
| + this.mousemoveCallback_ = null; |
| + this.timerId_ = null; |
| + this.scrollVelocity_ = null; |
| + // The period of time to wait between updating the viewport position by the |
| + // scroll velocity. |
| + 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.
|
| + // The maximum drag scroll velocity. |
| + this.MAX_DRAG_SCROLL_VELOCITY = 100; |
| +} |
| + |
| +ViewportScroller.prototype = { |
| + /** |
| + * @private |
| + * Start scrolling the page by |scrollVelocity_| every |DRAG_TIMER_INTERVAL|. |
| + */ |
| + startDragScrollTimer_: function() { |
| + if (this.timerId_ === null) { |
| + this.timerId_ = this.window_.setInterval(this.dragScrollPage_.bind(this), |
| + this.DRAG_TIMER_INTERVAL); |
| + } |
| + }, |
| + |
| + /** |
| + * @private |
| + * Stops the drag scroll timer if it is active. |
| + */ |
| + stopDragScrollTimer_: function() { |
| + if (this.timerId_ !== null) { |
| + this.window_.clearInterval(this.timerId_); |
| + this.timerId_ = null; |
| + } |
| + }, |
| + |
| + /** |
| + * @private |
| + * Scrolls the viewport by the current scroll velocity. |
| + */ |
| + dragScrollPage_: function() { |
| + var position = this.viewport_.position; |
| + position.y += this.scrollVelocity_.y; |
| + position.x += this.scrollVelocity_.x; |
| + this.viewport_.position = position; |
| + }, |
| + |
| + /** |
| + * @private |
| + * Calculate the velocity to scroll while dragging using the distance of the |
| + * cursor outside the viewport. |
|
Sam McNally
2015/02/12 06:27:45
@param for event
Deepak
2015/02/12 08:28:32
Done.
|
| + * @return {Object} Object with x and y direction scroll velocity. |
| + */ |
| + calculateVelocity_: function(event) { |
| + var x = Math.min(Math.max(-event.offsetX, |
| + event.offsetX - this.plugin_.offsetWidth, 0), |
| + this.MAX_DRAG_SCROLL_VELOCITY) * |
| + Math.sign(event.offsetX); |
| + var y = Math.min(Math.max(-event.offsetY, |
| + event.offsetY - this.plugin_.offsetHeight, 0), |
| + this.MAX_DRAG_SCROLL_VELOCITY) * |
| + Math.sign(event.offsetY); |
| + return { |
| + x: x, |
| + y: y |
| + }; |
| + }, |
| + |
| + /** |
| + * @private |
| + * Handles mousemove events. It updates the scroll velocity and starts and |
| + * stops timer based on scroll velocity. |
| + * @param {Object} event The mousemove event. |
| + */ |
| + onMousemove_: function(event) { |
| + this.scrollVelocity_ = this.calculateVelocity_(event); |
| + if (!this.scrollVelocity_.x && !this.scrollVelocity_.y) |
| + this.stopDragScrollTimer_(); |
| + else if (!this.timerId_) |
| + this.startDragScrollTimer_(); |
| + }, |
| + |
| + /** |
| + * Sets whether to scroll the viewport when the mouse is outside the |
| + * viewport. |
| + * @param {boolean} isSelecting Represents selection status. |
| + */ |
| + setEnableScrolling: function(isSelecting) { |
| + if (isSelecting) { |
| + if (!this.mousemoveCallback_) |
| + this.mousemoveCallback_ = this.onMousemove_.bind(this); |
| + this.plugin_.addEventListener('mousemove', this.mousemoveCallback_, |
| + false); |
| + } else { |
| + this.stopDragScrollTimer_(); |
| + if (this.mousemoveCallback_) { |
| + this.plugin_.removeEventListener('mousemove', this.mousemoveCallback_, |
| + false); |
| + } |
| + } |
| + } |
| +}; |