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 */ |
(...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
148 updateViewport_: function() { | 148 updateViewport_: function() { |
149 this.viewportChangedCallback_(); | 149 this.viewportChangedCallback_(); |
150 }, | 150 }, |
151 | 151 |
152 /** | 152 /** |
153 * @private | 153 * @private |
154 * Called when the viewport size changes. | 154 * Called when the viewport size changes. |
155 */ | 155 */ |
156 resize_: function() { | 156 resize_: function() { |
157 if (this.fittingType_ == Viewport.FittingType.FIT_TO_PAGE) | 157 if (this.fittingType_ == Viewport.FittingType.FIT_TO_PAGE) |
158 this.fitToPage(); | 158 this.fitToPageInternal_(false); |
159 else if (this.fittingType_ == Viewport.FittingType.FIT_TO_WIDTH) | 159 else if (this.fittingType_ == Viewport.FittingType.FIT_TO_WIDTH) |
160 this.fitToWidth(); | 160 this.fitToWidth(); |
161 else | 161 else |
162 this.updateViewport_(); | 162 this.updateViewport_(); |
163 }, | 163 }, |
164 | 164 |
165 /** | 165 /** |
166 * @type {Object} the scroll position of the viewport. | 166 * @type {Object} the scroll position of the viewport. |
167 */ | 167 */ |
168 get position() { | 168 get position() { |
(...skipping 218 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
387 }, | 387 }, |
388 | 388 |
389 /** | 389 /** |
390 * Zoom the viewport so that the page-width consumes the entire viewport. | 390 * Zoom the viewport so that the page-width consumes the entire viewport. |
391 */ | 391 */ |
392 fitToWidth: function() { | 392 fitToWidth: function() { |
393 this.mightZoom_(function() { | 393 this.mightZoom_(function() { |
394 this.fittingType_ = Viewport.FittingType.FIT_TO_WIDTH; | 394 this.fittingType_ = Viewport.FittingType.FIT_TO_WIDTH; |
395 if (!this.documentDimensions_) | 395 if (!this.documentDimensions_) |
396 return; | 396 return; |
397 // Track the last y-position to stay at the same position after zooming. | |
398 var oldY = this.window_.pageYOffset / this.zoom_; | |
399 // When computing fit-to-width, the maximum width of a page in the | 397 // When computing fit-to-width, the maximum width of a page in the |
400 // document is used, which is equal to the size of the document width. | 398 // document is used, which is equal to the size of the document width. |
401 this.setZoomInternal_(this.computeFittingZoom_(this.documentDimensions_, | 399 this.setZoomInternal_(this.computeFittingZoom_(this.documentDimensions_, |
402 true)); | 400 true)); |
403 var page = this.getMostVisiblePage(); | 401 var page = this.getMostVisiblePage(); |
404 this.window_.scrollTo(0, oldY * this.zoom_); | |
405 this.updateViewport_(); | 402 this.updateViewport_(); |
406 }.bind(this)); | 403 }.bind(this)); |
407 }, | 404 }, |
408 | 405 |
409 /** | 406 /** |
410 * Zoom the viewport so that a page consumes the entire viewport. Also scrolls | 407 * @private |
411 * to the top of the most visible page. | 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. |
412 */ | 412 */ |
413 fitToPage: function() { | 413 fitToPageInternal_: function(scrollToTopOfPage) { |
414 this.mightZoom_(function() { | 414 this.mightZoom_(function() { |
415 this.fittingType_ = Viewport.FittingType.FIT_TO_PAGE; | 415 this.fittingType_ = Viewport.FittingType.FIT_TO_PAGE; |
416 if (!this.documentDimensions_) | 416 if (!this.documentDimensions_) |
417 return; | 417 return; |
418 var page = this.getMostVisiblePage(); | 418 var page = this.getMostVisiblePage(); |
419 // Fit to the current page's height and the widest page's width. | 419 // Fit to the current page's height and the widest page's width. |
420 var dimensions = { | 420 var dimensions = { |
421 width: this.documentDimensions_.width, | 421 width: this.documentDimensions_.width, |
422 height: this.pageDimensions_[page].height, | 422 height: this.pageDimensions_[page].height, |
423 }; | 423 }; |
424 this.setZoomInternal_(this.computeFittingZoom_(dimensions, false)); | 424 this.setZoomInternal_(this.computeFittingZoom_(dimensions, false)); |
425 this.window_.scrollTo(0, this.pageDimensions_[page].y * this.zoom_); | 425 if (scrollToTopOfPage) |
| 426 this.window_.scrollTo(0, this.pageDimensions_[page].y * this.zoom_); |
426 this.updateViewport_(); | 427 this.updateViewport_(); |
427 }.bind(this)); | 428 }.bind(this)); |
428 }, | 429 }, |
429 | 430 |
430 /** | 431 /** |
| 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 /** |
431 * Zoom out to the next predefined zoom level. | 440 * Zoom out to the next predefined zoom level. |
432 */ | 441 */ |
433 zoomOut: function() { | 442 zoomOut: function() { |
434 this.mightZoom_(function() { | 443 this.mightZoom_(function() { |
435 this.fittingType_ = Viewport.FittingType.NONE; | 444 this.fittingType_ = Viewport.FittingType.NONE; |
436 var nextZoom = Viewport.ZOOM_FACTORS[0]; | 445 var nextZoom = Viewport.ZOOM_FACTORS[0]; |
437 for (var i = 0; i < Viewport.ZOOM_FACTORS.length; i++) { | 446 for (var i = 0; i < Viewport.ZOOM_FACTORS.length; i++) { |
438 if (Viewport.ZOOM_FACTORS[i] < this.zoom_) | 447 if (Viewport.ZOOM_FACTORS[i] < this.zoom_) |
439 nextZoom = Viewport.ZOOM_FACTORS[i]; | 448 nextZoom = Viewport.ZOOM_FACTORS[i]; |
440 } | 449 } |
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
541 spaceOnLeft = Math.max(spaceOnLeft, 0); | 550 spaceOnLeft = Math.max(spaceOnLeft, 0); |
542 | 551 |
543 return { | 552 return { |
544 x: x * this.zoom_ + spaceOnLeft - this.window_.pageXOffset, | 553 x: x * this.zoom_ + spaceOnLeft - this.window_.pageXOffset, |
545 y: insetDimensions.y * this.zoom_ - this.window_.pageYOffset, | 554 y: insetDimensions.y * this.zoom_ - this.window_.pageYOffset, |
546 width: insetDimensions.width * this.zoom_, | 555 width: insetDimensions.width * this.zoom_, |
547 height: insetDimensions.height * this.zoom_ | 556 height: insetDimensions.height * this.zoom_ |
548 }; | 557 }; |
549 } | 558 } |
550 }; | 559 }; |
OLD | NEW |