User:Bawolff/onebox-select-2.js

/* This is a rewrite of the DynamicNavbox script

Its based on the older DynamicNav script that has been passed around various wikipedia's.

It has no purpose as the other script works fine, except that I'm trying to further my limited understanding of js and writing random stuff will probably help me (I hope anyways).

p.s. if you find this useful in anyway, like all my edits I claim no copyright over it, however it might be considered a derivative of DynamicNav stuff at http://en.wikinews.org/w/index.php?title=MediaWiki:Common.js&oldid=480557 which comes from who knows where.



/* Opt is an object containing


 * barHideText = string showing text to press to hide bar (Optional)
 * barShowText = opposite (optional)
 * autoHideAmount = number of boxs on a page before all bars on page start off hidden. (Optional; Defaults to one)
 * autoClose = boolean Close open boxes after opening another (default false)
 * classes = array - needed clases to be recognized as a nav box (default NavFrame)
 * notClasses = array of classes to ignore potential nav boxes (recommend leave blank
 * var = the variable name that function constructed (REQUIRED - Specify this or else)

function DynamicNav { opt = arguments[0] || { }

this.barHideText = opt.barHideText || '[ ↑ ]'; this.barShowText = opt.barShowText || '[ ↓ ]'; this.autoHideAmount = opt.autoHideAmount || 1; this.autoClose = opt.autoClose || false; this.classes = opt.classes || ['NavFrame']; this.notClasses = opt.notClasses || [];

//may want to translate following to your lang for easier css coding, however it doesn't really matter as user won't see this.navToggleClass = "NavToggle"; this.navHead = /\bNavHead\b/i;

//internal stuff this.idNumb = (DynamicNav.numb ? DynamicNav.numb + 1 : DynamicNav.numb = 0, 1); //So multiple types of Nav boxes don't conflict. DynamicNav.numb++; this.objName = opt.var || opt.objName || null;//Must be a better way to do that. for javascript: links on toggle buttons this.toString = function { return "[object DynamicNavagation]" };

}

DynamicNav.prototype.initBoxes = function {

//First look at every div and see if it is a box var barNumber;

if (!this.objName) throw new Error("Specify 'var' when configuring the DynamicNavBox script"); //If the value isn't set, function fails

mainloop: for (var i=0;currentDiv = document.getElementsByTagName("div")[i];i++) { //if it is a nav box var class for (var j=0;j < this.classes.length;j++) { //If it doesn't have req. classes or has a not class, goto next div class = new RegExp ('\b' + this.classes[j] + '\b', 'i'); if (!class.test(currentDiv.classname)) { continue mainloop; }  }  for (var j=0;j < this.notClasses.length;j++) { class = new RegExp ('\b' + this.not+ LOCAL_indexNavigationBarClasses[j] + '\b', 'i'); if (class.test(currentDiv.classname)) { continue mainloop; }  }  barNumber++; //we found one var navToggle = document.createElement("a"); navToggle.className = this.navToggleClass; navToggle.setAttribute('id', 'NavToggle' + this.idNumb + barNumber); navToggle.setAttribute('href', 'javascript:' + this.objName + 'toggle(' + barNumber + ');'); navToggleText = document.createTextNode(this.barHideText); navToggle.appendChild(navToggleText); delete navToggleText; // Find the NavHead and attach the toggle link (Must be this complicated because Moz's firstChild handling is borked) for(j=0;j < currentDiv.childNodes.length; j++) { if (this.navHead.test(currentDiv.childNodes[j].className)) { //if one of the classes is NavHead (case-insensitive) currentDiv.childNodes[j].appendChild(navToggle); } }  currentDiv.setAttribute('id', 'NavFrame' + this.idNumb + barNumber); }//mainloop end if (barNumber > this.autoHideAmount) { for (k=1; k <= barNumber; k++) { this.toggle(k); } } currentDiv = null; //I just like nuillifying random things }

DynamicNav.prototype.toggle = function(barNumber) { var navToggle = document.getElementById("NavToggle" + this.idNumb + barNumber); var navFrame = document.getElementById("NavFrame" + this.idNumb + barNumber);

if (!navToggle || !navFrame) { return }

//If box currently shown if (navToggle.firstChild.data == this.barHideText) { var hideBar = function (navFrame, navToggle) { for (var navChild = navFrame.firstChild; navChild != null; navChild = navChild.nextSibling) { if (navChild.className == 'NavPic') { navChild.style.display = 'none'; }   if (navChild.className == 'NavContent') { navChild.style.display = 'none'; }  }   navToggle.firstChild.data = this.barShowText; }//func end hideBar(navFrame, navToggle); } //If the box is currently collasped else if (navToggle.firstChild.data = this.barShowText) { if (this.autoClose) { var navToggle_f, navFrame_f; for (var f = 1; f < 75; f++) { //f < 75 is to prevent possible accidental indef loop navToggle_f = document.getElementById("NavToggle" + this.idNumb + f); navFrame_f = document.getElementById("NavFrame" + this.idNumb + f); hideBar(navFrame_f, navToggle_f); }  delete navFrane_f; //I just like deleting things delete navToggle_f; } //Open selected for (var navChild = navFrame.firstChild; navChild != null; navChild = navChild.nextSibling) { if (navChild.className == 'NavPic') { navChild.style.display = 'block'; }  if (navChild.className == 'NavContent') { navChild.style.display = 'block'; } }  navToggle.firstChild.data = this.barHideText; } }//end toggle