OLD | NEW |
(Empty) | |
| 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. |
| 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 * @constructor |
| 14 */ |
| 15 function TicketItem() { |
| 16 /** |
| 17 * Backing store of the print ticket item. |
| 18 * @type {Object} |
| 19 * @private |
| 20 */ |
| 21 this.value_ = null; |
| 22 |
| 23 /** |
| 24 * A cache of whether the ticket item's value is valid. |
| 25 * @type {?boolean} |
| 26 * @private |
| 27 */ |
| 28 this.isValid_ = null; |
| 29 }; |
| 30 |
| 31 TicketItem.prototype = { |
| 32 /** |
| 33 * Determines whether a given value is valid for the ticket item. |
| 34 * @param {Object} value The value to check for validity. |
| 35 * @return {boolean} Whether the given value is valid for the ticket item. |
| 36 */ |
| 37 wouldValueBeValid: function(value) { |
| 38 throw Error('Abstract method not overriden'); |
| 39 }, |
| 40 |
| 41 /** |
| 42 * @return {boolean} Whether the print destination capability is available. |
| 43 */ |
| 44 isCapabilityAvailable: function() { |
| 45 throw Error('Abstract method not overriden'); |
| 46 }, |
| 47 |
| 48 /** @return {object} The value of the ticket item. */ |
| 49 getValue: function() { |
| 50 if (this.isCapabilityAvailable()) { |
| 51 if (this.value_ == null) { |
| 52 return this.getDefaultValueInternal(); |
| 53 } else { |
| 54 return this.value_; |
| 55 } |
| 56 } else { |
| 57 return this.getCapabilityNotAvailableValueInternal(); |
| 58 } |
| 59 }, |
| 60 |
| 61 /** @return {boolean} Whether the ticket item was modified by the user. */ |
| 62 isUserEdited: function() { |
| 63 return this.value_ != null; |
| 64 }, |
| 65 |
| 66 /** @return {boolean} Whether the ticket item's value is valid. */ |
| 67 isValid: function() { |
| 68 if (!this.isUserEdited()) { |
| 69 return true; |
| 70 } |
| 71 if (this.isValid_ == null) { |
| 72 this.isValid_ = this.wouldValueBeValid(this.value_); |
| 73 } |
| 74 return this.isValid_; |
| 75 }, |
| 76 |
| 77 /** @param {object} Value to set as the value of the ticket item. */ |
| 78 updateValue: function(value) { |
| 79 this.value_ = value; |
| 80 this.isValid_ = null; |
| 81 }, |
| 82 |
| 83 /** |
| 84 * @return {object} Default value of the ticket item if no value was set by |
| 85 * the user. |
| 86 * @protected |
| 87 */ |
| 88 getDefaultValueInternal: function() { |
| 89 throw Error('Abstract method not overriden'); |
| 90 }, |
| 91 |
| 92 /** |
| 93 * @return {object} Default value of the ticket item if the capability is |
| 94 * not available. |
| 95 * @protected |
| 96 */ |
| 97 getCapabilityNotAvailableValueInternal: function() { |
| 98 throw Error('Abstract method not overriden'); |
| 99 } |
| 100 }; |
| 101 |
| 102 // Export |
| 103 return { |
| 104 TicketItem: TicketItem |
| 105 }; |
| 106 }); |
OLD | NEW |