2018-01-03 20:33:25 +03:00
|
|
|
|
(function(document) {
|
2019-02-04 00:31:22 +03:00
|
|
|
|
var $body = document.body,
|
|
|
|
|
$grid = document.querySelector('.grid'),
|
2018-11-05 03:58:49 +03:00
|
|
|
|
$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');
|
2018-09-01 12:20:04 +03:00
|
|
|
|
|
2018-01-03 20:33:25 +03:00
|
|
|
|
var queryParameter = 'q',
|
2018-11-05 03:58:49 +03:00
|
|
|
|
orderingPreferenceIdentifier = 'ordering-preference',
|
2020-12-11 23:56:09 +03:00
|
|
|
|
previousQuery = '',
|
2018-11-05 03:58:49 +03:00
|
|
|
|
previousOrdering = $orderByColor;
|
2018-01-03 20:33:25 +03:00
|
|
|
|
|
|
|
|
|
// Remove the "disabled" attribute from the search input
|
|
|
|
|
$searchInput.setAttribute('title', 'Search Simple Icons');
|
|
|
|
|
$searchInput.removeAttribute('disabled');
|
2020-09-18 11:40:34 +03:00
|
|
|
|
$searchInput.focus();
|
2018-01-03 20:33:25 +03:00
|
|
|
|
|
|
|
|
|
// 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;
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-31 19:52:07 +03:00
|
|
|
|
// Get a parameter from the URL's search section (location.search). Based on:
|
2018-01-03 20:33:25 +03:00
|
|
|
|
// - https://davidwalsh.name/query-string-javascript
|
|
|
|
|
// - https://github.com/WebReflection/url-search-params
|
2020-07-31 19:52:07 +03:00
|
|
|
|
// - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions#Escaping
|
2018-01-03 20:33:25 +03:00
|
|
|
|
function getUrlParameter(parameter) {
|
2020-07-31 19:52:07 +03:00
|
|
|
|
var name = parameter.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
2018-01-03 20:33:25 +03:00
|
|
|
|
var regex = new RegExp('[\\?&]' + name + '=([^&#]*)');
|
|
|
|
|
var results = regex.exec(location.search);
|
|
|
|
|
return results === null ? '' : decodeURIComponent(results[1].replace(/\+/g, ' '));
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-23 18:21:10 +03:00
|
|
|
|
function normalizeSearchTerm(value) {
|
2020-12-11 23:56:09 +03:00
|
|
|
|
return value.toLowerCase()
|
2021-02-03 02:06:49 +03:00
|
|
|
|
.replace(/đ/g, "d")
|
|
|
|
|
.replace(/ħ/g, "h")
|
|
|
|
|
.replace(/ı/g, "i")
|
|
|
|
|
.replace(/ĸ/g, "k")
|
|
|
|
|
.replace(/ŀ/g, "l")
|
|
|
|
|
.replace(/ł/g, "l")
|
|
|
|
|
.replace(/ß/g, "ss")
|
|
|
|
|
.replace(/ŧ/g, "t")
|
|
|
|
|
.normalize("NFD")
|
|
|
|
|
.replace(/[\u0300-\u036f]/g, "");
|
2020-06-23 18:21:10 +03:00
|
|
|
|
}
|
|
|
|
|
|
2018-01-03 20:33:25 +03:00
|
|
|
|
function search(value) {
|
2019-02-04 00:31:22 +03:00
|
|
|
|
var query = normalizeSearchTerm(value)
|
|
|
|
|
queryLetters = query.split('');
|
2018-01-03 20:33:25 +03:00
|
|
|
|
|
2019-02-04 00:31:22 +03:00
|
|
|
|
var matchedIcons = icons.filter(function(iconName, iconIndex) {
|
2020-07-28 13:33:40 +03:00
|
|
|
|
var element = $icons[iconIndex];
|
|
|
|
|
var score = iconName.length - query.length;
|
|
|
|
|
var index = 0;
|
2018-01-03 20:33:25 +03:00
|
|
|
|
|
2019-02-04 00:31:22 +03:00
|
|
|
|
for (var i = 0; i < queryLetters.length; i++) {
|
|
|
|
|
var letter = queryLetters[i];
|
|
|
|
|
index = iconName.indexOf(letter, index);
|
2018-01-03 20:33:25 +03:00
|
|
|
|
|
|
|
|
|
if (index === -1) {
|
2019-02-04 00:31:22 +03:00
|
|
|
|
element.classList.add('hidden');
|
|
|
|
|
return false;
|
2018-01-03 20:33:25 +03:00
|
|
|
|
}
|
|
|
|
|
|
2019-02-04 00:31:22 +03:00
|
|
|
|
score += index;
|
2018-01-03 20:33:25 +03:00
|
|
|
|
index++;
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-04 00:31:22 +03:00
|
|
|
|
element.style.setProperty("--order-relevance", score);
|
2018-09-01 12:20:04 +03:00
|
|
|
|
element.classList.remove('hidden');
|
2019-02-04 00:31:22 +03:00
|
|
|
|
return true;
|
2018-01-03 20:33:25 +03:00
|
|
|
|
});
|
|
|
|
|
|
2018-10-29 20:21:34 +03:00
|
|
|
|
$grid.classList.toggle('search__empty', matchedIcons.length == 0);
|
2019-02-04 00:31:22 +03:00
|
|
|
|
$body.classList.toggle('search__active', matchedIcons.length < icons.length);
|
|
|
|
|
|
2018-09-01 12:20:04 +03:00
|
|
|
|
if (query === '') {
|
2020-12-11 23:56:09 +03:00
|
|
|
|
if ($body.classList.contains('order-by-relevance')) {
|
2018-11-05 03:58:49 +03:00
|
|
|
|
selectOrdering(previousOrdering);
|
2018-09-01 12:20:04 +03:00
|
|
|
|
}
|
|
|
|
|
} else {
|
2019-02-04 00:31:22 +03:00
|
|
|
|
if (previousQuery === '') {
|
2018-11-05 03:58:49 +03:00
|
|
|
|
selectOrdering($orderByRelevance);
|
2018-09-01 12:20:04 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
2019-02-04 00:31:22 +03:00
|
|
|
|
|
|
|
|
|
previousQuery = query;
|
2018-09-01 12:20:04 +03:00
|
|
|
|
}
|
2018-11-05 03:58:49 +03:00
|
|
|
|
function selectOrdering(selected) {
|
2019-02-04 00:31:22 +03:00
|
|
|
|
// Set the ordering type as a class on body
|
|
|
|
|
$body.classList.remove('order-alphabetically', 'order-by-color', 'order-by-relevance');
|
|
|
|
|
if (selected === $orderByColor) {
|
|
|
|
|
$body.classList.add('order-by-color');
|
|
|
|
|
} else if (selected === $orderAlphabetically) {
|
|
|
|
|
$body.classList.add('order-alphabetically');
|
|
|
|
|
} else if (selected === $orderByRelevance) {
|
|
|
|
|
$body.classList.add('order-by-relevance');
|
2018-09-01 12:20:04 +03:00
|
|
|
|
}
|
|
|
|
|
|
2018-11-05 03:58:49 +03:00
|
|
|
|
// Store ordering preference
|
|
|
|
|
var preferenceOptions = [$orderByColor, $orderAlphabetically];
|
|
|
|
|
if (localStorage && preferenceOptions.includes(selected)) {
|
|
|
|
|
localStorage.setItem(orderingPreferenceIdentifier, selected.id);
|
|
|
|
|
}
|
2019-02-04 00:31:22 +03:00
|
|
|
|
if (selected !== $orderByRelevance) {
|
|
|
|
|
previousOrdering = selected;
|
|
|
|
|
}
|
2018-01-03 20:33:25 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
document.addEventListener('DOMContentLoaded', function() {
|
2018-11-05 03:58:49 +03:00
|
|
|
|
// 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);
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-03 20:33:25 +03:00
|
|
|
|
// 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);
|
2019-02-04 00:32:44 +03:00
|
|
|
|
}, 200), false);
|
2018-01-03 20:33:25 +03:00
|
|
|
|
$searchClose.addEventListener('click', function(e) {
|
|
|
|
|
e.stopPropagation();
|
|
|
|
|
|
|
|
|
|
$searchInput.value = '';
|
|
|
|
|
$search.classList.remove('search--active');
|
|
|
|
|
window.history.replaceState(null, '', '/');
|
|
|
|
|
search('');
|
|
|
|
|
}, false);
|
|
|
|
|
|
2018-11-05 03:58:49 +03:00
|
|
|
|
$orderByColor.addEventListener('click', function() {
|
|
|
|
|
selectOrdering($orderByColor);
|
2018-01-03 20:33:25 +03:00
|
|
|
|
});
|
2018-11-05 03:58:49 +03:00
|
|
|
|
$orderAlphabetically.addEventListener('click', function() {
|
|
|
|
|
selectOrdering($orderAlphabetically);
|
2018-09-01 12:20:04 +03:00
|
|
|
|
});
|
2018-11-05 03:58:49 +03:00
|
|
|
|
$orderByRelevance.addEventListener('click', function() {
|
|
|
|
|
selectOrdering($orderByRelevance);
|
2018-01-03 20:33:25 +03:00
|
|
|
|
});
|
2021-02-04 19:29:09 +03:00
|
|
|
|
|
|
|
|
|
/* Redesign */
|
|
|
|
|
|
|
|
|
|
var $banner = document.querySelector('.redesign-banner'),
|
|
|
|
|
$redirectAutomatically = document.getElementById('redirect-to-redesign'),
|
|
|
|
|
$hideOnce = document.getElementById('hide-feedback-request-once'),
|
|
|
|
|
$hideAlways = document.getElementById('hide-feedback-request');
|
|
|
|
|
|
|
|
|
|
var redesignUrl = 'https://simple-icons.github.io/simple-icons-website/',
|
|
|
|
|
hideBannerAlwaysIdentifier = 'hide-banner',
|
|
|
|
|
redirectAutomaticallyIdentifier = 'redirect-to-redesign';
|
|
|
|
|
|
|
|
|
|
$redirectAutomatically.addEventListener('click', function() {
|
|
|
|
|
var redirect = true;
|
|
|
|
|
if (localStorage) {
|
|
|
|
|
var currentVal = localStorage.getItem(redirectAutomaticallyIdentifier);
|
|
|
|
|
if (currentVal === 'true') {
|
|
|
|
|
redirect = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
localStorage.setItem(redirectAutomaticallyIdentifier, redirect);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (redirect) {
|
|
|
|
|
window.location.replace(redesignUrl);
|
|
|
|
|
} else {
|
|
|
|
|
$redirectAutomatically.innerHTML = "Redirect automatically";
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
$hideOnce.addEventListener('click', function () {
|
|
|
|
|
$banner.classList.add('hidden');
|
|
|
|
|
});
|
|
|
|
|
$hideAlways.addEventListener('click', function () {
|
|
|
|
|
if (localStorage) {
|
|
|
|
|
localStorage.setItem(hideBannerAlwaysIdentifier, true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$banner.classList.add('hidden');
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (localStorage) {
|
|
|
|
|
var redirect = localStorage.getItem(redirectAutomaticallyIdentifier);
|
|
|
|
|
if (redirect === 'true') {
|
|
|
|
|
$redirectAutomatically.innerHTML = "Disable redirect";
|
|
|
|
|
if (document.referrer !== redesignUrl) {
|
|
|
|
|
window.location.replace(redesignUrl);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var hide = localStorage.getItem(hideBannerAlwaysIdentifier);
|
|
|
|
|
if (hide) {
|
|
|
|
|
$banner.classList.add('hidden');
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-01-03 20:33:25 +03:00
|
|
|
|
})( document );
|