File size: 6,423 Bytes
5f7f592
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
class SubjectSelector extends HTMLElement {
  connectedCallback() {
    this.attachShadow({ mode: 'open' });
    this.shadowRoot.innerHTML = `
      <style>
        :host {
          display: block;
          position: relative;
        }
        
        .selector-btn {
          background: linear-gradient(135deg, #6366f1, #8b5cf6);
          color: white;
          border: none;
          border-radius: 9999px;
          padding: 0.75rem 1.5rem;
          font-weight: 600;
          cursor: pointer;
          display: flex;
          align-items: center;
          gap: 0.5rem;
          transition: all 0.3s ease;
          box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1);
        }
        
        .selector-btn:hover {
          transform: translateY(-2px);
          box-shadow: 0 10px 15px -3px rgba(0, 0, 0, 0.1);
        }
        
        .dropdown {
          position: absolute;
          top: 100%;
          left: 0;
          width: 300px;
          background: white;
          border-radius: 12px;
          box-shadow: 0 10px 15px -3px rgba(0, 0, 0, 0.1);
          padding: 1rem;
          z-index: 50;
          display: none;
          margin-top: 0.5rem;
        }
        
        .dropdown.show {
          display: block;
          animation: fadeIn 0.2s ease-out;
        }
        
        .search-input {
          width: 100%;
          padding: 0.5rem;
          border: 1px solid #e5e7eb;
          border-radius: 6px;
          margin-bottom: 1rem;
        }
        
        .subject-list {
          max-height: 300px;
          overflow-y: auto;
        }
        
        .subject-item {
          padding: 0.5rem;
          border-radius: 6px;
          cursor: pointer;
          transition: background 0.2s;
        }
        
        .subject-item:hover {
          background: #f3f4f6;
        }
        
        @keyframes fadeIn {
          from { opacity: 0; transform: translateY(10px); }
          to { opacity: 1; transform: translateY(0); }
        }
      </style>
      
      <button class="selector-btn" id="selectorBtn">
        <i data-feather="book"></i>
        Choose Subject
      </button>
      
      <div class="dropdown" id="dropdown">
        <input type="text" class="search-input" placeholder="Search subjects..." id="searchInput">
        <div class="subject-list" id="subjectList">
          <!-- Subjects will be loaded here -->
        </div>
      </div>
    `;

    const btn = this.shadowRoot.getElementById('selectorBtn');
    const dropdown = this.shadowRoot.getElementById('dropdown');
    const searchInput = this.shadowRoot.getElementById('searchInput');
    const subjectList = this.shadowRoot.getElementById('subjectList');

    // Toggle dropdown
    btn.addEventListener('click', () => {
      dropdown.classList.toggle('show');
      if (dropdown.classList.contains('show') && subjectList.innerHTML === '') {
        this.loadSubjects();
      }
    });

    // Close dropdown when clicking outside
    document.addEventListener('click', (e) => {
      if (!this.shadowRoot.contains(e.target)) {
        dropdown.classList.remove('show');
      }
    });

    // Search functionality
    searchInput.addEventListener('input', () => {
      const searchTerm = searchInput.value.toLowerCase();
      const items = subjectList.querySelectorAll('.subject-item');
      
      items.forEach(item => {
        const text = item.textContent.toLowerCase();
        if (text.includes(searchTerm)) {
          item.style.display = 'block';
        } else {
          item.style.display = 'none';
        }
      });
    });

    // Initialize Feather icons
    if (window.feather) {
      window.feather.replace({ 'stroke-width': 2 });
    }
  }

  loadSubjects() {
    const subjectList = this.shadowRoot.getElementById('subjectList');
    
    // Common academic subjects
    const subjects = [
      { name: "Mathematics", level: "basic", path: "math.html?level=basic" },
      { name: "Mathematics", level: "intermediate", path: "math.html?level=intermediate" },
      { name: "Mathematics", level: "advanced", path: "math.html?level=advanced" },
      { name: "Physics", level: "basic", path: "science.html?subject=physics&level=basic" },
      { name: "Physics", level: "intermediate", path: "science.html?subject=physics&level=intermediate" },
      { name: "Chemistry", level: "basic", path: "science.html?subject=chemistry&level=basic" },
      { name: "Chemistry", level: "intermediate", path: "science.html?subject=chemistry&level=intermediate" },
      { name: "Biology", level: "basic", path: "science.html?subject=biology&level=basic" },
      { name: "Biology", level: "intermediate", path: "science.html?subject=biology&level=intermediate" },
      { name: "Computer Science", level: "basic", path: "tech.html?subject=cs&level=basic" },
      { name: "Computer Science", level: "intermediate", path: "tech.html?subject=cs&level=intermediate" },
      { name: "History", level: "basic", path: "history.html?level=basic" },
      { name: "History", level: "intermediate", path: "history.html?level=intermediate" },
      { name: "Geography", level: "basic", path: "geography.html?level=basic" },
      { name: "Art", level: "basic", path: "art.html?level=basic" },
      { name: "Music", level: "basic", path: "music.html?level=basic" },
      { name: "Literature", level: "basic", path: "literature.html?level=basic" },
      { name: "Philosophy", level: "basic", path: "philosophy.html?level=basic" },
      { name: "Economics", level: "basic", path: "economics.html?level=basic" },
      { name: "Psychology", level: "basic", path: "psychology.html?level=basic" },
      { name: "Spanish", level: "basic", path: "languages.html?lang=spanish&level=basic" },
      { name: "French", level: "basic", path: "languages.html?lang=french&level=basic" },
      { name: "German", level: "basic", path: "languages.html?lang=german&level=basic" },
      { name: "Chinese", level: "basic", path: "languages.html?lang=chinese&level=basic" }
    ];

    subjectList.innerHTML = subjects.map(subject => `
      <a href="${subject.path}" class="subject-item">
        ${subject.name} (${subject.level})
      </a>
    `).join('');

    // Initialize Feather icons again after loading content
    if (window.feather) {
      window.feather.replace({ 'stroke-width': 2 });
    }
  }
}

customElements.define('subject-selector', SubjectSelector);