MediaWiki:Common.js: Difference between revisions

From Linguifex
Jump to navigation Jump to search
Created page with "/** Collapsible tables ********************************************************* * * Description: Allows tables to be collapsed, showing only the header. See * ..."
 
No edit summary
 
(20 intermediate revisions by 3 users not shown)
Line 1: Line 1:
/** Collapsible tables *********************************************************
// Gadgets
*
 
  *  Description: Allows tables to be collapsed, showing only the header. See
//syntax highlighter
*                        http://www.mediawiki.org/wiki/Manual:Collapsible_tables.
  mw.loader.load('//www.mediawiki.org/w/index.php?title=MediaWiki:Gadget-DotsSyntaxHighlighter.js&action=raw&ctype=text/javascript');  
*  Maintainers: [[en:User:R. Koot]]
 
*/
/* For Favicon  */
jQuery("<link>", {
var autoCollapse = 2;
rel: "shortcut icon",
var collapseCaption = 'hide';
type: "image/x-icon",
var expandCaption = 'show';
href: "https://linguifex.com/assets/Linguifex4v1.png"
}).appendTo("head");  
function collapseTable( tableIndex ) {
 
        var Button = document.getElementById( 'collapseButton' + tableIndex );
        var Table = document.getElementById( 'collapsibleTable' + tableIndex );
        if ( !Table || !Button ) {
                return false;
        }
        var Rows = Table.rows;
        if ( Button.firstChild.data == collapseCaption ) {
                for ( var i = 1; i < Rows.length; i++ ) {
                        Rows[i].style.display = 'none';
                }
                Button.firstChild.data = expandCaption;
        } else {
                for ( var i = 1; i < Rows.length; i++ ) {
                        Rows[i].style.display = Rows[0].style.display;
                }
                Button.firstChild.data = collapseCaption;
        }
}
function createCollapseButtons() {
        var tableIndex = 0;
        var NavigationBoxes = new Object();
        var Tables = document.getElementsByTagName( 'table' );
        for ( var i = 0; i < Tables.length; i++ ) {
                if ( hasClass( Tables[i], 'collapsible' ) ) {
                        /* only add button and increment count if there is a header row to work with */
                        var HeaderRow = Tables[i].getElementsByTagName( 'tr' )[0];
                        if ( !HeaderRow ) {
                                continue;
                        }
                        var Header = HeaderRow.getElementsByTagName( 'th' )[0];
                        if ( !Header ) {
                                continue;
                        }
                        NavigationBoxes[tableIndex] = Tables[i];
                        Tables[i].setAttribute( 'id', 'collapsibleTable' + tableIndex );
                        var Button = document.createElement( 'span' );
                        var ButtonLink = document.createElement( 'a' );
                        var ButtonText = document.createTextNode( collapseCaption );
                        Button.className = 'collapseButton'; // Styles are declared in [[MediaWiki:Common.css]]
                        ButtonLink.style.color = Header.style.color;
                        ButtonLink.setAttribute( 'id', 'collapseButton' + tableIndex );
                        ButtonLink.setAttribute( 'href', "javascript:collapseTable(" + tableIndex + ");" );
                        ButtonLink.appendChild( ButtonText );
                        Button.appendChild( document.createTextNode( '[' ) );
                        Button.appendChild( ButtonLink );
                        Button.appendChild( document.createTextNode( ']' ) );
                        Header.insertBefore( Button, Header.childNodes[0] );
                        tableIndex++;
                }
        }
        for ( var i = 0;  i < tableIndex; i++ ) {
                if ( hasClass( NavigationBoxes[i], 'collapsed' ) || ( tableIndex >= autoCollapse && hasClass( NavigationBoxes[i], 'autocollapse' ) ) ) {
                        collapseTable( i );
                } else if ( hasClass( NavigationBoxes[i], 'innercollapse' ) ) {
                        var element = NavigationBoxes[i];
                        while ( element = element.parentNode ) {
                                if ( hasClass( element, 'outercollapse' ) ) {
                                        collapseTable( i );
                                        break;
                                }
                        }
                }
        }
}
addOnloadHook( createCollapseButtons );
/** Test if an element has a certain class **************************************
/** Test if an element has a certain class **************************************
  *
  *
Line 104: Line 24:
})();
})();


/* For Favicon  */
mw.loader.using( [ 'mediawiki.util' ] ).done( function () {
document.write("<link rel='shortcut icon' href='http://conlang.wikkii.com/w/images/conlang/uploads/2/26/Favicon.png' type='image/x-icon'>");
/* Begin of mw.loader.using callback */
/**
* Collapsible tables; reimplemented with mw-collapsible
* Styling is also in place to avoid FOUC
*
* Allows tables to be collapsed, showing only the header. See [[Help:Collapsing]].
* @version 3.0.0 (2018-05-20)
* @source https://www.mediawiki.org/wiki/MediaWiki:Gadget-collapsibleTables.js
* @author [[User:R. Koot]]
* @author [[User:Krinkle]]
* @author [[User:TheDJ]]
* @deprecated Since MediaWiki 1.20: Use class="mw-collapsible" instead which
* is supported in MediaWiki core. Shimmable since MediaWiki 1.32
*
* @param {jQuery} $content
*/
function makeCollapsibleMwCollapsible( $content ) {
var $tables = $content
.find( 'table.collapsible:not(.mw-collapsible)' )
.addClass( 'mw-collapsible' );
 
$.each( $tables, function ( index, table ) {
// mw.log.warn( 'This page is using the deprecated class collapsible. Please replace it with mw-collapsible.');
if ( $( table ).hasClass( 'collapsed' ) ) {
$( table ).addClass( 'mw-collapsed' );
// mw.log.warn( 'This page is using the deprecated class collapsed. Please replace it with mw-collapsed.');
}
} );
if ( $tables.length > 0 ) {
mw.loader.using( 'jquery.makeCollapsible' ).then( function () {
$tables.makeCollapsible();
} );
}
}
mw.hook( 'wikipage.content' ).add( makeCollapsibleMwCollapsible );
 
/**
* Add support to mw-collapsible for autocollapse, innercollapse and outercollapse
*
* Maintainers: TheDJ
*/
function mwCollapsibleSetup( $collapsibleContent ) {
var $element,
$toggle,
autoCollapseThreshold = 2;
$.each( $collapsibleContent, function ( index, element ) {
$element = $( element );
if ( $element.hasClass( 'collapsible' ) ) {
$element.find( 'tr:first > th:first' ).prepend( $element.find( 'tr:first > * > .mw-collapsible-toggle' ) );
}
if ( $collapsibleContent.length >= autoCollapseThreshold && $element.hasClass( 'autocollapse' ) ) {
$element.data( 'mw-collapsible' ).collapse();
} else if ( $element.hasClass( 'innercollapse' ) ) {
if ( $element.parents( '.outercollapse' ).length > 0 ) {
$element.data( 'mw-collapsible' ).collapse();
}
}
// because of colored backgrounds, style the link in the text color
// to ensure accessible contrast
$toggle = $element.find( '.mw-collapsible-toggle' );
if ( $toggle.length ) {
// Make the toggle inherit text color (Updated for T333357 2023-04-29)
if ( $toggle.parent()[ 0 ].style.color ) {
$toggle.css( 'color', 'inherit' );
$toggle.find( '.mw-collapsible-text' ).css( 'color', 'inherit' );
}
}
} );
}
 
mw.hook( 'wikipage.collapsibleContent' ).add( mwCollapsibleSetup );
});
 
(function addAutoCatIIFE () {
"use strict";
 
if (mw.config.get("wgNamespaceNumber") !== 14)
return;
 
const autoCatEditSummary = typeof window.autoCatEditSummary == "undefined"
? false : window.autoCatEditSummary;
 
const $textBox = $("#wpTextbox1");
const $editSummaryBox = $("#wpSummary");
 
function fillAutoCat(autoSave) {
$textBox.val("{{auto cat}}");
if ( autoCatEditSummary )
$editSummaryBox.val("added [[T:auto cat]] with [[User:Erutuon/scripts/addAutoCat.js|JavaScript]]");
$(autoSave ? "#wpSave" : "#wpPreview").click();
}
const action = mw.config.get("wgAction");
 
mw.loader.using("oojs-ui").done(() => {
if (mw.config.get("wgCurRevisionId") === 0) { // new page
$(() => {
const buttons = [];
if (action === "edit") {
const button1 = new OO.ui.ButtonWidget({
label: "Add auto cat and save",
});
const button2 = new OO.ui.ButtonWidget({
label: "or preview",
});
button1.$element
.click(() => fillAutoCat(true))
.attr("title", "Create this category using topic cat");
button2.$element
.click(() => fillAutoCat(false))
.attr("title", "Create this category using topic cat");
buttons.push(button1.$element);
buttons.push(button2.$element);
} else if (action === "view") {
const button = new OO.ui.ButtonWidget({
label: "Add auto cat and edit",
});
button.$element.click(() => {
location.href = new mw.Uri(location.href).extend({
action: "edit",
preloadtext: "{{autocat}}",
}).toString();
});
buttons.push(button.$element);
}
if (buttons.length > 0) {
const div = $("<div>").append(buttons);
$("#firstHeading").after(div);
}
});
} else if (action === "edit" && !$textBox.val().includes("auto cat")) {
$(() => {
const button = new OO.ui.ButtonWidget({
label: "Replace with autocat",
id: "add-autocat",
});
button.$element.click(() => $textBox.val("{{auto cat}}"));
$("#editform").prepend(button.$element);
});
}
});
 
})();
 
mw.loader.load('//www.mediawiki.org/w/index.php?title=MediaWiki:UpdateLanguageNameAndCode.js&action=raw&ctype=text/javascript');
 
 
// </nowiki>

Latest revision as of 13:05, 21 April 2026

// Gadgets

//syntax highlighter
 mw.loader.load('//www.mediawiki.org/w/index.php?title=MediaWiki:Gadget-DotsSyntaxHighlighter.js&action=raw&ctype=text/javascript'); 

/* For Favicon  */
jQuery("<link>", {
	rel: "shortcut icon",
	type: "image/x-icon",
	href: "https://linguifex.com/assets/Linguifex4v1.png"
}).appendTo("head"); 

/** Test if an element has a certain class **************************************
 *
 * Description: Uses regular expressions and caching for better performance.
 * Maintainers: [[User:Mike Dillon]], [[User:R. Koot]], [[User:SG]]
 */
 
var hasClass = ( function() {
        var reCache = {};
        return function( element, className ) {
                return ( reCache[className] ? reCache[className] : ( reCache[className] = new RegExp( "(?:\\s|^)" + className + "(?:\\s|$)" ) ) ).test( element.className );
        };
})();

mw.loader.using( [ 'mediawiki.util' ] ).done( function () {
	/* Begin of mw.loader.using callback */
/**
	 * Collapsible tables; reimplemented with mw-collapsible
	 * Styling is also in place to avoid FOUC
	 *
	 * Allows tables to be collapsed, showing only the header. See [[Help:Collapsing]].
	 * @version 3.0.0 (2018-05-20)
	 * @source https://www.mediawiki.org/wiki/MediaWiki:Gadget-collapsibleTables.js
	 * @author [[User:R. Koot]]
	 * @author [[User:Krinkle]]
	 * @author [[User:TheDJ]]
	 * @deprecated Since MediaWiki 1.20: Use class="mw-collapsible" instead which
	 * is supported in MediaWiki core. Shimmable since MediaWiki 1.32
	 *
	 * @param {jQuery} $content
	 */
	function makeCollapsibleMwCollapsible( $content ) {
		var $tables = $content
			.find( 'table.collapsible:not(.mw-collapsible)' )
			.addClass( 'mw-collapsible' );

		$.each( $tables, function ( index, table ) {
			// mw.log.warn( 'This page is using the deprecated class collapsible. Please replace it with mw-collapsible.');
			if ( $( table ).hasClass( 'collapsed' ) ) {
				$( table ).addClass( 'mw-collapsed' );
				// mw.log.warn( 'This page is using the deprecated class collapsed. Please replace it with mw-collapsed.');
			}
		} );
		if ( $tables.length > 0 ) {
			mw.loader.using( 'jquery.makeCollapsible' ).then( function () {
				$tables.makeCollapsible();
			} );
		}
	}
	mw.hook( 'wikipage.content' ).add( makeCollapsibleMwCollapsible );

	/**
	 * Add support to mw-collapsible for autocollapse, innercollapse and outercollapse
	 *
	 * Maintainers: TheDJ
	 */
	function mwCollapsibleSetup( $collapsibleContent ) {
		var $element,
			$toggle,
			autoCollapseThreshold = 2;
		$.each( $collapsibleContent, function ( index, element ) {
			$element = $( element );
			if ( $element.hasClass( 'collapsible' ) ) {
				$element.find( 'tr:first > th:first' ).prepend( $element.find( 'tr:first > * > .mw-collapsible-toggle' ) );
			}
			if ( $collapsibleContent.length >= autoCollapseThreshold && $element.hasClass( 'autocollapse' ) ) {
				$element.data( 'mw-collapsible' ).collapse();
			} else if ( $element.hasClass( 'innercollapse' ) ) {
				if ( $element.parents( '.outercollapse' ).length > 0 ) {
					$element.data( 'mw-collapsible' ).collapse();
				}
			}
			// because of colored backgrounds, style the link in the text color
			// to ensure accessible contrast
			$toggle = $element.find( '.mw-collapsible-toggle' );
			if ( $toggle.length ) {
				// Make the toggle inherit text color (Updated for T333357 2023-04-29)
				if ( $toggle.parent()[ 0 ].style.color ) {
					$toggle.css( 'color', 'inherit' );
					$toggle.find( '.mw-collapsible-text' ).css( 'color', 'inherit' );
				}
			}
		} );
	}

	mw.hook( 'wikipage.collapsibleContent' ).add( mwCollapsibleSetup );
});

(function addAutoCatIIFE () {
"use strict";

if (mw.config.get("wgNamespaceNumber") !== 14)
	return;

const autoCatEditSummary = typeof window.autoCatEditSummary == "undefined"
	? false : window.autoCatEditSummary;

const $textBox = $("#wpTextbox1");
const $editSummaryBox = $("#wpSummary");

function fillAutoCat(autoSave) {
	$textBox.val("{{auto cat}}");
	if ( autoCatEditSummary )
		$editSummaryBox.val("added [[T:auto cat]] with [[User:Erutuon/scripts/addAutoCat.js|JavaScript]]");
	
	$(autoSave ? "#wpSave" : "#wpPreview").click();
}
	
const action = mw.config.get("wgAction");

mw.loader.using("oojs-ui").done(() => {
	if (mw.config.get("wgCurRevisionId") === 0) { // new page
		$(() => {
			const buttons = [];
			if (action === "edit") {
				const button1 = new OO.ui.ButtonWidget({
					label: "Add auto cat and save",
				});
				const button2 = new OO.ui.ButtonWidget({
					label: "or preview",
				});
				
				button1.$element
					.click(() => fillAutoCat(true))
					.attr("title", "Create this category using topic cat");
				button2.$element
					.click(() => fillAutoCat(false))
					.attr("title", "Create this category using topic cat");
				
				buttons.push(button1.$element);
				buttons.push(button2.$element);
			} else if (action === "view") {
				const button = new OO.ui.ButtonWidget({
					label: "Add auto cat and edit",
				});
				
				button.$element.click(() => {
					location.href = new mw.Uri(location.href).extend({
						action: "edit",
						preloadtext: "{{autocat}}",
					}).toString();
				});
				
				buttons.push(button.$element);
			}
			
			if (buttons.length > 0) {
				const div = $("<div>").append(buttons);
				
				$("#firstHeading").after(div);
			}
		});
	} else if (action === "edit" && !$textBox.val().includes("auto cat")) {
		$(() => {
			const button = new OO.ui.ButtonWidget({
				label: "Replace with autocat",
				id: "add-autocat",
			});
			button.$element.click(() => $textBox.val("{{auto cat}}"));
			
			$("#editform").prepend(button.$element);
		});
	}
});

})();

 mw.loader.load('//www.mediawiki.org/w/index.php?title=MediaWiki:UpdateLanguageNameAndCode.js&action=raw&ctype=text/javascript'); 


// </nowiki>