MediaWiki:ItemIterator.js

Aus Wikiversity

Wechseln zu: Navigation, Suche

Hinweis - Leere nach dem Speichern den Browser-Cache, um die Änderungen sehen zu können: Mozilla/Firefox/Safari: Shift gedrückt halten und auf Aktualisieren klicken oder alternativ entweder Strg-F5 oder Strg-R (Befehlstaste-R bei Macintosh) drücken; Konqueror: Auf Aktualisieren klicken oder F5 drücken; Opera: Cache unter Extras → Einstellungen leeren; Internet Explorer: Strg-F5 drücken oder Strg gedrückt halten und dabei Aktualisieren anklicken.

//<pre>
// Iterator for selection of items, i.e. elements, that are children of some div with class="iterate"
// Description: Allows items (i.e. div, ul, ol, dl) to be iterated in some way.
// 		The type of iteration is "random" except otherwise stated by an object with
// 		id="IterationType" and with class="serial". In that case you get a deterministic iteration.
// 		First selection starts by calling the page. Further selection is made by some button
// Maintainers: User:Exxu (de.wikiversity.org)
 
var ItemIterator = {
 
	// private properties:
	_bLabel : 'Weiter',	// Button's label
	_oElem : new Array,	// collection of objects to be selected
	_lastIndex : -1, 	// last index of visible object
	_tagNames : new Array( 'DIV', 'UL', 'OL', 'DL' ),	// types of objects to be selected
	_type : '',	// type of selection: random or serial
 
	// public method. Begin process.
	// Process starts only, if "bodyContent" contains at least on div with class="iterate".
	// The immediate children of that div get collected and filtered.
	// For filtration the tag's name should be one of _tagNames.
	init : function() {
		var items = getElementsByClassName(document,"div","iterate");
		if (items.length>0){
			var divs = items[0].childNodes;
			for( var i=0; i<divs.length; i++){
				if(divs[i].nodeType == 1){
					divs[i].style.display = 'none';
					for (var j = 0; j < ItemIterator._tagNames.length; j++){
						if (divs[i].nodeName == ItemIterator._tagNames[j]){
							ItemIterator._oElem.push(divs[i]);
							ItemIterator._lastIndex = ItemIterator._oElem.length-1;
							break;
						}
					}
				}
			}
			// create a button to get more selections
			var bElem = ItemIterator._selectionButton();
			div = document.getElementById("IterationType");
			if (div){
				div.appendChild(bElem);
			}else{
				div = document.createElement("div");
				div.className="random";
				div.id = "IterationType";
				div.appendChild(bElem);
				items[0].parentNode.insertBefore(div,items[0].nextSibling);
			}
			ItemIterator._type = ItemIterator._selectionType();
			bElem.click();
			items[0].style.display='block';
		}
		items=document.getElementById("ItemIteratorWarning");
		if (items){
			items.style.display = 'none';
		}
		return false;
	},
 
	// privat method. 
	// create a button for selection
	_selectionButton : function(){
		var bElem = document.createElement("input");
		bElem.setAttribute("type","button");
		bElem.setAttribute("class","iterationbutton");
		bElem.setAttribute("title",ItemIterator._bLabel);
		bElem.setAttribute("value",ItemIterator._bLabel);
		bElem.onclick = ItemIterator._select;
		return bElem;
	},
 
	// privat method. 
	// define selection's type
	_selectionType : function() { 
		var type = document.getElementById("IterationType");
		return (type) ? type.className : null;
	},
 
	// privat method. 
	// select items
	_select : function() {
		if (ItemIterator._lastIndex >= 0 ){
			ItemIterator._oElem[ItemIterator._lastIndex].style.display = 'none';
			ItemIterator._lastIndex = ItemIterator._index();
			ItemIterator._oElem[ItemIterator._lastIndex].style.display = 'block';
		}
		return false;
	},
 
	// private method. Get next index.
	_index : function() {
 
		return (ItemIterator._type && ItemIterator._type == 'serial') ?
			(ItemIterator._lastIndex+1) % ItemIterator._oElem.length :
			Math.floor(Math.random() * ItemIterator._oElem.length);
	}
 
};
addOnloadHook( ItemIterator.init );
//</pre>
Persönliche Werkzeuge