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

Side by Side 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 unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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.
34 * @type {?string}
35 * @private
36 */
37 this.initialDestinationId_ = null;
38
39 /**
40 * Initial destination ID used to auto-select the first inserted destination
41 * that matches. This will be reset when match occurs.
42 * @type {?string}
43 * @private
44 */
45 this.autoSelectDestinationId_ = null;
46 };
47
48 /**
49 * Event types dispatched by the data store.
50 * @enum {string}
51 */
52 DestinationStore.EventType = {
53 DESTINATIONS_INSERTED:
54 'print_preview.DestinationStore.DESTINATIONS_INSERTED',
55 DESTINATION_SELECT: 'print_preview.DestinationStore.DESTINATION_SELECT'
56 };
57
58 DestinationStore.prototype = {
59 __proto__: cr.EventTarget.prototype,
60
61 /**
62 * @return {!Array.<!print_preview.Destination>} List of destinations in
63 * the store.
64 */
65 get destinations() {
66 return this.destinations_.slice(0);
67 },
68
69 /**
70 * @return {print_preview.Destination} The currently selected destination or
71 * {@code null} if none is selected.
72 */
73 get selectedDestination() {
74 return this.selectedDestination_;
75 },
76
77 /**
78 * Sets the initially selected destination. If any inserted destinations
79 * match this ID, that destination will be automatically selected. This
80 * occurs only once for every time this setter is called or if the store is
81 * cleared.
82 * @param {string} ID of the destination that should be selected
83 * automatically when added to the store.
84 */
85 set initialDestinationId(initialDestinationId) {
86 this.initialDestinationId_ = initialDestinationId;
87 this.autoSelectDestinationId_ = initialDestinationId;
88 },
89
90 /** @param {!print_preview.Destination} Destination to select. */
91 selectDestination: function(destination) {
92 if (!destination) {
93 throw Error('Selected destination must not be null nor undefined');
94 }
95 this.selectedDestination_ = destination;
96 this.selectedDestination_.isRecent = true;
97 this.autoSelectDestinationId_ = null;
98 cr.dispatchSimpleEvent(
99 this, DestinationStore.EventType.DESTINATION_SELECT);
100 },
101
102 /**
103 * Inserts a print destination to the data store and dispatches a
104 * DESTINATIONS_INSERTED event. If the destination matches the initial
105 * destination ID, then the destination will be automatically selected.
106 * @param {!print_preview.Destination} destination Print destination to
107 * insert.
108 */
109 insertDestination: function(destination) {
110 this.destinations_.push(destination);
111 cr.dispatchSimpleEvent(
112 this, DestinationStore.EventType.DESTINATIONS_INSERTED);
113 if (this.autoSelectDestinationId_ != null &&
114 destination.id == this.autoSelectDestinationId_) {
115 this.autoSelectDestinationId_ = null;
116 this.selectDestination(destination);
117 }
118 },
119
120 /**
121 * Inserts multiple print destinations to the data store and dispatches one
122 * DESTINATIONS_INSERTED event. If any of the destinations match the initial
123 * destination ID, then that destination will be automatically selected.
124 * @param {!Array.<print_preview.Destination>} destinations Print
125 * destinations to insert.
126 */
127 insertDestinations: function(destinations) {
128 this.destinations_ = this.destinations_.concat(destinations);
129 cr.dispatchSimpleEvent(
130 this, DestinationStore.EventType.DESTINATIONS_INSERTED);
131 if (this.autoSelectDestinationId_ != null) {
132 for (var dest, i = 0; dest = destinations[i]; i++) {
133 if (dest.id == this.autoSelectDestinationId_) {
134 this.autoSelectDestinationId_ = null;
135 this.selectDestination(dest);
136 break;
137 }
138 }
139 }
140 },
141
142 /**
143 * Updates an existing print destination with capabilities information.
144 * @param {!print_preview.Destination} destination Destination to update.
145 * @return {!print_preview.Destination} The existing destination that was
146 * updated.
147 */
148 updateDestination: function(destination) {
149 var existingDestination = null;
150 for (var d, i = 0; d = this.destinations_[i]; i++) {
151 if (destination.id == d.id) {
152 existingDestination = d;
153 break;
154 }
155 }
156 if (existingDestination) {
157 existingDestination.capabilities = destination.capabilities;
158 return existingDestination;
159 } else {
160 throw Error('Print destination not found: ' + destination.id);
161 }
162 },
163
164 /** Clears all print destinations. */
165 clear: function() {
166 this.destinations_ = [];
167 this.selectedDestination_ = null;
168 this.autoSelectDestinationId_ = this.initialDestinationId_;
169 }
170 };
171
172 // Export
173 return {
174 DestinationStore: DestinationStore
175 };
176 });
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698