User:Monster Domosed/common.js: Difference between revisionsGive feedback
Add support for HeroIcon for tooltips? |
Scrapped HeroIcon hovers for now, added support for AbilityIcon for tooltips? |
||
| Line 303: | Line 303: | ||
/** | /** | ||
* Custom | * Custom Tooltips — replaces Extension:Popups for .item-link-wrapper and .ability-link-wrapper links | ||
*/ | */ | ||
( function () { | ( function () { | ||
| Line 386: | Line 386: | ||
} | } | ||
/* ─── Show tooltip | /* ─── Show tooltip ─── */ | ||
function | function showTooltip( anchor, name, type ) { | ||
clearTimeout( hideTimer ); | clearTimeout( hideTimer ); | ||
clearTimeout( showTimer ); | clearTimeout( showTimer ); | ||
| Line 394: | Line 394: | ||
var tt = getTooltip(); | var tt = getTooltip(); | ||
currentTarget = anchor; | currentTarget = anchor; | ||
var cacheKey = type + ':' + name; | |||
// Already cached — show immediately | // Already cached — show immediately | ||
if ( tooltipCache[ | if ( tooltipCache[ cacheKey ] ) { | ||
tt.html( tooltipCache[ | tt.html( tooltipCache[ cacheKey ] ).show(); | ||
positionTooltip( anchor ); | positionTooltip( anchor ); | ||
return; | return; | ||
| Line 406: | Line 407: | ||
positionTooltip( anchor ); | positionTooltip( anchor ); | ||
// | // Build template text based on type | ||
var templateText; | |||
if ( type === 'item' ) { | |||
templateText = '{{Infobox item/auto|default=true|' + name + '}}'; | |||
} else if ( type === 'ability' ) { | |||
templateText = '{{Ability card v2' + | |||
'|hero_name={{#invoke:Abilities/utils|find_hero_by_ability|' + name + '}}' + | |||
'|ability_num={{#invoke:Abilities|get_ability_number|{{#invoke:Abilities/utils|find_hero_by_ability|' + name + '}}|' + name + '}}' + | |||
'}}'; | |||
} | } | ||
// Fetch rendered template | |||
// Fetch rendered | |||
new mw.Api().post( { | new mw.Api().post( { | ||
action: 'parse', | action: 'parse', | ||
text: | text: templateText, | ||
title: mw.config.get( 'wgPageName' ), | title: mw.config.get( 'wgPageName' ), | ||
prop: 'text', | prop: 'text', | ||
| Line 466: | Line 430: | ||
} ).then( function ( data ) { | } ).then( function ( data ) { | ||
var html = data.parse.text[ '*' ]; | var html = data.parse.text[ '*' ]; | ||
tooltipCache[ | tooltipCache[ cacheKey ] = html; | ||
// Only show if the mouse is still on this element | // Only show if the mouse is still on this element | ||
| Line 491: | Line 455: | ||
currentTarget = null; | currentTarget = null; | ||
}, HIDE_DELAY ); | }, HIDE_DELAY ); | ||
} | |||
/* ─── Attach handlers to wrapper ─── */ | |||
function attachTooltipHandlers( $wrapper, name, type ) { | |||
// All links inside the wrapper, not just the first one | |||
var $links = $wrapper.find( 'a[href]' ); | |||
// If no links found, attach to the wrapper itself (for icon-only mode) | |||
if ( !$links.length ) { | |||
$links = $wrapper; | |||
} | |||
$links.each( function () { | |||
var $link = $( this ); | |||
// Remove native title from each link | |||
$link.removeAttr( 'title' ); | |||
$link.on( 'mouseenter.customTooltip', function () { | |||
showTooltip( this, name, type ); | |||
} ).on( 'mouseleave.customTooltip', function () { | |||
scheduleHide(); | |||
} ); | |||
} ); | |||
} | } | ||
/* ─── Initialization ─── */ | /* ─── Initialization ─── */ | ||
mw.hook( 'wikipage.content' ).add( function ( $content ) { | mw.hook( 'wikipage.content' ).add( function ( $content ) { | ||
// | // Item tooltips | ||
$content.find( '.item-link-wrapper' ).each( function () { | $content.find( '.item-link-wrapper' ).each( function () { | ||
var $wrapper = $( this ); | var $wrapper = $( this ); | ||
| Line 501: | Line 489: | ||
if ( !itemName ) return; | if ( !itemName ) return; | ||
attachTooltipHandlers( $wrapper, itemName, 'item' ); | |||
} ); | } ); | ||
// | // Ability tooltips | ||
$content.find( '. | $content.find( '.ability-link-wrapper' ).each( function () { | ||
var $wrapper = $( this ); | var $wrapper = $( this ); | ||
var | var abilityName = $wrapper.attr( 'data-ability-name' ); | ||
if ( ! | if ( !abilityName ) return; | ||
attachTooltipHandlers( $wrapper, abilityName, 'ability' ); | |||
} ); | } ); | ||
} ); | } ); | ||
} )(); | } )(); | ||