Jump to content

User:Enterprisey/hover-edit-section.js

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Enterprisey (talk | contribs) at 11:45, 19 October 2018 (forgot to declare a variable). 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.
// <nowiki>
$( function () {
    if( mw.config.get( "wgAction" ) === "view" ) {
        var linkMapping = {}; // zone IDs to link elements

        function initMouseListeners() {
            var editSectionSpans = document.querySelectorAll( "span.mw-editsection" );
            var headerEl, currEl, container;
            for( var i = 0; i < editSectionSpans.length; i++ ) {
                
                // Walk up tree to header element
                headerEl = editSectionSpans[i].parentNode;
                if( headerEl.tagName.toLowerCase() === "h1" ) continue;

                // Insert container into DOM
                container = document.createElement( "div" );
                container.className = "hover-edit-section";
                container.dataset.hoverId = i;
                headerEl.parentNode.insertBefore( container, headerEl );

                var currEl = headerEl.nextSibling;
                container.appendChild( headerEl );
                var nextEl;

                while( currEl && ( currEl.nodeType !== 1 || currEl.tagName.toLowerCase().indexOf( "h" ) !== 0 ) ) {
                    nextEl = currEl.nextSibling;
                    container.appendChild( currEl );
                    currEl = nextEl;
                }

                // Keep track of which "zone" the mouse is over
                container.addEventListener( "mouseover", function () {
                    window.hoverEditSectionZone = this.dataset.hoverId;
                } );

                // Register "edit section" link
                linkMapping[i] = editSectionSpans[i].children[ editSectionSpans[i].children.length - 2 ];
            }

            document.getElementById( "mw-content-text" ).addEventListener( "mouseout", function () {
                window.hoverEditSectionZone = -1;
            } );
        }

        function editCurrentSection( e ) {
            e.preventDefault();
            if( window.hoverEditSectionZone >= 0 ) {
                linkMapping[ window.hoverEditSectionZone ].click();   
            }
            return false;
        }

        mw.loader.using( [ "jquery.chosen", "mediawiki.api", "mediawiki.util", "jquery.accessKeyLabel" ], function () {
            initMouseListeners();
            var portletLink = mw.util.addPortletLink ( 'p-tb', '#',
                'Edit current section', 'pt-edit-curr-sec', 'Edit current section');
            portletLink.addEventListener( "click", editCurrentSection );
            portletLink.accessKey = "d";
            $( portletLink ).updateAccessKeys();
        } );
    }
} );
// </nowiki>