MediaWiki:Common.js: Difference between revisions

From WikiMSK

No edit summary
No edit summary
Tag: Manual revert
 
(71 intermediate revisions by 2 users not shown)
Line 1: Line 1:
/* 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.
*/


function docReady(fn) {
/* global mw, $ */
    // see if DOM is already available
/* jshint strict:false, browser:true */
    if (document.readyState === "complete" || document.readyState === "interactive") {
        // call on next available tick
        setTimeout(fn, 1);
    } else {
        document.addEventListener("DOMContentLoaded", fn);
    }
}   


if (window.innerWidth < 768 && document.querySelectorAll('h2').length > 2){
mw.loader.using( [ 'mediawiki.util' ] ).done( function () {
docReady(collapsibleHeadings);
/* Begin of mw.loader.using callback */
}


function collapsibleHeadings() {
/**
* Main Page layout fixes
*
* Description: Adds an additional link to the complete list of languages available.
* Maintainers: [[User:AzaToth]], [[User:R. Koot]], [[User:Alex Smotrov]]
*/
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' );
} );
}


  // Get all the <h2> headings
/**
  var headings = document.querySelectorAll('.mw-parser-output h2');
* Map addPortletLink to mw.util
  Array.prototype.forEach.call(headings, function (heading) {
* @deprecated: Use mw.util.addPortletLink instead.
    // Give each <h2> a toggle button child
*/
    // with the SVG plus/minus icon
mw.log.deprecate( window, 'addPortletLink', mw.util.addPortletLink, 'Use mw.util.addPortletLink instead' );
    heading.innerHTML = "\n      <button aria-expanded=\"false\">\n        ".concat(heading.innerHTML, "\n        <svg aria-hidden=\"true\" focusable=\"false\" viewBox=\"0 0 10 10\">\n          <rect class=\"vert\" height=\"8\" width=\"2\" y=\"1\" x=\"4\"/>\n          <rect height=\"2\" width=\"8\" y=\"4\" x=\"1\"/>\n        </svg>\n      </button>\n    ");  
   


   
/**
    // Function to create a node list
* Extract a URL parameter from the current URL
    // of the content between this <h2> and the next
* @deprecated: Use mw.util.getParamValue with proper escaping
*/
mw.log.deprecate( window, 'getURLParamValue', mw.util.getParamValue, 'Use mw.util.getParamValue instead' );


    var getContent = function getContent(elem) {
/**
      var elems = [];
* 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' );


      while (elem.nextElementSibling && elem.nextElementSibling.tagName !== 'H2') {
/**
        elems.push(elem.nextElementSibling);
* @source www.mediawiki.org/wiki/Snippets/Load_JS_and_CSS_by_URL
        elem = elem.nextElementSibling;
* @rev 6
      } // Delete the old versions of the content nodes
*/
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' } );
}
}


      elems.forEach(function (node) {
if ( extraJS ) {
        node.parentNode.removeChild(node);
if ( extraJS.match( /^MediaWiki:[^&<>=%#]*\.js$/ ) ) {
      });
mw.loader.load( '/w/index.php?title=' + extraJS + '&action=raw&ctype=text/javascript' );
      return elems;
} else {
    }; // Assign the contents to be expanded/collapsed (array)
mw.notify( 'Only pages from the MediaWiki namespace are allowed.', { title: 'Invalid withJS value' } );
}
}


/**
* ImageStacks
* Maintainer: [[User:Hellerhoff]], [[User:DieBuche]]
*/
// Only load if page contains template
mw.hook( 'wikipage.content' ).add( function ( $content ) {
    if ( $content.find( 'div.ImageStack' ).length ) mw.loader.load( 'ext.gadget.ImageStack' );
} );


    var contents = getContent(heading); // Create a wrapper element for `contents` and hide it


    var wrapper = document.createElement('div');
    wrapper.hidden = true; // Add each element of `contents` to `wrapper`


    contents.forEach(function (node) {
}
      wrapper.appendChild(node);
    }); // Add the wrapped content back into the DOM
    // after the heading


    heading.parentNode.insertBefore(wrapper, heading.nextElementSibling); // Assign the button
/* End of mw.loader.using callback */
);
/* DO NOT ADD CODE BELOW THIS LINE */


    var btn = heading.querySelector('button');


    btn.onclick = function () {
/* Any JavaScript here will be loaded for all users on every page load. */
      // Cast the state as a boolean
/*Remove the file size information on the gallery image for dynamicpagelist*/
      var expanded = btn.getAttribute('aria-expanded') === 'true' || false; // Switch the state
var galleryText = document.querySelectorAll('.gallerytext');
 
var linkList = [];
      btn.setAttribute('aria-expanded', !expanded); // Switch the content's visibility
 
for (i = 0; i < galleryText.length; i++) {
      wrapper.hidden = expanded;
var pageLink = galleryText[i].innerHTML;
     };
var endLinkIndex = pageLink.indexOf("</a>");
  });
pageLink = pageLink.slice(0,endLinkIndex);
};
    pageLink += "</a></p>";
    linkList.push(pageLink);
}
   
     for (i = 0; i < galleryText.length; i++) {
    document.querySelectorAll('.gallerytext')[i].innerHTML = linkList[i];
}

Latest revision as of 14:39, 27 March 2023

/**
 * 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.
	 * Maintainers: [[User:AzaToth]], [[User:R. Koot]], [[User:Alex Smotrov]]
	 */
	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' );

	/**
	 * Extract a URL parameter from the current URL
	 * @deprecated: Use mw.util.getParamValue with proper escaping
	 */
	mw.log.deprecate( window, 'getURLParamValue', mw.util.getParamValue, 'Use mw.util.getParamValue 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' } );
		}
	}

/**
 * ImageStacks
 * Maintainer: [[User:Hellerhoff]], [[User:DieBuche]]
 */
// Only load if page contains template
mw.hook( 'wikipage.content' ).add( function ( $content ) {
    if ( $content.find( 'div.ImageStack' ).length ) mw.loader.load( 'ext.gadget.ImageStack' );
} );



	}

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


/* Any JavaScript here will be loaded for all users on every page load. */
/*Remove the file size information on the gallery image for dynamicpagelist*/
	var galleryText = document.querySelectorAll('.gallerytext');
	var linkList = [];
	
	for (i = 0; i < galleryText.length; i++) {
	var pageLink = galleryText[i].innerHTML;
	var endLinkIndex = pageLink.indexOf("</a>");
	pageLink = pageLink.slice(0,endLinkIndex);
    pageLink += "</a></p>";
    linkList.push(pageLink);
	}
    
    for (i = 0; i < galleryText.length; i++) {
    document.querySelectorAll('.gallerytext')[i].innerHTML = linkList[i];
	}