Index: chrome/browser/resources/print_preview/data/destination_store.js |
diff --git a/chrome/browser/resources/print_preview/data/destination_store.js b/chrome/browser/resources/print_preview/data/destination_store.js |
new file mode 100644 |
index 0000000000000000000000000000000000000000..95806f5d35c2ac33e6c841cd4d84e2cd706ad1bc |
--- /dev/null |
+++ b/chrome/browser/resources/print_preview/data/destination_store.js |
@@ -0,0 +1,159 @@ |
+// Copyright (c) 2012 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+cr.define('print_preview', function() { |
+ 'use strict'; |
+ |
+ /** |
+ * A data store that stores destinations and dispatches events when the data |
+ * store changes. |
+ * |
+ * @constructor |
+ * @extends {cr.EventTarget} |
+ */ |
+ function DestinationStore() { |
+ cr.EventTarget.call(this); |
+ |
+ /** |
+ * Internal backing store for the data store. |
+ * @type {Array.<print_preview.Destination>} |
+ * @private |
+ */ |
+ this.destinations_ = []; |
+ |
+ /** |
+ * Currently selected destination. |
+ * @type {print_preview.Destination} |
+ * @private |
+ */ |
+ this.selectedDestination_ = null; |
+ |
+ /** |
+ * Initial destination ID used to auto-select the first inserted destination |
+ * that matches. |
+ * @type {string?} |
+ * @private |
+ */ |
+ this.initialDestinationId_ = null; |
+ |
+ /** |
+ * Initial destination ID used to auto-select the first inserted destination |
+ * that matches. This will be reset when match occurs. |
+ * @type {string?} |
+ * @private |
+ */ |
+ this.autoSelectDestinationId_ = null; |
+ }; |
+ |
+ /** |
+ * Events dispatched by the data store. |
+ * @enum {string} |
+ */ |
+ DestinationStore.Event = { |
+ DESTINATIONS_INSERTED: |
+ 'print_preview.DestinationStore.DESTINATIONS_INSERTED', |
+ DESTINATION_SELECT: 'print_preview.DestinationStore.DESTINATION_SELECT' |
+ }; |
+ |
+ DestinationStore.prototype = { |
+ __proto__: cr.EventTarget.prototype, |
+ |
+ get destinations() { |
+ return this.destinations_.slice(0); |
+ }, |
+ |
+ get selectedDestination() { |
+ return this.selectedDestination_; |
+ }, |
+ |
+ set initialDestinationId(initialDestinationId) { |
+ this.initialDestinationId_ = initialDestinationId; |
+ this.autoSelectDestinationId_ = initialDestinationId; |
+ }, |
+ |
+ /** @param {print_preview.Destination} Destination to select. */ |
+ selectDestination: function(destination) { |
+ if (!destination) { |
+ throw Error('Selected destination must not be null nor undefined'); |
+ } |
+ this.selectedDestination_ = destination; |
+ this.selectedDestination_.isRecent = true; |
+ this.autoSelectDestinationId_ = null; |
+ cr.dispatchSimpleEvent(this, DestinationStore.Event.DESTINATION_SELECT); |
+ }, |
+ |
+ /** |
+ * Inserts a print destination to the data store and dispatches a |
+ * DESTINATIONS_INSERTED event. If the destination matches the initial |
+ * destination ID, then the destination will be automatically selected. |
+ * @param {print_preview.Destination} destination Print destination to |
+ * insert. |
+ */ |
+ insertDestination: function(destination) { |
+ this.destinations_.push(destination); |
+ cr.dispatchSimpleEvent( |
+ this, DestinationStore.Event.DESTINATIONS_INSERTED); |
+ if (this.autoSelectDestinationId_ != null && |
+ destination.id == this.autoSelectDestinationId_) { |
+ this.autoSelectDestinationId_ = null; |
+ this.selectDestination(destination); |
+ } |
+ }, |
+ |
+ /** |
+ * Inserts multiple print destinations to the data store and dispatches one |
+ * DESTINATIONS_INSERTED event. If any of the destinations match the initial |
+ * destination ID, then that destination will be automatically selected. |
+ * @param {Array.<print_preview.Destination>} destinations Print |
+ * destinations to insert. |
+ */ |
+ insertDestinations: function(destinations) { |
+ this.destinations_ = this.destinations_.concat(destinations); |
+ cr.dispatchSimpleEvent( |
+ this, DestinationStore.Event.DESTINATIONS_INSERTED); |
+ if (this.autoSelectDestinationId_ != null) { |
+ for (var dest, i = 0; dest = destinations[i]; i++) { |
+ if (dest.id == this.autoSelectDestinationId_) { |
+ this.autoSelectDestinationId_ = null; |
+ this.selectDestination(dest); |
+ break; |
+ } |
+ } |
+ } |
+ }, |
+ |
+ /** |
+ * Updates an existing print destination with capabilities information. |
+ * @param {print_preview.Destination} destination Destination to update. |
+ * @return {print_preview.Destination} The existing destination that was |
+ * updated. |
+ */ |
+ updateDestination: function(destination) { |
+ var existingDestination = null; |
+ for (var d, i = 0; d = this.destinations_[i]; i++) { |
+ if (destination.id == d.id) { |
+ existingDestination = d; |
+ break; |
+ } |
+ } |
+ if (existingDestination) { |
+ existingDestination.capabilities = destination.capabilities; |
+ return existingDestination; |
+ } else { |
+ throw Error('Print destination not found: ' + destination.id); |
+ } |
+ }, |
+ |
+ /** Clears all print destinations. */ |
+ clear: function() { |
+ this.destinations_ = []; |
+ this.autoSelectDestinationId_ = this.initialDestinationId_; |
+ } |
+ }; |
+ |
+ // Export |
+ return { |
+ DestinationStore: DestinationStore |
+ }; |
+}); |