2020-01-30 13:08:57 -05:00
|
|
|
import Vue from 'vue';
|
2021-12-18 07:13:19 -05:00
|
|
|
import Cookies from 'js-cookie';
|
2020-01-30 13:08:57 -05:00
|
|
|
import { parseBoolean } from '~/lib/utils/common_utils';
|
|
|
|
import DismissibleAlert from '~/vue_shared/components/dismissible_alert.vue';
|
|
|
|
|
2021-12-18 07:13:19 -05:00
|
|
|
const getCookieExpirationPeriod = (expirationPeriod) => {
|
|
|
|
const defaultExpirationPeriod = 30;
|
|
|
|
const alertExpirationPeriod = Number(expirationPeriod);
|
|
|
|
|
|
|
|
return !expirationPeriod || Number.isNaN(alertExpirationPeriod)
|
|
|
|
? defaultExpirationPeriod
|
|
|
|
: alertExpirationPeriod;
|
|
|
|
};
|
|
|
|
|
2020-12-23 16:10:24 -05:00
|
|
|
const mountVueAlert = (el) => {
|
2020-01-30 13:08:57 -05:00
|
|
|
const props = {
|
|
|
|
html: el.innerHTML,
|
|
|
|
};
|
|
|
|
const attrs = {
|
|
|
|
...el.dataset,
|
|
|
|
dismissible: parseBoolean(el.dataset.dismissible),
|
|
|
|
};
|
2021-12-18 07:13:19 -05:00
|
|
|
const { dismissCookieName, dismissCookieExpire } = el.dataset;
|
2020-01-30 13:08:57 -05:00
|
|
|
|
|
|
|
return new Vue({
|
|
|
|
el,
|
2021-12-18 07:13:19 -05:00
|
|
|
render(createElement) {
|
|
|
|
return createElement(DismissibleAlert, {
|
|
|
|
props,
|
|
|
|
attrs,
|
|
|
|
on: {
|
|
|
|
alertDismissed() {
|
|
|
|
if (!dismissCookieName) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
Cookies.set(dismissCookieName, true, {
|
|
|
|
expires: getCookieExpirationPeriod(dismissCookieExpire),
|
|
|
|
});
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
2020-01-30 13:08:57 -05:00
|
|
|
},
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
export default () => [...document.querySelectorAll('.js-vue-alert')].map(mountVueAlert);
|