MediaWiki:Card/code.js

/* Card Grid stuff */ (function(mw, $) { // Return list of elements whose data-value matches the active filters.  function filterCards(list, key, filter) {    // If no active filters, all elements valid.    if (!filter.length) {      return list;    }    return list.filter(function(i, entry) { return filter.find(function(filterEntry) {       return String(filterEntry.dataset.value).toLowerCase ===            String(entry.dataset[key]).toLowerCase;      }); }); }  function updateCardGridFilters {    var filterParams = [];    var cardList = $('.card-entry');    cardList.each(function { this.style.display = 'none'; });   var filteredList = cardList;    var filterKeys = [        'rarity', 'attribute', 'cardfeature', 'character', 'availability', 'appealskill', 'condition'];    filterKeys.forEach(function(key) { var activeFilters = $('.filter-group-' + key + ' > .active').toArray; var activeValues = activeFilters.map(function(filter) {           return filter.dataset.value;        }).join(','); if (activeValues.length) { filterParams.push(key + ':' + activeValues); }       filteredList = filterCards(filteredList, key, activeFilters); });   filteredList.each(function { this.style.display = ''; });   // Update the URL so the filtered state can be directly linked to.    var hashParams = new URLSearchParams;    hashParams.set('CardGridFilters', filterParams.join(';'));    history.replaceState(null, null, '#' + hashParams.toString)  } $(document).ready(function { $('.character-filters .button').on('click', function(event) {       $(event.delegateTarget).toggleClass('active');        updateCardGridFilters;    }); // Replace periods in URL hash with %'s because wiki links URL // encode characters with periods instead. var urlParams = new URLSearchParams(decodeURIComponent( window.location.hash.slice(1).replace(/\./g, '%'))); // If the URL hash-based param exists, set initial filters. // The expected format is filter1:valueA,valueB;filter2...   // e.g name:Gaku Yaotome;type:Rabbit Chat,Puchinana;... if (urlParams.has('CardGridFilters')) { urlParams.get('CardGridFilters').split(';') .forEach(function(filterString) {               var keyValue = filterString.split(':');                keyValue[1].split(',').forEach(function(value) { $('.filter-group-' + keyValue[0] +                       "> [data-value='" + value + "']").toggleClass('active'); });       });        updateCardGridFilters; } });

// BackToTopButton config - https://dev.fandom.com/wiki/BackToTopButton window.BackToTopModern = true; window.BackToTopArrow = true;

})(mediaWiki, jQuery);