MediaWiki:ItemIterator.js

Aus Wikiversity

Hinweis: Leere nach dem Veröffentlichen den Browser-Cache, um die Änderungen sehen zu können.

  • Firefox/Safari: Umschalttaste drücken und gleichzeitig Aktualisieren anklicken oder entweder Strg+F5 oder Strg+R (⌘+R auf dem Mac) drücken
  • Google Chrome: Umschalttaste+Strg+R (⌘+Umschalttaste+R auf dem Mac) drücken
  • Internet Explorer/Edge: Strg+F5 drücken oder Strg drücken und gleichzeitig Aktualisieren anklicken
  • Opera: Strg+F5
// 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 = document.getElementsByClassName( '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;
	},
 
	// private 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;
	},
 
	// private method.
	// define selection's type
	_selectionType: function() {
		var type = document.getElementById( 'IterationType' );
		return ( type ) ? type.className : null;
	},
 
	// private 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 );
	}
};
 
$(document).ready( ItemIterator.init );