MediaWiki:Gadget-minipedia.js

Da Minipedia.
Versione del 6 lug 2020 alle 08:03 di Valerio Bozzolan (discussione | contributi) (minor refactor and cosmetic changes on namespace change)
Jump to navigation Jump to search

Nota: dopo aver salvato, potrebbe essere necessario pulire la cache del proprio browser per vedere i cambiamenti.

  • Firefox / Safari: tenere premuto il tasto delle maiuscole Shift e fare clic su Ricarica, oppure premere Ctrl-F5 o Ctrl-R (⌘-R su Mac)
  • Google Chrome: premere Ctrl-Shift-R (⌘-Shift-R su un Mac)
  • Internet Explorer: tenere premuto il tasto Ctrl e fare clic su Aggiorna, oppure premere Ctrl-F5
  • Opera: Vai nel Menu → Impostazioni (Opera → Preferenze su un Mac) e poi in Privacy & sicurezza → Pulisci dati del browser → Immagini e file nella cache.
/**
 * Make Minipedia magics
 *
 * Dependencies: mediawiki.util
 *
 * @revision 2020-06-27
 */
$.when( mw.loader.using( 'mediawiki.util' ), $.ready ).then( function () {

	// 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';

	/**
	 * 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 ) {
					throw 'no response';
				}

				// the list should contain just one page
				var page;
				for( var i in response.query ) {
					page = response.query[i];
					if( page.pageid && page.pageid > 0 ) {

						// exists
						callback( page );
					}
				}

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

	/**
	 * Prepare the wiki
	 */
	function prepareNormalWiki() {

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

		// normal page title and mini version
		var pageName = mw.config.get( 'wgPageName' );
		var miniPageName = mp.namespace + ':' + pageName;

		/**
		 * Go to the Minipedia page in edit mode
		 *
		 * @param title Page title without Minipedia namespace
		 */
		function goToMinipediaEditPageTitle( title ) {

			// eventually just inherit this page name
			title = title || pageName;

			var mpTitle = mp.namespace + ':' + title;

			var queryString = {
				action: 'edit',
				title: mpTitle,
				preload: title,
				editintro: mp.editIntro,			
			};

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

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

		/**
		 * Go to the Minipedia page in view mode
		 *
		 * @param title Page title without Minipedia namespace
		 */
		function goToMinipediaPageTitle( title ) {

			// eventually just inherit this page name
			title = title || pageName;

			var miniTitleObject = new mw.Title( title,  mp.namespace );

			// go to the edit page URL
			window.location = miniTitleObject.getUrl();
		};

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

			// 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
						goToMinipediaPageTitle();
					} 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: "Accesso Minipedia",
							message: "Sii il primo a creare una versione più ridotta e più accessibile di questa voce, in Minipedia!"
						} );

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

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

				} );
			} );
		} );

	};
	// 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 );
	};
	// end prepareMinipedia()

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