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

111 lines
3.2 KiB
JavaScript
Raw Normal View History

2018-03-17 18:26:18 +05:30
import _ from 'underscore';
const hideFlash = (flashEl, fadeTransition = true) => {
if (fadeTransition) {
Object.assign(flashEl.style, {
transition: 'opacity .3s',
opacity: '0',
2017-08-17 22:00:37 +05:30
});
2018-03-17 18:26:18 +05:30
}
2017-09-10 17:25:29 +05:30
2018-12-13 13:39:08 +05:30
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-09-10 17:25:29 +05:30
2018-03-17 18:26:18 +05:30
if (!fadeTransition) flashEl.dispatchEvent(new Event('transitionend'));
};
2017-09-10 17:25:29 +05:30
2018-03-17 18:26:18 +05:30
const createAction = config => `
<a
href="${config.href || '#'}"
class="flash-action"
${config.href ? '' : 'role="button"'}
>
${_.escape(config.title)}
</a>
`;
2017-09-10 17:25:29 +05:30
2018-12-13 13:39:08 +05:30
const createFlashEl = (message, type, isFixedLayout = false) => `
2018-03-17 18:26:18 +05:30
<div
class="flash-${type}"
>
<div
2018-12-13 13:39:08 +05:30
class="flash-text ${
isFixedLayout ? 'container-fluid container-limited limit-container-width' : ''
}"
2018-03-17 18:26:18 +05:30
>
${_.escape(message)}
</div>
</div>
`;
const removeFlashClickListener = (flashEl, fadeTransition) => {
flashEl.addEventListener('click', () => hideFlash(flashEl, fadeTransition));
};
/*
* 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 {String} message Flash message text
* @param {String} type Type of Flash, it can be `notice` or `alert` (default)
* @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
*/
const createFlash = function createFlash(
message,
type = 'alert',
parent = document,
actionConfig = null,
fadeTransition = true,
addBodyClass = false,
) {
const flashContainer = parent.querySelector('.flash-container');
2018-12-13 13:39:08 +05:30
const navigation = parent.querySelector('.content');
2018-03-17 18:26:18 +05:30
if (!flashContainer) return null;
2018-12-13 13:39:08 +05:30
const isFixedLayout = navigation
? navigation.parentNode.classList.contains('container-limited')
: true;
2018-03-17 18:26:18 +05:30
2018-12-13 13:39:08 +05:30
flashContainer.innerHTML = createFlashEl(message, type, isFixedLayout);
2018-03-17 18:26:18 +05:30
const flashEl = flashContainer.querySelector(`.flash-${type}`);
removeFlashClickListener(flashEl, fadeTransition);
if (actionConfig) {
flashEl.innerHTML += createAction(actionConfig);
if (actionConfig.clickHandler) {
2018-12-13 13:39:08 +05:30
flashEl
.querySelector('.flash-action')
.addEventListener('click', e => actionConfig.clickHandler(e));
2017-08-17 22:00:37 +05:30
}
}
2016-09-13 17:45:13 +05:30
2018-03-17 18:26:18 +05:30
flashContainer.style.display = 'block';
if (addBodyClass) document.body.classList.add('flash-shown');
return flashContainer;
};
2018-12-13 13:39:08 +05:30
export { createFlash as default, createFlashEl, createAction, hideFlash, removeFlashClickListener };
2018-03-17 18:26:18 +05:30
window.Flash = createFlash;