MediaWiki:Common.js: Difference between revisions

From BoyWiki
No edit summary
(Copied from en.wikipedia.org)
 
(6 intermediate revisions by 3 users not shown)
Line 1: Line 1:
/* Any JavaScript here will be loaded for all users on every page load. */
/* Any JavaScript here will be loaded for all users on every page load. */


/**
* Keep code in MediaWiki:Common.js to a minimum as it is unconditionally
* loaded for all users on every wiki page. If possible create a gadget that is
* enabled by default instead of adding it here (since gadgets are fully
* optimized ResourceLoader modules with possibility to add dependencies etc.)
*
* Since Common.js isn't a gadget, there is no place to declare its
* dependencies, so we have to lazy load them with mw.loader.using on demand and
* then execute the rest in the callback. In most cases these dependencies will
* be loaded (or loading) already and the callback will not be delayed. In case a
* dependency hasn't arrived yet it'll make sure those are loaded before this.
*/


/* global mw, $ */
/* jshint strict:false, browser:true */


/* Manage call to function() on submit action before sendind the page  */
mw.loader.using( [ 'mediawiki.util' ] ).done( function () {
/* Begin of mw.loader.using callback */


function initeventpostform() {
/**
  if (wgAction == 'edit' || wgAction == 'submit') {
* Main Page layout fixes
      if (document.forms['editform'] == undefined || document.forms['editform'].elements['wpTextbox1'] == undefined) return;
*
      document.forms['editform'].onsubmit = function() {
* Description: Adds an additional link to the complete list of languages available.
        /* for agora pages: append section title link */
* Should T18962 be completed this can be removed along with associated CSS in common.css
        pgagora_insert_topsection();
* Maintainers: [[User:AzaToth]]
        /* other function */
*/
      }
if ( mw.config.get( 'wgPageName' ) === 'Main_Page' || mw.config.get( 'wgPageName' ) === 'Talk:Main_Page' ) {
  }
$( function () {
}
mw.util.addPortletLink( 'p-lang', '//meta.wikimedia.org/wiki/List_of_Wikipedias',
'Complete list', 'interwiki-completelist', 'Complete list of Wikipedias' );
} );
}


addOnloadHook(initeventpostform);
/**
* Map addPortletLink to mw.util
* @deprecated: Use mw.util.addPortletLink instead.
*/
mw.log.deprecate( window, 'addPortletLink', mw.util.addPortletLink, 'Use mw.util.addPortletLink instead' );


/**
* Test if an element has a certain class
* @deprecated:  Use $(element).hasClass() instead.
*/
mw.log.deprecate( window, 'hasClass', function ( element, className ) {
return $( element ).hasClass( className );
}, 'Use jQuery.hasClass() instead' );


/* Append an Agora link to Navigation menu (if connected) */
/**
* @source www.mediawiki.org/wiki/Snippets/Load_JS_and_CSS_by_URL
* @rev 6
*/
var extraCSS = mw.util.getParamValue( 'withCSS' ),
extraJS = mw.util.getParamValue( 'withJS' );


function addlinkportletmenu() {
if ( extraCSS ) {
  if (wgUserName != null)
if ( extraCSS.match( /^MediaWiki:[^&<>=%#]*\.css$/ ) ) {
  addPortletLink("p-navigation", "https://www.boywiki.org/en/BoyWiki:Agora", "Agora", "n-agora", "A place to discuss the administration and editing of BoyWiki");
mw.loader.load( '/w/index.php?title=' + extraCSS + '&action=raw&ctype=text/css', 'text/css' );
}
} else {
mw.notify( 'Only pages from the MediaWiki namespace are allowed.', { title: 'Invalid withCSS value' } );
}
}


addOnloadHook(addlinkportletmenu);
if ( extraJS ) {
if ( extraJS.match( /^MediaWiki:[^&<>=%#]*\.js$/ ) ) {
mw.loader.load( '/w/index.php?title=' + extraJS + '&action=raw&ctype=text/javascript' );
} else {
mw.notify( 'Only pages from the MediaWiki namespace are allowed.', { title: 'Invalid withJS value' } );
}
}


/**
* WikiMiniAtlas
*
* Description: WikiMiniAtlas is a popup click and drag world map.
*              This script causes all of our coordinate links to display the WikiMiniAtlas popup button.
*              The script itself is located on meta because it is used by many projects.
*              See [[Meta:WikiMiniAtlas]] for more information.
* Note - use of this service is recommended to be replaced with mw:Help:Extension:Kartographer
*/
$( function () {
var requireWikiminiatlas = $( 'a.external.text[href*="geohack"]' ).length || $( 'div.kmldata' ).length;
if ( requireWikiminiatlas ) {
mw.loader.load( '//meta.wikimedia.org/w/index.php?title=MediaWiki:Wikiminiatlas.js&action=raw&ctype=text/javascript' );
}
} );


/* For the 'dialogue' pages attribute the dialog class to body */
/**
/* Used by css to colorize indentation in Agora */
* 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' );


function attrib_class_dialog() {
$.each( $tables, function ( index, table ) {
  /* only for Agora or discussion pages */
// mw.log.warn( 'This page is using the deprecated class collapsible. Please replace it with mw-collapsible.');
  var bodyclass = document.body.className;
if ( $( table ).hasClass( 'collapsed' ) ) {
  if (bodyclass.match(/page-BoyWiki_Agora/)
$( table ).addClass( 'mw-collapsed' );
        || bodyclass.match(/page-Discussion_/)
// mw.log.warn( 'This page is using the deprecated class collapsed. Please replace it with mw-collapsed.');
        || bodyclass.match(/page-Discuss_/))
}
    document.body.className = bodyclass + ' dialog';
} );
}
if ( $tables.length > 0 ) {
mw.loader.using( 'jquery.makeCollapsible' ).then( function () {
$tables.makeCollapsible();
} );
}
}
mw.hook( 'wikipage.content' ).add( makeCollapsibleMwCollapsible );


addOnloadHook(attrib_class_dialog);
/**
* 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
if ( $toggle.parent()[ 0 ].style.color ) {
$toggle.find( 'a' ).css( 'color', 'inherit' );
}
}
} );
}


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


/* Agora : if new sub-page created append a title section link (called by submit) */
/**
* Magic editintros ****************************************************
*
* Description: Adds editintros on disambiguation pages and BLP pages.
* Maintainers: [[User:RockMFR]]
*
* @param {string} name
*/
function addEditIntro( name ) {
$( '.mw-editsection, #ca-edit, #ca-ve-edit' ).find( 'a' ).each( function ( i, el ) {
el.href = $( this ).attr( 'href' ) + '&editintro=' + name;
} );
}


function pgagora_insert_topsection() {
if ( mw.config.get( 'wgNamespaceNumber' ) === 0 ) {
  /* only if agora new page and write permission */
$( function () {
  if (wgAction != 'edit' && wgAction != 'submit') return true;
if ( document.getElementById( 'disambigbox' ) ) {
  if (wgUserGroups == null || wgUserGroups.join(" ").match(/user|sysop|bureaucrat/) == null) return true;
addEditIntro( 'Template:Disambig_editintro' );
  if (wgPageName == null || wgPageName.match(/^BoyWiki:Agora\//) == null) return true;
}
  if (wgTitle == null) return true;
} );
  if (wgArticleId != 0) return true;
  if (document.forms['editform'].elements['wpSummary'] == undefined) return true;
  if (document.getElementById('wpSummaryLabel').firstChild.firstChild.data.match(/Subject.*\/.*headline/) == null) return true;
 
  /* retrieve text */
  var content = document.forms['editform'].elements['wpTextbox1'].value;
  /* if title already present */
  if (content.match(/^\n?=\[\[[^\]\/]*Agora\/[^\]]*\]\]=/)) return true;
  /* otherwise copy subject sub-title */
  var sujet = document.forms['editform'].elements['wpSummary'].value;
  document.forms['editform'].elements['wpSummary'].value = "";
  if (sujet.length) sujet = "=="+sujet+"==\n";
  /* and insert title + subject at beginning of page */
  var titre = "=[[BoyWiki:"+wgTitle+"|"+wgTitle+"]]=\n";
  document.forms['editform'].elements['wpTextbox1'].value =  titre + sujet + content;
  return true;
}


$( function () {
var cats = mw.config.get( 'wgCategories' );
if ( !cats ) {
return;
}
if ( $.inArray( 'Living people', cats ) !== -1 || $.inArray( 'Possibly living people', cats ) !== -1 ) {
addEditIntro( 'Template:BLP_editintro' );
}
} );
}


/* La première lecture du jour de la page Accueil de la veille
/* Actions specific to the edit page */
/* provoque son rafraichissement par purge du cache */
if ( mw.config.get( 'wgAction' ) === 'edit' || mw.config.get( 'wgAction' ) === 'submit' ) {
/**
* Fix edit summary prompt for undo
*
*  Fixes the fact that the undo function combined with the "no edit summary prompter"
*  complains about missing editsummary, if leaving the edit summary unchanged.
*  Added by [[User:Deskana]], code by [[User:Tra]].
*  See also [[phab:T10912]].
*/
$( function () {
if ( document.location.search.indexOf( 'undo=' ) !== -1 && document.getElementsByName( 'wpAutoSummary' )[ 0 ] ) {
document.getElementsByName( 'wpAutoSummary' )[ 0 ].value = '1';
}
} );
}


function refresh_Image_of_the_day() {
/* End of mw.loader.using callback */
  // seulement pour page Main_Page(tout utilisateur a visiblement le droit de puger le cache)
} );
  if (wgPageName != 'Main_Page') return;
/* DO NOT ADD CODE BELOW THIS LINE */
  if (wgAction != 'view') return;
 
  // récup date dernière mise en cache de la page
  var datecache = null;
  var nodes = document.getElementById('bodyContent').childNodes;
  for (var i=0; i < nodes.length ; i++) {
      if (nodes[i].nodeName == '#comment' && nodes[i].data.match(/timestamp [0-9]{14}/)) {
        datecache = nodes[i].data.match(/timestamp ([0-9]{8})/);
        break;
      }
  }
  if (datecache == null) return;
 
  // date actuelle locale
  var now = new Date();
  var datenow = formatdateyyyymmdd(now);
 
  if (datecache[1] < datenow) {
 
      // init objet requete serveur
      var xhr_object = createxhrobject();
      if (xhr_object == null) return;
 
      // init requete serveur - purge cache pour page Main_Page
      xhr_object.open('POST', '/en/Main_Page', true);
      xhr_object.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
      xhr_object.setRequestHeader("Charset", "utf-8");
      // action sur fin requete async
      //xhr_object.onreadystatechange = function() {
      //  if (xhr_object.readyState == 4)  alert(xhr_object.status);
      //}
      xhr_object.send('action=purge');
  }
}
 
addOnloadHook(refresh_Image_of_the_day);
 
 
/* Retourne la date formatée yyyymmdd */
 
function formatdateyyyymmdd(obj) {
  var yer = obj.getFullYear();
  var mon = (obj.getMonth() + 1);
  var day = obj.getDate();
  if (mon < 10) mon = '0'+mon;
  if (day < 10) day = '0'+day;
  return yer.toString()+mon.toString()+day.toString();
}
 
 
/* Création objet http pour requête serveur */
function createxhrobject() {
  if (window.XMLHttpRequest) { // Firefox
    return new XMLHttpRequest();
  }
  else if (window.ActiveXObject) { // IE
    return new ActiveXObject("Msxml2.XMLHTTP");
  }
  else return null;
}

Latest revision as of 01:06, 21 December 2021

/* Any JavaScript here will be loaded for all users on every page load. */

/**
 * Keep code in MediaWiki:Common.js to a minimum as it is unconditionally
 * loaded for all users on every wiki page. If possible create a gadget that is
 * enabled by default instead of adding it here (since gadgets are fully
 * optimized ResourceLoader modules with possibility to add dependencies etc.)
 *
 * Since Common.js isn't a gadget, there is no place to declare its
 * dependencies, so we have to lazy load them with mw.loader.using on demand and
 * then execute the rest in the callback. In most cases these dependencies will
 * be loaded (or loading) already and the callback will not be delayed. In case a
 * dependency hasn't arrived yet it'll make sure those are loaded before this.
 */

/* global mw, $ */
/* jshint strict:false, browser:true */

mw.loader.using( [ 'mediawiki.util' ] ).done( function () {
	/* Begin of mw.loader.using callback */

	/**
	 * Main Page layout fixes
	 *
	 * Description: Adds an additional link to the complete list of languages available. 
	 * Should T18962 be completed this can be removed along with associated CSS in common.css
	 * Maintainers: [[User:AzaToth]]
	 */
	if ( mw.config.get( 'wgPageName' ) === 'Main_Page' || mw.config.get( 'wgPageName' ) === 'Talk:Main_Page' ) {
		$( function () {
			mw.util.addPortletLink( 'p-lang', '//meta.wikimedia.org/wiki/List_of_Wikipedias',
				'Complete list', 'interwiki-completelist', 'Complete list of Wikipedias' );
		} );
	}

	/**
	 * Map addPortletLink to mw.util
	 * @deprecated: Use mw.util.addPortletLink instead.
	 */
	mw.log.deprecate( window, 'addPortletLink', mw.util.addPortletLink, 'Use mw.util.addPortletLink instead' );

	/**
	 * Test if an element has a certain class
	 * @deprecated:  Use $(element).hasClass() instead.
	 */
	mw.log.deprecate( window, 'hasClass', function ( element, className ) {
		return $( element ).hasClass( className );
	}, 'Use jQuery.hasClass() instead' );

	/**
	 * @source www.mediawiki.org/wiki/Snippets/Load_JS_and_CSS_by_URL
	 * @rev 6
	 */
	var extraCSS = mw.util.getParamValue( 'withCSS' ),
		extraJS = mw.util.getParamValue( 'withJS' );

	if ( extraCSS ) {
		if ( extraCSS.match( /^MediaWiki:[^&<>=%#]*\.css$/ ) ) {
			mw.loader.load( '/w/index.php?title=' + extraCSS + '&action=raw&ctype=text/css', 'text/css' );
		} else {
			mw.notify( 'Only pages from the MediaWiki namespace are allowed.', { title: 'Invalid withCSS value' } );
		}
	}

	if ( extraJS ) {
		if ( extraJS.match( /^MediaWiki:[^&<>=%#]*\.js$/ ) ) {
			mw.loader.load( '/w/index.php?title=' + extraJS + '&action=raw&ctype=text/javascript' );
		} else {
			mw.notify( 'Only pages from the MediaWiki namespace are allowed.', { title: 'Invalid withJS value' } );
		}
	}

	/**
	 * WikiMiniAtlas
	 *
	 * Description: WikiMiniAtlas is a popup click and drag world map.
	 *              This script causes all of our coordinate links to display the WikiMiniAtlas popup button.
	 *              The script itself is located on meta because it is used by many projects.
	 *              See [[Meta:WikiMiniAtlas]] for more information.
	 * Note - use of this service is recommended to be replaced with mw:Help:Extension:Kartographer
	 */
	$( function () {
		var requireWikiminiatlas = $( 'a.external.text[href*="geohack"]' ).length || $( 'div.kmldata' ).length;
		if ( requireWikiminiatlas ) {
			mw.loader.load( '//meta.wikimedia.org/w/index.php?title=MediaWiki:Wikiminiatlas.js&action=raw&ctype=text/javascript' );
		}
	} );

	/**
	 * 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
				if ( $toggle.parent()[ 0 ].style.color ) {
					$toggle.find( 'a' ).css( 'color', 'inherit' );
				}
			}
		} );
	}

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

	/**
	 * Magic editintros ****************************************************
	 *
	 * Description: Adds editintros on disambiguation pages and BLP pages.
	 * Maintainers: [[User:RockMFR]]
	 *
	 * @param {string} name
	 */
	function addEditIntro( name ) {
		$( '.mw-editsection, #ca-edit, #ca-ve-edit' ).find( 'a' ).each( function ( i, el ) {
			el.href = $( this ).attr( 'href' ) + '&editintro=' + name;
		} );
	}

	if ( mw.config.get( 'wgNamespaceNumber' ) === 0 ) {
		$( function () {
			if ( document.getElementById( 'disambigbox' ) ) {
				addEditIntro( 'Template:Disambig_editintro' );
			}
		} );

		$( function () {
			var cats = mw.config.get( 'wgCategories' );
			if ( !cats ) {
				return;
			}
			if ( $.inArray( 'Living people', cats ) !== -1 || $.inArray( 'Possibly living people', cats ) !== -1 ) {
				addEditIntro( 'Template:BLP_editintro' );
			}
		} );
	}

	/* Actions specific to the edit page */
	if ( mw.config.get( 'wgAction' ) === 'edit' || mw.config.get( 'wgAction' ) === 'submit' ) {
		/**
		 * Fix edit summary prompt for undo
		 *
		 *  Fixes the fact that the undo function combined with the "no edit summary prompter"
		 *  complains about missing editsummary, if leaving the edit summary unchanged.
		 *  Added by [[User:Deskana]], code by [[User:Tra]].
		 *  See also [[phab:T10912]].
		 */
		$( function () {
			if ( document.location.search.indexOf( 'undo=' ) !== -1 && document.getElementsByName( 'wpAutoSummary' )[ 0 ] ) {
				document.getElementsByName( 'wpAutoSummary' )[ 0 ].value = '1';
			}
		} );
	}

	/* End of mw.loader.using callback */
} );
/* DO NOT ADD CODE BELOW THIS LINE */