MediaWiki:Gadget-IndexFormTools.js

From Wikisource
Jump to navigation Jump to search
Note: After saving, changes may not occur immediately. Click here to learn how to bypass your browser's cache.
  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (Cmd-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (Cmd-Shift-R on a Mac)
  • Internet Explorer: Hold Ctrl while clicking Refresh, or press Ctrl-F5
  • Opera: Clear the cache in Tools → Preferences

For details and instructions about other browsers, see Wikipedia:Bypass your cache.

/*
 * Simple JS to provide some reactivity to the index page form
 */

/* eslint-disable one-var, vars-on-top */

( function ( mw, $ ) {

	// only in edit of Index NS
	if ( mw.config.get( 'wgCanonicalNamespace' ) !== 'Index' ||
			[ 'edit', 'submit' ].indexOf( mw.config.get( 'wgAction' ) ) === -1 ) {
		return;
	}

	$( function () {

		// eslint-disable-next-line no-jquery/no-global-selector
		var progWidget = OO.ui.infuse( $( '#wpprpindex-Progress' ).parent() );

		// eslint-disable-next-line no-jquery/no-global-selector
		var validDateWidget = OO.ui.infuse( $( '#wpprpindex-Validation_date' ).parent() );

		function isValidated() {
			return progWidget.getValue() === 'T';
		}

		/**
		 * Update the status of the validated date widget
		 */
		function updateValidDate() {
			var canSetDate = isValidated();

			validDateWidget.setDisabled( !canSetDate );

			var placeholder;
			if ( !canSetDate ) {
				placeholder = 'Validation date cannot be set before the work is validated.';
			} else {
				var now = new Date();
				// don't use wgMonthNames, the categories are fixed as English
				var months = [ 'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December' ];
				placeholder = months[ now.getMonth() ] + ' ' + now.getFullYear();
			}

			validDateWidget.$input.attr( 'placeholder', placeholder );
		}

		function getCheckerUrl() {
			var pageName = mw.config.get( 'wgPageName' );
			return 'https://checker.toolforge.org/?db=enwikisource_p&title=' + pageName;
		}

		function addLinkToFieldLabel( id, link ) {
			$( id )
				.closest( '.oo-ui-fieldLayout-body' )
				.find( 'label' )
				.append( link
					.css( { 'font-size': '92%', float: 'right' } )
				);
		}

		updateValidDate();

		progWidget.on( 'change', function () {
			updateValidDate();
		} );

		addLinkToFieldLabel( '#wpprpindex-Transclusion', $( '<a>' )
			.attr( 'href', getCheckerUrl() )
			.attr( 'target', '_blank' )
			.append( 'Check transclusion' )
		);
	} );

// eslint-disable-next-line no-undef
}( mediaWiki, jQuery ) );