feat: add constants popup to calculator sample
This commit is contained in:
@@ -256,13 +256,22 @@ modeButton.addEventListener('click', (event) => {
|
|||||||
openModeMenu();
|
openModeMenu();
|
||||||
});
|
});
|
||||||
|
|
||||||
window.addEventListener('resize', closeModeMenu);
|
window.addEventListener('resize', () => {
|
||||||
window.addEventListener('scroll', closeModeMenu, true);
|
closeModeMenu();
|
||||||
|
closeConstMenu();
|
||||||
|
});
|
||||||
|
window.addEventListener('scroll', () => {
|
||||||
|
closeModeMenu();
|
||||||
|
closeConstMenu();
|
||||||
|
}, true);
|
||||||
|
|
||||||
window.addEventListener('click', (event) => {
|
window.addEventListener('click', (event) => {
|
||||||
if (modeMenuEl && !event.target.closest('.mode-menu') && event.target !== modeButton) {
|
if (modeMenuEl && !event.target.closest('.mode-menu') && event.target !== modeButton) {
|
||||||
closeModeMenu();
|
closeModeMenu();
|
||||||
}
|
}
|
||||||
|
if (constMenuEl && !event.target.closest('.mode-menu') && event.target !== constButton) {
|
||||||
|
closeConstMenu();
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
pasteButton.addEventListener('click', async () => {
|
pasteButton.addEventListener('click', async () => {
|
||||||
@@ -287,7 +296,54 @@ pasteButton.addEventListener('click', async () => {
|
|||||||
|
|
||||||
upButton.addEventListener('click', () => {});
|
upButton.addEventListener('click', () => {});
|
||||||
|
|
||||||
constButton.addEventListener('click', () => {});
|
const constants = [
|
||||||
|
{ label: 'π', value: Math.PI },
|
||||||
|
{ label: 'e', value: Math.E },
|
||||||
|
{ label: 'φ', value: (1 + Math.sqrt(5)) / 2 },
|
||||||
|
{ label: 'g', value: 9.80665 },
|
||||||
|
{ label: 'c', value: 299792458 },
|
||||||
|
];
|
||||||
|
|
||||||
|
let constMenuEl = null;
|
||||||
|
|
||||||
|
function closeConstMenu() {
|
||||||
|
if (constMenuEl) {
|
||||||
|
constMenuEl.remove();
|
||||||
|
constMenuEl = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function openConstMenu() {
|
||||||
|
closeConstMenu();
|
||||||
|
const rect = constButton.getBoundingClientRect();
|
||||||
|
constMenuEl = document.createElement('div');
|
||||||
|
constMenuEl.className = 'mode-menu';
|
||||||
|
constMenuEl.style.top = `${rect.bottom + 6 + window.scrollY}px`;
|
||||||
|
constMenuEl.style.left = `${rect.left + window.scrollX}px`;
|
||||||
|
constants.forEach((constant) => {
|
||||||
|
const button = document.createElement('button');
|
||||||
|
button.type = 'button';
|
||||||
|
button.className = 'mode-menu-item';
|
||||||
|
button.textContent = constant.label;
|
||||||
|
button.addEventListener('click', () => {
|
||||||
|
pushEditingValueIfNeeded();
|
||||||
|
calc.push(constant.value);
|
||||||
|
render();
|
||||||
|
closeConstMenu();
|
||||||
|
focusInput();
|
||||||
|
});
|
||||||
|
constMenuEl.appendChild(button);
|
||||||
|
});
|
||||||
|
document.body.appendChild(constMenuEl);
|
||||||
|
const menuRect = constMenuEl.getBoundingClientRect();
|
||||||
|
const maxLeft = Math.max(8, window.innerWidth - menuRect.width - 8);
|
||||||
|
constMenuEl.style.left = `${Math.max(8, Math.min(maxLeft, rect.left + window.scrollX))}px`;
|
||||||
|
}
|
||||||
|
|
||||||
|
constButton.addEventListener('click', (event) => {
|
||||||
|
event.stopPropagation();
|
||||||
|
openConstMenu();
|
||||||
|
});
|
||||||
|
|
||||||
leftButton.addEventListener('click', () => {});
|
leftButton.addEventListener('click', () => {});
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user