Chromium Code Reviews| 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 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 51 | 51 |
| 52 /** | 52 /** |
| 53 * ID of current in-flight request. Requests that do not share this ID will | 53 * ID of current in-flight request. Requests that do not share this ID will |
| 54 * be ignored. | 54 * be ignored. |
| 55 * @type {number} | 55 * @type {number} |
| 56 * @private | 56 * @private |
| 57 */ | 57 */ |
| 58 this.inFlightRequestId_ = -1; | 58 this.inFlightRequestId_ = -1; |
| 59 | 59 |
| 60 /** | 60 /** |
| 61 * Whether the current in flight request requires generating draft pages for | |
| 62 * print preview. This is true only for modifiable documents when the print | |
| 63 * settings has changed sufficiently to require re-rendering. | |
| 64 * @type {boolean} | |
|
dpapad
2017/04/22 01:32:49
Nit: @private {boolean}
Lei Zhang
2017/04/28 01:38:08
Done.
| |
| 65 * @private | |
| 66 */ | |
| 67 this.generateDraft_ = false; | |
| 68 | |
| 69 /** | |
| 61 * Media size to generate preview with. {@code null} indicates default size. | 70 * Media size to generate preview with. {@code null} indicates default size. |
| 62 * @type {cp.cdd.MediaSizeTicketItem} | 71 * @type {cp.cdd.MediaSizeTicketItem} |
| 63 * @private | 72 * @private |
| 64 */ | 73 */ |
| 65 this.mediaSize_ = null; | 74 this.mediaSize_ = null; |
| 66 | 75 |
| 67 /** | 76 /** |
| 68 * Whether the previews are being generated in landscape mode. | 77 * Whether the previews are being generated in landscape mode. |
| 69 * @type {boolean} | 78 * @type {boolean} |
| 70 * @private | 79 * @private |
| (...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 166 * Request that new preview be generated. A preview request will not be | 175 * Request that new preview be generated. A preview request will not be |
| 167 * generated if the print ticket has not changed sufficiently. | 176 * generated if the print ticket has not changed sufficiently. |
| 168 * @return {boolean} Whether a new preview was actually requested. | 177 * @return {boolean} Whether a new preview was actually requested. |
| 169 */ | 178 */ |
| 170 requestPreview: function() { | 179 requestPreview: function() { |
| 171 if (!this.printTicketStore_.isTicketValidForPreview() || | 180 if (!this.printTicketStore_.isTicketValidForPreview() || |
| 172 !this.printTicketStore_.isInitialized || | 181 !this.printTicketStore_.isInitialized || |
| 173 !this.destinationStore_.selectedDestination) { | 182 !this.destinationStore_.selectedDestination) { |
| 174 return false; | 183 return false; |
| 175 } | 184 } |
| 176 if (!this.hasPreviewChanged_()) { | 185 var previewChanged = this.hasPreviewChanged_(); |
| 186 if (!previewChanged && !this.hasPreviewPageRangeChanged_()) { | |
| 177 // Changes to these ticket items might not trigger a new preview, but | 187 // Changes to these ticket items might not trigger a new preview, but |
| 178 // they still need to be recorded. | 188 // they still need to be recorded. |
| 179 this.marginsType_ = this.printTicketStore_.marginsType.getValue(); | 189 this.marginsType_ = this.printTicketStore_.marginsType.getValue(); |
| 180 return false; | 190 return false; |
| 181 } | 191 } |
| 182 this.mediaSize_ = this.printTicketStore_.mediaSize.getValue(); | 192 this.mediaSize_ = this.printTicketStore_.mediaSize.getValue(); |
| 183 this.isLandscapeEnabled_ = this.printTicketStore_.landscape.getValue(); | 193 this.isLandscapeEnabled_ = this.printTicketStore_.landscape.getValue(); |
| 184 this.isHeaderFooterEnabled_ = | 194 this.isHeaderFooterEnabled_ = |
| 185 this.printTicketStore_.headerFooter.getValue(); | 195 this.printTicketStore_.headerFooter.getValue(); |
| 186 this.colorValue_ = this.printTicketStore_.color.getValue(); | 196 this.colorValue_ = this.printTicketStore_.color.getValue(); |
| 187 this.isFitToPageEnabled_ = this.printTicketStore_.fitToPage.getValue(); | 197 this.isFitToPageEnabled_ = this.printTicketStore_.fitToPage.getValue(); |
| 188 this.scalingValue_ = this.printTicketStore_.scaling.getValueAsNumber(); | 198 this.scalingValue_ = this.printTicketStore_.scaling.getValueAsNumber(); |
| 189 this.pageRanges_ = this.printTicketStore_.pageRange.getPageRanges(); | 199 this.pageRanges_ = this.printTicketStore_.pageRange.getPageRanges(); |
| 190 this.marginsType_ = this.printTicketStore_.marginsType.getValue(); | 200 this.marginsType_ = this.printTicketStore_.marginsType.getValue(); |
| 191 this.isCssBackgroundEnabled_ = | 201 this.isCssBackgroundEnabled_ = |
| 192 this.printTicketStore_.cssBackground.getValue(); | 202 this.printTicketStore_.cssBackground.getValue(); |
| 193 this.isSelectionOnlyEnabled_ = | 203 this.isSelectionOnlyEnabled_ = |
| 194 this.printTicketStore_.selectionOnly.getValue(); | 204 this.printTicketStore_.selectionOnly.getValue(); |
| 195 this.selectedDestination_ = this.destinationStore_.selectedDestination; | 205 this.selectedDestination_ = this.destinationStore_.selectedDestination; |
| 196 | 206 |
| 197 this.inFlightRequestId_++; | 207 this.inFlightRequestId_++; |
| 208 this.generateDraft_ = this.documentInfo_.isModifiable && previewChanged; | |
| 198 this.nativeLayer_.startGetPreview( | 209 this.nativeLayer_.startGetPreview( |
| 199 this.destinationStore_.selectedDestination, | 210 this.destinationStore_.selectedDestination, |
| 200 this.printTicketStore_, | 211 this.printTicketStore_, |
| 201 this.documentInfo_, | 212 this.documentInfo_, |
| 213 this.generateDraft_, | |
| 202 this.inFlightRequestId_); | 214 this.inFlightRequestId_); |
| 203 return true; | 215 return true; |
| 204 }, | 216 }, |
| 205 | 217 |
| 206 /** Removes all event listeners that the preview generator has attached. */ | 218 /** Removes all event listeners that the preview generator has attached. */ |
| 207 removeEventListeners: function() { | 219 removeEventListeners: function() { |
| 208 this.tracker_.removeAll(); | 220 this.tracker_.removeAll(); |
| 209 }, | 221 }, |
| 210 | 222 |
| 211 /** | 223 /** |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 266 PreviewGenerator.EventType.PREVIEW_START); | 278 PreviewGenerator.EventType.PREVIEW_START); |
| 267 if (!this.documentInfo_.isModifiable) { | 279 if (!this.documentInfo_.isModifiable) { |
| 268 index = -1; | 280 index = -1; |
| 269 } | 281 } |
| 270 previewStartEvent.previewUrl = 'chrome://print/' + | 282 previewStartEvent.previewUrl = 'chrome://print/' + |
| 271 previewUid.toString() + '/' + index + '/print.pdf'; | 283 previewUid.toString() + '/' + index + '/print.pdf'; |
| 272 this.dispatchEvent(previewStartEvent); | 284 this.dispatchEvent(previewStartEvent); |
| 273 }, | 285 }, |
| 274 | 286 |
| 275 /** | 287 /** |
| 276 * @return {boolean} Whether the print ticket has changed sufficiently to | 288 * @return {boolean} Whether the print ticket, excluding the page range, has |
| 277 * determine whether a new preview request should be issued. | 289 * changed sufficiently to determine whether a new preview request |
| 290 * should be issued. | |
| 278 * @private | 291 * @private |
| 279 */ | 292 */ |
| 280 hasPreviewChanged_: function() { | 293 hasPreviewChanged_: function() { |
| 281 var ticketStore = this.printTicketStore_; | 294 var ticketStore = this.printTicketStore_; |
| 282 return this.inFlightRequestId_ == -1 || | 295 return this.inFlightRequestId_ == -1 || |
| 283 !ticketStore.mediaSize.isValueEqual(this.mediaSize_) || | 296 !ticketStore.mediaSize.isValueEqual(this.mediaSize_) || |
| 284 !ticketStore.landscape.isValueEqual(this.isLandscapeEnabled_) || | 297 !ticketStore.landscape.isValueEqual(this.isLandscapeEnabled_) || |
| 285 !ticketStore.headerFooter.isValueEqual(this.isHeaderFooterEnabled_) || | 298 !ticketStore.headerFooter.isValueEqual(this.isHeaderFooterEnabled_) || |
| 286 !ticketStore.color.isValueEqual(this.colorValue_) || | 299 !ticketStore.color.isValueEqual(this.colorValue_) || |
| 287 !ticketStore.scaling.isValueEqual(this.scalingValue_) || | 300 !ticketStore.scaling.isValueEqual(this.scalingValue_) || |
| 288 !ticketStore.fitToPage.isValueEqual(this.isFitToPageEnabled_) || | 301 !ticketStore.fitToPage.isValueEqual(this.isFitToPageEnabled_) || |
| 289 this.pageRanges_ == null || | |
| 290 !areRangesEqual(ticketStore.pageRange.getPageRanges(), | |
| 291 this.pageRanges_) || | |
| 292 (!ticketStore.marginsType.isValueEqual(this.marginsType_) && | 302 (!ticketStore.marginsType.isValueEqual(this.marginsType_) && |
| 293 !ticketStore.marginsType.isValueEqual( | 303 !ticketStore.marginsType.isValueEqual( |
| 294 print_preview.ticket_items.MarginsType.Value.CUSTOM)) || | 304 print_preview.ticket_items.MarginsType.Value.CUSTOM)) || |
| 295 (ticketStore.marginsType.isValueEqual( | 305 (ticketStore.marginsType.isValueEqual( |
| 296 print_preview.ticket_items.MarginsType.Value.CUSTOM) && | 306 print_preview.ticket_items.MarginsType.Value.CUSTOM) && |
| 297 !ticketStore.customMargins.isValueEqual( | 307 !ticketStore.customMargins.isValueEqual( |
| 298 this.documentInfo_.margins)) || | 308 this.documentInfo_.margins)) || |
| 299 !ticketStore.cssBackground.isValueEqual( | 309 !ticketStore.cssBackground.isValueEqual( |
| 300 this.isCssBackgroundEnabled_) || | 310 this.isCssBackgroundEnabled_) || |
| 301 !ticketStore.selectionOnly.isValueEqual( | 311 !ticketStore.selectionOnly.isValueEqual( |
| 302 this.isSelectionOnlyEnabled_) || | 312 this.isSelectionOnlyEnabled_) || |
| 303 (this.selectedDestination_ != | 313 (this.selectedDestination_ != |
| 304 this.destinationStore_.selectedDestination); | 314 this.destinationStore_.selectedDestination); |
| 305 }, | 315 }, |
| 306 | 316 |
| 307 /** | 317 /** |
| 318 * @return {boolean} Whether the page range in the print ticket has changed. | |
| 319 * @private | |
| 320 */ | |
| 321 hasPreviewPageRangeChanged_: function() { | |
| 322 return this.pageRanges_ == null || | |
| 323 !areRangesEqual(this.printTicketStore_.pageRange.getPageRanges(), | |
| 324 this.pageRanges_); | |
|
dpapad
2017/04/22 01:32:49
Nit (optional): There used to be a so called "box
Lei Zhang
2017/04/28 01:38:08
Done.
| |
| 325 }, | |
| 326 | |
| 327 /** | |
| 308 * Called when the page layout of the document is ready. Always occurs | 328 * Called when the page layout of the document is ready. Always occurs |
| 309 * as a result of a preview request. | 329 * as a result of a preview request. |
| 310 * @param {Event} event Contains layout info about the document. | 330 * @param {Event} event Contains layout info about the document. |
| 311 * @private | 331 * @private |
| 312 */ | 332 */ |
| 313 onPageLayoutReady_: function(event) { | 333 onPageLayoutReady_: function(event) { |
| 314 // NOTE: A request ID is not specified, so assuming its for the current | 334 // NOTE: A request ID is not specified, so assuming its for the current |
| 315 // in-flight request. | 335 // in-flight request. |
| 316 | 336 |
| 317 var origin = new print_preview.Coordinate2d( | 337 var origin = new print_preview.Coordinate2d( |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 380 /** | 400 /** |
| 381 * Called when the preview generation is complete. Dispatches a | 401 * Called when the preview generation is complete. Dispatches a |
| 382 * DOCUMENT_READY event. | 402 * DOCUMENT_READY event. |
| 383 * @param {Event} event Contains the preview UID and response ID. | 403 * @param {Event} event Contains the preview UID and response ID. |
| 384 * @private | 404 * @private |
| 385 */ | 405 */ |
| 386 onPreviewGenerationDone_: function(event) { | 406 onPreviewGenerationDone_: function(event) { |
| 387 if (this.inFlightRequestId_ != event.previewResponseId) { | 407 if (this.inFlightRequestId_ != event.previewResponseId) { |
| 388 return; // Ignore old response. | 408 return; // Ignore old response. |
| 389 } | 409 } |
| 390 // Dispatch a PREVIEW_START event since non-modifiable documents don't | 410 if (!this.generateDraft_) { |
| 391 // trigger PAGE_READY events. | 411 // Dispatch a PREVIEW_START event since not generating a draft PDF, |
| 392 if (!this.documentInfo_.isModifiable) { | 412 // which includes print preview for non-modifiable documents, does not |
| 413 // trigger PAGE_READY events. | |
| 393 this.dispatchPreviewStartEvent_(event.previewUid, 0); | 414 this.dispatchPreviewStartEvent_(event.previewUid, 0); |
| 394 } | 415 } |
| 395 cr.dispatchSimpleEvent(this, PreviewGenerator.EventType.DOCUMENT_READY); | 416 cr.dispatchSimpleEvent(this, PreviewGenerator.EventType.DOCUMENT_READY); |
| 396 }, | 417 }, |
| 397 | 418 |
| 398 /** | 419 /** |
| 399 * Called when the preview generation fails. | 420 * Called when the preview generation fails. |
| 400 * @private | 421 * @private |
| 401 */ | 422 */ |
| 402 onPreviewGenerationFail_: function() { | 423 onPreviewGenerationFail_: function() { |
| 403 // NOTE: No request ID is returned from Chromium so its assumed its the | 424 // NOTE: No request ID is returned from Chromium so its assumed its the |
| 404 // current one. | 425 // current one. |
| 405 cr.dispatchSimpleEvent(this, PreviewGenerator.EventType.FAIL); | 426 cr.dispatchSimpleEvent(this, PreviewGenerator.EventType.FAIL); |
| 406 } | 427 } |
| 407 }; | 428 }; |
| 408 | 429 |
| 409 // Export | 430 // Export |
| 410 return { | 431 return { |
| 411 PreviewGenerator: PreviewGenerator | 432 PreviewGenerator: PreviewGenerator |
| 412 }; | 433 }; |
| 413 }); | 434 }); |
| OLD | NEW |