OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2011 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 cr.define('print_preview', function() { |
| 6 'strict'; |
| 7 |
| 8 /** |
| 9 * Creates a PreviewArea object. It represents the area where the preview |
| 10 * document is displayed. |
| 11 * @constructor |
| 12 */ |
| 13 function PreviewArea() { |
| 14 // The embedded pdf plugin object. |
| 15 this.pdfPlugin_ = null; |
| 16 |
| 17 // True if the pdf document is loaded in the preview area. |
| 18 this.pdfLoaded_ = false; |
| 19 |
| 20 // Contains the zoom level just before a new preview is requested so the |
| 21 // same zoom level can be restored. |
| 22 this.zoomLevel_ = null; |
| 23 // @type {{x: number, y: number}} Contains the page offset values just |
| 24 // before a new preview is requested so that the scroll amount can be |
| 25 // restored later. |
| 26 this.pageOffset_ = null; |
| 27 } |
| 28 |
| 29 cr.addSingletonGetter(PreviewArea); |
| 30 |
| 31 PreviewArea.prototype = { |
| 32 /** |
| 33 * The width of the plugin area in pixels, excluding any visible scrollbars, |
| 34 * @type {number} |
| 35 */ |
| 36 get width() { |
| 37 return this.widthPercent * this.pdfPlugin_.offsetWidth; |
| 38 }, |
| 39 |
| 40 /** |
| 41 * The height of the plugin area in pixels, excluding any visible |
| 42 * scrollbars. |
| 43 * @type {number} |
| 44 */ |
| 45 get height() { |
| 46 return this.heightPercent * this.pdfPlugin_.offsetHeight; |
| 47 }, |
| 48 |
| 49 /** |
| 50 * The width of the plugin area in percent, excluding any visible |
| 51 * scrollbars. |
| 52 * @type {number} |
| 53 */ |
| 54 get widthPercent() { |
| 55 var width = this.pdfPlugin_.getWidth(); |
| 56 var scrollbarWidth = this.pdfPlugin_.getVerticalScrollbarThickness(); |
| 57 return (width - scrollbarWidth) / width; |
| 58 }, |
| 59 |
| 60 /** |
| 61 * The height of the plugin area in percent, excluding any visible |
| 62 * scrollbars. |
| 63 * @type {number} |
| 64 */ |
| 65 get heightPercent() { |
| 66 var height = this.pdfPlugin_.getHeight(); |
| 67 var scrollbarHeight = this.pdfPlugin_.getHorizontalScrollbarThickness(); |
| 68 return (height - scrollbarHeight) / height; |
| 69 }, |
| 70 |
| 71 get pdfLoaded() { |
| 72 return this.pdfLoaded_; |
| 73 }, |
| 74 |
| 75 set pdfLoaded(pdfLoaded) { |
| 76 this.pdfLoaded_ = pdfLoaded; |
| 77 }, |
| 78 |
| 79 /** |
| 80 * @return {print_preview.Rect} A rectangle describing the postion of the |
| 81 * most visible page normalized with respect to the total height and width |
| 82 * of the plugin. |
| 83 */ |
| 84 getPageLocationNormalized: function() { |
| 85 var pluginLocation = |
| 86 this.pdfPlugin_.getPageLocationNormalized().split(';'); |
| 87 return new print_preview.Rect(parseFloat(pluginLocation[0]), |
| 88 parseFloat(pluginLocation[1]), |
| 89 parseFloat(pluginLocation[2]), |
| 90 parseFloat(pluginLocation[3])); |
| 91 }, |
| 92 |
| 93 /** |
| 94 * Resets the state variables of |this|. |
| 95 */ |
| 96 resetState: function() { |
| 97 if (this.pdfPlugin_) { |
| 98 this.zoomLevel_ = this.pdfPlugin_.getZoomLevel(); |
| 99 this.pageOffset_ = { |
| 100 x: this.pdfPlugin_.pageXOffset(), |
| 101 y: this.pdfPlugin_.pageYOffset() |
| 102 }; |
| 103 } |
| 104 this.pdfLoaded_ = false; |
| 105 }, |
| 106 |
| 107 /** |
| 108 * Adds event listeners for various events. |
| 109 * @private |
| 110 */ |
| 111 addEventListeners: function() { |
| 112 document.addEventListener('PDFLoaded', |
| 113 this.onPDFLoaded_.bind(this)); |
| 114 }, |
| 115 |
| 116 /** |
| 117 * Listener executing when a PDFLoaded event occurs. |
| 118 * @private |
| 119 */ |
| 120 onPDFLoaded_: function() { |
| 121 this.pdfPlugin_ = $('pdf-viewer'); |
| 122 this.pdfLoaded_ = true; |
| 123 if (this.zoomLevel_ != null && this.pageOffset_ != null) { |
| 124 this.pdfPlugin_.setZoomLevel(this.zoomLevel_); |
| 125 this.pdfPlugin_.setPageXOffset(this.pageOffset_.x); |
| 126 this.pdfPlugin_.setPageYOffset(this.pageOffset_.y); |
| 127 } else { |
| 128 this.pdfPlugin_.fitToHeight(); |
| 129 } |
| 130 } |
| 131 }; |
| 132 |
| 133 return { |
| 134 PreviewArea: PreviewArea, |
| 135 }; |
| 136 }); |
OLD | NEW |