-
Notifications
You must be signed in to change notification settings - Fork 338
Expand file tree
/
Copy pathfinder.html
More file actions
133 lines (115 loc) · 3.68 KB
/
Copy pathfinder.html
File metadata and controls
133 lines (115 loc) · 3.68 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
<article class="finder">
<div class="menu-search">
<input id="finder-input" type="search" autocomplete="off" placeholder="Search 129 CSS properties" autofocus>
<i class="icon is-search"></i>
</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 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 propertyName = $el.dataset.propertyName;
var isMatch = highlightQuery($el, propertyName, query);
if (isMatch && !$el.classList.contains('is-hidden')) {
matches.push({
DOMIndex: index,
propertyName: propertyName,
});
}
});
} else {
$finderList.classList.remove('is-searching');
Array.prototype.forEach.call($finderItems, function($el) {
var $elName = $el.querySelector('.item-name');
$elName.innerHTML = $el.dataset.propertyName;
});
}
}
});
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 propertyName = matches[currentMatch].propertyName;
var $target = document.getElementById(propertyName);
if ($target) {
$target.scrollIntoView();
} else {
window.location = window.location.origin + '/property/' + propertyName;
}
}
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 globalReset() {
isFocused = false;
$searchInput.value = '';
$searchInput.blur();
currentMatch = -1;
matches = initializeMatches($finderItems);
$finderList.classList.remove('is-searching');
cleanMenu($finderItems, true, true);
}
}
});
</script>