2019-08-05 05:00:34 -04:00
|
|
|
import { parseBoolean } from './lib/utils/common_utils';
|
2018-12-06 08:41:20 -05:00
|
|
|
import axios from './lib/utils/axios_utils';
|
|
|
|
import { __ } from './locale';
|
|
|
|
import Flash from './flash';
|
|
|
|
|
2019-08-05 05:00:34 -04:00
|
|
|
const DEFERRED_LINK_CLASS = 'deferred-link';
|
|
|
|
|
2018-12-06 08:41:20 -05:00
|
|
|
export default class PersistentUserCallout {
|
|
|
|
constructor(container) {
|
2019-08-05 05:00:34 -04:00
|
|
|
const { dismissEndpoint, featureId, deferLinks } = container.dataset;
|
2018-12-06 08:41:20 -05:00
|
|
|
this.container = container;
|
|
|
|
this.dismissEndpoint = dismissEndpoint;
|
|
|
|
this.featureId = featureId;
|
2019-08-05 05:00:34 -04:00
|
|
|
this.deferLinks = parseBoolean(deferLinks);
|
2018-12-06 08:41:20 -05:00
|
|
|
|
|
|
|
this.init();
|
|
|
|
}
|
|
|
|
|
|
|
|
init() {
|
|
|
|
const closeButton = this.container.querySelector('.js-close');
|
|
|
|
closeButton.addEventListener('click', event => this.dismiss(event));
|
2019-08-05 05:00:34 -04:00
|
|
|
|
|
|
|
if (this.deferLinks) {
|
|
|
|
this.container.addEventListener('click', event => {
|
|
|
|
const isDeferredLink = event.target.classList.contains(DEFERRED_LINK_CLASS);
|
|
|
|
|
|
|
|
if (isDeferredLink) {
|
|
|
|
const { href, target } = event.target;
|
|
|
|
|
|
|
|
this.dismiss(event, { href, target });
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2018-12-06 08:41:20 -05:00
|
|
|
}
|
|
|
|
|
2019-08-05 05:00:34 -04:00
|
|
|
dismiss(event, deferredLinkOptions = null) {
|
2018-12-06 08:41:20 -05:00
|
|
|
event.preventDefault();
|
|
|
|
|
|
|
|
axios
|
|
|
|
.post(this.dismissEndpoint, {
|
|
|
|
feature_name: this.featureId,
|
|
|
|
})
|
|
|
|
.then(() => {
|
|
|
|
this.container.remove();
|
2019-08-05 05:00:34 -04:00
|
|
|
|
|
|
|
if (deferredLinkOptions) {
|
|
|
|
const { href, target } = deferredLinkOptions;
|
|
|
|
window.open(href, target);
|
|
|
|
}
|
2018-12-06 08:41:20 -05:00
|
|
|
})
|
|
|
|
.catch(() => {
|
|
|
|
Flash(__('An error occurred while dismissing the alert. Refresh the page and try again.'));
|
|
|
|
});
|
|
|
|
}
|
2019-02-19 16:26:07 -05:00
|
|
|
|
|
|
|
static factory(container) {
|
|
|
|
if (!container) {
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
|
|
|
|
return new PersistentUserCallout(container);
|
|
|
|
}
|
2018-12-06 08:41:20 -05:00
|
|
|
}
|