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

Side by Side Diff: chrome/browser/resources/print_preview/data/print_ticket_store.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 // TODO Maybe clear print ticket when destination changes. Or better yet,
9 // carry over any print ticket state that is possible. I.e. if destination
10 // changes, the new destination might not support duplex anymore, so we should
11 // clear the ticket's isDuplexEnabled state.
12
13 /**
14 * Storage of the print ticket and document statistics. Dispatches events when
15 * the contents of the print ticket or document statistics change. Also
16 * handles validation of the print ticket against destination capabilities and
17 * against the document.
18 * @param {!print_preview.DestinationStore} destinationStore Used to
19 * understand which printer is selected.
20 * @constructor
21 * @extends {cr.EventTarget}
22 */
23 function PrintTicketStore(destinationStore) {
24 cr.EventTarget.call(this);
25
26 /**
27 * Destination store used to understand which printer is selected.
28 * @type {!print_preview.DestinationStore}
29 * @private
30 */
31 this.destinationStore_ = destinationStore;
32
33 // Create the document info with some initial settings. Actual
34 // page-related information won't be set until preview generation occurs,
35 // so we'll use some defaults until then. This way, the print ticket store
36 // will be valid even if no preview can be generated.
37 var initialPageSize = new print_preview.Size(612, 792); // 8.5"x11"
38
39 /**
40 * Information about the document to print.
41 * @type {!print_preview.DocumentInfo}
42 * @private
43 */
44 this.documentInfo_ = new print_preview.DocumentInfo();
45 this.documentInfo_.isModifiable = true;
46 this.documentInfo_.pageCount = 1;
47 this.documentInfo_.pageSize = initialPageSize;
48 this.documentInfo_.printableArea = new print_preview.PrintableArea(
49 new print_preview.Coordinate2d(0, 0), initialPageSize);
50
51 /**
52 * Printing capabilities of Chromium and the currently selected destination.
53 * @type {!print_preview.CapabilitiesHolder}
54 * @private
55 */
56 this.capabilitiesHolder_ = new print_preview.CapabilitiesHolder();
57
58 /**
59 * Current measurement system. Used to work with margin measurements.
60 * @type {!print_preview.MeasurementSystem}
61 * @private
62 */
63 this.measurementSystem_ = new print_preview.MeasurementSystem(
64 ',', '.', print_preview.MeasurementSystem.UnitType.IMPERIAL);
65
66 /**
67 * Collate ticket item.
68 * @type {!print_preview.ticket_items.Collate}
69 * @private
70 */
71 this.collate_ =
72 new print_preview.ticket_items.Collate(this.capabilitiesHolder_);
73
74 /**
75 * Color ticket item.
76 * @type {!print_preview.ticket_items.Color}
77 * @private
78 */
79 this.color_ = new print_preview.ticket_items.Color(
80 this.capabilitiesHolder_, this.destinationStore_);
81
82 /**
83 * Copies ticket item.
84 * @type {!print_preview.ticket_items.Copies}
85 * @private
86 */
87 this.copies_ =
88 new print_preview.ticket_items.Copies(this.capabilitiesHolder_);
89
90 /**
91 * Duplex ticket item.
92 * @type {!print_preview.ticket_items.Duplex}
93 * @private
94 */
95 this.duplex_ =
96 new print_preview.ticket_items.Duplex(this.capabilitiesHolder_);
97
98 /**
99 * Landscape ticket item.
100 * @type {!print_preview.ticket_items.Landscape}
101 * @private
102 */
103 this.landscape_ = new print_preview.ticket_items.Landscape(
104 this.capabilitiesHolder_, this.documentInfo_);
105
106 /**
107 * Page range ticket item.
108 * @type {!print_preview.ticket_items.PageRange}
109 * @private
110 */
111 this.pageRange_ =
112 new print_preview.ticket_items.PageRange(this.documentInfo_);
113
114 /**
115 * Margins type ticket item.
116 * @type {!print_preview.ticket_items.MarginsType}
117 * @private
118 */
119 this.marginsType_ =
120 new print_preview.ticket_items.MarginsType(this.documentInfo_);
121
122 /**
123 * Custom margins ticket item.
124 * @type {!print_preview.ticket_items.CustomMargins}
125 * @private
126 */
127 this.customMargins_ = new print_preview.ticket_items.CustomMargins(
128 this.documentInfo_, this.measurementSystem_);
129
130 /**
131 * Header-footer ticket item.
132 * @type {!print_preview.ticket_items.HeaderFooter}
133 * @private
134 */
135 this.headerFooter_ = new print_preview.ticket_items.HeaderFooter(
136 this.documentInfo_, this.marginsType_, this.customMargins_);
137
138 /**
139 * Fit-to-page ticket item.
140 * @type {!print_preview.ticket_items.FitToPage}
141 * @private
142 */
143 this.fitToPage_ = new print_preview.ticket_items.FitToPage(
144 this.documentInfo_, this.destinationStore_);
145 };
146
147 /**
148 * Event types dispatched by the print ticket store.
149 * @enum {string}
150 */
151 PrintTicketStore.EventType = {
152 CAPABILITIES_CHANGE: 'print_preview.PrintTicketStore.CAPABILITIES_CHANGE',
153 DOCUMENT_CHANGE: 'print_preview.PrintTicketStore.DOCUMENT_CHANGE',
154 INITIALIZE: 'print_preview.PrintTicketStore.INITIALIZE',
155 TICKET_CHANGE: 'print_preview.PrintTicketStore.TICKET_CHANGE'
156 };
157
158 PrintTicketStore.prototype = {
159 __proto__: cr.EventTarget.prototype,
160
161 /** @return {boolean} Whether the document is modifiable. */
162 get isDocumentModifiable() {
163 return this.documentInfo_.isModifiable;
164 },
165
166 /** @return {number} Number of pages in the document. */
167 get pageCount() {
168 return this.documentInfo_.pageCount;
169 },
170
171 /**
172 * @param {number} pageCount New number of pages in the document.
173 * Dispatches a DOCUMENT_CHANGE event if the value changes.
174 */
175 updatePageCount: function(pageCount) {
176 if (this.documentInfo_.pageCount != pageCount) {
177 this.documentInfo_.pageCount = pageCount;
178 cr.dispatchSimpleEvent(
179 this, PrintTicketStore.EventType.DOCUMENT_CHANGE);
180 }
181 },
182
183 /**
184 * @return {!print_preview.PrintableArea} Printable area of the document in
185 * points.
186 */
187 get printableArea() {
188 return this.documentInfo_.printableArea;
189 },
190
191 /**
192 * @param {!print_preview.PrintableArea} printableArea New printable area of
193 * the document in points. Dispatches a DOCUMENT_CHANGE event if the
194 * value changes.
195 */
196 updatePrintableArea: function(printableArea) {
197 if (!this.documentInfo_.printableArea.equals(printableArea)) {
198 this.documentInfo_.printableArea = printableArea;
199 cr.dispatchSimpleEvent(
200 this, PrintTicketStore.EventType.DOCUMENT_CHANGE);
201 }
202 },
203
204 /** @return {!print_preview.Size} Size of the document in points. */
205 get pageSize() {
206 return this.documentInfo_.pageSize;
207 },
208
209 /**
210 * @param {!print_preview.Size} pageSize New size of the document.
211 * Dispatches a DOCUMENT_CHANGE event if the value changes.
212 */
213 updatePageSize: function(pageSize) {
214 if (!this.documentInfo_.pageSize.equals(pageSize)) {
215 this.documentInfo_.pageSize = pageSize;
216 cr.dispatchSimpleEvent(
217 this, PrintTicketStore.EventType.DOCUMENT_CHANGE);
218 }
219 },
220
221 /**
222 * @return {!print_preview.MeasurementSystem} Measurement system of the
223 * local system.
224 */
225 get measurementSystem() {
226 return this.measurementSystem_;
227 },
228
229 /**
230 * Initializes the print ticket store. Dispatches an INITIALIZE event.
231 * @param {boolean} isDocumentModifiable Whether the document to print is
232 * modifiable (i.e. can be re-flowed by Chromium).
233 * @param {?boolean} isDuplexEnabled Previous duplex setting.
234 * @param {?boolean} isHeaderFooterEnabled Previous header-footer setting.
235 * @param {?print_preview.ticket_items.MarginsType.Value} marginsType
236 * Previous margins type.
237 * @param {print_preview.Margins} customMargins Previous custom margins.
238 * @param {string} thousandsDelimeter Delimeter of the thousands place.
239 * @param {string} decimalDelimeter Delimeter of the decimal point.
240 * @param {print_preview.MeasurementSystem.UnitType} unitType Type of unit
241 * of the local measurement system.
242 */
243 initialize: function(
244 isDocumentModifiable,
245 isDuplexEnabled,
246 isHeaderFooterEnabled,
247 marginsType,
248 customMargins,
249 thousandsDelimeter,
250 decimalDelimeter,
251 unitType) {
252
253 this.documentInfo_.isModifiable = isDocumentModifiable;
254 this.measurementSystem_.setSystem(
255 thousandsDelimeter, decimalDelimeter, unitType);
256
257 // Initialize ticket with user's previous values.
258 if (isDuplexEnabled != null) {
259 this.duplex_.updateValue(isDuplexEnabled);
260 }
261 if (isHeaderFooterEnabled != null) {
262 this.headerFooter_.updateValue(isHeaderFooterEnabled);
263 }
264 if (marginsType != null) {
265 this.marginsType_.updateValue(marginsType);
266 }
267 if (customMargins != null) {
268 this.customMargins_.updateValue(customMargins);
269 }
270 },
271
272 /**
273 * Updates the capabilities of the destination the print ticket is for.
274 * Dispatches a CAPABILITIES_CHANGE event.
275 * @param {!print_preview.ChromiumCapabilities} caps New capabilities.
276 */
277 updateDestinationCapabilities: function(caps) {
278 var isFirstUpdate = this.capabilitiesHolder_.get() == null;
279 this.capabilitiesHolder_.set(caps);
280 if (isFirstUpdate) {
281 cr.dispatchSimpleEvent(this, PrintTicketStore.EventType.INITIALIZE);
282 } else {
283 cr.dispatchSimpleEvent(
284 this, PrintTicketStore.EventType.CAPABILITIES_CHANGE);
285 }
286 },
287
288 /** @return {boolean} Whether the ticket store has the copies capability. */
289 hasCopiesCapability: function() {
290 return this.copies_.isCapabilityAvailable();
291 },
292
293 /**
294 * @return {boolean} Whether the string representation of the copies value
295 * currently in the ticket store is valid.
296 */
297 isCopiesValid: function() {
298 return this.copies_.isValid();
299 },
300
301 isCopiesValidForValue: function(value) {
302 return this.copies_.wouldValueBeValid(value);
303 },
304
305 /** @return {number} Number of copies to print. */
306 getCopies: function() {
307 return this.copies_.getValueAsNumber();
308 },
309
310 /**
311 * @return {string} String representation of the number of copies to print.
312 */
313 getCopiesStr: function() {
314 return this.copies_.getValue();
315 },
316
317 /**
318 * Updates the string representation of the number of copies to print.
319 * Dispatches a TICKET_CHANGE event if the string value has changed.
320 * @param {string} New string representation of the number of copies to
321 * print.
322 */
323 updateCopies: function(copies) {
324 if (this.copies_.getValue() != copies) {
325 this.copies_.updateValue(copies);
326 cr.dispatchSimpleEvent(this, PrintTicketStore.EventType.TICKET_CHANGE);
327 }
328 },
329
330 /** @return {boolean} Whether the ticket store has a collate capability. */
331 hasCollateCapability: function() {
332 return this.collate_.isCapabilityAvailable();
333 },
334
335 /** @return {boolean} Whether collate is enabled. */
336 isCollateEnabled: function() {
337 return this.collate_.getValue();
338 },
339
340 /**
341 * Updates whether collate is enabled. Dispatches a TICKET_CHANGE event if
342 * collate has changed.
343 * @param {boolean} isCollateEnabled Whether collate is enabled.
344 */
345 updateCollate: function(isCollateEnabled) {
346 if (this.collate_.getValue() != isCollateEnabled) {
347 this.collate_.updateValue(isCollateEnabled);
348 cr.dispatchSimpleEvent(this, PrintTicketStore.EventType.TICKET_CHANGE);
349 }
350 },
351
352 /**
353 * @return {boolean} Whether the ticket store has color printing capability.
354 */
355 hasColorCapability: function() {
356 return this.color_.isCapabilityAvailable();
357 },
358
359 /** @return {boolean} Whether color printing is enabled. */
360 isColorEnabled: function() {
361 return this.color_.getValue();
362 },
363
364 /**
365 * Updates whether color printing is enabled. Dispatches a TICKET_CHANGE if
366 * color has changed.
367 * @param {boolean} isColorEnabled Whether the color printing is enabled.
368 */
369 updateColor: function(isColorEnabled) {
370 if (this.color_.getValue() != isColorEnabled) {
371 this.color_.updateValue(isColorEnabled);
372 cr.dispatchSimpleEvent(this, PrintTicketStore.EventType.TICKET_CHANGE);
373 }
374 },
375
376 /** @return {boolean} Whether the header-footer capability is available. */
377 hasHeaderFooterCapability: function() {
378 return this.headerFooter_.isCapabilityAvailable();
379 },
380
381 /** @return {boolean} Whether the header-footer setting is enabled. */
382 isHeaderFooterEnabled: function() {
383 return this.headerFooter_.getValue();
384 },
385
386 /**
387 * Updates the whether the header-footer setting is enabled. Dispatches a
388 * TICKET_CHANGE event if the setting changed.
389 * @param {boolean} isHeaderFooterEnabled Whether the header-footer setting
390 * is enabled.
391 */
392 updateHeaderFooter: function(isHeaderFooterEnabled) {
393 if (this.headerFooter_.getValue() != isHeaderFooterEnabled) {
394 this.headerFooter_.updateValue(isHeaderFooterEnabled);
395 cr.dispatchSimpleEvent(this, PrintTicketStore.EventType.TICKET_CHANGE);
396 }
397 },
398
399 /**
400 * @return {boolean} Whether the page orientation capability is available.
401 */
402 hasOrientationCapability: function() {
403 return this.landscape_.isCapabilityAvailable();
404 },
405
406 /**
407 * @return {boolean} Whether the document should be printed in landscape.
408 */
409 isLandscapeEnabled: function() {
410 return this.landscape_.getValue();
411 },
412
413 /**
414 * Updates whether the document should be printed in landscape. Dispatches
415 * a TICKET_CHANGE event if the setting changes.
416 * @param {boolean} isLandscapeEnabled Whether the document should be
417 * printed in landscape.
418 */
419 updateOrientation: function(isLandscapeEnabled) {
420 if (this.landscape_.getValue() != isLandscapeEnabled) {
421 this.landscape_.updateValue(isLandscapeEnabled);
422 // Reset the user set margins.
423 this.customMargins_.updateValue(null);
424 cr.dispatchSimpleEvent(this, PrintTicketStore.EventType.TICKET_CHANGE);
425 }
426 },
427
428 /** @return {boolean} Whether the duplexing capability is available. */
429 hasDuplexCapability: function() {
430 return this.duplex_.isCapabilityAvailable();
431 },
432
433 /** @return {boolean} Whether the document should be printed in duplex. */
434 isDuplexEnabled: function() {
435 return this.duplex_.getValue();
436 },
437
438 /**
439 * Updates the duplexing setting. Dispatches a TICKET_CHANGE event if the
440 * value changes.
441 * @param {boolean} isDuplexEnabled Whether the document should be printed
442 * in duplex.
443 */
444 updateDuplex: function(isDuplexEnabled) {
445 if (this.duplex_.getValue() != isDuplexEnabled) {
446 this.duplex_.updateValue(isDuplexEnabled);
447 cr.dispatchSimpleEvent(this, PrintTicketStore.EventType.TICKET_CHANGE);
448 }
449 },
450
451 /** @return {boolean} Whether the margins capability is available. */
452 hasMarginsCapability: function() {
453 return this.marginsType_.isCapabilityAvailable();
454 },
455
456 /**
457 * @return {print_preview.ticket_items.MarginsType.Value} Type of predefined
458 * margins.
459 */
460 getMarginsType: function() {
461 return this.marginsType_.getValue();
462 },
463
464 /**
465 * Updates the type of predefined margins. Dispatches a TICKET_CHANGE event
466 * if the margins type changes.
467 * @param {print_preview.ticket_items.MarginsType.Value} marginsType Type of
468 * predefined margins.
469 */
470 updateMarginsType: function(marginsType) {
471 if (this.marginsType_.getValue() != marginsType) {
472 this.marginsType_.updateValue(marginsType);
473 if (marginsType ==
474 print_preview.ticket_items.MarginsType.Value.CUSTOM) {
475 // If CUSTOM, set the value of the custom margins so that it won't be
476 // overriden by the default value.
477 this.customMargins_.updateValue(this.customMargins_.getValue());
478 }
479 cr.dispatchSimpleEvent(this, PrintTicketStore.EventType.TICKET_CHANGE);
480 }
481 },
482
483 /** @return {boolean} Whether all of the custom margins are valid. */
484 isCustomMarginsValid: function() {
485 return this.customMargins_.isValid();
486 },
487
488 /**
489 * @return {!print_preview.Margins} Custom margins of the document in
490 * points.
491 */
492 getCustomMargins: function() {
493 return this.customMargins_.getValue();
494 },
495
496 /**
497 * @param {print_preview.ticket_items.CustomMargins.Orientation} orientation
498 * Specifies the margin to get the maximum value for.
499 * @return {number} Maximum value in points of the specified margin.
500 */
501 getCustomMarginMax: function(orientation) {
502 return this.customMargins_.getMarginMax(orientation);
503 },
504
505 /**
506 * Updates the custom margins of the document. Dispatches a TICKET_CHANGE
507 * event if the margins have changed.
508 * @param {!print_preview.Margins} margins New document page margins in
509 * points.
510 */
511 updateCustomMargins: function(margins) {
512 if (!this.isCustomMarginsValid() ||
513 !margins.equals(this.getCustomMargins())) {
514 this.customMargins_.updateValue(margins);
515 cr.dispatchSimpleEvent(this, PrintTicketStore.EventType.TICKET_CHANGE);
516 }
517 },
518
519 /**
520 * Updates a single custom margin's value in points.
521 * @param {print_preview.ticket_items.CustomMargins.Orientation} orientation
522 * Specifies the margin to update.
523 * @param {number} value Updated margin in points.
524 */
525 updateCustomMargin: function(orientation, value) {
526 if (this.customMargins_.getValue().get(orientation) != value) {
527 this.customMargins_.updateMargin(orientation, value);
528 cr.dispatchSimpleEvent(this, PrintTicketStore.EventType.TICKET_CHANGE);
529 }
530 },
531
532 /**
533 * Updates the default custom margins to use before the user makes any
534 * edits.
535 * @param {!print_preview.Margins} margins Updated default margins.
536 */
537 updateDefaultCustomMargins: function(margins) {
538 this.customMargins_.updateDefaultValue(margins);
539 },
540
541 /** @return {boolean} Whether the page range capability is available. */
542 hasPageRangeCapability: function() {
543 return this.pageRange_.isCapabilityAvailable();
544 },
545
546 /**
547 * @return {boolean} Whether the current page range string is defines a
548 * valid page number set.
549 */
550 isPageRangeValid: function() {
551 return this.pageRange_.isValid();
552 },
553
554 /** @return {string} String representation of the page range. */
555 getPageRangeStr: function() {
556 return this.pageRange_.getValue();
557 },
558
559 /**
560 * @return {!print_preview.PageNumberSet} Page number set specified by the
561 * string representation of the page range string.
562 */
563 getPageNumberSet: function() {
564 return this.pageRange_.getPageNumberSet();
565 },
566
567 /**
568 * Updates the page range string. Dispatches a TICKET_CHANGE if the string
569 * changed.
570 * @param {string} pageRangeStr New page range string.
571 */
572 updatePageRange: function(pageRangeStr) {
573 if (this.pageRange_.getValue() != pageRangeStr) {
574 this.pageRange_.updateValue(pageRangeStr);
575 cr.dispatchSimpleEvent(this, PrintTicketStore.EventType.TICKET_CHANGE);
576 }
577 },
578
579 /** @return {boolean} Whether the fit-to-page capability is available. */
580 hasFitToPageCapability: function() {
581 return this.fitToPage_.isCapabilityAvailable();
582 },
583
584 /** @return {boolean} Whether the fit-to-page capability is enabled. */
585 isFitToPageEnabled: function() {
586 return this.fitToPage_.getValue();
587 },
588
589 /**
590 * @param {boolean} isFitToPageEnabled Whether to enable the fit-to-page
591 * capability.
592 */
593 updateFitToPage: function(isFitToPageEnabled) {
594 if (this.fitToPage_.getValue() != isFitToPageEnabled) {
595 this.fitToPage_.updateValue(isFitToPageEnabled);
596 cr.dispatchSimpleEvent(this, PrintTicketStore.EventType.TICKET_CHANGE);
597 }
598 },
599
600 /**
601 * @return {boolean} {@code true} if the stored print ticket is valid,
602 * {@code false} otherwise.
603 */
604 isTicketValid: function() {
605 return (!this.hasCopiesCapability() || this.isCopiesValid()) &&
606 (!this.hasPageRangeCapability() || this.isPageRangeValid()) &&
607 (!this.hasMarginsCapability() ||
608 this.getMarginsType() !=
609 print_preview.ticket_items.MarginsType.Value.CUSTOM ||
610 this.isCustomMarginsValid());
611 }
612 };
613
614 // Export
615 return {
616 PrintTicketStore: PrintTicketStore
617 };
618 });
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698