[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 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 |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | /** |
sammc | d06823a96 | 2014-11-10 04:51:15 | [diff] [blame] | 6 | * Returns the height of the intersection of two rectangles. |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 7 | * @param {Object} rect1 the first rect |
| 8 | * @param {Object} rect2 the second rect |
sammc | d06823a96 | 2014-11-10 04:51:15 | [diff] [blame] | 9 | * @return {number} the height of the intersection of the rects |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 10 | */ |
sammc | d06823a96 | 2014-11-10 04:51:15 | [diff] [blame] | 11 | function getIntersectionHeight(rect1, rect2) { |
| 12 | return Math.max(0, |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 13 | Math.min(rect1.y + rect1.height, rect2.y + rect2.height) - |
| 14 | Math.max(rect1.y, rect2.y)); |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 15 | } |
| 16 | |
| 17 | /** |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 18 | * Create a new viewport. |
alexandrec | 9754b9e4 | 2015-01-19 03:41:19 | [diff] [blame] | 19 | * @constructor |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 20 | * @param {Window} window the window |
| 21 | * @param {Object} sizer is the element which represents the size of the |
| 22 | * document in the viewport |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 23 | * @param {Function} viewportChangedCallback is run when the viewport changes |
[email protected] | 499e956 | 2014-06-26 05:45:27 | [diff] [blame] | 24 | * @param {Function} beforeZoomCallback is run before a change in zoom |
| 25 | * @param {Function} afterZoomCallback is run after a change in zoom |
[email protected] | 345e7c6 | 2014-05-02 09:52:58 | [diff] [blame] | 26 | * @param {number} scrollbarWidth the width of scrollbars on the page |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 27 | */ |
| 28 | function Viewport(window, |
| 29 | sizer, |
[email protected] | 345e7c6 | 2014-05-02 09:52:58 | [diff] [blame] | 30 | viewportChangedCallback, |
[email protected] | 499e956 | 2014-06-26 05:45:27 | [diff] [blame] | 31 | beforeZoomCallback, |
| 32 | afterZoomCallback, |
alexandrec | 915a1a4 | 2015-02-06 06:41:16 | [diff] [blame] | 33 | scrollbarWidth) { |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 34 | this.window_ = window; |
| 35 | this.sizer_ = sizer; |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 36 | this.viewportChangedCallback_ = viewportChangedCallback; |
[email protected] | 499e956 | 2014-06-26 05:45:27 | [diff] [blame] | 37 | this.beforeZoomCallback_ = beforeZoomCallback; |
| 38 | this.afterZoomCallback_ = afterZoomCallback; |
| 39 | this.allowedToChangeZoom_ = false; |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 40 | this.zoom_ = 1; |
[email protected] | 312112c7 | 2014-04-14 01:45:43 | [diff] [blame] | 41 | this.documentDimensions_ = null; |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 42 | this.pageDimensions_ = []; |
[email protected] | 345e7c6 | 2014-05-02 09:52:58 | [diff] [blame] | 43 | this.scrollbarWidth_ = scrollbarWidth; |
[email protected] | 312112c7 | 2014-04-14 01:45:43 | [diff] [blame] | 44 | this.fittingType_ = Viewport.FittingType.NONE; |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 45 | |
| 46 | window.addEventListener('scroll', this.updateViewport_.bind(this)); |
[email protected] | 312112c7 | 2014-04-14 01:45:43 | [diff] [blame] | 47 | window.addEventListener('resize', this.resize_.bind(this)); |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 48 | } |
| 49 | |
[email protected] | 312112c7 | 2014-04-14 01:45:43 | [diff] [blame] | 50 | /** |
| 51 | * Enumeration of page fitting types. |
| 52 | * @enum {string} |
| 53 | */ |
| 54 | Viewport.FittingType = { |
| 55 | NONE: 'none', |
| 56 | FIT_TO_PAGE: 'fit-to-page', |
| 57 | FIT_TO_WIDTH: 'fit-to-width' |
| 58 | }; |
| 59 | |
| 60 | /** |
[email protected] | 8dcaa26 | 2014-05-30 13:33:37 | [diff] [blame] | 61 | * The increment to scroll a page by in pixels when up/down/left/right arrow |
| 62 | * keys are pressed. Usually we just let the browser handle scrolling on the |
| 63 | * window when these keys are pressed but in certain cases we need to simulate |
| 64 | * these events. |
| 65 | */ |
| 66 | Viewport.SCROLL_INCREMENT = 40; |
| 67 | |
| 68 | /** |
[email protected] | 312112c7 | 2014-04-14 01:45:43 | [diff] [blame] | 69 | * Predefined zoom factors to be used when zooming in/out. These are in |
[email protected] | c90b329 | 2014-08-20 00:57:41 | [diff] [blame] | 70 | * ascending order. This should match the list in |
wjmaclean | 91ddc47 | 2015-01-15 19:58:27 | [diff] [blame] | 71 | * components/ui/zoom/page_zoom_constants.h |
[email protected] | 312112c7 | 2014-04-14 01:45:43 | [diff] [blame] | 72 | */ |
| 73 | Viewport.ZOOM_FACTORS = [0.25, 0.333, 0.5, 0.666, 0.75, 0.9, 1, |
| 74 | 1.1, 1.25, 1.5, 1.75, 2, 2.5, 3, 4, 5]; |
| 75 | |
| 76 | /** |
[email protected] | 4271e16b | 2014-08-22 12:18:59 | [diff] [blame] | 77 | * The minimum and maximum range to be used to clip zoom factor. |
| 78 | */ |
| 79 | Viewport.ZOOM_FACTOR_RANGE = { |
| 80 | min: Viewport.ZOOM_FACTORS[0], |
| 81 | max: Viewport.ZOOM_FACTORS[Viewport.ZOOM_FACTORS.length - 1] |
| 82 | }; |
| 83 | |
| 84 | /** |
[email protected] | 312112c7 | 2014-04-14 01:45:43 | [diff] [blame] | 85 | * The width of the page shadow around pages in pixels. |
| 86 | */ |
| 87 | Viewport.PAGE_SHADOW = {top: 3, bottom: 7, left: 5, right: 5}; |
| 88 | |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 89 | Viewport.prototype = { |
| 90 | /** |
| 91 | * @private |
| 92 | * Returns true if the document needs scrollbars at the given zoom level. |
| 93 | * @param {number} zoom compute whether scrollbars are needed at this zoom |
[email protected] | 312112c7 | 2014-04-14 01:45:43 | [diff] [blame] | 94 | * @return {Object} with 'horizontal' and 'vertical' keys which map to bool |
| 95 | * values indicating if the horizontal and vertical scrollbars are needed |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 96 | * respectively. |
| 97 | */ |
| 98 | documentNeedsScrollbars_: function(zoom) { |
[email protected] | 499e956 | 2014-06-26 05:45:27 | [diff] [blame] | 99 | if (!this.documentDimensions_) { |
| 100 | return { |
| 101 | horizontal: false, |
| 102 | vertical: false |
| 103 | }; |
| 104 | } |
[email protected] | 312112c7 | 2014-04-14 01:45:43 | [diff] [blame] | 105 | var documentWidth = this.documentDimensions_.width * zoom; |
| 106 | var documentHeight = this.documentDimensions_.height * zoom; |
sammc | 63334fed | 2014-11-10 03:07:35 | [diff] [blame] | 107 | |
| 108 | // If scrollbars are required for one direction, expand the document in the |
| 109 | // other direction to take the width of the scrollbars into account when |
| 110 | // deciding whether the other direction needs scrollbars. |
| 111 | if (documentWidth > this.window_.innerWidth) |
| 112 | documentHeight += this.scrollbarWidth_; |
| 113 | else if (documentHeight > this.window_.innerHeight) |
| 114 | documentWidth += this.scrollbarWidth_; |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 115 | return { |
[email protected] | 312112c7 | 2014-04-14 01:45:43 | [diff] [blame] | 116 | horizontal: documentWidth > this.window_.innerWidth, |
| 117 | vertical: documentHeight > this.window_.innerHeight |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 118 | }; |
| 119 | }, |
| 120 | |
| 121 | /** |
| 122 | * Returns true if the document needs scrollbars at the current zoom level. |
| 123 | * @return {Object} with 'x' and 'y' keys which map to bool values |
| 124 | * indicating if the horizontal and vertical scrollbars are needed |
| 125 | * respectively. |
| 126 | */ |
| 127 | documentHasScrollbars: function() { |
| 128 | return this.documentNeedsScrollbars_(this.zoom_); |
| 129 | }, |
| 130 | |
| 131 | /** |
| 132 | * @private |
| 133 | * Helper function called when the zoomed document size changes. |
| 134 | */ |
| 135 | contentSizeChanged_: function() { |
[email protected] | 345e7c6 | 2014-05-02 09:52:58 | [diff] [blame] | 136 | if (this.documentDimensions_) { |
| 137 | this.sizer_.style.width = |
| 138 | this.documentDimensions_.width * this.zoom_ + 'px'; |
| 139 | this.sizer_.style.height = |
alexandrec | 915a1a4 | 2015-02-06 06:41:16 | [diff] [blame] | 140 | this.documentDimensions_.height * this.zoom_ + 'px'; |
[email protected] | 345e7c6 | 2014-05-02 09:52:58 | [diff] [blame] | 141 | } |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 142 | }, |
| 143 | |
| 144 | /** |
[email protected] | 312112c7 | 2014-04-14 01:45:43 | [diff] [blame] | 145 | * @private |
| 146 | * Called when the viewport should be updated. |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 147 | */ |
[email protected] | 312112c7 | 2014-04-14 01:45:43 | [diff] [blame] | 148 | updateViewport_: function() { |
| 149 | this.viewportChangedCallback_(); |
| 150 | }, |
| 151 | |
| 152 | /** |
| 153 | * @private |
| 154 | * Called when the viewport size changes. |
| 155 | */ |
| 156 | resize_: function() { |
| 157 | if (this.fittingType_ == Viewport.FittingType.FIT_TO_PAGE) |
raymes | 161514f | 2015-02-17 05:09:51 | [diff] [blame^] | 158 | this.fitToPageInternal_(false); |
[email protected] | 312112c7 | 2014-04-14 01:45:43 | [diff] [blame] | 159 | else if (this.fittingType_ == Viewport.FittingType.FIT_TO_WIDTH) |
| 160 | this.fitToWidth(); |
| 161 | else |
| 162 | this.updateViewport_(); |
| 163 | }, |
| 164 | |
| 165 | /** |
| 166 | * @type {Object} the scroll position of the viewport. |
| 167 | */ |
| 168 | get position() { |
| 169 | return { |
| 170 | x: this.window_.pageXOffset, |
| 171 | y: this.window_.pageYOffset |
| 172 | }; |
| 173 | }, |
| 174 | |
| 175 | /** |
| 176 | * Scroll the viewport to the specified position. |
| 177 | * @type {Object} position the position to scroll to. |
| 178 | */ |
| 179 | set position(position) { |
| 180 | this.window_.scrollTo(position.x, position.y); |
| 181 | }, |
| 182 | |
| 183 | /** |
| 184 | * @type {Object} the size of the viewport excluding scrollbars. |
| 185 | */ |
| 186 | get size() { |
| 187 | var needsScrollbars = this.documentNeedsScrollbars_(this.zoom_); |
| 188 | var scrollbarWidth = needsScrollbars.vertical ? this.scrollbarWidth_ : 0; |
| 189 | var scrollbarHeight = needsScrollbars.horizontal ? this.scrollbarWidth_ : 0; |
| 190 | return { |
| 191 | width: this.window_.innerWidth - scrollbarWidth, |
| 192 | height: this.window_.innerHeight - scrollbarHeight |
| 193 | }; |
| 194 | }, |
| 195 | |
| 196 | /** |
| 197 | * @type {number} the zoom level of the viewport. |
| 198 | */ |
| 199 | get zoom() { |
| 200 | return this.zoom_; |
| 201 | }, |
| 202 | |
| 203 | /** |
| 204 | * @private |
[email protected] | 499e956 | 2014-06-26 05:45:27 | [diff] [blame] | 205 | * Used to wrap a function that might perform zooming on the viewport. This is |
| 206 | * required so that we can notify the plugin that zooming is in progress |
| 207 | * so that while zooming is taking place it can stop reacting to scroll events |
| 208 | * from the viewport. This is to avoid flickering. |
| 209 | */ |
| 210 | mightZoom_: function(f) { |
| 211 | this.beforeZoomCallback_(); |
| 212 | this.allowedToChangeZoom_ = true; |
| 213 | f(); |
| 214 | this.allowedToChangeZoom_ = false; |
| 215 | this.afterZoomCallback_(); |
| 216 | }, |
| 217 | |
| 218 | /** |
| 219 | * @private |
[email protected] | 312112c7 | 2014-04-14 01:45:43 | [diff] [blame] | 220 | * Sets the zoom of the viewport. |
| 221 | * @param {number} newZoom the zoom level to zoom to. |
| 222 | */ |
[email protected] | fbad5bb | 2014-07-18 07:20:36 | [diff] [blame] | 223 | setZoomInternal_: function(newZoom) { |
| 224 | if (!this.allowedToChangeZoom_) { |
| 225 | throw 'Called Viewport.setZoomInternal_ without calling ' + |
| 226 | 'Viewport.mightZoom_.'; |
| 227 | } |
sammc | 5c33b7e | 2014-11-07 04:03:09 | [diff] [blame] | 228 | // Record the scroll position (relative to the top-left of the window). |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 229 | var currentScrollPos = [ |
sammc | 5c33b7e | 2014-11-07 04:03:09 | [diff] [blame] | 230 | this.window_.pageXOffset / this.zoom_, |
| 231 | this.window_.pageYOffset / this.zoom_ |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 232 | ]; |
sammc | 5c33b7e | 2014-11-07 04:03:09 | [diff] [blame] | 233 | this.zoom_ = newZoom; |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 234 | this.contentSizeChanged_(); |
| 235 | // Scroll to the scaled scroll position. |
sammc | 5c33b7e | 2014-11-07 04:03:09 | [diff] [blame] | 236 | this.window_.scrollTo(currentScrollPos[0] * newZoom, |
| 237 | currentScrollPos[1] * newZoom); |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 238 | }, |
| 239 | |
| 240 | /** |
[email protected] | fbad5bb | 2014-07-18 07:20:36 | [diff] [blame] | 241 | * Sets the zoom to the given zoom level. |
| 242 | * @param {number} newZoom the zoom level to zoom to. |
[email protected] | 499e956 | 2014-06-26 05:45:27 | [diff] [blame] | 243 | */ |
[email protected] | fbad5bb | 2014-07-18 07:20:36 | [diff] [blame] | 244 | setZoom: function(newZoom) { |
[email protected] | 4271e16b | 2014-08-22 12:18:59 | [diff] [blame] | 245 | newZoom = Math.max(Viewport.ZOOM_FACTOR_RANGE.min, |
| 246 | Math.min(newZoom, Viewport.ZOOM_FACTOR_RANGE.max)); |
[email protected] | 499e956 | 2014-06-26 05:45:27 | [diff] [blame] | 247 | this.mightZoom_(function() { |
[email protected] | fbad5bb | 2014-07-18 07:20:36 | [diff] [blame] | 248 | this.setZoomInternal_(newZoom); |
| 249 | this.updateViewport_(); |
[email protected] | 499e956 | 2014-06-26 05:45:27 | [diff] [blame] | 250 | }.bind(this)); |
| 251 | }, |
| 252 | |
| 253 | /** |
[email protected] | 312112c7 | 2014-04-14 01:45:43 | [diff] [blame] | 254 | * @type {number} the width of scrollbars in the viewport in pixels. |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 255 | */ |
[email protected] | 312112c7 | 2014-04-14 01:45:43 | [diff] [blame] | 256 | get scrollbarWidth() { |
| 257 | return this.scrollbarWidth_; |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 258 | }, |
| 259 | |
| 260 | /** |
[email protected] | 312112c7 | 2014-04-14 01:45:43 | [diff] [blame] | 261 | * @type {Viewport.FittingType} the fitting type the viewport is currently in. |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 262 | */ |
[email protected] | 312112c7 | 2014-04-14 01:45:43 | [diff] [blame] | 263 | get fittingType() { |
| 264 | return this.fittingType_; |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 265 | }, |
| 266 | |
| 267 | /** |
[email protected] | 56b7e3c | 2014-02-20 04:31:24 | [diff] [blame] | 268 | * @private |
| 269 | * @param {integer} y the y-coordinate to get the page at. |
| 270 | * @return {integer} the index of a page overlapping the given y-coordinate. |
| 271 | */ |
| 272 | getPageAtY_: function(y) { |
| 273 | var min = 0; |
| 274 | var max = this.pageDimensions_.length - 1; |
| 275 | while (max >= min) { |
| 276 | var page = Math.floor(min + ((max - min) / 2)); |
[email protected] | 13df2a4 | 2014-02-27 03:50:41 | [diff] [blame] | 277 | // There might be a gap between the pages, in which case use the bottom |
| 278 | // of the previous page as the top for finding the page. |
| 279 | var top = 0; |
| 280 | if (page > 0) { |
| 281 | top = this.pageDimensions_[page - 1].y + |
| 282 | this.pageDimensions_[page - 1].height; |
| 283 | } |
| 284 | var bottom = this.pageDimensions_[page].y + |
| 285 | this.pageDimensions_[page].height; |
| 286 | |
[email protected] | 56b7e3c | 2014-02-20 04:31:24 | [diff] [blame] | 287 | if (top <= y && bottom > y) |
| 288 | return page; |
| 289 | else if (top > y) |
| 290 | max = page - 1; |
| 291 | else |
| 292 | min = page + 1; |
| 293 | } |
| 294 | return 0; |
| 295 | }, |
| 296 | |
| 297 | /** |
sammc | d06823a96 | 2014-11-10 04:51:15 | [diff] [blame] | 298 | * Returns the page with the greatest proportion of its height in the current |
| 299 | * viewport. |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 300 | * @return {int} the index of the most visible page. |
| 301 | */ |
| 302 | getMostVisiblePage: function() { |
[email protected] | 312112c7 | 2014-04-14 01:45:43 | [diff] [blame] | 303 | var firstVisiblePage = this.getPageAtY_(this.position.y / this.zoom_); |
sammc | d06823a96 | 2014-11-10 04:51:15 | [diff] [blame] | 304 | if (firstVisiblePage == this.pageDimensions_.length - 1) |
| 305 | return firstVisiblePage; |
| 306 | |
[email protected] | 345e7c6 | 2014-05-02 09:52:58 | [diff] [blame] | 307 | var viewportRect = { |
| 308 | x: this.position.x / this.zoom_, |
| 309 | y: this.position.y / this.zoom_, |
| 310 | width: this.size.width / this.zoom_, |
| 311 | height: this.size.height / this.zoom_ |
| 312 | }; |
sammc | d06823a96 | 2014-11-10 04:51:15 | [diff] [blame] | 313 | var firstVisiblePageVisibility = getIntersectionHeight( |
| 314 | this.pageDimensions_[firstVisiblePage], viewportRect) / |
| 315 | this.pageDimensions_[firstVisiblePage].height; |
| 316 | var nextPageVisibility = getIntersectionHeight( |
| 317 | this.pageDimensions_[firstVisiblePage + 1], viewportRect) / |
| 318 | this.pageDimensions_[firstVisiblePage + 1].height; |
| 319 | if (nextPageVisibility > firstVisiblePageVisibility) |
| 320 | return firstVisiblePage + 1; |
| 321 | return firstVisiblePage; |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 322 | }, |
| 323 | |
| 324 | /** |
| 325 | * @private |
| 326 | * Compute the zoom level for fit-to-page or fit-to-width. |pageDimensions| is |
| 327 | * the dimensions for a given page and if |widthOnly| is true, it indicates |
| 328 | * that fit-to-page zoom should be computed rather than fit-to-page. |
| 329 | * @param {Object} pageDimensions the dimensions of a given page |
| 330 | * @param {boolean} widthOnly a bool indicating whether fit-to-page or |
| 331 | * fit-to-width should be computed. |
| 332 | * @return {number} the zoom to use |
| 333 | */ |
| 334 | computeFittingZoom_: function(pageDimensions, widthOnly) { |
| 335 | // First compute the zoom without scrollbars. |
| 336 | var zoomWidth = this.window_.innerWidth / pageDimensions.width; |
| 337 | var zoom; |
alexandrec | 9754b9e4 | 2015-01-19 03:41:19 | [diff] [blame] | 338 | var zoomHeight; |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 339 | if (widthOnly) { |
| 340 | zoom = zoomWidth; |
| 341 | } else { |
alexandrec | 9754b9e4 | 2015-01-19 03:41:19 | [diff] [blame] | 342 | zoomHeight = this.window_.innerHeight / pageDimensions.height; |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 343 | zoom = Math.min(zoomWidth, zoomHeight); |
| 344 | } |
| 345 | // Check if there needs to be any scrollbars. |
| 346 | var needsScrollbars = this.documentNeedsScrollbars_(zoom); |
| 347 | |
| 348 | // If the document fits, just return the zoom. |
[email protected] | 312112c7 | 2014-04-14 01:45:43 | [diff] [blame] | 349 | if (!needsScrollbars.horizontal && !needsScrollbars.vertical) |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 350 | return zoom; |
| 351 | |
| 352 | var zoomedDimensions = { |
| 353 | width: this.documentDimensions_.width * zoom, |
| 354 | height: this.documentDimensions_.height * zoom |
| 355 | }; |
| 356 | |
| 357 | // Check if adding a scrollbar will result in needing the other scrollbar. |
| 358 | var scrollbarWidth = this.scrollbarWidth_; |
[email protected] | 312112c7 | 2014-04-14 01:45:43 | [diff] [blame] | 359 | if (needsScrollbars.horizontal && |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 360 | zoomedDimensions.height > this.window_.innerHeight - scrollbarWidth) { |
[email protected] | 312112c7 | 2014-04-14 01:45:43 | [diff] [blame] | 361 | needsScrollbars.vertical = true; |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 362 | } |
[email protected] | 312112c7 | 2014-04-14 01:45:43 | [diff] [blame] | 363 | if (needsScrollbars.vertical && |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 364 | zoomedDimensions.width > this.window_.innerWidth - scrollbarWidth) { |
[email protected] | 312112c7 | 2014-04-14 01:45:43 | [diff] [blame] | 365 | needsScrollbars.horizontal = true; |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 366 | } |
| 367 | |
| 368 | // Compute available window space. |
| 369 | var windowWithScrollbars = { |
| 370 | width: this.window_.innerWidth, |
| 371 | height: this.window_.innerHeight |
| 372 | }; |
[email protected] | 312112c7 | 2014-04-14 01:45:43 | [diff] [blame] | 373 | if (needsScrollbars.horizontal) |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 374 | windowWithScrollbars.height -= scrollbarWidth; |
[email protected] | 312112c7 | 2014-04-14 01:45:43 | [diff] [blame] | 375 | if (needsScrollbars.vertical) |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 376 | windowWithScrollbars.width -= scrollbarWidth; |
| 377 | |
| 378 | // Recompute the zoom. |
| 379 | zoomWidth = windowWithScrollbars.width / pageDimensions.width; |
| 380 | if (widthOnly) { |
| 381 | zoom = zoomWidth; |
| 382 | } else { |
alexandrec | 9754b9e4 | 2015-01-19 03:41:19 | [diff] [blame] | 383 | zoomHeight = windowWithScrollbars.height / pageDimensions.height; |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 384 | zoom = Math.min(zoomWidth, zoomHeight); |
| 385 | } |
| 386 | return zoom; |
| 387 | }, |
| 388 | |
| 389 | /** |
| 390 | * Zoom the viewport so that the page-width consumes the entire viewport. |
| 391 | */ |
| 392 | fitToWidth: function() { |
[email protected] | 499e956 | 2014-06-26 05:45:27 | [diff] [blame] | 393 | this.mightZoom_(function() { |
| 394 | this.fittingType_ = Viewport.FittingType.FIT_TO_WIDTH; |
| 395 | if (!this.documentDimensions_) |
| 396 | return; |
[email protected] | 499e956 | 2014-06-26 05:45:27 | [diff] [blame] | 397 | // When computing fit-to-width, the maximum width of a page in the |
| 398 | // document is used, which is equal to the size of the document width. |
[email protected] | fbad5bb | 2014-07-18 07:20:36 | [diff] [blame] | 399 | this.setZoomInternal_(this.computeFittingZoom_(this.documentDimensions_, |
| 400 | true)); |
[email protected] | 499e956 | 2014-06-26 05:45:27 | [diff] [blame] | 401 | var page = this.getMostVisiblePage(); |
[email protected] | 499e956 | 2014-06-26 05:45:27 | [diff] [blame] | 402 | this.updateViewport_(); |
| 403 | }.bind(this)); |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 404 | }, |
| 405 | |
| 406 | /** |
raymes | 161514f | 2015-02-17 05:09:51 | [diff] [blame^] | 407 | * @private |
| 408 | * Zoom the viewport so that a page consumes the entire viewport. |
| 409 | * @param {boolean} scrollToTopOfPage Set to true if the viewport should be |
| 410 | * scrolled to the top of the current page. Set to false if the viewport |
| 411 | * should remain at the current scroll position. |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 412 | */ |
raymes | 161514f | 2015-02-17 05:09:51 | [diff] [blame^] | 413 | fitToPageInternal_: function(scrollToTopOfPage) { |
[email protected] | 499e956 | 2014-06-26 05:45:27 | [diff] [blame] | 414 | this.mightZoom_(function() { |
| 415 | this.fittingType_ = Viewport.FittingType.FIT_TO_PAGE; |
| 416 | if (!this.documentDimensions_) |
| 417 | return; |
| 418 | var page = this.getMostVisiblePage(); |
sammc | 07f53aa | 2014-11-06 06:57:46 | [diff] [blame] | 419 | // Fit to the current page's height and the widest page's width. |
| 420 | var dimensions = { |
| 421 | width: this.documentDimensions_.width, |
| 422 | height: this.pageDimensions_[page].height, |
| 423 | }; |
| 424 | this.setZoomInternal_(this.computeFittingZoom_(dimensions, false)); |
raymes | 161514f | 2015-02-17 05:09:51 | [diff] [blame^] | 425 | if (scrollToTopOfPage) |
| 426 | this.window_.scrollTo(0, this.pageDimensions_[page].y * this.zoom_); |
[email protected] | 499e956 | 2014-06-26 05:45:27 | [diff] [blame] | 427 | this.updateViewport_(); |
| 428 | }.bind(this)); |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 429 | }, |
| 430 | |
| 431 | /** |
raymes | 161514f | 2015-02-17 05:09:51 | [diff] [blame^] | 432 | * Zoom the viewport so that a page consumes the entire viewport. Also scrolls |
| 433 | * the viewport to the top of the current page. |
| 434 | */ |
| 435 | fitToPage: function() { |
| 436 | this.fitToPageInternal_(true); |
| 437 | }, |
| 438 | |
| 439 | /** |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 440 | * Zoom out to the next predefined zoom level. |
| 441 | */ |
| 442 | zoomOut: function() { |
[email protected] | 499e956 | 2014-06-26 05:45:27 | [diff] [blame] | 443 | this.mightZoom_(function() { |
| 444 | this.fittingType_ = Viewport.FittingType.NONE; |
| 445 | var nextZoom = Viewport.ZOOM_FACTORS[0]; |
| 446 | for (var i = 0; i < Viewport.ZOOM_FACTORS.length; i++) { |
| 447 | if (Viewport.ZOOM_FACTORS[i] < this.zoom_) |
| 448 | nextZoom = Viewport.ZOOM_FACTORS[i]; |
| 449 | } |
[email protected] | fbad5bb | 2014-07-18 07:20:36 | [diff] [blame] | 450 | this.setZoomInternal_(nextZoom); |
[email protected] | 499e956 | 2014-06-26 05:45:27 | [diff] [blame] | 451 | this.updateViewport_(); |
| 452 | }.bind(this)); |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 453 | }, |
| 454 | |
| 455 | /** |
| 456 | * Zoom in to the next predefined zoom level. |
| 457 | */ |
| 458 | zoomIn: function() { |
[email protected] | 499e956 | 2014-06-26 05:45:27 | [diff] [blame] | 459 | this.mightZoom_(function() { |
| 460 | this.fittingType_ = Viewport.FittingType.NONE; |
| 461 | var nextZoom = Viewport.ZOOM_FACTORS[Viewport.ZOOM_FACTORS.length - 1]; |
| 462 | for (var i = Viewport.ZOOM_FACTORS.length - 1; i >= 0; i--) { |
| 463 | if (Viewport.ZOOM_FACTORS[i] > this.zoom_) |
| 464 | nextZoom = Viewport.ZOOM_FACTORS[i]; |
| 465 | } |
[email protected] | fbad5bb | 2014-07-18 07:20:36 | [diff] [blame] | 466 | this.setZoomInternal_(nextZoom); |
[email protected] | 499e956 | 2014-06-26 05:45:27 | [diff] [blame] | 467 | this.updateViewport_(); |
| 468 | }.bind(this)); |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 469 | }, |
| 470 | |
| 471 | /** |
| 472 | * Go to the given page index. |
[email protected] | f90b9a4 | 2014-08-20 05:37:34 | [diff] [blame] | 473 | * @param {number} page the index of the page to go to. zero-based. |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 474 | */ |
| 475 | goToPage: function(page) { |
[email protected] | 499e956 | 2014-06-26 05:45:27 | [diff] [blame] | 476 | this.mightZoom_(function() { |
alexandrec | 9754b9e4 | 2015-01-19 03:41:19 | [diff] [blame] | 477 | if (this.pageDimensions_.length === 0) |
[email protected] | 499e956 | 2014-06-26 05:45:27 | [diff] [blame] | 478 | return; |
| 479 | if (page < 0) |
| 480 | page = 0; |
| 481 | if (page >= this.pageDimensions_.length) |
| 482 | page = this.pageDimensions_.length - 1; |
| 483 | var dimensions = this.pageDimensions_[page]; |
| 484 | this.window_.scrollTo(dimensions.x * this.zoom_, |
| 485 | dimensions.y * this.zoom_); |
| 486 | this.updateViewport_(); |
| 487 | }.bind(this)); |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 488 | }, |
| 489 | |
| 490 | /** |
| 491 | * Set the dimensions of the document. |
| 492 | * @param {Object} documentDimensions the dimensions of the document |
| 493 | */ |
| 494 | setDocumentDimensions: function(documentDimensions) { |
[email protected] | 499e956 | 2014-06-26 05:45:27 | [diff] [blame] | 495 | this.mightZoom_(function() { |
| 496 | var initialDimensions = !this.documentDimensions_; |
| 497 | this.documentDimensions_ = documentDimensions; |
| 498 | this.pageDimensions_ = this.documentDimensions_.pageDimensions; |
| 499 | if (initialDimensions) { |
[email protected] | fbad5bb | 2014-07-18 07:20:36 | [diff] [blame] | 500 | this.setZoomInternal_(this.computeFittingZoom_(this.documentDimensions_, |
| 501 | true)); |
[email protected] | 499e956 | 2014-06-26 05:45:27 | [diff] [blame] | 502 | if (this.zoom_ > 1) |
[email protected] | fbad5bb | 2014-07-18 07:20:36 | [diff] [blame] | 503 | this.setZoomInternal_(1); |
[email protected] | 499e956 | 2014-06-26 05:45:27 | [diff] [blame] | 504 | this.window_.scrollTo(0, 0); |
| 505 | } |
| 506 | this.contentSizeChanged_(); |
| 507 | this.resize_(); |
| 508 | }.bind(this)); |
[email protected] | 312112c7 | 2014-04-14 01:45:43 | [diff] [blame] | 509 | }, |
| 510 | |
| 511 | /** |
| 512 | * Get the coordinates of the page contents (excluding the page shadow) |
| 513 | * relative to the screen. |
| 514 | * @param {number} page the index of the page to get the rect for. |
| 515 | * @return {Object} a rect representing the page in screen coordinates. |
| 516 | */ |
| 517 | getPageScreenRect: function(page) { |
[email protected] | 499e956 | 2014-06-26 05:45:27 | [diff] [blame] | 518 | if (!this.documentDimensions_) { |
| 519 | return { |
| 520 | x: 0, |
| 521 | y: 0, |
| 522 | width: 0, |
| 523 | height: 0 |
| 524 | }; |
| 525 | } |
[email protected] | 312112c7 | 2014-04-14 01:45:43 | [diff] [blame] | 526 | if (page >= this.pageDimensions_.length) |
| 527 | page = this.pageDimensions_.length - 1; |
| 528 | |
| 529 | var pageDimensions = this.pageDimensions_[page]; |
| 530 | |
| 531 | // Compute the page dimensions minus the shadows. |
| 532 | var insetDimensions = { |
| 533 | x: pageDimensions.x + Viewport.PAGE_SHADOW.left, |
| 534 | y: pageDimensions.y + Viewport.PAGE_SHADOW.top, |
| 535 | width: pageDimensions.width - Viewport.PAGE_SHADOW.left - |
| 536 | Viewport.PAGE_SHADOW.right, |
| 537 | height: pageDimensions.height - Viewport.PAGE_SHADOW.top - |
| 538 | Viewport.PAGE_SHADOW.bottom |
| 539 | }; |
| 540 | |
[email protected] | 345e7c6 | 2014-05-02 09:52:58 | [diff] [blame] | 541 | // Compute the x-coordinate of the page within the document. |
| 542 | // TODO(raymes): This should really be set when the PDF plugin passes the |
| 543 | // page coordinates, but it isn't yet. |
| 544 | var x = (this.documentDimensions_.width - pageDimensions.width) / 2 + |
| 545 | Viewport.PAGE_SHADOW.left; |
| 546 | // Compute the space on the left of the document if the document fits |
| 547 | // completely in the screen. |
| 548 | var spaceOnLeft = (this.size.width - |
| 549 | this.documentDimensions_.width * this.zoom_) / 2; |
| 550 | spaceOnLeft = Math.max(spaceOnLeft, 0); |
[email protected] | 312112c7 | 2014-04-14 01:45:43 | [diff] [blame] | 551 | |
| 552 | return { |
[email protected] | 345e7c6 | 2014-05-02 09:52:58 | [diff] [blame] | 553 | x: x * this.zoom_ + spaceOnLeft - this.window_.pageXOffset, |
| 554 | y: insetDimensions.y * this.zoom_ - this.window_.pageYOffset, |
[email protected] | 312112c7 | 2014-04-14 01:45:43 | [diff] [blame] | 555 | width: insetDimensions.width * this.zoom_, |
| 556 | height: insetDimensions.height * this.zoom_ |
| 557 | }; |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 558 | } |
| 559 | }; |