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', function() { |
| 6 'use strict'; |
| 7 |
| 8 /** |
| 9 * A data store that stores destinations and dispatches events when the data |
| 10 * store changes. |
| 11 * @constructor |
| 12 * @extends {cr.EventTarget} |
| 13 */ |
| 14 function DestinationStore() { |
| 15 cr.EventTarget.call(this); |
| 16 |
| 17 /** |
| 18 * Internal backing store for the data store. |
| 19 * @type {!Array.<print_preview.Destination>} |
| 20 * @private |
| 21 */ |
| 22 this.destinations_ = []; |
| 23 |
| 24 /** |
| 25 * Currently selected destination. |
| 26 * @type {print_preview.Destination} |
| 27 * @private |
| 28 */ |
| 29 this.selectedDestination_ = null; |
| 30 |
| 31 /** |
| 32 * Initial destination ID used to auto-select the first inserted destination |
| 33 * that matches. If {@code null}, the first destination inserted into the |
| 34 * store will be selected. |
| 35 * @type {?string} |
| 36 * @private |
| 37 */ |
| 38 this.initialDestinationId_ = null; |
| 39 |
| 40 /** |
| 41 * Whether the destination store will auto select the destination that |
| 42 * matches the initial destination. |
| 43 * @type {boolean} |
| 44 * @private |
| 45 */ |
| 46 this.isInAutoSelectMode_ = false; |
| 47 }; |
| 48 |
| 49 /** |
| 50 * Event types dispatched by the data store. |
| 51 * @enum {string} |
| 52 */ |
| 53 DestinationStore.EventType = { |
| 54 DESTINATIONS_INSERTED: |
| 55 'print_preview.DestinationStore.DESTINATIONS_INSERTED', |
| 56 DESTINATION_SELECT: 'print_preview.DestinationStore.DESTINATION_SELECT' |
| 57 }; |
| 58 |
| 59 DestinationStore.prototype = { |
| 60 __proto__: cr.EventTarget.prototype, |
| 61 |
| 62 /** |
| 63 * @return {!Array.<!print_preview.Destination>} List of destinations in |
| 64 * the store. |
| 65 */ |
| 66 get destinations() { |
| 67 return this.destinations_.slice(0); |
| 68 }, |
| 69 |
| 70 /** |
| 71 * @return {print_preview.Destination} The currently selected destination or |
| 72 * {@code null} if none is selected. |
| 73 */ |
| 74 get selectedDestination() { |
| 75 return this.selectedDestination_; |
| 76 }, |
| 77 |
| 78 /** |
| 79 * Sets the initially selected destination. If any inserted destinations |
| 80 * match this ID, that destination will be automatically selected. This |
| 81 * occurs only once for every time this setter is called or if the store is |
| 82 * cleared. |
| 83 * @param {string} ID of the destination that should be selected |
| 84 * automatically when added to the store. |
| 85 */ |
| 86 setInitialDestinationId: function(initialDestinationId) { |
| 87 this.initialDestinationId_ = initialDestinationId; |
| 88 this.isInAutoSelectMode_ = true; |
| 89 if (this.initialDestinationId_ == null && this.destinations_.length > 0) { |
| 90 this.selectDestination(this.destinations_[0]); |
| 91 } else if (this.initialDestinationId_ != null) { |
| 92 for (var dest, i = 0; dest = this.destinations_[i]; i++) { |
| 93 if (dest.id == initialDestinationId) { |
| 94 this.selectDestination(dest); |
| 95 break; |
| 96 } |
| 97 } |
| 98 } |
| 99 }, |
| 100 |
| 101 /** @param {!print_preview.Destination} Destination to select. */ |
| 102 selectDestination: function(destination) { |
| 103 this.selectedDestination_ = destination; |
| 104 this.selectedDestination_.isRecent = true; |
| 105 this.isInAutoSelectMode_ = false; |
| 106 cr.dispatchSimpleEvent( |
| 107 this, DestinationStore.EventType.DESTINATION_SELECT); |
| 108 }, |
| 109 |
| 110 /** |
| 111 * Inserts a print destination to the data store and dispatches a |
| 112 * DESTINATIONS_INSERTED event. If the destination matches the initial |
| 113 * destination ID, then the destination will be automatically selected. |
| 114 * @param {!print_preview.Destination} destination Print destination to |
| 115 * insert. |
| 116 */ |
| 117 insertDestination: function(destination) { |
| 118 this.destinations_.push(destination); |
| 119 cr.dispatchSimpleEvent( |
| 120 this, DestinationStore.EventType.DESTINATIONS_INSERTED); |
| 121 if (this.isInAutoSelectMode_) { |
| 122 if (this.initialDestinationId_ == null) { |
| 123 this.selectDestination(destination); |
| 124 } else { |
| 125 if (destination.id == this.initialDestinationId_) { |
| 126 this.selectDestination(destination); |
| 127 } |
| 128 } |
| 129 } |
| 130 }, |
| 131 |
| 132 /** |
| 133 * Inserts multiple print destinations to the data store and dispatches one |
| 134 * DESTINATIONS_INSERTED event. If any of the destinations match the initial |
| 135 * destination ID, then that destination will be automatically selected. |
| 136 * @param {!Array.<print_preview.Destination>} destinations Print |
| 137 * destinations to insert. |
| 138 */ |
| 139 insertDestinations: function(destinations) { |
| 140 this.destinations_ = this.destinations_.concat(destinations); |
| 141 cr.dispatchSimpleEvent( |
| 142 this, DestinationStore.EventType.DESTINATIONS_INSERTED); |
| 143 if (this.isInAutoSelectMode_) { |
| 144 if (this.initialDestinationId_ == null && destinations.length > 0) { |
| 145 this.selectDestination(destinations[0]); |
| 146 } else if (this.initialDestinationId_ != null) { |
| 147 for (var dest, i = 0; dest = destinations[i]; i++) { |
| 148 if (dest.id == this.initialDestinationId_) { |
| 149 this.selectDestination(dest); |
| 150 break; |
| 151 } |
| 152 } |
| 153 } |
| 154 } |
| 155 }, |
| 156 |
| 157 /** |
| 158 * Updates an existing print destination with capabilities information. If |
| 159 * the destination doesn't already exist, it will be added. |
| 160 * @param {!print_preview.Destination} destination Destination to update. |
| 161 * @return {!print_preview.Destination} The existing destination that was |
| 162 * updated. |
| 163 */ |
| 164 updateDestination: function(destination) { |
| 165 var existingDestination = null; |
| 166 for (var d, i = 0; d = this.destinations_[i]; i++) { |
| 167 if (destination.id == d.id) { |
| 168 existingDestination = d; |
| 169 break; |
| 170 } |
| 171 } |
| 172 if (existingDestination) { |
| 173 existingDestination.capabilities = destination.capabilities; |
| 174 return existingDestination; |
| 175 } else { |
| 176 this.insertDestination(destination); |
| 177 } |
| 178 }, |
| 179 |
| 180 /** Clears all print destinations. */ |
| 181 clear: function() { |
| 182 this.destinations_ = []; |
| 183 this.selectedDestination_ = null; |
| 184 this.isInAutoSelectMode_ = true; |
| 185 } |
| 186 }; |
| 187 |
| 188 // Export |
| 189 return { |
| 190 DestinationStore: DestinationStore |
| 191 }; |
| 192 }); |
OLD | NEW |