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 * @private {boolean} |
| 65 */ |
| 66 this.generateDraft_ = false; |
| 67 |
| 68 /** |
61 * Media size to generate preview with. {@code null} indicates default size. | 69 * Media size to generate preview with. {@code null} indicates default size. |
62 * @type {cp.cdd.MediaSizeTicketItem} | 70 * @type {cp.cdd.MediaSizeTicketItem} |
63 * @private | 71 * @private |
64 */ | 72 */ |
65 this.mediaSize_ = null; | 73 this.mediaSize_ = null; |
66 | 74 |
67 /** | 75 /** |
68 * Whether the previews are being generated in landscape mode. | 76 * Whether the previews are being generated in landscape mode. |
69 * @type {boolean} | 77 * @type {boolean} |
70 * @private | 78 * @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 | 174 * Request that new preview be generated. A preview request will not be |
167 * generated if the print ticket has not changed sufficiently. | 175 * generated if the print ticket has not changed sufficiently. |
168 * @return {boolean} Whether a new preview was actually requested. | 176 * @return {boolean} Whether a new preview was actually requested. |
169 */ | 177 */ |
170 requestPreview: function() { | 178 requestPreview: function() { |
171 if (!this.printTicketStore_.isTicketValidForPreview() || | 179 if (!this.printTicketStore_.isTicketValidForPreview() || |
172 !this.printTicketStore_.isInitialized || | 180 !this.printTicketStore_.isInitialized || |
173 !this.destinationStore_.selectedDestination) { | 181 !this.destinationStore_.selectedDestination) { |
174 return false; | 182 return false; |
175 } | 183 } |
176 if (!this.hasPreviewChanged_()) { | 184 var previewChanged = this.hasPreviewChanged_(); |
| 185 if (!previewChanged && !this.hasPreviewPageRangeChanged_()) { |
177 // Changes to these ticket items might not trigger a new preview, but | 186 // Changes to these ticket items might not trigger a new preview, but |
178 // they still need to be recorded. | 187 // they still need to be recorded. |
179 this.marginsType_ = this.printTicketStore_.marginsType.getValue(); | 188 this.marginsType_ = this.printTicketStore_.marginsType.getValue(); |
180 return false; | 189 return false; |
181 } | 190 } |
182 this.mediaSize_ = this.printTicketStore_.mediaSize.getValue(); | 191 this.mediaSize_ = this.printTicketStore_.mediaSize.getValue(); |
183 this.isLandscapeEnabled_ = this.printTicketStore_.landscape.getValue(); | 192 this.isLandscapeEnabled_ = this.printTicketStore_.landscape.getValue(); |
184 this.isHeaderFooterEnabled_ = | 193 this.isHeaderFooterEnabled_ = |
185 this.printTicketStore_.headerFooter.getValue(); | 194 this.printTicketStore_.headerFooter.getValue(); |
186 this.colorValue_ = this.printTicketStore_.color.getValue(); | 195 this.colorValue_ = this.printTicketStore_.color.getValue(); |
187 this.isFitToPageEnabled_ = this.printTicketStore_.fitToPage.getValue(); | 196 this.isFitToPageEnabled_ = this.printTicketStore_.fitToPage.getValue(); |
188 this.scalingValue_ = this.printTicketStore_.scaling.getValueAsNumber(); | 197 this.scalingValue_ = this.printTicketStore_.scaling.getValueAsNumber(); |
189 this.pageRanges_ = this.printTicketStore_.pageRange.getPageRanges(); | 198 this.pageRanges_ = this.printTicketStore_.pageRange.getPageRanges(); |
190 this.marginsType_ = this.printTicketStore_.marginsType.getValue(); | 199 this.marginsType_ = this.printTicketStore_.marginsType.getValue(); |
191 this.isCssBackgroundEnabled_ = | 200 this.isCssBackgroundEnabled_ = |
192 this.printTicketStore_.cssBackground.getValue(); | 201 this.printTicketStore_.cssBackground.getValue(); |
193 this.isSelectionOnlyEnabled_ = | 202 this.isSelectionOnlyEnabled_ = |
194 this.printTicketStore_.selectionOnly.getValue(); | 203 this.printTicketStore_.selectionOnly.getValue(); |
195 this.selectedDestination_ = this.destinationStore_.selectedDestination; | 204 this.selectedDestination_ = this.destinationStore_.selectedDestination; |
196 | 205 |
197 this.inFlightRequestId_++; | 206 this.inFlightRequestId_++; |
| 207 this.generateDraft_ = this.documentInfo_.isModifiable && previewChanged; |
198 this.nativeLayer_.startGetPreview( | 208 this.nativeLayer_.startGetPreview( |
199 this.destinationStore_.selectedDestination, | 209 this.destinationStore_.selectedDestination, |
200 this.printTicketStore_, | 210 this.printTicketStore_, |
201 this.documentInfo_, | 211 this.documentInfo_, |
| 212 this.generateDraft_, |
202 this.inFlightRequestId_); | 213 this.inFlightRequestId_); |
203 return true; | 214 return true; |
204 }, | 215 }, |
205 | 216 |
206 /** Removes all event listeners that the preview generator has attached. */ | 217 /** Removes all event listeners that the preview generator has attached. */ |
207 removeEventListeners: function() { | 218 removeEventListeners: function() { |
208 this.tracker_.removeAll(); | 219 this.tracker_.removeAll(); |
209 }, | 220 }, |
210 | 221 |
211 /** | 222 /** |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
266 PreviewGenerator.EventType.PREVIEW_START); | 277 PreviewGenerator.EventType.PREVIEW_START); |
267 if (!this.documentInfo_.isModifiable) { | 278 if (!this.documentInfo_.isModifiable) { |
268 index = -1; | 279 index = -1; |
269 } | 280 } |
270 previewStartEvent.previewUrl = 'chrome://print/' + | 281 previewStartEvent.previewUrl = 'chrome://print/' + |
271 previewUid.toString() + '/' + index + '/print.pdf'; | 282 previewUid.toString() + '/' + index + '/print.pdf'; |
272 this.dispatchEvent(previewStartEvent); | 283 this.dispatchEvent(previewStartEvent); |
273 }, | 284 }, |
274 | 285 |
275 /** | 286 /** |
276 * @return {boolean} Whether the print ticket has changed sufficiently to | 287 * @return {boolean} Whether the print ticket, excluding the page range, has |
277 * determine whether a new preview request should be issued. | 288 * changed sufficiently to determine whether a new preview request |
| 289 * should be issued. |
278 * @private | 290 * @private |
279 */ | 291 */ |
280 hasPreviewChanged_: function() { | 292 hasPreviewChanged_: function() { |
281 var ticketStore = this.printTicketStore_; | 293 var ticketStore = this.printTicketStore_; |
282 return this.inFlightRequestId_ == -1 || | 294 return this.inFlightRequestId_ == -1 || |
283 !ticketStore.mediaSize.isValueEqual(this.mediaSize_) || | 295 !ticketStore.mediaSize.isValueEqual(this.mediaSize_) || |
284 !ticketStore.landscape.isValueEqual(this.isLandscapeEnabled_) || | 296 !ticketStore.landscape.isValueEqual(this.isLandscapeEnabled_) || |
285 !ticketStore.headerFooter.isValueEqual(this.isHeaderFooterEnabled_) || | 297 !ticketStore.headerFooter.isValueEqual(this.isHeaderFooterEnabled_) || |
286 !ticketStore.color.isValueEqual(this.colorValue_) || | 298 !ticketStore.color.isValueEqual(this.colorValue_) || |
287 !ticketStore.scaling.isValueEqual(this.scalingValue_) || | 299 !ticketStore.scaling.isValueEqual(this.scalingValue_) || |
288 !ticketStore.fitToPage.isValueEqual(this.isFitToPageEnabled_) || | 300 !ticketStore.fitToPage.isValueEqual(this.isFitToPageEnabled_) || |
289 this.pageRanges_ == null || | |
290 !areRangesEqual(ticketStore.pageRange.getPageRanges(), | |
291 this.pageRanges_) || | |
292 (!ticketStore.marginsType.isValueEqual(this.marginsType_) && | 301 (!ticketStore.marginsType.isValueEqual(this.marginsType_) && |
293 !ticketStore.marginsType.isValueEqual( | 302 !ticketStore.marginsType.isValueEqual( |
294 print_preview.ticket_items.MarginsTypeValue.CUSTOM)) || | 303 print_preview.ticket_items.MarginsTypeValue.CUSTOM)) || |
295 (ticketStore.marginsType.isValueEqual( | 304 (ticketStore.marginsType.isValueEqual( |
296 print_preview.ticket_items.MarginsTypeValue.CUSTOM) && | 305 print_preview.ticket_items.MarginsTypeValue.CUSTOM) && |
297 !ticketStore.customMargins.isValueEqual( | 306 !ticketStore.customMargins.isValueEqual( |
298 this.documentInfo_.margins)) || | 307 this.documentInfo_.margins)) || |
299 !ticketStore.cssBackground.isValueEqual( | 308 !ticketStore.cssBackground.isValueEqual( |
300 this.isCssBackgroundEnabled_) || | 309 this.isCssBackgroundEnabled_) || |
301 !ticketStore.selectionOnly.isValueEqual( | 310 !ticketStore.selectionOnly.isValueEqual( |
302 this.isSelectionOnlyEnabled_) || | 311 this.isSelectionOnlyEnabled_) || |
303 (this.selectedDestination_ != | 312 (this.selectedDestination_ != |
304 this.destinationStore_.selectedDestination); | 313 this.destinationStore_.selectedDestination); |
305 }, | 314 }, |
306 | 315 |
307 /** | 316 /** |
| 317 * @return {boolean} Whether the page range in the print ticket has changed. |
| 318 * @private |
| 319 */ |
| 320 hasPreviewPageRangeChanged_: function() { |
| 321 return this.pageRanges_ == null || |
| 322 !areRangesEqual( |
| 323 this.printTicketStore_.pageRange.getPageRanges(), |
| 324 this.pageRanges_); |
| 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 |