2734
правки
Изменения
Нет описания правки
*
* Replaces normal tooltips. Supports minecraft [[formatting codes]] (except k), and a description with line breaks (/).
* Use mcw.useNativeMinetip = true to use normal tooltips, with the description added
*/
mcw.minetip = { // Add normal minetip events, removing legacy tooltip create: function() { var tooltip; $( '#mw-content-text' ).on( { 'mouseenter.minetip': function(e ) { var escapeChars $elem = $( this ), title = $elem.data( 'minetip-title' ), description = $elem.data( 'minetip-text' ); // No title or title only contains formatting codes if ( title === undefined || title && title.replace( /&([0-9a-fl-o])|\s+/g, '' ) = == '' ) { // Use title attribute of the element or the first link directly under it var attrTitle = $elem.attr( '\\&title' ) || $elem.find( '> a: first' ).attr( 'title'&); if ( title === undefined ) { title = attrTitle; } else { title += attrTitle; } if ( title ) { // Set the retrieved title as data for future use $elem.data( 'minetip-title', title ); } else { return; } } $elem.add( '*', $elem ).filter( '[title]'<).removeAttr( ': title'<); if ( title === 0 ) { return; } var text = ', '<span class="title">': + title + '>f</span>' }; var escape = function if ( text description ) { // text += '\n<span class="\description" must be escaped first>' + return text description.replace( /\\\\//g, '␟' ) .replace( /\\//g, '<br>' ) + '&|[f</span>]/g, function'; } if ( !$( char '#minetip-tooltip' ).length ) { return escapeChars[char]; } $( 'body' ).append( '<div id="minetip-tooltip"/>' ); }; var $ tooltip = $('#minetip-tooltip' ); var // Add classes for minecraft formatting codes while ( text.match( /&[0-9a-el-o]/ ) ) { text = text.replace( /&([0-9a-el-o])(.*?)(&f|$win )/g, '<span class= "format-$1">$2</span>&f' ); } // Remove reset formatting text = text.replace( window /&f/g, '' ); tooltip.html( text ), winWidth, winHeight, width, height; // Trigger a mouse movement to position the tooltip $elem.trigger( '#mw-content-textmousemove' , e ).on( {; }, 'mouseentermousemove.minetip': function( e , trigger ) { if ( !$( '#minetip-tooltip' ).remove(length );{ var $elem = $( this ), title = $elem.attrtrigger( 'data-minetip-titlemouseenter' ); if ( title return; } // Get event data from remote trigger e = trigger || e; var top =e.clientY - 34, left =e.clientX + 14, width = undefined tooltip.outerWidth( true ) {, title height = $elemtooltip.attrouterHeight( 'title' true );, if $win = $( title !== undefined window ) {, title winWidth = $win.trimwidth( title), winHeight = $win.replaceheight( ); /&/gIf going off the right of the screen, '\\&' go to the left of the cursor if ( left + width > winWidth ) );{ $elem.attr( 'dataleft -minetip-title', title )= width + 36;
}
}
}
// Append child title as title may contain formatting codesApply the positions tooltip.css( { top: top, left: left } ); }, 'mouseleave.minetip': function() { if ( !title tooltip ) { title = ''return;
}
}
}, '.minetip, .grid .image, .grid .item, .grid2 .item' ).off( '.minetipNative' );
},
// Remove all events
destroy: function() {
$( '#mw-content-text' ).off( '.minetip .minetipNative' );
$( '#minetip-tooltip' ).remove();
},
// Add native browser tooltip events, removing normal minetip
native: function() {
$( '#mw-content-text' ).on( 'mouseenter.minetipNative', '.minetip, .grid .image, .grid .item, .grid2 .item', function() {
var title = $( this ).data( 'minetip-title' ),
description = $( this ).data( 'minetip-text' ),
existingTitle = $( this ).attr( 'title' ) || $( this ).find( '> a:first' ).attr( 'title' );
if ( !title || title === 0 || $elem( this ).dataattr( 'minetip-readytitle' ) ) { // Remove title attributes titles within so the native tooltip doesnthey don't get in the wayinterfere $elem( this ).find( '[title]' ).addBack().removeAttr( 'title' ); $elem.data( 'minetip-ready', true );
}
if ( title === 0 ) { $( this ).removeAttr( 'title' ) {;
return;
} else if ( !title && ( !existingTitle || !description ) ) {
return;
} else if ( !title && existingTitle ) {
$( this ).data( 'minetip-title', existingTitle );
}
var content text = '<span class="minetip-title">' + escape( title ) + '&r</span>'; var description = $.trim( $elem.attr( 'data-minetip-text' ) )|| existingTitle;
if ( description ) {
}
// Trigger a mouse movement to position the tooltipRemove formatting $elemtext = text.triggerreplace( /&( 'mousemove', e [0-9a-fl-o]); }/g, 'mousemove.minetip': function( e, trigger ) { if ( !$tooltip.length ) { $( this ).triggerreplace( 'mouseenter' ); return; } /\\\/ Get event data from remote trigger e = trigger || e; // Get mouse position and add default offsets var top = e.clientY - 34g, '/ var left = e.clientX + 14; // If going off the right of the screen, go to the left of the cursor if ( left + width > winWidth ' ) { left -= width + 36; } // If now going off to the left of the screen, resort to going above the cursor if .replace( left < 0 ) { left = 0; top -= height - 22; // Go below the cursor if too high if ( top < 0 ) { top += height + 47; } \// Dong, 't go off the top of the screen } else if ( top < 0 ) { top = 0; // Don\n't go off the bottom of the screen } else if ( top + height > winHeight ) { top = winHeight - height; } // Apply the positions $tooltip.cssreplace( { top: top, left: left } )// }/g, 'mouseleave.minetip/': function() { if ( !$tooltip.length ) { return; }
$tooltip( this ).removeattr('title', text ); $tooltip = $ } ).off('.minetip' ); }}; if ( mcw.useNativeMinetip ) { }, 'mcw.minetip, .grid > .item, .invslot-item' native();}else { mcw.minetip.create() );}