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 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
128 * @private | 128 * @private |
129 */ | 129 */ |
130 this.isCssBackgroundEnabled_ = false; | 130 this.isCssBackgroundEnabled_ = false; |
131 | 131 |
132 /** | 132 /** |
133 * Destination that was selected for the last preview. | 133 * Destination that was selected for the last preview. |
134 * @type {print_preview.Destination} | 134 * @type {print_preview.Destination} |
135 * @private | 135 * @private |
136 */ | 136 */ |
137 this.selectedDestination_ = null; | 137 this.selectedDestination_ = null; |
138 | |
139 /** | |
140 * Event tracker used to keep track of native layer events. | |
141 * @type {!EventTracker} | |
142 * @private | |
143 */ | |
144 this.tracker_ = new EventTracker(); | |
145 | |
146 this.addEventListeners_(); | |
147 } | 138 } |
148 | 139 |
149 /** | 140 /** |
150 * Event types dispatched by the preview generator. | 141 * Event types dispatched by the preview generator. |
151 * @enum {string} | 142 * @enum {string} |
152 */ | 143 */ |
153 PreviewGenerator.EventType = { | 144 PreviewGenerator.EventType = { |
154 // Dispatched when the document can be printed. | 145 // Dispatched when the document can be printed. |
155 DOCUMENT_READY: 'print_preview.PreviewGenerator.DOCUMENT_READY', | 146 DOCUMENT_READY: 'print_preview.PreviewGenerator.DOCUMENT_READY', |
156 | 147 |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
209 this.inFlightRequestId_++; | 200 this.inFlightRequestId_++; |
210 this.generateDraft_ = this.documentInfo_.isModifiable && previewChanged; | 201 this.generateDraft_ = this.documentInfo_.isModifiable && previewChanged; |
211 return { | 202 return { |
212 id: this.inFlightRequestId_, | 203 id: this.inFlightRequestId_, |
213 request: this.nativeLayer_.getPreview( | 204 request: this.nativeLayer_.getPreview( |
214 this.destinationStore_.selectedDestination, this.printTicketStore_, | 205 this.destinationStore_.selectedDestination, this.printTicketStore_, |
215 this.documentInfo_, this.generateDraft_, this.inFlightRequestId_), | 206 this.documentInfo_, this.generateDraft_, this.inFlightRequestId_), |
216 }; | 207 }; |
217 }, | 208 }, |
218 | 209 |
219 /** Removes all event listeners that the preview generator has attached. */ | |
220 removeEventListeners: function() { | |
221 this.tracker_.removeAll(); | |
222 }, | |
223 | |
224 /** | |
225 * Adds event listeners to the relevant native layer events. | |
226 * @private | |
227 */ | |
228 addEventListeners_: function() { | |
229 var nativeLayerEventTarget = this.nativeLayer_.getEventTarget(); | |
230 this.tracker_.add( | |
231 nativeLayerEventTarget, | |
232 print_preview.NativeLayer.EventType.PAGE_LAYOUT_READY, | |
233 this.onPageLayoutReady_.bind(this)); | |
234 this.tracker_.add( | |
235 nativeLayerEventTarget, | |
236 print_preview.NativeLayer.EventType.PAGE_COUNT_READY, | |
237 this.onPageCountReady_.bind(this)); | |
238 this.tracker_.add( | |
239 nativeLayerEventTarget, | |
240 print_preview.NativeLayer.EventType.PAGE_PREVIEW_READY, | |
241 this.onPagePreviewReady_.bind(this)); | |
242 }, | |
243 | |
244 /** | 210 /** |
245 * Dispatches a PAGE_READY event to signal that a page preview is ready. | 211 * Dispatches a PAGE_READY event to signal that a page preview is ready. |
246 * @param {number} previewIndex Index of the page with respect to the pages | 212 * @param {number} previewIndex Index of the page with respect to the pages |
247 * shown in the preview. E.g an index of 0 is the first displayed page, | 213 * shown in the preview. E.g an index of 0 is the first displayed page, |
248 * but not necessarily the first original document page. | 214 * but not necessarily the first original document page. |
249 * @param {number} pageNumber Number of the page with respect to the | 215 * @param {number} pageNumber Number of the page with respect to the |
250 * document. A value of 3 means it's the third page of the original | 216 * document. A value of 3 means it's the third page of the original |
251 * document. | 217 * document. |
252 * @param {number} previewUid Unique identifier of the preview. | 218 * @param {number} previewUid Unique identifier of the preview. |
253 * @private | 219 * @private |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
315 hasPreviewPageRangeChanged_: function() { | 281 hasPreviewPageRangeChanged_: function() { |
316 return this.pageRanges_ == null || | 282 return this.pageRanges_ == null || |
317 !areRangesEqual( | 283 !areRangesEqual( |
318 this.printTicketStore_.pageRange.getPageRanges(), | 284 this.printTicketStore_.pageRange.getPageRanges(), |
319 this.pageRanges_); | 285 this.pageRanges_); |
320 }, | 286 }, |
321 | 287 |
322 /** | 288 /** |
323 * Called when the page layout of the document is ready. Always occurs | 289 * Called when the page layout of the document is ready. Always occurs |
324 * as a result of a preview request. | 290 * as a result of a preview request. |
325 * @param {Event} event Contains layout info about the document. | 291 * @param {{marginTop: number, |
| 292 * marginLeft: number, |
| 293 * marginBottom: number, |
| 294 * marginRight: number, |
| 295 * contentWidth: number, |
| 296 * contentHeight: number, |
| 297 * printableAreaX: number, |
| 298 * printableAreaY: number, |
| 299 * printableAreaWidth: number, |
| 300 * printableAreaHeight: number, |
| 301 * }} pageLayout Layout information about the document. |
| 302 * @param {boolean} hasCustomPageSizeStyle Whether this document has a |
| 303 * custom page size or style to use. |
326 * @private | 304 * @private |
327 */ | 305 */ |
328 onPageLayoutReady_: function(event) { | 306 onPageLayoutReady_: function(pageLayout, hasCustomPageSizeStyle) { |
329 // NOTE: A request ID is not specified, so assuming its for the current | 307 // NOTE: A request ID is not specified, so assuming its for the current |
330 // in-flight request. | 308 // in-flight request. |
331 | 309 |
332 var origin = new print_preview.Coordinate2d( | 310 var origin = new print_preview.Coordinate2d( |
333 event.pageLayout.printableAreaX, event.pageLayout.printableAreaY); | 311 pageLayout.printableAreaX, pageLayout.printableAreaY); |
334 var size = new print_preview.Size( | 312 var size = new print_preview.Size( |
335 event.pageLayout.printableAreaWidth, | 313 pageLayout.printableAreaWidth, pageLayout.printableAreaHeight); |
336 event.pageLayout.printableAreaHeight); | |
337 | 314 |
338 var margins = new print_preview.Margins( | 315 var margins = new print_preview.Margins( |
339 Math.round(event.pageLayout.marginTop), | 316 Math.round(pageLayout.marginTop), Math.round(pageLayout.marginRight), |
340 Math.round(event.pageLayout.marginRight), | 317 Math.round(pageLayout.marginBottom), |
341 Math.round(event.pageLayout.marginBottom), | 318 Math.round(pageLayout.marginLeft)); |
342 Math.round(event.pageLayout.marginLeft)); | |
343 | 319 |
344 var o = print_preview.ticket_items.CustomMarginsOrientation; | 320 var o = print_preview.ticket_items.CustomMarginsOrientation; |
345 var pageSize = new print_preview.Size( | 321 var pageSize = new print_preview.Size( |
346 event.pageLayout.contentWidth + margins.get(o.LEFT) + | 322 pageLayout.contentWidth + margins.get(o.LEFT) + margins.get(o.RIGHT), |
347 margins.get(o.RIGHT), | 323 pageLayout.contentHeight + margins.get(o.TOP) + |
348 event.pageLayout.contentHeight + margins.get(o.TOP) + | |
349 margins.get(o.BOTTOM)); | 324 margins.get(o.BOTTOM)); |
350 | 325 |
351 this.documentInfo_.updatePageInfo( | 326 this.documentInfo_.updatePageInfo( |
352 new print_preview.PrintableArea(origin, size), pageSize, | 327 new print_preview.PrintableArea(origin, size), pageSize, |
353 event.hasCustomPageSizeStyle, margins); | 328 hasCustomPageSizeStyle, margins); |
354 }, | 329 }, |
355 | 330 |
356 /** | 331 /** |
357 * Called when the document page count is received from the native layer. | 332 * Called when the document page count is received from the native layer. |
358 * Always occurs as a result of a preview request. | 333 * Always occurs as a result of a preview request. |
359 * @param {Event} event Contains the document's page count. | 334 * @param {number} pageCount The document's page count. |
| 335 * @param {number} previewResponseId The request ID that corresponds to this |
| 336 * page count. |
| 337 * @param {number} fitToPageScaling The scaling required to fit the document |
| 338 * to page (unused). |
360 * @private | 339 * @private |
361 */ | 340 */ |
362 onPageCountReady_: function(event) { | 341 onPageCountReady_: function( |
363 if (this.inFlightRequestId_ != event.previewResponseId) { | 342 pageCount, previewResponseId, fitToPageScaling) { |
| 343 if (this.inFlightRequestId_ != previewResponseId) { |
364 return; // Ignore old response. | 344 return; // Ignore old response. |
365 } | 345 } |
366 this.documentInfo_.updatePageCount(event.pageCount); | 346 this.documentInfo_.updatePageCount(pageCount); |
367 this.pageRanges_ = this.printTicketStore_.pageRange.getPageRanges(); | 347 this.pageRanges_ = this.printTicketStore_.pageRange.getPageRanges(); |
368 }, | 348 }, |
369 | 349 |
370 /** | 350 /** |
371 * Called when a page's preview has been generated. Dispatches a | 351 * Called when a page's preview has been generated. Dispatches a |
372 * PAGE_READY event. | 352 * PAGE_READY event. |
373 * @param {Event} event Contains the page index and preview UID. | 353 * @param {number} pageIndex The index of the page whose preview is ready. |
| 354 * @param {number} previewUid The unique ID of the print preview UI. |
| 355 * @param {number} previewResponseId The preview request ID that this page |
| 356 * preview is a response to. |
374 * @private | 357 * @private |
375 */ | 358 */ |
376 onPagePreviewReady_: function(event) { | 359 onPagePreviewReady_: function(pageIndex, previewUid, previewResponseId) { |
377 if (this.inFlightRequestId_ != event.previewResponseId) { | 360 if (this.inFlightRequestId_ != previewResponseId) { |
378 return; // Ignore old response. | 361 return; // Ignore old response. |
379 } | 362 } |
380 var pageNumber = event.pageIndex + 1; | 363 var pageNumber = pageIndex + 1; |
381 var pageNumberSet = this.printTicketStore_.pageRange.getPageNumberSet(); | 364 var pageNumberSet = this.printTicketStore_.pageRange.getPageNumberSet(); |
382 if (pageNumberSet.hasPageNumber(pageNumber)) { | 365 if (pageNumberSet.hasPageNumber(pageNumber)) { |
383 var previewIndex = pageNumberSet.getPageNumberIndex(pageNumber); | 366 var previewIndex = pageNumberSet.getPageNumberIndex(pageNumber); |
384 if (previewIndex == 0) { | 367 if (previewIndex == 0) { |
385 this.dispatchPreviewStartEvent_(event.previewUid, event.pageIndex); | 368 this.dispatchPreviewStartEvent_(previewUid, pageIndex); |
386 } | 369 } |
387 this.dispatchPageReadyEvent_( | 370 this.dispatchPageReadyEvent_(previewIndex, pageNumber, previewUid); |
388 previewIndex, pageNumber, event.previewUid); | |
389 } | 371 } |
390 }, | 372 }, |
391 | 373 |
392 /** | 374 /** |
393 * Called when the preview generation is complete. Dispatches a | 375 * Called when the preview generation is complete. Dispatches a |
394 * DOCUMENT_READY event. | 376 * DOCUMENT_READY event. |
395 * @param {number} previewResponseId | 377 * @param {number} previewResponseId |
396 * @param {number} previewUid | 378 * @param {number} previewUid |
397 */ | 379 */ |
398 onPreviewGenerationDone: function(previewResponseId, previewUid) { | 380 onPreviewGenerationDone: function(previewResponseId, previewUid) { |
(...skipping 16 matching lines...) Expand all Loading... |
415 onPreviewGenerationFail_: function() { | 397 onPreviewGenerationFail_: function() { |
416 // NOTE: No request ID is returned from Chromium so its assumed its the | 398 // NOTE: No request ID is returned from Chromium so its assumed its the |
417 // current one. | 399 // current one. |
418 cr.dispatchSimpleEvent(this, PreviewGenerator.EventType.FAIL); | 400 cr.dispatchSimpleEvent(this, PreviewGenerator.EventType.FAIL); |
419 } | 401 } |
420 }; | 402 }; |
421 | 403 |
422 // Export | 404 // Export |
423 return {PreviewGenerator: PreviewGenerator}; | 405 return {PreviewGenerator: PreviewGenerator}; |
424 }); | 406 }); |
OLD | NEW |