This is a wiki for a reason. Anyone can contribute. If you see something that is inaccurate or can be improved, don't ask that it be fixed--just improve it.
[ Disclaimer, Create new user --- Wiki markup help, Install P99 ]

MediaWiki:HuntingGuide.js

From Project 1999 Wiki
Revision as of 21:57, 4 December 2017 by Loramin (Talk | contribs)

Jump to: navigation, search

Note: After saving, you may have to bypass your browser's cache to see the changes.

  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
  • Internet Explorer: Hold Ctrl while clicking Refresh, or press Ctrl-F5
  • Opera: Clear the cache in Tools → Preferences
// 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
  }
});

var filter = function(filteringFunction) {
  $('h4, table').hide();
  var rows = spots.map(filteringFunction);
  var $resultsTbody = $('#filterResults').show().find('tbody').empty();
  rows.each(function(i, row) {console.log(row, i)|| $resultsTbody.append(row); });
  selectFilter($('#levelFilterLabel'));
};

/**
 * 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();*/

// New Version
  filter(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) return spot.$tr.clone();
  });
};

/**
 * 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() === "no filter") {
    $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().toLowerCase();
  $rows.hide();
  spots.each((function(i, spot) {
    var lowerCaseMonsters = spot.monsters.toLowerCase();
    if (!lowerCaseMonsters.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)

  // Hide level range headers
  $('h4').hide();
  $('h4:first').next('table').show().find('tr:first').show();
  /*$('h4').each(function(i, h4) {
    var $h4 = $(h4);
    $h4.toggle($h4.next('table').is(':visible'))
    if(!$h4.next('table').is(':visible')) return;
    $h4.next('table').find('tr:first').show()
  });*/
}

/**
 * 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);