User:Jberkel/semhide.js
Note: You may have to bypass your browser’s cache to see the changes. In addition, after saving a sitewide CSS file such as MediaWiki:Common.css, it will take 5-10 minutes before the changes take effect, even if you clear your cache.
- Mozilla / Firefox / Safari: hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (Command-R on a Macintosh);
- Konqueror and Chrome: click Reload or press F5;
- Opera: clear the cache in Tools → Preferences;
- Internet Explorer: hold Ctrl while clicking Refresh, or press Ctrl-F5.
- This script lacks a documentation subpage. Please create it.
- Useful links: root page • root page’s subpages • links • redirects • your own
/* globals $ */
// based on https://en.wiktionary.org/wiki/User:Ungoliant_MMDCCLXIV/synshide.js
(function () {
'use strict';
function setupNyms(index, dlTag) {
// https://en.wiktionary.org/wiki/Wiktionary:Semantic_relations
var relationClasses = [ 'synonym', 'antonym', 'hypernym', 'hyponym', 'meronym',
'holonym', 'troponym', 'coordinate-term' ];
var relations = $(dlTag).find('dd > .nyms').map(function(index, element) {
for (var i=0; i<element.classList.length; i++) {
if (relationClasses.includes(element.classList[i])) {
$(element).data('relationClass', element.classList[i]);
return element;
}
}
return null;
}).get();
function setupToggle(elements, category, visibleByDefault) {
if (elements.length === 0) return null;
var toggler = $('<a>');
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($('<span>')
.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'].includes($(e).data().relationClass);
});
var other = relations.filter(function (e) { return !synonyms.includes(e); });
setupToggle(synonyms, 'synonyms', true /* show by default */);
setupToggle(other, 'semantic relations');
}
$(function() {
// if (mw.config.get('wgNamespaceNumber') === 0) {
$('dl:has(dd > .nyms)').each(setupNyms);
// }
});
})();