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

Side by Side Diff: chrome/browser/resources/shared/js/cr/ui/card_slider.js

Issue 9116037: [NTP4] Make TilePage and CardSlider evented to simplify code and fix page switcher bug (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: rebase Created 8 years, 11 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 | Annotate | Revision Log
OLDNEW
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 55 matching lines...) Expand 10 before | Expand all | Expand 10 after
66 */ 66 */
67 this.cardWidth_ = cardWidth; 67 this.cardWidth_ = cardWidth;
68 68
69 /** 69 /**
70 * @type {!cr.ui.TouchHandler} 70 * @type {!cr.ui.TouchHandler}
71 * @private 71 * @private
72 */ 72 */
73 this.touchHandler_ = new cr.ui.TouchHandler(this.container_); 73 this.touchHandler_ = new cr.ui.TouchHandler(this.container_);
74 } 74 }
75 75
76 /**
77 * Events fired by the slider.
78 * Events are fired at the container.
79 */
80 CardSlider.EventType = {
81 // Fired when the user slides to another card.
82 CARD_CHANGED: 'cardSlider:card_changed'
83 };
84
85 76
86 /** 77 /**
87 * The time to transition between cards when animating. Measured in ms. 78 * The time to transition between cards when animating. Measured in ms.
88 * @type {number} 79 * @type {number}
89 * @private 80 * @private
90 * @const 81 * @const
91 */ 82 */
92 CardSlider.TRANSITION_TIME_ = 200; 83 CardSlider.TRANSITION_TIME_ = 200;
93 84
94 85
(...skipping 30 matching lines...) Expand all
125 116
126 this.updateCardWidths_(); 117 this.updateCardWidths_();
127 118
128 this.mouseWheelScrollAmount_ = 0; 119 this.mouseWheelScrollAmount_ = 0;
129 this.mouseWheelCardSelected_ = false; 120 this.mouseWheelCardSelected_ = false;
130 this.mouseWheelIsContinuous_ = false; 121 this.mouseWheelIsContinuous_ = false;
131 this.scrollClearTimeout_ = null; 122 this.scrollClearTimeout_ = null;
132 this.frame_.addEventListener('mousewheel', 123 this.frame_.addEventListener('mousewheel',
133 this.onMouseWheel_.bind(this)); 124 this.onMouseWheel_.bind(this));
134 this.container_.addEventListener( 125 this.container_.addEventListener(
135 'webkitTransitionEnd', this.onAnimationTransitioned_.bind(this)); 126 'webkitTransitionEnd', this.onWebkitTransitionEnd_.bind(this));
136 127
137 // Also support touch events in case a touch screen happens to be 128 // Also support touch events in case a touch screen happens to be
138 // available. Ideally we would support touch events whenever they 129 // available. Ideally we would support touch events whenever they
139 // are fired, but for now restrict this extra code to when we know 130 // are fired, but for now restrict this extra code to when we know
140 // we want to support touch input. 131 // we want to support touch input.
141 if (cr.isTouchOptimized) { 132 if (cr.isTouchOptimized) {
142 var TouchHandler = cr.ui.TouchHandler; 133 var TouchHandler = cr.ui.TouchHandler;
143 this.container_.addEventListener(TouchHandler.EventType.TOUCH_START, 134 this.container_.addEventListener(TouchHandler.EventType.TOUCH_START,
144 this.onTouchStart_.bind(this)); 135 this.onTouchStart_.bind(this));
145 this.container_.addEventListener(TouchHandler.EventType.DRAG_START, 136 this.container_.addEventListener(TouchHandler.EventType.DRAG_START,
(...skipping 146 matching lines...) Expand 10 before | Expand all | Expand 10 after
292 * Resets the amount of horizontal scroll we've seen to 0. See 283 * Resets the amount of horizontal scroll we've seen to 0. See
293 * onMouseWheel_. 284 * onMouseWheel_.
294 * @private 285 * @private
295 */ 286 */
296 clearMouseWheelScroll_: function() { 287 clearMouseWheelScroll_: function() {
297 this.mouseWheelScrollAmount_ = 0; 288 this.mouseWheelScrollAmount_ = 0;
298 this.mouseWheelCardSelected_ = false; 289 this.mouseWheelCardSelected_ = false;
299 }, 290 },
300 291
301 /** 292 /**
302 * A handler for the animations ending their transition. 293 * Handles the ends of -webkit-transitions on -webkit-transform (animated
294 * card switches).
295 * @param {Event} e The webkitTransitionEnd event.
303 * @private 296 * @private
304 */ 297 */
305 onAnimationTransitioned_: function(event) { 298 onWebkitTransitionEnd_: function(e) {
306 if (event.target.id == 'page-list') { 299 // Ignore irrelevant transitions that might bubble up.
307 cr.dispatchSimpleEvent(this.currentCardValue, 'cardSelectionCompleted', 300 if (e.target !== this.container_ ||
308 true, true); 301 e.propertyName != '-webkit-transform') {
302 return;
309 } 303 }
304 this.fireChangeEndedEvent_(true);
310 }, 305 },
311 306
312 /** 307 /**
308 * Dispatches a simple event to tell subscribers we're done moving to the
309 * newly selected card.
310 * @param {boolean} wasAnimated whether or not the change was animated.
311 * @private
312 */
313 fireChangeEndedEvent_: function(wasAnimated) {
314 var e = document.createEvent('Event');
315 e.initEvent('cardSlider:card_change_ended', true, true);
316 e.cardSlider = this;
317 e.changedTo = this.currentCard_;
318 e.wasAnimated = wasAnimated;
319 this.container_.dispatchEvent(e);
320 },
321
322 /**
323 * Add a card to the card slider at a particular index. If the card being
324 * added is inserted in front of the current card, cardSlider.currentCard
325 * will be adjusted accordingly (to current card + 1).
326 * @param {!Node} card A card that will be added to the card slider.
327 * @param {number} index An index at which the given |card| should be
328 * inserted. Must be positive and less than the number of cards.
329 */
330 addCardAtIndex: function(card, index) {
331 assert(card instanceof Node, '|card| isn\'t a Node');
332 this.assertValidIndex_(index);
333 this.cards_ = Array.prototype.concat.call(
334 this.cards_.slice(0, index), card, this.cards_.slice(index));
335 if (index <= this.currentCard_)
336 this.selectCard(this.currentCard_ + 1, false, true);
337 this.fireAddedEvent_(card, index);
338 },
339
340 /**
341 * Append a card to the end of the list.
342 * @param {!Node} card A card to add at the end of the card slider.
343 */
344 appendCard: function(card) {
345 assert(card instanceof Node, '|card| isn\'t a Node');
346 this.cards_.push(card);
347 this.fireAddedEvent_(card, this.cards_.length - 1);
348 },
349
350 /**
351 * Dispatches a simple event to tell interested subscribers that a card was
352 * added to this card slider.
353 * @param {Node} card The recently added card.
354 * @param {number} index The position of the newly added card.
355 * @private
356 */
357 fireAddedEvent_: function(card, index) {
358 this.assertValidIndex_(index);
359 var e = document.createEvent('Event');
360 e.initEvent('cardSlider:card_added', true, true);
361 e.addedIndex = index;
362 e.addedCard = card;
363 this.container_.dispatchEvent(e);
364 },
365
366 /**
367 * Removes a card by index from the card slider. If the card to be removed
368 * is the current card or in front of the current card, the current card
369 * will be updated (to current card - 1).
370 * @param {!Node} card A card to be removed.
371 */
372 removeCard: function(card) {
373 assert(card instanceof Node, '|card| isn\'t a Node');
374 this.removeCardAtIndex(this.cards_.indexOf(card));
375 },
376
377 /**
378 * Removes a card by index from the card slider. If the card to be removed
379 * is the current card or in front of the current card, the current card
380 * will be updated (to current card - 1).
381 * @param {number} index The index of the tile that should be removed.
382 */
383 removeCardAtIndex: function(index) {
384 this.assertValidIndex_(index);
385 var removed = this.cards_.splice(index, 1).pop();
386 if (index < this.currentCard_)
387 this.selectCard(this.currentCard_ - 1, false, true);
388 this.fireRemovedEvent_(removed, index);
389 },
390
391 /**
392 * Dispatches a cardSlider:card_removed event so interested subscribers know
393 * when a card was removed from this card slider.
394 * @param {Node} card The recently removed card.
395 * @param {number} index The index of the card before it was removed.
396 * @private
397 */
398 fireRemovedEvent_: function(card, index) {
399 var e = document.createEvent('Event');
400 e.initEvent('cardSlider:card_removed', true, true);
401 e.removedCard = card;
402 e.removedIndex = index;
403 this.container_.dispatchEvent(e);
404 },
405
406 /**
407 * Checks the the given |index| exists in this.cards_.
408 * @param {number} index An index to check.
409 * @private
410 */
411 assertValidIndex_: function(index) {
412 assert(index >= 0 && index < this.cards_.length);
413 },
414
415 /**
313 * Selects a new card, ensuring that it is a valid index, transforming the 416 * Selects a new card, ensuring that it is a valid index, transforming the
314 * view and possibly calling the change card callback. 417 * view and possibly calling the change card callback.
315 * @param {number} newCardIndex Index of card to show. 418 * @param {number} newCardIndex Index of card to show.
316 * @param {boolean=} opt_animate If true will animate transition from 419 * @param {boolean=} opt_animate If true will animate transition from
317 * current position to new position. 420 * current position to new position.
421 * @param {boolean=} opt_dontNotify If true, don't tell subscribers that
422 * we've changed cards.
318 */ 423 */
319 selectCard: function(newCardIndex, opt_animate) { 424 selectCard: function(newCardIndex, opt_animate, opt_dontNotify) {
425 this.assertValidIndex_(newCardIndex);
426
320 var previousCard = this.currentCardValue; 427 var previousCard = this.currentCardValue;
321
322 var isChangingCard = 428 var isChangingCard =
323 !this.cards_[newCardIndex].classList.contains('selected-card'); 429 !this.cards_[newCardIndex].classList.contains('selected-card');
324 430
325 if (isChangingCard) { 431 if (isChangingCard) {
326 previousCard.classList.remove('selected-card'); 432 if (previousCard)
327 // If we have a new card index and it is valid then update the left 433 previousCard.classList.remove('selected-card');
328 // position and current card index.
329 this.currentCard_ = newCardIndex; 434 this.currentCard_ = newCardIndex;
330 this.currentCardValue.classList.add('selected-card'); 435 this.currentCardValue.classList.add('selected-card');
331 } 436 }
332 437
333 this.transformToCurrentCard_(opt_animate); 438 var willTransitionHappen = this.transformToCurrentCard_(opt_animate);
334 439
335 if (isChangingCard) { 440 if (isChangingCard && !opt_dontNotify) {
336 var event = document.createEvent('Event'); 441 var event = document.createEvent('Event');
337 event.initEvent(CardSlider.EventType.CARD_CHANGED, true, true); 442 event.initEvent('cardSlider:card_changed', true, true);
338 event.cardSlider = this; 443 event.cardSlider = this;
444 event.wasAnimated = !!opt_animate;
339 this.container_.dispatchEvent(event); 445 this.container_.dispatchEvent(event);
340 446
341 // We also dispatch an event on the cards themselves. 447 // We also dispatch an event on the cards themselves.
342 if (previousCard) { 448 if (previousCard) {
343 cr.dispatchSimpleEvent(previousCard, 'carddeselected', 449 cr.dispatchSimpleEvent(previousCard, 'carddeselected',
344 true, true); 450 true, true);
345 } 451 }
346 cr.dispatchSimpleEvent(this.currentCardValue, 'cardselected', 452 cr.dispatchSimpleEvent(this.currentCardValue, 'cardselected',
347 true, true); 453 true, true);
348 } 454 }
455
456 // If we're not changing, animated, or transitioning, fire a
457 // cardSlider:card_change_ended event right away.
458 if ((!isChangingCard || !opt_animate || !willTransitionHappen) &&
459 !opt_dontNotify) {
460 this.fireChangeEndedEvent_(false);
461 }
349 }, 462 },
350 463
351 /** 464 /**
352 * Selects a card from the stack. Passes through to selectCard. 465 * Selects a card from the stack. Passes through to selectCard.
353 * @param {Node} newCard The card that should be selected. 466 * @param {Node} newCard The card that should be selected.
354 * @param {boolean=} opt_animate Whether to animate. 467 * @param {boolean=} opt_animate Whether to animate.
355 */ 468 */
356 selectCardByValue: function(newCard, opt_animate) { 469 selectCardByValue: function(newCard, opt_animate) {
357 var i = this.cards_.indexOf(newCard); 470 var i = this.cards_.indexOf(newCard);
358 assert(i != -1); 471 assert(i != -1);
359 this.selectCard(i, opt_animate); 472 this.selectCard(i, opt_animate);
360 }, 473 },
361 474
362 /** 475 /**
363 * Centers the view on the card denoted by this.currentCard. Can either 476 * Centers the view on the card denoted by this.currentCard. Can either
364 * animate to that card or snap to it. 477 * animate to that card or snap to it.
365 * @param {boolean=} opt_animate If true will animate transition from 478 * @param {boolean=} opt_animate If true will animate transition from
366 * current position to new position. 479 * current position to new position.
480 * @return {boolean} Whether or not a transformation was necessary.
367 * @private 481 * @private
368 */ 482 */
369 transformToCurrentCard_: function(opt_animate) { 483 transformToCurrentCard_: function(opt_animate) {
484 var prevLeft = this.currentLeft_;
370 this.currentLeft_ = -this.cardWidth_ * 485 this.currentLeft_ = -this.cardWidth_ *
371 (isRTL() ? this.cards_.length - this.currentCard - 1 : 486 (isRTL() ? this.cards_.length - this.currentCard - 1 :
372 this.currentCard); 487 this.currentCard);
373 488
489 // If there's no change, return something to let the caller know there
490 // won't be a transition occuring.
491 if (prevLeft == this.currentLeft_)
492 return false;
493
374 // Animate to the current card, which will either transition if the 494 // Animate to the current card, which will either transition if the
375 // current card is new, or reset the existing card if we didn't drag 495 // current card is new, or reset the existing card if we didn't drag
376 // enough to change cards. 496 // enough to change cards.
377 var transition = ''; 497 var transition = '';
378 if (opt_animate) { 498 if (opt_animate) {
379 transition = '-webkit-transform ' + CardSlider.TRANSITION_TIME_ + 499 transition = '-webkit-transform ' + CardSlider.TRANSITION_TIME_ +
380 'ms ease-in-out'; 500 'ms ease-in-out';
381 } 501 }
382 this.container_.style.WebkitTransition = transition; 502 this.container_.style.WebkitTransition = transition;
383 this.translateTo_(this.currentLeft_); 503 this.translateTo_(this.currentLeft_);
504
505 return true;
384 }, 506 },
385 507
386 /** 508 /**
387 * Moves the view to the specified position. 509 * Moves the view to the specified position.
388 * @param {number} x Horizontal position to move to. 510 * @param {number} x Horizontal position to move to.
389 * @private 511 * @private
390 */ 512 */
391 translateTo_: function(x) { 513 translateTo_: function(x) {
392 // We use a webkitTransform to slide because this is GPU accelerated on 514 // We use a webkitTransform to slide because this is GPU accelerated on
393 // Chrome and iOS. Once Chrome does GPU acceleration on the position 515 // Chrome and iOS. Once Chrome does GPU acceleration on the position
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after
468 590
469 // Ensure we're at a card bounary 591 // Ensure we're at a card bounary
470 this.transformToCurrentCard_(true); 592 this.transformToCurrentCard_(true);
471 }, 593 },
472 }; 594 };
473 595
474 return { 596 return {
475 CardSlider: CardSlider 597 CardSlider: CardSlider
476 }; 598 };
477 }); 599 });
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698