2020-04-21 14:09:31 -04:00
|
|
|
import { escape } from 'lodash';
|
2020-10-13 02:09:09 -04:00
|
|
|
import * as Sentry from '~/sentry/wrapper';
|
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',
|
|
|
|
};
|
|
|
|
|
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'));
|
|
|
|
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
|
|
|
|
2017-10-02 08:32:53 -04:00
|
|
|
const createAction = config => `
|
|
|
|
<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) => `
|
2019-11-08 07:06:32 -05:00
|
|
|
<div class="flash-${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
|
|
|
|
2017-10-13 05:35:40 -04:00
|
|
|
const removeFlashClickListener = (flashEl, fadeTransition) => {
|
2019-10-01 20:06:26 -04:00
|
|
|
flashEl
|
|
|
|
.querySelector('.js-close-icon')
|
|
|
|
.addEventListener('click', () => hideFlash(flashEl, fadeTransition));
|
2017-10-13 05:35:40 -04:00
|
|
|
};
|
|
|
|
|
2017-10-04 07:13:11 -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
|
|
|
|
*
|
2017-10-04 09:44:43 -04:00
|
|
|
* @param {String} message Flash message text
|
2020-01-23 13:08:53 -05:00
|
|
|
* @param {String} type Type of Flash, it can be `notice`, `success`, `warning` or `alert` (default)
|
2017-10-04 09:44:43 -04:00
|
|
|
* @param {Object} parent Reference to parent element under which Flash needs to appear
|
|
|
|
* @param {Object} actonConfig Map of config to show action on banner
|
|
|
|
* @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} fadeTransition Boolean to determine whether to fade the alert out
|
2017-10-04 07:13:11 -04:00
|
|
|
*/
|
2020-08-20 05:09:55 -04:00
|
|
|
const deprecatedCreateFlash = function deprecatedCreateFlash(
|
2017-10-04 07:13:11 -04:00
|
|
|
message,
|
2020-01-23 13:08:53 -05:00
|
|
|
type = FLASH_TYPES.ALERT,
|
2017-10-04 07:13:11 -04:00
|
|
|
parent = document,
|
|
|
|
actionConfig = null,
|
|
|
|
fadeTransition = true,
|
2018-01-19 04:38:34 -05:00
|
|
|
addBodyClass = false,
|
2017-10-04 07:13:11 -04:00
|
|
|
) {
|
2017-10-02 08:32:53 -04:00
|
|
|
const flashContainer = parent.querySelector('.flash-container');
|
2017-10-03 10:01:02 -04:00
|
|
|
|
|
|
|
if (!flashContainer) return null;
|
|
|
|
|
2019-09-03 05:03:35 -04:00
|
|
|
flashContainer.innerHTML = createFlashEl(message, type);
|
2017-05-31 10:55:57 -04:00
|
|
|
|
2017-10-02 08:32:53 -04:00
|
|
|
const flashEl = flashContainer.querySelector(`.flash-${type}`);
|
2017-05-25 04:48:10 -04:00
|
|
|
|
2017-10-02 08:32:53 -04:00
|
|
|
if (actionConfig) {
|
|
|
|
flashEl.innerHTML += createAction(actionConfig);
|
|
|
|
|
|
|
|
if (actionConfig.clickHandler) {
|
2018-10-24 15:17:03 -04:00
|
|
|
flashEl
|
|
|
|
.querySelector('.flash-action')
|
|
|
|
.addEventListener('click', e => actionConfig.clickHandler(e));
|
2016-07-24 16:45:11 -04:00
|
|
|
}
|
2017-03-11 02:30:44 -05:00
|
|
|
}
|
2016-07-24 16:45:11 -04:00
|
|
|
|
2019-10-01 20:06:26 -04:00
|
|
|
removeFlashClickListener(flashEl, fadeTransition);
|
|
|
|
|
2017-10-02 08:32:53 -04:00
|
|
|
flashContainer.style.display = 'block';
|
|
|
|
|
2018-01-19 04:38:34 -05:00
|
|
|
if (addBodyClass) document.body.classList.add('flash-shown');
|
|
|
|
|
2017-10-02 08:32:53 -04:00
|
|
|
return flashContainer;
|
|
|
|
};
|
|
|
|
|
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
|
|
|
|
* @param {Object} options.actonConfig Map of config to show action on banner
|
|
|
|
* @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
|
|
|
|
* @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-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')
|
|
|
|
.addEventListener('click', e => actionConfig.clickHandler(e));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
removeFlashClickListener(flashEl, fadeTransition);
|
|
|
|
|
|
|
|
flashContainer.classList.add('gl-display-block');
|
|
|
|
|
|
|
|
if (addBodyClass) document.body.classList.add('flash-shown');
|
|
|
|
|
|
|
|
if (captureError && error) Sentry.captureException(error);
|
|
|
|
|
|
|
|
return flashContainer;
|
|
|
|
};
|
|
|
|
|
2020-01-23 13:08:53 -05:00
|
|
|
export {
|
|
|
|
createFlash as default,
|
2020-08-20 05:09:55 -04:00
|
|
|
deprecatedCreateFlash,
|
2020-01-23 13:08:53 -05:00
|
|
|
createFlashEl,
|
|
|
|
createAction,
|
|
|
|
hideFlash,
|
|
|
|
removeFlashClickListener,
|
|
|
|
FLASH_TYPES,
|
|
|
|
};
|
2020-09-14 05:09:34 -04:00
|
|
|
window.Flash = createFlash;
|