User:The Transhumanist/ViewAsOutline-CategoryTree.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.
// <syntaxhighlight lang="javascript">

/*

(Script under development - not yet functional)

ViewAsOutline-CategoryTree.js = Convert CategoryTree into outline on screen, ready for copying/pasting

What it does: it's a utility for viewing a CategoryTree as an outline in wikicode (as you would see it in an editor)

When completed,
this script will provide 4 menu items to affect a displayed category tree: one for expanding, one for collapsing, 
one for turning wikicodification on, and another for turning wikicodification off. Wikicodification means adding 
the heading delimiters and list item wikicoding that you would see in an editor (even though we're not in an editor),
for easy copying/pasting into an editor.

Brief comments are provided within the source code below. For extensive explanatory 
notes on what the source code does and how it works, see the Script's workshop on 
the talk page (not ready yet).

*/

// ============== Set up ==============

// Start off with a bodyguard function to reserve the aliases mw and $
( function ( mw, $ ) {

    // we can now rely on mw and $ within the safety of our “bodyguard” function, to mean 
    // "mediawiki" and "jQuery", respectively

    // ============== ready() event listener/handler ==============
    // below is jQuery short-hand for $(document).ready(function() { ... });
    // it makes the rest of the script wait until the page's DOM is loaded and ready
    $(function() {
        
		// ============== activation filters ==============
        // Only activate on Vector skin
        if ( mw.config.get( 'skin' ) === 'vector' ) {

	        // Run this script only if "Category tree " is in the page title
			if (document.title.indexOf("Category tree ") != -1) {

				// End of set up
			
        	    // =================== Prep work =====================
				
				// Variable declarations, etc., go here

            	// ================= Core program ================= 

                $( function() {
                    VAOCTExpand();
                    VAOCTCollapse();
                    VAOCTWikicodify();
                    VAOCTDeWikicodify();
				} );
			}
        }

        // ======================== Subroutines ===========================
        // Functions (aka subroutines) are activated only when they are called.
    	// Below are the functions called in the core control structure of the program above.
    	// They are placed at the end of the program, so that the script's flow 
    	// is easier to follow.

       	// ============ Function to expand unexpanded categories in the tree ==============

       	function VAOCTExpand() {
			// Create linked menu item
			var portletlink = mw.util.addPortletLink('p-tb', '#', 'CatTree expand', 'ct-expand', 'Expand all categories');
			// Bind click handler
			$(portletlink).click( function(e) {
				e.preventDefault();     // prevents any default action -- we want only the following actions to run:

				// Do some stuff when clicked...
				//$('.CategoryTreeToggle[data-ct-state="collapsed"]').click();
				$('.CategoryTreeToggle[title="expand"]').click();

			});     // end of function(e) (click handler)
       	}

       	// ============ Function to collapse expanded categories in the tree ==============

       	function VAOCTCollapse() {
			// Create linked menu item
			var portletlink = mw.util.addPortletLink('p-tb', '#', 'CatTree collapse', 'ct-collapse', 'Collapse all categories');
			// Bind click handler
			$(portletlink).click( function(e) {
				e.preventDefault();     // prevents any default action -- we want only the following actions to run:

				// Do some stuff when clicked...
				$('.CategoryTreeToggle[title="collapse"]').click();

			});     // end of function(e) (click handler)
       	}

       	// ============ Function to insert/show list wikicode on-screen for links to articles ==============

       	function VAOCTWikicodify() {
			// Create linked menu item
			var portletlink = mw.util.addPortletLink('p-tb', '#', 'CT Wikicodify', 'ct-wiki', 'Add list wikicode');
			// Bind click handler
			$(portletlink).click( function(e) {
				e.preventDefault();     // prevents any default action -- we want only the following actions to run:

				// Do some stuff when clicked...
				// Add brackets enclosed in classed spans, except for entries that already have them.
				var cont = document.getElementById('mw-content-text');
            	// wrap the page names in spanned wikicodes, and add Brackets class to prevent matching same entries again
            	cont.outerHTML = cont.outerHTML.replace(/(<a class=\")(CategoryTreeLabel  CategoryTreeLabelNs0.*?<\/a>)/g,'<span class="OpeningBrackets">* [[</span>$1Brackets $2<span class="ClosingBrackets">]]</span>');				
				// Show hidden brackets
				$('.OpeningBrackets').show();
				$('.ClosingBrackets').show();

			});     // end of function(e) (click handler)
       	}

       	// ============ Function to hide list wikicode ==============

       	function VAOCTDeWikicodify() {
			// Create linked menu item
			var portletlink = mw.util.addPortletLink('p-tb', '#', 'CT Dewikicodify', 'ct-dewiki', 'Remove list wikicode');
			// Bind click handler
			$(portletlink).click( function(e) {
				e.preventDefault();     // prevents any default action -- we want only the following actions to run:

				// Do some stuff when clicked...
				//$('.CategoryTreeSection').find('.CategoryTreeLabelPage').parent().hide();
				$('.OpeningBrackets').hide();
				$('.ClosingBrackets').hide();

			});     // end of function(e) (click handler)
       	}

    } );
}( mediaWiki, jQuery ) );

// </syntaxhighlight>