| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 cr.define('print_preview', function() { | 5 cr.define('print_preview', function() { |
| 6 'use strict'; | 6 'use strict'; |
| 7 | 7 |
| 8 /** | 8 /** |
| 9 * Interface to the Chromium print preview generator. | 9 * Interface to the Chromium print preview generator. |
| 10 * @param {!print_preview.DestinationStore} destinationStore Used to get the | 10 * @param {!print_preview.DestinationStore} destinationStore Used to get the |
| (...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 166 // Dispatched when the current print preview request fails. | 166 // Dispatched when the current print preview request fails. |
| 167 FAIL: 'print_preview.PreviewGenerator.FAIL' | 167 FAIL: 'print_preview.PreviewGenerator.FAIL' |
| 168 }; | 168 }; |
| 169 | 169 |
| 170 PreviewGenerator.prototype = { | 170 PreviewGenerator.prototype = { |
| 171 __proto__: cr.EventTarget.prototype, | 171 __proto__: cr.EventTarget.prototype, |
| 172 | 172 |
| 173 /** | 173 /** |
| 174 * Request that new preview be generated. A preview request will not be | 174 * Request that new preview be generated. A preview request will not be |
| 175 * generated if the print ticket has not changed sufficiently. | 175 * generated if the print ticket has not changed sufficiently. |
| 176 * @return {boolean} Whether a new preview was actually requested. | 176 * @return {{id: number, |
| 177 * request: Promise}} The preview request id, or -1 if no preview |
| 178 * was requested, and a promise that will resolve when the preview is |
| 179 * complete (null if no preview was actually requested). |
| 177 */ | 180 */ |
| 178 requestPreview: function() { | 181 requestPreview: function() { |
| 179 if (!this.printTicketStore_.isTicketValidForPreview() || | 182 if (!this.printTicketStore_.isTicketValidForPreview() || |
| 180 !this.printTicketStore_.isInitialized || | 183 !this.printTicketStore_.isInitialized || |
| 181 !this.destinationStore_.selectedDestination) { | 184 !this.destinationStore_.selectedDestination) { |
| 182 return false; | 185 return {id: -1, request: null}; |
| 183 } | 186 } |
| 184 var previewChanged = this.hasPreviewChanged_(); | 187 var previewChanged = this.hasPreviewChanged_(); |
| 185 if (!previewChanged && !this.hasPreviewPageRangeChanged_()) { | 188 if (!previewChanged && !this.hasPreviewPageRangeChanged_()) { |
| 186 // Changes to these ticket items might not trigger a new preview, but | 189 // Changes to these ticket items might not trigger a new preview, but |
| 187 // they still need to be recorded. | 190 // they still need to be recorded. |
| 188 this.marginsType_ = this.printTicketStore_.marginsType.getValue(); | 191 this.marginsType_ = this.printTicketStore_.marginsType.getValue(); |
| 189 return false; | 192 return {id: -1, request: null}; |
| 190 } | 193 } |
| 191 this.mediaSize_ = this.printTicketStore_.mediaSize.getValue(); | 194 this.mediaSize_ = this.printTicketStore_.mediaSize.getValue(); |
| 192 this.isLandscapeEnabled_ = this.printTicketStore_.landscape.getValue(); | 195 this.isLandscapeEnabled_ = this.printTicketStore_.landscape.getValue(); |
| 193 this.isHeaderFooterEnabled_ = | 196 this.isHeaderFooterEnabled_ = |
| 194 this.printTicketStore_.headerFooter.getValue(); | 197 this.printTicketStore_.headerFooter.getValue(); |
| 195 this.colorValue_ = this.printTicketStore_.color.getValue(); | 198 this.colorValue_ = this.printTicketStore_.color.getValue(); |
| 196 this.isFitToPageEnabled_ = this.printTicketStore_.fitToPage.getValue(); | 199 this.isFitToPageEnabled_ = this.printTicketStore_.fitToPage.getValue(); |
| 197 this.scalingValue_ = this.printTicketStore_.scaling.getValueAsNumber(); | 200 this.scalingValue_ = this.printTicketStore_.scaling.getValueAsNumber(); |
| 198 this.pageRanges_ = this.printTicketStore_.pageRange.getPageRanges(); | 201 this.pageRanges_ = this.printTicketStore_.pageRange.getPageRanges(); |
| 199 this.marginsType_ = this.printTicketStore_.marginsType.getValue(); | 202 this.marginsType_ = this.printTicketStore_.marginsType.getValue(); |
| 200 this.isCssBackgroundEnabled_ = | 203 this.isCssBackgroundEnabled_ = |
| 201 this.printTicketStore_.cssBackground.getValue(); | 204 this.printTicketStore_.cssBackground.getValue(); |
| 202 this.isSelectionOnlyEnabled_ = | 205 this.isSelectionOnlyEnabled_ = |
| 203 this.printTicketStore_.selectionOnly.getValue(); | 206 this.printTicketStore_.selectionOnly.getValue(); |
| 204 this.selectedDestination_ = this.destinationStore_.selectedDestination; | 207 this.selectedDestination_ = this.destinationStore_.selectedDestination; |
| 205 | 208 |
| 206 this.inFlightRequestId_++; | 209 this.inFlightRequestId_++; |
| 207 this.generateDraft_ = this.documentInfo_.isModifiable && previewChanged; | 210 this.generateDraft_ = this.documentInfo_.isModifiable && previewChanged; |
| 208 this.nativeLayer_.startGetPreview( | 211 return { |
| 209 this.destinationStore_.selectedDestination, this.printTicketStore_, | 212 id: this.inFlightRequestId_, |
| 210 this.documentInfo_, this.generateDraft_, this.inFlightRequestId_); | 213 request: this.nativeLayer_.getPreview( |
| 211 return true; | 214 this.destinationStore_.selectedDestination, this.printTicketStore_, |
| 215 this.documentInfo_, this.generateDraft_, this.inFlightRequestId_), |
| 216 }; |
| 212 }, | 217 }, |
| 213 | 218 |
| 214 /** Removes all event listeners that the preview generator has attached. */ | 219 /** Removes all event listeners that the preview generator has attached. */ |
| 215 removeEventListeners: function() { | 220 removeEventListeners: function() { |
| 216 this.tracker_.removeAll(); | 221 this.tracker_.removeAll(); |
| 217 }, | 222 }, |
| 218 | 223 |
| 219 /** | 224 /** |
| 220 * Adds event listeners to the relevant native layer events. | 225 * Adds event listeners to the relevant native layer events. |
| 221 * @private | 226 * @private |
| 222 */ | 227 */ |
| 223 addEventListeners_: function() { | 228 addEventListeners_: function() { |
| 224 var nativeLayerEventTarget = this.nativeLayer_.getEventTarget(); | 229 var nativeLayerEventTarget = this.nativeLayer_.getEventTarget(); |
| 225 this.tracker_.add( | 230 this.tracker_.add( |
| 226 nativeLayerEventTarget, | 231 nativeLayerEventTarget, |
| 227 print_preview.NativeLayer.EventType.PAGE_LAYOUT_READY, | 232 print_preview.NativeLayer.EventType.PAGE_LAYOUT_READY, |
| 228 this.onPageLayoutReady_.bind(this)); | 233 this.onPageLayoutReady_.bind(this)); |
| 229 this.tracker_.add( | 234 this.tracker_.add( |
| 230 nativeLayerEventTarget, | 235 nativeLayerEventTarget, |
| 231 print_preview.NativeLayer.EventType.PAGE_COUNT_READY, | 236 print_preview.NativeLayer.EventType.PAGE_COUNT_READY, |
| 232 this.onPageCountReady_.bind(this)); | 237 this.onPageCountReady_.bind(this)); |
| 233 this.tracker_.add( | 238 this.tracker_.add( |
| 234 nativeLayerEventTarget, | 239 nativeLayerEventTarget, |
| 235 print_preview.NativeLayer.EventType.PAGE_PREVIEW_READY, | 240 print_preview.NativeLayer.EventType.PAGE_PREVIEW_READY, |
| 236 this.onPagePreviewReady_.bind(this)); | 241 this.onPagePreviewReady_.bind(this)); |
| 237 this.tracker_.add( | |
| 238 nativeLayerEventTarget, | |
| 239 print_preview.NativeLayer.EventType.PREVIEW_GENERATION_DONE, | |
| 240 this.onPreviewGenerationDone_.bind(this)); | |
| 241 this.tracker_.add( | |
| 242 nativeLayerEventTarget, | |
| 243 print_preview.NativeLayer.EventType.PREVIEW_GENERATION_FAIL, | |
| 244 this.onPreviewGenerationFail_.bind(this)); | |
| 245 }, | 242 }, |
| 246 | 243 |
| 247 /** | 244 /** |
| 248 * Dispatches a PAGE_READY event to signal that a page preview is ready. | 245 * Dispatches a PAGE_READY event to signal that a page preview is ready. |
| 249 * @param {number} previewIndex Index of the page with respect to the pages | 246 * @param {number} previewIndex Index of the page with respect to the pages |
| 250 * shown in the preview. E.g an index of 0 is the first displayed page, | 247 * shown in the preview. E.g an index of 0 is the first displayed page, |
| 251 * but not necessarily the first original document page. | 248 * but not necessarily the first original document page. |
| 252 * @param {number} pageNumber Number of the page with respect to the | 249 * @param {number} pageNumber Number of the page with respect to the |
| 253 * document. A value of 3 means it's the third page of the original | 250 * document. A value of 3 means it's the third page of the original |
| 254 * document. | 251 * document. |
| (...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 388 this.dispatchPreviewStartEvent_(event.previewUid, event.pageIndex); | 385 this.dispatchPreviewStartEvent_(event.previewUid, event.pageIndex); |
| 389 } | 386 } |
| 390 this.dispatchPageReadyEvent_( | 387 this.dispatchPageReadyEvent_( |
| 391 previewIndex, pageNumber, event.previewUid); | 388 previewIndex, pageNumber, event.previewUid); |
| 392 } | 389 } |
| 393 }, | 390 }, |
| 394 | 391 |
| 395 /** | 392 /** |
| 396 * Called when the preview generation is complete. Dispatches a | 393 * Called when the preview generation is complete. Dispatches a |
| 397 * DOCUMENT_READY event. | 394 * DOCUMENT_READY event. |
| 398 * @param {Event} event Contains the preview UID and response ID. | 395 * @param {number} previewResponseId |
| 399 * @private | 396 * @param {number} previewUid |
| 400 */ | 397 */ |
| 401 onPreviewGenerationDone_: function(event) { | 398 onPreviewGenerationDone: function(previewResponseId, previewUid) { |
| 402 if (this.inFlightRequestId_ != event.previewResponseId) { | 399 if (this.inFlightRequestId_ != previewResponseId) { |
| 403 return; // Ignore old response. | 400 return; // Ignore old response. |
| 404 } | 401 } |
| 405 if (!this.generateDraft_) { | 402 if (!this.generateDraft_) { |
| 406 // Dispatch a PREVIEW_START event since not generating a draft PDF, | 403 // Dispatch a PREVIEW_START event since not generating a draft PDF, |
| 407 // which includes print preview for non-modifiable documents, does not | 404 // which includes print preview for non-modifiable documents, does not |
| 408 // trigger PAGE_READY events. | 405 // trigger PAGE_READY events. |
| 409 this.dispatchPreviewStartEvent_(event.previewUid, 0); | 406 this.dispatchPreviewStartEvent_(previewUid, 0); |
| 410 } | 407 } |
| 411 cr.dispatchSimpleEvent(this, PreviewGenerator.EventType.DOCUMENT_READY); | 408 cr.dispatchSimpleEvent(this, PreviewGenerator.EventType.DOCUMENT_READY); |
| 412 }, | 409 }, |
| 413 | 410 |
| 414 /** | 411 /** |
| 415 * Called when the preview generation fails. | 412 * Called when the preview generation fails. |
| 416 * @private | 413 * @private |
| 417 */ | 414 */ |
| 418 onPreviewGenerationFail_: function() { | 415 onPreviewGenerationFail_: function() { |
| 419 // NOTE: No request ID is returned from Chromium so its assumed its the | 416 // NOTE: No request ID is returned from Chromium so its assumed its the |
| 420 // current one. | 417 // current one. |
| 421 cr.dispatchSimpleEvent(this, PreviewGenerator.EventType.FAIL); | 418 cr.dispatchSimpleEvent(this, PreviewGenerator.EventType.FAIL); |
| 422 } | 419 } |
| 423 }; | 420 }; |
| 424 | 421 |
| 425 // Export | 422 // Export |
| 426 return {PreviewGenerator: PreviewGenerator}; | 423 return {PreviewGenerator: PreviewGenerator}; |
| 427 }); | 424 }); |
| OLD | NEW |