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

Da Minipedia.
Jump to navigation Jump to search
(add support to modal and page creation)
m (cosmetic change)
Riga 30: Riga 30:
 
'p-cactions',
 
'p-cactions',
 
'#',
 
'#',
'Mini Version',
+
'Minipedia',
 
'ca-minipedia',
 
'ca-minipedia',
 
'Open Minipedia',
 
'Open Minipedia',

Versione delle 00:19, 27 giu 2020

/**
 * 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 name
	mp.namespace = mp.namespace || 'Mini';

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

	// work only in the main namespace
	var ns = mw.config.get( 'wgNamespaceNumber' );
	if( ns !== 0 ) {
		return;
	}

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

	/**
	 * 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 );
			} );
		
		} );
	};

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

			} );
		} );
	} );
} );