Jump to content

User:Polygnotus/Scripts/MySubpages.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.
$.when( mw.loader.using( ['mediawiki.util'] ), $.ready ).done( function() {
    var userName = mw.config.get( 'wgUserName' );
    var $portletLink = $( mw.util.addPortletLink(
        'p-personal',
        mw.util.getUrl( 'Special:PrefixIndex/User:' + userName + '/' ),
        'Subpages',
        'pt-mysubpages',
        'Show your subpages',
        null,
        '#pt-preferences'
    ) );

    // Fire the hook so Navigation Popups/Popups process the new link
    if ( $portletLink.length ) {
        mw.hook( 'wikipage.content' ).fire( $portletLink );
    }

    mw.util.addCSS(
        '#pt-userspace-search input.userspace-no-results {' +
        '  outline: 2px solid red;' +
        '  transition: outline-color 1.5s ease;' +
        '}' +
        '#pt-userspace-search input.userspace-no-results-fade {' +
        '  outline: 2px solid transparent;' +
        '  transition: outline-color 1.5s ease;' +
        '}'
    );

    var $li   = $( '<li>' ).attr( 'id', 'pt-userspace-search' );
    var $icon = $( '<a>' )
        .attr( 'title', 'Search in your userspace' )
        .attr( 'href', '#' )
        .attr( 'role', 'button' )
        .css( 'textDecoration', 'none' )
        .text( '🔍' );

    var $input = $( '<input>' )
        .attr( 'type', 'text' )
        .attr( 'placeholder', 'Search userspace…' )
        .css( {
            display    : 'none',
            width      : '140px',
            marginLeft : '4px',
            font       : 'inherit',
            fontSize   : '0.85em'
        } );

    var fadeTimer = null;
    function showNoResults() {
        if ( fadeTimer ) { clearTimeout( fadeTimer ); }
        $input.removeClass( 'userspace-no-results-fade' ).addClass( 'userspace-no-results' );
        fadeTimer = setTimeout( function() {
            $input.removeClass( 'userspace-no-results' ).addClass( 'userspace-no-results-fade' );
            setTimeout( function() {
                $input.removeClass( 'userspace-no-results-fade' );
            }, 1500 );
        }, 600 );
    }

    $icon.on( 'click', function( e ) {
        e.preventDefault();
        $input.toggle();
        if ( $input.is( ':visible' ) ) {
            $input.trigger( 'focus' );
        }
    } );

    $input.on( 'keydown', function( e ) {
        if ( e.key === 'Enter' ) {
            var query = $.trim( $( this ).val() );
            if ( !query ) { return; }
            var searchString = query + ' prefix:"User:' + userName + '"';
            $.getJSON( mw.config.get( 'wgScriptPath' ) + '/api.php', {
                action     : 'query',
                list       : 'search',
                srsearch   : searchString,
                srnamespace: 2,
                srlimit    : 1,
                srinfo     : 'totalhits',
                format     : 'json'
            } ).done( function( data ) {
                var hits = data &&
                           data.query &&
                           data.query.searchinfo &&
                           data.query.searchinfo.totalhits;
                if ( hits > 0 ) {
                    window.location.href =
                        mw.config.get( 'wgScript' ) +
                        '?title=Special%3ASearch' +
                        '&search=' + encodeURIComponent( searchString ) +
                        '&ns2=1' +
                        '&profile=advanced' +
                        '&fulltext=1';
                } else {
                    showNoResults();
                }
            } ).fail( function() {
                window.location.href =
                    mw.config.get( 'wgScript' ) +
                    '?title=Special%3ASearch' +
                    '&search=' + encodeURIComponent( searchString ) +
                    '&ns2=1' +
                    '&profile=advanced' +
                    '&fulltext=1';
            } );
        } else if ( e.key === 'Escape' ) {
            $input.val( '' ).hide();
        }
    } );

    $( document ).on( 'click', function( e ) {
        if ( !$li.is( e.target ) && $li.has( e.target ).length === 0 ) {
            $input.val( '' ).hide();
        }
    } );

    $li.append( $icon, $input );
    $( '#pt-mysubpages' ).after( $li );
} );