Skip to content

[amp-carousel] Add support for defined number of loops to autoplay#18981

Merged
aghassemi merged 9 commits intoampproject:masterfrom
nainar:carousel_autoplay
Nov 2, 2018
Merged

[amp-carousel] Add support for defined number of loops to autoplay#18981
aghassemi merged 9 commits intoampproject:masterfrom
nainar:carousel_autoplay

Conversation

@nainar
Copy link
Copy Markdown
Contributor

@nainar nainar commented Oct 26, 2018

Addresses #18911 for carousel V1 only.

attrs: {
name: "autoplay"
value: ""
value_regex: "[0-9]+"
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For attrs it's considered a oneof of different types of values. Some can be repeated like value. In this case I'd recommend value_regex: "(|[0-9]+)" since it should allow both empty and some numeric.

For limited sets repeating value is sometimes easier to read than regex, e.g.:

value: "_top"
value: "_blank"

instead of
value_regex: "(_top|_blank)"

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Made the change @honeybadgerdontcare, also added a validator test for this.

this.hasAutoplay_ = this.element.hasAttribute('autoplay');
const autoplayVal = this.element.getAttribute('autoplay');
if (autoplayVal != '') {
this.autoplayLoops_ = parseInt(autoplayVal, 10);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

user().assert(isFiniteNumber(this.autoplayLoops_)) would be nice
(isFiniteNumber is in types.js)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

// we have reached the last slide in the set
// carry out removing autoplay logic.
// This only works because the initial Slide is always the first slide.
if (this.autoplayLoops_ && this.slideIndex_ === this.noOfSlides_ - 1) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wha if autoplayLoops_ is set to 0? this logic would treat it as infinite loop. We probably want to either disallow it or treat autoplay=0 same as no autoplay

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. If the value of autoplayLoops_ is set to 0 we never call setupAutoplay_ or autoplay_

this.clearAutoplay();
this.hasAutoplay_ = false;
this.shouldAutoplay_ = this.hasAutoplay_ && this.isLoopingEligible();
this.element.removeAttribute('loop');
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should not disable loop unless it was forced by autoplay in the first place. If author has loop specified, after autoplay ends, user should still manually loop.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. Put a guard around it that checks that autoplay is set as well.

Copy link
Copy Markdown
Contributor Author

@nainar nainar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@aghassemi made the changes you asked for. Added a test for autoplay=0 as well.

this.hasAutoplay_ = this.element.hasAttribute('autoplay');
const autoplayVal = this.element.getAttribute('autoplay');
if (autoplayVal != '') {
this.autoplayLoops_ = parseInt(autoplayVal, 10);
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

this.clearAutoplay();
this.hasAutoplay_ = false;
this.shouldAutoplay_ = this.hasAutoplay_ && this.isLoopingEligible();
this.element.removeAttribute('loop');
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. Put a guard around it that checks that autoplay is set as well.

// we have reached the last slide in the set
// carry out removing autoplay logic.
// This only works because the initial Slide is always the first slide.
if (this.autoplayLoops_ && this.slideIndex_ === this.noOfSlides_ - 1) {
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. If the value of autoplayLoops_ is set to 0 we never call setupAutoplay_ or autoplay_

this.shouldAutoplay_ = this.hasAutoplay_ && this.isLoopingEligible();
if (this.element.hasAttribute('autoplay')) {
// Only remove if specified due to the `autoplay` attribute
this.element.removeAttribute('loop');
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

    this.hasLoop_ = false;
this.shouldLoop = false;

too?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@nainar nainar force-pushed the carousel_autoplay branch from d84ee9f to e4f0255 Compare October 29, 2018 20:06
this.clearAutoplay();
this.hasAutoplay_ = false;
this.shouldAutoplay_ = this.hasAutoplay_ && this.isLoopingEligible();
if (this.element.hasAttribute('autoplay')) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just the attribute check ignores the isLoopingEligible part. The value of this.shouldAutoplay_ before it changes should be the condition.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree with you there. Since we set this.hasAutoplay_ to false we are guaranteed that this.shouldAutoplay_ will be false. I can still guard it?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess we don't even get here if it was not eligible for autoplay. I think the check to remove loop only if it was added because of autoplay is still wrong. Maybe it should be

// loop added due to autoplay if element originally did not have `loop` attribute, so remove it.
if(!this.element.hasAttribute('loop') {
...
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looking at this again, I realize with don't actually have a signal to know whether loop was added because of autoplay or it was there from the beginning. We need a new variable to track that when autoplay adds loop.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can also just maintain state to see if loop was added by autoplay status addition

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in latest Patch - 1b9d553

Copy link
Copy Markdown
Contributor

@aghassemi aghassemi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Left one comment, feel free to merge after addressing.

@nainar
Copy link
Copy Markdown
Contributor Author

nainar commented Nov 1, 2018

@aghassemi made the change in this patch: a247fe3

Will merge if it looks good to you.

@aghassemi
Copy link
Copy Markdown
Contributor

LGTM

@aghassemi aghassemi merged commit a0ca29d into ampproject:master Nov 2, 2018
twifkak added a commit to twifkak/amphtml that referenced this pull request Nov 2, 2018
twifkak added a commit to twifkak/amphtml that referenced this pull request Nov 2, 2018
@twifkak twifkak mentioned this pull request Nov 2, 2018
twifkak added a commit that referenced this pull request Nov 3, 2018
* cl/219531121 Revision bump for #17481

* cl/219850294 Allow `http` scheme for links in email spec

* cl/219866890 Revision bump for #19043

* cl/219867113 Revision bump for #18981

* cl/219877087 Make ESlint happy.

* cl/219882876 Fix lint for #19096
Enriqe pushed a commit to Enriqe/amphtml that referenced this pull request Nov 28, 2018
* cl/219531121 Revision bump for ampproject#17481

* cl/219850294 Allow `http` scheme for links in email spec

* cl/219866890 Revision bump for ampproject#19043

* cl/219867113 Revision bump for ampproject#18981

* cl/219877087 Make ESlint happy.

* cl/219882876 Fix lint for ampproject#19096
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants