Differenze tra le versioni di "MediaWiki:Gadget-minipedia.js"

Da Minipedia.
Jump to navigation Jump to search
(introduce some L10N configuration)
m (-typo)
Riga 241: Riga 241:
 
L10N.normalpedia,
 
L10N.normalpedia,
 
'ca-normalpedia',
 
'ca-normalpedia',
L10N.openNormalpedia,
+
L10N.openNormalpedia
 
);
 
);
 
};
 
};

Versione delle 07:12, 7 lug 2020

/**
 * Make Minipedia magics
 *
 * Dependencies: mediawiki.util
 *
 * @revision 2020-06-27
 */
$.when( mw.loader.using( 'mediawiki.util' ), $.ready ).then( function () {

	/*
	 * CONFIGURATION/LOCALIZATION INSTRUCTIONS
	 *
	 * Declare somewhere something like this:
	 *
	 *   // assure that you do not overwrite other-people customizations
	 *   window.MiniPedia = window.MiniPedia           || {};
	 *   window.MiniPedia.L10N = window.MiniPedia.L10N || {};
	 *
	 *   // customize the logo and some strings
	 *   window.MiniPedia.logoURL = 'https://path/to/your/logo';
	 *   window.MiniPedia.L10N.minipedia = "SomethingElse-Pedia";
	 *
	 * For all the strings available in the L10N object
	 * see the L10N_DEFAULTS variable below.
	 */

	// load localization defaults
	var L10N_DEFAULTS = {
		minipedia: "Minipedia",
		normalpedia: "Wikipedia Test",
		openMinipedia: "Apri Minipedia",
		openNormalpedia: "Apri Wikipedia Test",
		createMinipediaPageTitle: "Accesso Minipedia",
		createMinipediaPageBody: "Sii il primo a creare una versione più ridotta e più accessibile di questa voce, in Minipedia!",
	};

	// global configuration file
	window.MiniPedia = window.MiniPedia || {};

	// shortcut for the global configuration file (mp = mini-pedia)
	var mp = window.MiniPedia;

	// default namespace informations
	mp.namespace    = mp.namespace    || 'Mini';
	mp.namespaceNum = mp.namespaceNum || 3002;

	// default edit intro page title
	mp.editIntro = mp.editIntro || 'Progetto:Minipedia/Creazione voce';

	// default logo URL of Minipedia
	mp.logoURL   = mp.logoURL   || '/images/5/57/MinipediA_coconut_noun_simple_logo.png';

	// font size of Minipedia
	mp.fontSize  = mp.fontSize  || '1.2em';

	// localization stuff
	mp.L10N      = mp.L10N      || {};

	// shortcut
	var L10N = mp.L10N;

	// load the defaults
	for( var key in L10N_DEFAULTS ) {
		if( !L10N[ key ] ) {
			L10N[ key ] = L10N_DEFAULTS[ key ];
		}
	}

	/**
	 * Check if a page title already exists
	 *
	 * @param title    Page title
	 * @param callback Callback with one boolean argument telling if the page title exists or not
	 */
	function pageExists( title, callback ) {

		// require the API stuff
		mw.loader.using( 'mediawiki.api' ).then( function() {

			// prepare the API request
			var request = {
				action: 'query',
				prop: 'info',
				titles: title
			};

			// make the API request
			( new mw.Api() ).get( request ).done( function ( response ) {

				// no response no party
				if( !response.query || !response.query.pages ) {
					throw 'no valid response';
				}

				// the list should contain just one page
				var pages = response.query.pages;
				for( var id in pages ) {

					// for each page (just one) check if it exists
					var page = pages[ id ];
					if( page.pageid && page.pageid > 0 ) {

						// found!
						callback( page );
						return;
					}
				}

				// does not exist
				callback( false );
			} );
		
		} );
	};

	/**
	 * Prepare the wiki
	 */
	function prepareNormalpedia() {
	
		// normal page title and mini version
		var pageName = mw.config.get( 'wgPageName' );

		// prepare the Minipedia title object
		var miniTitleObject = new mw.Title( pageName,  mp.namespaceNum );

		var miniPageName = miniTitleObject.getPrefixedText();

		/**
		 * Go to the Minipedia page in edit mode
		 *
		 * The page in the main namespace will be preloaded.
		 
		 */
		function goToMinipediaEditPageTitle() {

			/**
			 * Build the query string to edit a page
			 *
			 * TODO: eventually add VisualEditor support
			 */
			var editPageQueryString = {
				action: 'edit',
				title: miniPageName,
				preload: pageName,
				editintro: mp.editIntro,			
			};

			// '/index.php'
			var wgScript = mw.config.get( 'wgScript' );

			// go to the edit page URL
			window.location = wgScript + '?' + $.param( editPageQueryString );
		};

		// add a "Minipedia"
		var miniVersionPortletLink = mw.util.addPortletLink(
			'p-cactions',
			'#',
			L10N.minipedia,
			'ca-minipedia',
			L10N.openMinipedia,
			'n'
		);

		// on the mini toolback click, check if a mini version exists
		$( miniVersionPortletLink ).click( function( e ) {

			// check if the mini version exists
			pageExists( miniPageName, function( miniPageExists ) {

				// allow to open OO UI windows
				mw.loader.using( 'oojs-ui-windows' ).then( function() {

					// check if the page already exist
					if( miniPageExists ) {

						// just redirect to the Minipedia version
						window.location = miniTitleObject.getUrl();
					} else {

						// ask if you want to create the page

						// create message dialog window
						var messageDialog = new OO.ui.MessageDialog();
						var windowManager = new OO.ui.WindowManager();
						$( 'body' ).append( windowManager.$element );
						windowManager.addWindows( [ messageDialog ] );

						// configure and open dialog
						var windowInstance = windowManager.openWindow( messageDialog, {
							title:   L10N.createMinipediaPageTitle,
							message: L10N.createMinipediaPageBody,
						} );

						// check if you accepted the page creation
						windowInstance.closed.then( function ( data ) {
							if( data.action === 'accept' ) {

								// go go go!
								goToMinipediaEditPageTitle();
							}
						} );

					}
					// end if

				} );
				// end mw.loader

			} );
			// end onPageExists

			// avoid scrolling to the top
			e.preventDefault();

		} );
		// end $( miniVersionPortletLink ).click

	};
	// end prepareNormalWiki()

	function prepareMinipedia() {

		// website logo
		var $logo = $( '#p-logo .mw-wiki-logo' );

		// body content
		var $mwBodyContent = $( '.mw-body-content' );

		// set Minipedia logo (animation?)
		$logo.css( 'background-image', 'url(' + mp.logoURL + ')' );

		// increase font size (animation?)
		$mwBodyContent.css( 'font-size', mp.fontSize );

		// add a "Minipedia"
		var normalVersionPortletLink = mw.util.addPortletLink(
			'p-cactions',
			'#',
			L10N.normalpedia,
			'ca-normalpedia',
			L10N.openNormalpedia
		);
	};
	// end prepareMinipedia()

	// work only in the main namespace
	var ns = mw.config.get( 'wgNamespaceNumber' );
	if( ns === 0 ) {
		prepareNormalpedia();
	} else if( ns == mp.namespaceNum ) {
		prepareMinipedia();
	}
	// end namespace zero check
} );