Chromium Code Reviews| Index: chrome/browser/resources/pdf/viewportscroller.js |
| diff --git a/chrome/browser/resources/pdf/viewportscroller.js b/chrome/browser/resources/pdf/viewportscroller.js |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..7fd1b9d0eb44d8ad612ec1779d43f12b911a0ba4 |
| --- /dev/null |
| +++ b/chrome/browser/resources/pdf/viewportscroller.js |
| @@ -0,0 +1,124 @@ |
| +// 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.
|
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +'use strict'; |
| + |
| + |
|
Sam McNally
2015/02/11 06:59:13
Remove.
Deepak
2015/02/11 10:29:55
Done.
|
| +/** |
| + * The period of time to wait between updating the viewport position by the |
| + * scroll velocity. |
| + */ |
| +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.
|
| + |
| +/** |
| + * 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.
|
| + * of viewport. |
| + */ |
| +ViewportScroller.MAX_SCROLL_VELOCITY = 100; |
| + |
| +/** |
| + * 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.
|
| + * and mouse is dragging out of the viewport. |
| + * @param {Object} viewport The viewport info of the page. |
| + * @param {Object} plugin The plugin object of PDF. |
| + * @param {Object} window The window object for PDF. |
| + */ |
| +function ViewportScroller(viewport, plugin, window) { |
| + this.viewport_ = viewport; |
| + this.plugin_ = plugin; |
| + this.window_ = window; |
| + this.mousemoveCallback_ = null; |
| + this.timerId_ = null; |
| + this.scrollVelocity_ = null; |
| +} |
| + |
| +ViewportScroller.prototype = { |
| + |
| + /** |
| + * @private |
| + * 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.
|
| + * the timer is stopped. |
| + */ |
| + startDragScrollTimer_: function() { |
| + 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.
|
| + this.dragScrollPage_.bind(this), ViewportScroller.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. |
| + * @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), |
| + ViewportScroller.MAX_SCROLL_VELOCITY) * |
| + Math.sign(event.offsetX); |
| + var y = Math.min(Math.max(-event.offsetY, |
| + event.offsetY - this.plugin_.offsetHeight, 0), |
| + ViewportScroller.MAX_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. |
| + */ |
| + selectionDragListener_: function(event) { |
|
Sam McNally
2015/02/11 06:59:13
onMousemove_
Deepak
2015/02/11 10:29:55
Done.
|
| + this.scrollVelocity_ = this.calculateVelocity_(event); |
| + if (!this.scrollVelocity_.x && !this.scrollVelocity_.y) |
| + this.stopDragScrollTimer_(); |
| + else if (!this.timerId_) |
| + this.startDragScrollTimer_(); |
| + }, |
| + |
| + /** |
| + * @private |
|
Sam McNally
2015/02/11 06:59:13
Remove.
Deepak
2015/02/11 10:29:55
Done.
|
| + * 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.
|
| + * @param {boolean} isSelecting Represents selection status. |
| + */ |
| + viewportScroll: function(isSelecting) { |
|
Sam McNally
2015/02/11 06:59:13
How about calling this setEnableScrolling?
Deepak
2015/02/11 10:29:56
Done.
|
| + if (isSelecting) { |
| + if (!this.mousemoveCallback_) |
| + this.mousemoveCallback_ = this.selectionDragListener_.bind(this); |
| + this.plugin_.addEventListener('mousemove', this.mousemoveCallback_, |
| + false); |
| + } else { |
| + this.stopDragScrollTimer_(); |
| + if (this.mousemoveCallback_) { |
| + this.plugin_.removeEventListener('mousemove', this.mousemoveCallback_, |
| + false); |
| + } |
| + } |
| + } |
| +}; |