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.
5 cr.define('print_preview', function() {
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 * @param {!print_preview.Metrics} metrics Used to record usage statistics.
19 * @extends {print_preview.Component}
21 function DestinationSearch(destinationStore, userInfo, metrics) {
22 print_preview.Component.call(this);
25 * Data store containing the destinations to search through.
26 * @type {!print_preview.DestinationStore}
29 this.destinationStore_ = destinationStore;
32 * Event target that contains information about the logged in user.
33 * @type {!print_preview.UserInfo}
36 this.userInfo_ = userInfo;
39 * Used to record usage statistics.
40 * @type {!print_preview.Metrics}
43 this.metrics_ = metrics;
46 * Search box used to search through the destination lists.
47 * @type {!print_preview.SearchBox}
50 this.searchBox_ = new print_preview.SearchBox();
51 this.addChild(this.searchBox_);
54 * Destination list containing recent destinations.
55 * @type {!print_preview.DestinationList}
58 this.recentList_ = new print_preview.RecentDestinationList(this);
59 this.addChild(this.recentList_);
62 * Destination list containing local destinations.
63 * @type {!print_preview.DestinationList}
66 this.localList_ = new print_preview.DestinationList(
68 localStrings.getString('localDestinationsTitle'),
69 cr.isChromeOS ? null : localStrings.getString('manage'));
70 this.addChild(this.localList_);
73 * Destination list containing cloud destinations.
74 * @type {!print_preview.DestinationList}
77 this.cloudList_ = new print_preview.CloudDestinationList(this);
78 this.addChild(this.cloudList_);
82 * Event types dispatched by the component.
85 DestinationSearch.EventType = {
86 // Dispatched when the user requests to manage their cloud destinations.
87 MANAGE_CLOUD_DESTINATIONS:
88 'print_preview.DestinationSearch.MANAGE_CLOUD_DESTINATIONS',
90 // Dispatched when the user requests to manage their local destinations.
91 MANAGE_LOCAL_DESTINATIONS:
92 'print_preview.DestinationSearch.MANAGE_LOCAL_DESTINATIONS',
94 // Dispatched when the user requests to sign-in to their Google account.
95 SIGN_IN: 'print_preview.DestinationSearch.SIGN_IN'
99 * Padding at the bottom of a destination list in pixels.
104 DestinationSearch.LIST_BOTTOM_PADDING_ = 18;
106 DestinationSearch.prototype = {
107 __proto__: print_preview.Component.prototype,
109 /** @return {boolean} Whether the component is visible. */
110 getIsVisible: function() {
111 return !this.getElement().classList.contains('transparent');
114 /** @param {boolean} isVisible Whether the component is visible. */
115 setIsVisible: function(isVisible) {
117 this.searchBox_.focus();
118 this.getElement().classList.remove('transparent');
119 var promoEl = this.getChildElement('.cloudprint-promo');
120 if (getIsVisible(promoEl)) {
121 this.metrics_.incrementDestinationSearchBucket(
122 print_preview.Metrics.DestinationSearchBucket.
123 CLOUDPRINT_PROMO_SHOWN);
127 this.getElement().classList.add('transparent');
128 // Collapse all destination lists
129 this.localList_.setIsShowAll(false);
130 this.cloudList_.setIsShowAll(false);
131 this.searchBox_.setQuery('');
132 this.filterLists_(null);
136 /** @param {string} email Email of the logged-in user. */
137 setCloudPrintEmail: function(email) {
138 var userInfoEl = this.getChildElement('.user-info');
139 userInfoEl.textContent = localStrings.getStringF('userInfo', email);
140 userInfoEl.title = localStrings.getStringF('userInfo', email);
141 setIsVisible(userInfoEl, true);
142 setIsVisible(this.getChildElement('.cloud-list'), true);
143 setIsVisible(this.getChildElement('.cloudprint-promo'), false);
147 /** Shows the Google Cloud Print promotion banner. */
148 showCloudPrintPromo: function() {
149 setIsVisible(this.getChildElement('.cloudprint-promo'), true);
150 if (this.getIsVisible()) {
151 this.metrics_.incrementDestinationSearchBucket(
152 print_preview.Metrics.DestinationSearchBucket.
153 CLOUDPRINT_PROMO_SHOWN);
159 enterDocument: function() {
160 print_preview.Component.prototype.enterDocument.call(this);
162 this.getChildElement('.page > .close-button'),
164 this.onCloseClick_.bind(this));
167 this.getChildElement('.sign-in'),
169 this.onSignInActivated_.bind(this));
172 this.getChildElement('.cloudprint-promo > .close-button'),
174 this.onCloudprintPromoCloseButtonClick_.bind(this));
177 print_preview.SearchBox.EventType.SEARCH,
178 this.onSearch_.bind(this));
181 print_preview.DestinationListItem.EventType.SELECT,
182 this.onDestinationSelect_.bind(this));
185 this.destinationStore_,
186 print_preview.DestinationStore.EventType.DESTINATIONS_INSERTED,
187 this.onDestinationsInserted_.bind(this));
189 this.destinationStore_,
190 print_preview.DestinationStore.EventType.DESTINATION_SELECT,
191 this.onDestinationStoreSelect_.bind(this));
193 this.destinationStore_,
194 print_preview.DestinationStore.EventType.DESTINATION_SEARCH_STARTED,
195 this.updateThrobbers_.bind(this));
197 this.destinationStore_,
198 print_preview.DestinationStore.EventType.DESTINATION_SEARCH_DONE,
199 this.updateThrobbers_.bind(this));
203 print_preview.DestinationList.EventType.ACTION_LINK_ACTIVATED,
204 this.onManageLocalDestinationsActivated_.bind(this));
207 print_preview.DestinationList.EventType.ACTION_LINK_ACTIVATED,
208 this.onManageCloudDestinationsActivated_.bind(this));
211 this.getElement(), 'click', this.onClick_.bind(this));
213 this.getChildElement('.page'),
214 'webkitAnimationEnd',
215 this.onAnimationEnd_.bind(this));
219 print_preview.UserInfo.EventType.EMAIL_CHANGE,
220 this.onEmailChange_.bind(this));
222 this.tracker.add(window, 'resize', this.onWindowResize_.bind(this));
224 this.updateThrobbers_();
226 // Render any destinations already in the store.
227 this.renderDestinations_();
231 decorateInternal: function() {
232 this.searchBox_.decorate($('search-box'));
233 this.recentList_.render(this.getChildElement('.recent-list'));
234 this.localList_.render(this.getChildElement('.local-list'));
235 this.cloudList_.render(this.getChildElement('.cloud-list'));
236 this.getChildElement('.promo-text').innerHTML = localStrings.getStringF(
237 'cloudPrintPromotion',
238 '<span class="sign-in link-button">',
243 * @return {number} Height available for destination lists, in pixels.
246 getAvailableListsHeight_: function() {
247 var elStyle = window.getComputedStyle(this.getElement());
248 return this.getElement().offsetHeight -
249 parseInt(elStyle.getPropertyValue('padding-top')) -
250 parseInt(elStyle.getPropertyValue('padding-bottom')) -
251 this.getChildElement('.lists').offsetTop -
252 this.getChildElement('.cloudprint-promo').offsetHeight;
256 * Filters all destination lists with the given query.
257 * @param {?string} query Query to filter destination lists by.
260 filterLists_: function(query) {
261 this.recentList_.updateSearchQuery(query);
262 this.localList_.updateSearchQuery(query);
263 this.cloudList_.updateSearchQuery(query);
267 * Resets the search query.
270 resetSearch_: function() {
271 this.searchBox_.setQuery(null);
272 this.filterLists_(null);
276 * Renders all of the destinations in the destination store.
279 renderDestinations_: function() {
280 var recentDestinations = [];
281 var localDestinations = [];
282 var cloudDestinations = [];
283 this.destinationStore_.destinations.forEach(function(destination) {
284 if (destination.isRecent) {
285 recentDestinations.push(destination);
287 if (destination.isLocal ||
288 destination.origin == print_preview.Destination.Origin.DEVICE) {
289 localDestinations.push(destination);
291 cloudDestinations.push(destination);
294 this.recentList_.updateDestinations(recentDestinations);
295 this.localList_.updateDestinations(localDestinations);
296 this.cloudList_.updateDestinations(cloudDestinations);
300 * Reflows the destination lists according to the available height.
303 reflowLists_: function() {
304 if (!this.getIsVisible()) {
308 var hasCloudList = getIsVisible(this.getChildElement('.cloud-list'));
309 var lists = [this.recentList_, this.localList_];
311 lists.push(this.cloudList_);
314 var availableHeight = this.getAvailableListsHeight_();
315 this.getChildElement('.lists').style.maxHeight = availableHeight + 'px';
317 var maxListLength = lists.reduce(function(prevCount, list) {
318 return Math.max(prevCount, list.getDestinationsCount());
320 for (var i = 1; i <= maxListLength; i++) {
321 var listsHeight = lists.reduce(function(sum, list) {
322 return sum + list.getEstimatedHeightInPixels(i) +
323 DestinationSearch.LIST_BOTTOM_PADDING_;
325 if (listsHeight > availableHeight) {
331 lists.forEach(function(list) {
332 list.updateShortListSize(i);
335 // Set height of the list manually so that search filter doesn't change
337 this.getChildElement('.lists').style.height =
338 lists.reduce(function(sum, list) {
339 return sum + list.getEstimatedHeightInPixels(i) +
340 DestinationSearch.LIST_BOTTOM_PADDING_;
345 * Updates whether the throbbers for the various destination lists should be
349 updateThrobbers_: function() {
350 this.localList_.setIsThrobberVisible(
351 this.destinationStore_.isLocalDestinationSearchInProgress);
352 this.cloudList_.setIsThrobberVisible(
353 this.destinationStore_.isCloudDestinationSearchInProgress);
354 this.recentList_.setIsThrobberVisible(
355 this.destinationStore_.isLocalDestinationSearchInProgress &&
356 this.destinationStore_.isCloudDestinationSearchInProgress);
361 * Called when a destination search should be executed. Filters the
362 * destination lists with the given query.
363 * @param {Event} evt Contains the search query.
366 onSearch_: function(evt) {
367 this.filterLists_(evt.query);
371 * Called when the close button is clicked. Hides the search widget.
374 onCloseClick_: function() {
375 this.setIsVisible(false);
377 this.metrics_.incrementDestinationSearchBucket(
378 print_preview.Metrics.DestinationSearchBucket.CANCELED);
382 * Called when a destination is selected. Clears the search and hides the
384 * @param {Event} evt Contains the selected destination.
387 onDestinationSelect_: function(evt) {
388 this.setIsVisible(false);
390 this.destinationStore_.selectDestination(evt.destination);
391 this.metrics_.incrementDestinationSearchBucket(
392 print_preview.Metrics.DestinationSearchBucket.DESTINATION_SELECTED);
396 * Called when a destination is selected. Selected destination are marked as
397 * recent, so we have to update our recent destinations list.
400 onDestinationStoreSelect_: function() {
401 var destinations = this.destinationStore_.destinations;
402 var recentDestinations = [];
403 destinations.forEach(function(destination) {
404 if (destination.isRecent) {
405 recentDestinations.push(destination);
408 this.recentList_.updateDestinations(recentDestinations);
413 * Called when destinations are inserted into the store. Rerenders
417 onDestinationsInserted_: function() {
418 this.renderDestinations_();
423 * Called when the manage cloud printers action is activated.
426 onManageCloudDestinationsActivated_: function() {
427 cr.dispatchSimpleEvent(
429 print_preview.DestinationSearch.EventType.MANAGE_CLOUD_DESTINATIONS);
433 * Called when the manage local printers action is activated.
436 onManageLocalDestinationsActivated_: function() {
437 cr.dispatchSimpleEvent(
439 print_preview.DestinationSearch.EventType.MANAGE_LOCAL_DESTINATIONS);
443 * Called when the "Sign in" link on the Google Cloud Print promo is
447 onSignInActivated_: function() {
448 cr.dispatchSimpleEvent(this, DestinationSearch.EventType.SIGN_IN);
449 this.metrics_.incrementDestinationSearchBucket(
450 print_preview.Metrics.DestinationSearchBucket.SIGNIN_TRIGGERED);
454 * Called when the close button on the cloud print promo is clicked. Hides
458 onCloudprintPromoCloseButtonClick_: function() {
459 setIsVisible(this.getChildElement('.cloudprint-promo'), false);
463 * Called when the overlay is clicked. Pulses the page.
464 * @param {Event} event Contains the element that was clicked.
467 onClick_: function(event) {
468 if (event.target == this.getElement()) {
469 this.getChildElement('.page').classList.add('pulse');
474 * Called when an animation ends on the page.
477 onAnimationEnd_: function() {
478 this.getChildElement('.page').classList.remove('pulse');
482 * Called when the user's email field has changed. Updates the UI.
485 onEmailChange_: function() {
486 var userEmail = this.userInfo_.getUserEmail();
488 this.setCloudPrintEmail(userEmail);
493 * Called when the window is resized. Reflows layout of destination lists.
496 onWindowResize_: function() {
503 DestinationSearch: DestinationSearch