Skip to main content

max / makenotwork

2.4 KB · 67 lines History Blame Raw
1 (function() {
2 var input = document.getElementById('wiz-category');
3 var dropdown = document.getElementById('category-dropdown');
4 if (!input || !dropdown) return;
5 var debounce;
6
7 function showDropdown(items, query) {
8 dropdown.innerHTML = '';
9 items.forEach(function(c) {
10 var div = document.createElement('div');
11 div.className = 'suggestion-item';
12 div.textContent = c.name;
13 div.addEventListener('mousedown', function(e) {
14 e.preventDefault();
15 input.value = c.name;
16 dropdown.classList.remove('open');
17 });
18 dropdown.appendChild(div);
19 });
20 var q = query.trim();
21 if (q.length > 0 && !items.some(function(c) { return c.name.toLowerCase() === q.toLowerCase(); })) {
22 var create = document.createElement('div');
23 create.className = 'suggestion-item suggestion-create';
24 create.textContent = 'Create: ' + q;
25 create.addEventListener('mousedown', function(e) {
26 e.preventDefault();
27 input.value = q;
28 dropdown.classList.remove('open');
29 });
30 dropdown.appendChild(create);
31 }
32 if (dropdown.children.length > 0) {
33 dropdown.classList.add('open');
34 } else {
35 dropdown.classList.remove('open');
36 }
37 }
38
39 input.addEventListener('input', function() {
40 clearTimeout(debounce);
41 var q = input.value.trim();
42 if (q.length < 1) { dropdown.classList.remove('open'); return; }
43 debounce = setTimeout(function() {
44 fetch('/api/categories/search?q=' + encodeURIComponent(q))
45 .then(function(r) { return r.json(); })
46 .then(function(cats) { showDropdown(cats, q); })
47 .catch(function() {});
48 }, 200);
49 });
50
51 input.addEventListener('focus', function() {
52 if (dropdown.children.length > 0) dropdown.classList.add('open');
53 });
54
55 input.addEventListener('blur', function() {
56 setTimeout(function() { dropdown.classList.remove('open'); }, 150);
57 });
58
59 // AI tier toggle
60 document.querySelectorAll('input[name="ai_tier"]').forEach(function(r) {
61 r.addEventListener('change', function() {
62 var group = document.getElementById('ai-disclosure-group');
63 group.classList.toggle('hidden', this.value !== 'assisted');
64 });
65 });
66 })();
67