Skip to content

Commit

Permalink
new: Modern notifications for less important messages
Browse files Browse the repository at this point in the history
  • Loading branch information
lscambo13 committed Jan 4, 2024
1 parent 8fa3c40 commit 981c570
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 146 deletions.
1 change: 1 addition & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
</head>

<body>
<div id="notifyContainer" class="notifyContainer disable-select"></div>
<div id="overlay"></div>
<canvas id="canvasFar"></canvas>
<div id="gradient_overlay"></div>
Expand Down
11 changes: 6 additions & 5 deletions js_modules/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
} from './cli.js';
import { isUrlValid } from './validators.js';
import { genericAlert } from './utils/alertDialog.js';
import { Notify } from './utils/notifyDialog.js';

const MSG = 'You must enter a search query to continue.';

Expand Down Expand Up @@ -38,7 +39,7 @@ export function webSearch() {
window.open(url, '_self');
} else cliParse(input);
} else {
genericAlert('Error', MSG);
Notify.show(MSG);
}
}

Expand All @@ -50,7 +51,7 @@ export function movies() {
const url = EXT_SEARCH_DOMAIN + input + '/Movies/time/desc/1/';
window.open(url);
} else {
genericAlert('Error', MSG);
Notify.show(MSG);
}
}

Expand All @@ -61,7 +62,7 @@ export function tv() {
const url = EXT_SEARCH_DOMAIN + input + '/TV/size/desc/1/';
window.open(url);
} else {
genericAlert('Error', MSG);
Notify.show(MSG);
}
}

Expand All @@ -72,7 +73,7 @@ export function games() {
const url = EXT_SEARCH_DOMAIN + input + '/Games/time/desc/1/';
window.open(url);
} else {
genericAlert('Error', MSG);
Notify.show(MSG);
}
}
export function ebooks() {
Expand All @@ -82,7 +83,7 @@ export function ebooks() {
const url = EXT_SEARCH_DOMAIN + input + '/Other/seeders/desc/1/';
window.open(url);
} else {
genericAlert('Error', MSG);
Notify.show(MSG);
}
};

Expand Down
184 changes: 44 additions & 140 deletions js_modules/utils/notifyDialog.js
Original file line number Diff line number Diff line change
@@ -1,152 +1,56 @@
let modalContainer;
let modalSubmitButton;
const notifyModalContainer = document.getElementById('notifyContainer');
let modalCancelButton;
let tickBoxField;
let inputFields;
let notify;

const showInputDialog = (
title = null,
description = null,
inputBoxes = ['Input A', 'Input B'],
submitButtonName = 'Submit',
cancelButtonName = 'Cancel',
tickBox = null,
listeners = [onInput = null, onChange = null],
onInit = null,
const showNotifyDialog = (
description,
ms = '5000',
onClick = null,
) => {
modalContainer = document.getElementById('inputDialogContainer');
if (modalContainer) modalContainer.remove();

document.activeElement.blur();

if (title) {
title = `<h3 id="inputDialogTitle" class="modalTitle">${title}</h3>`;
} else (title = '');
if (description) {
description = `
<h4 id="inputDialogDescription" class="modalDescription">${description}</h4>
`;
} else (description = '');

document.body
.insertAdjacentHTML('afterbegin', `
<div id="inputDialogContainer" class="modalContainer disable-select">
<form autocomplete="off" action="#" class="modal">
${title}
${description}
<div id="inputDialogButtonsBar" class="modalButtonsBar">
<button
id="inputDialogSubmitButton"
class="mainButton button"
disabled = "true"
onclick=""
type="submit">${submitButtonName}</button>
<button
id="inputDialogCancelButton"
class="button"
onclick=""
type="button">${cancelButtonName}</button>
</div>
<form>
const id = Date.now();
notify = document.getElementById(`notifyModal-${id}`);
if (notify) notify.parentNode.remove();

notifyModalContainer.insertAdjacentHTML('afterbegin', `
<div id="notifyModal-${id}" class="notifyModal">
<div class="notifyDescriptionContainer">
<h3 id="notifyDescription-${id}" class="notifyDescription">
${description}
</h3>
<hr id="notifyLoader-${id}" class="notifyLoader">
</div>
<button id="notifyDialogCancelButton-${id}" class="button tinyButton">
x
</button>
</div>
`);
inputBoxes.forEach((e) => {
let id = e.replaceAll(' ', '-').toLowerCase();
id = `MODAL-INPUT-${id}`;
document.getElementById('inputDialogButtonsBar')
.insertAdjacentHTML('beforebegin', `
<label
class="label"
for="${id}">${e}</label>
<input
class="modalInputField"
type="text"
id="${id}">
`);
});
if (tickBox) {
document.getElementById('inputDialogButtonsBar')
.insertAdjacentHTML('beforebegin', `
<label
class="label"
for="tickBoxField">
<input
class="tickBoxField"
type="checkbox"
id="tickBoxField">
<span>${tickBox}</span>
</label>
`);
tickBoxField = document.getElementById('tickBoxField');
};

modalContainer = document.getElementById('inputDialogContainer');
modalSubmitButton = document.getElementById('inputDialogSubmitButton');
modalCancelButton = document.getElementById('inputDialogCancelButton');
inputFields = document.getElementsByClassName('modalInputField');
document.body.style.overflow = 'hidden';

inputFields[0].focus();
if (!cancelButtonName) modalCancelButton.style.display = 'none';
const notifyLoader = document.getElementById(`notifyLoader-${id}`);

const promise = new Promise((resolve, reject) => {
modalContainer.style.paddingBlockStart = '4em';
modalContainer.style.opacity = '1';
notify = document.getElementById(`notifyModal-${id}`);
if (onClick) notify.addEventListner('click', onClick);

const rejectModal = () => {
modalCancelButton.removeEventListener('click', rejectModal);
modalContainer.remove();
document.body.style.overflow = 'auto';
reject(Error(null));
};

const resolveModal = () => {
modalSubmitButton.removeEventListener('click', resolveModal);
if (tickBox) {
tickBoxField.removeEventListener('change', listeners[1]);
tickBoxField = tickBoxField.checked;
}
const inputValues = [];
for (const e of inputFields) {
inputValues.push(e.value);
if (listeners) e.removeEventListener('input', listeners);
}
const result = {
'inputValues': inputValues, 'checkboxChecked': tickBoxField,
};
modalContainer.remove();
document.body.style.overflow = 'auto';

console.log(result);
resolve(result);
};

modalCancelButton.addEventListener('click', rejectModal);
modalSubmitButton.addEventListener('click', resolveModal);
if (listeners[0]) {
for (const e of inputFields) e.addEventListener('input', listeners[0]);
}
if (listeners[1] && tickBox) {
tickBoxField.addEventListener('change', listeners[1]);
}
});
const closeNotification = (event) => {
if (onClick) notify.removeEventListner('click', onClick);
modalCancelButton.removeEventListener('click', closeNotification);
event.target.parentNode.remove();
};

if (onInit) onInit();
return promise;
modalCancelButton = document.getElementById(`notifyDialogCancelButton-${id}`);
modalCancelButton.addEventListener('click', closeNotification);

setTimeout(() => {
const close = modalCancelButton;
setTimeout(() => {
close.click();
}, ms);
notifyLoader.style.transition = ms + 'ms';
notify.style.opacity = '1';
notify.style.marginTop = '2em';
notifyLoader.style.width = '0%';
}, 50);
};

export const InputDialog = {
show: showInputDialog,
getSubmitButton: () => {
return modalSubmitButton;
},
getCancelButton: () => {
return modalCancelButton;
},
getInputFields: () => {
return inputFields;
},
getCheckboxField: (n) => {
return tickBoxField;
},
export const Notify = {
show: showNotifyDialog,
};
65 changes: 64 additions & 1 deletion style.css
Original file line number Diff line number Diff line change
Expand Up @@ -1045,6 +1045,23 @@ input::-webkit-search-cancel-button:hover {
transition: 200ms;
}

.notifyContainer {
color: white;
font-family: 'Ubuntu';
position: fixed;
top: 0;
width: 100%;
height: auto;
background-color: transparent;
z-index: 5;
display: flex;
align-items: center;
justify-content: center;
opacity: 1;
transition: 200ms;
flex-direction: column-reverse;
}

.modal {
min-width: 10em;
width: 90%;
Expand All @@ -1058,6 +1075,27 @@ input::-webkit-search-cancel-button:hover {
box-shadow: rgba(0, 0, 0, 0.2) 0 0 2em 0;
}

.notifyModal {
min-width: 10em;
width: 90%;
max-width: 30em;
display: flex;
flex-direction: row;
background: #2b2b2b;
padding: 1.5em;
margin-top: 0em;
margin-bottom: -1em;
z-index: 5;
border-radius: 1em;
border: 1px solid #ffffff24;
box-shadow: rgba(0, 0, 0, 0.2) 0 0 2em 0;
align-items: center;
opacity: 0;
transition: 200ms;
gap: 1em;
justify-content: space-between;
}

.modalTitle {
padding-bottom: 0.5em;
}
Expand All @@ -1068,6 +1106,17 @@ input::-webkit-search-cancel-button:hover {
font-weight: 100;
}

.notifyDescriptionContainer {
width: 90%;
}

.notifyDescription {
overflow: hidden;
word-wrap: break-word;
font-size: 1em;
font-weight: 200;
}

.modalButtonsBar {
vertical-align: middle;
display: inline-flex;
Expand Down Expand Up @@ -1126,6 +1175,21 @@ input::-webkit-search-cancel-button:hover {
transition: 100ms;
}

.tinyButton {
min-width: 1.75em;
min-height: 1.75em;
max-width: 1.75em;
max-height: 1.75em;
padding-bottom: 2px;
}

.notifyLoader {
width: 100%;
margin-top: 1em;
margin-bottom: -1em;
opacity: 0.1;
}

.button:disabled {
background-color: #fff;
opacity: 0.75;
Expand All @@ -1139,7 +1203,6 @@ input::-webkit-search-cancel-button:hover {
opacity: 0.75;
}


.mainButton {
/* background-color: #8cffc9; */
/* border: 1px solid rgba(206, 206, 206, 0.5); */
Expand Down

0 comments on commit 981c570

Please sign in to comment.