User:Glakuns/common.junk

/* * Show All / Hide All extension to Media Wiki's Collapsible functionality * * Contract: * *   For every pair of elements of class mw-customtoggle and mw-collapsible, *  the elements must also have a matching class in the front of their list of classes *  not including mw-showall or mw-hideall. *  For every Show All or Hide All link/button, this class must be the first class *  not including mw-showall or mw-hideall. * *  We just so happen to be hijacking the 'plainlinks' class for this purpose, *  but that does not have to be the case. The matching class in the front of the list *  of classes will work. */ // This function is not used. Examples kept in case order is necessary. function mwGetOrderedElementsByClassName( mwClass, mapFn ) { // mwToggles = mwGetOrderedElementsByClassName(mwCollapsiblePairClass+' mw-customtoggle', function(a, i){return {index: i, value: a.className.split(/[ -]/)[3]}}); // mwCollapsibles = mwGetOrderedElementsByClassName(mwCollapsiblePairClass+' mw-collapsible', function(a, i){return {index: i, value: a.id.split(/[ -]/)[2]}});

var mapped; mwObjects = document.getElementsByClassName(mwClass); mapped   = Array.prototype.slice.call(mwObjects).map(mapFn); mapped.sort(function(a, b) {   return a.value > b.value ? 1 : (a.value < b.value ? -1 : 0); });  return mapped.map(function(el){    mwObject = mwObjects[el.index];    mwObject.idglak = el.value;    return mwObject;  }); }

// Assert function meant to catch contract violations. function mwAssertIdglakArrays( arri, arriName, arrj, arrjName ) { // assert that mwToggle has a matching mwCollapsible and vice versa if (arri.length != arrj.length) { console.log(arri.length+' of '+arriName+' is not '+arrj.length+' like '+arrjName); } for (var i=0; i<arri.length; i++) { var idglak = arri[i].idglak; if (idglak) { var matches = []; for (var j=0; j<arrj.length; j++) { if (idglak == arrj[j].idglak) { matches.push(idglak); }     }      if (matches.length == 0) { console.log((idglak ? idglak : '') +' in '+arriName+' is not in '+arrjName); }   }  }  return arri; }

// Lazily capture mw-customtoggle and mw-collapsible objects var mwShowHidePairs = {}; function mwTogglesDefine( mwCollapsiblePairClass ) { if (!(mwCollapsiblePairClass in mwShowHidePairs)) { mwShowHidePairs[mwCollapsiblePairClass] = {}; } mwCollapsiblePair = mwShowHidePairs[mwCollapsiblePairClass]; if ('mwToggles' in mwCollapsiblePair) { return mwCollapsiblePair['mwToggles']; } mwCollapsiblePair['mwToggles'] = document.getElementsByClassName(mwCollapsiblePairClass+' mw-customtoggle'); if ('mwCollapsibles' in mwShowHidePairs[mwCollapsiblePairClass]) { mwAssertIdglakArrays(mwCollapsiblePair['mwToggles'], 'mwToggles', mwCollapsiblePair['mwCollapsibles'], 'mwCollapsibles'); } return mwCollapsiblePair['mwToggles']; } function mwCollapsiblesDefine( mwCollapsiblePairClass ) { if (!(mwCollapsiblePairClass in mwShowHidePairs)) { mwShowHidePairs[mwCollapsiblePairClass] = {}; } mwCollapsiblePair = mwShowHidePairs[mwCollapsiblePairClass]; if ('mwCollapsibles' in mwCollapsiblePair) { return mwCollapsiblePair['mwCollapsibles']; } mwCollapsiblePair['mwCollapsibles'] = document.getElementsByClassName(mwCollapsiblePairClass+' mw-collapsible'); if ('mwToggles' in mwCollapsiblePair) { mwAssertIdglakArrays(mwCollapsiblePair['mwCollapsibles'], 'mwCollapsibles', mwShowHidePairs[mwCollapsiblePairClass]['mwToggles'], 'mwToggles'); } return mwCollapsiblePair['mwCollapsibles']; }

/* * Add onclick events to DOM objects for showing/hiding all collapsible tables. * The show/hide DOM objects specify what class to do a match with the other two sets of DOM objects. * Different show/hide classes allow for multiple Show All / Hide All buttons if necessary. */ function mwShowHideAddOnclick( mwSHAClass, doCollapse ) { // Get all of the elements matching mwSHAClass to accept the onclick function var mwSHAArr = document.getElementsByClassName(mwSHAClass);

// there should only be one, but just being sure for ( var i = 0; i < mwSHAArr.length; i++ ) { var a = mwSHAArr[i];

// Remove the mwSHAClass from an array copy of the classes of a   // a.class is always empty var classesArr = jQuery.grep(a.className.split(' '), function(value) {     return value != mwSHAClass;    }); var mwCollapsiblePairClass = classesArr[0];

var mwCollapsibles = mwCollapsiblesDefine(mwCollapsiblePairClass); var mwToggles = mwTogglesDefine(mwCollapsiblePairClass);

// This is the class that is assigned to each entity that Media Wiki collapsed. var mwCollapsedClass = 'mw-collapsed';

// Add the show all / hide all closure to list of functions // to call during the onclick event of the show all / hide all button a.onclick = function(e) { // Prevent redirect on click!!! e.preventDefault;

// Act like a user clicking the toggling elements to      // appropriately show or hide their collapsible entities for (var i=0;i<mwToggles.length;i++) { var isCollapsed = $(mwCollapsibles[i]).hasClass(mwCollapsedClass);

if ((isCollapsed || doCollapse) &&           !(isCollapsed && doCollapse)) { mwToggles[i].click; }     }

// Prevent redirect on click!!! return false; }; } }

// Attach all Javascript-enable functionality to DOM objects after the page loads. window.onload = function { // Add onclick events to 'mw-showall' and 'mw-hideall' DOM objects mwShowHideAddOnclick('mw-showall', false); mwShowHideAddOnclick('mw-hideall', true); };