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

Da Minipedia.
Jump to navigation Jump to search
m (cosmetic change)
(minor refactor and cosmetic changes on namespace change)
Riga 14: Riga 14:
 
var mp = window.MiniPedia;
 
var mp = window.MiniPedia;
   
// default namespace name
+
// default namespace informations
mp.namespace = mp.namespace || 'Mini';
+
mp.namespace = mp.namespace || 'Mini';
  +
mp.namespaceNum = mp.namespaceNum || 3002;
   
 
// 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';
   
// work only in the main namespace
+
// default logo URL of Minipedia
  +
mp.logoURL = mp.logoURL || '/images/5/57/MinipediA_coconut_noun_simple_logo.png';
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'
 
);
 
   
  +
// font size of Minipedia
// normal page title and mini version
 
  +
mp.fontSize = mp.fontSize || '1.2em';
var pageName = mw.config.get( 'wgPageName' );
 
var miniPageName = mp.namespace + ':' + pageName;
 
   
 
/**
 
/**
Riga 85: Riga 72:
   
 
/**
 
/**
* Go to the Minipedia page in edit mode
+
* Prepare the wiki
*
 
* @param title Page title without Minipedia namespace
 
 
*/
 
*/
function goToMinipediaEditPageTitle( title ) {
+
function prepareNormalWiki() {
   
  +
// add a "Minipedia"
// eventually just inherit this page name
 
  +
var miniVersionPortletLink = mw.util.addPortletLink(
title = title || pageName;
 
  +
'p-cactions',
  +
'#',
  +
'Minipedia',
  +
'ca-minipedia',
  +
'Open Minipedia',
  +
'n'
  +
);
   
  +
// normal page title and mini version
var mpTitle = mp.namespace + ':' + title;
 
  +
var pageName = mw.config.get( 'wgPageName' );
  +
var miniPageName = mp.namespace + ':' + pageName;
   
  +
/**
var queryString = {
 
  +
* Go to the Minipedia page in edit mode
action: 'edit',
 
  +
*
title: mpTitle,
 
  +
* @param title Page title without Minipedia namespace
preload: title,
 
  +
*/
editintro: mp.editIntro,
 
  +
function goToMinipediaEditPageTitle( title ) {
};
 
   
  +
// eventually just inherit this page name
// '/index.php'
 
  +
title = title || pageName;
var wgScript = mw.config.get( 'wgScript' );
 
   
  +
var mpTitle = mp.namespace + ':' + title;
// go to the edit page URL
 
window.location = wgScript + '?' + $.param( queryString );
 
};
 
   
  +
var queryString = {
/**
 
  +
action: 'edit',
* Go to the Minipedia page in view mode
 
  +
title: mpTitle,
*
 
  +
preload: title,
* @param title Page title without Minipedia namespace
 
  +
editintro: mp.editIntro,
*/
 
  +
};
function goToMinipediaPageTitle( title ) {
 
   
  +
// '/index.php'
// eventually just inherit this page name
 
  +
var wgScript = mw.config.get( 'wgScript' );
title = title || pageName;
 
   
  +
// go to the edit page URL
var miniTitleObject = new mw.Title( title, mp.namespace );
 
  +
window.location = wgScript + '?' + $.param( queryString );
  +
};
   
  +
/**
// go to the edit page URL
 
  +
* Go to the Minipedia page in view mode
window.location = miniTitleObject.getUrl();
 
  +
*
};
 
  +
* @param title Page title without Minipedia namespace
  +
*/
  +
function goToMinipediaPageTitle( title ) {
   
  +
// eventually just inherit this page name
// on the mini toolback click, check if a mini version exists
 
  +
title = title || pageName;
$( miniVersionPortletLink ).click( function() {
 
   
  +
var miniTitleObject = new mw.Title( title, mp.namespace );
// check if the mini version exists
 
pageExists( miniPageName, function( miniPageExists ) {
 
   
// allow to open OO UI windows
+
// go to the edit page URL
  +
window.location = miniTitleObject.getUrl();
mw.loader.using( 'oojs-ui-windows' ).then( function() {
 
  +
};
   
// check if the page already exist
+
// on the mini toolback click, check if a mini version exists
  +
$( miniVersionPortletLink ).click( function() {
if( miniPageExists ) {
 
   
// just redirect to the Minipedia version
+
// check if the mini version exists
  +
pageExists( miniPageName, function( miniPageExists ) {
goToMinipediaPageTitle();
 
} else {
 
   
// ask if you want to create the page
+
// allow to open OO UI windows
  +
mw.loader.using( 'oojs-ui-windows' ).then( function() {
   
// create message dialog window
+
// check if the page already exist
  +
if( miniPageExists ) {
var messageDialog = new OO.ui.MessageDialog();
 
var windowManager = new OO.ui.WindowManager();
 
$( 'body' ).append( windowManager.$element );
 
windowManager.addWindows( [ messageDialog ] );
 
   
// configure and open dialog
+
// just redirect to the Minipedia version
  +
goToMinipediaPageTitle();
var windowInstance = windowManager.openWindow( messageDialog, {
 
title: "Accesso Minipedia",
+
} else {
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
+
// ask if you want to create the page
windowInstance.closed.then( function ( data ) {
 
if( data.action === 'accept' ) {
 
   
// go go go!
+
// create message dialog window
  +
var messageDialog = new OO.ui.MessageDialog();
goToMinipediaEditPageTitle();
 
  +
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
 
} );
 
} );

Versione delle 07:03, 6 lug 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 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
} );