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

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: 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 // TODO(rltoscano): Maybe clear print ticket when destination changes. Or
9 // better yet, carry over any print ticket state that is possible. I.e. if
10 // destination changes, the new destination might not support duplex anymore,
11 // so we should 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 /** @return {!print_preview.Size} Size of the document in points. */
192 get pageSize() {
193 return this.documentInfo_.pageSize;
194 },
195
196 /**
197 * Updates a subset of fields of the print document relating to the format
198 * of the page.
199 * @param {!print_preview.PrintableArea} printableArea New printable area of
200 * the document in points. Dispatches a DOCUMENT_CHANGE event if the
201 * value changes.
202 * @param {!print_preview.Size} pageSize New size of the document in points.
203 * Dispatches a DOCUMENT_CHANGE event if the value changes.
204 * @param {boolean} documentHasCssMediaStyles Whether the document is styled
205 * with CSS media styles.
206 * @param {!print_preview.Margins} margins Document margins in points.
207 */
208 updateDocumentPageInfo: function(
209 printableArea, pageSize, documentHasCssMediaStyles, margins) {
210 if (!this.documentInfo_.printableArea.equals(printableArea) ||
211 !this.documentInfo_.pageSize.equals(pageSize) ||
212 this.documentInfo_.hasCssMediaStyles != documentHasCssMediaStyles ||
213 this.documentInfo_.margins == null ||
214 !this.documentInfo_.margins.equals(margins)) {
215 this.documentInfo_.printableArea = printableArea;
216 this.documentInfo_.pageSize = pageSize;
217 this.documentInfo_.hasCssMediaStyles = documentHasCssMediaStyles;
218 this.documentInfo_.margins = margins;
219 cr.dispatchSimpleEvent(
220 this, PrintTicketStore.EventType.DOCUMENT_CHANGE);
221 }
222 },
223
224 /**
225 * @return {!print_preview.MeasurementSystem} Measurement system of the
226 * local system.
227 */
228 get measurementSystem() {
229 return this.measurementSystem_;
230 },
231
232 /**
233 * @return {print_preview.Margins} Document margins of the currently
234 * generated preview.
235 */
236 getDocumentMargins: function() {
237 return this.documentInfo_.margins;
238 },
239
240 /**
241 * Initializes the print ticket store. Dispatches an INITIALIZE event.
242 * @param {boolean} isDocumentModifiable Whether the document to print is
243 * modifiable (i.e. can be re-flowed by Chromium).
244 * @param {?boolean} isDuplexEnabled Previous duplex setting.
245 * @param {?boolean} isHeaderFooterEnabled Previous header-footer setting.
246 * @param {?print_preview.ticket_items.MarginsType.Value} marginsType
247 * Previous margins type.
248 * @param {print_preview.Margins} customMargins Previous custom margins.
249 * @param {string} thousandsDelimeter Delimeter of the thousands place.
250 * @param {string} decimalDelimeter Delimeter of the decimal point.
251 * @param {print_preview.MeasurementSystem.UnitType} unitType Type of unit
252 * of the local measurement system.
253 */
254 initialize: function(
255 isDocumentModifiable,
256 isDuplexEnabled,
257 isHeaderFooterEnabled,
258 marginsType,
259 customMargins,
260 thousandsDelimeter,
261 decimalDelimeter,
262 unitType) {
263
264 this.documentInfo_.isModifiable = isDocumentModifiable;
265 this.measurementSystem_.setSystem(
266 thousandsDelimeter, decimalDelimeter, unitType);
267
268 // Initialize ticket with user's previous values.
269 this.duplex_.updateValue(isDuplexEnabled);
270 this.headerFooter_.updateValue(isHeaderFooterEnabled);
271 if (marginsType != null) {
272 this.marginsType_.updateValue(marginsType);
273 }
274 if (customMargins != null) {
275 this.customMargins_.updateValue(customMargins);
276 }
277 },
278
279 /**
280 * Updates the capabilities of the destination the print ticket is for.
281 * Dispatches a CAPABILITIES_CHANGE event.
282 * @param {!print_preview.ChromiumCapabilities} caps New capabilities.
283 */
284 updateDestinationCapabilities: function(caps) {
285 var isFirstUpdate = this.capabilitiesHolder_.get() == null;
286 this.capabilitiesHolder_.set(caps);
287 if (isFirstUpdate) {
288 cr.dispatchSimpleEvent(this, PrintTicketStore.EventType.INITIALIZE);
289 } else {
290 this.customMargins_.updateValue(null);
291 if (this.marginsType_.getValue() ==
292 print_preview.ticket_items.MarginsType.Value.CUSTOM) {
293 this.marginsType_.updateValue(
294 print_preview.ticket_items.MarginsType.Value.DEFAULT);
295 }
296 cr.dispatchSimpleEvent(
297 this, PrintTicketStore.EventType.CAPABILITIES_CHANGE);
298 }
299 },
300
301 /** @return {boolean} Whether the ticket store has the copies capability. */
302 hasCopiesCapability: function() {
303 return this.copies_.isCapabilityAvailable();
304 },
305
306 /**
307 * @return {boolean} Whether the string representation of the copies value
308 * currently in the ticket store is valid.
309 */
310 isCopiesValid: function() {
311 return this.copies_.isValid();
312 },
313
314 isCopiesValidForValue: function(value) {
315 return this.copies_.wouldValueBeValid(value);
316 },
317
318 /** @return {number} Number of copies to print. */
319 getCopies: function() {
320 return this.copies_.getValueAsNumber();
321 },
322
323 /**
324 * @return {string} String representation of the number of copies to print.
325 */
326 getCopiesStr: function() {
327 return this.copies_.getValue();
328 },
329
330 /**
331 * Updates the string representation of the number of copies to print.
332 * Dispatches a TICKET_CHANGE event if the string value has changed.
333 * @param {string} New string representation of the number of copies to
334 * print.
335 */
336 updateCopies: function(copies) {
337 if (this.copies_.getValue() != copies) {
338 this.copies_.updateValue(copies);
339 cr.dispatchSimpleEvent(this, PrintTicketStore.EventType.TICKET_CHANGE);
340 }
341 },
342
343 /** @return {boolean} Whether the ticket store has a collate capability. */
344 hasCollateCapability: function() {
345 return this.collate_.isCapabilityAvailable();
346 },
347
348 /** @return {boolean} Whether collate is enabled. */
349 isCollateEnabled: function() {
350 return this.collate_.getValue();
351 },
352
353 /**
354 * Updates whether collate is enabled. Dispatches a TICKET_CHANGE event if
355 * collate has changed.
356 * @param {boolean} isCollateEnabled Whether collate is enabled.
357 */
358 updateCollate: function(isCollateEnabled) {
359 if (this.collate_.getValue() != isCollateEnabled) {
360 this.collate_.updateValue(isCollateEnabled);
361 cr.dispatchSimpleEvent(this, PrintTicketStore.EventType.TICKET_CHANGE);
362 }
363 },
364
365 /**
366 * @return {boolean} Whether the ticket store has color printing capability.
367 */
368 hasColorCapability: function() {
369 return this.color_.isCapabilityAvailable();
370 },
371
372 /** @return {boolean} Whether color printing is enabled. */
373 isColorEnabled: function() {
374 return this.color_.getValue();
375 },
376
377 /**
378 * Updates whether color printing is enabled. Dispatches a TICKET_CHANGE if
379 * color has changed.
380 * @param {boolean} isColorEnabled Whether the color printing is enabled.
381 */
382 updateColor: function(isColorEnabled) {
383 if (this.color_.getValue() != isColorEnabled) {
384 this.color_.updateValue(isColorEnabled);
385 cr.dispatchSimpleEvent(this, PrintTicketStore.EventType.TICKET_CHANGE);
386 }
387 },
388
389 /** @return {boolean} Whether the header-footer capability is available. */
390 hasHeaderFooterCapability: function() {
391 return this.headerFooter_.isCapabilityAvailable();
392 },
393
394 /** @return {boolean} Whether the header-footer setting is enabled. */
395 isHeaderFooterEnabled: function() {
396 return this.headerFooter_.getValue();
397 },
398
399 /**
400 * Updates the whether the header-footer setting is enabled. Dispatches a
401 * TICKET_CHANGE event if the setting changed.
402 * @param {boolean} isHeaderFooterEnabled Whether the header-footer setting
403 * is enabled.
404 */
405 updateHeaderFooter: function(isHeaderFooterEnabled) {
406 if (this.headerFooter_.getValue() != isHeaderFooterEnabled) {
407 this.headerFooter_.updateValue(isHeaderFooterEnabled);
408 cr.dispatchSimpleEvent(this, PrintTicketStore.EventType.TICKET_CHANGE);
409 }
410 },
411
412 /**
413 * @return {boolean} Whether the page orientation capability is available.
414 */
415 hasOrientationCapability: function() {
416 return this.landscape_.isCapabilityAvailable();
417 },
418
419 /**
420 * @return {boolean} Whether the document should be printed in landscape.
421 */
422 isLandscapeEnabled: function() {
423 return this.landscape_.getValue();
424 },
425
426 /**
427 * Updates whether the document should be printed in landscape. Dispatches
428 * a TICKET_CHANGE event if the setting changes.
429 * @param {boolean} isLandscapeEnabled Whether the document should be
430 * printed in landscape.
431 */
432 updateOrientation: function(isLandscapeEnabled) {
433 if (this.landscape_.getValue() != isLandscapeEnabled) {
434 this.landscape_.updateValue(isLandscapeEnabled);
435 // Reset the user set margins.
436 this.marginsType_.updateValue(
437 print_preview.ticket_items.MarginsType.Value.DEFAULT);
438 this.customMargins_.updateValue(null);
439 cr.dispatchSimpleEvent(this, PrintTicketStore.EventType.TICKET_CHANGE);
440 }
441 },
442
443 /** @return {boolean} Whether the duplexing capability is available. */
444 hasDuplexCapability: function() {
445 return this.duplex_.isCapabilityAvailable();
446 },
447
448 /** @return {boolean} Whether the document should be printed in duplex. */
449 isDuplexEnabled: function() {
450 return this.duplex_.getValue();
451 },
452
453 /**
454 * Updates the duplexing setting. Dispatches a TICKET_CHANGE event if the
455 * value changes.
456 * @param {boolean} isDuplexEnabled Whether the document should be printed
457 * in duplex.
458 */
459 updateDuplex: function(isDuplexEnabled) {
460 if (this.duplex_.getValue() != isDuplexEnabled) {
461 this.duplex_.updateValue(isDuplexEnabled);
462 cr.dispatchSimpleEvent(this, PrintTicketStore.EventType.TICKET_CHANGE);
463 }
464 },
465
466 /** @return {boolean} Whether the margins capability is available. */
467 hasMarginsCapability: function() {
468 return this.marginsType_.isCapabilityAvailable();
469 },
470
471 /**
472 * @return {print_preview.ticket_items.MarginsType.Value} Type of predefined
473 * margins.
474 */
475 getMarginsType: function() {
476 return this.marginsType_.getValue();
477 },
478
479 /**
480 * Updates the type of predefined margins. Dispatches a TICKET_CHANGE event
481 * if the margins type changes.
482 * @param {print_preview.ticket_items.MarginsType.Value} marginsType Type of
483 * predefined margins.
484 */
485 updateMarginsType: function(marginsType) {
486 if (this.marginsType_.getValue() != marginsType) {
487 this.marginsType_.updateValue(marginsType);
488 if (marginsType ==
489 print_preview.ticket_items.MarginsType.Value.CUSTOM) {
490 // If CUSTOM, set the value of the custom margins so that it won't be
491 // overridden by the default value.
492 this.customMargins_.updateValue(this.customMargins_.getValue());
493 }
494 cr.dispatchSimpleEvent(this, PrintTicketStore.EventType.TICKET_CHANGE);
495 }
496 },
497
498 /** @return {boolean} Whether all of the custom margins are valid. */
499 isCustomMarginsValid: function() {
500 return this.customMargins_.isValid();
501 },
502
503 /**
504 * @return {!print_preview.Margins} Custom margins of the document in
505 * points.
506 */
507 getCustomMargins: function() {
508 return this.customMargins_.getValue();
509 },
510
511 /**
512 * @param {print_preview.ticket_items.CustomMargins.Orientation} orientation
513 * Specifies the margin to get the maximum value for.
514 * @return {number} Maximum value in points of the specified margin.
515 */
516 getCustomMarginMax: function(orientation) {
517 return this.customMargins_.getMarginMax(orientation);
518 },
519
520 /**
521 * Updates the custom margins of the document. Dispatches a TICKET_CHANGE
522 * event if the margins have changed.
523 * @param {!print_preview.Margins} margins New document page margins in
524 * points.
525 */
526 updateCustomMargins: function(margins) {
527 if (!this.isCustomMarginsValid() ||
528 !margins.equals(this.getCustomMargins())) {
529 this.customMargins_.updateValue(margins);
530 cr.dispatchSimpleEvent(this, PrintTicketStore.EventType.TICKET_CHANGE);
531 }
532 },
533
534 /**
535 * Updates a single custom margin's value in points.
536 * @param {print_preview.ticket_items.CustomMargins.Orientation} orientation
537 * Specifies the margin to update.
538 * @param {number} value Updated margin in points.
539 */
540 updateCustomMargin: function(orientation, value) {
541 if (this.customMargins_.getValue().get(orientation) != value) {
542 this.customMargins_.updateMargin(orientation, value);
543 cr.dispatchSimpleEvent(this, PrintTicketStore.EventType.TICKET_CHANGE);
544 }
545 },
546
547 /** @return {boolean} Whether the page range capability is available. */
548 hasPageRangeCapability: function() {
549 return this.pageRange_.isCapabilityAvailable();
550 },
551
552 /**
553 * @return {boolean} Whether the current page range string is defines a
554 * valid page number set.
555 */
556 isPageRangeValid: function() {
557 return this.pageRange_.isValid();
558 },
559
560 /** @return {string} String representation of the page range. */
561 getPageRangeStr: function() {
562 return this.pageRange_.getValue();
563 },
564
565 /**
566 * @return {!print_preview.PageNumberSet} Page number set specified by the
567 * string representation of the page range string.
568 */
569 getPageNumberSet: function() {
570 return this.pageRange_.getPageNumberSet();
571 },
572
573 /**
574 * Updates the page range string. Dispatches a TICKET_CHANGE if the string
575 * changed.
576 * @param {string} pageRangeStr New page range string.
577 */
578 updatePageRange: function(pageRangeStr) {
579 if (this.pageRange_.getValue() != pageRangeStr) {
580 this.pageRange_.updateValue(pageRangeStr);
581 cr.dispatchSimpleEvent(this, PrintTicketStore.EventType.TICKET_CHANGE);
582 }
583 },
584
585 /** @return {boolean} Whether the fit-to-page capability is available. */
586 hasFitToPageCapability: function() {
587 return this.fitToPage_.isCapabilityAvailable();
588 },
589
590 /** @return {boolean} Whether the fit-to-page capability is enabled. */
591 isFitToPageEnabled: function() {
592 return this.fitToPage_.getValue();
593 },
594
595 /**
596 * @param {boolean} isFitToPageEnabled Whether to enable the fit-to-page
597 * capability.
598 */
599 updateFitToPage: function(isFitToPageEnabled) {
600 if (this.fitToPage_.getValue() != isFitToPageEnabled) {
601 this.fitToPage_.updateValue(isFitToPageEnabled);
602 cr.dispatchSimpleEvent(this, PrintTicketStore.EventType.TICKET_CHANGE);
603 }
604 },
605
606 /**
607 * @return {boolean} {@code true} if the stored print ticket is valid,
608 * {@code false} otherwise.
609 */
610 isTicketValid: function() {
611 return this.isTicketValidForPreview() &&
612 (!this.hasPageRangeCapability() || this.isPageRangeValid());
613 },
614
615 /** @return {boolean} Whether the ticket is valid for preview generation. */
616 isTicketValidForPreview: function() {
617 return (!this.hasCopiesCapability() || this.isCopiesValid()) &&
618 (!this.hasMarginsCapability() ||
619 this.getMarginsType() !=
620 print_preview.ticket_items.MarginsType.Value.CUSTOM ||
621 this.isCustomMarginsValid());
622 }
623 };
624
625 // Export
626 return {
627 PrintTicketStore: PrintTicketStore
628 };
629 });
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698