User:GZWDer/newentity.js

From Wikidata
Jump to navigation Jump to search

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)
  • Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5.
//<syntaxhighlight lang="javascript">
mw.loader.using( ['mediawiki.language','jquery.ui','mediawiki.api','mediawiki.language','mediawiki.util'] ).then( function ( mw, $ ) {
	'use strict';/*
	var i18nData = {
		'en': {
			'create-title': 'Create entity',
			'create-button': 'Create'
		},
	};

	var chain = mw.language.getFallbackLanguageChain(),
		len = chain.length,
		messages = {},
		i;
	for ( i = len - 1; i >= 0; i-- ) {
		if ( i18nData.hasOwnProperty( chain[ i ] ) ) {
			$.extend( messages, i18nData[ chain[ i ] ] );
		}
	}

	mw.messages.set( messages );
*/
	mw.messages.set( {
			'create-title': 'Create entity',
			'create-button': 'Create'
		});

	/**
	 * Called on error
	 */
	function onError() {
		$.removeSpinner( 'CreateEntity' );
		$( '#create-button' ).button( 'option', 'disabled', false );
	}

	/**
	 * Called on API error
	 */
	function onApiError( jqXHR, textStatus, errorThrown ) {
		alert( mw.msg( 'error-api' ) );
		onError();
	}

	function createEntity (entitydata) {
		var entitytype='item';
		if (JSON.parse(entitydata).datatype) entitytype='property';
		if (JSON.parse(entitydata).lexicalCategory) entitytype='lexeme';
		new mw.Api().postWithToken( 'csrf', {
			action: 'wbeditentity',
			'new': entitytype,
			data: entitydata
		} )
		.done( function ( data ) {
			if ( data.entity) {
		    	$( '#CreatedEntities' ).append("Created " + data.entity.id+"<br/>");
			} else {
				onApiError();
			}
		} )
		.fail( onApiError );
     }

	function work() {
		new mw.Api().get( {
			action: 'expandtemplates',
			prop: 'wikitext',
			text: $( '#entitydata' ).val()
		} )
		.done( function ( data ) {
			if ( data.expandtemplates) {
				createEntity ( data.expandtemplates.wikitext );
			} else {
				onApiError();
			}
		}  )
		.fail( onApiError );
	}

	/**
	 * Show the dialog
	 */
	function show() {
		$( '#CreateEntity-error' ).html( '' );
		$( 'div#CreateEntity' ).dialog( 'open' );
		return false;
	}

	/**
	 * Create the dialog and add a link in toolBox
	 */
	function init() {
		mw.loader.using( ['jquery.ui'],function(){$( '<div>' ).attr( {
			id: 'CreateEntity',
			title: mw.msg( 'create-title' )
		} ).append(
			$( '<form>' ).append(
				$( '<textarea id="entitydata">' ),
				$( '<p class="error" id="CreateEntity-error">' ),
				$( '<p id="CreatedEntities">' )
			)
		)
		.appendTo( '#content' )
		.dialog( {
			autoOpen: false,
			modal: true,
			width: 500,
			buttons: [ {
				id: 'createEntity-create-button',
				text: mw.msg( 'create-button' ),
				click: work
			} ]
		} );

		var portletLink = mw.util.addPortletLink( 'p-tb', '#', mw.msg( 'create-title' ), 't-CreateEntity', mw.msg( 'create-title' ) );
		$( portletLink ).click( show );});
	}

	$( init );
} ( mediaWiki, jQuery ) );
//</syntaxhighlight>