| OLD | NEW |
| (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('manage')); |
| 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} |
| 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 PROMO_TEXT: 'destination-search-cloudprint-promo-text', |
| 115 PULSE: 'pulse', |
| 116 RECENT_LIST: 'destination-search-recent-list', |
| 117 SEARCH_BOX_CONTAINER: 'destination-search-search-box-container', |
| 118 SIGN_IN: 'destination-search-sign-in', |
| 119 TRANSPARENT: 'transparent', |
| 120 USER_EMAIL: 'destination-search-user-email', |
| 121 USER_INFO: 'destination-search-user-info' |
| 122 }; |
| 123 |
| 124 DestinationSearch.prototype = { |
| 125 __proto__: print_preview.Component.prototype, |
| 126 |
| 127 /** @return {boolean} Whether the component is visible. */ |
| 128 getIsVisible: function() { |
| 129 return !this.getElement().classList.contains( |
| 130 DestinationSearch.Classes_.TRANSPARENT); |
| 131 }, |
| 132 |
| 133 /** @param {boolean} isVisible Whether the component is visible. */ |
| 134 setIsVisible: function(isVisible) { |
| 135 if (isVisible) { |
| 136 this.searchBox_.focus(); |
| 137 this.getElement().classList.remove( |
| 138 DestinationSearch.Classes_.TRANSPARENT); |
| 139 } else { |
| 140 this.getElement().classList.add(DestinationSearch.Classes_.TRANSPARENT); |
| 141 // Collapse all destination lists |
| 142 this.localList_.setIsShowAll(false); |
| 143 this.cloudList_.setIsShowAll(false); |
| 144 this.searchBox_.setQuery(''); |
| 145 this.filterLists_(null); |
| 146 } |
| 147 }, |
| 148 |
| 149 /** @param {string} email Email of the logged-in user. */ |
| 150 setCloudPrintEmail: function(email) { |
| 151 var userEmailEl = this.getElement().getElementsByClassName( |
| 152 DestinationSearch.Classes_.USER_EMAIL)[0]; |
| 153 userEmailEl.textContent = email; |
| 154 var userInfoEl = this.getElement().getElementsByClassName( |
| 155 DestinationSearch.Classes_.USER_INFO)[0]; |
| 156 setIsVisible(userInfoEl, true); |
| 157 var cloudListEl = this.getElement().getElementsByClassName( |
| 158 DestinationSearch.Classes_.CLOUD_LIST)[0]; |
| 159 setIsVisible(cloudListEl, true); |
| 160 var promoEl = this.getElement().getElementsByClassName( |
| 161 DestinationSearch.Classes_.CLOUDPRINT_PROMO)[0]; |
| 162 setIsVisible(promoEl, false); |
| 163 }, |
| 164 |
| 165 /** Shows the Google Cloud Print promotion banner. */ |
| 166 showCloudPrintPromo: function() { |
| 167 var promoEl = this.getElement().getElementsByClassName( |
| 168 DestinationSearch.Classes_.CLOUDPRINT_PROMO)[0]; |
| 169 setIsVisible(promoEl, true); |
| 170 }, |
| 171 |
| 172 /** @override */ |
| 173 enterDocument: function() { |
| 174 print_preview.Component.prototype.enterDocument.call(this); |
| 175 var closeButtonEl = this.getElement().getElementsByClassName( |
| 176 DestinationSearch.Classes_.CLOSE_BUTTON)[0]; |
| 177 var signInButton = this.getElement().getElementsByClassName( |
| 178 DestinationSearch.Classes_.SIGN_IN)[0]; |
| 179 var cloudprintPromoCloseButton = this.getElement().getElementsByClassName( |
| 180 DestinationSearch.Classes_.CLOUDPRINT_PROMO_CLOSE_BUTTON)[0]; |
| 181 |
| 182 this.tracker.add( |
| 183 closeButtonEl, 'click', this.onCloseClick_.bind(this)); |
| 184 |
| 185 var promoTextEl = this.getElement().getElementsByClassName( |
| 186 DestinationSearch.Classes_.PROMO_TEXT)[0]; |
| 187 var signInEl = promoTextEl.getElementsByClassName( |
| 188 DestinationSearch.Classes_.SIGN_IN)[0]; |
| 189 this.tracker.add( |
| 190 signInEl, 'click', this.onSignInActivated_.bind(this)); |
| 191 |
| 192 this.tracker.add( |
| 193 cloudprintPromoCloseButton, |
| 194 'click', |
| 195 this.onCloudprintPromoCloseButtonClick_.bind(this)); |
| 196 this.tracker.add( |
| 197 this.searchBox_, |
| 198 print_preview.SearchBox.EventType.SEARCH, |
| 199 this.onSearch_.bind(this)); |
| 200 this.tracker.add( |
| 201 this, |
| 202 print_preview.DestinationListItem.EventType.SELECT, |
| 203 this.onDestinationSelect_.bind(this)); |
| 204 |
| 205 this.tracker.add( |
| 206 this.destinationStore_, |
| 207 print_preview.DestinationStore.EventType.DESTINATIONS_INSERTED, |
| 208 this.onDestinationsInserted_.bind(this)); |
| 209 this.tracker.add( |
| 210 this.destinationStore_, |
| 211 print_preview.DestinationStore.EventType.DESTINATION_SELECT, |
| 212 this.onDestinationStoreSelect_.bind(this)); |
| 213 |
| 214 this.tracker.add( |
| 215 this.localList_, |
| 216 print_preview.DestinationList.EventType.ACTION_LINK_ACTIVATED, |
| 217 this.onManageLocalDestinationsActivated_.bind(this)); |
| 218 this.tracker.add( |
| 219 this.cloudList_, |
| 220 print_preview.DestinationList.EventType.ACTION_LINK_ACTIVATED, |
| 221 this.onManageCloudDestinationsActivated_.bind(this)); |
| 222 |
| 223 this.tracker.add( |
| 224 this.getElement(), 'click', this.onClick_.bind(this)); |
| 225 this.tracker.add( |
| 226 this.pageEl_, 'webkitAnimationEnd', this.onAnimationEnd_.bind(this)); |
| 227 |
| 228 this.tracker.add( |
| 229 this.userInfo_, |
| 230 print_preview.UserInfo.EventType.EMAIL_CHANGE, |
| 231 this.onEmailChange_.bind(this)); |
| 232 }, |
| 233 |
| 234 /** @override */ |
| 235 exitDocument: function() { |
| 236 print_preview.Component.prototype.exitDocument.call(this); |
| 237 this.pageEl_ = null; |
| 238 }, |
| 239 |
| 240 /** @override */ |
| 241 decorateInternal: function() { |
| 242 this.searchBox_.decorate($('search-box')); |
| 243 |
| 244 var recentListEl = this.getElement().getElementsByClassName( |
| 245 DestinationSearch.Classes_.RECENT_LIST)[0]; |
| 246 this.recentList_.render(recentListEl); |
| 247 |
| 248 var localListEl = this.getElement().getElementsByClassName( |
| 249 DestinationSearch.Classes_.LOCAL_LIST)[0]; |
| 250 this.localList_.render(localListEl); |
| 251 |
| 252 var cloudListEl = this.getElement().getElementsByClassName( |
| 253 DestinationSearch.Classes_.CLOUD_LIST)[0]; |
| 254 this.cloudList_.render(cloudListEl); |
| 255 |
| 256 this.pageEl_ = this.getElement().getElementsByClassName( |
| 257 DestinationSearch.Classes_.PAGE)[0]; |
| 258 |
| 259 var promoTextEl = this.getElement().getElementsByClassName( |
| 260 DestinationSearch.Classes_.PROMO_TEXT)[0]; |
| 261 promoTextEl.innerHTML = localStrings.getStringF( |
| 262 'cloudPrintPromotion', |
| 263 '<span class="destination-search-sign-in link-button">', |
| 264 '</span>'); |
| 265 }, |
| 266 |
| 267 /** |
| 268 * Filters all destination lists with the given query. |
| 269 * @param {?string} query Query to filter destination lists by. |
| 270 * @private |
| 271 */ |
| 272 filterLists_: function(query) { |
| 273 this.recentList_.filter(query); |
| 274 this.localList_.filter(query); |
| 275 this.cloudList_.filter(query); |
| 276 }, |
| 277 |
| 278 /** |
| 279 * Resets the search query. |
| 280 * @private |
| 281 */ |
| 282 resetSearch_: function() { |
| 283 this.searchBox_.setQuery(null); |
| 284 this.filterLists_(null); |
| 285 }, |
| 286 |
| 287 /** |
| 288 * Called when a destination search should be executed. Filters the |
| 289 * destination lists with the given query. |
| 290 * @param {cr.Event} evt Contains the search query. |
| 291 * @private |
| 292 */ |
| 293 onSearch_: function(evt) { |
| 294 this.filterLists_(evt.query); |
| 295 }, |
| 296 |
| 297 /** |
| 298 * Called when the close button is clicked. Hides the search widget. |
| 299 * @private |
| 300 */ |
| 301 onCloseClick_: function() { |
| 302 this.setIsVisible(false); |
| 303 this.resetSearch_(); |
| 304 }, |
| 305 |
| 306 /** |
| 307 * Called when a destination is selected. Clears the search and hides the |
| 308 * widget. |
| 309 * @param {cr.Event} evt Contains the selected destination. |
| 310 * @private |
| 311 */ |
| 312 onDestinationSelect_: function(evt) { |
| 313 this.setIsVisible(false); |
| 314 this.resetSearch_(); |
| 315 this.destinationStore_.selectDestination(evt.destination); |
| 316 }, |
| 317 |
| 318 /** |
| 319 * Called when destinations are added to the destination store. Refreshes UI |
| 320 * with new destinations. |
| 321 * @private |
| 322 */ |
| 323 onDestinationsInserted_: function() { |
| 324 var recentDestinations = []; |
| 325 var localDestinations = []; |
| 326 var cloudDestinations = []; |
| 327 this.destinationStore_.destinations.forEach(function(destination) { |
| 328 if (destination.isRecent) { |
| 329 recentDestinations.push(destination); |
| 330 } |
| 331 if (destination.isLocal) { |
| 332 localDestinations.push(destination); |
| 333 } else { |
| 334 cloudDestinations.push(destination); |
| 335 } |
| 336 }); |
| 337 this.recentList_.updateDestinations(recentDestinations); |
| 338 this.localList_.updateDestinations(localDestinations); |
| 339 this.cloudList_.updateDestinations(cloudDestinations); |
| 340 }, |
| 341 |
| 342 /** |
| 343 * Called when a destination is selected. Selected destination are marked as |
| 344 * recent, so we have to update our recent destinations list. |
| 345 * @private |
| 346 */ |
| 347 onDestinationStoreSelect_: function() { |
| 348 var destinations = this.destinationStore_.destinations; |
| 349 var recentDestinations = []; |
| 350 destinations.forEach(function(destination) { |
| 351 if (destination.isRecent) { |
| 352 recentDestinations.push(destination); |
| 353 } |
| 354 }); |
| 355 this.recentList_.updateDestinations(recentDestinations); |
| 356 }, |
| 357 |
| 358 /** |
| 359 * Called when the manage cloud printers action is activated. |
| 360 * @private |
| 361 */ |
| 362 onManageCloudDestinationsActivated_: function() { |
| 363 cr.dispatchSimpleEvent( |
| 364 this, |
| 365 print_preview.DestinationSearch.EventType.MANAGE_CLOUD_DESTINATIONS); |
| 366 }, |
| 367 |
| 368 /** |
| 369 * Called when the manage local printers action is activated. |
| 370 * @private |
| 371 */ |
| 372 onManageLocalDestinationsActivated_: function() { |
| 373 cr.dispatchSimpleEvent( |
| 374 this, |
| 375 print_preview.DestinationSearch.EventType.MANAGE_LOCAL_DESTINATIONS); |
| 376 }, |
| 377 |
| 378 /** |
| 379 * Called when the "Sign in" link on the Google Cloud Print promo is |
| 380 * activated. |
| 381 * @private |
| 382 */ |
| 383 onSignInActivated_: function() { |
| 384 cr.dispatchSimpleEvent(this, DestinationSearch.EventType.SIGN_IN); |
| 385 }, |
| 386 |
| 387 /** |
| 388 * Called when the close button on the cloud print promo is clicked. Hides |
| 389 * the promo. |
| 390 * @private |
| 391 */ |
| 392 onCloudprintPromoCloseButtonClick_: function() { |
| 393 var promoEl = this.getElement().getElementsByClassName( |
| 394 DestinationSearch.Classes_.CLOUDPRINT_PROMO)[0]; |
| 395 setIsVisible(promoEl, false); |
| 396 }, |
| 397 |
| 398 /** |
| 399 * Called when the overlay is clicked. Pulses the page. |
| 400 * @param {Event} event Contains the element that was clicked. |
| 401 * @private |
| 402 */ |
| 403 onClick_: function(event) { |
| 404 if (event.target == this.getElement()) { |
| 405 this.pageEl_.classList.add(DestinationSearch.Classes_.PULSE); |
| 406 } |
| 407 }, |
| 408 |
| 409 /** |
| 410 * Called when an animation ends on the page. |
| 411 * @private |
| 412 */ |
| 413 onAnimationEnd_: function() { |
| 414 this.pageEl_.classList.remove(DestinationSearch.Classes_.PULSE); |
| 415 }, |
| 416 |
| 417 /** |
| 418 * Called when the user's email field has changed. Updates the UI. |
| 419 * @private |
| 420 */ |
| 421 onEmailChange_: function() { |
| 422 var userEmail = this.userInfo_.getUserEmail(); |
| 423 if (userEmail) { |
| 424 this.setCloudPrintEmail(userEmail); |
| 425 } |
| 426 } |
| 427 }; |
| 428 |
| 429 // Export |
| 430 return { |
| 431 DestinationSearch: DestinationSearch |
| 432 }; |
| 433 }); |
| OLD | NEW |