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

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

Issue 10450022: Print Preview Print Destination Search Widget (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Addresses CSS and style comments. Created 8 years, 6 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
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 * Component used for searching for a print destination.
10 * This is a modal dialog that allows the user to search and select a
11 * destination to print to. When a destination is selected, it is written to
12 * the destination store.
13 * @param {!print_preview.DestinationStore} destinationStore Data store
14 * containing the destinations to search through.
15 * @param {!print_preview.UserInfo} userInfo Event target that contains
16 * information about the logged in user.
17 * @constructor
18 * @extends {print_preview.Component}
19 */
20 function DestinationSearch(destinationStore, userInfo) {
21 print_preview.Component.call(this);
22
23 /**
24 * Data store containing the destinations to search through.
25 * @type {!print_preview.DestinationStore}
26 * @private
27 */
28 this.destinationStore_ = destinationStore;
29
30 /**
31 * Event target that contains information about the logged in user.
32 * @type {!print_preview.UserInfo}
33 * @private
34 */
35 this.userInfo_ = userInfo;
36
37 /**
38 * Search box used to search through the destination lists.
39 * @type {!print_preview.SearchBox}
40 * @private
41 */
42 this.searchBox_ = new print_preview.SearchBox();
43 this.addChild(this.searchBox_);
44
45 /**
46 * Destination list containing recent destinations.
47 * @type {!print_preview.DestinationList}
48 * @private
49 */
50 this.recentList_ = new print_preview.DestinationList(
51 this,
52 localStrings.getString('recentDestinationsTitle'),
53 3 /*opt_maxSize*/);
54 this.addChild(this.recentList_);
55
56 /**
57 * Destination list containing local destinations.
58 * @type {!print_preview.DestinationList}
59 * @private
60 */
61 this.localList_ = new print_preview.DestinationList(
62 this,
63 localStrings.getString('localDestinationsTitle'),
64 0 /*opt_maxSize*/,
65 localStrings.getString('manageLocalPrinters'));
66 this.addChild(this.localList_);
67
68 /**
69 * Destination list containing cloud destinations.
70 * @type {!print_preview.DestinationList}
71 * @private
72 */
73 this.cloudList_ = new print_preview.CloudDestinationList(this);
74 this.addChild(this.cloudList_);
75
76 /**
77 * Page element of the overlay.
78 * @type {HTMLElement}
Dan Beam 2012/05/29 23:36:10 ?HTMLElement
Robert Toscano 2012/05/30 21:08:00 The '?' is extraneous.
79 * @private
80 */
81 this.pageEl_ = null;
82 };
83
84 /**
85 * Event types dispatched by the component.
86 * @enum {string}
87 */
88 DestinationSearch.EventType = {
89 // Dispatched when the user requests to manage their cloud destinations.
90 MANAGE_CLOUD_DESTINATIONS:
91 'print_preview.DestinationSearch.MANAGE_CLOUD_DESTINATIONS',
92
93 // Dispatched when the user requests to manage their local destinations.
94 MANAGE_LOCAL_DESTINATIONS:
95 'print_preview.DestinationSearch.MANAGE_LOCAL_DESTINATIONS',
96
97 // Dispatched when the user requests to sign-in to their Google account.
98 SIGN_IN: 'print_preview.DestinationSearch.SIGN_IN'
99 },
100
101 /**
102 * CSS classes used by the component.
103 * @enum {string}
104 * @private
105 */
106 DestinationSearch.Classes_ = {
107 CLOSE_BUTTON: 'destination-search-close-button',
108 CLOUDPRINT_PROMO: 'destination-search-cloudprint-promo',
109 CLOUDPRINT_PROMO_CLOSE_BUTTON:
110 'destination-search-cloudprint-promo-close-button',
111 CLOUD_LIST: 'destination-search-cloud-list',
112 LOCAL_LIST: 'destination-search-local-list',
113 PAGE: 'destination-search-page',
114 PULSE: 'pulse',
115 RECENT_LIST: 'destination-search-recent-list',
116 SEARCH_BOX_CONTAINER: 'destination-search-search-box-container',
117 SIGN_IN: 'destination-search-sign-in',
118 TRANSPARENT: 'transparent',
119 USER_EMAIL: 'destination-search-user-email',
120 USER_INFO: 'destination-search-user-info'
121 };
122
123 DestinationSearch.prototype = {
124 __proto__: print_preview.Component.prototype,
125
126 /** @return {boolean} Whether the component is visible. */
127 getIsVisible: function() {
128 return !this.getElement().classList.contains(
129 DestinationSearch.Classes_.TRANSPARENT);
130 },
131
132 /** @param {boolean} isVisible Whether the component is visible. */
133 setIsVisible: function(isVisible) {
134 if (isVisible) {
135 this.searchBox_.focus();
136 this.getElement().classList.remove(
137 DestinationSearch.Classes_.TRANSPARENT);
138 } else {
139 this.getElement().classList.add(DestinationSearch.Classes_.TRANSPARENT);
140 // Collapse all destination lists
141 this.localList_.setIsShowAll(false);
142 this.cloudList_.setIsShowAll(false);
143 this.searchBox_.setQuery('');
144 this.filterLists_(null);
145 }
146 },
147
148 /** @param {string} email Email of the logged-in user. */
149 setCloudPrintEmail: function(email) {
150 var userEmailEl = this.getElement().getElementsByClassName(
151 DestinationSearch.Classes_.USER_EMAIL)[0];
152 userEmailEl.textContent = email;
153 var userInfoEl = this.getElement().getElementsByClassName(
154 DestinationSearch.Classes_.USER_INFO)[0];
155 setIsVisible(userInfoEl, true);
156 var cloudListEl = this.getElement().getElementsByClassName(
157 DestinationSearch.Classes_.CLOUD_LIST)[0];
158 setIsVisible(cloudListEl, true);
159 var promoEl = this.getElement().getElementsByClassName(
160 DestinationSearch.Classes_.CLOUDPRINT_PROMO)[0];
161 setIsVisible(promoEl, false);
162 },
163
164 /** Shows the Google Cloud Print promotion banner. */
165 showCloudPrintPromo: function() {
166 var promoEl = this.getElement().getElementsByClassName(
167 DestinationSearch.Classes_.CLOUDPRINT_PROMO)[0];
168 setIsVisible(promoEl, true);
169 },
170
171 /** @override */
172 enterDocument: function() {
173 print_preview.Component.prototype.enterDocument.call(this);
174 var closeButtonEl = this.getElement().getElementsByClassName(
175 DestinationSearch.Classes_.CLOSE_BUTTON)[0];
176 var signInButton = this.getElement().getElementsByClassName(
177 DestinationSearch.Classes_.SIGN_IN)[0];
178 var cloudprintPromoCloseButton = this.getElement().getElementsByClassName(
179 DestinationSearch.Classes_.CLOUDPRINT_PROMO_CLOSE_BUTTON)[0];
180
181 this.tracker.add(
182 closeButtonEl, 'click', this.onCloseClick_.bind(this));
183 this.tracker.add(
184 signInButton, 'click', this.onSignInActivated_.bind(this));
185 this.tracker.add(
186 cloudprintPromoCloseButton,
187 'click',
188 this.onCloudprintPromoCloseButtonClick_.bind(this));
189 this.tracker.add(
190 this.searchBox_,
191 print_preview.SearchBox.EventType.SEARCH,
192 this.onSearch_.bind(this));
193 this.tracker.add(
194 this,
195 print_preview.DestinationListItem.EventType.SELECT,
196 this.onDestinationSelect_.bind(this));
197
198 this.tracker.add(
199 this.destinationStore_,
200 print_preview.DestinationStore.EventType.DESTINATIONS_INSERTED,
201 this.onDestinationsInserted_.bind(this));
202 this.tracker.add(
203 this.destinationStore_,
204 print_preview.DestinationStore.EventType.DESTINATION_SELECT,
205 this.onDestinationStoreSelect_.bind(this));
206
207 this.tracker.add(
208 this.localList_,
209 print_preview.DestinationList.EventType.ACTION_LINK_ACTIVATED,
210 this.onManageLocalDestinationsActivated_.bind(this));
211 this.tracker.add(
212 this.cloudList_,
213 print_preview.DestinationList.EventType.ACTION_LINK_ACTIVATED,
214 this.onManageCloudDestinationsActivated_.bind(this));
215
216 this.tracker.add(
217 this.getElement(), 'click', this.onClick_.bind(this));
218 this.tracker.add(
219 this.pageEl_, 'webkitAnimationEnd', this.onAnimationEnd_.bind(this));
220
221 this.tracker.add(
222 this.userInfo_,
223 print_preview.UserInfo.EventType.EMAIL_CHANGE,
224 this.onEmailChange_.bind(this));
225 },
226
227 /** @override */
228 exitDocument: function() {
229 print_preview.Component.prototype.exitDocument.call(this);
230 this.pageEl_ = null;
231 },
232
233 /** @override */
234 decorateInternal: function() {
235 this.searchBox_.decorate($('search-box'));
236
237 var recentListEl = this.getElement().getElementsByClassName(
238 DestinationSearch.Classes_.RECENT_LIST)[0];
239 this.recentList_.render(recentListEl);
240
241 var localListEl = this.getElement().getElementsByClassName(
242 DestinationSearch.Classes_.LOCAL_LIST)[0];
243 this.localList_.render(localListEl);
244
245 var cloudListEl = this.getElement().getElementsByClassName(
246 DestinationSearch.Classes_.CLOUD_LIST)[0];
247 this.cloudList_.render(cloudListEl);
248
249 this.pageEl_ = this.getElement().getElementsByClassName(
250 DestinationSearch.Classes_.PAGE)[0];
251 },
252
253 /**
254 * Filters all destination lists with the given query.
255 * @param {?string} query Query to filter destination lists by.
256 * @private
257 */
258 filterLists_: function(query) {
259 this.recentList_.filter(query);
260 this.localList_.filter(query);
261 this.cloudList_.filter(query);
262 },
263
264 /**
265 * Resets the search query.
266 * @private
267 */
268 resetSearch_: function() {
269 this.searchBox_.setQuery(null);
270 this.filterLists_(null);
271 },
272
273 /**
274 * Called when a destination search should be executed. Filters the
275 * destination lists with the given query.
276 * @param {cr.Event} evt Contains the search query.
277 * @private
278 */
279 onSearch_: function(evt) {
280 this.filterLists_(evt.query);
281 },
282
283 /**
284 * Called when the close button is clicked. Hides the search widget.
285 * @private
286 */
287 onCloseClick_: function() {
288 this.setIsVisible(false);
289 this.resetSearch_();
290 },
291
292 /**
293 * Called when a destination is selected. Clears the search and hides the
294 * widget.
295 * @param {cr.Event} evt Contains the selected destination.
296 * @private
297 */
298 onDestinationSelect_: function(evt) {
299 this.setIsVisible(false);
300 this.resetSearch_();
301 this.destinationStore_.selectDestination(evt.destination);
302 },
303
304 /**
305 * Called when destinations are added to the destination store. Refreshes UI
306 * with new destinations.
307 * @private
308 */
309 onDestinationsInserted_: function() {
310 var destinations = this.destinationStore_.destinations;
311 var recentDestinations = [];
312 var localDestinations = [];
313 var cloudDestinations = [];
314 destinations.forEach(function(destination) {
315 if (destination.isRecent) {
316 recentDestinations.push(destination);
317 }
318 if (destination.isLocal) {
319 localDestinations.push(destination);
320 } else {
321 cloudDestinations.push(destination);
322 }
323 });
324 this.recentList_.updateDestinations(recentDestinations);
325 this.localList_.updateDestinations(localDestinations);
326 this.cloudList_.updateDestinations(cloudDestinations);
327 },
328
329 /**
330 * Called when a destination is selected. Selected destination are marked as
331 * recent, so we have to update our recent destinations list.
332 * @private
333 */
334 onDestinationStoreSelect_: function() {
335 var destinations = this.destinationStore_.destinations;
336 var recentDestinations = [];
337 destinations.forEach(function(destination) {
338 if (destination.isRecent) {
339 recentDestinations.push(destination);
340 }
341 });
342 this.recentList_.updateDestinations(recentDestinations);
343 },
344
345 /**
346 * Called when the manage cloud printers action is activated.
347 * @private
348 */
349 onManageCloudDestinationsActivated_: function() {
350 cr.dispatchSimpleEvent(
351 this,
352 print_preview.DestinationSearch.EventType.MANAGE_CLOUD_DESTINATIONS);
353 },
354
355 /**
356 * Called when the manage local printers action is activated.
357 * @private
358 */
359 onManageLocalDestinationsActivated_: function() {
360 cr.dispatchSimpleEvent(
361 this,
362 print_preview.DestinationSearch.EventType.MANAGE_LOCAL_DESTINATIONS);
363 },
364
365 /**
366 * Called when the "Sign in" link on the Google Cloud Print promo is
367 * activated.
368 * @private
369 */
370 onSignInActivated_: function() {
371 cr.dispatchSimpleEvent(this, DestinationSearch.EventType.SIGN_IN);
372 },
373
374 /**
375 * Called when the close button on the cloud print promo is clicked. Hides
376 * the promo.
377 * @private
378 */
379 onCloudprintPromoCloseButtonClick_: function() {
380 var promoEl = this.getElement().getElementsByClassName(
381 DestinationSearch.Classes_.CLOUDPRINT_PROMO)[0];
382 setIsVisible(promoEl, false);
383 },
384
385 /**
386 * Called when the overlay is clicked. Pulses the page.
387 * @param {Event} event Contains the element that was clicked.
388 * @private
389 */
390 onClick_: function(event) {
391 if (event.target == this.getElement()) {
392 this.pageEl_.classList.add(DestinationSearch.Classes_.PULSE);
393 }
394 },
395
396 /**
397 * Called when an animation ends on the page.
398 * @private
399 */
400 onAnimationEnd_: function() {
401 this.pageEl_.classList.remove(DestinationSearch.Classes_.PULSE);
402 },
403
404 /**
405 * Called when the user's email field has changed. Updates the UI.
406 * @private
407 */
408 onEmailChange_: function() {
409 var userEmail = this.userInfo_.getUserEmail();
410 if (userEmail) {
411 this.setCloudPrintEmail(userEmail);
412 }
413 }
414 };
415
416 // Export
417 return {
418 DestinationSearch: DestinationSearch
419 };
420 });
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698