MediaWiki:Common.js: Difference between revisions
(console log; changing label) |
(moving quick-add) |
||
Line 55: | Line 55: | ||
// Add the form to the page | // Add the form to the page | ||
$( ' | $( '.wikibase-entitytermsview-entitytermsforlanguagelistview' ).append( formPanel.$element ); | ||
}); | }); | ||
} | } |
Revision as of 00:40, 8 July 2023
// 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').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({ "text": url, "type": "url" })
} ).done( function ( data ) {
// If successful
location.reload(); // Refresh the page
} ).fail( function () {
// If failed
console.log();
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: true, expanded: false } );
formPanel.$element.append( fieldset.$element );
// Add the form to the page
$( '.wikibase-entitytermsview-entitytermsforlanguagelistview' ).append( formPanel.$element );
});
}