Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not trigger slideChange on the initial render. #24717

Merged
merged 2 commits into from
Sep 25, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 6 additions & 1 deletion extensions/amp-base-carousel/0.1/carousel.js
Original file line number Diff line number Diff line change
Expand Up @@ -680,13 +680,18 @@ export class Carousel {
}

/**
* Updates the resting index as well as firing an event.
* Updates the resting index as well as firing an event, if it actually
* changed.
* @param {number} restingIndex The new resting index.
* @param {ActionSource=} actionSource The actionSource associated with this
* change.
* @private
*/
updateRestingIndex_(restingIndex, actionSource) {
if (this.restingIndex_ == restingIndex) {
return;
}

this.restingIndex_ = restingIndex;
this.element_.dispatchEvent(
createCustomEvent(
Expand Down
29 changes: 18 additions & 11 deletions extensions/amp-carousel/0.2/amp-carousel.js
Original file line number Diff line number Diff line change
Expand Up @@ -510,13 +510,27 @@ class AmpCarousel extends AMP.BaseElement {
}

/**
* Updates the current index, resuming the current slide and pausing all
* others.
* Updates the current index, triggering actions and analytics events.
* @param {number} index
* @param {!ActionSource} actionSource
*/
updateCurrentIndex_(index) {
updateCurrentIndex_(index, actionSource) {
const prevIndex = this.currentIndex_;
this.currentIndex_ = index;

// Ignore the first indexChange, we do not want to trigger any events.
if (prevIndex == null) {
return;
}

const data = dict({'index': index});
const name = 'slideChange';
const isHighTrust = this.isHighTrustActionSource_(actionSource);
const trust = isHighTrust ? ActionTrust.HIGH : ActionTrust.LOW;

const action = createCustomEvent(this.win, `slidescroll.${name}`, data);
this.action_.trigger(this.element, name, action, trust);
this.element.dispatchCustomEvent(name, data);
this.triggerAnalyticsEvent_(prevIndex, index);
}

Expand Down Expand Up @@ -618,16 +632,9 @@ class AmpCarousel extends AMP.BaseElement {
const detail = getDetail(event);
const index = detail['index'];
const actionSource = detail['actionSource'];
const data = dict({'index': index});
const name = 'slideChange';
const isHighTrust = this.isHighTrustActionSource_(actionSource);
const trust = isHighTrust ? ActionTrust.HIGH : ActionTrust.LOW;

const action = createCustomEvent(this.win, `slidescroll.${name}`, data);
this.action_.trigger(this.element, name, action, trust);
this.element.dispatchCustomEvent(name, data);
this.hadTouch_ = this.hadTouch_ || actionSource == ActionSource.TOUCH;
this.updateCurrentIndex_(index);
this.updateCurrentIndex_(index, actionSource);
this.updateUi_();
}
}
Expand Down
21 changes: 21 additions & 0 deletions extensions/amp-carousel/0.2/test/test-type-slides.js
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,27 @@ describes.realWin(
});
});

describe('slideChange event', () => {
it('should not dispatch on initial render', async () => {
const eventSpy = sandbox.spy();
container.addEventListener('slideChange', eventSpy);
await getCarousel({loop: false});

expect(eventSpy).to.have.not.been.called;
});

it('should dispatch when changing slides', async () => {
const eventSpy = sandbox.spy();
container.addEventListener('slideChange', eventSpy);
const carousel = await getCarousel({loop: false});

carousel.implementation_.interactionNext();
await afterIndexUpdate(carousel);

expect(eventSpy).to.have.been.calledOnce;
});
});

describe('goToSlide action', () => {
it('should propagate high trust', async () => {
const carousel = await getCarousel({loop: false});
Expand Down