Toggle menu
Toggle preferences menu
Toggle personal menu
Not logged in
Your IP address will be publicly visible if you make any edits.

MediaWiki:Common.js: Difference between revisions

MediaWiki interface page
No edit summary
No edit summary
Line 22: Line 22:
}
}
}());
}());
// Firefox APZ bug - Fix sticky TOC offset by forcing reposition when detected
mw.hook('wikipage.content').add(function ($content) {
if (!/firefox/i.test(navigator.userAgent)) {
return;
}
const toc = document.getElementById('citizen-toc');
if (!toc) {
return;
}
let t = 0;
window.addEventListener('scroll', function() {
// Prevent multiple instances
if (t) {
return;
}
t = setTimeout(function () {
t = 0;
if (toc.getBoundingClientRect().y < -50) {
toc.classList.toggle('ff-fix');
}
}, 16);
}, { passive: true });
});


mw.loader.load('/resources/assets/kompas.js?dev=' + Date.now());
mw.loader.load('/resources/assets/kompas.js?dev=' + Date.now());

Revision as of 06:49, 25 January 2026

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

// Wait for document ready
$(function() {
	const htmle = (str) => String(str).replace(/[&<>"']/g, (c) => ({ '&': '&amp;', '<': '&lt;', '>': '&gt;', '"': '&quot;', "'": '&#39;' }[c]));

	if ($('body').hasClass('page-Summertime_Saga_Wiki') && $('body').hasClass('action-view') && $('#ajax-posts').length && $('#ajax-version').length) {
		const maxPosts = 5;

		$.getJSON('https://summertimesaga.com/data/wiki.php', function(data) {
			$('#ajax-version').text(data.version);
			$('#ajax-posts').html('');

			data.posts.forEach(function(post, i) {
				if (i >= maxPosts) {
					return;
				}

				$('#ajax-posts').append('<dd><b>▪ ' + htmle(post.date) + ':</b> <a rel="nofollow" class="external text" href="https://www.patreon.com' + htmle(post.url) + '">' + htmle(post.title) + '</a></dd>');
			});
		});
	}
}());

// Firefox APZ bug - Fix sticky TOC offset by forcing reposition when detected
mw.hook('wikipage.content').add(function ($content) {
	if (!/firefox/i.test(navigator.userAgent)) {
		return;
	}

	const toc = document.getElementById('citizen-toc');
	if (!toc) {
		return;
	}

	let t = 0;

	window.addEventListener('scroll', function() {
		// Prevent multiple instances
		if (t) {
			return;
		}

		t = setTimeout(function () {
			t = 0;

			if (toc.getBoundingClientRect().y < -50) {
				toc.classList.toggle('ff-fix');
			}
		}, 16);
	}, { passive: true });
});

mw.loader.load('/resources/assets/kompas.js?dev=' + Date.now());

mw.hook('wikipage.content').add(function ($content) {
  // Manage the Preview / Stable tabs
  return;

  const $tabs = $content.find('ul.version-tabs');

  if (!$tabs.length) return;

  const allow = ['21', '0.20.16'];

  const pref = location.hash
             ? location.hash.endsWith('-0.20.16') ? '0.20.16' : '21'
             : localStorage.getItem('preferredVersion') || '21';

  function activateTab(tab) {
    if (allow.indexOf(tab) === -1) return;

    $content.find('div.vt-content').hide();
    $content.find('div.vt-' + tab.replaceAll('.', '-')).show();

    $tabs.find('li').removeClass('active');
    $tabs.find('li[data-tab="' + tab + '"]').addClass('active');

    localStorage.setItem('preferredVersion', tab);
  }

  function rewriteAnchors(scopeSelector, suffix) {
    const $scope = $(scopeSelector);
    const idMap = {};
    const baselines = {}; // lowest index seen

    // Regex to parse base and numeric suffix
    const headingIdRegex = /^(.*?)(?:_(\d+))?$/;

    function getBaseAndIndex(id) {
      const match = id.match(headingIdRegex);
      return {base: match[1], index: match[2] ? parseInt(match[2], 10) : 1};
    }

    // Determine baseline MediaWiki suffixes
    $scope.find('[id]').each(function () {
      const o = getBaseAndIndex(this.id);
      if (!(o.base in baselines) || o.index < baselines[o.base]) {
        baselines[o.base] = o.index;
      }
    });

    // Rewrite all elements with 'id'
    $scope.find('[id]').each(function () {
      const $el = $(this);
      const oldId = $el.attr('id');
      const o = getBaseAndIndex(oldId);
      const baseline = baselines[o.base];

      const newIndex = o.index - baseline + 1;

      const newId = newIndex === 1 ? o.base + suffix : o.base + '_' + newIndex + suffix;
      $el.attr('id', newId);
      idMap[oldId] = newId;
    });

    // Rewrite links
    $scope.find('a[href^="#"]').each(function () {
      const $a = $(this);
      const href = $a.attr('href').substring(1); // strip leading #
      if (idMap[href]) {
        $a.attr('href', '#' + idMap[href]);
      }
    });

    // Rewrite labels
    $scope.find('label[for]').each(function () {
      const $label = $(this);
      const forId = $label.attr('for');
      if (idMap[forId]) {
        $label.attr('for', idMap[forId]);
      }
    });
  }

  $tabs.find('li').click(function (e) {
    e.preventDefault();
    const selected = this.dataset.tab;
    activateTab(selected);
  });

  activateTab(pref);
  rewriteAnchors('div.vt-0-20-16', '-0.20.16');
  $('.citizen-body').addClass('has_version-tabs');

  if (location.hash) {
    location.hash = location.hash;
  }
});