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

Da Minipedia.
Jump to navigation Jump to search
m (-typo)
(moved stylesheet to MediaWiki:Gadget-minipedia.css)
Riga 28: Riga 28:
 
var L10N_DEFAULTS = {
 
var L10N_DEFAULTS = {
 
minipedia: "Minipedia",
 
minipedia: "Minipedia",
  +
minipediaShort: "Mini",
 
normalpedia: "Wikipedia Test",
 
normalpedia: "Wikipedia Test",
  +
normalpediaShort: "WikipediaTest",
 
openMinipedia: "Apri Minipedia",
 
openMinipedia: "Apri Minipedia",
 
openNormalpedia: "Apri Wikipedia Test",
 
openNormalpedia: "Apri Wikipedia Test",
Riga 47: Riga 49:
 
// default edit intro page title
 
// default edit intro page title
 
mp.editIntro = mp.editIntro || 'Progetto:Minipedia/Creazione voce';
 
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
 
// localization stuff
Riga 125: Riga 121:
 
var miniTitleObject = new mw.Title( pageName, mp.namespaceNum );
 
var miniTitleObject = new mw.Title( pageName, mp.namespaceNum );
   
  +
// minipedia page title with prefix
 
var miniPageName = miniTitleObject.getPrefixedText();
 
var miniPageName = miniTitleObject.getPrefixedText();
  +
  +
// minipedia page URL
  +
var miniPageUrl = miniTitleObject.getUrl();
   
 
/**
 
/**
Riga 133: Riga 133:
 
 
 
*/
 
*/
function goToMinipediaEditPageTitle() {
+
function goToMinipediaEditPage() {
   
 
/**
 
/**
Riga 156: Riga 156:
 
// add a "Minipedia"
 
// add a "Minipedia"
 
var miniVersionPortletLink = mw.util.addPortletLink(
 
var miniVersionPortletLink = mw.util.addPortletLink(
'p-cactions',
+
'p-namespaces',
'#',
+
miniPageUrl,
L10N.minipedia,
+
L10N.minipediaShort,
 
'ca-minipedia',
 
'ca-minipedia',
 
L10N.openMinipedia,
 
L10N.openMinipedia,
Riga 177: Riga 177:
   
 
// just redirect to the Minipedia version
 
// just redirect to the Minipedia version
window.location = miniTitleObject.getUrl();
+
window.location = miniPageUrl;
 
} else {
 
} else {
   
Riga 199: Riga 199:
   
 
// go go go!
 
// go go go!
goToMinipediaEditPageTitle();
+
goToMinipediaEditPage();
 
}
 
}
 
} );
 
} );
Riga 221: Riga 221:
 
// end prepareNormalWiki()
 
// end prepareNormalWiki()
   
  +
/**
  +
* Prepare the Minipedia namespace
  +
*/
 
function prepareMinipedia() {
 
function prepareMinipedia() {
  +
  +
// normal page title and mini version
  +
var pageName = mw.config.get( 'wgTitle' );
  +
  +
// prepare the Normalpedia title object
  +
var normalTitleObject = new mw.Title( pageName );
  +
  +
// minipedia page title with prefix
  +
var normalPageName = normalTitleObject.getPrefixedText();
  +
  +
// minipedia page URL
  +
var normalPageUrl = normalTitleObject.getUrl();
   
 
// website logo
 
// website logo
Riga 237: Riga 252:
 
// add a "Minipedia"
 
// add a "Minipedia"
 
var normalVersionPortletLink = mw.util.addPortletLink(
 
var normalVersionPortletLink = mw.util.addPortletLink(
'p-cactions',
+
'p-namespaces',
'#',
+
normalPageUrl,
L10N.normalpedia,
+
L10N.normalpediaShort,
 
'ca-normalpedia',
 
'ca-normalpedia',
 
L10N.openNormalpedia
 
L10N.openNormalpedia

Versione delle 09:47, 10 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",
		minipediaShort: "Mini",
		normalpedia: "Wikipedia Test",
		normalpediaShort: "WikipediaTest",
		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';

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

		// minipedia page title with prefix
		var miniPageName = miniTitleObject.getPrefixedText();

		// minipedia page URL
		var miniPageUrl = miniTitleObject.getUrl();

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

			/**
			 * 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-namespaces',
			miniPageUrl,
			L10N.minipediaShort,
			'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 = miniPageUrl;
					} 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!
								goToMinipediaEditPage();
							}
						} );

					}
					// end if

				} );
				// end mw.loader

			} );
			// end onPageExists

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

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

	};
	// end prepareNormalWiki()

	/**
	 * Prepare the Minipedia namespace
	 */
	function prepareMinipedia() {

		// normal page title and mini version
		var pageName = mw.config.get( 'wgTitle' );

		// prepare the Normalpedia title object
		var normalTitleObject = new mw.Title( pageName );

		// minipedia page title with prefix
		var normalPageName = normalTitleObject.getPrefixedText();

		// minipedia page URL
		var normalPageUrl = normalTitleObject.getUrl();

		// 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-namespaces',
			normalPageUrl,
			L10N.normalpediaShort,
			'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
} );