|
|
| class Numpad { |
| constructor(containerId, options = {}) { |
| this.container = document.getElementById(containerId); |
| this.options = { |
| code: options.code || '123', |
| onSuccess: options.onSuccess || (() => alert('Correct code!')), |
| onError: options.onError || (() => alert('Wrong code!')) |
| }; |
| |
| this.currentCode = ''; |
| this.init(); |
| } |
|
|
| init() { |
| |
| this.display = document.createElement('div'); |
| this.display.className = 'numpad-display'; |
| this.container.appendChild(this.display); |
| |
| |
| this.numpadGrid = document.createElement('div'); |
| this.numpadGrid.className = 'numpad-grid'; |
| this.container.appendChild(this.numpadGrid); |
| |
| |
| for (let i = 1; i <= 9; i++) { |
| this.createButton(i); |
| } |
| this.createButton(0); |
| |
| this.updateDisplay(); |
| } |
|
|
| createButton(number) { |
| const button = document.createElement('button'); |
| button.className = 'numpad-key'; |
| button.textContent = number; |
| button.addEventListener('click', () => this.handleInput(number)); |
| this.numpadGrid.appendChild(button); |
| } |
|
|
| handleInput(number) { |
| if (this.currentCode.length < 3) { |
| this.currentCode += number; |
| this.updateDisplay(); |
| |
| if (this.currentCode.length === 3) { |
| this.checkCode(); |
| } |
| } |
| } |
|
|
| updateDisplay() { |
| this.display.textContent = this.currentCode.padEnd(3, 'X'); |
| } |
|
|
| checkCode() { |
| if (this.currentCode === this.options.code) { |
| this.options.onSuccess(); |
| } else { |
| this.options.onError(); |
| } |
| this.currentCode = ''; |
| this.updateDisplay(); |
| } |
|
|
| reset() { |
| this.currentCode = ''; |
| this.updateDisplay(); |
| } |
| } |
|
|
| |
| document.addEventListener('DOMContentLoaded', () => { |
| |
| if (!document.getElementById('numpad-overlay')) { |
| const overlay = document.createElement('div'); |
| overlay.id = 'numpad-overlay'; |
| overlay.className = 'numpad-overlay hidden'; |
| |
| const container = document.createElement('div'); |
| container.id = 'numpad-container'; |
| overlay.appendChild(container); |
| |
| document.body.appendChild(overlay); |
| } |
| |
| if (!document.getElementById('toggle-numpad')) { |
| const toggleButton = document.createElement('button'); |
| toggleButton.id = 'toggle-numpad'; |
| toggleButton.className = 'toggle-numpad'; |
| toggleButton.textContent = 'Enter Code'; |
| document.body.appendChild(toggleButton); |
| } |
|
|
| |
| const numpad = new Numpad('numpad-container', { |
| code: '123', |
| onSuccess: () => { |
| console.log('Success! Correct code entered'); |
| |
| }, |
| onError: () => { |
| console.log('Error! Wrong code'); |
| } |
| }); |
|
|
| |
| const toggleButton = document.getElementById('toggle-numpad'); |
| const numpadOverlay = document.getElementById('numpad-overlay'); |
|
|
| toggleButton.addEventListener('click', () => { |
| numpadOverlay.classList.toggle('hidden'); |
| }); |
|
|
| |
| document.addEventListener('click', (event) => { |
| if (!numpadOverlay.contains(event.target) && |
| !toggleButton.contains(event.target) && |
| !numpadOverlay.classList.contains('hidden')) { |
| numpadOverlay.classList.add('hidden'); |
| } |
| }); |
| }); |