Skip to content

Instantly share code, notes, and snippets.

@MrMeison
Created March 13, 2020 18:00
Show Gist options
  • Select an option

  • Save MrMeison/2a75782162b430d3f9de96eb31b3252e to your computer and use it in GitHub Desktop.

Select an option

Save MrMeison/2a75782162b430d3f9de96eb31b3252e to your computer and use it in GitHub Desktop.
'use strict';
(function () {
// начальное состояние фильтров, можно сразу заполнить дефолтными значениями
var filterState = {
'housing-type': 'any',
'housing-rooms': 'any',
'housing-guests': 'any',
features: {}
}
var checkHouseType = function (elementValue, filterStateValue) {
return filterStateValue === 'any' || filterStateValue === elementValue.toString();
};
// функция для применения фильтров
var apply = function (offers) {
console.log('filterState', filterState);
return offers.filter(function (element) {
return checkHouseType(element.offer.type, filterState['housing-type']);
});
};
// функция для изменения фильтра
var change = function(key, value) {
filterState[key] = value;
}
var changeFilter = function(key, value) {
filterState.features[key] = value;
}
window.filter = {
apply: apply,
change: change,
changeFilter: changeFilter
};
})();
// использование
mapFilters.addEventListener('change', function (evt) {
window.map.closePopUp();
window.pins.removePins();
var filterName = evt.target.id;
var filterValue = evt.target.value;
if (evt.target.name === 'features') {
window.filter.changeFilter(filterValue, evt.target.checked);
} else {
window.filter.change(filterName, filterValue);
}
var pins = window.filter.apply(window.map.offers).slice(0, window.constants.PINS_NUMBER);
pinsContainer.appendChild(window.pins.renderPins(pins));
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment