| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 284 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 295 this.mouseWheelScrollAmount_ = 0; | 295 this.mouseWheelScrollAmount_ = 0; |
| 296 this.mouseWheelCardSelected_ = false; | 296 this.mouseWheelCardSelected_ = false; |
| 297 }, | 297 }, |
| 298 | 298 |
| 299 /** | 299 /** |
| 300 * Selects a new card, ensuring that it is a valid index, transforming the | 300 * Selects a new card, ensuring that it is a valid index, transforming the |
| 301 * view and possibly calling the change card callback. | 301 * view and possibly calling the change card callback. |
| 302 * @param {number} newCardIndex Index of card to show. | 302 * @param {number} newCardIndex Index of card to show. |
| 303 * @param {boolean=} opt_animate If true will animate transition from | 303 * @param {boolean=} opt_animate If true will animate transition from |
| 304 * current position to new position. | 304 * current position to new position. |
| 305 * @param {function=} opt_callback A callback to be executed when done |
| 306 * selecting the new card. |
| 305 */ | 307 */ |
| 306 selectCard: function(newCardIndex, opt_animate) { | 308 selectCard: function(newCardIndex, opt_animate, opt_callback) { |
| 307 var previousCard = this.currentCardValue; | 309 var previousCard = this.currentCardValue; |
| 308 | 310 |
| 309 var isChangingCard = | 311 var isChangingCard = |
| 310 !this.cards_[newCardIndex].classList.contains('selected-card'); | 312 !this.cards_[newCardIndex].classList.contains('selected-card'); |
| 311 | 313 |
| 312 if (isChangingCard) { | 314 if (isChangingCard) { |
| 313 previousCard.classList.remove('selected-card'); | 315 if (previousCard) |
| 316 previousCard.classList.remove('selected-card'); |
| 314 // If we have a new card index and it is valid then update the left | 317 // If we have a new card index and it is valid then update the left |
| 315 // position and current card index. | 318 // position and current card index. |
| 316 this.currentCard_ = newCardIndex; | 319 this.currentCard_ = newCardIndex; |
| 317 this.currentCardValue.classList.add('selected-card'); | 320 this.currentCardValue.classList.add('selected-card'); |
| 318 } | 321 } |
| 319 | 322 |
| 320 this.transformToCurrentCard_(opt_animate); | 323 var willTransitionHappen = this.transformToCurrentCard_(opt_animate); |
| 324 |
| 325 // If changing, animated, and a callback, do when done animating. |
| 326 if (typeof opt_callback == 'function') { |
| 327 if (isChangingCard && opt_animate && willTransitionHappen) { |
| 328 var endHandler = function(e) { |
| 329 // Ignore irrelevant transitions that might bubble up. |
| 330 if (e.target !== this.container_ || |
| 331 e.propertyName != '-webkit-transform') { |
| 332 return; |
| 333 } |
| 334 // Otherwise call the callback and unattach. |
| 335 opt_callback(); |
| 336 this.container_.removeEventListener('webkitTransitionEnd', |
| 337 endHandler); |
| 338 }.bind(this); |
| 339 this.container_.addEventListener('webkitTransitionEnd', endHandler); |
| 340 } else { |
| 341 opt_callback(); |
| 342 } |
| 343 } |
| 321 | 344 |
| 322 if (isChangingCard) { | 345 if (isChangingCard) { |
| 323 var event = document.createEvent('Event'); | 346 var event = document.createEvent('Event'); |
| 324 event.initEvent(CardSlider.EventType.CARD_CHANGED, true, true); | 347 event.initEvent(CardSlider.EventType.CARD_CHANGED, true, true); |
| 325 event.cardSlider = this; | 348 event.cardSlider = this; |
| 326 this.container_.dispatchEvent(event); | 349 this.container_.dispatchEvent(event); |
| 327 | 350 |
| 328 // We also dispatch an event on the cards themselves. | 351 // We also dispatch an event on the cards themselves. |
| 329 if (previousCard) { | 352 if (previousCard) { |
| 330 cr.dispatchSimpleEvent(previousCard, 'carddeselected', | 353 cr.dispatchSimpleEvent(previousCard, 'carddeselected', |
| (...skipping 14 matching lines...) Expand all Loading... |
| 345 assert(i != -1); | 368 assert(i != -1); |
| 346 this.selectCard(i, opt_animate); | 369 this.selectCard(i, opt_animate); |
| 347 }, | 370 }, |
| 348 | 371 |
| 349 /** | 372 /** |
| 350 * Centers the view on the card denoted by this.currentCard. Can either | 373 * Centers the view on the card denoted by this.currentCard. Can either |
| 351 * animate to that card or snap to it. | 374 * animate to that card or snap to it. |
| 352 * @param {boolean=} opt_animate If true will animate transition from | 375 * @param {boolean=} opt_animate If true will animate transition from |
| 353 * current position to new position. | 376 * current position to new position. |
| 354 * @private | 377 * @private |
| 378 * @return {boolean} Whether or not a transformation was necessary. |
| 355 */ | 379 */ |
| 356 transformToCurrentCard_: function(opt_animate) { | 380 transformToCurrentCard_: function(opt_animate) { |
| 381 var prevLeft = this.currentLeft_; |
| 357 this.currentLeft_ = -this.cardWidth_ * | 382 this.currentLeft_ = -this.cardWidth_ * |
| 358 (isRTL() ? this.cards_.length - this.currentCard - 1 : | 383 (isRTL() ? this.cards_.length - this.currentCard - 1 : |
| 359 this.currentCard); | 384 this.currentCard); |
| 360 | 385 |
| 386 // If there's no change, return something to let the caller no there won't |
| 387 // be a transition occuring. |
| 388 if (prevLeft == this.currentLeft_) |
| 389 return false; |
| 390 |
| 361 // Animate to the current card, which will either transition if the | 391 // Animate to the current card, which will either transition if the |
| 362 // current card is new, or reset the existing card if we didn't drag | 392 // current card is new, or reset the existing card if we didn't drag |
| 363 // enough to change cards. | 393 // enough to change cards. |
| 364 var transition = ''; | 394 var transition = ''; |
| 365 if (opt_animate) { | 395 if (opt_animate) { |
| 366 transition = '-webkit-transform ' + CardSlider.TRANSITION_TIME_ + | 396 transition = '-webkit-transform ' + CardSlider.TRANSITION_TIME_ + |
| 367 'ms ease-in-out'; | 397 'ms ease-in-out'; |
| 368 } | 398 } |
| 369 this.container_.style.WebkitTransition = transition; | 399 this.container_.style.WebkitTransition = transition; |
| 370 this.translateTo_(this.currentLeft_); | 400 this.translateTo_(this.currentLeft_); |
| 401 |
| 402 return true; |
| 371 }, | 403 }, |
| 372 | 404 |
| 373 /** | 405 /** |
| 374 * Moves the view to the specified position. | 406 * Moves the view to the specified position. |
| 375 * @param {number} x Horizontal position to move to. | 407 * @param {number} x Horizontal position to move to. |
| 376 * @private | 408 * @private |
| 377 */ | 409 */ |
| 378 translateTo_: function(x) { | 410 translateTo_: function(x) { |
| 379 // We use a webkitTransform to slide because this is GPU accelerated on | 411 // We use a webkitTransform to slide because this is GPU accelerated on |
| 380 // Chrome and iOS. Once Chrome does GPU acceleration on the position | 412 // Chrome and iOS. Once Chrome does GPU acceleration on the position |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 455 | 487 |
| 456 // Ensure we're at a card bounary | 488 // Ensure we're at a card bounary |
| 457 this.transformToCurrentCard_(true); | 489 this.transformToCurrentCard_(true); |
| 458 }, | 490 }, |
| 459 }; | 491 }; |
| 460 | 492 |
| 461 return { | 493 return { |
| 462 CardSlider: CardSlider | 494 CardSlider: CardSlider |
| 463 }; | 495 }; |
| 464 }); | 496 }); |
| OLD | NEW |