Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(194)

Side by Side Diff: chrome/browser/resources/print_preview/preview_generator.js

Issue 2969383003: Print Preview: Finish removing global Javascript functions. (Closed)
Patch Set: Fix test Created 3 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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 * Starts listening for relevant WebUI events and adds the listeners to
166 * |listenerTracker|. |listenerTracker| is responsible for removing the
167 * listeners when necessary.
168 * @param {!WebUIListenerTracker} listenerTracker
169 */
170 addWebUIEventListeners: function(listenerTracker) {
171 listenerTracker.add(
172 'page-count-ready', this.onPageCountReady_.bind(this));
173 listenerTracker.add(
174 'page-layout-ready', this.onPageLayoutReady_.bind(this));
175 listenerTracker.add(
176 'page-preview-ready', this.onPagePreviewReady_.bind(this));
177 },
178
179 /**
174 * Request that new preview be generated. A preview request will not be 180 * Request that new preview be generated. A preview request will not be
175 * generated if the print ticket has not changed sufficiently. 181 * generated if the print ticket has not changed sufficiently.
176 * @return {{id: number, 182 * @return {{id: number,
177 * request: Promise}} The preview request id, or -1 if no preview 183 * request: Promise}} The preview request id, or -1 if no preview
178 * was requested, and a promise that will resolve when the preview is 184 * was requested, and a promise that will resolve when the preview is
179 * complete (null if no preview was actually requested). 185 * complete (null if no preview was actually requested).
180 */ 186 */
181 requestPreview: function() { 187 requestPreview: function() {
182 if (!this.printTicketStore_.isTicketValidForPreview() || 188 if (!this.printTicketStore_.isTicketValidForPreview() ||
183 !this.printTicketStore_.isInitialized || 189 !this.printTicketStore_.isInitialized ||
(...skipping 25 matching lines...) Expand all
209 this.inFlightRequestId_++; 215 this.inFlightRequestId_++;
210 this.generateDraft_ = this.documentInfo_.isModifiable && previewChanged; 216 this.generateDraft_ = this.documentInfo_.isModifiable && previewChanged;
211 return { 217 return {
212 id: this.inFlightRequestId_, 218 id: this.inFlightRequestId_,
213 request: this.nativeLayer_.getPreview( 219 request: this.nativeLayer_.getPreview(
214 this.destinationStore_.selectedDestination, this.printTicketStore_, 220 this.destinationStore_.selectedDestination, this.printTicketStore_,
215 this.documentInfo_, this.generateDraft_, this.inFlightRequestId_), 221 this.documentInfo_, this.generateDraft_, this.inFlightRequestId_),
216 }; 222 };
217 }, 223 },
218 224
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 /** 225 /**
245 * Dispatches a PAGE_READY event to signal that a page preview is ready. 226 * 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 227 * @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, 228 * shown in the preview. E.g an index of 0 is the first displayed page,
248 * but not necessarily the first original document page. 229 * but not necessarily the first original document page.
249 * @param {number} pageNumber Number of the page with respect to the 230 * @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 231 * document. A value of 3 means it's the third page of the original
251 * document. 232 * document.
252 * @param {number} previewUid Unique identifier of the preview. 233 * @param {number} previewUid Unique identifier of the preview.
253 * @private 234 * @private
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after
315 hasPreviewPageRangeChanged_: function() { 296 hasPreviewPageRangeChanged_: function() {
316 return this.pageRanges_ == null || 297 return this.pageRanges_ == null ||
317 !areRangesEqual( 298 !areRangesEqual(
318 this.printTicketStore_.pageRange.getPageRanges(), 299 this.printTicketStore_.pageRange.getPageRanges(),
319 this.pageRanges_); 300 this.pageRanges_);
320 }, 301 },
321 302
322 /** 303 /**
323 * Called when the page layout of the document is ready. Always occurs 304 * Called when the page layout of the document is ready. Always occurs
324 * as a result of a preview request. 305 * as a result of a preview request.
325 * @param {Event} event Contains layout info about the document. 306 * @param {{marginTop: number,
307 * marginLeft: number,
308 * marginBottom: number,
309 * marginRight: number,
310 * contentWidth: number,
311 * contentHeight: number,
312 * printableAreaX: number,
313 * printableAreaY: number,
314 * printableAreaWidth: number,
315 * printableAreaHeight: number,
316 * }} pageLayout Layout information about the document.
317 * @param {boolean} hasCustomPageSizeStyle Whether this document has a
318 * custom page size or style to use.
326 * @private 319 * @private
327 */ 320 */
328 onPageLayoutReady_: function(event) { 321 onPageLayoutReady_: function(pageLayout, hasCustomPageSizeStyle) {
329 // NOTE: A request ID is not specified, so assuming its for the current 322 // NOTE: A request ID is not specified, so assuming its for the current
330 // in-flight request. 323 // in-flight request.
331 324
332 var origin = new print_preview.Coordinate2d( 325 var origin = new print_preview.Coordinate2d(
333 event.pageLayout.printableAreaX, event.pageLayout.printableAreaY); 326 pageLayout.printableAreaX, pageLayout.printableAreaY);
334 var size = new print_preview.Size( 327 var size = new print_preview.Size(
335 event.pageLayout.printableAreaWidth, 328 pageLayout.printableAreaWidth, pageLayout.printableAreaHeight);
336 event.pageLayout.printableAreaHeight);
337 329
338 var margins = new print_preview.Margins( 330 var margins = new print_preview.Margins(
339 Math.round(event.pageLayout.marginTop), 331 Math.round(pageLayout.marginTop), Math.round(pageLayout.marginRight),
340 Math.round(event.pageLayout.marginRight), 332 Math.round(pageLayout.marginBottom),
341 Math.round(event.pageLayout.marginBottom), 333 Math.round(pageLayout.marginLeft));
342 Math.round(event.pageLayout.marginLeft));
343 334
344 var o = print_preview.ticket_items.CustomMarginsOrientation; 335 var o = print_preview.ticket_items.CustomMarginsOrientation;
345 var pageSize = new print_preview.Size( 336 var pageSize = new print_preview.Size(
346 event.pageLayout.contentWidth + margins.get(o.LEFT) + 337 pageLayout.contentWidth + margins.get(o.LEFT) + margins.get(o.RIGHT),
347 margins.get(o.RIGHT), 338 pageLayout.contentHeight + margins.get(o.TOP) +
348 event.pageLayout.contentHeight + margins.get(o.TOP) +
349 margins.get(o.BOTTOM)); 339 margins.get(o.BOTTOM));
350 340
351 this.documentInfo_.updatePageInfo( 341 this.documentInfo_.updatePageInfo(
352 new print_preview.PrintableArea(origin, size), pageSize, 342 new print_preview.PrintableArea(origin, size), pageSize,
353 event.hasCustomPageSizeStyle, margins); 343 hasCustomPageSizeStyle, margins);
354 }, 344 },
355 345
356 /** 346 /**
357 * Called when the document page count is received from the native layer. 347 * Called when the document page count is received from the native layer.
358 * Always occurs as a result of a preview request. 348 * Always occurs as a result of a preview request.
359 * @param {Event} event Contains the document's page count. 349 * @param {number} pageCount The document's page count.
350 * @param {number} previewResponseId The request ID that corresponds to this
351 * page count.
352 * @param {number} fitToPageScaling The scaling required to fit the document
353 * to page (unused).
360 * @private 354 * @private
361 */ 355 */
362 onPageCountReady_: function(event) { 356 onPageCountReady_: function(
363 if (this.inFlightRequestId_ != event.previewResponseId) { 357 pageCount, previewResponseId, fitToPageScaling) {
358 if (this.inFlightRequestId_ != previewResponseId) {
364 return; // Ignore old response. 359 return; // Ignore old response.
365 } 360 }
366 this.documentInfo_.updatePageCount(event.pageCount); 361 this.documentInfo_.updatePageCount(pageCount);
367 this.pageRanges_ = this.printTicketStore_.pageRange.getPageRanges(); 362 this.pageRanges_ = this.printTicketStore_.pageRange.getPageRanges();
368 }, 363 },
369 364
370 /** 365 /**
371 * Called when a page's preview has been generated. Dispatches a 366 * Called when a page's preview has been generated. Dispatches a
372 * PAGE_READY event. 367 * PAGE_READY event.
373 * @param {Event} event Contains the page index and preview UID. 368 * @param {number} pageIndex The index of the page whose preview is ready.
369 * @param {number} previewUid The unique ID of the print preview UI.
370 * @param {number} previewResponseId The preview request ID that this page
371 * preview is a response to.
374 * @private 372 * @private
375 */ 373 */
376 onPagePreviewReady_: function(event) { 374 onPagePreviewReady_: function(pageIndex, previewUid, previewResponseId) {
377 if (this.inFlightRequestId_ != event.previewResponseId) { 375 if (this.inFlightRequestId_ != previewResponseId) {
378 return; // Ignore old response. 376 return; // Ignore old response.
379 } 377 }
380 var pageNumber = event.pageIndex + 1; 378 var pageNumber = pageIndex + 1;
381 var pageNumberSet = this.printTicketStore_.pageRange.getPageNumberSet(); 379 var pageNumberSet = this.printTicketStore_.pageRange.getPageNumberSet();
382 if (pageNumberSet.hasPageNumber(pageNumber)) { 380 if (pageNumberSet.hasPageNumber(pageNumber)) {
383 var previewIndex = pageNumberSet.getPageNumberIndex(pageNumber); 381 var previewIndex = pageNumberSet.getPageNumberIndex(pageNumber);
384 if (previewIndex == 0) { 382 if (previewIndex == 0) {
385 this.dispatchPreviewStartEvent_(event.previewUid, event.pageIndex); 383 this.dispatchPreviewStartEvent_(previewUid, pageIndex);
386 } 384 }
387 this.dispatchPageReadyEvent_( 385 this.dispatchPageReadyEvent_(previewIndex, pageNumber, previewUid);
388 previewIndex, pageNumber, event.previewUid);
389 } 386 }
390 }, 387 },
391 388
392 /** 389 /**
393 * Called when the preview generation is complete. Dispatches a 390 * Called when the preview generation is complete. Dispatches a
394 * DOCUMENT_READY event. 391 * DOCUMENT_READY event.
395 * @param {number} previewResponseId 392 * @param {number} previewResponseId
396 * @param {number} previewUid 393 * @param {number} previewUid
397 */ 394 */
398 onPreviewGenerationDone: function(previewResponseId, previewUid) { 395 onPreviewGenerationDone: function(previewResponseId, previewUid) {
(...skipping 16 matching lines...) Expand all
415 onPreviewGenerationFail_: function() { 412 onPreviewGenerationFail_: function() {
416 // NOTE: No request ID is returned from Chromium so its assumed its the 413 // NOTE: No request ID is returned from Chromium so its assumed its the
417 // current one. 414 // current one.
418 cr.dispatchSimpleEvent(this, PreviewGenerator.EventType.FAIL); 415 cr.dispatchSimpleEvent(this, PreviewGenerator.EventType.FAIL);
419 } 416 }
420 }; 417 };
421 418
422 // Export 419 // Export
423 return {PreviewGenerator: PreviewGenerator}; 420 return {PreviewGenerator: PreviewGenerator};
424 }); 421 });
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698