MediaWiki:Common.js: Difference between revisions

From Official Gamemode 4 Wiki
Jump to navigation Jump to search
(fix non-styled tooltips)
(LORE)
Line 3: Line 3:
// Even More fancy hovers for recipes
// Even More fancy hovers for recipes
$(function() {
$(function() {
    // Tooltip only Text
  // Tooltip only Text
    $('.masterTooltip').hover(function(){
  $('.masterTooltip').hover(function() {
        // Hover over code
      // Hide non-JS
        var title = $(this).attr('title');
      $(this).data('tipText', $(this).attr('title')).removeAttr('title');
        $(this).data('tipText', title).removeAttr('title');
 
        // Add formating with &
      //Custom Tooltip
        var codes = title.match(/&[0-9a-fl-o]/g)
      var title = $(this).attr('data-title');
        if (!codes) codes = []
      var lore = $(this).attr('data-lore');
        var classes = codes.map(function(x){return 'format-' + x[1]}).join(' ')
      var tooltip = '<p class="tooltip">' + parseFormatCodes(title) + '<br><span class="lore">' + parseFormatCodes(lore) + '</span></p>'
        $('<p class="tooltip ' + classes + '">' + title.slice(codes.length * 2) + '</p>')
      $(tooltip).appendTo('body');
        //.text(title + "\n1")
     },
        .appendTo('body')
    function() {
        .show();
      // Hover out
     }, function() {
      $('.tooltip').remove();
        // Hover out code
        $(this).attr('title', $(this).data('tipText'));
        $('.tooltip').remove();
     }).mousemove(function(e) {
     }).mousemove(function(e) {
        var mousex = e.pageX + 20; //Get X coordinates
    var mousex = e.pageX + 20; //Get X coordinates
        var mousey = e.pageY - 40; //Get Y coordinates
    var mousey = e.pageY - 40; //Get Y coordinates
        $('.tooltip').css({ top: mousey, left: mousex });
    $('.tooltip').css({
      top: mousey,
      left: mousex
     });
     });
  // Hide the stupid empty newlines after crafting grids
  });
  $('.crafting + p br:only-child').parent().hide();
  // Hide the stupid empty newlines after crafting grids
  $('.crafting + p br:only-child').parent().hide();
});
});
function parseFormatCodes(text) {
  var spans = 0;
  var res = ''
  for (var i = 1; i < text.length; i++) {
    if (text[i] == '&') continue;
    else if (text[i] == '/') res += '<br>';
    else if (text[i - 1] == '&') {
      if (text[i] == 'r') {
        res += "</span>".repeat(spans);
        spans = 0;
      } else {
        res += '<span class="format-' + text[i] + '">';
        spans++;
      }
    } else res += text[i];
  }
  return res + "</span>".repeat(spans);
}

Revision as of 01:29, 11 August 2020

/* Any JavaScript here will be loaded for all users on every page load. */

// Even More fancy hovers for recipes
$(function() {
  // Tooltip only Text
  $('.masterTooltip').hover(function() {
      // Hide non-JS
      $(this).data('tipText', $(this).attr('title')).removeAttr('title');

      //Custom Tooltip
      var title = $(this).attr('data-title');
      var lore = $(this).attr('data-lore');
      var tooltip = '<p class="tooltip">' + parseFormatCodes(title) + '<br><span class="lore">' + parseFormatCodes(lore) + '</span></p>'
      $(tooltip).appendTo('body');
    },
    function() {
      // Hover out
      $('.tooltip').remove();
    }).mousemove(function(e) {
    var mousex = e.pageX + 20; //Get X coordinates
    var mousey = e.pageY - 40; //Get Y coordinates
    $('.tooltip').css({
      top: mousey,
      left: mousex
    });
  });
  // Hide the stupid empty newlines after crafting grids
  $('.crafting + p br:only-child').parent().hide();
});

function parseFormatCodes(text) {
  var spans = 0;
  var res = ''
  for (var i = 1; i < text.length; i++) {
    if (text[i] == '&') continue;
    else if (text[i] == '/') res += '<br>';
    else if (text[i - 1] == '&') {
      if (text[i] == 'r') {
        res += "</span>".repeat(spans);
        spans = 0;
      } else {
        res += '<span class="format-' + text[i] + '">';
        spans++;
      }
    } else res += text[i];
  }
  return res + "</span>".repeat(spans);
}