OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 /** | 5 /** |
6 * Returns the height of the intersection of two rectangles. | 6 * Returns the height of the intersection of two rectangles. |
7 * @param {Object} rect1 the first rect | 7 * @param {Object} rect1 the first rect |
8 * @param {Object} rect2 the second rect | 8 * @param {Object} rect2 the second rect |
9 * @return {number} the height of the intersection of the rects | 9 * @return {number} the height of the intersection of the rects |
10 */ | 10 */ |
11 function getIntersectionHeight(rect1, rect2) { | 11 function getIntersectionHeight(rect1, rect2) { |
12 return Math.max(0, | 12 return Math.max(0, |
13 Math.min(rect1.y + rect1.height, rect2.y + rect2.height) - | 13 Math.min(rect1.y + rect1.height, rect2.y + rect2.height) - |
14 Math.max(rect1.y, rect2.y)); | 14 Math.max(rect1.y, rect2.y)); |
15 } | 15 } |
16 | 16 |
17 /** | 17 /** |
18 * Create a new viewport. | 18 * Create a new viewport. |
19 * @param {Window} window the window | 19 * @param {Window} window the window |
20 * @param {Object} sizer is the element which represents the size of the | 20 * @param {Object} sizer is the element which represents the size of the |
21 * document in the viewport | 21 * document in the viewport |
22 * @param {Function} viewportChangedCallback is run when the viewport changes | 22 * @param {Function} viewportChangedCallback is run when the viewport changes |
23 * @param {Function} beforeZoomCallback is run before a change in zoom | 23 * @param {Function} beforeZoomCallback is run before a change in zoom |
24 * @param {Function} afterZoomCallback is run after a change in zoom | 24 * @param {Function} afterZoomCallback is run after a change in zoom |
25 * @param {number} scrollbarWidth the width of scrollbars on the page | 25 * @param {number} scrollbarWidth the width of scrollbars on the page |
| 26 * @param {number} yPos the offset of the viewport from the top of the window |
26 */ | 27 */ |
27 function Viewport(window, | 28 function Viewport(window, |
28 sizer, | 29 sizer, |
29 viewportChangedCallback, | 30 viewportChangedCallback, |
30 beforeZoomCallback, | 31 beforeZoomCallback, |
31 afterZoomCallback, | 32 afterZoomCallback, |
32 scrollbarWidth) { | 33 scrollbarWidth, |
| 34 yPos) { |
33 this.window_ = window; | 35 this.window_ = window; |
34 this.sizer_ = sizer; | 36 this.sizer_ = sizer; |
35 this.viewportChangedCallback_ = viewportChangedCallback; | 37 this.viewportChangedCallback_ = viewportChangedCallback; |
36 this.beforeZoomCallback_ = beforeZoomCallback; | 38 this.beforeZoomCallback_ = beforeZoomCallback; |
37 this.afterZoomCallback_ = afterZoomCallback; | 39 this.afterZoomCallback_ = afterZoomCallback; |
38 this.allowedToChangeZoom_ = false; | 40 this.allowedToChangeZoom_ = false; |
39 this.zoom_ = 1; | 41 this.zoom_ = 1; |
40 this.documentDimensions_ = null; | 42 this.documentDimensions_ = null; |
41 this.pageDimensions_ = []; | 43 this.pageDimensions_ = []; |
42 this.scrollbarWidth_ = scrollbarWidth; | 44 this.scrollbarWidth_ = scrollbarWidth; |
43 this.fittingType_ = Viewport.FittingType.NONE; | 45 this.fittingType_ = Viewport.FittingType.NONE; |
| 46 this.yPos = yPos; |
44 | 47 |
45 window.addEventListener('scroll', this.updateViewport_.bind(this)); | 48 window.addEventListener('scroll', this.updateViewport_.bind(this)); |
46 window.addEventListener('resize', this.resize_.bind(this)); | 49 window.addEventListener('resize', this.resize_.bind(this)); |
47 } | 50 } |
48 | 51 |
49 /** | 52 /** |
50 * Enumeration of page fitting types. | 53 * Enumeration of page fitting types. |
51 * @enum {string} | 54 * @enum {string} |
52 */ | 55 */ |
53 Viewport.FittingType = { | 56 Viewport.FittingType = { |
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
129 | 132 |
130 /** | 133 /** |
131 * @private | 134 * @private |
132 * Helper function called when the zoomed document size changes. | 135 * Helper function called when the zoomed document size changes. |
133 */ | 136 */ |
134 contentSizeChanged_: function() { | 137 contentSizeChanged_: function() { |
135 if (this.documentDimensions_) { | 138 if (this.documentDimensions_) { |
136 this.sizer_.style.width = | 139 this.sizer_.style.width = |
137 this.documentDimensions_.width * this.zoom_ + 'px'; | 140 this.documentDimensions_.width * this.zoom_ + 'px'; |
138 this.sizer_.style.height = | 141 this.sizer_.style.height = |
139 this.documentDimensions_.height * this.zoom_ + 'px'; | 142 this.documentDimensions_.height * this.zoom_ + this.yPos + 'px'; |
140 } | 143 } |
141 }, | 144 }, |
142 | 145 |
143 /** | 146 /** |
144 * @private | 147 * @private |
145 * Called when the viewport should be updated. | 148 * Called when the viewport should be updated. |
146 */ | 149 */ |
147 updateViewport_: function() { | 150 updateViewport_: function() { |
148 this.viewportChangedCallback_(); | 151 this.viewportChangedCallback_(); |
149 }, | 152 }, |
(...skipping 389 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
539 spaceOnLeft = Math.max(spaceOnLeft, 0); | 542 spaceOnLeft = Math.max(spaceOnLeft, 0); |
540 | 543 |
541 return { | 544 return { |
542 x: x * this.zoom_ + spaceOnLeft - this.window_.pageXOffset, | 545 x: x * this.zoom_ + spaceOnLeft - this.window_.pageXOffset, |
543 y: insetDimensions.y * this.zoom_ - this.window_.pageYOffset, | 546 y: insetDimensions.y * this.zoom_ - this.window_.pageYOffset, |
544 width: insetDimensions.width * this.zoom_, | 547 width: insetDimensions.width * this.zoom_, |
545 height: insetDimensions.height * this.zoom_ | 548 height: insetDimensions.height * this.zoom_ |
546 }; | 549 }; |
547 } | 550 } |
548 }; | 551 }; |
OLD | NEW |