1 // Copyright 2014 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.DestinationStore} destinationStore Used listen for
16 * changes in the currently selected destination's capabilities. Since
17 * this is a common dependency of ticket items, it's handled in the base
20 * @extends {cr.EventTarget}
22 function VendorItems(appState, destinationStore) {
23 cr.EventTarget.call(this);
26 * Application state model to update when ticket items update.
27 * @private {print_preview.AppState}
29 this.appState_ = appState || null;
32 * Used listen for changes in the currently selected destination's
34 * @private {print_preview.DestinationStore}
36 this.destinationStore_ = destinationStore || null;
39 * Vendor ticket items store, maps item id to the item value.
40 * @private {!Object<string>}
45 VendorItems.prototype = {
46 __proto__: cr.EventTarget.prototype,
48 /** @return {boolean} Whether vendor capabilities are available. */
49 isCapabilityAvailable: function() {
50 return !!this.capability;
53 /** @return {boolean} Whether the ticket item was modified by the user. */
54 isUserEdited: function() {
55 // If there's at least one ticket item stored in values, it was edited.
56 for (var key in this.items_) {
57 if (this.items_.hasOwnProperty(key))
63 /** @return {Object} Vendor capabilities of the selected destination. */
65 var destination = this.destinationStore_ ?
66 this.destinationStore_.selectedDestination : null;
69 if (destination.id == print_preview.Destination.GooglePromotedId.FEDEX ||
70 destination.type == print_preview.Destination.Type.MOBILE) {
73 return (destination.capabilities &&
74 destination.capabilities.printer &&
75 destination.capabilities.printer.vendor_capability) ||
80 * Vendor ticket items store, maps item id to the item value.
81 * @return {!Object<string>}
88 * @param {!Object<string>} values Values to set as the values of vendor
89 * ticket items. Maps vendor item id to the value.
91 updateValue: function(values) {
93 if (typeof values == 'object') {
94 for (var key in values) {
95 if (values.hasOwnProperty(key) && typeof values[key] == 'string') {
96 // Let's empirically limit each value at 2K.
97 this.items_[key] = values[key].substring(0, 2048);
102 if (this.appState_) {
103 this.appState_.persistField(
104 print_preview.AppState.Field.VENDOR_OPTIONS, this.items_);
111 VendorItems: VendorItems