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.
5 cr.define('print_preview.ticket_items', function() {
9 * An object that represents a user modifiable item in a print ticket. Each
10 * ticket item has a value which can be set by the user. Ticket items can also
11 * be unavailable for modifying if the print destination doesn't support it or
12 * if other ticket item constraints are not met.
13 * @param {?print_preview.AppState} appState Application state model to update
14 * when ticket items update.
15 * @param {?print_preview.AppState.Field} field Field of the app state to
16 * update when ticket item is updated.
17 * @param {?print_preview.DestinationStore} destinationStore Used listen for
18 * changes in the currently selected destination's capabilities. Since
19 * this is a common dependency of ticket items, it's handled in the base
21 * @param {?print_preview.DocumentInfo=} opt_documentInfo Used to listen for
22 * changes in the document. Since this is a common dependency of ticket
23 * items, it's handled in the base class.
25 * @extends {cr.EventTarget}
27 function TicketItem(appState, field, destinationStore, opt_documentInfo) {
28 cr.EventTarget.call(this);
31 * Application state model to update when ticket items update.
32 * @type {print_preview.AppState}
35 this.appState_ = appState || null;
38 * Field of the app state to update when ticket item is updated.
39 * @type {?print_preview.AppState.Field}
42 this.field_ = field || null;
45 * Used listen for changes in the currently selected destination's
47 * @type {print_preview.DestinationStore}
50 this.destinationStore_ = destinationStore || null;
53 * Used to listen for changes in the document.
54 * @type {print_preview.DocumentInfo}
57 this.documentInfo_ = opt_documentInfo || null;
60 * Backing store of the print ticket item.
67 * Keeps track of event listeners for the ticket item.
68 * @type {!EventTracker}
71 this.tracker_ = new EventTracker();
73 this.addEventHandlers_();
77 * Event types dispatched by this class.
80 TicketItem.EventType = {
81 CHANGE: 'print_preview.ticket_items.TicketItem.CHANGE'
84 TicketItem.prototype = {
85 __proto__: cr.EventTarget.prototype,
88 * Determines whether a given value is valid for the ticket item.
89 * @param {?} value The value to check for validity.
90 * @return {boolean} Whether the given value is valid for the ticket item.
92 wouldValueBeValid: function(value) {
93 throw Error('Abstract method not overridden');
97 * @return {boolean} Whether the print destination capability is available.
99 isCapabilityAvailable: function() {
100 throw Error('Abstract method not overridden');
103 /** @return {!Object} The value of the ticket item. */
104 getValue: function() {
105 if (this.isCapabilityAvailable()) {
106 if (this.value_ == null) {
107 return this.getDefaultValueInternal();
112 return this.getCapabilityNotAvailableValueInternal();
116 /** @return {boolean} Whether the ticket item was modified by the user. */
117 isUserEdited: function() {
118 return this.value_ != null;
121 /** @return {boolean} Whether the ticket item's value is valid. */
122 isValid: function() {
123 if (!this.isUserEdited()) {
126 return this.wouldValueBeValid(this.value_);
130 * @param {?} value Value to compare to the value of this ticket item.
131 * @return {boolean} Whether the given value is equal to the value of the
134 isValueEqual: function(value) {
135 return this.getValue() == value;
139 * @param {?} value Value to set as the value of the ticket item.
141 updateValue: function(value) {
142 // Use comparison with capabilities for event.
143 var sendUpdateEvent = !this.isValueEqual(value);
144 // Don't lose requested value if capability is not available.
145 this.updateValueInternal(value);
146 if (this.appState_) {
147 this.appState_.persistField(this.field_, value);
150 cr.dispatchSimpleEvent(this, TicketItem.EventType.CHANGE);
154 * @return {?} Default value of the ticket item if no value was set by
158 getDefaultValueInternal: function() {
159 throw Error('Abstract method not overridden');
163 * @return {?} Default value of the ticket item if the capability is
167 getCapabilityNotAvailableValueInternal: function() {
168 throw Error('Abstract method not overridden');
172 * @return {!EventTracker} Event tracker to keep track of events from
176 getTrackerInternal: function() {
177 return this.tracker_;
181 * @return {print_preview.Destination} Selected destination from the
182 * destination store, or {@code null} if no destination is selected.
185 getSelectedDestInternal: function() {
186 return this.destinationStore_ ?
187 this.destinationStore_.selectedDestination : null;
191 * @return {print_preview.DocumentInfo} Document data model.
194 getDocumentInfoInternal: function() {
195 return this.documentInfo_;
199 * Dispatches a CHANGE event.
202 dispatchChangeEventInternal: function() {
203 cr.dispatchSimpleEvent(
204 this, print_preview.ticket_items.TicketItem.EventType.CHANGE);
208 * Updates the value of the ticket item without dispatching any events or
209 * persisting the value.
212 updateValueInternal: function(value) {
217 * Adds event handlers for this class.
220 addEventHandlers_: function() {
221 if (this.destinationStore_) {
223 this.destinationStore_,
224 print_preview.DestinationStore.EventType.
225 SELECTED_DESTINATION_CAPABILITIES_READY,
226 this.dispatchChangeEventInternal.bind(this));
228 if (this.documentInfo_) {
231 print_preview.DocumentInfo.EventType.CHANGE,
232 this.dispatchChangeEventInternal.bind(this));
239 TicketItem: TicketItem