Skip to content

Commit

Permalink
new: Modern alert dialogs
Browse files Browse the repository at this point in the history
dev: WIP notify dialog
  • Loading branch information
lscambo13 committed Jan 3, 2024
1 parent becc930 commit 5210aec
Show file tree
Hide file tree
Showing 6 changed files with 322 additions and 16 deletions.
12 changes: 6 additions & 6 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
});
}
</script> -->
<link rel="manifest" href="manifest.json?v=1703602562285">
<link rel="icon" type="image/x-icon" href="./favicon.ico?v=1703602562285">
<link rel="manifest" href="manifest.json?v=1703602562286">
<link rel="icon" type="image/x-icon" href="./favicon.ico?v=1703602562286">
<title>Search &bull; Casa Mia</title>
<meta name="theme-color" content="black">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
Expand All @@ -29,10 +29,10 @@
rel="stylesheet" />
<link rel="stylesheet" href="./icons.css" />
<link rel="stylesheet"
href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css?v=1703602562285">
<link rel="stylesheet" href="./animations.css?v=1703602562285">
<link rel="stylesheet" href="./style.css?v=1703602562285" />
<script src="./index.js?v=1703602562285" type="module"></script>
href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css?v=1703602562286">
<link rel="stylesheet" href="./animations.css?v=1703602562286">
<link rel="stylesheet" href="./style.css?v=1703602562286" />
<script src="./index.js?v=1703602562286" type="module"></script>
</head>

<body>
Expand Down
4 changes: 3 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ import { isItChristmas } from './js_modules/utils/letItSnow.js';
import { InputDialog } from './js_modules/utils/inputDialog.js';
import { isTouchDevice } from './js_modules/utils/isTouchDevice.js';
import { enableSubmitButton } from './js_modules/utils/enableSubmitButton.js';
import { genericAlert } from './js_modules/utils/alertDialog.js';

const bottomFilmRollContainer = document.getElementById('wallpapers');
const wrap = document.getElementById('wrap');
Expand Down Expand Up @@ -372,7 +373,8 @@ document.addEventListener('DOMContentLoaded', () => {
preOnboarding().then(() => {
if (isTouchDevice()) {
setTimeout(() => {
alert('Press and hold anywhere to open settings.');
genericAlert('Information',
'Press and hold on empty area to open settings.');
}, 5000);
}
postOnboarding();
Expand Down
10 changes: 6 additions & 4 deletions js_modules/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
import { resetAll, resetBookmarks } from './preferences.js';
import { cliUnexpectedCmdText } from './strings.js';
import { fetchBookmarks } from './utils.js';
import { genericAlert } from './utils/alertDialog.js';
import { downloadFile } from './utils/downloadFile.js';


Expand Down Expand Up @@ -32,7 +33,8 @@ function searchViaCli(url, searchTerm) {
// value = encodeURIComponent(value);
const o = window.open(`${url}${value}`, '_blank');
if (o == null) {
alert(`Allow pop-ups for this feature to work properly`);
genericAlert('Information',
'Allow pop-ups for this feature to work properly.');
return;
};
});
Expand All @@ -50,14 +52,14 @@ export function cliParse(input) {
case 'reset':
if (input[1] == 'bookmarks') resetBookmarks();
else if (input[1] == 'all') resetAll();
else alert(cliUnexpectedCmdText);
else genericAlert('Error', cliUnexpectedCmdText);
break;
case 'fetch default':
fetchBookmarks();
break;
case 'dl':
if (input[1]) parseDL(input[1]);
else alert(cliUnexpectedCmdText);
else genericAlert('Error', cliUnexpectedCmdText);
break;
case 'g':
searchViaCli(GOOGLE_SEARCH_DOMAIN, forBatchSearch);
Expand All @@ -69,7 +71,7 @@ export function cliParse(input) {
searchViaCli(DUCKDUCKGO_SEARCH_DOMAIN, forBatchSearch);
break;
default:
alert(cliUnexpectedCmdText);
genericAlert('Error', cliUnexpectedCmdText);
};
}

14 changes: 9 additions & 5 deletions js_modules/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ import {
cliParse,
} from './cli.js';
import { isUrlValid } from './validators.js';
import { genericAlert } from './utils/alertDialog.js';

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

function loadSearchDomain(input) {
let domain = localStorage.getItem('default-search-url');
Expand All @@ -35,7 +38,7 @@ export function webSearch() {
window.open(url, '_self');
} else cliParse(input);
} else {
alert('You need to enter a search query.');
genericAlert('Error', MSG);
}
}

Expand All @@ -47,7 +50,7 @@ export function movies() {
const url = EXT_SEARCH_DOMAIN + input + '/Movies/time/desc/1/';
window.open(url);
} else {
alert('You need to enter a search query.');
genericAlert('Error', MSG);
}
}

Expand All @@ -58,7 +61,7 @@ export function tv() {
const url = EXT_SEARCH_DOMAIN + input + '/TV/size/desc/1/';
window.open(url);
} else {
alert('You need to enter a search query.');
genericAlert('Error', MSG);
}
}

Expand All @@ -69,7 +72,7 @@ export function games() {
const url = EXT_SEARCH_DOMAIN + input + '/Games/time/desc/1/';
window.open(url);
} else {
alert('You need to enter a search query.');
genericAlert('Error', MSG);
}
}
export function ebooks() {
Expand All @@ -79,7 +82,7 @@ export function ebooks() {
const url = EXT_SEARCH_DOMAIN + input + '/Other/seeders/desc/1/';
window.open(url);
} else {
alert('You need to enter a search query.');
genericAlert('Error', MSG);
}
};

Expand All @@ -91,6 +94,7 @@ export function switchToCLI(event) {
} else {
btnIcon.className = currentIcon;
}
console.log(event.target.value);
};

export function enterToSearch(event) {
Expand Down
146 changes: 146 additions & 0 deletions js_modules/utils/alertDialog.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
let alertModalContainer;
let modalCancelButton;
let tickBoxField;
let submitButtons;

const showAlertDialog = (
title = null,
description = null,
submitButtonNames = ['Submit A', 'Submit B'],
cancelButtonName = 'Ok',
tickBox = [null, null],
listeners = [],
onInit = null,
) => {
alertModalContainer = document.getElementById('alertDialogContainer');
if (alertModalContainer) alertModalContainer.remove();

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

document.body
.insertAdjacentHTML('afterbegin', `
<div id="alertDialogContainer" class="modalContainer disable-select">
<div class="modal">
${title}
${description}
<div id="alertDialogButtonsBar" class="modalButtonsBar">
<button
id="alertDialogCancelButton"
class="button"
onclick=""
type="button">${cancelButtonName}</button>
</div>
</div>
</div>
`);
if (submitButtonNames) {
submitButtonNames.forEach((e) => {
let id = e.replaceAll(' ', '-').toLowerCase();
id = `ALERT-BUTTON-${id}`;
document.getElementById('alertDialogButtonsBar')
.insertAdjacentHTML('afterbegin', `
<button
id="${id}"
class="alertDialogSubmitButton button">
${e}</button>
`);
});
}
if (tickBox[0]) {
document.getElementById('alertDialogButtonsBar')
.insertAdjacentHTML('beforebegin', `
<label
class="label"
for="tickBoxField">
<input
class="tickBoxField"
type="checkbox"
id="tickBoxField">
<span>${tickBox[0]}</span>
</label>
`);
tickBoxField = document.getElementById('tickBoxField');
};

alertModalContainer = document.getElementById('alertDialogContainer');
modalCancelButton = document.getElementById('alertDialogCancelButton');
submitButtons = document.getElementsByClassName('alertDialogSubmitButton');
document.body.style.overflow = 'hidden';
modalCancelButton.focus();

const promise = new Promise((resolve, reject) => {
alertModalContainer.style.alignItems = 'center';
// alertModalContainer.style.paddingBlockStart = '0em';
alertModalContainer.style.opacity = '1';

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

const resolveModal = () => {
modalCancelButton.removeEventListener('click', resolveModal);
if (tickBox[0]) {
tickBoxField.removeEventListener('change', tickBox[1]);
tickBoxField = tickBoxField.checked;
}
if (listeners) {
listeners.forEach((element, index) => {
submitButtons[index].removeEventListener('click', element);
});
}
alertModalContainer.remove();
document.body.style.overflow = 'auto';
// console.log(result);
resolve(tickBoxField);
};

modalCancelButton.addEventListener('click', resolveModal);
if (listeners) {
listeners.forEach((element, index) => {
submitButtons[index].addEventListener('click', element);
});
}
if (tickBox[0]) {
tickBoxField.addEventListener('change', tickBox[1]);
}
});

if (onInit) onInit();
return promise;
};

export const AlertDialog = {
show: showAlertDialog,
getCancelButton: () => {
return modalCancelButton;
},
getSubmitButtons: () => {
return submitButtons;
},
getCheckboxField: () => {
return tickBoxField;
},
};

export const genericAlert = (title, msg) => {
AlertDialog.show(
title,
msg,
null,
'Ok',
[null, null],
null,
null,
).then((res) => console.log(res))
.catch((e) => console.error(e));
};
Loading

0 comments on commit 5210aec

Please sign in to comment.