MediaWiki:Common.js: Difference between revisions
Books filters, Still needed checklist, related-media wrap |
Books filters: build UI in JS; match book-kind-* classes |
||
| Line 2,998: | Line 2,998: | ||
(function rbBooksFilters() { | (function rbBooksFilters() { | ||
var $ | var $host = $('.books-filters-host').first(); | ||
if (!$filters.length) return; | var $slides = $('.mw-parser-output .notable-slides').first(); | ||
$filters.on('click', '.books-filter', function () { | if (!$host.length || !$slides.length) return; | ||
var kind = $(this). | if (!$slides.find('.notable-slide-card[class*="book-kind-"]').length) return; | ||
$ | if ($host.find('.books-filters').length) return; | ||
var filters = [ | |||
{ id: 'all', label: 'All' }, | |||
{ id: 'memoir', label: 'Memoirs & letters' }, | |||
{ id: 'history', label: 'History' }, | |||
{ id: 'novel', label: 'Novels' }, | |||
{ id: 'other', label: 'Other' } | |||
]; | |||
var $bar = $('<div class="books-filters" role="group" aria-label="Filter books"></div>'); | |||
filters.forEach(function (f, i) { | |||
var $btn = $('<button type="button" class="books-filter"></button>'); | |||
$btn.attr('data-filter', f.id).text(f.label); | |||
if (i === 0) $btn.addClass('is-active'); | |||
$bar.append($btn); | |||
}); | |||
$host.empty().append($bar); | |||
$bar.on('click', '.books-filter', function () { | |||
var kind = $(this).attr('data-filter'); | |||
$bar.find('.books-filter').removeClass('is-active'); | |||
$(this).addClass('is-active'); | $(this).addClass('is-active'); | ||
$(' | $slides.find('.notable-slide-card').each(function () { | ||
var | var $c = $(this); | ||
var cls = $c.attr('class') || ''; | |||
var m = cls.match(/book-kind-([a-z]+)/); | |||
var k = m ? m[1] : 'other'; | |||
var show = kind === 'all' || k === kind; | var show = kind === 'all' || k === kind; | ||
$ | $c.toggleClass('is-book-hidden', !show); | ||
}); | }); | ||
}); | }); | ||