2021-02-18 13:10:41 -05:00
|
|
|
import * as Sentry from '@sentry/browser';
|
2020-04-21 14:09:31 -04:00
|
|
|
import { escape } from 'lodash';
|
2022-01-05 13:13:30 -05:00
|
|
|
import Vue from 'vue';
|
|
|
|
import { GlAlert } from '@gitlab/ui';
|
|
|
|
import { __ } from '~/locale';
|
2019-09-03 05:03:35 -04:00
|
|
|
import { spriteIcon } from './lib/utils/common_utils';
|
2016-07-24 16:45:11 -04:00
|
|
|
|
2020-01-23 13:08:53 -05:00
|
|
|
const FLASH_TYPES = {
|
|
|
|
ALERT: 'alert',
|
|
|
|
NOTICE: 'notice',
|
|
|
|
SUCCESS: 'success',
|
|
|
|
WARNING: 'warning',
|
|
|
|
};
|
|
|
|
|
2022-01-05 13:13:30 -05:00
|
|
|
const VARIANT_SUCCESS = 'success';
|
|
|
|
const VARIANT_WARNING = 'warning';
|
|
|
|
const VARIANT_DANGER = 'danger';
|
|
|
|
const VARIANT_INFO = 'info';
|
|
|
|
const VARIANT_TIP = 'tip';
|
|
|
|
|
2022-02-10 07:18:48 -05:00
|
|
|
const TYPE_TO_VARIANT = {
|
|
|
|
[FLASH_TYPES.ALERT]: VARIANT_DANGER,
|
|
|
|
[FLASH_TYPES.NOTICE]: VARIANT_INFO,
|
|
|
|
[FLASH_TYPES.SUCCESS]: VARIANT_SUCCESS,
|
|
|
|
[FLASH_TYPES.WARNING]: VARIANT_WARNING,
|
|
|
|
};
|
|
|
|
|
2021-10-26 08:12:11 -04:00
|
|
|
const FLASH_CLOSED_EVENT = 'flashClosed';
|
|
|
|
|
2021-04-29 11:10:07 -04:00
|
|
|
const getCloseEl = (flashEl) => {
|
|
|
|
return flashEl.querySelector('.js-close-icon');
|
|
|
|
};
|
|
|
|
|
2017-10-04 07:13:11 -04:00
|
|
|
const hideFlash = (flashEl, fadeTransition = true) => {
|
|
|
|
if (fadeTransition) {
|
|
|
|
Object.assign(flashEl.style, {
|
2020-02-13 07:08:49 -05:00
|
|
|
transition: 'opacity 0.15s',
|
2017-10-04 07:13:11 -04:00
|
|
|
opacity: '0',
|
|
|
|
});
|
|
|
|
}
|
2016-07-24 16:45:11 -04:00
|
|
|
|
2018-10-24 15:17:03 -04:00
|
|
|
flashEl.addEventListener(
|
|
|
|
'transitionend',
|
|
|
|
() => {
|
|
|
|
flashEl.remove();
|
|
|
|
window.dispatchEvent(new Event('resize'));
|
2021-10-26 08:12:11 -04:00
|
|
|
flashEl.dispatchEvent(new Event(FLASH_CLOSED_EVENT));
|
2018-10-24 15:17:03 -04:00
|
|
|
if (document.body.classList.contains('flash-shown'))
|
|
|
|
document.body.classList.remove('flash-shown');
|
|
|
|
},
|
|
|
|
{
|
|
|
|
once: true,
|
|
|
|
passive: true,
|
|
|
|
},
|
|
|
|
);
|
2017-10-04 07:13:11 -04:00
|
|
|
|
|
|
|
if (!fadeTransition) flashEl.dispatchEvent(new Event('transitionend'));
|
2017-10-02 08:32:53 -04:00
|
|
|
};
|
2017-03-11 02:30:44 -05:00
|
|
|
|
2020-12-23 19:10:25 -05:00
|
|
|
const createAction = (config) => `
|
2017-10-02 08:32:53 -04:00
|
|
|
<a
|
|
|
|
href="${config.href || '#'}"
|
|
|
|
class="flash-action"
|
2017-10-04 07:13:11 -04:00
|
|
|
${config.href ? '' : 'role="button"'}
|
2017-10-02 08:32:53 -04:00
|
|
|
>
|
2020-04-21 14:09:31 -04:00
|
|
|
${escape(config.title)}
|
2017-10-02 08:32:53 -04:00
|
|
|
</a>
|
|
|
|
`;
|
2017-05-25 04:48:10 -04:00
|
|
|
|
2019-09-03 05:03:35 -04:00
|
|
|
const createFlashEl = (message, type) => `
|
2022-02-10 07:18:48 -05:00
|
|
|
<div class="flash-${type}" data-testid="alert-${TYPE_TO_VARIANT[type]}">
|
2019-09-03 05:03:35 -04:00
|
|
|
<div class="flash-text">
|
2020-04-21 14:09:31 -04:00
|
|
|
${escape(message)}
|
2019-10-01 20:06:26 -04:00
|
|
|
<div class="close-icon-wrapper js-close-icon">
|
|
|
|
${spriteIcon('close', 'close-icon')}
|
|
|
|
</div>
|
2017-10-02 08:32:53 -04:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
`;
|
2017-05-31 10:55:57 -04:00
|
|
|
|
2021-12-08 07:13:04 -05:00
|
|
|
const addDismissFlashClickListener = (flashEl, fadeTransition) => {
|
2021-05-25 11:10:33 -04:00
|
|
|
// There are some flash elements which do not have a closeEl.
|
|
|
|
// https://gitlab.com/gitlab-org/gitlab/blob/763426ef344488972eb63ea5be8744e0f8459e6b/ee/app/views/layouts/header/_read_only_banner.html.haml
|
|
|
|
getCloseEl(flashEl)?.addEventListener('click', () => hideFlash(flashEl, fadeTransition));
|
2017-10-13 05:35:40 -04:00
|
|
|
};
|
|
|
|
|
2022-01-05 13:13:30 -05:00
|
|
|
/**
|
|
|
|
* Render an alert at the top of the page, or, optionally an
|
|
|
|
* arbitrary existing container.
|
|
|
|
*
|
|
|
|
* This alert is always dismissible.
|
|
|
|
*
|
|
|
|
* Usage:
|
|
|
|
*
|
|
|
|
* 1. Render a new alert
|
|
|
|
*
|
|
|
|
* import { createAlert, ALERT_VARIANTS } from '~/flash';
|
|
|
|
*
|
|
|
|
* createAlert({ message: 'My error message' });
|
|
|
|
* createAlert({ message: 'My warning message', variant: ALERT_VARIANTS.WARNING });
|
|
|
|
*
|
|
|
|
* 2. Dismiss this alert programmatically
|
|
|
|
*
|
|
|
|
* const alert = createAlert({ message: 'Message' });
|
|
|
|
*
|
|
|
|
* // ...
|
|
|
|
*
|
|
|
|
* alert.dismiss();
|
|
|
|
*
|
|
|
|
* 3. Respond to the alert being dismissed
|
|
|
|
*
|
|
|
|
* createAlert({ message: 'Message', onDismiss: () => { ... }});
|
|
|
|
*
|
|
|
|
* @param {Object} options Options to control the flash message
|
|
|
|
* @param {String} options.message Alert message text
|
|
|
|
* @param {String?} options.variant Which GlAlert variant to use, should be VARIANT_SUCCESS, VARIANT_WARNING, VARIANT_DANGER, VARIANT_INFO or VARIANT_TIP. Defaults to VARIANT_DANGER.
|
|
|
|
* @param {Object?} options.parent Reference to parent element under which alert needs to appear. Defaults to `document`.
|
|
|
|
* @param {Function?} options.onDismiss Handler to call when this alert is dismissed.
|
|
|
|
* @param {Object?} options.containerSelector Selector for the container of the alert
|
|
|
|
* @param {Object?} options.primaryButton Object describing primary button of alert
|
|
|
|
* @param {String?} link Href of primary button
|
|
|
|
* @param {String?} text Text of primary button
|
|
|
|
* @param {Function?} clickHandler Handler to call when primary button is clicked on. The click event is sent as an argument.
|
|
|
|
* @param {Object?} options.secondaryButton Object describing secondary button of alert
|
|
|
|
* @param {String?} link Href of secondary button
|
|
|
|
* @param {String?} text Text of secondary button
|
|
|
|
* @param {Function?} clickHandler Handler to call when secondary button is clicked on. The click event is sent as an argument.
|
|
|
|
* @param {Boolean?} options.captureError Whether to send error to Sentry
|
|
|
|
* @param {Object} options.error Error to be captured in Sentry
|
|
|
|
* @returns
|
|
|
|
*/
|
|
|
|
const createAlert = function createAlert({
|
|
|
|
message,
|
|
|
|
variant = VARIANT_DANGER,
|
|
|
|
parent = document,
|
|
|
|
containerSelector = '.flash-container',
|
|
|
|
primaryButton = null,
|
|
|
|
secondaryButton = null,
|
|
|
|
onDismiss = null,
|
|
|
|
captureError = false,
|
|
|
|
error = null,
|
|
|
|
}) {
|
|
|
|
if (captureError && error) Sentry.captureException(error);
|
|
|
|
|
|
|
|
const alertContainer = parent.querySelector(containerSelector);
|
|
|
|
if (!alertContainer) return null;
|
|
|
|
|
|
|
|
const el = document.createElement('div');
|
|
|
|
alertContainer.appendChild(el);
|
|
|
|
|
|
|
|
return new Vue({
|
|
|
|
el,
|
|
|
|
components: {
|
|
|
|
GlAlert,
|
|
|
|
},
|
|
|
|
methods: {
|
|
|
|
/**
|
|
|
|
* Public method to dismiss this alert and removes
|
|
|
|
* this Vue instance.
|
|
|
|
*/
|
|
|
|
dismiss() {
|
|
|
|
if (onDismiss) {
|
|
|
|
onDismiss();
|
|
|
|
}
|
|
|
|
this.$destroy();
|
|
|
|
this.$el.parentNode.removeChild(this.$el);
|
|
|
|
},
|
|
|
|
},
|
|
|
|
render(h) {
|
|
|
|
const on = {};
|
|
|
|
|
|
|
|
on.dismiss = () => {
|
|
|
|
this.dismiss();
|
|
|
|
};
|
|
|
|
|
|
|
|
if (primaryButton?.clickHandler) {
|
|
|
|
on.primaryAction = (e) => {
|
|
|
|
primaryButton.clickHandler(e);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
if (secondaryButton?.clickHandler) {
|
|
|
|
on.secondaryAction = (e) => {
|
|
|
|
secondaryButton.clickHandler(e);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
return h(
|
|
|
|
GlAlert,
|
|
|
|
{
|
|
|
|
props: {
|
|
|
|
dismissible: true,
|
|
|
|
dismissLabel: __('Dismiss'),
|
|
|
|
variant,
|
|
|
|
primaryButtonLink: primaryButton?.link,
|
|
|
|
primaryButtonText: primaryButton?.text,
|
|
|
|
secondaryButtonLink: secondaryButton?.link,
|
|
|
|
secondaryButtonText: secondaryButton?.text,
|
|
|
|
},
|
2022-02-10 07:18:48 -05:00
|
|
|
attrs: {
|
|
|
|
'data-testid': `alert-${variant}`,
|
|
|
|
},
|
2022-01-05 13:13:30 -05:00
|
|
|
on,
|
|
|
|
},
|
|
|
|
message,
|
|
|
|
);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2020-08-11 08:09:55 -04:00
|
|
|
/*
|
|
|
|
* Flash banner supports different types of Flash configurations
|
|
|
|
* along with ability to provide actionConfig which can be used to show
|
|
|
|
* additional action or link on banner next to message
|
|
|
|
*
|
|
|
|
* @param {Object} options Options to control the flash message
|
|
|
|
* @param {String} options.message Flash message text
|
|
|
|
* @param {String} options.type Type of Flash, it can be `notice`, `success`, `warning` or `alert` (default)
|
|
|
|
* @param {Object} options.parent Reference to parent element under which Flash needs to appear
|
2021-01-07 01:10:18 -05:00
|
|
|
* @param {Object} options.actionConfig Map of config to show action on banner
|
2020-08-11 08:09:55 -04:00
|
|
|
* @param {String} href URL to which action config should point to (default: '#')
|
|
|
|
* @param {String} title Title of action
|
|
|
|
* @param {Function} clickHandler Method to call when action is clicked on
|
|
|
|
* @param {Boolean} options.fadeTransition Boolean to determine whether to fade the alert out
|
2022-01-05 13:13:30 -05:00
|
|
|
* @param {Boolean} options.captureError Boolean to determine whether to send error to Sentry
|
|
|
|
* @param {Object} options.error Error to be captured in Sentry
|
2020-08-11 08:09:55 -04:00
|
|
|
*/
|
2020-08-20 05:09:55 -04:00
|
|
|
const createFlash = function createFlash({
|
2020-08-11 08:09:55 -04:00
|
|
|
message,
|
|
|
|
type = FLASH_TYPES.ALERT,
|
|
|
|
parent = document,
|
|
|
|
actionConfig = null,
|
|
|
|
fadeTransition = true,
|
|
|
|
addBodyClass = false,
|
|
|
|
captureError = false,
|
|
|
|
error = null,
|
|
|
|
}) {
|
|
|
|
const flashContainer = parent.querySelector('.flash-container');
|
|
|
|
|
|
|
|
if (!flashContainer) return null;
|
|
|
|
|
|
|
|
flashContainer.innerHTML = createFlashEl(message, type);
|
|
|
|
|
|
|
|
const flashEl = flashContainer.querySelector(`.flash-${type}`);
|
|
|
|
|
|
|
|
if (actionConfig) {
|
|
|
|
flashEl.insertAdjacentHTML('beforeend', createAction(actionConfig));
|
|
|
|
|
|
|
|
if (actionConfig.clickHandler) {
|
|
|
|
flashEl
|
|
|
|
.querySelector('.flash-action')
|
2020-12-23 19:10:25 -05:00
|
|
|
.addEventListener('click', (e) => actionConfig.clickHandler(e));
|
2020-08-11 08:09:55 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-08 07:13:04 -05:00
|
|
|
addDismissFlashClickListener(flashEl, fadeTransition);
|
2020-08-11 08:09:55 -04:00
|
|
|
|
|
|
|
flashContainer.classList.add('gl-display-block');
|
|
|
|
|
|
|
|
if (addBodyClass) document.body.classList.add('flash-shown');
|
|
|
|
|
|
|
|
if (captureError && error) Sentry.captureException(error);
|
|
|
|
|
2021-04-29 11:10:07 -04:00
|
|
|
flashContainer.close = () => {
|
|
|
|
getCloseEl(flashEl).click();
|
|
|
|
};
|
|
|
|
|
2020-08-11 08:09:55 -04:00
|
|
|
return flashContainer;
|
|
|
|
};
|
|
|
|
|
2020-01-23 13:08:53 -05:00
|
|
|
export {
|
|
|
|
createFlash as default,
|
|
|
|
hideFlash,
|
2021-12-08 07:13:04 -05:00
|
|
|
addDismissFlashClickListener,
|
2020-01-23 13:08:53 -05:00
|
|
|
FLASH_TYPES,
|
2021-10-26 08:12:11 -04:00
|
|
|
FLASH_CLOSED_EVENT,
|
2022-01-05 13:13:30 -05:00
|
|
|
createAlert,
|
|
|
|
VARIANT_SUCCESS,
|
|
|
|
VARIANT_WARNING,
|
|
|
|
VARIANT_DANGER,
|
|
|
|
VARIANT_INFO,
|
|
|
|
VARIANT_TIP,
|
2020-01-23 13:08:53 -05:00
|
|
|
};
|