| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2012 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 'use 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 // @type {HTMLDivElement} A layer on top of |this.pdfPlugin_| used for | |
| 18 // displaying messages to the user. | |
| 19 this.overlayLayer = $('overlay-layer'); | |
| 20 // @type {HTMLDivElement} Contains text displayed to the user followed by | |
| 21 // three animated dots. | |
| 22 this.customMessageWithDots_ = $('custom-message-with-dots'); | |
| 23 // @type {HTMLDivElement} Contains text displayed to the user. | |
| 24 this.customMessage_ = $('custom-message'); | |
| 25 // @type {HTMLInputElement} Button associated with a displayed error | |
| 26 // message. | |
| 27 this.errorButton = $('error-button'); | |
| 28 // @type {HTMLDivElement} Contains three animated (dancing) dots. | |
| 29 this.dancingDotsText = $('dancing-dots-text'); | |
| 30 | |
| 31 // True if the pdf document is loaded in the preview area. | |
| 32 this.pdfLoaded_ = false; | |
| 33 | |
| 34 // Contains the zoom level just before a new preview is requested so the | |
| 35 // same zoom level can be restored. | |
| 36 this.zoomLevel_ = null; | |
| 37 // @type {{x: number, y: number}} Contains the page offset values just | |
| 38 // before a new preview is requested so that the scroll amount can be | |
| 39 // restored later. | |
| 40 this.pageOffset_ = null; | |
| 41 // @type {print_preview.Rect} A rectangle describing the postion of the | |
| 42 // most visible page normalized with respect to the total height and width | |
| 43 // of the plugin. | |
| 44 this.pageLocationNormalized = null; | |
| 45 | |
| 46 // @type {EventTracker} Used to keep track of certain event listeners. | |
| 47 this.eventTracker = new EventTracker(); | |
| 48 | |
| 49 this.addEventListeners_(); | |
| 50 } | |
| 51 | |
| 52 cr.addSingletonGetter(PreviewArea); | |
| 53 | |
| 54 PreviewArea.prototype = { | |
| 55 /** | |
| 56 * The width of the plugin area in pixels, excluding any visible scrollbars, | |
| 57 * @type {number} | |
| 58 */ | |
| 59 get width() { | |
| 60 return this.widthPercent * this.pdfPlugin_.offsetWidth; | |
| 61 }, | |
| 62 | |
| 63 /** | |
| 64 * The height of the plugin area in pixels, excluding any visible | |
| 65 * scrollbars. | |
| 66 * @type {number} | |
| 67 */ | |
| 68 get height() { | |
| 69 return this.heightPercent * this.pdfPlugin_.offsetHeight; | |
| 70 }, | |
| 71 | |
| 72 /** | |
| 73 * The width of the plugin area in percent, excluding any visible | |
| 74 * scrollbars. | |
| 75 * @type {number} | |
| 76 */ | |
| 77 get widthPercent() { | |
| 78 var width = this.pdfPlugin_.getWidth(); | |
| 79 var scrollbarWidth = this.pdfPlugin_.getVerticalScrollbarThickness(); | |
| 80 return (width - scrollbarWidth) / width; | |
| 81 }, | |
| 82 | |
| 83 /** | |
| 84 * The height of the plugin area in percent, excluding any visible | |
| 85 * scrollbars. | |
| 86 * @type {number} | |
| 87 */ | |
| 88 get heightPercent() { | |
| 89 var height = this.pdfPlugin_.getHeight(); | |
| 90 var scrollbarHeight = this.pdfPlugin_.getHorizontalScrollbarThickness(); | |
| 91 return (height - scrollbarHeight) / height; | |
| 92 }, | |
| 93 | |
| 94 get pdfPlugin() { | |
| 95 return this.pdfPlugin_; | |
| 96 }, | |
| 97 | |
| 98 get pdfLoaded() { | |
| 99 return this.pdfLoaded_; | |
| 100 }, | |
| 101 | |
| 102 set pdfLoaded(pdfLoaded) { | |
| 103 this.pdfLoaded_ = pdfLoaded; | |
| 104 }, | |
| 105 | |
| 106 /** | |
| 107 * Initializes the PDF plugin and places it on the page. | |
| 108 * @param {!string} srcURL The URL of the document to be loaded in the | |
| 109 * plugin. | |
| 110 * @private | |
| 111 */ | |
| 112 createPDFPlugin_: function(srcURL) { | |
| 113 this.pdfPlugin_ = document.createElement('embed'); | |
| 114 this.pdfPlugin_.setAttribute('id', 'pdf-viewer'); | |
| 115 this.pdfPlugin_.setAttribute( | |
| 116 'type', 'application/x-google-chrome-print-preview-pdf'); | |
| 117 this.pdfPlugin_.setAttribute('src', srcURL); | |
| 118 this.pdfPlugin_.setAttribute('aria-live', 'polite'); | |
| 119 this.pdfPlugin_.setAttribute('aria-atomic', 'true'); | |
| 120 $('mainview').appendChild(this.pdfPlugin_); | |
| 121 | |
| 122 this.pdfPlugin_.onload('onPDFLoad()'); | |
| 123 this.pdfPlugin_.onScroll('onPreviewPositionChanged()'); | |
| 124 this.pdfPlugin_.onPluginSizeChanged('onPreviewPositionChanged()'); | |
| 125 this.pdfPlugin_.removePrintButton(); | |
| 126 this.pdfPlugin_.grayscale(true); | |
| 127 }, | |
| 128 | |
| 129 /** | |
| 130 * Reloads the plugin with a new url. | |
| 131 * @param {string} srcURL The URL to load in the plugin. | |
| 132 * @private | |
| 133 */ | |
| 134 reloadPDFPlugin_: function(srcURL) { | |
| 135 // Need to call this before the reload(), where the plugin resets its | |
| 136 // internal page count. | |
| 137 this.pdfPlugin_.goToPage('0'); | |
| 138 this.pdfPlugin_.resetPrintPreviewUrl(srcURL); | |
| 139 this.pdfPlugin_.reload(); | |
| 140 this.pdfPlugin_.grayscale( | |
| 141 colorSettings.colorMode == print_preview.ColorSettings.GRAY); | |
| 142 }, | |
| 143 | |
| 144 /** | |
| 145 * Creates the PDF plugin or reloads the existing one. | |
| 146 * @param {number} srcDataIndex Preview data source index. | |
| 147 */ | |
| 148 createOrReloadPDFPlugin: function(srcDataIndex) { | |
| 149 var srcURL = getPageSrcURL(currentPreviewUid, srcDataIndex); | |
| 150 this.pdfPlugin_ ? this.reloadPDFPlugin_(srcURL) : | |
| 151 this.createPDFPlugin_(srcURL); | |
| 152 }, | |
| 153 | |
| 154 /** | |
| 155 * Queries the plugin for the location of the most visible page and updates | |
| 156 * |this.pageLocationNormalized|. | |
| 157 */ | |
| 158 update: function() { | |
| 159 if (!this.pdfLoaded_) | |
| 160 return; | |
| 161 var pluginLocation = | |
| 162 this.pdfPlugin_.getPageLocationNormalized().split(';'); | |
| 163 this.pageLocationNormalized = new print_preview.Rect( | |
| 164 parseFloat(pluginLocation[0]), | |
| 165 parseFloat(pluginLocation[1]), | |
| 166 parseFloat(pluginLocation[2]), | |
| 167 parseFloat(pluginLocation[3])); | |
| 168 }, | |
| 169 | |
| 170 /** | |
| 171 * Resets the state variables of |this|. | |
| 172 */ | |
| 173 resetState: function() { | |
| 174 if (this.pdfPlugin_ && previewModifiable) { | |
| 175 this.zoomLevel_ = this.pdfPlugin_.getZoomLevel(); | |
| 176 this.pageOffset_ = { | |
| 177 x: this.pdfPlugin_.pageXOffset(), | |
| 178 y: this.pdfPlugin_.pageYOffset() | |
| 179 }; | |
| 180 } | |
| 181 this.pdfLoaded_ = false; | |
| 182 }, | |
| 183 | |
| 184 /** | |
| 185 * Adds event listeners for various events. | |
| 186 * @private | |
| 187 */ | |
| 188 addEventListeners_: function() { | |
| 189 document.addEventListener(customEvents.PDF_LOADED, | |
| 190 this.onPDFLoaded_.bind(this)); | |
| 191 }, | |
| 192 | |
| 193 /** | |
| 194 * Listener executing when a |customEvents.PDF_LOADED| event occurs. | |
| 195 * @private | |
| 196 */ | |
| 197 onPDFLoaded_: function() { | |
| 198 this.pdfPlugin_ = $('pdf-viewer'); | |
| 199 this.pdfLoaded_ = true; | |
| 200 if (this.zoomLevel_ != null && this.pageOffset_ != null) { | |
| 201 this.pdfPlugin_.setZoomLevel(this.zoomLevel_); | |
| 202 this.pdfPlugin_.setPageXOffset(this.pageOffset_.x); | |
| 203 this.pdfPlugin_.setPageYOffset(this.pageOffset_.y); | |
| 204 } else { | |
| 205 this.pdfPlugin_.fitToHeight(); | |
| 206 } | |
| 207 }, | |
| 208 | |
| 209 /** | |
| 210 * Hides the |this.overlayLayer| and any messages currently displayed. | |
| 211 */ | |
| 212 hideOverlayLayer: function() { | |
| 213 this.eventTracker.add(this.overlayLayer, 'webkitTransitionEnd', | |
| 214 this.hideOverlayLayerCleanup_.bind(this), false); | |
| 215 if (this.pdfPlugin_) | |
| 216 this.pdfPlugin_.classList.remove('invisible'); | |
| 217 this.overlayLayer.classList.add('invisible'); | |
| 218 }, | |
| 219 | |
| 220 /** | |
| 221 * Displays the "Preview loading..." animation. | |
| 222 */ | |
| 223 showLoadingAnimation: function() { | |
| 224 this.showCustomMessage(localStrings.getString('loading')); | |
| 225 }, | |
| 226 | |
| 227 /** | |
| 228 * Necessary cleanup so that the dancing dots animation is not being | |
| 229 * rendered in the background when not displayed. | |
| 230 */ | |
| 231 hideOverlayLayerCleanup_: function() { | |
| 232 this.customMessageWithDots_.hidden = true; | |
| 233 this.eventTracker.remove(this.overlayLayer, 'webkitTransitionEnd'); | |
| 234 }, | |
| 235 | |
| 236 /** | |
| 237 * Displays |message| followed by three dancing dots animation. | |
| 238 * @param {string} message The message to be displayed. | |
| 239 */ | |
| 240 showCustomMessage: function(message) { | |
| 241 this.customMessageWithDots_.innerHTML = message + | |
| 242 this.dancingDotsText.innerHTML; | |
| 243 this.customMessageWithDots_.hidden = false; | |
| 244 if (this.pdfPlugin_) | |
| 245 this.pdfPlugin_.classList.add('invisible'); | |
| 246 this.overlayLayer.classList.remove('invisible'); | |
| 247 }, | |
| 248 | |
| 249 /** | |
| 250 * Clears the custom message with dots animation. | |
| 251 */ | |
| 252 clearCustomMessageWithDots: function() { | |
| 253 this.customMessageWithDots_.innerHTML = ''; | |
| 254 this.customMessageWithDots_.hidden = true; | |
| 255 }, | |
| 256 | |
| 257 /** | |
| 258 * Display an error message in the center of the preview area. | |
| 259 * @param {string} errorMessage The error message to be displayed. | |
| 260 */ | |
| 261 displayErrorMessageAndNotify: function(errorMessage) { | |
| 262 this.overlayLayer.classList.remove('invisible'); | |
| 263 this.customMessage_.textContent = errorMessage; | |
| 264 this.customMessage_.hidden = false; | |
| 265 this.customMessageWithDots_.innerHTML = ''; | |
| 266 this.customMessageWithDots_.hidden = true; | |
| 267 if (this.pdfPlugin_) { | |
| 268 $('mainview').removeChild(this.pdfPlugin_); | |
| 269 this.pdfPlugin_ = null; | |
| 270 } | |
| 271 cr.dispatchSimpleEvent(document, customEvents.PDF_GENERATION_ERROR); | |
| 272 }, | |
| 273 | |
| 274 /** | |
| 275 * Display an error message in the center of the preview area followed by a | |
| 276 * button. | |
| 277 * @param {string} errorMessage The error message to be displayed. | |
| 278 * @param {string} buttonText The text to be displayed within the button. | |
| 279 * @param {string} buttonListener The listener to be executed when the | |
| 280 * button is clicked. | |
| 281 */ | |
| 282 displayErrorMessageWithButtonAndNotify: function( | |
| 283 errorMessage, buttonText, buttonListener) { | |
| 284 this.errorButton.disabled = false; | |
| 285 this.errorButton.textContent = buttonText; | |
| 286 this.errorButton.onclick = buttonListener; | |
| 287 this.errorButton.hidden = false; | |
| 288 $('system-dialog-throbber').hidden = true; | |
| 289 $('native-print-dialog-throbber').hidden = true; | |
| 290 if (cr.isMac) | |
| 291 $('open-preview-app-throbber').hidden = true; | |
| 292 this.displayErrorMessageAndNotify(errorMessage); | |
| 293 } | |
| 294 }; | |
| 295 | |
| 296 return { | |
| 297 PreviewArea: PreviewArea | |
| 298 }; | |
| 299 }); | |
| OLD | NEW |