MediaWiki:Common.js: Difference between revisions
(Created page with "// 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: 'Wikidata item' } ), { label: 'Wikidata item', al...") |
(console log; changing label) |
||
Line 10: | Line 10: | ||
fieldLayout = new OO.ui.FieldLayout( new OO.ui.TextInputWidget( { | fieldLayout = new OO.ui.FieldLayout( new OO.ui.TextInputWidget( { | ||
id: 'wikidataInput', | id: 'wikidataInput', | ||
placeholder: ' | placeholder: 'Q123' | ||
} ), { | } ), { | ||
label: 'Wikidata | label: 'Quick-add Wikidata ID', | ||
align: 'top' | align: 'top' | ||
} ); | } ); | ||
Line 40: | Line 40: | ||
} ).fail( function () { | } ).fail( function () { | ||
// If failed | // If failed | ||
console.log(); | |||
alert( 'Failed to create a new statement.' ); | alert( 'Failed to create a new statement.' ); | ||
} ); | } ); |
Revision as of 00:38, 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
$( '#mw-content-text' ).prepend( formPanel.$element );
});
}