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

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: Resolve conflicts 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 * Whether the document should be fitted to the page.
74 * @type {boolean}
75 * @private
76 */
77 this.isFitToPageEnabled_ = false;
78
79 /**
80 * Page number set used to generate the last preview.
81 * @type {print_preview.PageNumberSet}
82 * @private
83 */
84 this.pageNumberSet_ = null;
85
86 /**
87 * Margins type used to generate the last preview.
88 * @type {print_preview.ticket_items.MarginsType.Value}
89 * @private
90 */
91 this.marginsType_ = print_preview.ticket_items.MarginsType.Value.DEFAULT;
92
93 /**
94 * Destination that was selected for the last preview.
95 * @type {print_preview.Destination}
96 * @private
97 */
98 this.selectedDestination_ = null;
99
100 /**
101 * Event tracker used to keep track of native layer events.
102 * @type {!EventTracker}
103 * @private
104 */
105 this.tracker_ = new EventTracker();
106
107 this.addEventListeners_();
108 };
109
110 /**
111 * Event types dispatched by the preview generator.
112 * @enum {string}
113 */
114 PreviewGenerator.EventType = {
115 // Dispatched when the document can be printed.
116 DOCUMENT_READY: 'print_preview.PreviewGenerator.DOCUMENT_READY',
117
118 // Dispatched when a page preview is ready. The previewIndex field of the
119 // event is the index of the page in the modified document, not the
120 // original. So page 4 of the original document might be previewIndex = 0 of
121 // the modified document.
122 PAGE_READY: 'print_preview.PreviewGenerator.PAGE_READY',
123
124 // Dispatched when the document preview starts to be generated.
125 PREVIEW_START: 'print_preview.PreviewGenerator.PREVIEW_START',
126
127 // Dispatched when the current print preview request fails.
128 FAIL: 'print_preview.PreviewGenerator.FAIL'
129 };
130
131 PreviewGenerator.prototype = {
132 __proto__: cr.EventTarget.prototype,
133
134 /**
135 * Request that new preview be generated. A preview request will not be
136 * generated if the print ticket has not changed sufficiently.
137 * @return {boolean} Whether a new preview was actually requested.
138 */
139 requestPreview: function() {
140 if (!this.printTicketStore_.isTicketValidForPreview()) {
141 return false;
142 }
143 if (!this.hasPreviewChanged_()) {
144 // Changes to these ticket items might not trigger a new preview, but
145 // they still need to be recorded.
146 this.marginsType_ = this.printTicketStore_.getMarginsType();
147 return false;
148 }
149 this.isLandscapeEnabled_ = this.printTicketStore_.isLandscapeEnabled();
150 this.isHeaderFooterEnabled_ =
151 this.printTicketStore_.isHeaderFooterEnabled();
152 this.isColorEnabled_ = this.printTicketStore_.isColorEnabled();
153 this.isFitToPageEnabled_ = this.printTicketStore_.isFitToPageEnabled();
154 this.pageNumberSet_ = this.printTicketStore_.getPageNumberSet();
155 this.marginsType_ = this.printTicketStore_.getMarginsType();
156 this.selectedDestination_ = this.destinationStore_.selectedDestination;
157
158 this.inFlightRequestId_++;
159 this.nativeLayer_.startGetPreview(
160 this.destinationStore_.selectedDestination,
161 this.printTicketStore_,
162 this.inFlightRequestId_);
163 return true;
164 },
165
166 /** Removes all event listeners that the preview generator has attached. */
167 removeEventListeners: function() {
168 this.tracker_.removeAll();
169 },
170
171 /**
172 * Adds event listeners to the relevant native layer events.
173 * @private
174 */
175 addEventListeners_: function() {
176 this.tracker_.add(
177 this.nativeLayer_,
178 print_preview.NativeLayer.EventType.PAGE_LAYOUT_READY,
179 this.onPageLayoutReady_.bind(this));
180 this.tracker_.add(
181 this.nativeLayer_,
182 print_preview.NativeLayer.EventType.PAGE_COUNT_READY,
183 this.onPageCountReady_.bind(this));
184 this.tracker_.add(
185 this.nativeLayer_,
186 print_preview.NativeLayer.EventType.PREVIEW_RELOAD,
187 this.onPreviewReload_.bind(this));
188 this.tracker_.add(
189 this.nativeLayer_,
190 print_preview.NativeLayer.EventType.PAGE_PREVIEW_READY,
191 this.onPagePreviewReady_.bind(this));
192 this.tracker_.add(
193 this.nativeLayer_,
194 print_preview.NativeLayer.EventType.PREVIEW_GENERATION_DONE,
195 this.onPreviewGenerationDone_.bind(this));
196 this.tracker_.add(
197 this.nativeLayer_,
198 print_preview.NativeLayer.EventType.PREVIEW_GENERATION_FAIL,
199 this.onPreviewGenerationFail_.bind(this));
200 },
201
202 /**
203 * Dispatches a PAGE_READY event to signal that a page preview is ready.
204 * @param {number} previewIndex Index of the page with respect to the pages
205 * shown in the preview. E.g an index of 0 is the first displayed page,
206 * but not necessarily the first original document page.
207 * @param {number} pageNumber Number of the page with respect to the
208 * document. A value of 3 means it's the third page of the original
209 * document.
210 * @param {string} previewUid Unique identifier of the preview.
211 * @private
212 */
213 dispatchPageReadyEvent_: function(previewIndex, pageNumber, previewUid) {
214 var pageGenEvent = new cr.Event(PreviewGenerator.EventType.PAGE_READY);
215 pageGenEvent.previewIndex = previewIndex;
216 pageGenEvent.previewUrl =
217 'chrome://print/' + previewUid + '/' + (pageNumber - 1) +
218 '/print.pdf';
219 this.dispatchEvent(pageGenEvent);
220 },
221
222 /**
223 * Dispatches a PREVIEW_START event. Signals that the preview should be
224 * reloaded.
225 * @param {string} previewUid Unique identifier of the preview.
226 * @private
227 */
228 dispatchPreviewStartEvent_: function(previewUid) {
229 var previewStartEvent = new cr.Event(
230 PreviewGenerator.EventType.PREVIEW_START);
231 var index = -1;
232 if (this.printTicketStore_.isDocumentModifiable) {
233 index = 0;
234 }
235 previewStartEvent.previewUrl =
236 'chrome://print/' + previewUid + '/' + index + '/print.pdf';
237 this.dispatchEvent(previewStartEvent);
238 },
239
240 /**
241 * @return {boolean} Whether the print ticket has changed sufficiently to
242 * determine whether a new preview request should be issued.
243 * @private
244 */
245 hasPreviewChanged_: function() {
246 var ticketStore = this.printTicketStore_;
247 return this.inFlightRequestId_ == -1 ||
248 ticketStore.isLandscapeEnabled() != this.isLandscapeEnabled_ ||
249 ticketStore.isHeaderFooterEnabled() != this.isHeaderFooterEnabled_ ||
250 ticketStore.isColorEnabled() != this.isColorEnabled_ ||
251 ticketStore.isFitToPageEnabled() != this.isFitToPageEnabled_ ||
252 !ticketStore.getPageNumberSet().equals(this.pageNumberSet_) ||
253 (ticketStore.getMarginsType() != this.marginsType_ &&
254 ticketStore.getMarginsType() !=
255 print_preview.ticket_items.MarginsType.Value.CUSTOM) ||
256 (ticketStore.getMarginsType() ==
257 print_preview.ticket_items.MarginsType.Value.CUSTOM &&
258 !ticketStore.getCustomMargins().equals(
259 ticketStore.getDocumentMargins())) ||
260 (this.selectedDestination_ !=
261 this.destinationStore_.selectedDestination &&
262 (this.destinationStore_.selectedDestination.id ==
263 print_preview.Destination.GooglePromotedId.SAVE_AS_PDF ||
264 this.selectedDestination_.id ==
265 print_preview.Destination.GooglePromotedId.SAVE_AS_PDF));
266 },
267
268 /**
269 * Called when the page layout of the document is ready. Always occurs
270 * as a result of a preview request.
271 * @param {cr.Event} event Contains layout info about the document.
272 * @private
273 */
274 onPageLayoutReady_: function(event) {
275 // NOTE: A request ID is not specified, so assuming its for the current
276 // in-flight request.
277
278 var origin = new print_preview.Coordinate2d(
279 event.pageLayout.printableAreaX,
280 event.pageLayout.printableAreaY);
281 var size = new print_preview.Size(
282 event.pageLayout.printableAreaWidth,
283 event.pageLayout.printableAreaHeight);
284
285 var margins = new print_preview.Margins(
286 Math.round(event.pageLayout.marginTop),
287 Math.round(event.pageLayout.marginRight),
288 Math.round(event.pageLayout.marginBottom),
289 Math.round(event.pageLayout.marginLeft));
290
291 var o = print_preview.ticket_items.CustomMargins.Orientation;
292 var pageSize = new print_preview.Size(
293 event.pageLayout.contentWidth +
294 margins.get(o.LEFT) + margins.get(o.RIGHT),
295 event.pageLayout.contentHeight +
296 margins.get(o.TOP) + margins.get(o.BOTTOM));
297
298 this.printTicketStore_.updateDocumentPageInfo(
299 new print_preview.PrintableArea(origin, size),
300 pageSize,
301 event.hasCustomPageSizeStyle,
302 margins);
303 },
304
305 /**
306 * Called when the document page count is received from the native layer.
307 * Always occurs as a result of a preview request.
308 * @param {cr.Event} event Contains the document's page count.
309 * @private
310 */
311 onPageCountReady_: function(event) {
312 if (this.inFlightRequestId_ != event.previewResponseId) {
313 return; // Ignore old response.
314 }
315 this.printTicketStore_.updatePageCount(event.pageCount);
316 this.pageNumberSet_ = this.printTicketStore_.getPageNumberSet();
317 },
318
319 /**
320 * Called when the print preview should be reloaded.
321 * @param {cr.Event} event Contains the preview UID and request ID.
322 * @private
323 */
324 onPreviewReload_: function(event) {
325 if (this.inFlightRequestId_ != event.previewResponseId) {
326 return; // Ignore old response.
327 }
328 this.dispatchPreviewStartEvent_(event.previewUid);
329 var pageNumberSet = this.printTicketStore_.getPageNumberSet();
330 for (var i = 0; i < pageNumberSet.size; i++) {
331 var pageNumber = pageNumberSet.getPageNumberAt(i);
332 this.dispatchPageReadyEvent_(i, pageNumber, event.previewUid);
333 }
334 cr.dispatchSimpleEvent(this, PreviewGenerator.EventType.DOCUMENT_READY);
335 },
336
337 /**
338 * Called when a page's preview has been generated. Dispatches a
339 * PAGE_READY event.
340 * @param {cr.Event} event Contains the page index and preview UID.
341 * @private
342 */
343 onPagePreviewReady_: function(event) {
344 if (this.inFlightRequestId_ != event.previewResponseId) {
345 return; // Ignore old response.
346 }
347 var pageNumber = event.pageIndex + 1;
348 if (this.printTicketStore_.getPageNumberSet().hasPageNumber(pageNumber)) {
349 var previewIndex = this.printTicketStore_.getPageNumberSet()
350 .getPageNumberIndex(pageNumber);
351 if (previewIndex == 0) {
352 this.dispatchPreviewStartEvent_(event.previewUid);
353 }
354 this.dispatchPageReadyEvent_(
355 previewIndex, pageNumber, event.previewUid);
356 }
357 },
358
359 /**
360 * Called when the preview generation is complete. Dispatches a
361 * DOCUMENT_READY event.
362 * @param {cr.Event} event Contains the preview UID and response ID.
363 * @private
364 */
365 onPreviewGenerationDone_: function(event) {
366 if (this.inFlightRequestId_ != event.previewResponseId) {
367 return; // Ignore old response.
368 }
369 // Dispatch a PREVIEW_START event since non-modifiable documents don't
370 // trigger PAGE_READY events.
371 if (!this.printTicketStore_.isDocumentModifiable) {
372 this.dispatchPreviewStartEvent_(event.previewUid);
373 }
374 cr.dispatchSimpleEvent(this, PreviewGenerator.EventType.DOCUMENT_READY);
375 },
376
377 /**
378 * Called when the preview generation fails.
379 * @private
380 */
381 onPreviewGenerationFail_: function() {
382 // NOTE: No request ID is returned from Chromium so its assumed its the
383 // current one.
384 cr.dispatchSimpleEvent(this, PreviewGenerator.EventType.FAIL);
385 }
386 };
387
388 // Export
389 return {
390 PreviewGenerator: PreviewGenerator
391 };
392 });
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698