|
| 1 | +class DifficultyDropdown { |
| 2 | + constructor(container, difficulties, onChange) { |
| 3 | + this.container = container; |
| 4 | + this.difficulties = difficulties; |
| 5 | + this.onChange = onChange; |
| 6 | + this.selectedDifficulty = 'all'; |
| 7 | + this.filteredDifficulties = ['all', ...difficulties]; |
| 8 | + this.createDropdown(); |
| 9 | + } |
| 10 | + |
| 11 | + createDropdown() { |
| 12 | + this.container.innerHTML = ''; |
| 13 | + this.dropdown = document.createElement('div'); |
| 14 | + this.dropdown.className = 'difficulty-dropdown'; |
| 15 | + |
| 16 | + this.selected = document.createElement('div'); |
| 17 | + this.selected.className = 'difficulty-dropdown-selected'; |
| 18 | + this.selected.tabIndex = 0; |
| 19 | + |
| 20 | + const selectedText = document.createElement('span'); |
| 21 | + selectedText.className = 'difficulty-dropdown-selected-text'; |
| 22 | + selectedText.textContent = 'All Difficulties'; |
| 23 | + this.selected.appendChild(selectedText); |
| 24 | + const chevron = document.createElement('i'); |
| 25 | + chevron.className = 'ri-arrow-down-s-line difficulty-dropdown-chevron' |
| 26 | + this.selected.appendChild(chevron); |
| 27 | + this.selected.addEventListener('click', () => this.toggleList()); |
| 28 | + this.selected.addEventListener('keydown', (e) => { |
| 29 | + if (e.key == 'Enter' || e.key == ' ') this.toggleList(); |
| 30 | + }); |
| 31 | + this.dropdown.appendChild(this.selected); |
| 32 | + |
| 33 | + this.list = document.createElement('div'); |
| 34 | + this.list.className = 'difficulty-dropdown-list' |
| 35 | + this.list.style.display = 'none' |
| 36 | + |
| 37 | + this.searchInput = document.createElement('input'); |
| 38 | + this.searchInput.type = 'text'; |
| 39 | + this.searchInput.className = 'difficulty-dropdown-search'; |
| 40 | + this.searchInput.placeholder = 'Search Difficulties...'; |
| 41 | + this.searchInput.addEventListener('input', () => this.filterDifficulties()); |
| 42 | + this.list.appendChild(this.searchInput); |
| 43 | + |
| 44 | + this.optionsContainer = document.createElement('div'); |
| 45 | + this.optionsContainer.className = 'difficulty-dropdown-options'; |
| 46 | + this.list.appendChild(this.optionsContainer); |
| 47 | + |
| 48 | + this.dropdown.appendChild(this.list); |
| 49 | + this.container.appendChild(this.dropdown); |
| 50 | + |
| 51 | + this.renderOptions(); |
| 52 | + document.addEventListener('click', (e) => { |
| 53 | + if (!this.dropdown.contains(e.target)) this.closeList(); |
| 54 | + }); |
| 55 | + } |
| 56 | + |
| 57 | + renderOptions() { |
| 58 | + this.optionsContainer.innerHTML = ''; |
| 59 | + this.filteredDifficulties.forEach(difficulty => { |
| 60 | + const opt = document.createElement('div'); |
| 61 | + opt.className = 'difficulty-dropdown-option'; |
| 62 | + opt.textContent = difficulty === 'all' ? 'All Difficulties' : difficulty; |
| 63 | + if (difficulty === this.selectedDifficulty) opt.classList.add('selected'); |
| 64 | + opt.addEventListener('click', () => this.selectDifficulty(difficulty)); |
| 65 | + this.optionsContainer.appendChild(opt); |
| 66 | + }); |
| 67 | + } |
| 68 | + |
| 69 | + filterDifficulties() { |
| 70 | + const val = this.searchInput.value.toLowerCase(); |
| 71 | + this.filteredDifficulties = ['all', ...this.difficulties |
| 72 | + .filter(difficulty => difficulty |
| 73 | + .toLowerCase() |
| 74 | + .includes(val) |
| 75 | + )]; |
| 76 | + this.renderOptions(); |
| 77 | + } |
| 78 | + |
| 79 | + selectDifficulty(difficulty) { |
| 80 | + this.selectedDifficulty = difficulty; |
| 81 | + |
| 82 | + const textSpan = this.selected.querySelector('.difficulty-dropdown-selected-text'); |
| 83 | + if (textSpan) textSpan.textContent = difficulty === 'all' ? 'All Difficulties' : difficulty; |
| 84 | + this.closeList(); |
| 85 | + if (this.onChange) this.onChange(difficulty); |
| 86 | + } |
| 87 | + |
| 88 | + toggleList() { |
| 89 | + this.list.style.display = this.list.style.display === 'none' ? 'block' : 'none'; |
| 90 | + if (this.list.style.display === 'block') { |
| 91 | + this.searchInput.value = ''; |
| 92 | + this.filterDifficulties(); |
| 93 | + this.searchInput.focus(); |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + closeList() { |
| 98 | + this.list.style.display = 'none'; |
| 99 | + } |
| 100 | + |
| 101 | + setDifficulty(difficulties) { |
| 102 | + this.difficulties = difficulties; |
| 103 | + this.filteredDifficulties = ['all', ...difficulties]; |
| 104 | + this.renderOptions(); |
| 105 | + } |
| 106 | +} |
| 107 | +window.DifficultyDropdown = DifficultyDropdown; |
0 commit comments