Allow only one bookmark to be added for multiple fast starring
[chromium-blink-merge.git] / chrome / browser / resources / print_preview / data / ticket_items / ticket_item.js
bloba6d8824c572f603ed00630445a3b2336129666cf
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() {
6 'use strict';
8 /**
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
20 * class.
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.
24 * @constructor
25 * @extends {cr.EventTarget}
27 function TicketItem(appState, field, destinationStore, opt_documentInfo) {
28 cr.EventTarget.call(this);
30 /**
31 * Application state model to update when ticket items update.
32 * @type {print_preview.AppState}
33 * @private
35 this.appState_ = appState || null;
37 /**
38 * Field of the app state to update when ticket item is updated.
39 * @type {?print_preview.AppState.Field}
40 * @private
42 this.field_ = field || null;
44 /**
45 * Used listen for changes in the currently selected destination's
46 * capabilities.
47 * @type {print_preview.DestinationStore}
48 * @private
50 this.destinationStore_ = destinationStore || null;
52 /**
53 * Used to listen for changes in the document.
54 * @type {print_preview.DocumentInfo}
55 * @private
57 this.documentInfo_ = opt_documentInfo || null;
59 /**
60 * Backing store of the print ticket item.
61 * @type {Object}
62 * @private
64 this.value_ = null;
66 /**
67 * Keeps track of event listeners for the ticket item.
68 * @type {!EventTracker}
69 * @private
71 this.tracker_ = new EventTracker();
73 this.addEventHandlers_();
76 /**
77 * Event types dispatched by this class.
78 * @enum {string}
80 TicketItem.EventType = {
81 CHANGE: 'print_preview.ticket_items.TicketItem.CHANGE'
84 TicketItem.prototype = {
85 __proto__: cr.EventTarget.prototype,
87 /**
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');
96 /**
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();
108 } else {
109 return this.value_;
111 } else {
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()) {
124 return true;
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
132 * ticket item.
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);
149 if (sendUpdateEvent)
150 cr.dispatchSimpleEvent(this, TicketItem.EventType.CHANGE);
154 * @return {?} Default value of the ticket item if no value was set by
155 * the user.
156 * @protected
158 getDefaultValueInternal: function() {
159 throw Error('Abstract method not overridden');
163 * @return {?} Default value of the ticket item if the capability is
164 * not available.
165 * @protected
167 getCapabilityNotAvailableValueInternal: function() {
168 throw Error('Abstract method not overridden');
172 * @return {!EventTracker} Event tracker to keep track of events from
173 * dependencies.
174 * @protected
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.
183 * @protected
185 getSelectedDestInternal: function() {
186 return this.destinationStore_ ?
187 this.destinationStore_.selectedDestination : null;
191 * @return {print_preview.DocumentInfo} Document data model.
192 * @protected
194 getDocumentInfoInternal: function() {
195 return this.documentInfo_;
199 * Dispatches a CHANGE event.
200 * @protected
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.
210 * @protected
212 updateValueInternal: function(value) {
213 this.value_ = value;
217 * Adds event handlers for this class.
218 * @private
220 addEventHandlers_: function() {
221 if (this.destinationStore_) {
222 this.tracker_.add(
223 this.destinationStore_,
224 print_preview.DestinationStore.EventType.
225 SELECTED_DESTINATION_CAPABILITIES_READY,
226 this.dispatchChangeEventInternal.bind(this));
228 if (this.documentInfo_) {
229 this.tracker_.add(
230 this.documentInfo_,
231 print_preview.DocumentInfo.EventType.CHANGE,
232 this.dispatchChangeEventInternal.bind(this));
237 // Export
238 return {
239 TicketItem: TicketItem