Jump to content

User:GhostInTheMachine/FoldSections.js

From Wikipedia, the free encyclopedia
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
// Adds an arrow to sections to make them collapsible
// Cloned from [[User:BrandonXLF/CollapseSections]]

mw.hook('wikipage.content').add(function(content) {
	if (mw.config.get('skin') === 'minerva') return;

	mw.loader.load(['mediawiki.ui.icon', 'oojs-ui.styles.icons-movement']);
	mw.util.addCSS('[class*="hide-sec"]{display:none!important}');

	// repair the contributions list after phab T298638
	mw.util.addCSS('h4.mw-index-pager-list-header { width: inherit; height: inherit; padding-top: 7px; position: relative !important; background-color: cornsilk; }');

	mainpart = $('.mw-parser-output, .mw-pager-body', content);
	mainpart.find(':header').each(function() {
		var level = +this.nodeName[1],
			heading = $(this),
			icon = $('<i class="mw-ui-icon-before mw-ui-icon-small mw-ui-icon mw-ui-icon-expand" style="margin-bottom: 4px;"></i>');

		icon.click(function() {
			var levelMatch = 'h1';
			for (var i = 2; i <= level; i++) levelMatch += ',h' + i + ':has(*)';

			icon.toggleClass('mw-ui-icon-next');
			icon.toggleClass('mw-ui-icon-expand');
			heading.nextUntil(levelMatch).toggleClass('hide-sect-h' + level);
		});

		if (window.collapseSections) icon.click();
		heading.prepend(icon);
	});
});