User:Pointy/monobook.js

// a test script to try to copy the [show] | [hide] feature currently in toc panels

// check the 'initToggle' function is defined and set the 'show' and 'hide' text if it is if (window.initToggle) { var toggleShowText = "show"; var toggleHideText = "hide"; initToggle("testbox"); };

// initialise the toggle box function initToggle(name) { if (document.createTextNode) { // Uses DOM calls to avoid document.write + XHTML issues var title = document.getElementById(name + 'title'); if (!title) { return; }		// this section of code manipulates the dom tree and inserts a new toggle link span that contains the 'show' and 'hide' links. // dom tree before: // title // 		//  ...		// 		// dom tree after // title [%tocHideText%] // 		//  ...		// 		var outerSpan = document.createElement('span'); outerSpan.className = 'togglespan'; var toggleLink = document.createElement('a'); toggleLink.id = name + 'link'; toggleLink.className = 'internal'; toggleLink.href = 'javascript:flipToggle(\'' + name + '\')'; toggleLink.appendChild(document.createTextNode(toggleHideText)); outerSpan.appendChild(document.createTextNode('[')); outerSpan.appendChild(toggleLink); outerSpan.appendChild(document.createTextNode(']')); title.appendChild(document.createTextNode(' ')); title.appendChild(outerSpan); // now we need to check whether the panel should be in 'show' mode or 'hide' mode when the page initialises var cookiePos = document.cookie.indexOf("hide" + name + "="); if (cookiePos > -1 && document.cookie.charAt(cookiePos + ("hide" + name + "=").length) == 1) { flipToggle(name); }	} }

function flipToggle( name ) { // get references to the body content and toggle link for this named panel var toggleBody = document.getElementById(name + 'body'); var toggleLink = document.getElementById(name + 'link'); // check if the panel is in expanded or collapsed mode, then switch the mode and update the cookie if (toggleBody && toggleLink && toggleBody.style.display == 'none') { changeText(toggleLink, toggleHideText); toggleBody.style.display = 'block'; document.cookie = "hide" + name + "=0"; }	else { changeText(toggleLink, toggleShowText); toggleBody.style.display = 'none'; document.cookie = "hide" + name + "=1"; } }

function changeText(el, newText) { // Safari work around if (el.innerText) { el.innerText = newText; }	else if (el.firstChild && el.firstChild.nodeValue) { el.firstChild.nodeValue = newText; } }