MediaWiki:Common.js: Difference between revisions

From Official Gamemode 4 Wiki
Jump to navigation Jump to search
(ok epyon, this should have fixed it)
(do you not like let)
 
(7 intermediate revisions by the same user not shown)
Line 2: Line 2:


// 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 () {
       // Hide non-JS
       // Hide non-JS
       $(this).data('tipText', $(this).attr('title')).removeAttr('title');
       $(this).data('tipText', $(this).attr('title')).removeAttr('title');
Line 11: Line 12:
       var title = $(this).attr('data-title');
       var title = $(this).attr('data-title');
       var lore = $(this).attr('data-lore');
       var lore = $(this).attr('data-lore');
       var tooltip = '<p class="tooltip">' + parseFormatCodes(title)
       var tooltip = '<p class="tooltip">' + parseFormatCodes(title);
       if (lore) tooltip += '<br><span class="lore format-5">' + parseFormatCodes(lore) + '</span>'
       if (lore) tooltip += '<br><span class="lore format-5">' + parseFormatCodes(lore) + '</span>';
       tooltip += '</p>'
       tooltip += '</p>';
       $(tooltip).appendTo('body');
       $(tooltip).appendTo('body');
     },
     },
     function() {
     function () {
       // Hover out
       // Hover out
       $('.tooltip').remove();
       $('.tooltip').remove();
     }).mousemove(function(e) {
     }).mousemove(
    var mousex = e.pageX + 20; //Get X coordinates
      function(e) {
    var mousey = e.pageY - 40; //Get Y coordinates
        var mousex = e.pageX + 20;
    $('.tooltip').css({
        var mousey = e.pageY - 40;
      top: mousey,
        var tooltip = $('.tooltip');
      left: mousex
        if (mousex + tooltip.width() >= $(window).width()) mousex -= tooltip.width() + 40;
    });
        tooltip.css({
  });
          top: mousey,
          left: mousex,
        });
      });
   // Hide the stupid empty newlines after crafting grids
   // Hide the stupid empty newlines after crafting grids
   $('.crafting + p br:only-child').parent().hide();
   $('.crafting + p br:only-child').parent().hide();
   let active = $('.pageactive');
 
  active.each((_, page) => {
 
     setupPage($(page))
  //Code For books
   $('.pageactive').each(function (_, page) {
     setupPage($(page));
   });
   });
   $('.book_leftarrow').click(function() {
   $('.book_leftarrow').click(function () {
     let active = $(this).siblings('.pageactive');
     setupPage($(this).siblings('.pageactive').removeClass('pageactive').prev());
    active.removeClass('pageactive');
    active = active.prev()
    setupPage(active);
   });
   });
   $('.book_rightarrow').click(function() {
   $('.book_rightarrow').click(function () {
     let active = $(this).siblings('.pageactive');
     setupPage($(this).siblings('.pageactive').removeClass('pageactive').next());
    active.removeClass('pageactive');
    active = active.next()
    setupPage(active);
   });
   });




 
  //Code for Cycling
 
  $('.cyclist > :not(:first-child)').hide();
  setInterval(function() {
    if (!!$('.cyclist').filter(function() { return $(this).is(":hover"); }).length) return;
    $('.cyclist > :not(:hidden)').hide().each(function() {
      var elem = $(this);
      elem.next().show();
      if (elem.is(':last-child')) elem.siblings().first().show();
    });
  }, 2000);
});
});


function parseFormatCodes(text) {
function parseFormatCodes (text) {
   var spans = 0;
   var spans = 0;
   var res = ''
   var res = '';
   for (var i = 0; i < text.length; i++) {
   for (var i = 0; i < text.length; i++) {
     if ((text[i] == '&') && /[0-9a-fl-or]/.test(text[i + 1])) continue;
     if (text[i] == '\\') {
     else if (text[i] == '/') {
      res += text[i+1];
      i++;
    } else if (/&[0-9a-fl-o]/.test(text.substring(i,i+2))) {
      res += '<span class="format-' + text[i+1] + '">';
      spans++;
      i++;
    } else if (text.substring(i,i+2) == '&r') {
      res += '</span>'.repeat(spans);
      spans = 0;
      i++;
    } else if (/&#[0-9A-Fa-f]{6}/.test(text.substring(i,i+8))) {
      res += '<span style="color: ' + text.substring(i+1,i+8) + '">';
      spans++;
      i += 7;
     } else if (text[i] == '/') {
       res += '<br>';
       res += '<br>';
       continue;
    } else {
       res += text[i];
     }
     }
    else if ((text[i - 1] == '&') && /[0-9a-fl-or]/.test(text[i])) {
      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);
 
   return res + '</span>'.repeat(spans);
}
}


function setupPage(active) {
function setupPage (active) {
   active.addClass('pageactive');
   active.addClass('pageactive');
   let index = active.index();
   var index = active.index();
   let length = (active.siblings().length - 2);
   var length = active.siblings().length - 2;
   active.siblings('.book_pagenum').text('Page ' + index + ' of ' + length);
   active.siblings('.book_pagenum').text('Page ' + index + ' of ' + length);
   active.siblings('.book_leftarrow, .book_rightarrow').show()
   active.siblings('.book_leftarrow, .book_rightarrow').show();
   if (index == 1) {
   if (index == 1) {
     active.siblings('.book_leftarrow').hide()
     active.siblings('.book_leftarrow').hide();
   }
   }
   if (index == length) {
   if (index == length) {
     active.siblings('.book_rightarrow').hide()
     active.siblings('.book_rightarrow').hide();
   }
   }
}
}

Latest revision as of 23:50, 7 August 2021

/* 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);
      if (lore) tooltip += '<br><span class="lore format-5">' + parseFormatCodes(lore) + '</span>';
      tooltip += '</p>';
      $(tooltip).appendTo('body');
    },
    function () {
      // Hover out
      $('.tooltip').remove();
    }).mousemove(
      function(e) {
        var mousex = e.pageX + 20;
        var mousey = e.pageY - 40;
        var tooltip = $('.tooltip');
        if (mousex + tooltip.width() >= $(window).width()) mousex -= tooltip.width() + 40;
        tooltip.css({
          top: mousey,
          left: mousex,
        });
      });
  // Hide the stupid empty newlines after crafting grids
  $('.crafting + p br:only-child').parent().hide();


  //Code For books
  $('.pageactive').each(function (_, page) {
    setupPage($(page));
  });
  $('.book_leftarrow').click(function () {
    setupPage($(this).siblings('.pageactive').removeClass('pageactive').prev());
  });
  $('.book_rightarrow').click(function () {
    setupPage($(this).siblings('.pageactive').removeClass('pageactive').next());
  });


  //Code for Cycling
  $('.cyclist > :not(:first-child)').hide();
  setInterval(function() {
    if (!!$('.cyclist').filter(function() { return $(this).is(":hover"); }).length) return;
    $('.cyclist > :not(:hidden)').hide().each(function() {
      var elem = $(this);
      elem.next().show();
      if (elem.is(':last-child')) elem.siblings().first().show();
    });
  }, 2000);
});

function parseFormatCodes (text) {
  var spans = 0;
  var res = '';
  for (var i = 0; i < text.length; i++) {
    if (text[i] == '\\') {
      res += text[i+1];
      i++;
    } else if (/&[0-9a-fl-o]/.test(text.substring(i,i+2))) {
      res += '<span class="format-' + text[i+1] + '">';
      spans++;
      i++;
    } else if (text.substring(i,i+2) == '&r') {
      res += '</span>'.repeat(spans);
      spans = 0;
      i++;
    } else if (/&#[0-9A-Fa-f]{6}/.test(text.substring(i,i+8))) {
      res += '<span style="color: ' + text.substring(i+1,i+8) + '">';
      spans++;
      i += 7;
    } else if (text[i] == '/') {
      res += '<br>';
    } else {
      res += text[i];
    }
  }

  return res + '</span>'.repeat(spans);
}

function setupPage (active) {
  active.addClass('pageactive');
  var index = active.index();
  var length = active.siblings().length - 2;
  active.siblings('.book_pagenum').text('Page ' + index + ' of ' + length);
  active.siblings('.book_leftarrow, .book_rightarrow').show();
  if (index == 1) {
    active.siblings('.book_leftarrow').hide();
  }
  if (index == length) {
    active.siblings('.book_rightarrow').hide();
  }
}