MediaWiki:Gadget-falloff.js

Note: After publishing, you may have to bypass your browser's cache to see the changes.

  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
  • Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5.
// Damage falloff charts ({{Falloff chart}}): a line and a readout follow the
// mouse or a finger across the plot, like the in-game inspect window.
mw.hook('wikipage.content').add(function ($content) {
	$content.find('.ts-falloff-plot').each(function () {
		var plot = this;
		var bars = plot.querySelectorAll('.ts-falloff-bar');
		if (!bars.length) {
			return;
		}
		bars.forEach(function (bar) {
			bar.removeAttribute('title');
		});
		var line = document.createElement('div');
		line.className = 'ts-falloff-cursor';
		var readout = document.createElement('div');
		readout.className = 'ts-falloff-readout';
		plot.appendChild(line);
		plot.appendChild(readout);

		function show(clientX) {
			var box = plot.getBoundingClientRect();
			var x = Math.min(Math.max(clientX - box.left, 0), box.width - 1);
			var bar = bars[Math.min(bars.length - 1, Math.floor(x / box.width * bars.length))];
			line.style.left = x + 'px';
			readout.textContent = bar.getAttribute('data-readout');
			line.style.display = readout.style.display = 'block';
			var w = readout.offsetWidth;
			readout.style.left = (x + 8 + w > box.width ? Math.max(0, x - 8 - w) : x + 8) + 'px';
		}
		function hide() {
			line.style.display = readout.style.display = 'none';
		}
		plot.addEventListener('mousemove', function (e) { show(e.clientX); });
		plot.addEventListener('mouseleave', hide);
		plot.addEventListener('touchstart', function (e) { show(e.touches[0].clientX); }, { passive: true });
		plot.addEventListener('touchmove', function (e) { show(e.touches[0].clientX); }, { passive: true });
	});
});