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

add mutateElement to amp-geo for layers support. #15226

Merged
merged 9 commits into from May 15, 2018

Conversation

jpettitt
Copy link
Contributor

Adds mutateElement in layoutCallback to let layers know when change the body classes. Per #14846 and #15158

// Let the runtime know we just if we added/changed classes that
// may case a re-layout.
if (this.mutated_) {
return this.mutateElement(() => {
Copy link
Contributor

Choose a reason for hiding this comment

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

This needs to be down where the body mutations happen.

Copy link
Contributor Author

@jpettitt jpettitt May 11, 2018

Choose a reason for hiding this comment

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

The issue I had is if I do it in the buildCallback then it fails because the element isn't attached ... layoutCallback was the earliest that it would work. This may be me not quite understanding how it all hangs together.

Copy link
Contributor

Choose a reason for hiding this comment

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

then it fails because the element isn't attached

How's it not attached?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

the getResources() in baseElement.measureMutateElement() fails with "'no resources yet, since element is not attached"

Copy link
Contributor

Choose a reason for hiding this comment

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

Stack please. It should definitely have connected by the time #buildCallback runs.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah, found it, I wasn't attaching the element in my unit tests :-( Amended code shortly.

@@ -204,6 +205,8 @@ export class AmpGeo extends AMP.BaseElement {
return waitForBodyPromise(doc).then(() => {
self.findCountry_(doc);
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: self should just be this.

@@ -214,10 +217,17 @@ export class AmpGeo extends AMP.BaseElement {
states.ISOCountry = self.country_;

for (let group = 0; group < self.matchedGroups_.length; group++) {
Copy link
Contributor

Choose a reason for hiding this comment

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

A map?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

self.mutateElement(() => {
// add classes to <body>
doc.body.classList.add.apply(doc.body.classList, classesToAdd);
}, doc.body);
Copy link
Contributor

Choose a reason for hiding this comment

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

The config.AmpBind stuff should be in this block, too.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

}
doc.body.classList.add(COUNTRY_PREFIX + this.country_);
doc.body.classList.remove('amp-geo-pending');
/* @type {Array<string>} */
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: Variables don't need type annotations, only properties.

self.mutateElement(() => {
// Always remove the pending class
classesToRemove.push('amp-geo-pending');
doc.body.classList.remove.apply(
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: Save doc.body.classList to a variable.

const classList = doc.body.classList;
// Always remove the pending class
classesToRemove.push('amp-geo-pending');
classList.remove.apply(classList, classesToRemove);
Copy link
Contributor

Choose a reason for hiding this comment

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

Eek, just remembered that IE doesn't support multi-parameter add and remove: https://developer.mozilla.org/en-US/docs/Web/API/Element/classList#Browser_compatibility

@jridgewell jridgewell merged commit 0bd2d3c into ampproject:master May 15, 2018
@jpettitt jpettitt deleted the layers-geo-integration branch May 15, 2018 18:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants