MediaWiki:Common.js

From Librarybase

Note: After publishing, 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: Press Ctrl-F5.
// Quick-add Wikidata ID

// Check if we're on an Item: page
if (mw.config.get('wgNamespaceNumber') === 120) {
    // Import OOUI
    mw.loader.using( 'oojs-ui' ).done( function () {
        var fieldLayout, formPanel, fieldset;

        // Create a FieldLayout
        fieldLayout = new OO.ui.FieldLayout( new OO.ui.TextInputWidget( {
            id: 'wikidataInput',
            placeholder: 'Q123'
        } ), {
            label: 'Quick-add Wikidata ID',
            align: 'top'
        } );

        // Create a button widget
        var buttonWidget = new OO.ui.ButtonWidget( {
            label: 'Submit',
            icon: 'check',
            flags: [ 'primary', 'progressive' ],
        } );

        buttonWidget.on( 'click', function () {
            var wikidataId = $('#wikidataInput input').val().trim();
            var url = "http://www.wikidata.org/entity/" + wikidataId;

            // Post action
            new mw.Api().postWithToken( 'csrf', {
                action: 'wbcreateclaim',
                format: 'json',
                entity: mw.config.get('wgTitle'),
                property: 'P1',
                snaktype: 'value',
                value: JSON.stringify(url)
            } ).done( function ( data ) {
                // If successful
                location.reload(); // Refresh the page
            } ).fail( function ( jqXHR, textStatus, errorThrown ) {
                // If failed
                console.log(jqXHR);
                console.log(textStatus);
                console.log(errorThrown);
                alert( 'Failed to create a new statement.' );
            } );

        } );

        // Add the field layout and button to a fieldset
        fieldset = new OO.ui.FieldsetLayout();
        fieldset.addItems( [ fieldLayout, buttonWidget ] );

        // Add the fieldset to a panel
        formPanel = new OO.ui.PanelLayout( { padded: false, expanded: false } );
        formPanel.$element.append( fieldset.$element );

        // Add the form to the page
        $( '.wikibase-entitytermsview-entitytermsforlanguagelistview' ).append( formPanel.$element );
    });
}