User:Prime.mover/common.js

From ProofWiki
Jump to navigation Jump to search

Note: After publishing, you may have to bypass your browser's cache to see the changes.

  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
  • Internet Explorer / Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5
  • Opera: Press Ctrl-F5.
var hasClass = (function () {
    var reCache = {};
    return function (element, className) {
        return (reCache[className] ? reCache[className] : (reCache[className] = new RegExp("(?:\\s|^)" + className + "(?:\\s|$)"))).test(element.className);
    };
})();

/* Process the collapsible subproof environments 
 *
 * Description: Allow technical lemmata to have an included, but initially collapsed proof on the page itself.
 *
 * Author/Maintainer: Lord_Farin
 */
function toggleSubproof( index ){
	var Link = document.getElementById( "subprooflink" + index );
	var Content = document.getElementById( "subproofcontent" + index);
	if(Content.style.display == 'none'){
		Content.style.display = 'block';
		Link.innerHTML = 'hide';
	}
	else{
		Content.style.display = 'none';
		Link.innerHTML = 'show';
	}
}

function processSubproofs(){
	var Divs = document.getElementsByTagName( "div" );
	var subproofCount = 1;

	for( var i = 0 ; i < Divs.length ; i++ ){
		var Subproof = Divs[i];

		/* Filter out the actual subproofs */
		if( !hasClass( Divs[i], "subproof" ) ){
			continue;
		}

		/* Extract the Header and Content divs, and the LinkHolder span. Abort if they aren't present */
		var SubDivs = Subproof.getElementsByTagName( "div" );
		var Header = null;
		var Content = null;
		for ( var j = 0; j < SubDivs.length ; j++ ){
			if( !Header && hasClass( SubDivs[j], "subproofheader" )){
				Header = SubDivs[j];
			}
			if( !Content && hasClass( SubDivs[j], "subproofcontent" )){
				Content = SubDivs[j];
			}
		}
		if( !Header || !Content ){
			continue;
		}

		var HeaderSpans = Header.getElementsByTagName( "span" );
		var LinkHolder;
		for ( var j = 0; j < HeaderSpans.length ; j++ ){
			if( hasClass( HeaderSpans[j], "linkholder" )){
				LinkHolder = HeaderSpans[j];
				break;
			}
		}
		if( !LinkHolder ){
			continue;
		}

		/* Add a unique identifier to this particular subproof */
		Subproof.setAttribute( "id", "subproof" + subproofCount);
		Header.setAttribute( "id", "subproofheader" + subproofCount);
		Content.setAttribute( "id", "subproofcontent" + subproofCount);
		LinkHolder.setAttribute( "id", "subprooflinkholder" + subproofCount);
		LinkHolder.innerHTML = '(<a id="subprooflink' + subproofCount + '" onclick="toggleSubproof('+subproofCount+')"></a>)';
		
		/* Bring this subproof to its initial state */
		Content.style.display = 'none';
		document.getElementById( "subprooflink" + subproofCount).innerHTML = 'show';
		subproofCount++;
	}
}

jQuery( processSubproofs );