Skip to content

Instantly share code, notes, and snippets.

@kvnol
Created June 14, 2017 12:57
Show Gist options
  • Save kvnol/38db6660b9730f649bd79e5ef9513856 to your computer and use it in GitHub Desktop.
Save kvnol/38db6660b9730f649bd79e5ef9513856 to your computer and use it in GitHub Desktop.
Filter bar
(function(win, doc) {
'use strict';
var state = doc.querySelector('[data-id="estado"]');
var city = doc.querySelector('[data-id="cidades"]');
var district = doc.querySelector('[data-id="regioes"]');
if (win.location.pathname === '/bares.html') {
city.addEventListener('change', barFilter, 'false');
city.addEventListener('change', disableSiblings, 'false');
city.addEventListener('change', removeCity, 'false');
}
function barFilter() {
var barItem = doc.querySelectorAll('.bares-item');
var itemValue = this.options[this.selectedIndex].value;
var itemClass = ('.' + itemValue).toString();
Array.prototype.forEach.call(barItem, function(item, index) {
var itemSelected = doc.querySelectorAll(itemClass);
if (itemValue === 'estados' || itemValue === 'cidades' || itemValue === 'regioes')
return item.classList.remove('bares-item--hidden');
item.classList.add('bares-item--hidden');
Array.prototype.forEach.call(itemSelected, function(item) {
item.classList.remove('bares-item--hidden');
});
});
}
function disableSiblings() {
var itemValue = this.options[this.selectedIndex].value;
if (itemValue !== 'estados' || itemValue !== 'cidades' || itemValue !== 'regioes')
return this.nextSibling.removeAttribute('disabled');
}
function removeCity() {
var itemValue = this.options[this.selectedIndex].value;
Array.prototype.forEach.call(this.nextSibling.options, function(item, index) {
if (index !== 0) {
item.style.display = 'none';
}
if (item.value === 'estados' || item.value === 'cidades' || item.attributes[0].value === itemValue) {
item.style.display = 'block';
}
});
}
})(window, document);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment