Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(539)

Unified Diff: chrome/browser/resources/print_preview/data/destination_store.js

Issue 10108001: Refactor print preview web ui (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fixes broken tests Created 8 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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..3fd392e3157f414ce20698c6bcd0a8ec0cf49e55
--- /dev/null
+++ b/chrome/browser/resources/print_preview/data/destination_store.js
@@ -0,0 +1,176 @@
+// 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;
+ };
+
+ /**
+ * Event types dispatched by the data store.
+ * @enum {string}
+ */
+ DestinationStore.EventType = {
+ DESTINATIONS_INSERTED:
+ 'print_preview.DestinationStore.DESTINATIONS_INSERTED',
+ DESTINATION_SELECT: 'print_preview.DestinationStore.DESTINATION_SELECT'
+ };
+
+ DestinationStore.prototype = {
+ __proto__: cr.EventTarget.prototype,
+
+ /**
+ * @return {!Array.<!print_preview.Destination>} List of destinations in
+ * the store.
+ */
+ get destinations() {
+ return this.destinations_.slice(0);
+ },
+
+ /**
+ * @return {print_preview.Destination} The currently selected destination or
+ * {@code null} if none is selected.
+ */
+ get selectedDestination() {
+ return this.selectedDestination_;
+ },
+
+ /**
+ * Sets the initially selected destination. If any inserted destinations
+ * match this ID, that destination will be automatically selected. This
+ * occurs only once for every time this setter is called or if the store is
+ * cleared.
+ * @param {string} ID of the destination that should be selected
+ * automatically when added to the store.
+ */
+ 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.EventType.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.EventType.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.EventType.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.selectedDestination_ = null;
+ this.autoSelectDestinationId_ = this.initialDestinationId_;
+ }
+ };
+
+ // Export
+ return {
+ DestinationStore: DestinationStore
+ };
+});

Powered by Google App Engine
This is Rietveld 408576698