MediaWiki:Common.js: Difference between revisions

From Official Gamemode 4 Wiki
Jump to navigation Jump to search
(ok epyon, this should have fixed it)
m (or not, maybe this will?)
Line 29: Line 29:
   // 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((_, page) => {
     setupPage($(page));
   });
   });
   $('.book_leftarrow').click(function() {
   $('.book_leftarrow').click(function() {
     let active = $(this).siblings('.pageactive');
     let active = $(this).siblings('.pageactive');
     active.removeClass('pageactive');
     active.removeClass('pageactive');
     active = active.prev()
     active = active.prev();
     setupPage(active);
     setupPage(active);
   });
   });
Line 42: Line 44:
     let active = $(this).siblings('.pageactive');
     let active = $(this).siblings('.pageactive');
     active.removeClass('pageactive');
     active.removeClass('pageactive');
     active = active.next()
     active = active.next();
     setupPage(active);
     setupPage(active);
   });
   });
Line 78: Line 80:
   let length = (active.siblings().length - 2);
   let 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();
   }
   }
}
}

Revision as of 17:52, 15 February 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; //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();


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




});

function parseFormatCodes(text) {
  var spans = 0;
  var res = ''
  for (var i = 0; i < text.length; i++) {
    if ((text[i] == '&') && /[0-9a-fl-or]/.test(text[i + 1])) continue;
    else if (text[i] == '/') {
      res += '<br>';
      continue;
    }
    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);
}

function setupPage(active) {
  active.addClass('pageactive');
  let index = active.index();
  let 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();
  }
}