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 |
11 * currently selected destination. | 11 * currently selected destination. |
12 * @param {!print_preview.PrintTicketStore} printTicketStore Used to read the | 12 * @param {!print_preview.PrintTicketStore} printTicketStore Used to read the |
13 * state of the ticket and write document information. | 13 * state of the ticket and write document information. |
14 * @param {!print_preview.NativeLayer} nativeLayer Used to communicate to | 14 * @param {!print_preview.NativeLayer} nativeLayer Used to communicate to |
15 * Chromium's preview rendering system. | 15 * Chromium's preview rendering system. |
16 * @param {!print_preview.DocumentInfo} documentInfo Document data model. | 16 * @param {!print_preview.DocumentInfo} documentInfo Document data model. |
17 * @constructor | 17 * @constructor |
18 * @extends {cr.EventTarget} | 18 * @extends {cr.EventTarget} |
19 */ | 19 */ |
20 function PreviewGenerator( | 20 function PreviewGenerator( |
21 destinationStore, printTicketStore, nativeLayer, documentInfo) { | 21 destinationStore, printTicketStore, nativeLayer, documentInfo) { |
22 cr.EventTarget.call(this); | 22 cr.EventTarget.call(this); |
23 | 23 |
24 /** | 24 /** |
25 * Used to get the currently selected destination. | 25 * Used to get the currently selected destination. |
26 * @type {!print_preview.DestinationStore} | 26 * @type {!print_preview.DestinationStore} |
27 * @private | 27 * @private |
28 */ | 28 */ |
29 this.destinationStore_ = destinationStore; | 29 this.destinationStore_ = destinationStore; |
30 | 30 |
31 /** | 31 /** |
(...skipping 167 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
199 this.marginsType_ = this.printTicketStore_.marginsType.getValue(); | 199 this.marginsType_ = this.printTicketStore_.marginsType.getValue(); |
200 this.isCssBackgroundEnabled_ = | 200 this.isCssBackgroundEnabled_ = |
201 this.printTicketStore_.cssBackground.getValue(); | 201 this.printTicketStore_.cssBackground.getValue(); |
202 this.isSelectionOnlyEnabled_ = | 202 this.isSelectionOnlyEnabled_ = |
203 this.printTicketStore_.selectionOnly.getValue(); | 203 this.printTicketStore_.selectionOnly.getValue(); |
204 this.selectedDestination_ = this.destinationStore_.selectedDestination; | 204 this.selectedDestination_ = this.destinationStore_.selectedDestination; |
205 | 205 |
206 this.inFlightRequestId_++; | 206 this.inFlightRequestId_++; |
207 this.generateDraft_ = this.documentInfo_.isModifiable && previewChanged; | 207 this.generateDraft_ = this.documentInfo_.isModifiable && previewChanged; |
208 this.nativeLayer_.startGetPreview( | 208 this.nativeLayer_.startGetPreview( |
209 this.destinationStore_.selectedDestination, | 209 this.destinationStore_.selectedDestination, this.printTicketStore_, |
210 this.printTicketStore_, | 210 this.documentInfo_, this.generateDraft_, this.inFlightRequestId_); |
211 this.documentInfo_, | |
212 this.generateDraft_, | |
213 this.inFlightRequestId_); | |
214 return true; | 211 return true; |
215 }, | 212 }, |
216 | 213 |
217 /** Removes all event listeners that the preview generator has attached. */ | 214 /** Removes all event listeners that the preview generator has attached. */ |
218 removeEventListeners: function() { | 215 removeEventListeners: function() { |
219 this.tracker_.removeAll(); | 216 this.tracker_.removeAll(); |
220 }, | 217 }, |
221 | 218 |
222 /** | 219 /** |
223 * Adds event listeners to the relevant native layer events. | 220 * Adds event listeners to the relevant native layer events. |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
267 }, | 264 }, |
268 | 265 |
269 /** | 266 /** |
270 * Dispatches a PREVIEW_START event. Signals that the preview should be | 267 * Dispatches a PREVIEW_START event. Signals that the preview should be |
271 * reloaded. | 268 * reloaded. |
272 * @param {number} previewUid Unique identifier of the preview. | 269 * @param {number} previewUid Unique identifier of the preview. |
273 * @param {number} index Index of the first page of the preview. | 270 * @param {number} index Index of the first page of the preview. |
274 * @private | 271 * @private |
275 */ | 272 */ |
276 dispatchPreviewStartEvent_: function(previewUid, index) { | 273 dispatchPreviewStartEvent_: function(previewUid, index) { |
277 var previewStartEvent = new Event( | 274 var previewStartEvent = |
278 PreviewGenerator.EventType.PREVIEW_START); | 275 new Event(PreviewGenerator.EventType.PREVIEW_START); |
279 if (!this.documentInfo_.isModifiable) { | 276 if (!this.documentInfo_.isModifiable) { |
280 index = -1; | 277 index = -1; |
281 } | 278 } |
282 previewStartEvent.previewUrl = 'chrome://print/' + | 279 previewStartEvent.previewUrl = 'chrome://print/' + previewUid.toString() + |
283 previewUid.toString() + '/' + index + '/print.pdf'; | 280 '/' + index + '/print.pdf'; |
284 this.dispatchEvent(previewStartEvent); | 281 this.dispatchEvent(previewStartEvent); |
285 }, | 282 }, |
286 | 283 |
287 /** | 284 /** |
288 * @return {boolean} Whether the print ticket, excluding the page range, has | 285 * @return {boolean} Whether the print ticket, excluding the page range, has |
289 * changed sufficiently to determine whether a new preview request | 286 * changed sufficiently to determine whether a new preview request |
290 * should be issued. | 287 * should be issued. |
291 * @private | 288 * @private |
292 */ | 289 */ |
293 hasPreviewChanged_: function() { | 290 hasPreviewChanged_: function() { |
294 var ticketStore = this.printTicketStore_; | 291 var ticketStore = this.printTicketStore_; |
295 return this.inFlightRequestId_ == -1 || | 292 return this.inFlightRequestId_ == -1 || |
296 !ticketStore.mediaSize.isValueEqual(this.mediaSize_) || | 293 !ticketStore.mediaSize.isValueEqual(this.mediaSize_) || |
297 !ticketStore.landscape.isValueEqual(this.isLandscapeEnabled_) || | 294 !ticketStore.landscape.isValueEqual(this.isLandscapeEnabled_) || |
298 !ticketStore.headerFooter.isValueEqual(this.isHeaderFooterEnabled_) || | 295 !ticketStore.headerFooter.isValueEqual(this.isHeaderFooterEnabled_) || |
299 !ticketStore.color.isValueEqual(this.colorValue_) || | 296 !ticketStore.color.isValueEqual(this.colorValue_) || |
300 !ticketStore.scaling.isValueEqual(this.scalingValue_) || | 297 !ticketStore.scaling.isValueEqual(this.scalingValue_) || |
301 !ticketStore.fitToPage.isValueEqual(this.isFitToPageEnabled_) || | 298 !ticketStore.fitToPage.isValueEqual(this.isFitToPageEnabled_) || |
302 (!ticketStore.marginsType.isValueEqual(this.marginsType_) && | 299 (!ticketStore.marginsType.isValueEqual(this.marginsType_) && |
303 !ticketStore.marginsType.isValueEqual( | 300 !ticketStore.marginsType.isValueEqual( |
304 print_preview.ticket_items.MarginsTypeValue.CUSTOM)) || | 301 print_preview.ticket_items.MarginsTypeValue.CUSTOM)) || |
305 (ticketStore.marginsType.isValueEqual( | 302 (ticketStore.marginsType.isValueEqual( |
306 print_preview.ticket_items.MarginsTypeValue.CUSTOM) && | 303 print_preview.ticket_items.MarginsTypeValue.CUSTOM) && |
307 !ticketStore.customMargins.isValueEqual( | 304 !ticketStore.customMargins.isValueEqual( |
308 this.documentInfo_.margins)) || | 305 this.documentInfo_.margins)) || |
309 !ticketStore.cssBackground.isValueEqual( | 306 !ticketStore.cssBackground.isValueEqual( |
310 this.isCssBackgroundEnabled_) || | 307 this.isCssBackgroundEnabled_) || |
311 !ticketStore.selectionOnly.isValueEqual( | 308 !ticketStore.selectionOnly.isValueEqual( |
312 this.isSelectionOnlyEnabled_) || | 309 this.isSelectionOnlyEnabled_) || |
313 (this.selectedDestination_ != | 310 (this.selectedDestination_ != |
314 this.destinationStore_.selectedDestination); | 311 this.destinationStore_.selectedDestination); |
315 }, | 312 }, |
316 | 313 |
317 /** | 314 /** |
318 * @return {boolean} Whether the page range in the print ticket has changed. | 315 * @return {boolean} Whether the page range in the print ticket has changed. |
319 * @private | 316 * @private |
320 */ | 317 */ |
321 hasPreviewPageRangeChanged_: function() { | 318 hasPreviewPageRangeChanged_: function() { |
322 return this.pageRanges_ == null || | 319 return this.pageRanges_ == null || |
323 !areRangesEqual( | 320 !areRangesEqual( |
324 this.printTicketStore_.pageRange.getPageRanges(), | 321 this.printTicketStore_.pageRange.getPageRanges(), |
325 this.pageRanges_); | 322 this.pageRanges_); |
326 }, | 323 }, |
327 | 324 |
328 /** | 325 /** |
329 * Called when the page layout of the document is ready. Always occurs | 326 * Called when the page layout of the document is ready. Always occurs |
330 * as a result of a preview request. | 327 * as a result of a preview request. |
331 * @param {Event} event Contains layout info about the document. | 328 * @param {Event} event Contains layout info about the document. |
332 * @private | 329 * @private |
333 */ | 330 */ |
334 onPageLayoutReady_: function(event) { | 331 onPageLayoutReady_: function(event) { |
335 // NOTE: A request ID is not specified, so assuming its for the current | 332 // NOTE: A request ID is not specified, so assuming its for the current |
336 // in-flight request. | 333 // in-flight request. |
337 | 334 |
338 var origin = new print_preview.Coordinate2d( | 335 var origin = new print_preview.Coordinate2d( |
339 event.pageLayout.printableAreaX, | 336 event.pageLayout.printableAreaX, event.pageLayout.printableAreaY); |
340 event.pageLayout.printableAreaY); | |
341 var size = new print_preview.Size( | 337 var size = new print_preview.Size( |
342 event.pageLayout.printableAreaWidth, | 338 event.pageLayout.printableAreaWidth, |
343 event.pageLayout.printableAreaHeight); | 339 event.pageLayout.printableAreaHeight); |
344 | 340 |
345 var margins = new print_preview.Margins( | 341 var margins = new print_preview.Margins( |
346 Math.round(event.pageLayout.marginTop), | 342 Math.round(event.pageLayout.marginTop), |
347 Math.round(event.pageLayout.marginRight), | 343 Math.round(event.pageLayout.marginRight), |
348 Math.round(event.pageLayout.marginBottom), | 344 Math.round(event.pageLayout.marginBottom), |
349 Math.round(event.pageLayout.marginLeft)); | 345 Math.round(event.pageLayout.marginLeft)); |
350 | 346 |
351 var o = print_preview.ticket_items.CustomMarginsOrientation; | 347 var o = print_preview.ticket_items.CustomMarginsOrientation; |
352 var pageSize = new print_preview.Size( | 348 var pageSize = new print_preview.Size( |
353 event.pageLayout.contentWidth + | 349 event.pageLayout.contentWidth + margins.get(o.LEFT) + |
354 margins.get(o.LEFT) + margins.get(o.RIGHT), | 350 margins.get(o.RIGHT), |
355 event.pageLayout.contentHeight + | 351 event.pageLayout.contentHeight + margins.get(o.TOP) + |
356 margins.get(o.TOP) + margins.get(o.BOTTOM)); | 352 margins.get(o.BOTTOM)); |
357 | 353 |
358 this.documentInfo_.updatePageInfo( | 354 this.documentInfo_.updatePageInfo( |
359 new print_preview.PrintableArea(origin, size), | 355 new print_preview.PrintableArea(origin, size), pageSize, |
360 pageSize, | 356 event.hasCustomPageSizeStyle, margins); |
361 event.hasCustomPageSizeStyle, | |
362 margins); | |
363 }, | 357 }, |
364 | 358 |
365 /** | 359 /** |
366 * Called when the document page count is received from the native layer. | 360 * Called when the document page count is received from the native layer. |
367 * Always occurs as a result of a preview request. | 361 * Always occurs as a result of a preview request. |
368 * @param {Event} event Contains the document's page count. | 362 * @param {Event} event Contains the document's page count. |
369 * @private | 363 * @private |
370 */ | 364 */ |
371 onPageCountReady_: function(event) { | 365 onPageCountReady_: function(event) { |
372 if (this.inFlightRequestId_ != event.previewResponseId) { | 366 if (this.inFlightRequestId_ != event.previewResponseId) { |
373 return; // Ignore old response. | 367 return; // Ignore old response. |
374 } | 368 } |
375 this.documentInfo_.updatePageCount(event.pageCount); | 369 this.documentInfo_.updatePageCount(event.pageCount); |
376 this.pageRanges_ = this.printTicketStore_.pageRange.getPageRanges(); | 370 this.pageRanges_ = this.printTicketStore_.pageRange.getPageRanges(); |
377 }, | 371 }, |
378 | 372 |
379 /** | 373 /** |
380 * Called when a page's preview has been generated. Dispatches a | 374 * Called when a page's preview has been generated. Dispatches a |
381 * PAGE_READY event. | 375 * PAGE_READY event. |
382 * @param {Event} event Contains the page index and preview UID. | 376 * @param {Event} event Contains the page index and preview UID. |
383 * @private | 377 * @private |
384 */ | 378 */ |
385 onPagePreviewReady_: function(event) { | 379 onPagePreviewReady_: function(event) { |
386 if (this.inFlightRequestId_ != event.previewResponseId) { | 380 if (this.inFlightRequestId_ != event.previewResponseId) { |
387 return; // Ignore old response. | 381 return; // Ignore old response. |
388 } | 382 } |
389 var pageNumber = event.pageIndex + 1; | 383 var pageNumber = event.pageIndex + 1; |
390 var pageNumberSet = this.printTicketStore_.pageRange.getPageNumberSet(); | 384 var pageNumberSet = this.printTicketStore_.pageRange.getPageNumberSet(); |
391 if (pageNumberSet.hasPageNumber(pageNumber)) { | 385 if (pageNumberSet.hasPageNumber(pageNumber)) { |
392 var previewIndex = pageNumberSet.getPageNumberIndex(pageNumber); | 386 var previewIndex = pageNumberSet.getPageNumberIndex(pageNumber); |
393 if (previewIndex == 0) { | 387 if (previewIndex == 0) { |
394 this.dispatchPreviewStartEvent_(event.previewUid, event.pageIndex); | 388 this.dispatchPreviewStartEvent_(event.previewUid, event.pageIndex); |
395 } | 389 } |
396 this.dispatchPageReadyEvent_( | 390 this.dispatchPageReadyEvent_( |
397 previewIndex, pageNumber, event.previewUid); | 391 previewIndex, pageNumber, event.previewUid); |
398 } | 392 } |
399 }, | 393 }, |
400 | 394 |
401 /** | 395 /** |
402 * Called when the preview generation is complete. Dispatches a | 396 * Called when the preview generation is complete. Dispatches a |
403 * DOCUMENT_READY event. | 397 * DOCUMENT_READY event. |
404 * @param {Event} event Contains the preview UID and response ID. | 398 * @param {Event} event Contains the preview UID and response ID. |
405 * @private | 399 * @private |
406 */ | 400 */ |
407 onPreviewGenerationDone_: function(event) { | 401 onPreviewGenerationDone_: function(event) { |
408 if (this.inFlightRequestId_ != event.previewResponseId) { | 402 if (this.inFlightRequestId_ != event.previewResponseId) { |
409 return; // Ignore old response. | 403 return; // Ignore old response. |
410 } | 404 } |
411 if (!this.generateDraft_) { | 405 if (!this.generateDraft_) { |
412 // Dispatch a PREVIEW_START event since not generating a draft PDF, | 406 // Dispatch a PREVIEW_START event since not generating a draft PDF, |
413 // which includes print preview for non-modifiable documents, does not | 407 // which includes print preview for non-modifiable documents, does not |
414 // trigger PAGE_READY events. | 408 // trigger PAGE_READY events. |
415 this.dispatchPreviewStartEvent_(event.previewUid, 0); | 409 this.dispatchPreviewStartEvent_(event.previewUid, 0); |
416 } | 410 } |
417 cr.dispatchSimpleEvent(this, PreviewGenerator.EventType.DOCUMENT_READY); | 411 cr.dispatchSimpleEvent(this, PreviewGenerator.EventType.DOCUMENT_READY); |
418 }, | 412 }, |
419 | 413 |
420 /** | 414 /** |
421 * Called when the preview generation fails. | 415 * Called when the preview generation fails. |
422 * @private | 416 * @private |
423 */ | 417 */ |
424 onPreviewGenerationFail_: function() { | 418 onPreviewGenerationFail_: function() { |
425 // NOTE: No request ID is returned from Chromium so its assumed its the | 419 // NOTE: No request ID is returned from Chromium so its assumed its the |
426 // current one. | 420 // current one. |
427 cr.dispatchSimpleEvent(this, PreviewGenerator.EventType.FAIL); | 421 cr.dispatchSimpleEvent(this, PreviewGenerator.EventType.FAIL); |
428 } | 422 } |
429 }; | 423 }; |
430 | 424 |
431 // Export | 425 // Export |
432 return { | 426 return {PreviewGenerator: PreviewGenerator}; |
433 PreviewGenerator: PreviewGenerator | |
434 }; | |
435 }); | 427 }); |
OLD | NEW |