User:Wiegels/slideshow.js

From Wikimedia Commons, the free media repository
Jump to navigation Jump to search
Note: After saving, you have to bypass your browser's cache to see the changes. Internet Explorer: press Ctrl-F5, Mozilla: hold down Shift while clicking Reload (or press Ctrl-Shift-R), Opera/Konqueror: press F5, Safari: hold down Shift + Alt while clicking Reload, Chrome: hold down Shift while clicking Reload.
/*
 * slideshow.js
 * extension of gallery slideshow gadget on Commons for WLE/WLM Germany jurors
 *
 * https://commons.wikimedia.org/wiki/Help:Gadget-GallerySlideshow
 * https://commons.wikimedia.org/wiki/MediaWiki:Gadget-GallerySlideshow.js
 * https://commons.wikimedia.org/wiki/MediaWiki:GallerySlideshow.js
 */
if ($('.gallery[title="Jury"]').length>0) { // 2020-09-30
    var boxes = $('.gallery > li.gallerybox');
    var numbers = function(index, format) {
        var data = [
            (index + 1) + '.',
            boxes.eq(index).find('.gallerytext').text().trim(),
            Math.round(index / boxes.length * 100) + ' %'
        ];
        return (format ?
            '<span style="color:#555; font-size:1.5em;">' + data[0] +
            '<span style="color:#666; font-size:2em; padding:0 .5ex;">' + data[1] + '</span>' +
            data[2] + '</span>' :
            data.join('\n')
        );
    }
    var placeholder = (mw.config.get('wgPageName').includes('WLE') ?
        '85/WLE_Austria_Logo_(no_text).svg/120' :
        'f3/LUSITANA_WLM_2011_d.svg/103');
    boxes.filter(':has(a.new)').each(function() {
        var description = $(this).find('.gallerytext').text().trim();
        $(this).find('.thumb > span').html(
            '<img alt="'+description+'" src="https://upload.wikimedia.org/wikipedia/commons/thumb/' +
            placeholder.replace(/^(((.).)\/(.+)\/\d+)$/, '$3/$1px-$2.png') +
            '" decoding="async" class="mw-file-element" height="120">'
        );
    });
    $(document)
        .on('slideshow', function(event, command, object) {
            /// console.debug(command, object);
            if (command == 'codeLoaded') {
                object.findImageSize = function(height, width) {
                    object.actualMaxSize = { w: 1870, h: 1066 }; // for resolution 1920 * 1200
                };
            }
            else if (command == 'beforeQuery' || command == 'afterQuery' || command == 'shown') {
                if (object.$thumbsUl) {
                    object.$thumbsUl.eq(0).find('img').each(function(index, element) {
                        $(this).attr('title', numbers(index, false).replace(/<[^>]+>/g, ''));
                    });
                }
                if (command == 'shown') {
                    object.maxImageHeight = $(window).height() - object.$thumbsUl.height() - object.$controlsContainer.height();
                    object.maxImageWidth = $(window).width() - 50;
                    object.$thumbsUl.eq(0).css('width', (boxes.length * 81) + 'px');
                    object.$controlsContainer.prepend('<div id="jw-index" style="float:left; margin-top:8px;"></div>');
                    object.$captionContainer.hide().css('min-width', '0').css('width', '0')
                        .find('.image-caption').css('padding', '0').css('width', '0');
                    var tbody = $('#GallerySlideHelpSplash tbody');
                    if (tbody) {
                        var tr = function(key, description) {
                            return tbody.children().last().clone().prop('outerHTML')
                                .replace(/>[^<]+(<\/kbd>)/, '>' + key + '$1')
                                .replace(/>[^<]+(<\/td>)/, '>' + description + '$1');
                        }
                        tbody.prepend(tr('?', 'Tasten-Navigation ein-/ausblenden'));
                        tbody.find('tr:eq(1) td + td').text('nächstes Bild anzeigen');
                        tbody.find('tr:eq(2) td + td').text('vorheriges Bild anzeigen');
                        $(Array.from(Array(10).keys())).each(function(index) {
                            tbody.find('tr:eq(-3)').before(tr(index, 'Bild nach ' + (index * 10) + ' % anzeigen'));
                        });
                        tbody.find('tr:eq(-3)').prop('outerHTML', tr('Ende', 'letztes Bild anzeigen'));
                        tbody.find('tr:eq(-2)').prop('outerHTML', tr('Komma', 'abspielen/pausieren'));
                    }
                }
            }
            else if (command == 'newSlide') {
                $('#jw-index').html(numbers(object.preloadStartIndex, true));
            }
        })
        .on('keyup', function(event) {
            if ($('#SlideContainer').is(':visible')) {
                var key = (event.which % 48).toString();
                if (key.match(/^\d$/)) { // digits
                    window.GallerySlide.gotoIndex(Math.floor(boxes.length / 10 * key));
                }
                else if (event.which == 63) {
                    $('#GallerySlideHelpSplash').toggle();
                }
                else if ([108, 188].includes(event.which)) { // comma
                    window.GallerySlide.toggleSlideshow();
                }
            }
        });
}