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

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

Issue 10108001: Refactor print preview web ui (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Address reviewer comments Created 8 years, 7 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 | Annotate | Revision Log
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 cr.define('print_preview', function() {
6 'use strict';
7
8 /**
9 * Interface to the Chromium print preview generator.
10 * @param {!print_preview.DestinationStore} destinationStore Used to get the
11 * currently selected destination.
12 * @param {!print_preview.PrintTicketStore} printTicketStore Used to read the
13 * state of the ticket and write document information.
14 * @param {!print_preview.NativeLayer} nativeLayer Used to communicate to
15 * Chromium's preview rendering system.
16 * @constructor
17 * @extends {cr.EventTarget}
18 */
19 function PreviewGenerator(destinationStore, printTicketStore, nativeLayer) {
20 cr.EventTarget.call(this);
21
22 /**
23 * Used to get the currently selected destination.
24 * @type {!print_preview.DestinationStore}
25 * @private
26 */
27 this.destinationStore_ = destinationStore;
28
29 /**
30 * Used to read the state of the ticket and write document information.
31 * @type {!print_preview.PrintTicketStore}
32 * @private
33 */
34 this.printTicketStore_ = printTicketStore;
35
36 /**
37 * Interface to the Chromium native layer.
38 * @type {!print_preview.NativeLayer}
39 * @private
40 */
41 this.nativeLayer_ = nativeLayer;
42
43 /**
44 * ID of current in-flight request. Requests that do not share this ID will
45 * be ignored.
46 * @type {number}
47 * @private
48 */
49 this.inFlightRequestId_ = -1;
50
51 /**
52 * Whether the previews are being generated in landscape mode.
53 * @type {boolean}
54 * @private
55 */
56 this.isLandscapeEnabled_ = false;
57
58 /**
59 * Whether the previews are being generated with a header and footer.
60 * @type {boolean}
61 * @private
62 */
63 this.isHeaderFooterEnabled_ = false;
64
65 /**
66 * Whether the previews are being generated in color.
67 * @type {boolean}
68 * @private
69 */
70 this.isColorEnabled_ = false;
71
72 /**
73 * Page number set used to generate the last preview.
74 * @type {print_preview.PageNumberSet}
75 * @private
76 */
77 this.pageNumberSet_ = null;
78
79 /**
80 * Margins type used to generate the last preview.
81 * @type {print_preview.ticket_items.MarginsType.Value}
82 * @private
83 */
84 this.marginsType_ = print_preview.ticket_items.MarginsType.Value.DEFAULT;
85
86 /**
87 * Custom margins used to generate the last preview.
88 * @type {print_preview.Margins}
89 * @private
90 */
91 this.customMargins_ = null;
92
93 /**
94 * Event tracker used to keep track of native layer events.
95 * @type {!EventTracker}
96 * @private
97 */
98 this.tracker_ = new EventTracker();
99
100 this.addEventListeners_();
101 };
102
103 /**
104 * Event types dispatched by the preview generator.
105 * @enum {string}
106 */
107 PreviewGenerator.EventType = {
108 // Dispatched when the document can be printed.
109 DOCUMENT_READY: 'print_preview.PreviewGenerator.DOCUMENT_READY',
110
111 // Dispatched when a page preview is ready. The previewIndex field of the
112 // event is the index of the page in the modified document, not the
113 // original. So page 4 of the original document might be previewIndex = 0 of
114 // the modified document.
115 PAGE_READY: 'print_preview.PreviewGenerator.PAGE_READY',
116
117 // Dispatched when the document preview starts to be generated.
118 PREVIEW_START: 'print_preview.PreviewGenerator.PREVIEW_START',
119
120 // Dispatched when the current print preview request fails.
121 FAIL: 'print_preview.PreviewGenerator.FAIL'
122 };
123
124 PreviewGenerator.prototype = {
125 __proto__: cr.EventTarget.prototype,
126
127 /**
128 * Request that new preview be generated. A preview request will not be
129 * generated if the print ticket has not changed sufficiently.
130 * @return {boolean} Whether a new preview was actually requested.
131 */
132 requestPreview: function() {
133 if (!this.printTicketStore_.isTicketValid()) {
134 throw Error('Requesting preview generation with invalid print ticket');
135 }
136 if (!this.hasPreviewChanged_()) {
137 return false;
138 }
139 this.isLandscapeEnabled_ = this.printTicketStore_.isLandscapeEnabled();
140 this.isHeaderFooterEnabled_ =
141 this.printTicketStore_.isHeaderFooterEnabled();
142 this.isColorEnabled_ = this.printTicketStore_.isColorEnabled();
143 this.pageNumberSet_ = this.printTicketStore_.getPageNumberSet();
144 this.marginsType_ = this.printTicketStore_.getMarginsType();
145 this.customMargins_ = this.printTicketStore_.getCustomMarginsInPts();
146
147 this.inFlightRequestId_++;
148 this.nativeLayer_.startGetPreview(
149 this.destinationStore_.selectedDestination,
150 this.printTicketStore_,
151 this.inFlightRequestId_);
152 return true;
153 },
154
155 /** Removes all event listeners that the preview generator has attached. */
156 removeEventListeners: function() {
157 this.tracker_.removeAll();
158 },
159
160 /**
161 * Adds event listeners to the relevant native layer events.
162 * @private
163 */
164 addEventListeners_: function() {
165 this.tracker_.add(
166 this.nativeLayer_,
167 print_preview.NativeLayer.EventType.PAGE_LAYOUT_READY,
168 this.onPageLayoutReady_.bind(this));
169 this.tracker_.add(
170 this.nativeLayer_,
171 print_preview.NativeLayer.EventType.PAGE_COUNT_READY,
172 this.onPageCountReady_.bind(this));
173 this.tracker_.add(
174 this.nativeLayer_,
175 print_preview.NativeLayer.EventType.PREVIEW_RELOAD,
176 this.onPreviewReload_.bind(this));
177 this.tracker_.add(
178 this.nativeLayer_,
179 print_preview.NativeLayer.EventType.PAGE_PREVIEW_READY,
180 this.onPagePreviewReady_.bind(this));
181 this.tracker_.add(
182 this.nativeLayer_,
183 print_preview.NativeLayer.EventType.PREVIEW_GENERATION_DONE,
184 this.onPreviewGenerationDone_.bind(this));
185 this.tracker_.add(
186 this.nativeLayer_,
187 print_preview.NativeLayer.EventType.PREVIEW_GENERATION_FAIL,
188 this.onPreviewGenerationFail_.bind(this));
189 },
190
191 /**
192 * Dispatches a PAGE_READY event to signal that a page preview is ready.
193 * @param {number} previewIndex Index of the page with respect to the pages
194 * shown in the preview. E.g an index of 0 is the first displayed page,
195 * but not necessarily the first original document page.
196 * @param {number} pageNumber Number of the page with respect to the
197 * document. A value of 3 means it's the third page of the original
198 * document.
199 * @param {string} previewUid Unique identifier of the preview.
200 * @private
201 */
202 dispatchPageReadyEvent_: function(previewIndex, pageNumber, previewUid) {
203 var pageGenEvent = new cr.Event(PreviewGenerator.EventType.PAGE_READY);
204 pageGenEvent.previewIndex = previewIndex;
205 pageGenEvent.previewUrl =
206 'chrome://print/' + previewUid + '/' + (pageNumber - 1) +
207 '/print.pdf';
208 this.dispatchEvent(pageGenEvent);
209 },
210
211 /**
212 * Dispatches a PREVIEW_START event. Signals that the preview should be
213 * reloaded.
214 * @param {string} previewUid Unique identifier of the preview.
215 * @private
216 */
217 dispatchPreviewStartEvent_: function(previewUid) {
218 var previewStartEvent = new cr.Event(
219 PreviewGenerator.EventType.PREVIEW_START);
220 var index = -1;
221 if (this.printTicketStore_.isDocumentModifiable) {
222 index = 0;
223 }
224 previewStartEvent.previewUrl =
225 'chrome://print/' + previewUid + '/' + index + '/print.pdf';
226 this.dispatchEvent(previewStartEvent);
227 },
228
229 /**
230 * Called when the page layout of the document is ready. Always occurs
231 * as a result of a preview request.
232 * @param {cr.Event} event Contains layout info about the document.
233 * @private
234 */
235 onPageLayoutReady_: function(event) {
236 // NOTE: A request ID is not specified, so assuming its for the current
237 // in-flight request.
238
239 // TODO Do we need this?
240 //var hasCustomPageSizeStyle = event.hasCustomPageSizeStyle;
241
242 // Printable area info is unavailable on linux nor Google Chrome OS.
243 var origin = new print_preview.Coordinate2d(
244 event.pageLayout.printableAreaX,
245 event.pageLayout.printableAreaY);
246 var size = new print_preview.Size(
247 event.pageLayout.printableAreaWidth,
248 event.pageLayout.printableAreaHeight);
249 this.printTicketStore_.updatePrintableArea(
250 new print_preview.PrintableArea(origin, size));
251
252 var margins = new print_preview.Margins(
253 event.pageLayout.marginTop,
254 event.pageLayout.marginRight,
255 event.pageLayout.marginBottom,
256 event.pageLayout.marginLeft);
257
258 var orientationEnum =
259 print_preview.ticket_items.CustomMargins.Orientation;
260 var pageSize = new print_preview.Size(
261 event.pageLayout.contentWidth +
262 margins.get(orientationEnum.LEFT) +
263 margins.get(orientationEnum.RIGHT),
264 event.pageLayout.contentHeight +
265 margins.get(orientationEnum.TOP) +
266 margins.get(orientationEnum.BOTTOM));
267 this.printTicketStore_.updatePageSize(pageSize);
268
269 if (this.inFlightRequestId_ == 0) {
270 this.printTicketStore_.updateDefaultCustomMarginsInPts(margins);
271 }
272 },
273
274 /**
275 * Called when the document page count is received from the native layer.
276 * Always occurs as a result of a preview request.
277 * @param {cr.Event} event Contains the document's page count.
278 * @private
279 */
280 onPageCountReady_: function(event) {
281 if (this.inFlightRequestId_ != event.previewResponseId) {
282 return; // Ignore old response.
283 }
284 this.printTicketStore_.updatePageCount(event.pageCount);
285 },
286
287 /**
288 * Called when the print preview should be reloaded.
289 * @param {cr.Event} event Contains the preview UID and request ID.
290 * @private
291 */
292 onPreviewReload_: function(event) {
293 if (this.inFlightRequestId_ != event.previewResponseId) {
294 return; // Ignore old response.
295 }
296 this.dispatchPreviewStartEvent_(event.previewUid);
297 var pageNumberSet = this.printTicketStore_.getPageNumberSet();
298 for (var i = 0; i < pageNumberSet.size; i++) {
299 var pageNumber = pageNumberSet.getPageNumberAt(i);
300 this.dispatchPageReadyEvent_(i, pageNumber, event.previewUid);
301 }
302 },
303
304 /**
305 * Called when a page's preview has been generated. Dispatches a
306 * PAGE_READY event.
307 * @param {cr.Event} event Contains the page index and preview UID.
308 * @private
309 */
310 onPagePreviewReady_: function(event) {
311 if (this.inFlightRequestId_ != event.previewResponseId) {
312 return; // Ignore old response.
313 }
314 var pageNumber = event.pageIndex + 1;
315 if (this.printTicketStore_.getPageNumberSet().hasPageNumber(pageNumber)) {
316 var previewIndex = this.printTicketStore_.getPageNumberSet()
317 .getPageNumberIndex(pageNumber);
318 if (previewIndex == 0) {
319 this.dispatchPreviewStartEvent_(event.previewUid);
320 }
321 this.dispatchPageReadyEvent_(
322 previewIndex, pageNumber, event.previewUid);
323 }
324 },
325
326 /**
327 * Called when the preview generation is complete. Dispatches a
328 * DOCUMENT_READY event.
329 * @param {cr.Event} event Contains the preview UID and response ID.
330 * @private
331 */
332 onPreviewGenerationDone_: function(event) {
333 if (this.inFlightRequestId_ != event.previewResponseId) {
334 return; // Ignore old response.
335 }
336 // Dispatch a PREVIEW_START event since non-modifiable documents don't
337 // trigger PAGE_READY events.
338 if (!this.printTicketStore_.isDocumentModifiable) {
339 this.dispatchPreviewStartEvent_(event.previewUid);
340 }
341 cr.dispatchSimpleEvent(this, PreviewGenerator.EventType.DOCUMENT_READY);
342 },
343
344 /**
345 * Called when the preview generation fails.
346 * @private
347 */
348 onPreviewGenerationFail_: function() {
349 // NOTE: No request ID is returned from Chromium so its assumed its the
350 // current one.
351 cr.dispatchSimpleEvent(this, PreviewGenerator.EventType.FAIL);
352 },
353
354 /**
355 * @return {boolean} Whether the print ticket has changed sufficiently to
356 * determine whether a new preview request should be issued.
357 * @private
358 */
359 hasPreviewChanged_: function() {
360 var ticketStore = this.printTicketStore_;
361 return this.inFlightRequestId_ == -1 ||
362 ticketStore.isLandscapeEnabled() != this.isLandscapeEnabled_ ||
363 ticketStore.isHeaderFooterEnabled() != this.isHeaderFooterEnabled_ ||
364 ticketStore.isColorEnabled() != this.isColorEnabled_ ||
365 !ticketStore.getPageNumberSet().equals(this.pageNumberSet_) ||
366 ticketStore.getMarginsType() != this.marginsType_ ||
367 (this.marginsType_ ==
368 print_preview.ticket_items.MarginsType.Value.CUSTOM &&
369 !ticketStore.getCustomMarginsInPts().equals(this.customMargins_));
370 }
371 };
372
373 // Export
374 return {
375 PreviewGenerator: PreviewGenerator
376 };
377 });
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698