Differenze tra le versioni di "MediaWiki:Gadget-minipedia.js"
Jump to navigation
Jump to search
(minor refactor and cosmetic changes on namespace change) |
(introduce some L10N configuration) |
||
Riga 7: | Riga 7: | ||
*/ |
*/ |
||
$.when( mw.loader.using( 'mediawiki.util' ), $.ready ).then( function () { |
$.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 |
// global configuration file |
||
Riga 26: | Riga 53: | ||
// font size of Minipedia |
// font size of Minipedia |
||
mp.fontSize = mp.fontSize || '1.2em'; |
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 ]; |
||
+ | } |
||
+ | } |
||
/** |
/** |
||
Riga 49: | Riga 89: | ||
// no response no party |
// no response no party |
||
− | if( !response.query ) { |
+ | if( !response.query || !response.query.pages ) { |
− | throw 'no response'; |
+ | throw 'no valid response'; |
} |
} |
||
// the list should contain just one page |
// the list should contain just one page |
||
− | var |
+ | var pages = response.query.pages; |
− | for( var |
+ | for( var id in pages ) { |
+ | |||
− | page = response.query[i]; |
||
+ | // for each page (just one) check if it exists |
||
+ | var page = pages[ id ]; |
||
if( page.pageid && page.pageid > 0 ) { |
if( page.pageid && page.pageid > 0 ) { |
||
− | // |
+ | // found! |
callback( page ); |
callback( page ); |
||
+ | return; |
||
} |
} |
||
} |
} |
||
Riga 74: | Riga 117: | ||
* Prepare the wiki |
* Prepare the wiki |
||
*/ |
*/ |
||
− | function |
+ | 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 miniVersionPortletLink = mw.util.addPortletLink( |
||
− | 'p-cactions', |
||
− | '#', |
||
− | 'Minipedia', |
||
− | 'ca-minipedia', |
||
− | 'Open Minipedia', |
||
− | 'n' |
||
− | ); |
||
+ | var miniPageName = miniTitleObject.getPrefixedText(); |
||
− | // 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 |
* Go to the Minipedia page in edit mode |
||
* |
* |
||
− | * |
+ | * The page in the main namespace will be preloaded. |
+ | |||
*/ |
*/ |
||
− | function goToMinipediaEditPageTitle( |
+ | function goToMinipediaEditPageTitle() { |
+ | /** |
||
− | // eventually just inherit this page name |
||
+ | * Build the query string to edit a page |
||
− | title = title || pageName; |
||
+ | * |
||
− | |||
+ | * TODO: eventually add VisualEditor support |
||
− | var mpTitle = mp.namespace + ':' + title; |
||
+ | */ |
||
− | |||
− | var |
+ | var editPageQueryString = { |
action: 'edit', |
action: 'edit', |
||
− | title: |
+ | title: miniPageName, |
− | preload: |
+ | preload: pageName, |
editintro: mp.editIntro, |
editintro: mp.editIntro, |
||
}; |
}; |
||
Riga 113: | Riga 151: | ||
// go to the edit page URL |
// go to the edit page URL |
||
− | window.location = wgScript + '?' + $.param( |
+ | window.location = wgScript + '?' + $.param( editPageQueryString ); |
}; |
}; |
||
+ | // add a "Minipedia" |
||
− | /** |
||
+ | var miniVersionPortletLink = mw.util.addPortletLink( |
||
− | * Go to the Minipedia page in view mode |
||
+ | 'p-cactions', |
||
− | * |
||
+ | '#', |
||
− | * @param title Page title without Minipedia namespace |
||
+ | L10N.minipedia, |
||
− | */ |
||
+ | 'ca-minipedia', |
||
− | function goToMinipediaPageTitle( title ) { |
||
+ | L10N.openMinipedia, |
||
− | |||
+ | 'n' |
||
− | // 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 |
// on the mini toolback click, check if a mini version exists |
||
− | $( miniVersionPortletLink ).click( function() { |
+ | $( miniVersionPortletLink ).click( function( e ) { |
// check if the mini version exists |
// check if the mini version exists |
||
Riga 145: | Riga 177: | ||
// just redirect to the Minipedia version |
// just redirect to the Minipedia version |
||
+ | window.location = miniTitleObject.getUrl(); |
||
− | goToMinipediaPageTitle(); |
||
} else { |
} else { |
||
Riga 158: | Riga 190: | ||
// configure and open dialog |
// configure and open dialog |
||
var windowInstance = windowManager.openWindow( messageDialog, { |
var windowInstance = windowManager.openWindow( messageDialog, { |
||
− | title: |
+ | title: L10N.createMinipediaPageTitle, |
+ | message: L10N.createMinipediaPageBody, |
||
− | message: "Sii il primo a creare una versione più ridotta e più accessibile di questa voce, in Minipedia!" |
||
} ); |
} ); |
||
Riga 170: | Riga 202: | ||
} |
} |
||
} ); |
} ); |
||
+ | |||
} |
} |
||
+ | // end if |
||
} ); |
} ); |
||
+ | // end mw.loader |
||
+ | |||
} ); |
} ); |
||
+ | // end onPageExists |
||
+ | |||
+ | // avoid scrolling to the top |
||
+ | e.preventDefault(); |
||
+ | |||
} ); |
} ); |
||
+ | // end $( miniVersionPortletLink ).click |
||
}; |
}; |
||
Riga 192: | Riga 234: | ||
// increase font size (animation?) |
// increase font size (animation?) |
||
$mwBodyContent.css( 'font-size', mp.fontSize ); |
$mwBodyContent.css( 'font-size', mp.fontSize ); |
||
+ | |||
+ | // add a "Minipedia" |
||
+ | var normalVersionPortletLink = mw.util.addPortletLink( |
||
+ | 'p-cactions', |
||
+ | '#', |
||
+ | L10N.normalpedia, |
||
+ | 'ca-normalpedia', |
||
+ | L10N.openNormalpedia, |
||
+ | ); |
||
}; |
}; |
||
// end prepareMinipedia() |
// end prepareMinipedia() |
||
Riga 198: | Riga 249: | ||
var ns = mw.config.get( 'wgNamespaceNumber' ); |
var ns = mw.config.get( 'wgNamespaceNumber' ); |
||
if( ns === 0 ) { |
if( ns === 0 ) { |
||
− | + | prepareNormalpedia(); |
|
} else if( ns == mp.namespaceNum ) { |
} else if( ns == mp.namespaceNum ) { |
||
prepareMinipedia(); |
prepareMinipedia(); |
Versione delle 07:10, 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 } );