MediaWiki:DynamicTemplates.js

function infobox(window, $) { var MediaCollection = { init: function($content) { var $mediaCollections = $content.find('.pi-media-collection');

$mediaCollections.each( function( index ) {               var $collection = $mediaCollections.eq(index),                    $tabs = $collection.find('ul.pi-media-collection-tabs li'),                    $tabContent = $collection.find('.pi-media-collection-tab-content');

$tabs.click( function {                   var $target = $(this),                        tabId = $target.attr('data-pi-tab');

$tabs.removeClass('current'); $tabContent.removeClass('current');

$target.addClass('current'); $collection.find('#' + tabId).addClass('current'); });           });        }    };

var CollapsibleGroup = { init: function($content) { var $collapsibleGroups = $content.find('.pi-collapse');

$collapsibleGroups.each( function( index ) {               var $group = $collapsibleGroups.eq(index),                    $header = $group.find('.pi-header:first');

$header.click( function {                   $group.toggleClass('pi-collapse-closed');

// SUS-3245: lazy-load any images in the un-collapsed section $(window).trigger('scroll'); });           });        }    };

var Panel = { init: function($content) { var $panels = $content.find('.pi-panel');

$panels.each( function( index ) {               var $panel = $panels.eq(index),                    $toggles = $panel.find('.pi-section-navigation');

$toggles.on('click', function(e) {                   var toggle = e.target.closest('.pi-section-tab');

if (toggle !== null) { var $newActiveToggle = $(toggle), newRef = $newActiveToggle.attr('data-ref'), $oldActiveToggle = $toggles.find('.pi-section-active'), $oldActiveContent = $panel.find('.pi-section-content.pi-section-active'), $newActiveContent = $panel.find('.pi-section-content[data-ref=' + newRef + ']');

$oldActiveToggle.removeClass('pi-section-active'); $oldActiveContent.removeClass('pi-section-active');

$newActiveToggle.addClass('pi-section-active'); $newActiveContent.addClass('pi-section-active'); };               });            }.bind(this)); },   };

$content = $(document);

MediaCollection.init($content); CollapsibleGroup.init($content); Panel.init($content); };

var types = ["inlinesubst"]; var prefix = "js-special-div js-special-unprocessed";

var promises = [];

var locked = false; var lock_counter = 0;

function sleep(ms) { return new Promise(resolve => setTimeout(resolve, ms)); };

function randint(min, max) { min = Math.ceil(min); max = Math.floor(max); return Math.floor(Math.random * (max - min + 1)) + min; };

function getElementsByClassName(array, class_) { var out = []; for (var itr = 0; itr < array.length; itr++) if (array[itr].classList.contains(class_)) out.push(array[itr]); return out; };

async function doit { var elements = get_unprocessed(document.body); while (elements.length) { doit_(elements); locked = true; lock_counter = 0; if (promises.length) Promise.all(promises).then(function {locked = false;}); promises = []; while (locked) { await sleep(10); lock_counter++; };       elements = get_unprocessed(document.body); }; };

function doit_(elements) { console.log(elements); types.forEach(function (type) {getElementsByClassName(elements, "js-special-type-" + type).forEach(eval("doit_" + type));}); };

function get_unprocessed(element) { var array = []; Array.from(element.getElementsByClassName(prefix)).forEach(function (item) {Array.prototype.push.apply(array, get_unprocessed(item));}); if (element.classList.contains("js-special-div") && element.classList.contains("js-special-unprocessed")) array.push(element); return array; };

function doit_inlinesubst(div, index, array) { var data = div.getAttribute("data-content"); var restorevars = div.getAttribute("data-restorevars"); var revid = div.getAttribute("data-revid"); if (revid === "") revid = 1; else revid = parseInt(revid); var pagetitle = div.getAttribute("data-pagetitle"); var id = div.id; div.className = "js-special-div js-special-placeholder"; div.id = id.replace("unprocessed", "placeholder"); div.innerHTML = ""; div.removeAttribute("data-restorevars"); div.removeAttribute("data-revid"); div.removeAttribute("data-pagetitle"); div.removeAttribute("data-content"); div.style = ""; div.style.position = "absolute"; div.style.display = "none"; div.style.width = "0%"; div.style.height = "0%"; div.style.opacity = 0; div.style["z-index"] = 0; var open_template = ""; var wikitext = open_template + "subst:restorevars|" + restorevars + close_template + open_template + "#vardefine:__var_list|" + restorevars + close_template + open_template + "vardefine|seedcnt|" + randint(0, 65536).toString + close_template + $(" ").html(data).text.toString.replace(/\[escape:1\]/g, "").replace(/\[escape:(\d+)\]/g, function(_, value) {return "[escape:" + (parseInt(value) - 1).toString + "]";}); console.log(wikitext); var api = new mw.Api; promises.push(api.post({ "action": "parse", "format": "json", "title": pagetitle, "text": wikitext, "revid": revid, "pst": true, "disablelimitreport": false, "disableeditsection": true, "disablestylededuplication": false, "preview": true, "wrapoutputclass": "js-special-div js-special-processed-inner", "effectivelanglinks": true }).then(function (response) { var parsed = response.parse.text["*"]; var parsed_div = document.createElement("div"); parsed_div.innerHTML = parsed; parsed_div.className = "js-special-div js-special-processed js-special-type-inlinesubst"; parsed_div.id = id.replace("unprocessed", "processed"); div.parentNode.insertBefore(parsed_div, div.nextSibling); sleep(3000); infobox(window, jQuery); }).fail(function (error) {document.write("error: " + error + "\n\ndebug: title=" + pagetitle + ", revid=" + revid + ", text=" + wikitext);})); sleep(750); };

doit;