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

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

Powered by Google App Engine
This is Rietveld 408576698