OLD | NEW |
(Empty) | |
| 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 |
| 5 cr.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 values) { |
| 57 if (values.hasOwnProperty(key)) |
| 58 return true; |
| 59 } |
| 60 return false; |
| 61 }, |
| 62 |
| 63 /** @return {Object} Media size capability of the selected destination. */ |
| 64 get capability() { |
| 65 var destination = this.destinationStore_ ? |
| 66 this.destinationStore_.selectedDestination : null; |
| 67 return (destination && |
| 68 destination.capabilities && |
| 69 destination.capabilities.printer && |
| 70 destination.capabilities.printer.vendor_capability) || |
| 71 null; |
| 72 }, |
| 73 |
| 74 /** |
| 75 * Vendor ticket items store, maps item id to the item value. |
| 76 * @return {!Object.<string, string>} |
| 77 */ |
| 78 get ticketItems() { |
| 79 return this.items_; |
| 80 }, |
| 81 |
| 82 /** |
| 83 * @param {!Object.<string, string>} values Values to set as the values of |
| 84 * vendor ticket items. Maps vendor item id to the value. |
| 85 */ |
| 86 updateValue: function(values) { |
| 87 this.items_ = {}; |
| 88 if (typeof values == 'object') { |
| 89 for (var key in values) { |
| 90 if (values.hasOwnProperty(key) && typeof values[key] == 'string') { |
| 91 // Let's empirically limit each value at 2K. |
| 92 this.items_[key] = values[key].substring(0, 2048); |
| 93 } |
| 94 } |
| 95 } |
| 96 |
| 97 if (this.appState_) { |
| 98 this.appState_.persistField( |
| 99 print_preview.AppState.Field.VENDOR_OPTIONS, this.items_); |
| 100 } |
| 101 } |
| 102 }; |
| 103 |
| 104 // Export |
| 105 return { |
| 106 VendorItems: VendorItems |
| 107 }; |
| 108 }); |
OLD | NEW |