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=} 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.
26 function TicketItem(appState, field, destinationStore, documentInfo) {
27 cr.EventTarget.call(this);
30 * Application state model to update when ticket items update.
31 * @type {print_preview.AppState}
34 this.appState_ = appState || null;
37 * Field of the app state to update when ticket item is updated.
38 * @type {print_preview.AppState.Field}
41 this.field_ = field || null;
44 * Used listen for changes in the currently selected destination's
46 * @type {print_preview.DestinationStore}
49 this.destinationStore_ = destinationStore || null;
52 * Used to listen for changes in the document.
53 * @type {print_preview.DocumentInfo}
56 this.documentInfo_ = documentInfo || null;
59 * Backing store of the print ticket item.
66 * Keeps track of event listeners for the ticket item.
67 * @type {!EventTracker}
70 this.tracker_ = new EventTracker();
72 this.addEventHandlers_();
76 * Event types dispatched by this class.
79 TicketItem.EventType = {
80 CHANGE: 'print_preview.ticket_items.TicketItem.CHANGE'
83 TicketItem.prototype = {
84 __proto__: cr.EventTarget.prototype,
87 * Determines whether a given value is valid for the ticket item.
88 * @param {Object} value The value to check for validity.
89 * @return {boolean} Whether the given value is valid for the ticket item.
91 wouldValueBeValid: function(value) {
92 throw Error('Abstract method not overridden');
96 * @return {boolean} Whether the print destination capability is available.
98 isCapabilityAvailable: function() {
99 throw Error('Abstract method not overridden');
102 /** @return {!Object} The value of the ticket item. */
103 getValue: function() {
104 if (this.isCapabilityAvailable()) {
105 if (this.value_ == null) {
106 return this.getDefaultValueInternal();
111 return this.getCapabilityNotAvailableValueInternal();
115 /** @return {boolean} Whether the ticket item was modified by the user. */
116 isUserEdited: function() {
117 return this.value_ != null;
120 /** @return {boolean} Whether the ticket item's value is valid. */
121 isValid: function() {
122 if (!this.isUserEdited()) {
125 return this.wouldValueBeValid(this.value_);
129 * @param {Object} value Value to compare to the value of this ticket item.
130 * @return {boolean} Whether the given value is equal to the value of the
133 isValueEqual: function(value) {
134 return this.getValue() == value;
137 /** @param {!Object} value Value to set as the value of the ticket item. */
138 updateValue: function(value) {
139 // Use comparison with capabilities for event.
140 var sendUpdateEvent = !this.isValueEqual(value);
141 // Don't lose requested value if capability is not available.
142 this.updateValueInternal(value);
143 if (this.appState_) {
144 this.appState_.persistField(this.field_, value);
147 cr.dispatchSimpleEvent(this, TicketItem.EventType.CHANGE);
151 * @return {!Object} Default value of the ticket item if no value was set by
155 getDefaultValueInternal: function() {
156 throw Error('Abstract method not overridden');
160 * @return {!Object} Default value of the ticket item if the capability is
164 getCapabilityNotAvailableValueInternal: function() {
165 throw Error('Abstract method not overridden');
169 * @return {!EventTracker} Event tracker to keep track of events from
173 getTrackerInternal: function() {
174 return this.tracker_;
178 * @return {print_preview.Destination} Selected destination from the
179 * destination store, or {@code null} if no destination is selected.
182 getSelectedDestInternal: function() {
183 return this.destinationStore_ ?
184 this.destinationStore_.selectedDestination : null;
188 * @return {print_preview.DocumentInfo} Document data model.
191 getDocumentInfoInternal: function() {
192 return this.documentInfo_;
196 * Dispatches a CHANGE event.
199 dispatchChangeEventInternal: function() {
200 cr.dispatchSimpleEvent(
201 this, print_preview.ticket_items.TicketItem.EventType.CHANGE);
205 * Updates the value of the ticket item without dispatching any events or
206 * persisting the value.
209 updateValueInternal: function(value) {
214 * Adds event handlers for this class.
217 addEventHandlers_: function() {
218 if (this.destinationStore_) {
220 this.destinationStore_,
221 print_preview.DestinationStore.EventType.
222 SELECTED_DESTINATION_CAPABILITIES_READY,
223 this.dispatchChangeEventInternal.bind(this));
225 if (this.documentInfo_) {
228 print_preview.DocumentInfo.EventType.CHANGE,
229 this.dispatchChangeEventInternal.bind(this));
236 TicketItem: TicketItem