2019-10-12 21:52:04 +05:30
|
|
|
import { parseBoolean } from './lib/utils/common_utils';
|
2019-02-15 15:39:39 +05:30
|
|
|
import axios from './lib/utils/axios_utils';
|
|
|
|
import { __ } from './locale';
|
|
|
|
import Flash from './flash';
|
|
|
|
|
2019-10-12 21:52:04 +05:30
|
|
|
const DEFERRED_LINK_CLASS = 'deferred-link';
|
|
|
|
|
2019-02-15 15:39:39 +05:30
|
|
|
export default class PersistentUserCallout {
|
2020-01-01 13:55:28 +05:30
|
|
|
constructor(container, options = container.dataset) {
|
|
|
|
const { dismissEndpoint, featureId, deferLinks } = options;
|
2019-02-15 15:39:39 +05:30
|
|
|
this.container = container;
|
|
|
|
this.dismissEndpoint = dismissEndpoint;
|
|
|
|
this.featureId = featureId;
|
2019-10-12 21:52:04 +05:30
|
|
|
this.deferLinks = parseBoolean(deferLinks);
|
2019-02-15 15:39:39 +05:30
|
|
|
|
|
|
|
this.init();
|
|
|
|
}
|
|
|
|
|
|
|
|
init() {
|
|
|
|
const closeButton = this.container.querySelector('.js-close');
|
|
|
|
closeButton.addEventListener('click', event => this.dismiss(event));
|
2019-10-12 21:52:04 +05:30
|
|
|
|
|
|
|
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 });
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2019-02-15 15:39:39 +05:30
|
|
|
}
|
|
|
|
|
2019-10-12 21:52:04 +05:30
|
|
|
dismiss(event, deferredLinkOptions = null) {
|
2019-02-15 15:39:39 +05:30
|
|
|
event.preventDefault();
|
|
|
|
|
|
|
|
axios
|
|
|
|
.post(this.dismissEndpoint, {
|
|
|
|
feature_name: this.featureId,
|
|
|
|
})
|
|
|
|
.then(() => {
|
|
|
|
this.container.remove();
|
2019-10-12 21:52:04 +05:30
|
|
|
|
|
|
|
if (deferredLinkOptions) {
|
|
|
|
const { href, target } = deferredLinkOptions;
|
|
|
|
window.open(href, target);
|
|
|
|
}
|
2019-02-15 15:39:39 +05:30
|
|
|
})
|
|
|
|
.catch(() => {
|
|
|
|
Flash(__('An error occurred while dismissing the alert. Refresh the page and try again.'));
|
|
|
|
});
|
|
|
|
}
|
2019-07-07 11:18:12 +05:30
|
|
|
|
2020-01-01 13:55:28 +05:30
|
|
|
static factory(container, options) {
|
2019-07-07 11:18:12 +05:30
|
|
|
if (!container) {
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
|
2020-01-01 13:55:28 +05:30
|
|
|
return new PersistentUserCallout(container, options);
|
2019-07-07 11:18:12 +05:30
|
|
|
}
|
2019-02-15 15:39:39 +05:30
|
|
|
}
|