MediaWiki

Difference between revisions of "Gadget-Demo.js"

(Created page with "'use strict'; ( function ( mw, $, rw, OO ) { /* * This first part allows the creation of a basic generator * and allows the RecordWizard to find it (and thus, add a but...")
 
m (Blind test of blind idea : convert to array as in other gadgets.)
 
(7 intermediate revisions by 2 users not shown)
Line 1: Line 1:
 +
/* ************************************************************************** */
 +
// Description: Adds a personal generator of words list to the RecordWizard
 +
// Usage: Copy this code into you User:Name/common.js, hack it as you need to.
 +
// See also : [[Help:Create a new generator]]
 +
 
'use strict';
 
'use strict';
  
( function ( mw, $, rw, OO ) {
+
if ( mw.config.get( 'wgCanonicalSpecialPageName' ) === 'RecordWizard' ) {
 
+
    mw.loader.using( ['oojs', 'oojs-ui', 'ext.recordWizard.generator'], function() {
/*
+
    console.log('MediaWiki:Gadget-Demo.js');
* This first part allows the creation of a basic generator
+
        var rw = mw.recordWizard;
* and allows the RecordWizard to find it (and thus, add a button in the UI for it)
 
*
 
* Replace everywhere in this file "rw.generator.Demo" by a custom
 
* and unique class name, but always in the "rw.generator" namespace.
 
* For example it can be: rw.generator.SomethingCool
 
*/
 
rw.generator.Demo = function ( config ) {
 
rw.generator.Generator.call( this, config );
 
};
 
  
OO.inheritClass( rw.generator.Demo, rw.generator.Generator );
+
    /*
 +
    * This first part allows the creation of a basic generator
 +
    * and allows the RecordWizard to find it (and thus, add a button in the UI for it)
 +
    *
 +
    * Replace everywhere in this file "rw.generator.Demo" by a custom
 +
    * and unique class name, but always in the "rw.generator" namespace.
 +
    * For example it can be: rw.generator.SomethingCool
 +
    */
 +
    rw.generator.Demo = function ( config ) {
 +
    rw.generator.Generator.call( this, config );
 +
    };
  
// This line defines an internal name for the generator
+
    OO.inheritClass( rw.generator.Demo, rw.generator.Generator );
rw.generator.Demo.static.name = 'demo';
 
  
// And this one defines the name for the generator which will be displayed in the UI
+
    // This line defines an internal name for the generator
rw.generator.Demo.static.title = 'My Demo';
+
    rw.generator.Demo.static.name = 'demo';
  
/*
+
    // And this one defines the name for the generator which will be displayed in the UI
* This function should contain all the popup initialization stuff.
+
    rw.generator.Demo.static.title = 'My Demo';
* We create here for example two text fields, with a custom layout.
 
* For more information, see OOui documentation:
 
* https://www.mediawiki.org/wiki/OOUI
 
*/
 
rw.generator.Demo.prototype.initialize = function () {
 
// The two text fields
 
this.aTextField = new OO.ui.TextInputWidget();
 
this.anotherTextField = new OO.ui.TextInputWidget();
 
  
// The custom layout
+
    /*
this.layout = new OO.ui.Widget( {
+
    * This function should contain all the popup initialization stuff.
content: [
+
    * We create here for example two text fields, with a custom layout.
new OO.ui.FieldLayout( this.aTextField, {
+
    * For more information, see OOui documentation:
align: 'top',
+
    * https://www.mediawiki.org/wiki/OOUI
label: 'This field do something cool.'
+
    */
}
+
    rw.generator.Demo.prototype.initialize = function () {
),
+
    // The two text fields
new OO.ui.FieldLayout(
+
    this.aTextField = new OO.ui.TextInputWidget();
this.anotherTextField, {
+
    this.anotherTextField = new OO.ui.TextInputWidget();
align: 'top',
 
label: 'This one too!'
 
}
 
)
 
]
 
} );
 
  
// To be displayed, all the fields/widgets/... should be appended to "this.content.$element"
+
    // The custom layout
this.content.$element.append( this.layout.$element );
+
    this.layout = new OO.ui.Widget( {
 +
    content: [
 +
    new OO.ui.FieldLayout( this.aTextField, {
 +
    align: 'top',
 +
    label: 'This field do something cool.'
 +
    }
 +
    ),
 +
    new OO.ui.FieldLayout(
 +
    this.anotherTextField, {
 +
    align: 'top',
 +
    label: 'This one too!'
 +
    }
 +
    )
 +
    ]
 +
    } );
  
// Do not remove this line, it will initialize the popup itself
+
    // To be displayed, all the fields/widgets/... should be appended to "this.content.$element"
rw.generator.Generator.prototype.initialize.call( this );
+
    this.content.$element.append( this.layout.$element );
};
 
  
/*
+
    // Do not remove this line, it will initialize the popup itself
* This function will be called when the user press the "Done" button.
+
    rw.generator.Generator.prototype.initialize.call( this );
*
+
    };
* Every words that you want to be added to the RecordWizard's word list
 
* have to be added to an array called "this.list".
 
*
 
* The returned value can either be True, or if you want to do some asynchrone
 
* stuff, you can return a jQuery promise
 
*/
 
rw.generator.Demo.prototype.fetch = function () {
 
// Get the values of our text fields
 
var generator = this,
 
demoText = this.aTextField.getValue(),
 
anotherDemoText = this.anotherTextField.getValue();
 
  
// Initialize a new promise
+
    /*
this.deferred = $.Deferred();
+
    * This function will be called when the user press the "Done" button.
 +
    *
 +
    * Every words that you want to be added to the RecordWizard's word list
 +
    * have to be added to an array called "this.list".
 +
    *
 +
    * The returned value can either be True, or if you want to do some asynchrone
 +
    * stuff, you can return a jQuery promise
 +
    */
 +
    rw.generator.Demo.prototype.fetch = function () {
 +
    // Get the values of our text fields
 +
    var generator = this,
 +
    demoText = this.aTextField.getValue(),
 +
    anotherDemoText = this.anotherTextField.getValue();
  
// Initialize our word list
+
    // Initialize a new promise
this.list = [];
+
    this.deferred = $.Deferred();
  
// We will do a asynchronous AJAX request to frwiki's API
+
    // Initialize our word list
this.frwikiApi = new mw.ForeignApi( 'https://fr.wikipedia.org/w/api.php', {
+
    this.list = [];
anonymous: true,
 
parameters: { origin: '*' },
 
ajax: { timeout: 10000 }
 
} );
 
  
// To get 10 random pages from the main namespace
+
    // We will do a asynchronous AJAX request to frwiki's API
this.frwiki.get( {
+
    this.frwikiApi = new mw.ForeignApi( 'https://fr.wikipedia.org/w/api.php', {
"action": "query",
+
    anonymous: true,
"format": "json",
+
    parameters: { origin: '*' },
"list": "random",
+
    ajax: { timeout: 10000 }
"rnnamespace": "0",
+
    } );
"rnlimit": "10"
 
} ).then( function( data ) {
 
// We process here the result of our request
 
// By adding each page title to our list
 
var i;
 
for ( var i=0; i < data.query.random.length; i++ ) {
 
generator.list.push( data.query.random[ i ].title );
 
}
 
  
// And once we're done we can resolve our promise
+
    // To get 10 random pages from the main namespace
// So the process can end
+
    this.frwikiApi.get( {
generator.deferred.resolve();
+
    "action": "query",
} ).fail( function ( error ) {
+
    "format": "json",
// If something goes wrong, we reject our promise
+
    "list": "random",
// So the process stops and our popup shows the error message
+
    "rnnamespace": "0",
generator.deferred.reject( new OO.ui.Error( error ) );
+
    "rnlimit": "10"
} );
+
    } ).then( function( data ) {
 +
    // We process here the result of our request
 +
    // By adding each page title to our list
 +
    var i;
 +
    for ( var i=0; i < data.query.random.length; i++ ) {
 +
    generator.list.push( data.query.random[ i ].title );
 +
    }
  
// At this point we're not done yet, make the dialog closing process
+
    // And once we're done we can resolve our promise
// to wait the promise to be resolved or rejected
+
    // So the process can end
return this.deferred.promise();
+
    generator.deferred.resolve();
};
+
    } ).fail( function ( error ) {
 +
    // If something goes wrong, we reject our promise
 +
    // So the process stops and our popup shows the error message
 +
    generator.deferred.reject( new OO.ui.Error( error ) );
 +
    } );
  
}( mediaWiki, jQuery, mediaWiki.recordWizard, OO ) );
+
    // At this point we're not done yet, make the dialog closing process
 +
    // to wait the promise to be resolved or rejected
 +
    return this.deferred.promise();
 +
    };
 +
    } );
 +
}

Latest revision as of 20:00, 2 June 2022

/* ************************************************************************** */
// Description: Adds a personal generator of words list to the RecordWizard
// Usage: Copy this code into you User:Name/common.js, hack it as you need to.
// See also : [[Help:Create a new generator]]

'use strict';

if ( mw.config.get( 'wgCanonicalSpecialPageName' ) === 'RecordWizard' ) {
    mw.loader.using( ['oojs', 'oojs-ui', 'ext.recordWizard.generator'], function() {
    	console.log('MediaWiki:Gadget-Demo.js');
        var rw = mw.recordWizard;

	    /*
	     * This first part allows the creation of a basic generator
	     * and allows the RecordWizard to find it (and thus, add a button in the UI for it)
	     *
	     * Replace everywhere in this file "rw.generator.Demo" by a custom
	     * and unique class name, but always in the "rw.generator" namespace.
	     * For example it can be: rw.generator.SomethingCool
	     */
	    rw.generator.Demo = function ( config ) {
		    rw.generator.Generator.call( this, config );
	    };

	    OO.inheritClass( rw.generator.Demo, rw.generator.Generator );

	    // This line defines an internal name for the generator
	    rw.generator.Demo.static.name = 'demo';

	    // And this one defines the name for the generator which will be displayed in the UI
	    rw.generator.Demo.static.title = 'My Demo';

	    /*
	     * This function should contain all the popup initialization stuff.
	     * We create here for example two text fields, with a custom layout.
	     * For more information, see OOui documentation:
	     * https://www.mediawiki.org/wiki/OOUI
	     */
	    rw.generator.Demo.prototype.initialize = function () {
		    // The two text fields
		    this.aTextField = new OO.ui.TextInputWidget();
		    this.anotherTextField = new OO.ui.TextInputWidget();

		    // The custom layout
		    this.layout = new OO.ui.Widget( {
			    content: [
				    new OO.ui.FieldLayout( this.aTextField, {
						    align: 'top',
						    label: 'This field do something cool.'
					    }
				    ),
				    new OO.ui.FieldLayout(
					    this.anotherTextField, {
						    align: 'top',
						    label: 'This one too!'
					    }
				    )
			    ]
		    } );

		    // To be displayed, all the fields/widgets/... should be appended to "this.content.$element"
		    this.content.$element.append( this.layout.$element );

		    // Do not remove this line, it will initialize the popup itself
		    rw.generator.Generator.prototype.initialize.call( this );
	    };

	    /*
	     * This function will be called when the user press the "Done" button.
	     * 
	     * Every words that you want to be added to the RecordWizard's word list
	     * have to be added to an array called "this.list".
	     *
	     * The returned value can either be True, or if you want to do some asynchrone
	     * stuff, you can return a jQuery promise
	     */
	    rw.generator.Demo.prototype.fetch = function () {
		    // Get the values of our text fields
		    var generator = this,
			    demoText = this.aTextField.getValue(),
			    anotherDemoText = this.anotherTextField.getValue();

		    // Initialize a new promise
		    this.deferred = $.Deferred();

		    // Initialize our word list
		    this.list = [];

		    // We will do a asynchronous AJAX request to frwiki's API
		    this.frwikiApi = new mw.ForeignApi( 'https://fr.wikipedia.org/w/api.php', {
			    anonymous: true,
			    parameters: { origin: '*' },
			    ajax: { timeout: 10000 }
		    } );

		    // To get 10 random pages from the main namespace
		    this.frwikiApi.get( {
			    "action": "query",
			    "format": "json",
			    "list": "random",
			    "rnnamespace": "0",
			    "rnlimit": "10"
		    } ).then( function( data ) {
			    // We process here the result of our request
			    // By adding each page title to our list
			    var i;
			    for ( var i=0; i < data.query.random.length; i++ ) {
				    generator.list.push( data.query.random[ i ].title );
			    }

			    // And once we're done we can resolve our promise
			    // So the process can end
			    generator.deferred.resolve();
		    } ).fail( function ( error ) {
			    // If something goes wrong, we reject our promise
			    // So the process stops and our popup shows the error message
			    generator.deferred.reject( new OO.ui.Error( error ) );
		    } );

		    // At this point we're not done yet, make the dialog closing process
		    // to wait the promise to be resolved or rejected
		    return this.deferred.promise();
	    };
    } );
}