Difference between revisions of "MediaWiki:Gadget-CreateNewItem.js"

From Wikibase Personal data
Jump to navigation Jump to search
(Blanked the page)
Tag: Blanking
 
(13 intermediate revisions by the same user not shown)
Line 1: Line 1:
 +
/**
 +
* 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
 +
*
 +
* @author [https://meta.wikimedia.org/wiki/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:
 +
mw.messages.set( {
 +
'not-found-message': '$1. Create new item...'
 +
} );
 +
break;
 +
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() {
 +
//                mw.notify( $('<span>init Gadget-CreateNewItem</span>') );
 +
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()
 +
"Create New Item"
 +
) );
 +
}
 +
}
 +
} );
 +
}
 +
 +
$( function () {
 +
mw.hook( 'wikibase.entityPage.entityLoaded' ).add( init );
 +
} );
 +
 +
}( jQuery, mediaWiki ) );

Latest revision as of 13:34, 15 October 2019

/**
 * 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
 *
 * @author [https://meta.wikimedia.org/wiki/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:
			mw.messages.set( {
				'not-found-message': '$1. Create new item...'
			} );
			break;
		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() {
//                 mw.notify( $('<span>init Gadget-CreateNewItem</span>') );
		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() 
"Create New Item"
) );
				}
			}
		} );
	}

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

}( jQuery, mediaWiki ) );