MediaWiki:Toggler.js

From Gallowpedia, the MediEvil Wiki. You'll be dying to read!

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)
  • Internet Explorer / Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5
  • Opera: Press Ctrl-F5.
// <syntax type="javascript">

    /** 
        Toggles the display of elements on a page 
        Author/contact: Austin Che http://openwetware.org/wiki/User:Austin_J._Che
        See http://openwetware.org/wiki/OpenWetWare:Toggle for examples and documentation
     */

// indexed array of toggler ids to array of associated toggle operations
// each operation is a two element array, the first being the type, the second a class name or array of elements
// operation types are strings like "_reset" or "" for the default toggle operation
var togglers = [];   
var togglerStates = {};  
var allClasses = {}; // associative map of class names to page elements

function toggler(id)
{
    var toBeToggled = togglers[id];
    if (!toBeToggled)
        return;

    // if some element is in list more than once, it will be toggled multiple times
    for (var i = 0; i < toBeToggled.length; i++)
    {
        // get array of elements to operate on
        var toggles = toBeToggled[i][1];
        if (typeof(toggles) == "string")
        {
            toggles = allClasses[toggles];
        }
        if (!toggles || !toggles.length)
            continue;

        var op = toBeToggled[i][0]; // what the operation will be

        switch (op)
        {
            case "_reset":
                for (var j = 0; j < toggles.length; j++)
                    toggles[j].style.display = toggles[j]._toggle_original_display;
                break;
            case "_show":
                for (j = 0; j < toggles.length; j++)
                    toggles[j].style.display = '';
                break;
            case "_hide":
                for (j = 0; j < toggles.length; j++)
                    toggles[j].style.display = 'none';
                break;
            case "":
            default:
                // Toggle
                var toggleName = toBeToggled[i][1];
                togglerStates[toggleName] = !togglerStates[toggleName];
                for (j = 0; j < toggles.length; j++) {
                      if (isToggler(toggles[j]))
                           toggles[j].style.display = ((toggles[j].style.display == 'none') ? '' : 'none');
                      else if (togglerStates[toggleName] || !togglerStates[toggleName] && areAllTogglersOff(toggles[j]))
                           toggles[j].style.display = (togglerStates[toggleName] ? toggles[j]._toggle_original_display : opposite(toggles[j]._toggle_original_display));
                }
                break;
        }
    }
}

function opposite(display) 
{
    if (display === 'none')
       return ''
    return 'none'
}

function isToggler(elem)
{
    var elemClasses = elem.className.split(' ');
    for (var j = 0; j < elemClasses.length; j++)
    {
         var elemClass = elemClasses[j];
         if (elemClass.substring(0, 8) == "_toggler")
            return true;
    }
    return false;
}

function areAllTogglersOff(elem)
{
    for (var i = 0; i < togglers.length; i++)
    {
      if (allClasses[togglers[i][0][1]].includes(elem) && togglerStates[togglers[i][0][1]] === true)
      {
        return false;
      }
		}
	return true;
}

function createTogglerLink(toggler, id)
{
    var toggle = document.createElement("a");
    toggle.className = 'toggler-link';
    toggle.setAttribute('id', 'toggler' + id);
    toggle.setAttribute('href', 'javascript:toggler("' + id + '");');
    var child = toggler.firstChild;
    toggler.removeChild(child);
    toggle.appendChild(child);
    toggler.insertBefore(toggle, toggler.firstChild);
}

function toggleInit()
{
    var togglerElems = [];

    // initialize/clear any old information
    togglers = [];   
		togglerStates = {};
    allClasses = {};
        
    // make list of all document classes
    var elems = document.getElementsByTagName("*");
    for (var i = 0; i < elems.length; i++)
    {
        var elem = elems[i];
        if (!elem.className)
            continue;

        elem._toggle_original_display = elem.style.display;
        var togglerID = -1;
		var togglerState = true;
        var elemClasses = elem.className.split(' '); // get list of classes
        for (var j = 0; j < elemClasses.length; j++)
        {
            var elemClass = elemClasses[j];
			
			// all the special classes begin with _toggle
            if (elemClass.substring(0, 7) != "_toggle")
                continue;

            if (elemClass.substring(0, 12) == "_toggle_init")
            {
                // set initial value for display (ignore the original CSS set value)
                // understands _toggle_initshow and _toggle_inithide
                var disp = elemClass.substring(12);
                if (disp == "show")
                    elem.style.display = '';
                else if (disp == "hide")
                    elem.style.display = 'none';
                elem._toggle_original_display = disp;
            }
            else if (elemClass.substring(0, 8) == "_toggler")
            {
                if (togglerID == -1)
                {
                    togglerID = togglers.length;
                    togglers[togglerID] = [];
                    togglerElems[togglerID] = elem;
                }

                // all classes are of form _toggler_op-CLASS
                // figure out what class we're toggling
                // if none is specified, then we use the current toggle group
                var toBeToggled;
                var hyphen = elemClass.indexOf('-');
                
								if (hyphen != -1)
                    toBeToggled = elemClass.substring(hyphen+1);

                var op = elemClass.substring(8, hyphen);
				
                var collection = document.getElementsByClassName(toBeToggled);
                for (var a=[], k=collection.length; k;) a[--k] = collection[k];
                
                if (!allClasses[toBeToggled])
                  	allClasses[toBeToggled] = a;
           
                togglers[togglerID].push(new Array(op, toBeToggled));
                
                if (!togglerStates[toBeToggled])
                  	togglerStates[toBeToggled] = togglerState;
            }
        }
    }

    // add javascript links to all toggler elements
    for (i = 0; i < togglerElems.length; i++)
        createTogglerLink(togglerElems[i], i);
}

$( toggleInit );
// </syntax>