[ Disclaimer, Create new user --- Wiki markup help, Install P99 ]
Difference between revisions of "MediaWiki:HuntingGuide.js"
From Project 1999 Wiki
| Line 57: | Line 57: | ||
/** | /** | ||
* Filters out (hides) rows which aren't specifically "good for" the clicked class | * Filters out (hides) rows which aren't specifically "good for" the clicked class | ||
| − | |||
*/ | */ | ||
var filterForClass = function(e) { | var filterForClass = function(e) { | ||
| Line 72: | Line 71: | ||
var lowerCaseNotes = spot.notes.toLowerCase(); | var lowerCaseNotes = spot.notes.toLowerCase(); | ||
if (!lowerCaseNotes.match(new RegExp(`good for .*?${eqClass.toLowerCase()}`))) return; | if (!lowerCaseNotes.match(new RegExp(`good for .*?${eqClass.toLowerCase()}`))) return; | ||
| + | spot.$tr.show(); | ||
| + | })); | ||
| + | hideHeaders(); | ||
| + | } | ||
| + | |||
| + | /** | ||
| + | * Filters out (hides) rows which don't involve the specified monster type | ||
| + | */ | ||
| + | var filterForMonsterType = function(e) { | ||
| + | var $target = $(e.target); | ||
| + | selectFilter($target); | ||
| + | var monsterType = $target.text(); | ||
| + | $rows.hide(); | ||
| + | spots.each((function(i, spot) { | ||
| + | var lowerCaseDescription = spot.description.toLowerCase(); | ||
| + | if (!lowerCaseDescription.match(new RegExp(`${monsterType}[s\W\s]`))) return; | ||
spot.$tr.show(); | spot.$tr.show(); | ||
})); | })); | ||
| Line 107: | Line 122: | ||
$('.filterLink').click(filterForClass); | $('.filterLink').click(filterForClass); | ||
$('#levelFilter').change(filterForLevel); | $('#levelFilter').change(filterForLevel); | ||
| + | $('.monsterFilterLink').click(filterForMonsterType); | ||
Revision as of 21:02, 4 December 2017
// Basic DOM manipulation onReady (since the wiki won't let us add <input> tags in the wiki text
$('#levelFilterPlaceholder').replaceWith('<input id="levelFilter" style="width:30px"/>');
// Get jQuery object of every row in the hunting guide (because all of the filter functions need it)
var $rows = $('.wikitable tbody tr');
/**
* Build a in-memory data object with the data parsed from each row (which will tell us which
* levels/classes should be shown for any filter).
* @example a "spot" created by this function for critters in Butcherblock Mountains: {
* soloLevel: '04-06',
* groupLevel: '03-05',
* zone: 'Butcherblock Mountains',
* area: 'Greater Faydark zoneline',
* monsters: 'Assorted Critters ',
* xpMod: '100%',
* notes: '',
* $tr: *a jQuery object pointing to the <tr> that all this data originally came from*
* }
*/
var spots = $rows.map(function (i, el) {
var groupLevel = $(el).children('td:eq(1)').text();
groupLevel = groupLevel.trim() === '-' ? null : groupLevel;
var $tr = $(el);
return {
soloLevel: $tr.children('td:eq(0)').text(),
groupLevel,
zone: $tr.children('td:eq(2)').text(),
area: $tr.children('td:eq(3)').text(),
monsters: $tr.children('td:eq(4)').text(),
xpMod: $tr.children('td:eq(5)').text(),
notes: $tr.children('td:eq(6)').text(),
$tr
}
});
/**
* Filter out (hides) rows which aren't for the level the user entered
*/
var filterForLevel = function () {
var level = parseInt($('#levelFilter').val(), 10);
var $rows = $('.wikitable tbody tr');
$rows.hide();
spots.each(function(i, spot) {
var levelRangeText = spot.$tr.children('td:first').text();
var splitLevelRanges = levelRangeText.split('-');
var minLevel = parseInt(splitLevelRanges[0], 10);
var maxLevel = parseInt(splitLevelRanges[1] || splitLevelRanges[0], 10);
if (minLevel <= level && maxLevel >= level) {
spot.$tr.show();
}
});
selectFilter($('#levelFilterLabel'));
hideHeaders();
};
/**
* Filters out (hides) rows which aren't specifically "good for" the clicked class
*/
var filterForClass = function(e) {
var $target = $(e.target);
selectFilter($target);
var eqClass = $target.text();
if (eqClass.trim().toLowerCase() === "none") {
$rows.show();
return;
}
$rows.hide();
spots.each((function(i, spot) {
var lowerCaseNotes = spot.notes.toLowerCase();
if (!lowerCaseNotes.match(new RegExp(`good for .*?${eqClass.toLowerCase()}`))) return;
spot.$tr.show();
}));
hideHeaders();
}
/**
* Filters out (hides) rows which don't involve the specified monster type
*/
var filterForMonsterType = function(e) {
var $target = $(e.target);
selectFilter($target);
var monsterType = $target.text();
$rows.hide();
spots.each((function(i, spot) {
var lowerCaseDescription = spot.description.toLowerCase();
if (!lowerCaseDescription.match(new RegExp(`${monsterType}[s\W\s]`))) return;
spot.$tr.show();
}));
hideHeaders();
}
/**
* Hides the table headers and level range headings for level ranges that have been completely
* filtered out
*/
var hideHeaders = function () {
// If anything got hidden by previous filters, show it (because this filter might not hide it)
$('table, h4').show();
// Hide the tables
$('table').each(function(i, table) {
var $table = $(table);
if (!$table.find('tbody').children('tr:visible').length) $table.hide();
});
// Hide the headings (<h4> elements)
$('h4').each(function(i, h4) {
var $h4 = $(h4);
$h4.toggle($h4.next('table').is(':visible'))
});
}
/**
* Visually marks the currently-used filter with an underline
*/
var selectFilter = function ($target) {
$('.filterLink, #levelFilterLabel').css({textDecoration: ''});
$target.css({textDecoration: 'underline'});
}
// Bind event handlers
$('.filterLink').click(filterForClass);
$('#levelFilter').change(filterForLevel);
$('.monsterFilterLink').click(filterForMonsterType);