[ Disclaimer, Create new user --- Wiki markup help, Install P99 ]
MediaWiki:Common.js
From Project 1999 Wiki
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
/* Any JavaScript here will be loaded for all users on every page load. */
// Polyfill for "startsWith" (since IE11 is lame)
if (!String.prototype.startsWith) {
Object.defineProperty(String.prototype, 'startsWith', {
value: function(search, pos) {
pos = !pos || pos < 0 ? 0 : +pos;
return this.substring(pos, pos + search.length) === search;
}
});
}
/* p1999wiki.js
* written by http://wiki.project1999.com/User:Ravhin
* last update: 7 January, 2018 by Loramin
*/
$(function() {
var hideDelay = 0;
var trigDelay = 250;
var hideTimer = null;
var ajax = null;
var currentPosition = { left: '0px', top: '0px' };
// One instance that's reused to show info for the current person
var container = $('<div id="itemHoverContainer">'
+ '<div id="itemHoverContent"></div>'
+ '</div>');
$('body').append(container);
/* --- hoverbox for item/mob, currently only used in magelo --- */
// Determine which "a" elements should trigger the item stats mouseover
var $mouseoverTargets = $('span.ih a');
var isItemCategory = document.title.startsWith('Category:') && document.title.includes('Equipment - Project 1999 Wiki') &&
!document.title.includes('Worshiper Equipment');
if (isItemCategory) {
// Include the category's item list along with ".ih" links
$mouseoverTargets = $mouseoverTargets.add('.mw-content-ltr a');
}
$mouseoverTargets = $mouseoverTargets.filter(function(i, a) {
// Don't add hover to links like "next 200"
return !$(a).attr('href').startsWith('/Special:') &&
!$(a).attr('href').includes('title=Category:')
});
$mouseoverTargets.on('mouseover', function()
{
var $this = $(this);
var itemname = $this.attr('title');
if (itemname == '' || itemname == 'undefined')
return;
if (hideTimer)
clearTimeout(hideTimer);
if ($this.parents('div.mw-content-ltr').length) {
var pos = $this.offset();
var width = $this.width();
container.css({
left: pos.left + 5 + 'px',
top: pos.top + 5 + 'px'
});
}
$(this).trigger('mousemove');
$('#itemHoverContent').html(' ');
//$('#itemHoverContent').html('<div class="itemtopbg"><div class="itemtitle">Loading...</div></div>'
// + '<div class="itembg" style="min-height:50px;"><div class="itemdata">'
// + '<div class="itemicon" style="float:right;"><img alt="" src="/images/Ajax_loader.gif" border="0"></div>'
// + '<p></p></div></div><div class="itembotbg"></div>');
if (ajax)
{
ajax.abort();
ajax = null;
}
ajax = $.ajax({
url: window.location.protocol +
'//wiki.project1999.com/index.php/Special:AjaxHoverHelper/' + itemname,
cacheResponse: true,
success: function(html)
{
var $html = $(html);
$('#itemHoverContent').html($html[2]).prepend($html[0]).prepend($html[1]);
}
});
container.css('display', 'block');
//container.fadeIn('fast');
}); //on mouseover
$('span.ih a').on('mouseout', function()
{
if (hideTimer)
clearTimeout(hideTimer);
hideTimer = setTimeout(function()
{
container.css('display', 'none');
//container.fadeOut('fast');
}, hideDelay);
});
$('span.ih a').mousemove(function(e){
var mousex = e.pageX + 20; //Get X coodrinates
var mousey = e.pageY + 20; //Get Y coordinates
var tipWidth = container.width(); //Find width of tooltip
var tipHeight = container.height(); //Find height of tooltip
//Distance of element from the right edge of viewport
var tipVisX = $(window).width() - (mousex + tipWidth);
//Distance of element from the bottom of viewport
var tipVisY = $(window).height() - (mousey + tipHeight);
if ( tipVisX < 20 ) { //If tooltip exceeds the X coordinate of viewport
if( tipWidth > e.pageX - 20 ){
mousex = 0;
} else {
mousex = e.pageX - tipWidth - 20;
}
} if ( tipVisY < 20 ) { //If tooltip exceeds the Y coordinate of viewport
mousey = e.pageY - tipHeight - 20;
}
container.css({ top: mousey, left: mousex });
});
// Allow mouse over of details without hiding details
$('#itemHoverContainer').mouseover(function()
{
if (hideTimer)
clearTimeout(hideTimer);
});
// Hide after mouseout
$('#itemHoverContainer').mouseout(function()
{
if (hideTimer)
clearTimeout(hideTimer);
hideTimer = setTimeout(function()
{
container.css('display', 'none');
//container.fadeOut('fast');
}, hideDelay);
});
// magelo non-ajax item hover, but move box with mouse
$('.magelohb').mousemove(function(e){
var childContainer = $(this).children('span.hb');
var tipWidth = childContainer.width(); //Find width of tooltip
var tipHeight = childContainer.height(); //Find height of tooltip
var mousex = e.pageX + 20; //Get X coodrinates
var mousey = e.pageY + 20; //Get Y coordinates
//Distance of element from the right edge of viewport
var tipVisX = $(window).width() - (mousex + tipWidth - 20);
//Distance of element from the bottom of viewport
var tipVisY = $(window).height() - (mousey + tipHeight - 20);
if ( tipVisX < 20 ) { //If tooltip exceeds the X coordinate of viewport
if( tipWidth > e.pageX - 20){
mousex = 0;
} else {
mousex = e.pageX - tipWidth - 20;
}
} if ( tipVisY < 20 ) { //If tooltip exceeds the Y coordinate of viewport
mousey = e.pageY - tipHeight - 20;
}
childContainer.css({ top: mousey, left: mousex, 'z-index':'999' });
});
// change to position:fixed on all hover divs if we have JS active
// otherwise leave as position:absolute so the stationary hovers are near their items
$('.magelohb span.hb').each(function(i) {
$(this).css({'position':'fixed'});
});
// Chrome no longer displays alt text when an image is hovered over. However the wiki only has
// alt attributes for images, not titles. This fixes that by converting alt => title
$('img[alt]').each(function (i, img) {
$(img).attr('title', $(img).attr('alt'));
});
//*****************************************
// Add class-based filtering to item tables
//*****************************************
// Determine whether or not a provided row should be shown for the provided
// class abbreviation (eg. "BRD")
var isRowShown = function($tr, classAbbrev) {
// Extract classes (and races)
var classText = $tr.find('td').eq(3).text().split('Class:')[1];
if (!classText) return true; // Ignore (don't filter) rows without class text
// remove "Race: " part (if any) and upper-case text
classText = classText.split('Race:')[0] || classText;
classText = classText.toUpperCase();
// determine matching text
var isMatch = classText.includes(classAbbrev);
var isAllMatch = classText.includes('ALL');
var isExcept = classText.includes('ALL EXCEPT');
// determine matches
if (isMatch && !isExcept) return true; // (eg. BRD in "BRD")
if (!isMatch && isExcept) return true; // (eg. BRD in "ALL EXCEPT WIZ")
if (!isExcept && isAllMatch) return true; // (eg. BRD in "ALL")
return false;
};
var scrollToTable = function($table) {
$('html, body').animate({ scrollTop: ($table.offset().top) }, 200);
};
// Filter the provided table to only display rows for the provided class
// abbreviation
var filterTable = function($table, classAbbrev) {
$table.find('tr').each((i, tr) => {
var $tr = $(tr);
$tr.toggle(isRowShown($tr, classAbbrev));
});
scrollToTable($table);
var $statsHeaderCells = $table.find('th:contains("Stats")');
addFilterLink($table, false);
$('.itemsUnfilterLink').click(function() {
unfilterTable($table);
});
alert('Filtered to only show rows containing ' + classAbbrev + ' gear.');
};
// Restore a table to its previous, un-filtered state
var unfilterTable = function($table) {
$table.find('tr').show();
addItemFilteringLinksToTable($table);
scrollToTable($table);
};
// When a user clicks on a table class filter link, handle it by asking them
// which class (and then filtering the table for that class)
var handleItemFilterLinkClick = function(e) {
var promptMessage = 'Please enter the three-letter abbreviation for the ' +
'class you want to filter by (eg. "brd" for "Bard").';
var $table = $(e.target).closest('table');
var classAbbrev = prompt(promptMessage).toUpperCase();
if (!classAbbrev || classAbbrev.length !== 3 || classAbbrev === 'ALL') {
alert('A 3-digit abbreviation wasn\'t entered (or "All" was entered); ' +
'filtering disabled');
unfilterTable($table);
return false;
}
filterTable($table, classAbbrev);
};
// Adds either a filter or unfilter link (as determined by showFilter) to the
// provided table
var addFilterLink = function($table, showFilter) {
var $statsHeaderCells = $table.find('th:contains("Stats")');
var filterType = showFilter ? 'Filter' : 'Unfilter';
$statsHeaderCells.html('Stats <span style="float:right">' +
'(<a class="items' + filterType + 'Link" href="#">' +
filterType + ' by Class</a>)</span>');
var $link = $statsHeaderCells.find('.items' + filterType + 'Link');
if (showFilter) $link.click(handleItemFilterLinkClick);
else $link.click(function() { unfilterTable($table); })
};
// Add class filtering links to all item tables
var addItemFilteringLinks = function() {
var $tables = $('table th.headerSort:contains("Item Name")').closest('table');
$tables.each(function(i, table) {
addFilterLink($(table), true);
});
};
addItemFilteringLinks();
//*****************************************
// End class-based filtering to item tables
//*****************************************
importScript('MediaWiki:CheckboxLists.js');
var fullTitle = $('title').text();
var title = fullTitle.substr(0, fullTitle.length - ' - Project 1999 Wiki'.length);
switch(title) {
// Give the Per-Level Hunting Guide Page its own JS
case 'Per-Level Hunting Guide':
importScript('MediaWiki:HuntingGuide.js');
break;
// The item category search also needs special JS
case 'Item Category Search':
importScript('MediaWiki:ItemCategorySearch.js');
break;
// So does the Solo Artist Challenge
case 'Solo Artist Challenge':
importScript('MediaWiki:SoloArtistChallenge.js');
break;
case 'Treasure Hunting Guide':
importScript('MediaWiki:TreasureHuntingGuide.js');
break;
case 'Buff Lines':
importScript('MediaWiki:BuffLines.js');
break;
case 'Mobs By Level':
// Basic DOM manipulation (since the wiki won't let us add <input> tags in the wiki text
$('#placeholder')
.replaceWith(
'<form id="form">' +
'Class: <input id="class" style="width: 8em" value="Warrior" /> ' +
'Level: <input id="level" style="width: 4em" /> ' +
'<input type="submit"/>' +
'</form>'
);
$('#form').submit(function() {
var clazz = $('#class').val();
var level = $('#level').val();
window.location = 'https://wiki.project1999.com/index.php?title=Special:Search&limit=500&offset=0&redirs=0&profile=default&search=%22Level%5C%3A%5C+' +
level + '%22+-%22Shopkeeper%22+-%22Merchant%22+%22Class%5C%3A%5C+'+ clazz + '%22+-"startMageloProfile"';
return false;
});
break;
}
// Warn users who accidentally try to edit a templated section
if ((window.location + '').includes('title=Template:Namedmobpage&action=edit')) {
alert('Warning: You are attempting you edit the template for all named mobs. You probably didn\'t mean to do that: you probably clicked on an edit link somewhere in the page and wound up here. To avoid this simply go back and use the edit *tab* at the top of the page instead.');
}
});