User:Hagindaz/seccollapse.js

/** * seccollapse script * * Collapses sections with headings which contain an element with * class="seccollapse". * * For example: * * == A section == * This text will be collapsed until the above heading is clicked. * * == Another section == * This text will collapse when the heading is clicked (but loads uncollapsed). * *  *     This text ends the previous section, and will not be collapsed. * *  * This program is available under CC0 1.0, viewable at * http://creativecommons.org/publicdomain/zero/1.0/. */ (function(mw, $, d) {	'use strict';	mw.loader.using('mediawiki.util', function { $(d).ready(function {			mw.util.addCSS( '.seccollapse, .seccollapse-start-open { cursor: pointer; padding-left: 17px; background: url(//upload.wikimedia.org/wikipedia/commons/thumb/f/f1/MediaWiki_Vector_skin_action_arrow.svg/17px-MediaWiki_Vector_skin_action_arrow.svg.png) left center no-repeat; }' + '.seccollapse-collapsed { color: #0645ad ! important; background: url(//upload.wikimedia.org/wikipedia/commons/thumb/0/03/MediaWiki_Vector_skin_right_arrow.svg/17px-MediaWiki_Vector_skin_right_arrow.svg.png) left center no-repeat; }' + '.seccollapse-collapsed:hover { text-decoration: underline; }' );			$('.seccollapse, .seccollapse-start-open').click(function toggleSectionVisibility { var $btn = $(this), $header = $($btn.parents(':header')), $body = $header.next('div.seccollapse-wrapper').length ? $header.next : $($header.nextUntil(headersList($header.get[0].tagName) + ', .seccollapse-exclude')).wrapAll(' ').parent; /**				 * Returns a list of header tag names up to the level of tagName. * 				 * headerList('h2') => 'h2' * headerList('h3') => 'h2, h3' * headerList('h4') => 'h2, h3, h4' */				function headersList(tagName) { var tags = [], i;					for (i = 2; i <= Number(tagName.charAt(1)); i++) { tags.push('h' + i); }					return tags.join(', '); }				$btn.toggleClass('seccollapse-collapsed'); $body.slideToggle(250); });			$('.seccollapse')				.not($(d.getElementById(d.location.hash.substring(1))).find('.seccollapse'))				.click;		}); }); })(mediaWiki, jQuery, document);