forked from jgthms/html-reference
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfinder.html
More file actions
182 lines (159 loc) · 5.35 KB
/
Copy pathfinder.html
File metadata and controls
182 lines (159 loc) · 5.35 KB
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
179
180
181
182
<article class="finder">
<div class="menu-search">
<input id="finder-input" type="search" autocomplete="off" placeholder="Search 113 HTML elements">
<i class="icon is-search"></i>
</div>
<div id="finder-checkboxes" class="finder-checkboxes">
<label class="light light--experimental is-active"><input type="checkbox" name="experimental" checked>experimental</label>
<label class="light light--meta is-active"><input type="checkbox" name="meta" checked>meta</label>
<label class="light light--selfclosing is-active"><input type="checkbox" name="selfclosing" checked>self-closing</label>
<label class="light light--inline is-active"><input type="checkbox" name="inline" checked>inline</label>
<label class="light light--block is-active"><input type="checkbox" name="block" checked>block</label>
</div>
<div id="finder-list" class="finder-list">
{% include lists/finder-list.html %}
</div>
</article>
<script type="text/javascript">
document.addEventListener('DOMContentLoaded', function() {
var $searchInput = document.getElementById('finder-input');
var $finderList = document.getElementById('finder-list');
var $finderItems = document.querySelectorAll('.finder-item');
var $finderCheckboxes = document.querySelectorAll('#finder-checkboxes input');
var lights = {
experimental: true,
meta: true,
selfClosing: true,
inline: true,
block: true,
}
var isFocused = false;
var isSearching = false;
var isModaling = false;
var currentMatch = -1;
var matches = initializeMatches($finderItems);
if ($searchInput) {
$searchInput.addEventListener('focus', function(event) {
isFocused = true;
isSearching = true;
});
$searchInput.addEventListener('blur', function(event) {
isFocused = false;
if (this.value === '') {
globalReset();
}
});
$searchInput.addEventListener('keyup', function(event) {
var query = this.value.toLowerCase();
isFocused = (this === document.activeElement);
if (isFocused) {
isSearching = true;
if (query.length > 0) {
matches = [];
currentMatch = -1;
$finderList.classList.add('is-searching');
Array.prototype.forEach.call($finderItems, function($el, index) {
var elementName = $el.dataset.elementName;
var isMatch = highlightQuery($el, elementName, query);
if (isMatch && !$el.classList.contains('is-hidden')) {
matches.push({
DOMIndex: index,
elementName: elementName,
});
}
});
} else {
$finderList.classList.remove('is-searching');
Array.prototype.forEach.call($finderItems, function($el) {
var $elName = $el.querySelector('.item-name');
$elName.innerHTML = $el.dataset.elementName;
});
}
}
});
Array.prototype.forEach.call($finderCheckboxes, function($el) {
$el.addEventListener('change', function(event) {
lights[this.name] = this.checked;
changeLights();
});
});
window.addEventListener('click', function(event) {
if (!isFocused) {
isSearching = false;
cleanMenu($finderItems, false, true);
}
});
window.addEventListener('keydown', function(event) {
var key = event.code || event.key || false;
switch (key) {
case 'Enter':
if (isSearching && currentMatch > -1) {
event.preventDefault();
var elementName = matches[currentMatch].elementName;
var $target = document.getElementById(elementName);
if ($target) {
$target.scrollIntoView();
} else {
window.location = window.location.origin + '/element/' + elementName;
}
}
break;
case 'Escape':
if (isModaling) {
closeModal();
} else {
isSearching = false;
globalReset();
}
break;
case 'ArrowUp':
case 'ArrowDown':
if (isSearching) {
event.preventDefault();
if (isFocused) {
$searchInput.blur();
}
var increment = (key === 'ArrowDown');
currentMatch = navigateMenu($finderItems, matches, currentMatch, increment);
if (currentMatch === -1) {
$searchInput.focus();
}
break;
}
}
});
function checkLights() {
Array.prototype.forEach.call($finderCheckboxes, function($el) {
lights[this.name] = this.checked;
});
}
function changeLights() {
Array.prototype.forEach.call($finderItems, function($el, index) {
var hide = false;
Object.keys(lights).forEach(function(key) {
// Check if we hide "experimental"
if (!lights[key]) {
hide = (hide) || ($el.dataset[key] == 'true');
}
});
if (hide) {
$el.classList.add('is-hidden');
} else {
$el.classList.remove('is-hidden');
}
});
}
function globalReset() {
isFocused = false;
$searchInput.value = '';
$searchInput.blur();
currentMatch = -1;
matches = initializeMatches($finderItems);
$finderList.classList.remove('is-searching');
cleanMenu($finderItems, true, true);
}
checkLights();
changeLights();
}
});
</script>