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

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: Remove extra files Created 8 years, 8 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 // TODO Need to handle termination case--detach event listeners from native
9 // layer.
10
11 /**
12 * Interface to the Chromium print preview generator.
13 *
14 * @param {print_preview.DestinationStore!} destinationStore Used to get the
15 * currently selected destination.
16 * @param {print_preview.PrintTicketStore!} printTicketStore Used to read the
17 * state of the ticket and write document information.
18 * @param {print_preview.NativeLayer!} nativeLayer Used to communicate to
19 * Chromium's preview rendering system.
20 * @constructor
21 * @extends {cr.EventTarget}
22 */
23 function PreviewGenerator(destinationStore, printTicketStore, nativeLayer) {
24 cr.EventTarget.call(this);
25
26 /**
27 * Used to get the currently selected destination.
28 * @type {print_preview.DestinationStore!}
29 * @private
30 */
31 this.destinationStore_ = destinationStore;
32
33 /**
34 * Used to read the state of the ticket and write document information.
35 * @type {print_preview.PrintTicketStore!}
36 * @private
37 */
38 this.printTicketStore_ = printTicketStore;
39
40 /**
41 * Interface to the Chromium native layer.
42 * @type {print_preview.NativeLayer!}
43 * @private
44 */
45 this.nativeLayer_ = nativeLayer;
46
47 /**
48 * ID of current in-flight request. Requests that do not share this ID will
49 * be ignored.
50 * @type {number}
51 * @private
52 */
53 this.inFlightRequestId_ = -1;
54
55 /**
56 * Whether the previews are being generated in landscape mode.
57 * @type {boolean}
58 * @private
59 */
60 this.isLandscapeEnabled_ = false;
61
62 /**
63 * Whether the previews are being generated with a header and footer.
64 * @type {boolean}
65 * @private
66 */
67 this.isHeaderFooterEnabled_ = false;
68
69 /**
70 * Whether the previews are being generated in color.
71 * @type {boolean}
72 * @private
73 */
74 this.isColorEnabled_ = false;
75
76 /**
77 * Page number set used to generate the last preview.
78 * @type {print_preview.PageNumberSet}
79 * @private
80 */
81 this.pageNumberSet_ = null;
82
83 /**
84 * Margins type used to generate the last preview.
85 * @type {print_preview.Margins.Type}
86 * @private
87 */
88 this.marginsType_ = print_preview.Margins.Type.DEFAULT;
89
90 /**
91 * Custom margins used to generate the last preview.
92 * @type {print_preview.Margins}
93 * @private
94 */
95 this.customMargins_ = null;
96
97 /**
98 * Event tracker used to keep track of native layer events.
99 * @type {EventTracker}
100 * @private
101 */
102 this.tracker_ = new EventTracker();
103
104 this.addEventListeners_();
105 };
106
107 /**
108 * Events dispatched by the preview generator.
109 * @enum {string}
110 */
111 PreviewGenerator.Event = {
112 // Dispatched when the document can be printed.
113 DOCUMENT_READY: 'print_preview.PreviewGenerator.DOCUMENT_READY',
114
115 // Dispatched when a page preview is ready. The previewIndex field of the
116 // event is the index of the page in the modified document, not the
117 // original. So page 4 of the original document might be previewIndex = 0 of
118 // the modified document.
119 PAGE_READY: 'print_preview.PreviewGenerator.PAGE_READY',
120
121 // Dispatched when the current print preview request fails.
122 FAIL: 'print_preview.PreviewGenerator.FAIL'
123 };
124
125 PreviewGenerator.prototype = {
126 __proto__: cr.EventTarget.prototype,
127
128 /** @override */
129 dispatchEvent: function(evt) {
130 // TODO REMOVE ME
131 log(evt.type);
132 cr.EventTarget.prototype.dispatchEvent.call(this, evt);
133 },
134
135 /**
136 * Request that new preview be generated. A preview request will not be
137 * generated if the print ticket has not changed sufficiently.
138 */
139 requestPreview: function() {
140 if (this.hasPreviewChanged_()) {
141 log('print_preview.PreviewGenerator.requestPreview');
142 this.isLandscapeEnabled_ = this.printTicketStore_.isLandscapeEnabled();
143 this.isHeaderFooterEnabled_ =
144 this.printTicketStore_.isHeaderFooterEnabled();
145 this.isColorEnabled_ = this.printTicketStore_.isColorEnabled();
146 this.pageNumberSet_ = this.printTicketStore_.getPageNumberSet();
147 this.marginsType_ = this.printTicketStore_.getMarginsType();
148 this.customMargins_ = this.printTicketStore_.getCustomMargins();
149
150 this.inFlightRequestId_++;
151 this.nativeLayer_.startGetPreview(
152 this.destinationStore_.selectedDestination,
153 this.printTicketStore_,
154 this.inFlightRequestId_);
155 }
156 },
157
158 /**
159 * Adds event listeners to the relevant native layer events.
160 * @private
161 */
162 addEventListeners_: function() {
163 this.tracker_.add(
164 this.nativeLayer_,
165 print_preview.NativeLayer.Event.PAGE_LAYOUT_CHANGE,
166 this.onPageLayoutChange_.bind(this));
167 this.tracker_.add(
168 this.nativeLayer_,
169 print_preview.NativeLayer.Event.PAGE_COUNT_CHANGE,
170 this.onPageCountChange_.bind(this));
171 this.tracker_.add(
172 this.nativeLayer_,
173 print_preview.NativeLayer.Event.PREVIEW_RELOAD,
174 this.onPreviewReload_.bind(this));
175 this.tracker_.add(
176 this.nativeLayer_,
177 print_preview.NativeLayer.Event.PAGE_PREVIEW_READY,
178 this.onPagePreviewReady_.bind(this));
179 this.tracker_.add(
180 this.nativeLayer_,
181 print_preview.NativeLayer.Event.PREVIEW_GENERATION_DONE,
182 this.onPreviewGenerationDone_.bind(this));
183 this.tracker_.add(
184 this.nativeLayer_,
185 print_preview.NativeLayer.Event.PREVIEW_GENERATION_FAIL,
186 this.onPreviewGenerationFail_.bind(this));
187 },
188
189 dispatchPageReadyEvent_: function(previewIndex, pageNumber, previewUid) {
190 var pageGenEvt = new cr.Event(PreviewGenerator.Event.PAGE_READY);
191 pageGenEvt.previewIndex = previewIndex;
192 pageGenEvt.previewUrl =
193 'chrome://print/' + previewUid + '/' + (pageNumber - 1) +
194 '/print.pdf';
195 this.dispatchEvent(pageGenEvt);
196 },
197
198 /**
199 * Called when the page layout of the document has changed. Always occurs
200 * as a result of a preview request.
201 * @param {cr.Event} evt Contains layout info about the document.
202 * @private
203 */
204 onPageLayoutChange_: function(evt) {
205 // NOTE: A request ID is not specified, so assuming its for the current
206 // in-flight request.
207
208 // TODO Do we need this?
209 //var hasCustomPageSizeStyle = evt.hasCustomPageSizeStyle;
210
211 // Printable area info is unavailable on linux nor Google Chrome OS.
212 // TODO Maybe we can not have to have this if condition, and always set
213 // printable area?
214 log(JSON.stringify(evt.pageLayout));
215 if (!cr.isLinux && !cr.isChromeOS) {
216 var origin = new print_preview.Coordinate2d(
217 evt.pageLayout.printableAreaX,
218 evt.pageLayout.printableAreaY);
219 var size = new print_preview.Size(
220 evt.pageLayout.printableAreaWidth,
221 evt.pageLayout.printableAreaHeight);
222 this.printTicketStore_.updatePrintableArea(
223 new print_preview.PrintableArea(origin, size));
224 } // TODO else create a default printable area?
225
226 var margins = new print_preview.Margins(
227 evt.pageLayout.marginTop,
228 evt.pageLayout.marginRight,
229 evt.pageLayout.marginBottom,
230 evt.pageLayout.marginLeft);
231 if (this.printTicketStore_.hasMarginsCapability()) {
232 this.printTicketStore_.updateCustomMargins(margins);
233 }
234
235 var pageSize = new print_preview.Size(
236 evt.pageLayout.contentWidth + margins.left + margins.right,
237 evt.pageLayout.contentHeight + margins.top + margins.bottom);
238 this.printTicketStore_.updatePageSize(pageSize);
239 },
240
241 /**
242 * Called when the document page count is received from the native layer.
243 * Always occurs as a result of a preview request.
244 * @param {cr.Event} evt Contains the document's page count.
245 * @private
246 */
247 onPageCountChange_: function(evt) {
248 if (this.inFlightRequestId_ != evt.previewResponseId) {
249 return; // Ignore old response.
250 }
251 this.printTicketStore_.updatePageCount(evt.pageCount);
252 },
253
254 /**
255 * Called when the print preview should be reloaded.
256 * @param {cr.Event} evt Contains the preview UID and request ID.
257 * @private
258 */
259 onPreviewReload_: function(evt) {
260 if (this.inFlightRequestId_ != evt.previewResponseId) {
261 return; // Ignore old response.
262 }
263 var pageNumberSet = this.printTicketStore_.getPageNumberSet();
264 for (var i = 0; i < pageNumberSet.size; i++) {
265 var pageNumber = pageNumberSet.getPageNumberAt(i);
266 this.dispatchPageReadyEvent_(i, pageNumber, evt.previewUid);
267 }
268 },
269
270 /**
271 * Called when a page's preview has been generated. Dispatches a
272 * PAGE_READY event.
273 * @param {cr.Event} evt Contains the page index and preview UID.
274 * @private
275 */
276 onPagePreviewReady_: function(evt) {
277 if (this.inFlightRequestId_ != evt.previewResponseId) {
278 return; // Ignore old response.
279 }
280 var pageNumber = evt.pageIndex + 1;
281 if (this.printTicketStore_.getPageNumberSet().hasPageNumber(pageNumber)) {
282 var previewIndex = this.printTicketStore_.getPageNumberSet()
283 .getPageNumberIndex(pageNumber);
284 this.dispatchPageReadyEvent_(previewIndex, pageNumber, evt.previewUid);
285 }
286 },
287
288 /**
289 * Called when the preview generation is complete. Dispatches a
290 * DOCUMENT_READY event.
291 * @param {cr.Event} evt Contains the preview UID and response ID.
292 * @private
293 */
294 onPreviewGenerationDone_: function(evt) {
295 if (this.inFlightRequestId_ != evt.previewResponseId) {
296 return; // Ignore old response.
297 }
298 },
299
300 /**
301 * Called when the preview generation fails.
302 * @private
303 */
304 onPreviewGenerationFail_: function() {
305 // NOTE: No request ID is returned from Chromium so its assumed its the
306 // current one.
307 cr.dispatchSimpleEvent(this, PreviewGenerator.Event.FAIL);
308 },
309
310 /**
311 * @return {boolean} Whether the print ticket has changed sufficiently to
312 * determine whether a new preview request should be issued.
313 * @private
314 */
315 hasPreviewChanged_: function() {
316 var ticketStore = this.printTicketStore_;
317 return this.inFlightRequestId_ == -1 ||
318 ticketStore.isLandscapeEnabled() != this.isLandscapeEnabled_ ||
319 ticketStore.isHeaderFooterEnabled() != this.isHeaderFooterEnabled_ ||
320 ticketStore.isColorEnabled() != this.isColorEnabled_ ||
321 !ticketStore.getPageNumberSet().equals(this.pageNumberSet_) ||
322 ticketStore.getMarginsType() != this.marginsType_ ||
323 (this.marginsType_ == print_preview.Margins.Type.CUSTOM &&
324 !ticketStore.getCustomMargins().equals(this.customMargins_));
325 }
326 };
327
328 // Export
329 return {
330 PreviewGenerator: PreviewGenerator
331 };
332 });
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698