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
✨auto-lightbox carousels under experiment #20910
Changes from 13 commits
20faf6f
fa481d2
6752de8
b30a981
08e9314
a9db67b
1474b52
f0aa4a1
0bc467d
83fddb1
0d6d084
721053a
d3104e2
e40fa7b
c80ddd0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
/** | ||
* Copyright 2019 The AMP HTML Authors. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS-IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
import {devAssert} from '../../../src/log'; | ||
import {isActionableByTap} from '../../../src/auto-lightbox'; | ||
import {isExperimentOn} from '../../../src/experiments'; | ||
import {iterateCursor} from '../../../src/dom'; | ||
import {resolveFalse, resolveTrue} from './utils/promise'; | ||
import {toWin} from '../../../src/types'; | ||
import {tryResolve} from '../../../src/utils/promise'; | ||
|
||
const MIN_IMG_SLIDE_AREA_RATIO = 0.5; | ||
|
||
export class CarouselCriteria { | ||
/** | ||
* @param {!Element} element | ||
* @return {!Promise<boolean>} | ||
*/ | ||
static meetsAll(element) { | ||
const win = toWin(element.ownerDocument.defaultView); | ||
|
||
if (!isExperimentOn(win, 'amp-auto-lightbox-carousel')) { | ||
return resolveFalse(); | ||
} | ||
|
||
const slides = element.querySelectorAll('.amp-carousel-slide'); | ||
const images = element.querySelectorAll('amp-img'); | ||
|
||
if (images.length < 1) { | ||
return resolveFalse(); | ||
} | ||
|
||
if (slides.length != images.length) { | ||
return resolveFalse(); | ||
} | ||
|
||
let promise = resolveTrue(); | ||
|
||
iterateCursor(slides, slide => { | ||
promise = promise.then(previousWasAccepted => { | ||
if (!previousWasAccepted) { | ||
return false; | ||
} | ||
return SlideCriteria.meetsAll(slide); | ||
}); | ||
}); | ||
|
||
return promise; | ||
} | ||
} | ||
|
||
class SlideCriteria { | ||
|
||
/** | ||
* @param {!Element} element | ||
* @return {!Promise<boolean>} | ||
*/ | ||
static meetsAll(element) { | ||
if (element.tagName == 'AMP-IMG') { | ||
return tryResolve(() => !isActionableByTap(element)); | ||
} | ||
|
||
const img = element.querySelector('amp-img'); | ||
if (!img) { | ||
return resolveFalse(); | ||
} | ||
|
||
const slideMeetsSizingPromise = | ||
SlideCriteria.meetsSizingCriteria(img, element); | ||
|
||
return slideMeetsSizingPromise.then(slideMeetsSizingCriteria => { | ||
if (!slideMeetsSizingCriteria) { | ||
return false; | ||
} | ||
return !isActionableByTap(element); | ||
}); | ||
} | ||
|
||
/** | ||
* @param {!AmpElement} img | ||
* @param {!Element} slide | ||
* @return {!Promise<boolean>} | ||
*/ | ||
static meetsSizingCriteria(img, slide) { | ||
devAssert(img.tagName == 'AMP-IMG'); | ||
|
||
return img.getImpl().then(impl => new Promise(resolve => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I believe return img.getImpl().then(impl => {
return impl.measureElement(() => {
/*...*/
return (imageArea/slideArea >= MIN_IMG_SLIDE_AREA_RATIO);
});
}); There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unfortunately the measure promise is shared by the entire batch so returning values is not possible. |
||
impl.measureElement(() => { | ||
const { | ||
width: imgWidth, | ||
height: imgHeight, | ||
} = img.getLayoutBox(); | ||
|
||
const { | ||
width: slideWidth, | ||
height: slideHeight, | ||
} = slide./*OK*/getBoundingClientRect(); | ||
|
||
const imgArea = imgWidth * imgHeight; | ||
const slideArea = slideWidth * slideHeight; | ||
|
||
resolve((imgArea / slideArea) >= MIN_IMG_SLIDE_AREA_RATIO); | ||
}); | ||
})); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
there is a race condition since it carousel extension is not loaded yet, we don't have this class.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nope, this runs after
layoutCallback
.