forked from simple-icons/simple-icons
-
Notifications
You must be signed in to change notification settings - Fork 0
/
site_script.js
206 lines (178 loc) · 6.49 KB
/
site_script.js
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
(function(document) {
var $grid = document.querySelector('.grid'),
$icons = $grid.querySelectorAll('.grid-item:not(.grid-item--ad)'),
$search = document.querySelector('.search'),
$searchClose = $search.querySelector('.search__close'),
$searchInput = $search.querySelector('input'),
$orderByColor = document.getElementById('sort-color'),
$orderAlphabetically = document.getElementById('sort-alphabetically'),
$orderByRelevance = document.getElementById('sort-relevance');
var queryParameter = 'q',
orderingPreferenceIdentifier = 'ordering-preference',
previousQuery = null,
previousOrdering = $orderByColor;
// Remove the "disabled" attribute from the search input
$searchInput.setAttribute('title', 'Search Simple Icons');
$searchInput.removeAttribute('disabled');
// include a modified debounce underscorejs helper function.
// see
// - http://underscorejs.org/docs/underscore.html#section-83
// - http://underscorejs.org/#debounce
function debounce(func, wait, immediate) {
var timeout, args, context, timestamp, result;
var later = function() {
var last = +new Date - timestamp;
if (last < wait && last >= 0) {
timeout = setTimeout(later, wait - last);
} else {
timeout = null;
if (!immediate) {
result = func.apply(context, args);
if (!timeout) context = args = null;
}
}
};
return function() {
context = this;
args = arguments;
timestamp = +new Date;
var callNow = immediate && !timeout;
if (!timeout) timeout = setTimeout(later, wait);
if (callNow) {
result = func.apply(context, args);
context = args = null;
}
return result;
};
}
// Get any parameter from the URL's search section (location.search).
// see
// - https://davidwalsh.name/query-string-javascript
// - https://github.com/WebReflection/url-search-params
function getUrlParameter(parameter) {
name = parameter.replace(/[\[]/, '\\[').replace(/[\]]/, '\\]');
var regex = new RegExp('[\\?&]' + name + '=([^&#]*)');
var results = regex.exec(location.search);
return results === null ? '' : decodeURIComponent(results[1].replace(/\+/g, ' '));
}
function search(value) {
var query = normalizeSearchTerm(value);
var matchedIcons = icons.map(function(icon, iconIndex) {
var letters = query.split(''),
indexes = [],
index = 0;
if (icon === query) {
return {element: $icons[iconIndex], score: 1};
}
for (var i = 0; i < letters.length; i++) {
var letter = letters[i];
index = icon.indexOf(letter, index);
if (index === -1) {
$icons[iconIndex].classList.add('hidden');
return null;
}
indexes.push(index);
index++;
}
return {
element: $icons[iconIndex],
score: indexes.reduce(function(a, b) {
return a + b;
}, 2)
};
}).filter(function(item) {
return item !== null;
});
matchedIcons.sort(function(a, b) {
return a.score - b.score;
}).forEach(function(item, index) {
var element = item.element;
element.setAttribute('data-relevance', index);
element.classList.remove('hidden');
});
$grid.classList.toggle('search__empty', matchedIcons.length == 0);
if (query === '') {
if ($orderByRelevance.classList.contains('active')) {
selectOrdering(previousOrdering);
}
$orderByRelevance.setAttribute('display', 'none');
previousQuery = null;
} else {
if (previousQuery === null) {
selectOrdering($orderByRelevance);
}
previousQuery = query;
}
}
function orderIcons() {
if ($orderByColor.classList.contains('active')) {
$icons.forEach(icon => { icon.style.order = null; });
} else if ($orderAlphabetically.classList.contains('active')) {
$icons.forEach(icon => { icon.style.order = icon.getAttribute('order'); });
} else if ($orderByRelevance.classList.contains('active')) {
$icons.forEach(icon => { icon.style.order = icon.getAttribute('data-relevance'); });
}
}
function selectOrdering(selected) {
selected.classList.add('active');
var options = [$orderByColor, $orderAlphabetically, $orderByRelevance];
for (var option of options.filter(option => option !== selected)) {
option.classList.remove('active');
}
if (selected !== $orderByRelevance) {
previousOrdering = selected;
} else {
$orderByRelevance.removeAttribute('display');
}
orderIcons();
// Store ordering preference
var preferenceOptions = [$orderByColor, $orderAlphabetically];
if (localStorage && preferenceOptions.includes(selected)) {
localStorage.setItem(orderingPreferenceIdentifier, selected.id);
}
}
document.addEventListener('DOMContentLoaded', function() {
// Restore ordering preference of the user. This should be performed before
// applying the search query as it would overwrite "order by relevance"
if (localStorage) {
var storedOrderingId = localStorage.getItem(orderingPreferenceIdentifier);
var ordering = document.getElementById(storedOrderingId);
if (ordering) selectOrdering(ordering);
}
// Load search query if present
var query = getUrlParameter(queryParameter);
if (query) {
$search.classList.add('search--active');
$searchInput.value = query;
search(query);
}
});
$search.addEventListener('input', debounce(function(e) {
e.preventDefault();
var value = e.target.value;
if (value) {
$search.classList.add('search--active');
window.history.replaceState(null, '', '?' + queryParameter + '=' + value);
} else {
$search.classList.remove('search--active');
window.history.replaceState(null, '', '/');
}
search(value);
}, 50), false);
$searchClose.addEventListener('click', function(e) {
e.stopPropagation();
$searchInput.value = '';
$search.classList.remove('search--active');
window.history.replaceState(null, '', '/');
search('');
}, false);
$orderByColor.addEventListener('click', function() {
selectOrdering($orderByColor);
});
$orderAlphabetically.addEventListener('click', function() {
selectOrdering($orderAlphabetically);
});
$orderByRelevance.addEventListener('click', function() {
selectOrdering($orderByRelevance);
});
})( document );