// Alert box design by Igor Ferrão de Souza: https://www.linkedin.com/in/igor-ferr%C3%A3o-de-souza-4122407b/
const cuteAlert = ({
url,
type,
title,
message,
img,
buttonText = 'OK',
confirmText = 'OK',
vibrate = [],
playSound = null,
cancelText = 'Cancel',
closeStyle,
}) => {
return new Promise(resolve => {
const existingAlert = document.querySelector('.alert-wrapper');
if (existingAlert) {
existingAlert.remove();
}
const body = document.querySelector('body');
const scripts = document.getElementsByTagName('script');
let src = '';
for (let script of scripts) {
if (script.src.includes('cutealert.js')) {
src = script.src.substring(0, script.src.lastIndexOf('/'));
}
}
let btnTemplate = `
`;
if (type === 'question') {
btnTemplate = `
`;
}
if (vibrate.length > 0) {
navigator.vibrate(vibrate);
}
if (playSound !== null) {
let sound = new Audio(playSound);
sound.play();
}
const template = `
${
img === undefined
? '
`;
body.insertAdjacentHTML('afterend', template);
const alertWrapper = document.querySelector('.alert-wrapper');
const alertFrame = document.querySelector('.alert-frame');
const alertClose = document.querySelector('.alert-close');
if (type === 'question') {
const confirmButton = document.querySelector('.confirm-button');
const cancelButton = document.querySelector('.cancel-button');
confirmButton.addEventListener('click', () => {
alertWrapper.remove();
resolve('confirm');
});
cancelButton.addEventListener('click', () => {
alertWrapper.remove();
resolve();
});
} else {
const alertButton = document.querySelector('.alert-button');
alertButton.addEventListener('click', () => {
alertWrapper.remove();
resolve('ok');
});
}
alertClose.addEventListener('click', () => {
alertWrapper.remove();
resolve('close');
});
/* alertWrapper.addEventListener('click', () => {
alertWrapper.remove();
resolve();
}); */
alertFrame.addEventListener('click', e => {
e.stopPropagation();
});
});
};
const cuteToast = ({ type, title, message, timer = 5000, vibrate = [], playSound = null }) => {
return new Promise(resolve => {
const body = document.querySelector('body');
const scripts = document.getElementsByTagName('script');
let src = '';
for (let script of scripts) {
if (script.src.includes('cutealert.js')) {
src = script.src.substring(0, script.src.lastIndexOf('/'));
}
}
let templateContainer = document.querySelector('.toast-container');
if (!templateContainer) {
body.insertAdjacentHTML(
'afterend',
'
',
);
templateContainer = document.querySelector('.toast-container');
}
const toastId = id();
const templateContent = `
${title}
${message}
X