Jump to content

User:Enterprisey/search-links.js

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Enterprisey (talk | contribs) at 01:08, 13 October 2019 (hey it works! (breaks, like, everything else though)). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
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.
// vim: ts=4 sw=4 et ai
// <nowiki>
$.when(
    $.ready,
    mw.loader.load( [ "mediawiki.util" ] )
).then( function () {

    // The main setup function that inserts links into the suggestions.
    function insertSearchLinks() {
        try {
            if( $( ".suggestions-results" ).children().first().hasClass( "suggestion-container" ) ) {
                return;
            }
            $( ".suggestions-results" ).children()
                .wrap( "<div class='suggestion-container'></div>" )
                .after( function () {
                    try {
                        return $( "<a>" )
                            .addClass( "suggestion-link" )
                            .attr( "href", mw.util.getUrl( $( this ).text(),
                                { action: "edit" } ) )
                            .text( "(edit)" );
                    } catch (e) {
                        console.error(e);
                    }
                } )
                .each(function(idx,e){e.textContent = e.textContent})

                $( ".suggestion-link" ).on( "click", function () {
                    window.location.href = $( this ).attr( "href" );
                } );

            $( ".suggestion-container" ).on( "mouseover", function () {
                $( this ).addClass( "suggestions-result-current" );
            } );

            $( ".suggestion-container" ).on( "mouseout", function () {
                $( this ).removeClass( "suggestions-result-current" );
            } );
        } catch (e) {
            console.error(e);
        }
    }

    function isLink(el) {
        return el && el.nodeType === 1 && el.tagName.toLowerCase() === "a";
    }

    window.addEventListener( "blur", function (e) {
        if( isLink(e.explicitOriginalTarget) || isLink(e.explicitOriginalTarget.parentNode) ) {
            e.stopPropagation();
            e.stopImmediatePropagation();
            return false;
        }
    },true );

//    Object.keys(window).forEach(key => {if (/^on/.test(key)) {
//        if(["ontransitionend","onkeyup","onkeydown","oninput","onkeypress","onblur","onmousemove","onpointermove"].includes(key))return;
//        if(key.includes("transition"))return;
//        window.addEventListener(key.slice(2), event => {console.log("false",key.slice(2),event);
//        event.stopPropagation();
//        event.stopImmediatePropagation();
//        return false;
//        });
//        window.addEventListener(key.slice(2), event => {console.log("true",key.slice(2),event);
//        event.stopPropagation();
//        event.stopImmediatePropagation();
//        return false;
//        },true);
//    }
//    });

    // Wire up the handler for searching
    //var CHECK_INTERVAL = 50; // milliseconds
    $( "#searchInput" ).on( "input", function () {
        var newVal = $( this ).val();
        var suggestionsEl = $( ".suggestions" ).first();
                setTimeout( insertSearchLinks, 250 );
        
        //var checkForSearchUpdate = function () {
        //    if( suggestionsEl.find( ".special-query" ).text() === newVal ) {
        //        console.log(newVal);
        //        setTimeout( insertSearchLinks, 10 );
        //    } else {
        //        setTimeout( checkForSearchUpdate, CHECK_INTERVAL );
        //    }
        //};

        //checkForSearchUpdate();
    } );

    mw.loader.addStyleTag( ".suggestions-result { display: inline; }" +
        ".suggestion-link { z-index: 1; float: right; line-height: 1.5; color: blue; }" +
        ".suggestion-container:hover .suggestion-link { color: white; }" +
        ".suggestion-container { position: relative; }" +
        ".suggestions-results .mw-searchSuggest-link { content: ''; z-index: 0;"+
                " left: 0; top: 0; right: 0; bottom: 0; display:inline-block; "+
                "width: 70%; height: 100%; line-height: 1.5; padding: 0.01em 0.25em }"+
        ".suggestions-result-current * { color: white; }");
} );

// </nowiki>