Navigation Placeholder

Archive

Show more

Compact Labels In Contempo...

I had a need for a more compact Label gadget that would operate in a similar fashion to the existing Label gadget, but, would allow more granular control over which labels were on display all the time and which labels would be hidden behind a collapsible section. The existing Label gadget can do most of that already, but, I needed a bit more. Although the existing Label gadget allows you to choose which labels to show in the gadget itself, it doesn't allow you to pick which labels are outside the collapsible section and which labels are inside.

Since I don't want to use a sidebar, I needed a small selection of labels visible in the footer and a different selection of other labels hidden inside a collapsible section, but, I wanted to pick and choose which labels are outside and which are inside. In addition, I wanted certain labels not shown at all. I use a handful of labels to indicate which posts have graphs, collapsible sections, posts where comments are locked and other backend maintenance flags that I find useful, but, don't necessarily need wide exposure in my footer.

For my first task, I needed to make the labels to physically take up less space and that meant removing some of the formatting. With the formatting removed, I was able to reduce the size of the labels footprint, using my labels, by half. And, that was with the full list of my labels and no collapsible section. The second task was to pick and choose which labels to be outside and which labels to put inside a collapsible section. My solution is to have two Label gadgets, one for the outside labels and one for the inside labels and a script to combine them into one.

The script reads the existing Label gadget(s) in the order in which they occur, starting with the page body, then the footer and finally the sidebar. The first Label gadget it finds will be the outside labels with the formatting removed and separated by dots. If there is a second Label gadget, those labels will be in a collapsible section. Any Label gadget(s) after that will not be touched. The script needs to be placed after the Label gadget(s). For the Contempo and Essential templates, the bottom of the sidebar is usually the best place. In my case, since I'm not using a sidebar, my script is at the bottom of the page in the footer, but, still after the Label gadgets. With nine gadgets, I have a lot crammed into my footer.

Copy the following code to an HTML/JavaScript gadget and place it somewhere after the Label gadget(s):
<script type="text/javascript">

function Do_Labels() {
var d=document;
var labelBox=d.createElement("div");
var altLabelBox=d.createElement("div");
altLabelBox.id="dvMoreLabels";
var labelDivs=d.getElementsByClassName("widget Label");
if ((new String(labelDivs)!="null") && (new String(labelDivs)!="undefined")){
if (labelDivs.length>0){
var labelH3s=labelDivs[0].getElementsByTagName("h3");
var labelH3="";
if ((new String(labelH3s)!="null") && (new String(labelH3s)!="undefined")){labelH3=labelH3s[0].innerText;}
var maxLabelContainers=labelDivs.length;
if (maxLabelContainers>2){maxLabelContainers=2;}
for (a=0;a<maxLabelContainers;a++){
const allLabels=labelDivs[a].getElementsByTagName("a");
for (l=0;l<allLabels.length;l++){
var aLabel=d.createElement("a");
aLabel.href=allLabels[l].href;
var whatText=allLabels[l].innerHTML;
var whatSplit=whatText.split("<");
if (whatSplit.length>1) {
aLabel.innerText=whatSplit[0];
var aSpan=allLabels[l].getElementsByTagName("span");
if (aSpan.length>0) {aLabel.innerText+=" (" + aSpan[0].innerHTML + ")";}
} else 
{aLabel.innerHTML=allLabels[l].innerHTML;}
if (a==0) {labelBox.appendChild(aLabel);} else {altLabelBox.appendChild(aLabel);}
var lDot=d.createElement("span");
lDot.innerHTML="&nbsp;&nbsp;&#x25CF;&nbsp;&nbsp;";
var labelPlusDot=d.createElement("span");
labelPlusDot.className="dontwrap";
labelPlusDot.appendChild(aLabel);
if (l!=allLabels.length-1){labelPlusDot.appendChild(lDot);}
if (a==0) {labelBox.appendChild(labelPlusDot);} else {altLabelBox.appendChild(labelPlusDot);}
}
if (a==1) {
var chk = d.createElement("input");
chk.type="checkbox";
chk.id="chkMoreLabels";
chk.name="chkMoreLabels";
chk.title="Show/Hide More Labels";
labelBox.appendChild(chk);
var lbl=d.createElement("label");
lbl.id="lblMoreLabels";
lbl.setAttribute("for", "chkMoreLabels");
lbl.title="Show/Hide More Labels";
lbl.innerHTML="<span>&plusb;</span><span>&minusb;</span>";
labelBox.appendChild(lbl);
}
if (a>0) {labelDivs[a].className="widget Label dontshow";}
}
if (labelDivs.length>1){labelBox.appendChild(altLabelBox);}
if (labelH3!="") {labelH3='<h3 class="title">'+labelH3+'</h3>';}
labelDivs[0].innerHTML=labelH3+'<div id="dvLabels" class="widget-content">'+labelBox.innerHTML+'<div class="dontshow">'+labelDivs[0].innerHTML+'</div></div>';
}}}//function

Do_Labels();
</script>
Add the following CSS code to your Theme > Customize > Advanced > Add CSS menu:
#lblMoreLabels {
/* Place custom toggle formatting here: such as font-size or color */

/* Leave these lines */
margin-left:15px;
cursor:pointer;
}
#dvMoreLabels {
/* Place custom label formatting here: such as font-size or color */

}
#dvMoreLabels, #chkMoreLabels, .dontshow {display:none;}
#lblMoreLabels>span:first-child {display:inline;}
#lblMoreLabels>span:nth-child(2) {display:none;}
#chkMoreLabels:checked ~ #dvMoreLabels {display:block;}
#chkMoreLabels:checked ~ #lblMoreLabels>span:first-child {display:none;}
#chkMoreLabels:checked ~ #lblMoreLabels>span:nth-child(2) {display:inline;}
That's all I have for today. Enjoy.

Comments

  1. How to center header on all pages. Give css, if possible

    ReplyDelete
    Replies
    1. A bit of CSS is all that you need to center headers. Details

      Delete

Post a Comment

Note Anonymous posts are allowed, but, all comments are subject to moderation by the content manager. Comments may be rejected, edited or deleted at the sole discretion of the content manager.