ميدياويكي:Gadget-VE-wditem-input.js

من أرابيكا، الموسوعة الحرة
اذهب إلى التنقل اذهب إلى البحث

ملاحظة: بعد الحفظ، قد يلزمك إفراغ الكاش لرؤية التغييرات.

/*!
 *  WdItemInputWidget class.
 *  by حبيشان@arwiki forked from media wiki files
 * @copyright 2011-2015 MediaWiki Widgets Team and others; see AUTHORS.txt
 * @license The MIT License (MIT); see LICENSE.txt
 */
( function () {

	/**
	 * Creates a mw.widgets.WdItemInputWidget object.
	 *
	 * @class
	 * @extends OO.ui.TextInputWidget
	 * @mixins OO.ui.mixin.LookupElement
	 *
	 * @constructor
	 * @param {Object} [config] Configuration options
	 * @cfg {number} [limit=10] Number of results to show
	 * @cfg {mw.Api} [api] API object to use, creates a default mw.Api instance if not specified
	 */
	mw.widgets.WdItemInputWidget = function MwWidgetsWdItemInputWidget( config ) {
		// Config initialization
		config = config || {};

		// Parent constructor
		mw.widgets.WdItemInputWidget.parent.call( this, $.extend( {}, config, { autocomplete: false } ) );

		// Mixin constructors
		OO.ui.mixin.LookupElement.call( this, config );

		// Properties
		this.limit = config.limit || 10;
		this.api =  config.api || new mw.ForeignApi('https://www.wikidata.org/w/api.php');
        

		// Initialization
		this.$element.addClass( 'mw-widget-titleInputWidget' );
		this.lookupMenu.$element.addClass( 'mw-widget-titleWidget-menu' );
        this.lookupMenu.$element.addClass( 'mw-widget-titleWidget-menu-withDescriptions' );
	};

	/* Setup */

	OO.inheritClass( mw.widgets.WdItemInputWidget, OO.ui.TextInputWidget );
	OO.mixinClass( mw.widgets.WdItemInputWidget, OO.ui.mixin.LookupElement );

	/* Methods */

	/**
	 * Handle menu item 'choose' event, updating the text input value to the value of the clicked item.
	 *
	 * @param {OO.ui.MenuOptionWidget} item Selected item
	 */
	mw.widgets.WdItemInputWidget.prototype.onLookupMenuChoose = function ( item ) {
		this.closeLookupMenu();
		this.setLookupsDisabled( true );
		this.setValue( item.getData() );
		this.setLookupsDisabled( false );
	};

	/**
	 * @inheritdoc
	 */
	mw.widgets.WdItemInputWidget.prototype.focus = function () {
		var retval;

		// Prevent programmatic focus from opening the menu
		this.setLookupsDisabled( true );

		// Parent method
		retval = mw.widgets.WdItemInputWidget.parent.prototype.focus.apply( this, arguments );

		this.setLookupsDisabled( false );

		return retval;
	};

	/**
	 * @inheritdoc
	 */
	mw.widgets.WdItemInputWidget.prototype.getLookupRequest = function () {
		return this.api.get({
			action: 'wbsearchentities',
			search: this.value,
			format: 'json',
			language: 'ar',
			uselang: 'ar',
			type: 'item',
			props: '',
			limit: this.limit
		} );
	};

	/**
	 * Get lookup cache item from server response data.
	 *
	 * @method
	 * @param {Mixed} response Response from server
	 * @return {Object}
	 */
	mw.widgets.WdItemInputWidget.prototype.getLookupCacheDataFromResponse = function ( response ) {
        return response.search || {};
	};

	/**
	 * Get list of menu items from a server response.
	 *
	 * @param {Object} data Query result
	 * @return {OO.ui.MenuOptionWidget[]} Menu items
	 */
	mw.widgets.WdItemInputWidget.prototype.getLookupMenuOptionsFromData = function ( data ) {
		var len, i, wd,
			items = [];

        console.log(data)

		for ( i = 0, len = data.length; i < len; i++ ) {
			wd = data[ i ] || {};
			items.push( new OO.ui.MenuOptionWidget( {
				label: wd.label,
				data: wd.id,
                description: wd.label
			} ) );
		}

		return items;
	};
	
	mw.hook( 've.activate' ).add( function() {
	ve.ui.MWParameterPage.prototype.createValueInputOrgin = ve.ui.MWParameterPage.prototype.createValueInput;
	
	ve.ui.MWParameterPage.prototype.createValueInput = function () {
		var type = this.parameter.getType(),
			value = this.parameter.getValue(),
			desc = this.parameter.getTemplate().spec.getParameterDescription(this.parameter.name,'ar')
			valueInputConfig = this.getDefaultInputConfig();
			console.log(desc)
		if ( type === 'string' && 
	        desc != null && desc.indexOf('wikidata-item') > -1 &&
			( value === '' || mw.Title.newFromText( value ) )
		) {
			valueInputConfig.validate = function ( val ) {
				return !!mw.Title.newFromText( val );
			};
			return new mw.widgets.WdItemInputWidget( ve.extendObject( {
				api: new mw.ForeignApi('https://www.wikidata.org/w/api.php')
			}, valueInputConfig ) );
	    }
		return this.createValueInputOrgin();
	};
	}); 
}() );