[amp-carousel] Add support for defined number of loops to autoplay#18981
[amp-carousel] Add support for defined number of loops to autoplay#18981aghassemi merged 9 commits intoampproject:masterfrom
Conversation
| attrs: { | ||
| name: "autoplay" | ||
| value: "" | ||
| value_regex: "[0-9]+" |
There was a problem hiding this comment.
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)"
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
user().assert(isFiniteNumber(this.autoplayLoops_)) would be nice
(isFiniteNumber is in types.js)
| // 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) { |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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'); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Done. Put a guard around it that checks that autoplay is set as well.
nainar
left a comment
There was a problem hiding this comment.
@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); |
| this.clearAutoplay(); | ||
| this.hasAutoplay_ = false; | ||
| this.shouldAutoplay_ = this.hasAutoplay_ && this.isLoopingEligible(); | ||
| this.element.removeAttribute('loop'); |
There was a problem hiding this comment.
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) { |
There was a problem hiding this comment.
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'); |
There was a problem hiding this comment.
this.hasLoop_ = false;
this.shouldLoop = false;
too?
d84ee9f to
e4f0255
Compare
| this.clearAutoplay(); | ||
| this.hasAutoplay_ = false; | ||
| this.shouldAutoplay_ = this.hasAutoplay_ && this.isLoopingEligible(); | ||
| if (this.element.hasAttribute('autoplay')) { |
There was a problem hiding this comment.
just the attribute check ignores the isLoopingEligible part. The value of this.shouldAutoplay_ before it changes should be the condition.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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') {
...
}
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
We can also just maintain state to see if loop was added by autoplay status addition
aghassemi
left a comment
There was a problem hiding this comment.
Left one comment, feel free to merge after addressing.
|
@aghassemi made the change in this patch: a247fe3 Will merge if it looks good to you. |
|
LGTM |
* 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
Addresses #18911 for carousel V1 only.