2013-06-18 21:24:16 +00:00
|
|
|
/*!
|
|
|
|
* VisualEditor UserInterface MWTitleInputWidget class.
|
|
|
|
*
|
2014-01-05 12:05:05 +00:00
|
|
|
* @copyright 2011-2014 VisualEditor Team and others; see AUTHORS.txt
|
2013-06-18 21:24:16 +00:00
|
|
|
* @license The MIT License (MIT); see LICENSE.txt
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*global mw*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates an ve.ui.MWTitleInputWidget object.
|
|
|
|
*
|
|
|
|
* @class
|
2013-10-09 20:09:59 +00:00
|
|
|
* @extends OO.ui.TextInputWidget
|
|
|
|
* @mixins OO.ui.LookupInputWidget
|
2013-06-18 21:24:16 +00:00
|
|
|
*
|
|
|
|
* @constructor
|
2013-09-25 10:21:09 +00:00
|
|
|
* @param {Object} [config] Configuration options
|
2013-06-18 21:24:16 +00:00
|
|
|
* @param {number} [namespace] Namespace to prepend to queries not prefixed with ':'
|
|
|
|
*/
|
|
|
|
ve.ui.MWTitleInputWidget = function VeUiMWTitleInputWidget( config ) {
|
|
|
|
// Config intialization
|
|
|
|
config = config || {};
|
|
|
|
|
|
|
|
// Parent constructor
|
2013-10-09 20:09:59 +00:00
|
|
|
OO.ui.TextInputWidget.call( this, config );
|
2013-06-18 21:24:16 +00:00
|
|
|
|
|
|
|
// Mixin constructors
|
2013-10-09 20:09:59 +00:00
|
|
|
OO.ui.LookupInputWidget.call( this, this, config );
|
2013-06-18 21:24:16 +00:00
|
|
|
|
|
|
|
// Properties
|
|
|
|
this.namespace = config.namespace || null;
|
|
|
|
|
|
|
|
// Events
|
2014-04-07 19:15:01 +00:00
|
|
|
this.lookupMenu.connect( this, { 'choose': 'onLookupMenuItemChoose' } );
|
2013-06-18 21:24:16 +00:00
|
|
|
|
|
|
|
// Initialization
|
2013-11-01 19:45:59 +00:00
|
|
|
this.$element.addClass( 've-ui-mwTitleInputWidget' );
|
|
|
|
this.lookupMenu.$element.addClass( 've-ui-mwTitleInputWidget-menu' );
|
2013-06-18 21:24:16 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/* Inheritance */
|
|
|
|
|
2013-10-09 20:09:59 +00:00
|
|
|
OO.inheritClass( ve.ui.MWTitleInputWidget, OO.ui.TextInputWidget );
|
2013-06-18 21:24:16 +00:00
|
|
|
|
2013-10-09 20:09:59 +00:00
|
|
|
OO.mixinClass( ve.ui.MWTitleInputWidget, OO.ui.LookupInputWidget );
|
2013-06-18 21:24:16 +00:00
|
|
|
|
|
|
|
/* Methods */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle menu item select event.
|
|
|
|
*
|
|
|
|
* @method
|
2013-10-09 20:09:59 +00:00
|
|
|
* @param {OO.ui.MenuItemWidget} item Selected item
|
2013-06-18 21:24:16 +00:00
|
|
|
*/
|
2014-04-07 19:15:01 +00:00
|
|
|
ve.ui.MWTitleInputWidget.prototype.onLookupMenuItemChoose = function ( item ) {
|
2013-06-18 21:24:16 +00:00
|
|
|
if ( item ) {
|
|
|
|
this.setValue( item.getData() );
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets a new request object of the current lookup query value.
|
|
|
|
*
|
|
|
|
* @method
|
|
|
|
* @returns {jQuery.Deferred} Deferred object with success and fail handlers already attached
|
|
|
|
*/
|
|
|
|
ve.ui.MWTitleInputWidget.prototype.getLookupRequest = function () {
|
|
|
|
var value = this.value;
|
|
|
|
|
|
|
|
// Prefix with default namespace name
|
|
|
|
if ( this.namespace !== null && value.charAt( 0 ) !== ':' ) {
|
|
|
|
value = mw.config.get( 'wgFormattedNamespaces' )[this.namespace] + ':' + value;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Dont send leading ':' to open search
|
|
|
|
if ( value.charAt( 0 ) === ':' ) {
|
|
|
|
value = value.substr( 1 );
|
|
|
|
}
|
|
|
|
|
2014-01-05 04:44:13 +00:00
|
|
|
return ve.init.mw.Target.static.apiRequest( {
|
|
|
|
'action': 'opensearch',
|
|
|
|
'search': value,
|
|
|
|
'suggest': ''
|
2013-06-18 21:24:16 +00:00
|
|
|
} );
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get lookup cache item from server response data.
|
|
|
|
*
|
|
|
|
* @method
|
|
|
|
* @param {Mixed} data Response from server
|
|
|
|
*/
|
|
|
|
ve.ui.MWTitleInputWidget.prototype.getLookupCacheItemFromData = function ( data ) {
|
2014-02-09 22:03:29 +00:00
|
|
|
return data[1] || [];
|
2013-06-18 21:24:16 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get list of menu items from a server response.
|
|
|
|
*
|
|
|
|
* @param {Object} data Query result
|
2013-10-09 20:09:59 +00:00
|
|
|
* @returns {OO.ui.MenuItemWidget[]} Menu items
|
2013-06-18 21:24:16 +00:00
|
|
|
*/
|
|
|
|
ve.ui.MWTitleInputWidget.prototype.getLookupMenuItemsFromData = function ( data ) {
|
|
|
|
var i, len, title, value,
|
2013-11-01 19:45:59 +00:00
|
|
|
menu$ = this.lookupMenu.$,
|
2013-06-18 21:24:16 +00:00
|
|
|
items = [],
|
|
|
|
matchingPages = data;
|
|
|
|
|
|
|
|
// Matching pages
|
|
|
|
if ( matchingPages && matchingPages.length ) {
|
|
|
|
for ( i = 0, len = matchingPages.length; i < len; i++ ) {
|
|
|
|
title = new mw.Title( matchingPages[i] );
|
|
|
|
if ( this.namespace !== null ) {
|
|
|
|
value = title.getNamespaceId() === this.namespace ?
|
|
|
|
title.getNameText() : ':' + title.getPrefixedText();
|
|
|
|
} else {
|
|
|
|
value = title.getPrefixedText();
|
|
|
|
}
|
2013-10-09 20:09:59 +00:00
|
|
|
items.push( new OO.ui.MenuItemWidget(
|
2013-11-01 19:45:59 +00:00
|
|
|
value, { '$': menu$, 'label': value }
|
2013-06-18 21:24:16 +00:00
|
|
|
) );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return items;
|
|
|
|
};
|