User:Bluemorpho/common.js

(function(mw, $) {	const SplitKey = '-'; // Return list of elements whose data-value matches the active filters.  function filterCards(list, key, filter) {    // If no active filters, all elements valid.    console.log(filter);    if (!filter.length) {      return list;    }    return list.filter(function(i, entry) { return filter.find(function(filterEntry) {       let filterEntryStr = String(filterEntry.dataset.value).toLowerCase;        let entryStr = String(entry.dataset[key]).toLowerCase;        if (entryStr.includes(SplitKey)) {        	console.log("splitkey");        	let dorms = splitString(entryStr);          console.log(dorms);          return dorms.includes(filterEntryStr);        }        else {        console.log("no splitkey");          return filterEntryStr === entryStr;        }      }); }); }  function splitString(str) {  	return str.split(SplitKey);  }  function updateCardGridFilters {    var filterParams = [];    var cardList = $('.filter');    cardList.each(function { this.style.display = 'none'; });   var filteredList = cardList;    var filterKeys = ['rarity', 'dorm', 'theme', 'materials'];    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 { $('.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);