| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 /** | 5 /** |
| 6 * @fileoverview Card slider implementation. Allows you to create interactions | 6 * @fileoverview Card slider implementation. Allows you to create interactions |
| 7 * that have items that can slide left to right to reveal additional items. | 7 * that have items that can slide left to right to reveal additional items. |
| 8 * Works by adding the necessary event handlers to a specific DOM structure | 8 * Works by adding the necessary event handlers to a specific DOM structure |
| 9 * including a frame, container and cards. | 9 * including a frame, container and cards. |
| 10 * - The frame defines the boundary of one item. Each card will be expanded to | 10 * - The frame defines the boundary of one item. Each card will be expanded to |
| (...skipping 168 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 179 * @param {number} index Index of the card to in the new set of cards to | 179 * @param {number} index Index of the card to in the new set of cards to |
| 180 * navigate to. | 180 * navigate to. |
| 181 */ | 181 */ |
| 182 setCards: function(cards, index) { | 182 setCards: function(cards, index) { |
| 183 assert(index >= 0 && index < cards.length, | 183 assert(index >= 0 && index < cards.length, |
| 184 'Invalid index in CardSlider#setCards'); | 184 'Invalid index in CardSlider#setCards'); |
| 185 this.cards_ = cards; | 185 this.cards_ = cards; |
| 186 | 186 |
| 187 this.updateCardWidths_(); | 187 this.updateCardWidths_(); |
| 188 | 188 |
| 189 // Mark all cards as hidden for accessibility. The selected card will |
| 190 // be marked visible during selectCard(). |
| 191 for (var i = 0; i < cards.length; i++) { |
| 192 this.cards_[i].setAttribute('aria-hidden', true); |
| 193 } |
| 194 |
| 189 // Jump to the given card index. | 195 // Jump to the given card index. |
| 190 this.selectCard(index); | 196 this.selectCard(index); |
| 191 }, | 197 }, |
| 192 | 198 |
| 193 /** | 199 /** |
| 194 * Updates the width of each card. | 200 * Updates the width of each card. |
| 195 * @private | 201 * @private |
| 196 */ | 202 */ |
| 197 updateCardWidths_: function() { | 203 updateCardWidths_: function() { |
| 198 for (var i = 0, card; card = this.cards_[i]; i++) | 204 for (var i = 0, card; card = this.cards_[i]; i++) |
| (...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 343 * @param {!Node} card A card that will be added to the card slider. | 349 * @param {!Node} card A card that will be added to the card slider. |
| 344 * @param {number} index An index at which the given |card| should be | 350 * @param {number} index An index at which the given |card| should be |
| 345 * inserted. Must be positive and less than the number of cards. | 351 * inserted. Must be positive and less than the number of cards. |
| 346 */ | 352 */ |
| 347 addCardAtIndex: function(card, index) { | 353 addCardAtIndex: function(card, index) { |
| 348 assert(card instanceof Node, '|card| isn\'t a Node'); | 354 assert(card instanceof Node, '|card| isn\'t a Node'); |
| 349 this.assertValidIndex_(index); | 355 this.assertValidIndex_(index); |
| 350 this.cards_ = Array.prototype.concat.call( | 356 this.cards_ = Array.prototype.concat.call( |
| 351 this.cards_.slice(0, index), card, this.cards_.slice(index)); | 357 this.cards_.slice(0, index), card, this.cards_.slice(index)); |
| 352 | 358 |
| 359 // Mark the new card as hidden for accessibility. This will be corrected |
| 360 // during selectCard() if this is the selected card. |
| 361 card.setAttribute('aria-hidden', true); |
| 362 |
| 353 if (this.currentCard_ == -1) | 363 if (this.currentCard_ == -1) |
| 354 this.currentCard_ = 0; | 364 this.currentCard_ = 0; |
| 355 else if (index <= this.currentCard_) | 365 else if (index <= this.currentCard_) |
| 356 this.selectCard(this.currentCard_ + 1, false, true, true); | 366 this.selectCard(this.currentCard_ + 1, false, true, true); |
| 357 | 367 |
| 358 this.fireAddedEvent_(card, index); | 368 this.fireAddedEvent_(card, index); |
| 359 }, | 369 }, |
| 360 | 370 |
| 361 /** | 371 /** |
| 362 * Append a card to the end of the list. | 372 * Append a card to the end of the list. |
| (...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 464 this.assertValidIndex_(newCardIndex); | 474 this.assertValidIndex_(newCardIndex); |
| 465 | 475 |
| 466 var previousCard = this.currentCardValue; | 476 var previousCard = this.currentCardValue; |
| 467 var isChangingCard = | 477 var isChangingCard = |
| 468 !this.cards_[newCardIndex].classList.contains('selected-card'); | 478 !this.cards_[newCardIndex].classList.contains('selected-card'); |
| 469 | 479 |
| 470 if (typeof opt_forceChange != 'undefined' && opt_forceChange) | 480 if (typeof opt_forceChange != 'undefined' && opt_forceChange) |
| 471 isChangingCard = true; | 481 isChangingCard = true; |
| 472 | 482 |
| 473 if (isChangingCard) { | 483 if (isChangingCard) { |
| 474 if (previousCard) | 484 if (previousCard) { |
| 475 previousCard.classList.remove('selected-card'); | 485 previousCard.classList.remove('selected-card'); |
| 486 previousCard.setAttribute('aria-hidden', true); |
| 487 } |
| 476 this.currentCard_ = newCardIndex; | 488 this.currentCard_ = newCardIndex; |
| 477 this.currentCardValue.classList.add('selected-card'); | 489 this.currentCardValue.classList.add('selected-card'); |
| 490 this.currentCardValue.setAttribute('aria-hidden', false); |
| 478 } | 491 } |
| 479 | 492 |
| 480 var willTransitionHappen = this.transformToCurrentCard_(opt_animate); | 493 var willTransitionHappen = this.transformToCurrentCard_(opt_animate); |
| 481 | 494 |
| 482 if (isChangingCard && !opt_dontNotify) { | 495 if (isChangingCard && !opt_dontNotify) { |
| 483 var event = document.createEvent('Event'); | 496 var event = document.createEvent('Event'); |
| 484 event.initEvent('cardSlider:card_changed', true, true); | 497 event.initEvent('cardSlider:card_changed', true, true); |
| 485 event.cardSlider = this; | 498 event.cardSlider = this; |
| 486 event.wasAnimated = !!opt_animate; | 499 event.wasAnimated = !!opt_animate; |
| 487 this.container_.dispatchEvent(event); | 500 this.container_.dispatchEvent(event); |
| (...skipping 152 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 640 | 653 |
| 641 // Ensure we're at a card bounary | 654 // Ensure we're at a card bounary |
| 642 this.transformToCurrentCard_(true); | 655 this.transformToCurrentCard_(true); |
| 643 }, | 656 }, |
| 644 }; | 657 }; |
| 645 | 658 |
| 646 return { | 659 return { |
| 647 CardSlider: CardSlider | 660 CardSlider: CardSlider |
| 648 }; | 661 }; |
| 649 }); | 662 }); |
| OLD | NEW |