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

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

Powered by Google App Engine
This is Rietveld 408576698