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

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: Fix fit-to-page tests 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 // Changes to these ticket items might not trigger a new preview, but
138 // they still need to be recorded.
139 this.marginsType_ = this.printTicketStore_.getMarginsType();
140 this.customMargins_ = this.printTicketStore_.getCustomMargins();
141 return false;
142 }
143 this.isLandscapeEnabled_ = this.printTicketStore_.isLandscapeEnabled();
144 this.isHeaderFooterEnabled_ =
145 this.printTicketStore_.isHeaderFooterEnabled();
146 this.isColorEnabled_ = this.printTicketStore_.isColorEnabled();
147 this.pageNumberSet_ = this.printTicketStore_.getPageNumberSet();
148 this.marginsType_ = this.printTicketStore_.getMarginsType();
149 this.customMargins_ = this.printTicketStore_.getCustomMargins();
150
151 this.inFlightRequestId_++;
152 this.nativeLayer_.startGetPreview(
153 this.destinationStore_.selectedDestination,
154 this.printTicketStore_,
155 this.inFlightRequestId_);
156 return true;
157 },
158
159 /** Removes all event listeners that the preview generator has attached. */
160 removeEventListeners: function() {
161 this.tracker_.removeAll();
162 },
163
164 /**
165 * Adds event listeners to the relevant native layer events.
166 * @private
167 */
168 addEventListeners_: function() {
169 this.tracker_.add(
170 this.nativeLayer_,
171 print_preview.NativeLayer.EventType.PAGE_LAYOUT_READY,
172 this.onPageLayoutReady_.bind(this));
173 this.tracker_.add(
174 this.nativeLayer_,
175 print_preview.NativeLayer.EventType.PAGE_COUNT_READY,
176 this.onPageCountReady_.bind(this));
177 this.tracker_.add(
178 this.nativeLayer_,
179 print_preview.NativeLayer.EventType.PREVIEW_RELOAD,
180 this.onPreviewReload_.bind(this));
181 this.tracker_.add(
182 this.nativeLayer_,
183 print_preview.NativeLayer.EventType.PAGE_PREVIEW_READY,
184 this.onPagePreviewReady_.bind(this));
185 this.tracker_.add(
186 this.nativeLayer_,
187 print_preview.NativeLayer.EventType.PREVIEW_GENERATION_DONE,
188 this.onPreviewGenerationDone_.bind(this));
189 this.tracker_.add(
190 this.nativeLayer_,
191 print_preview.NativeLayer.EventType.PREVIEW_GENERATION_FAIL,
192 this.onPreviewGenerationFail_.bind(this));
193 },
194
195 /**
196 * Dispatches a PAGE_READY event to signal that a page preview is ready.
197 * @param {number} previewIndex Index of the page with respect to the pages
198 * shown in the preview. E.g an index of 0 is the first displayed page,
199 * but not necessarily the first original document page.
200 * @param {number} pageNumber Number of the page with respect to the
201 * document. A value of 3 means it's the third page of the original
202 * document.
203 * @param {string} previewUid Unique identifier of the preview.
204 * @private
205 */
206 dispatchPageReadyEvent_: function(previewIndex, pageNumber, previewUid) {
207 var pageGenEvent = new cr.Event(PreviewGenerator.EventType.PAGE_READY);
208 pageGenEvent.previewIndex = previewIndex;
209 pageGenEvent.previewUrl =
210 'chrome://print/' + previewUid + '/' + (pageNumber - 1) +
211 '/print.pdf';
212 this.dispatchEvent(pageGenEvent);
213 },
214
215 /**
216 * Dispatches a PREVIEW_START event. Signals that the preview should be
217 * reloaded.
218 * @param {string} previewUid Unique identifier of the preview.
219 * @private
220 */
221 dispatchPreviewStartEvent_: function(previewUid) {
222 var previewStartEvent = new cr.Event(
223 PreviewGenerator.EventType.PREVIEW_START);
224 var index = -1;
225 if (this.printTicketStore_.isDocumentModifiable) {
226 index = 0;
227 }
228 previewStartEvent.previewUrl =
229 'chrome://print/' + previewUid + '/' + index + '/print.pdf';
230 this.dispatchEvent(previewStartEvent);
231 },
232
233 /**
234 * @return {boolean} Whether the print ticket has changed sufficiently to
235 * determine whether a new preview request should be issued.
236 * @private
237 */
238 hasPreviewChanged_: function() {
239 var ticketStore = this.printTicketStore_;
240 return this.inFlightRequestId_ == -1 ||
241 ticketStore.isLandscapeEnabled() != this.isLandscapeEnabled_ ||
242 ticketStore.isHeaderFooterEnabled() != this.isHeaderFooterEnabled_ ||
243 ticketStore.isColorEnabled() != this.isColorEnabled_ ||
244 !ticketStore.getPageNumberSet().equals(this.pageNumberSet_) ||
245 (ticketStore.getMarginsType() != this.marginsType_ &&
246 ticketStore.getMarginsType() !=
247 print_preview.ticket_items.MarginsType.Value.CUSTOM) ||
248 (ticketStore.getMarginsType() ==
249 print_preview.ticket_items.MarginsType.Value.CUSTOM &&
250 !ticketStore.getCustomMargins().equals(this.customMargins_));
251 },
252
253 /**
254 * Called when the page layout of the document is ready. Always occurs
255 * as a result of a preview request.
256 * @param {cr.Event} event Contains layout info about the document.
257 * @private
258 */
259 onPageLayoutReady_: function(event) {
260 // NOTE: A request ID is not specified, so assuming its for the current
261 // in-flight request.
262
263 // TODO Do we need this?
264 //var hasCustomPageSizeStyle = event.hasCustomPageSizeStyle;
265
266 // Printable area info is unavailable on linux nor Google Chrome OS.
267 var origin = new print_preview.Coordinate2d(
268 event.pageLayout.printableAreaX,
269 event.pageLayout.printableAreaY);
270 var size = new print_preview.Size(
271 event.pageLayout.printableAreaWidth,
272 event.pageLayout.printableAreaHeight);
273 this.printTicketStore_.updatePrintableArea(
274 new print_preview.PrintableArea(origin, size));
275
276 var margins = new print_preview.Margins(
277 Math.round(event.pageLayout.marginTop),
278 Math.round(event.pageLayout.marginRight),
279 Math.round(event.pageLayout.marginBottom),
280 Math.round(event.pageLayout.marginLeft));
281
282 var orientationEnum =
283 print_preview.ticket_items.CustomMargins.Orientation;
284 var pageSize = new print_preview.Size(
285 event.pageLayout.contentWidth +
286 margins.get(orientationEnum.LEFT) +
287 margins.get(orientationEnum.RIGHT),
288 event.pageLayout.contentHeight +
289 margins.get(orientationEnum.TOP) +
290 margins.get(orientationEnum.BOTTOM));
291 this.printTicketStore_.updatePageSize(pageSize);
292
293 if (this.printTicketStore_.getMarginsType() !=
294 print_preview.ticket_items.MarginsType.Value.CUSTOM) {
295 this.printTicketStore_.updateDefaultCustomMargins(margins);
296 }
297 },
298
299 /**
300 * Called when the document page count is received from the native layer.
301 * Always occurs as a result of a preview request.
302 * @param {cr.Event} event Contains the document's page count.
303 * @private
304 */
305 onPageCountReady_: function(event) {
306 if (this.inFlightRequestId_ != event.previewResponseId) {
307 return; // Ignore old response.
308 }
309 this.printTicketStore_.updatePageCount(event.pageCount);
310 this.pageNumberSet_ = this.printTicketStore_.getPageNumberSet();
311 },
312
313 /**
314 * Called when the print preview should be reloaded.
315 * @param {cr.Event} event Contains the preview UID and request ID.
316 * @private
317 */
318 onPreviewReload_: function(event) {
319 if (this.inFlightRequestId_ != event.previewResponseId) {
320 return; // Ignore old response.
321 }
322 this.dispatchPreviewStartEvent_(event.previewUid);
323 var pageNumberSet = this.printTicketStore_.getPageNumberSet();
324 for (var i = 0; i < pageNumberSet.size; i++) {
325 var pageNumber = pageNumberSet.getPageNumberAt(i);
326 this.dispatchPageReadyEvent_(i, pageNumber, event.previewUid);
327 }
328 cr.dispatchSimpleEvent(this, PreviewGenerator.EventType.DOCUMENT_READY);
329 },
330
331 /**
332 * Called when a page's preview has been generated. Dispatches a
333 * PAGE_READY event.
334 * @param {cr.Event} event Contains the page index and preview UID.
335 * @private
336 */
337 onPagePreviewReady_: function(event) {
338 if (this.inFlightRequestId_ != event.previewResponseId) {
339 return; // Ignore old response.
340 }
341 var pageNumber = event.pageIndex + 1;
342 if (this.printTicketStore_.getPageNumberSet().hasPageNumber(pageNumber)) {
343 var previewIndex = this.printTicketStore_.getPageNumberSet()
344 .getPageNumberIndex(pageNumber);
345 if (previewIndex == 0) {
346 this.dispatchPreviewStartEvent_(event.previewUid);
347 }
348 this.dispatchPageReadyEvent_(
349 previewIndex, pageNumber, event.previewUid);
350 }
351 },
352
353 /**
354 * Called when the preview generation is complete. Dispatches a
355 * DOCUMENT_READY event.
356 * @param {cr.Event} event Contains the preview UID and response ID.
357 * @private
358 */
359 onPreviewGenerationDone_: function(event) {
360 if (this.inFlightRequestId_ != event.previewResponseId) {
361 return; // Ignore old response.
362 }
363 // Dispatch a PREVIEW_START event since non-modifiable documents don't
364 // trigger PAGE_READY events.
365 if (!this.printTicketStore_.isDocumentModifiable) {
366 this.dispatchPreviewStartEvent_(event.previewUid);
367 }
368 cr.dispatchSimpleEvent(this, PreviewGenerator.EventType.DOCUMENT_READY);
369 },
370
371 /**
372 * Called when the preview generation fails.
373 * @private
374 */
375 onPreviewGenerationFail_: function() {
376 // NOTE: No request ID is returned from Chromium so its assumed its the
377 // current one.
378 cr.dispatchSimpleEvent(this, PreviewGenerator.EventType.FAIL);
379 }
380 };
381
382 // Export
383 return {
384 PreviewGenerator: PreviewGenerator
385 };
386 });
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698