User

VIGNERON/common.js

< User:VIGNERON
Revision as of 12:31, 15 December 2018 by VIGNERON (talk | contribs) (first test)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Note: After saving, you may have to bypass your browser's cache to see the changes.

  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
  • Internet Explorer / Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5
  • Opera: Go to Menu → Settings (Opera → Preferences on a Mac) and then to Privacy & security → Clear browsing data → Cached images and files.
'use strict';

if ( mw.config.get( 'wgCanonicalSpecialPageName' ) === 'RecordWizard' ) {
    mw.loader.using( 'ext.recordWizard.generator', function() {
        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.Lexeme = function ( config ) {
		    rw.generator.Generator.call( this, config );
	    };

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

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

	    // And this one defines the name for the generator which will be displayed in the UI
	    rw.generator.Lexeme.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.Lexeme.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.Lexeme.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 = [];

		    // Call to the Query Service (query already built, part to improve later)
		    this.LexemeQuery = new ( 'https://query.wikidata.org/bigdata/namespace/wdq/sparql?format=json&query=SELECT%20DISTINCT%20%3FlexemeLabel%0AWITH%20{%0A%20%20SELECT%20%3Flexeme%20%3FlexemeLabel%20%3Flexical_category%20WHERE%20{%0A%20%20%20%20%3Flexeme%20a%20ontolex%3ALexicalEntry%20%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20dct%3Alanguage%20wd%3AQ12107%20%3B%20%0A%20%20%20%20%20%20%20%20%20%20%20%20wikibase%3Alemma%20%3FlexemeLabel%20.%0A%20%20%20%20OPTIONAL%20{%0A%20%20%20%20%20%20%3Flexeme%20wikibase%3AlexicalCategory%20%3Flexical_category%20.%0A%20%20%20%20}%0A%20%20}%0A}%20AS%20%25results%0AWHERE%20{%0A%20%20INCLUDE%20%25results%0A%20%20OPTIONAL%20{%20%20%20%20%20%20%20%20%0A%20%20%20%20%3Flexical_category%20rdfs%3Alabel%20%3Flexical_categoryLabel%20.%0A%20%20%20%20FILTER%20(LANG(%3Flexical_categoryLabel)%20%3D%20%22en%22)%0A%20%20}%0A}%0A%20%20' );

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