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

Side by Side Diff: chrome/browser/resources/print_preview/search/destination_search.js

Issue 10108001: Refactor print preview web ui (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 8 years, 8 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 // TODO Make sub classes for this widget, one for chrome (logged in and not
6 // logged in) and one for chrome OS.
7
8 cr.define('print_preview', function() {
9 'use strict';
10
11 /**
12 * Component used for searching for a print destination.
13 *
14 * This is a modal dialog that allows the user to search and select a
15 * destination to print to. When a destination is selected, it is written to
16 * the destination store.
17 *
18 * @param {!print_preview.DestinationStore} destinationStore Data store
19 * containing the destinations to search through.
20 * @constructor
21 * @extends {print_preview.Component}
22 */
23 function DestinationSearch(destinationStore) {
24 print_preview.Component.call(this);
25
26 /**
27 * Data store containing the destinations to search through.
28 * @type {!print_preview.DestinationStore}
29 * @private
30 */
31 this.destinationStore_ = destinationStore;
32
33 /**
34 * Search box used to search through the destination lists.
35 * @type {print_preview.SearchBox}
36 * @private
37 */
38 this.searchBox_ = new print_preview.SearchBox();
39 this.addChild(this.searchBox_);
40
41 /**
42 * Destination list containing recent destinations.
43 * @type {print_preview.DestinationList}
44 * @private
45 */
46 this.recentList_ = new print_preview.DestinationList(
47 this, 'Recent Destinations', 3);
48 this.addChild(this.recentList_);
49
50 /**
51 * Destination list containing local destinations.
52 * TODO Internationalize link text
53 * @type {print_preview.DestinationList}
54 * @private
55 */
56 this.localList_ = new print_preview.DestinationList(
57 this,
58 'Local Destinations',
59 0 /*opt_maxSize*/,
60 'Manage Local Printers...');
61 this.addChild(this.localList_);
62
63 /**
64 * Destination list containing cloud destinations.
65 * TODO Internationalize link text
66 * @type {print_preview.DestinationList}
67 * @private
68 */
69 this.cloudList_ = new print_preview.DestinationList(
70 this,
71 'Cloud Destinations',
72 0 /*opt_maxSize*/,
73 'Manage Google Cloud Print...');
74 this.addChild(this.cloudList_);
75 };
76
77 DestinationSearch.Event = {
78 SIGN_IN: 'print_preview.DestinationSearch.SIGN_IN'
79 },
80
81 /**
82 * CSS classes used by the search widget.
83 * @enum {string}
84 * @private
85 */
86 DestinationSearch.Classes_ = {
87 CLOSE_BUTTON: 'destination-search-close-button',
88 CLOUDPRINT_PROMO: 'destination-search-cloudprint-promo',
89 CLOUD_LIST: 'destination-search-cloud-list',
90 LOCAL_LIST: 'destination-search-local-list',
91 RECENT_LIST: 'destination-search-recent-list',
92 SEARCH_BOX_CONTAINER: 'destination-search-search-box-container',
93 SIGN_IN: 'destination-search-sign-in',
94 USER_EMAIL: 'destination-search-user-email',
95 USER_INFO: 'destination-search-user-info'
96 };
97
98 /**
99 * Chrome API ID of the manage cloud printers action.
100 * @type {string}
101 * @private
102 */
103 DestinationSearch.MANAGE_CLOUD_PRINTERS_ACTION_ = 'manageCloudPrinters';
104
105 /**
106 * Chrome API ID of the manage local printers action.
107 * @type {string}
108 * @private
109 */
110 DestinationSearch.MANAGE_LOCAL_PRINTERS_ACTION_ = 'manageLocalPrinters';
111
112 DestinationSearch.prototype = {
113 __proto__: print_preview.Component.prototype,
114
115 get isVisible() {
116 return this.getElement().style.display == '';
117 },
118
119 set isVisible(isVisible) {
120 this.getElement().style.display = isVisible ? '' : 'none';
121 if (!isVisible) {
122 // Collapse all destination lists
123 this.localList_.isShowAll = false;
124 this.cloudList_.isShowAll = false;
125 }
126 },
127
128 set cloudPrintEmail(email) {
129 var userEmailEl = this.getElement().getElementsByClassName(
130 DestinationSearch.Classes_.USER_EMAIL)[0];
131 userEmailEl.textContent = email;
132 var userInfoEl = this.getElement().getElementsByClassName(
133 DestinationSearch.Classes_.USER_INFO)[0];
134 userInfoEl.style.display = '';
135 var cloudListEl = this.getElement().getElementsByClassName(
136 DestinationSearch.Classes_.CLOUD_LIST)[0];
137 cloudListEl.style.display = '';
138 var promoEl = this.getElement().getElementsByClassName(
139 DestinationSearch.Classes_.CLOUDPRINT_PROMO)[0];
140 promoEl.style.display = 'none';
141 },
142
143 showCloudPrintPromo: function() {
144 var promoEl = this.getElement().getElementsByClassName(
145 DestinationSearch.Classes_.CLOUDPRINT_PROMO)[0];
146 promoEl.style.display = '';
147 },
148
149 /** @override */
150 decorateInternal: function() {
151 this.searchBox_.decorate($('search-box'));
152
153 var recentListEl = this.getElement().getElementsByClassName(
154 DestinationSearch.Classes_.RECENT_LIST)[0];
155 this.recentList_.render(recentListEl);
156
157 var localListEl = this.getElement().getElementsByClassName(
158 DestinationSearch.Classes_.LOCAL_LIST)[0];
159 this.localList_.render(localListEl);
160
161 var cloudListEl = this.getElement().getElementsByClassName(
162 DestinationSearch.Classes_.CLOUD_LIST)[0];
163 this.cloudList_.render(cloudListEl);
164 },
165
166 /** @override */
167 enterDocument: function() {
168 print_preview.Component.prototype.enterDocument.call(this);
169 var closeButtonEl = this.getElement().getElementsByClassName(
170 DestinationSearch.Classes_.CLOSE_BUTTON)[0];
171 var signInButton = this.getElement().getElementsByClassName(
172 DestinationSearch.Classes_.SIGN_IN)[0];
173
174 this.tracker.add(
175 closeButtonEl, 'click', this.onCloseClick_.bind(this));
176 this.tracker.add(
177 signInButton, 'click', this.onSignInActivated_.bind(this));
178 this.tracker.add(
179 this.searchBox_,
180 print_preview.SearchBox.Event.SEARCH,
181 this.onSearch_.bind(this));
182 this.tracker.add(
183 this,
184 print_preview.DestinationListItem.Event.SELECT,
185 this.onDestinationSelect_.bind(this));
186
187 this.tracker.add(
188 this.destinationStore_,
189 print_preview.DestinationStore.Event.DESTINATIONS_INSERTED,
190 this.onDestinationsInserted_.bind(this));
191 this.tracker.add(
192 this.destinationStore_,
193 print_preview.DestinationStore.Event.DESTINATION_SELECT,
194 this.onDestinationStoreSelect_.bind(this));
195
196 this.tracker.add(
197 this.localList_,
198 print_preview.DestinationList.Event.ACTION_LINK_ACTIVATED,
199 this.onManageLocalPrintersActivated_.bind(this));
200 this.tracker.add(
201 this.cloudList_,
202 print_preview.DestinationList.Event.ACTION_LINK_ACTIVATED,
203 this.onManageCloudPrintersActivated_.bind(this));
204 },
205
206 /**
207 * Filters all destination lists with the given query.
208 * @param {string?} query Query to filter destination lists by.
209 * @private
210 */
211 filterLists_: function(query) {
212 this.recentList_.filter(query);
213 this.localList_.filter(query);
214 this.cloudList_.filter(query);
215 },
216
217 /**
218 * Resets the search query.
219 * @private
220 */
221 resetSearch_: function() {
222 this.searchBox_.query = null;
223 this.filterLists_(null);
224 },
225
226 /**
227 * Called when a destination search should be executed. Filters the
228 * destination lists with the given query.
229 * @param {cr.Event} evt Contains the search query.
230 * @private
231 */
232 onSearch_: function(evt) {
233 this.filterLists_(evt.query);
234 },
235
236 /**
237 * Called when the close button is clicked. Hides the search widget.
238 * @private
239 */
240 onCloseClick_: function() {
241 this.isVisible = false;
242 this.resetSearch_();
243 },
244
245 /**
246 * Called when a destination is selected. Clears the search and hides the
247 * widget.
248 * @param {cr.Event} evt Contains the selected destination.
249 * @private
250 */
251 onDestinationSelect_: function(evt) {
252 this.isVisible = false;
253 this.resetSearch_();
254 this.destinationStore_.selectDestination(evt.destination);
255 },
256
257 /**
258 * Called when destinations are added to the destination store. Refreshes UI
259 * with new destinations.
260 * @private
261 */
262 onDestinationsInserted_: function() {
263 var destinations = this.destinationStore_.destinations;
264 var recentDestinations = [];
265 var localDestinations = [];
266 var cloudDestinations = [];
267 for (var destination, i = 0; destination = destinations[i]; i++) {
268 if (destination.isRecent) {
269 recentDestinations.push(destination);
270 }
271 if (destination.isLocal) {
272 localDestinations.push(destination);
273 } else {
274 cloudDestinations.push(destination);
275 }
276 }
277 this.recentList_.updateDestinations(recentDestinations);
278 this.localList_.updateDestinations(localDestinations);
279 this.cloudList_.updateDestinations(cloudDestinations);
280 },
281
282 /**
283 * Called when a destination is selected. Selected destination are marked as
284 * recent, so we have to update our recent destinations list.
285 * @private
286 */
287 onDestinationStoreSelect_: function() {
288 var destinations = this.destinationStore_.destinations;
289 var recentDestinations = [];
290 for (var destination, i = 0; destination = destinations[i]; i++) {
291 if (destination.isRecent) {
292 recentDestinations.push(destination);
293 }
294 }
295 this.recentList_.updateDestinations(recentDestinations);
296 },
297
298 /**
299 * Called when the manage cloud printers action is activated.
300 * @private
301 */
302 onManageCloudPrintersActivated_: function() {
303 cr.dispatchSimpleEvent(
304 document, print_preview.CommonEvent.MANAGE_CLOUD_PRINTERS);
305 },
306
307 /**
308 * Called when the manage local printers action is activated.
309 * @private
310 */
311 onManageLocalPrintersActivated_: function() {
312 cr.dispatchSimpleEvent(
313 document, print_preview.CommonEvent.MANAGE_LOCAL_PRINTERS);
314 },
315
316 /**
317 * Called when the "Sign in" link on the Google Cloud Print promo is
318 * activated.
319 * @private
320 */
321 onSignInActivated_: function() {
322 cr.dispatchSimpleEvent(this, DestinationSearch.Event.SIGN_IN);
323 }
324 };
325
326 // Export
327 return {
328 DestinationSearch: DestinationSearch
329 };
330 });
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698