Skip to content

Commit

Permalink
Plumb trust in amp-lightbox. (#25112)
Browse files Browse the repository at this point in the history
  • Loading branch information
William Chou committed Oct 24, 2019
1 parent cd2e517 commit a6feca0
Showing 1 changed file with 27 additions and 16 deletions.
43 changes: 27 additions & 16 deletions extensions/amp-lightbox/0.1/amp-lightbox.js
Original file line number Diff line number Diff line change
Expand Up @@ -181,8 +181,8 @@ class AmpLightbox extends AMP.BaseElement {
this.action_ = Services.actionServiceForDoc(this.element);
this.maybeSetTransparentBody_();

this.registerDefaultAction(unused => this.open_(), 'open');
this.registerAction('close', this.close.bind(this));
this.registerDefaultAction(i => this.open_(i.trust), 'open');
this.registerAction('close', i => this.close(i.trust));
}

/**
Expand Down Expand Up @@ -259,9 +259,10 @@ class AmpLightbox extends AMP.BaseElement {
}

/**
* @param {!ActionTrust} trust
* @private
*/
open_() {
open_(trust) {
if (this.active_) {
return;
}
Expand All @@ -277,17 +278,18 @@ class AmpLightbox extends AMP.BaseElement {
const {promise, resolve} = new Deferred();
this.getViewport()
.enterLightboxMode(this.element, promise)
.then(() => this.finalizeOpen_(resolve));
.then(() => this.finalizeOpen_(resolve, trust));
}

/** @override */
mutatedAttributesCallback(mutations) {
const open = mutations['open'];
if (open !== undefined) {
// Mutations via AMP.setState() require high trust.
if (open) {
this.open_();
this.open_(ActionTrust.HIGH);
} else {
this.close();
this.close(ActionTrust.HIGH);
}
}
}
Expand All @@ -306,9 +308,10 @@ class AmpLightbox extends AMP.BaseElement {

/**
* @param {!Function} callback Called when open animation completes.
* @param {!ActionTrust} trust
* @private
*/
finalizeOpen_(callback) {
finalizeOpen_(callback, trust) {
const {element} = this;

const {
Expand Down Expand Up @@ -372,7 +375,7 @@ class AmpLightbox extends AMP.BaseElement {
const owners = Services.ownersForDoc(this.element);
owners.scheduleLayout(this.element, container);
owners.scheduleResume(this.element, container);
this.triggerEvent_(LightboxEvents.OPEN);
this.triggerEvent_(LightboxEvents.OPEN, trust);

this.getHistory_()
.push(this.close.bind(this))
Expand All @@ -395,7 +398,8 @@ class AmpLightbox extends AMP.BaseElement {

this.closeButtonHeader_ = header;

listenOnce(header, 'click', () => this.close());
// Click gesture is high trust.
listenOnce(header, 'click', () => this.close(ActionTrust.HIGH));

element.insertBefore(header, this.container_);

Expand Down Expand Up @@ -433,14 +437,17 @@ class AmpLightbox extends AMP.BaseElement {
closeOnEscape_(event) {
if (event.key == Keys.ESCAPE) {
event.preventDefault();
this.close();
// Keypress gesture is high trust.
this.close(ActionTrust.HIGH);
}
}

/**
* Closes the lightbox.
*
* @param {!ActionTrust} trust
*/
close() {
close(trust) {
if (!this.active_) {
return;
}
Expand All @@ -453,13 +460,16 @@ class AmpLightbox extends AMP.BaseElement {
}
this.getViewport()
.leaveLightboxMode(this.element)
.then(() => this.finalizeClose_());
.then(() => this.finalizeClose_(trust));
}

/**
* Clean up when closing lightbox.
*
* @param {!ActionTrust} trust
* @private
*/
finalizeClose_() {
finalizeClose_(trust) {
const {element} = this;
const event = ++this.eventCounter_;

Expand Down Expand Up @@ -499,7 +509,7 @@ class AmpLightbox extends AMP.BaseElement {
dev().assertElement(this.container_)
);
this.active_ = false;
this.triggerEvent_(LightboxEvents.CLOSE);
this.triggerEvent_(LightboxEvents.CLOSE, trust);
}

/**
Expand Down Expand Up @@ -684,11 +694,12 @@ class AmpLightbox extends AMP.BaseElement {
* Triggeres event to window.
*
* @param {string} name
* @param {!ActionTrust} trust
* @private
*/
triggerEvent_(name) {
triggerEvent_(name, trust) {
const event = createCustomEvent(this.win, `${TAG}.${name}`, dict({}));
this.action_.trigger(this.element, name, event, ActionTrust.HIGH);
this.action_.trigger(this.element, name, event, trust);
}
}

Expand Down

0 comments on commit a6feca0

Please sign in to comment.