MediaWiki:Common.js: Difference between revisions

From WikiMSK

No edit summary
No edit summary
Tag: Manual revert
 
(102 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.
*/
 
/* 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' );
} );
 
 
 
}


function docReady(fn) {
/* End of mw.loader.using callback */
    // see if DOM is already available
);
    if (document.readyState === "complete" || document.readyState === "interactive") {
/* DO NOT ADD CODE BELOW THIS LINE */
        // call on next available tick
        setTimeout(fn, 1);
    } else {
        document.addEventListener("DOMContentLoaded", fn);
    }
}   


$('document').ready(function() {
  // Get all the <h2> headings
  const headings = document.querySelectorAll('.mw-parser-output h2')
 
  Array.prototype.forEach.call(headings, heading => {
    // Give each <h2> a toggle button child
    // with the SVG plus/minus icon
    heading.innerHTML = `
      <button aria-expanded="false">
        ${heading.textContent}
        <svg aria-hidden="true" focusable="false" viewBox="0 0 10 10">
          <rect class="vert" height="8" width="2" y="1" x="4"/>
          <rect height="2" width="8" y="4" x="1"/>
        </svg>
      </button>
    `
   
    // Function to create a node list
    // of the content between this <h2> and the next
    const getContent = (elem) => {
      let elems = []
      while (elem.nextElementSibling && elem.nextElementSibling.tagName !== 'H2') {
        elems.push(elem.nextElementSibling)
        elem = elem.nextElementSibling
      }
     
      // Delete the old versions of the content nodes
      elems.forEach((node) => {
        node.parentNode.removeChild(node)
      })


      return elems
/* 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');
    // Assign the contents to be expanded/collapsed (array)
var linkList = [];
    let contents = getContent(heading)
   
for (i = 0; i < galleryText.length; i++) {
    // Create a wrapper element for `contents` and hide it
var pageLink = galleryText[i].innerHTML;
    let wrapper = document.createElement('div')
var endLinkIndex = pageLink.indexOf("</a>");
    wrapper.hidden = true
pageLink = pageLink.slice(0,endLinkIndex);
   
     pageLink += "</a></p>";
    // Add each element of `contents` to `wrapper`
     linkList.push(pageLink);
    contents.forEach(node => {
}
      wrapper.appendChild(node)
    })
   
    // Add the wrapped content back into the DOM
    // after the heading
    heading.parentNode.insertBefore(wrapper, heading.nextElementSibling)
      
    // Assign the button
     let btn = heading.querySelector('button')
      
      
     btn.onclick = () => {
     for (i = 0; i < galleryText.length; i++) {
      // Cast the state as a boolean
    document.querySelectorAll('.gallerytext')[i].innerHTML = linkList[i];
      let expanded = btn.getAttribute('aria-expanded') === 'true' || false
}
     
      // Switch the state
      btn.setAttribute('aria-expanded', !expanded)
      // Switch the content's visibility
      wrapper.hidden = expanded   
    }
  })
})()

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];
	}