User:Technical 13/Scripts/Gadget-closeRfC.js

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>
* Script to help close discussions listed at [[WP:ANRFC]]
*/
$( document ).ready( function () {
        var initiatedSection = {}, initiatedSections = [];
        function createSections() {
                $( 'div#mw-content-text' ).find( 'h3' ).each( function( i ) {
                        $( this ).nextUntil( 'h3' ).wrapAll( '<div id="section_' + i + '" class="initiated">' );
                        var thisH3Header = $( this );
                        $( this ).remove();
                        $( 'div#section_' + i ).prepend( thisH3Header );
                } );
        }
 
        function createSectionArray() {
                $( 'span.wpRfC' ).each( function() {
                        var initiatedRaw = $( this ).attr( 'class' ).match( /.*?([\d]*)_([\w]*).*/ );
                        var initiatedType = initiatedRaw[ 2 ];
                        switch ( initiatedType ) {
                                case 'tban' :
                                        var initiatedModifier = 10;
                                        break;
                                case 'afd' :
                                case 'cfd' :
                                case 'ffd' :
                                case 'mfd' :
                                case 'rfd' :
                                case 'tfd' :
                                case 'drv' :
                                case 'mrv' :
                                case 'rmv' :
                                case 'xfd' :
                                        var initiatedModifier = ( 30 / 7 );
                                        break;
                                default :
                                        var initiatedModifier = 1;
                                        break;
                        }
                        var initiatedAgo = initiatedRaw[ 1 ];
                        var initiatedSorter = Math.floor( initiatedAgo * initiatedModifier );
//						console.info( 'Raw: %s :Type: %s :Mod: %s :Ago: %d :Sorter: %d', initiatedRaw, initiatedType, initiatedModifier, initiatedAgo, initiatedSorter );
                        initiatedSection = { Sorter: initiatedSorter, content: $( this ).parents( 'div.initiated' ).html() };
                        initiatedSections[ initiatedSections.length ] = initiatedSection;
						$( this ).parents( 'div.initiated' ).remove();
                } );
        }
 
        // Create a deferred object
        var dfd = $.Deferred();
 
        dfd.done(
                createSections,
                createSectionArray
        ).done( function() {
                initiatedSections.sort( function ( a, b ) {
					var v = ( a.Sorter > b.Sorter ) ? 1 : ( a.Sorter < b.Sorter ) ? -1 : 0;
					return v;
				} ).reverse();
				var newContent = [];
				$.each( initiatedSections, function() {
					newContent[ newContent.length ] = $( this )[ 0 ].content;
				} );
				var uniqueSections = [];
				$.each( newContent, function( i, iS ) {
					if ( $.inArray( iS, uniqueSections ) === -1 ) {
						uniqueSections.push( iS );
					}
				} );
				$( 'div#mw-content-text' ).append( uniqueSections.join( '\n' ) );
//				createSections();
// Need to sort the TOC as well

        } );
 
        dfd.resolve();
} );
//</nowiki>