debian-mirror-gitlab/app/assets/javascripts/persistent_user_callout.js

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

109 lines
2.8 KiB
JavaScript
Raw Normal View History

2021-09-30 23:02:18 +05:30
import createFlash from './flash';
2019-02-15 15:39:39 +05:30
import axios from './lib/utils/axios_utils';
2021-03-11 19:13:27 +05:30
import { parseBoolean } from './lib/utils/common_utils';
2019-02-15 15:39:39 +05:30
import { __ } from './locale';
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) {
2022-04-04 11:22:00 +05:30
const { dismissEndpoint, featureId, groupId, deferLinks } = options;
2019-02-15 15:39:39 +05:30
this.container = container;
this.dismissEndpoint = dismissEndpoint;
this.featureId = featureId;
2022-04-04 11:22:00 +05:30
this.groupId = groupId;
2019-10-12 21:52:04 +05:30
this.deferLinks = parseBoolean(deferLinks);
2022-05-07 20:08:51 +05:30
this.closeButtons = this.container.querySelectorAll('.js-close');
2019-02-15 15:39:39 +05:30
this.init();
}
init() {
2020-07-28 23:09:34 +05:30
const followLink = this.container.querySelector('.js-follow-link');
2020-05-24 23:13:21 +05:30
2022-05-07 20:08:51 +05:30
if (this.closeButtons.length) {
this.handleCloseButtonCallout();
2020-07-28 23:09:34 +05:30
} else if (followLink) {
this.handleFollowLinkCallout(followLink);
2020-05-24 23:13:21 +05:30
}
2020-07-28 23:09:34 +05:30
}
2020-05-24 23:13:21 +05:30
2022-05-07 20:08:51 +05:30
handleCloseButtonCallout() {
this.closeButtons.forEach((closeButton) => {
closeButton.addEventListener('click', this.dismiss);
});
2019-10-12 21:52:04 +05:30
if (this.deferLinks) {
2021-03-08 18:12:59 +05:30
this.container.addEventListener('click', (event) => {
2019-10-12 21:52:04 +05:30
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
}
2020-07-28 23:09:34 +05:30
handleFollowLinkCallout(followLink) {
2021-03-08 18:12:59 +05:30
followLink.addEventListener('click', (event) => this.registerCalloutWithLink(event));
2020-07-28 23:09:34 +05:30
}
2022-05-07 20:08:51 +05:30
dismiss = (event, deferredLinkOptions = null) => {
2019-02-15 15:39:39 +05:30
event.preventDefault();
axios
.post(this.dismissEndpoint, {
feature_name: this.featureId,
2022-04-04 11:22:00 +05:30
group_id: this.groupId,
2019-02-15 15:39:39 +05:30
})
.then(() => {
this.container.remove();
2022-05-07 20:08:51 +05:30
this.closeButtons.forEach((closeButton) => {
closeButton.removeEventListener('click', this.dismiss);
});
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(() => {
2021-09-30 23:02:18 +05:30
createFlash({
message: __(
'An error occurred while dismissing the alert. Refresh the page and try again.',
),
});
2019-02-15 15:39:39 +05:30
});
2022-05-07 20:08:51 +05:30
};
2019-07-07 11:18:12 +05:30
2020-07-28 23:09:34 +05:30
registerCalloutWithLink(event) {
event.preventDefault();
const { href } = event.currentTarget;
axios
.post(this.dismissEndpoint, {
feature_name: this.featureId,
})
.then(() => {
window.location.assign(href);
})
.catch(() => {
2021-09-30 23:02:18 +05:30
createFlash({
message: __(
2020-07-28 23:09:34 +05:30
'An error occurred while acknowledging the notification. Refresh the page and try again.',
),
2021-09-30 23:02:18 +05:30
});
2020-07-28 23:09:34 +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
}