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 |
157 // Dispatched when a page preview is ready. The previewIndex field of the | 148 // Dispatched when a page preview is ready. The previewIndex field of the |
158 // event is the index of the page in the modified document, not the | 149 // event is the index of the page in the modified document, not the |
159 // original. So page 4 of the original document might be previewIndex = 0 of | 150 // original. So page 4 of the original document might be previewIndex = 0 of |
160 // the modified document. | 151 // the modified document. |
161 PAGE_READY: 'print_preview.PreviewGenerator.PAGE_READY', | 152 PAGE_READY: 'print_preview.PreviewGenerator.PAGE_READY', |
162 | 153 |
163 // Dispatched when the document preview starts to be generated. | 154 // Dispatched when the document preview starts to be generated. |
164 PREVIEW_START: 'print_preview.PreviewGenerator.PREVIEW_START', | 155 PREVIEW_START: 'print_preview.PreviewGenerator.PREVIEW_START', |
165 | 156 |
166 // Dispatched when the current print preview request fails. | 157 // Dispatched when the current print preview request fails. |
167 FAIL: 'print_preview.PreviewGenerator.FAIL' | 158 FAIL: 'print_preview.PreviewGenerator.FAIL' |
168 }; | 159 }; |
169 | 160 |
170 PreviewGenerator.prototype = { | 161 PreviewGenerator.prototype = { |
171 __proto__: cr.EventTarget.prototype, | 162 __proto__: cr.EventTarget.prototype, |
172 | 163 |
173 /** | 164 /** |
165 * Registers the preview generator for the WebUI events it responds to and | |
166 * adds the listeners to |listenerTracker| so that they can be | |
167 * automatically removed by this tracker later. | |
168 * @param {!WebUIListenerTracker} listenerTracker The tracker to add the | |
169 * listeners to. | |
170 */ | |
171 addWebUIEventListeners: function(listenerTracker) { | |
dpapad
2017/07/07 21:12:41
Not for this CL: Just wondering, would it make sen
rbpotter
2017/07/07 23:58:39
I think that would make sense. Will change in foll
rbpotter
2017/07/08 00:25:39
Follow up for this and other issues at https://cod
| |
172 listenerTracker.add( | |
173 'page-count-ready', this.onPageCountReady_.bind(this)); | |
174 listenerTracker.add( | |
175 'page-layout-ready', this.onPageLayoutReady_.bind(this)); | |
176 listenerTracker.add( | |
177 'page-preview-ready', this.onPagePreviewReady_.bind(this)); | |
178 }, | |
179 | |
180 /** | |
174 * Request that new preview be generated. A preview request will not be | 181 * Request that new preview be generated. A preview request will not be |
175 * generated if the print ticket has not changed sufficiently. | 182 * generated if the print ticket has not changed sufficiently. |
176 * @return {{id: number, | 183 * @return {{id: number, |
177 * request: Promise}} The preview request id, or -1 if no preview | 184 * request: Promise}} The preview request id, or -1 if no preview |
178 * was requested, and a promise that will resolve when the preview is | 185 * was requested, and a promise that will resolve when the preview is |
179 * complete (null if no preview was actually requested). | 186 * complete (null if no preview was actually requested). |
180 */ | 187 */ |
181 requestPreview: function() { | 188 requestPreview: function() { |
182 if (!this.printTicketStore_.isTicketValidForPreview() || | 189 if (!this.printTicketStore_.isTicketValidForPreview() || |
183 !this.printTicketStore_.isInitialized || | 190 !this.printTicketStore_.isInitialized || |
(...skipping 25 matching lines...) Expand all Loading... | |
209 this.inFlightRequestId_++; | 216 this.inFlightRequestId_++; |
210 this.generateDraft_ = this.documentInfo_.isModifiable && previewChanged; | 217 this.generateDraft_ = this.documentInfo_.isModifiable && previewChanged; |
211 return { | 218 return { |
212 id: this.inFlightRequestId_, | 219 id: this.inFlightRequestId_, |
213 request: this.nativeLayer_.getPreview( | 220 request: this.nativeLayer_.getPreview( |
214 this.destinationStore_.selectedDestination, this.printTicketStore_, | 221 this.destinationStore_.selectedDestination, this.printTicketStore_, |
215 this.documentInfo_, this.generateDraft_, this.inFlightRequestId_), | 222 this.documentInfo_, this.generateDraft_, this.inFlightRequestId_), |
216 }; | 223 }; |
217 }, | 224 }, |
218 | 225 |
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 /** | 226 /** |
245 * Dispatches a PAGE_READY event to signal that a page preview is ready. | 227 * 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 | 228 * @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, | 229 * shown in the preview. E.g an index of 0 is the first displayed page, |
248 * but not necessarily the first original document page. | 230 * but not necessarily the first original document page. |
249 * @param {number} pageNumber Number of the page with respect to the | 231 * @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 | 232 * document. A value of 3 means it's the third page of the original |
251 * document. | 233 * document. |
252 * @param {number} previewUid Unique identifier of the preview. | 234 * @param {number} previewUid Unique identifier of the preview. |
253 * @private | 235 * @private |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
315 hasPreviewPageRangeChanged_: function() { | 297 hasPreviewPageRangeChanged_: function() { |
316 return this.pageRanges_ == null || | 298 return this.pageRanges_ == null || |
317 !areRangesEqual( | 299 !areRangesEqual( |
318 this.printTicketStore_.pageRange.getPageRanges(), | 300 this.printTicketStore_.pageRange.getPageRanges(), |
319 this.pageRanges_); | 301 this.pageRanges_); |
320 }, | 302 }, |
321 | 303 |
322 /** | 304 /** |
323 * Called when the page layout of the document is ready. Always occurs | 305 * Called when the page layout of the document is ready. Always occurs |
324 * as a result of a preview request. | 306 * as a result of a preview request. |
325 * @param {Event} event Contains layout info about the document. | 307 * @param {{marginTop: number, |
308 * marginLeft: number, | |
309 * marginBottom: number, | |
310 * marginRight: number, | |
311 * contentWidth: number, | |
312 * contentHeight: number, | |
313 * printableAreaX: number, | |
314 * printableAreaY: number, | |
315 * printableAreaWidth: number, | |
316 * printableAreaHeight: number, | |
317 * }} pageLayout Layout information about the document. | |
318 * @param {boolean} hasCustomPageSizeStyle Whether this document has a | |
319 * custom page size or style to use. | |
326 * @private | 320 * @private |
327 */ | 321 */ |
328 onPageLayoutReady_: function(event) { | 322 onPageLayoutReady_: function(pageLayout, hasCustomPageSizeStyle) { |
329 // NOTE: A request ID is not specified, so assuming its for the current | 323 // NOTE: A request ID is not specified, so assuming its for the current |
330 // in-flight request. | 324 // in-flight request. |
331 | 325 |
332 var origin = new print_preview.Coordinate2d( | 326 var origin = new print_preview.Coordinate2d( |
333 event.pageLayout.printableAreaX, event.pageLayout.printableAreaY); | 327 pageLayout.printableAreaX, pageLayout.printableAreaY); |
334 var size = new print_preview.Size( | 328 var size = new print_preview.Size( |
335 event.pageLayout.printableAreaWidth, | 329 pageLayout.printableAreaWidth, pageLayout.printableAreaHeight); |
336 event.pageLayout.printableAreaHeight); | |
337 | 330 |
338 var margins = new print_preview.Margins( | 331 var margins = new print_preview.Margins( |
339 Math.round(event.pageLayout.marginTop), | 332 Math.round(pageLayout.marginTop), Math.round(pageLayout.marginRight), |
340 Math.round(event.pageLayout.marginRight), | 333 Math.round(pageLayout.marginBottom), |
341 Math.round(event.pageLayout.marginBottom), | 334 Math.round(pageLayout.marginLeft)); |
342 Math.round(event.pageLayout.marginLeft)); | |
343 | 335 |
344 var o = print_preview.ticket_items.CustomMarginsOrientation; | 336 var o = print_preview.ticket_items.CustomMarginsOrientation; |
345 var pageSize = new print_preview.Size( | 337 var pageSize = new print_preview.Size( |
346 event.pageLayout.contentWidth + margins.get(o.LEFT) + | 338 pageLayout.contentWidth + margins.get(o.LEFT) + margins.get(o.RIGHT), |
347 margins.get(o.RIGHT), | 339 pageLayout.contentHeight + margins.get(o.TOP) + |
348 event.pageLayout.contentHeight + margins.get(o.TOP) + | |
349 margins.get(o.BOTTOM)); | 340 margins.get(o.BOTTOM)); |
350 | 341 |
351 this.documentInfo_.updatePageInfo( | 342 this.documentInfo_.updatePageInfo( |
352 new print_preview.PrintableArea(origin, size), pageSize, | 343 new print_preview.PrintableArea(origin, size), pageSize, |
353 event.hasCustomPageSizeStyle, margins); | 344 hasCustomPageSizeStyle, margins); |
354 }, | 345 }, |
355 | 346 |
356 /** | 347 /** |
357 * Called when the document page count is received from the native layer. | 348 * Called when the document page count is received from the native layer. |
358 * Always occurs as a result of a preview request. | 349 * Always occurs as a result of a preview request. |
359 * @param {Event} event Contains the document's page count. | 350 * @param {number} pageCount The document's page count. |
351 * @param {number} previewResponseId The request ID that corresponds to this | |
352 * page count. | |
353 * @param {number} fitToPageScaling The scaling required to fit the document | |
354 * to page (unused). | |
360 * @private | 355 * @private |
361 */ | 356 */ |
362 onPageCountReady_: function(event) { | 357 onPageCountReady_: function( |
363 if (this.inFlightRequestId_ != event.previewResponseId) { | 358 pageCount, previewResponseId, fitToPageScaling) { |
359 if (this.inFlightRequestId_ != previewResponseId) { | |
364 return; // Ignore old response. | 360 return; // Ignore old response. |
365 } | 361 } |
366 this.documentInfo_.updatePageCount(event.pageCount); | 362 this.documentInfo_.updatePageCount(pageCount); |
367 this.pageRanges_ = this.printTicketStore_.pageRange.getPageRanges(); | 363 this.pageRanges_ = this.printTicketStore_.pageRange.getPageRanges(); |
368 }, | 364 }, |
369 | 365 |
370 /** | 366 /** |
371 * Called when a page's preview has been generated. Dispatches a | 367 * Called when a page's preview has been generated. Dispatches a |
372 * PAGE_READY event. | 368 * PAGE_READY event. |
373 * @param {Event} event Contains the page index and preview UID. | 369 * @param {number} pageIndex The index of the page whose preview is ready. |
370 * @param {number} previewUid The unique ID of the print preview UI. | |
371 * @param {number} previewResponseId The preview request ID that this page | |
372 * preview is a response to. | |
374 * @private | 373 * @private |
375 */ | 374 */ |
376 onPagePreviewReady_: function(event) { | 375 onPagePreviewReady_: function(pageIndex, previewUid, previewResponseId) { |
377 if (this.inFlightRequestId_ != event.previewResponseId) { | 376 if (this.inFlightRequestId_ != previewResponseId) { |
378 return; // Ignore old response. | 377 return; // Ignore old response. |
379 } | 378 } |
380 var pageNumber = event.pageIndex + 1; | 379 var pageNumber = pageIndex + 1; |
381 var pageNumberSet = this.printTicketStore_.pageRange.getPageNumberSet(); | 380 var pageNumberSet = this.printTicketStore_.pageRange.getPageNumberSet(); |
382 if (pageNumberSet.hasPageNumber(pageNumber)) { | 381 if (pageNumberSet.hasPageNumber(pageNumber)) { |
383 var previewIndex = pageNumberSet.getPageNumberIndex(pageNumber); | 382 var previewIndex = pageNumberSet.getPageNumberIndex(pageNumber); |
384 if (previewIndex == 0) { | 383 if (previewIndex == 0) { |
385 this.dispatchPreviewStartEvent_(event.previewUid, event.pageIndex); | 384 this.dispatchPreviewStartEvent_(previewUid, pageIndex); |
386 } | 385 } |
387 this.dispatchPageReadyEvent_( | 386 this.dispatchPageReadyEvent_(previewIndex, pageNumber, previewUid); |
388 previewIndex, pageNumber, event.previewUid); | |
389 } | 387 } |
390 }, | 388 }, |
391 | 389 |
392 /** | 390 /** |
393 * Called when the preview generation is complete. Dispatches a | 391 * Called when the preview generation is complete. Dispatches a |
394 * DOCUMENT_READY event. | 392 * DOCUMENT_READY event. |
395 * @param {number} previewResponseId | 393 * @param {number} previewResponseId |
396 * @param {number} previewUid | 394 * @param {number} previewUid |
397 */ | 395 */ |
398 onPreviewGenerationDone: function(previewResponseId, previewUid) { | 396 onPreviewGenerationDone: function(previewResponseId, previewUid) { |
(...skipping 16 matching lines...) Expand all Loading... | |
415 onPreviewGenerationFail_: function() { | 413 onPreviewGenerationFail_: function() { |
416 // NOTE: No request ID is returned from Chromium so its assumed its the | 414 // NOTE: No request ID is returned from Chromium so its assumed its the |
417 // current one. | 415 // current one. |
418 cr.dispatchSimpleEvent(this, PreviewGenerator.EventType.FAIL); | 416 cr.dispatchSimpleEvent(this, PreviewGenerator.EventType.FAIL); |
419 } | 417 } |
420 }; | 418 }; |
421 | 419 |
422 // Export | 420 // Export |
423 return {PreviewGenerator: PreviewGenerator}; | 421 return {PreviewGenerator: PreviewGenerator}; |
424 }); | 422 }); |
OLD | NEW |