User:Erutuon/scripts/semhide.js

// based on User:Jberkel/semhide.js and User:Ungoliant_MMDCCLXIV/synshide.js

/* globals $ */

(function { 'use strict';

function setupNyms(index, dlTag) { // Semantic relations var relationClasses = [ 'synonym', 'antonym', 'hypernym', 'hyponym', 'meronym', 'holonym', 'troponym', 'coordinate-term' ]; var relations = $(dlTag).find('dd > .nyms').get.filter(		function(element) {			return Array.prototype.some.call(element.classList, function (className) { if (relationClasses.indexOf(className) !== -1) { $(element).data('relationClass', className); return true; }			});		});	function setupToggle(elements, category, visibleByDefault) { if (elements.length === 0) return null; var toggler = $(''); var text = elements.map(function (e) {			return $(e).data('relationClass').replace('-', ' ') +				 (($(e).find('a').length > 1) ? 's' : '');		}).join(', '); function show { toggler.html(text + ' ▲'); $(dlTag).show; $(elements).show; }		function hide { toggler.html(text + ' ▼'); if ($(dlTag).children.length === elements.length) { $(dlTag).hide; } else { $(elements).hide; }		}		$(dlTag).before($(' ')		  .addClass('HQToggle')		   .append(toggler)		   .css('margin-left', '5px')); toggler.click(window.VisibilityToggles.register(category, show, hide, visibleByDefault)); }	var synonyms = relations.filter(function (e) {		return ['synonym', 'antonym'].indexOf($(e).data('relationClass')) !== -1;	}); var other = relations.filter(function (e) { return synonyms.indexOf(e) === -1; }); setupToggle(synonyms, 'synonyms', true /* show by default */); setupToggle(other, 'semantic relations'); }

$(function {	// if (mw.config.get('wgNamespaceNumber') === 0) {		$('dl:has(dd > .nyms)').each(setupNyms);	// } });

});