Difference between revisions of "MediaWiki:Common.js"

From Summertime Saga Wiki
Jump to: navigation, search
 
(57 intermediate revisions by 3 users not shown)
Line 1: Line 1:
 
/* Any JavaScript here will be loaded for all users on every page load. */
 
/* Any JavaScript here will be loaded for all users on every page load. */
 
// Google Analytics -Filip
 
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
 
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
 
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
 
})(window,document,'script','https://www.google-analytics.com/analytics.js','ga');
 
 
ga('create', 'UA-19057746-5', 'auto');
 
ga('send', 'pageview');
 
ga('set', 'contentGroup1', 'wiki');
 
  
 
// Wait for document ready
 
// Wait for document ready
 
$(function() {
 
$(function() {
// Set progress bar
+
var htmle = function(str) {
var issueProgress = $('.issue-progress');
+
return String(str).replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/"/g, '&quot;');
if (issueProgress.length !== 0) {
+
};
issueProgress.html('<img src="http://summertimesaga.com/api/build/progress/img.png?width=500&color=eaeced&nc=' + Math.random() + '" alt="" border="0"/>');
 
}
 
  
// Test code for youtube embeds -sam9
+
if ($('body').hasClass('page-Summertime_Saga_Wiki') && $('body').hasClass('action-view') && $('body').hasClass('skin-vector') && $('#ajax-posts').length && $('#ajax-version').length) {
// Example tag: <span class="vid" data-id="YT_ID">VID_TITLE</span>
+
var maxPosts = 5;
var vidTags = $('.vid');
 
  
// Check to make sure vid tags exist
+
$.getJSON('/ssdata.json', function(data) {
if (vidTags.length !== 0) {
+
$('#ajax-version').text(data.version);
// Click handler
+
$('#ajax-posts').html('');
$('#content').on('click', '.vid-embed', function(e) {
 
e.preventDefault();
 
  
var elm = $(this);
+
data.posts.forEach(function(post, i) {
$.magnificPopup.open({
+
if (i >= maxPosts) {
items: {
+
return;
src : elm.attr('href'),
 
type : 'iframe'
 
 
}
 
}
 +
 +
$('#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>');
 
});
 
});
 
});
 
});
 +
}
 +
}());
 +
 +
mw.hook('wikipage.content').add(function ($content) {
 +
  // Manage the Preview / Stable tabs
 +
 +
  const $tabs = $content.find('ul.version-tabs');
 +
 +
  if (!$tabs) 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
  
// Main parsing function
+
    // Regex to parse base and numeric suffix
var parseTags = function() {
+
    const headingIdRegex = /^(.*?)(?:_(\d+))?$/;
// Loop through our tags
 
vidTags.each(function() {
 
var elm = $(this);
 
var vidId = elm.data('id');
 
var title = elm.text();
 
  
elm.replaceWith('<a class="vid-embed" href="https://youtube.com/watch?v=' + vidId + '" target="_blank" title="' + title + '" style="background-image:url(//i.ytimg.com/vi/' + vidId + '/maxresdefault.jpg)"><div class="vid-mask"><span class="vid-embed-title">' + title + '</span><div class="vid-play"></div></div></a>');
+
    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]);
 +
      }
 +
    });
 +
  }
  
// Load magnificPopup if it isn't already
+
  $tabs.find('li').click(function (e) {
if (typeof $.magnificPopup === 'undefined') {
+
    e.preventDefault();
// Temporary work around due to lack of server access
+
    const selected = this.dataset.tab;
$.getScript('//cdnjs.cloudflare.com/ajax/libs/magnific-popup.js/1.1.0/jquery.magnific-popup.min.js', function() {
+
    activateTab(selected);
var headElm = $('head');
+
  });
// Load magnificPopup CSS
 
headElm.append('<link rel="stylesheet" type="text/css" href="//cdnjs.cloudflare.com/ajax/libs/magnific-popup.js/1.1.0/magnific-popup.min.css"/>');
 
  
// Add our own custom CSS, again temporary
+
  activateTab(pref);
headElm.append('<style type="text/css"> a.vid-embed { position: relative; text-decoration: none; display: inline-block; width: 384px; height: 216px; margin: 0 10px 10px 0; background-size: cover; background-color: #000; background-position: center center; border: 1px solid #000; transition: border-color 0.15s ease-in-out; box-shadow: 0 0 10px 3px #191B26; border-radius: 2px; overflow: hidden; } a.vid-embed:hover { border-color: #777; text-decoration: none; } a.vid-embed:hover div.vid-mask { background: rgba(0, 0, 0, 0); } a.vid-embed:hover div.vid-play { border-left: 28px solid rgba(255, 255, 255, 0.9); } a.vid-embed:focus { outline: 0; } span.vid-embed-title { text-shadow: 0px 0px 5px #000; color: #cecece; text-align: left; margin: 8px 8px 0 12px; font-size: 16px; display: block; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; } div.vid-mask { background: rgba(0, 0, 0, 0.3); width: 100%; height: 100%; position: absolute; top: 0; left: 0; transition: background 0.15s ease-in-out; } div.vid-play { width: 0; height: 0; border-top: 16px solid transparent; border-left: 28px solid rgba(255, 255, 255, 0.7); border-bottom: 16px solid transparent; position: absolute; top: 0; left: 0; right: 0; bottom: 0; margin: auto; transition: border-left 0.15s ease-in-out; } </style>');
+
  rewriteAnchors('div.vt-0-20-16', '-0.20.16');
  
// Run our main function
+
  if (location.hash) {
parseTags();
+
    location.hash = location.hash;
});
+
  }
} else {
+
});
// Run our main function
 
parseTags();
 
}
 
}
 
}());
 

Latest revision as of 17:28, 11 October 2025

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

// Wait for document ready
$(function() {
	var htmle = function(str) {
		return String(str).replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/"/g, '&quot;');
	};

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

		$.getJSON('/ssdata.json', 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>');
			});
		});
	}
}());

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

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

  if (!$tabs) 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');

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