MediaWiki:Gadget-CreateNewItem.js

Revision as of 15:28, 15 February 2022 by Admin (talk | contribs) (Created page with "→‎* * Add a "create new item" link in the dropdown menu for when you want to * add an item to a property, but the item does not exist. * * https://phabricator.wikimedia.org/T107693 * https://www.wikidata.org/wiki/User:Efly/Gadget-CreateNewItem.js * @author [[User:Efly]]: ( function ( $, mw ) { 'use strict'; // To translators: // $1 is the original error message (e.g. "No match was found.") // $2 is the add new item link's text in the sidebar of the page (e...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Go to top

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.
/**
 * Add a "create new item" link in the dropdown menu for when you want to
 * add an item to a property, but the item does not exist.
 *
 * https://phabricator.wikimedia.org/T107693
 * https://www.wikidata.org/wiki/User:Efly/Gadget-CreateNewItem.js
 * @author [[User:Efly]]
 */

( function ( $, mw ) {
	'use strict';

	// To translators:
	// $1 is the original error message (e.g. "No match was found.")
	// $2 is the add new item link's text in the sidebar of the page (e.g. "Create a new item")
	switch ( mw.config.get( 'wgUserLanguage' ) ) {
		default:
		case 'en':
			mw.messages.set( {
				'not-found-message': '$1. $2...'
			} );
			break;
		case 'nl':
			mw.messages.set( {
				'not-found-message': '$1. $2...'
			} );
			break;
		case 'zh':
		case 'zh-hans':
		case 'zh-cn':
		case 'zh-sg':
		case 'zh-my':
			mw.messages.set( {
				'not-found-message': '$1。$2...'
			} );
			break;
		case 'zh-hant':
		case 'zh-tw':
		case 'zh-hk':
		case 'zh-mo':
			mw.messages.set( {
				'not-found-message': '$1。$2...'
			} );
			break;
	}

	function openNewItemPopup( currentField ) {
		// .submit( requestDeletion )
		var currentLabel = currentField.val();
		// console.log("openNewItemPopup");
		mw.loader.using( 'oojs-ui-core' ).done( function () {
			$( function () {
				$.get( '/wiki/Special:NewItem?label=' + currentLabel, function ( data ) {
					var popupWindow = $( '<div>' )
						.attr( 'id', 'createnewitem' )
						.append( $( data ).find( '#bodyContent' ) )
						.find( 'form' ).attr( 'target', '_blank' )
						.find( ':submit' ).on( 'click', function () {
						// console.log("Close popup");
							popup.toggle( false );
							/* console.log(currentLabel);
							currentField.val("Creating New Entity...");
							window.setTimeout(function () {
								currentField.val(currentLabel);
							}, 3000);*/
							return true;
						} )
						.closest( '#createnewitem' );

					var popup = new OO.ui.PopupWidget( {
						$content: popupWindow,
						padded: true,
						width: currentField.width() + 200,
						head: true
					} );
					popup.$element.attr( 'style', 'z-index: 9999999;' );
					currentField.parent().append( popup.$element );
					popup.toggle( true );
				} );
			} );
		} );
	}

	function init() {
		var currentField = $( 'input' );
		var willEntitySelectorListUpdate = false;
		$( document ).on( 'input propertychange paste', '.wikibase-snakview-value-container:has(.ui-suggester-input)', function () {
			currentField = $( this ).find( '.ui-suggester-input' ).first();
			// console.log("Entering: "+currentFieldText);
			willEntitySelectorListUpdate = true;
		} );
		$( document ).on( 'DOMSubtreeModified', '.ui-entityselector-list', function () {
			if ( willEntitySelectorListUpdate ) {
				var firstLi = $( this ).find( 'li' ).first();
				var isNotFound = firstLi.hasClass( 'ui-entityselector-notfound' );
				// console.log('Current Text Field content: '+currentField.val()+". Not found: "+isNotFound);
				if ( isNotFound ) {
					willEntitySelectorListUpdate = false;
					var $innerA = firstLi.find( 'a' ).first();
					$innerA.on( 'click', function () {
						firstLi.remove();
						openNewItemPopup( currentField, firstLi );
						return false;
					} );
					$innerA.text( mw.message( 'not-found-message', $innerA.text(), $( '#n-special-newitem' ).text() ) );
				}
			}
		} );
	}

	$( function () {
		mw.hook( 'wikibase.entityPage.entityLoaded' ).add( init );
	} );

}( jQuery, mediaWiki ) );