Jump to content

User:Flatscan/showCCI.js

From Wikipedia, the free encyclopedia
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
// works with Template:CCI item

const hideCaption = "hide";
const showCaption = "show";

const CCI_item_hide_class = "CCI_item_hide";
 
function showCCI_toggleCCIItem(itemIndex)
{
    var itm = document.getElementById("cciItem" + itemIndex);
    var button = document.getElementById("cciButton" + itemIndex);

    if (!itm || !button) {
        return false;
    }

    if (itm.style.display == "none") {
        itm.style.display = "inline";
        button.firstChild.data = hideCaption;
    } else {
        itm.style.display = "none";
        button.firstChild.data = showCaption;
    }
}

function showCCI_createToggleButtons()
{
    var itemIndex = 0;
    var spans = document.getElementsByTagName("span");

    for (var i = 0; i < spans.length; i++) {
        if (!$(spans[i]).hasClass(CCI_item_hide_class)) { continue; }

        var itm = spans[i];
        itm.setAttribute("id", "cciItem" + itemIndex);

        var button     = document.createElement("span");
        var buttonLink = document.createElement("a");
        caption = itm.style.display == "none" ? showCaption : hideCaption;
        var buttonText = document.createTextNode(caption);

        buttonLink.setAttribute("id", "cciButton" + itemIndex);
        buttonLink.setAttribute("href", "javascript:showCCI_toggleCCIItem(" + itemIndex + ");");
        buttonLink.appendChild(buttonText);

        button.appendChild(document.createTextNode("["));
        button.appendChild(buttonLink);
        button.appendChild(document.createTextNode("]"));

        itm.parentNode.insertBefore(button, itm.nextSibling);
        itemIndex++;
    }
}

$(showCCI_createToggleButtons);