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. 4 5cr.define('print_preview.ticket_items', function() { 6 'use strict'; 7 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.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 18 * class. 19 * @constructor 20 * @extends {cr.EventTarget} 21 */ 22 function VendorItems(appState, destinationStore) { 23 cr.EventTarget.call(this); 24 25 /** 26 * Application state model to update when ticket items update. 27 * @private {print_preview.AppState} 28 */ 29 this.appState_ = appState || null; 30 31 /** 32 * Used listen for changes in the currently selected destination's 33 * capabilities. 34 * @private {print_preview.DestinationStore} 35 */ 36 this.destinationStore_ = destinationStore || null; 37 38 /** 39 * Vendor ticket items store, maps item id to the item value. 40 * @private {!Object.<string, string>} 41 */ 42 this.items_ = {}; 43 }; 44 45 VendorItems.prototype = { 46 __proto__: cr.EventTarget.prototype, 47 48 /** @return {boolean} Whether vendor capabilities are available. */ 49 isCapabilityAvailable: function() { 50 return !!this.capability; 51 }, 52 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)) 58 return true; 59 } 60 return false; 61 }, 62 63 /** @return {Object} Vendor capabilities of the selected destination. */ 64 get capability() { 65 var destination = this.destinationStore_ ? 66 this.destinationStore_.selectedDestination : null; 67 if (!destination) 68 return null; 69 if (destination.id == print_preview.Destination.GooglePromotedId.FEDEX || 70 destination.type == print_preview.Destination.Type.MOBILE) { 71 return null; 72 } 73 return (destination.capabilities && 74 destination.capabilities.printer && 75 destination.capabilities.printer.vendor_capability) || 76 null; 77 }, 78 79 /** 80 * Vendor ticket items store, maps item id to the item value. 81 * @return {!Object.<string, string>} 82 */ 83 get ticketItems() { 84 return this.items_; 85 }, 86 87 /** 88 * @param {!Object.<string, string>} values Values to set as the values of 89 * vendor ticket items. Maps vendor item id to the value. 90 */ 91 updateValue: function(values) { 92 this.items_ = {}; 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); 98 } 99 } 100 } 101 102 if (this.appState_) { 103 this.appState_.persistField( 104 print_preview.AppState.Field.VENDOR_OPTIONS, this.items_); 105 } 106 } 107 }; 108 109 // Export 110 return { 111 VendorItems: VendorItems 112 }; 113}); 114