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
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
2014-06-20 00:21:42 +00:00
|
|
|
* @cfg {number} [namespace] Namespace to prepend to queries
|
2013-06-18 21:24:16 +00:00
|
|
|
*/
|
|
|
|
ve.ui.MWTitleInputWidget = function VeUiMWTitleInputWidget( config ) {
|
2014-11-21 13:00:50 +00:00
|
|
|
// Config initialization
|
2013-06-18 21:24:16 +00:00
|
|
|
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-08-22 20:50:48 +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
|
2014-11-20 01:02:44 +00:00
|
|
|
* @param {OO.ui.MenuOptionWidget} 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 ) {
|
2014-10-31 19:08:02 +00:00
|
|
|
this.closeLookupMenu();
|
2013-06-18 21:24:16 +00:00
|
|
|
if ( item ) {
|
2014-10-31 19:08:02 +00:00
|
|
|
this.setLookupsDisabled( true );
|
2013-06-18 21:24:16 +00:00
|
|
|
this.setValue( item.getData() );
|
2014-10-31 19:08:02 +00:00
|
|
|
this.setLookupsDisabled( false );
|
2013-06-18 21:24:16 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2014-10-31 19:08:02 +00:00
|
|
|
* @inheritdoc
|
2013-06-18 21:24:16 +00:00
|
|
|
*/
|
|
|
|
ve.ui.MWTitleInputWidget.prototype.getLookupRequest = function () {
|
|
|
|
var value = this.value;
|
|
|
|
|
|
|
|
// Prefix with default namespace name
|
2014-06-20 00:21:42 +00:00
|
|
|
if ( this.namespace !== null && mw.Title.newFromText( value, this.namespace ) ) {
|
|
|
|
value = mw.Title.newFromText( value, this.namespace ).getPrefixedText();
|
2013-06-18 21:24:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Dont send leading ':' to open search
|
|
|
|
if ( value.charAt( 0 ) === ':' ) {
|
|
|
|
value = value.substr( 1 );
|
|
|
|
}
|
|
|
|
|
2014-05-23 09:57:58 +00:00
|
|
|
return ve.init.target.constructor.static.apiRequest( {
|
2014-08-22 20:50:48 +00:00
|
|
|
action: 'opensearch',
|
|
|
|
search: value,
|
|
|
|
suggest: ''
|
2013-06-18 21:24:16 +00:00
|
|
|
} );
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2014-10-31 19:08:02 +00:00
|
|
|
* @inheritdoc
|
2013-06-18 21:24:16 +00:00
|
|
|
*/
|
|
|
|
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
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2014-10-31 19:08:02 +00:00
|
|
|
* @inheritdoc
|
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 = [],
|
2014-09-09 22:08:14 +00:00
|
|
|
matchingPages = data,
|
|
|
|
linkCacheUpdate = {};
|
2013-06-18 21:24:16 +00:00
|
|
|
|
|
|
|
// Matching pages
|
|
|
|
if ( matchingPages && matchingPages.length ) {
|
|
|
|
for ( i = 0, len = matchingPages.length; i < len; i++ ) {
|
|
|
|
title = new mw.Title( matchingPages[i] );
|
2014-09-09 22:08:14 +00:00
|
|
|
linkCacheUpdate[matchingPages[i]] = { missing: false };
|
2014-07-07 19:36:38 +00:00
|
|
|
if ( this.namespace !== null ) {
|
2014-09-22 17:48:42 +00:00
|
|
|
value = title.getRelativeText( this.namespace );
|
2013-06-18 21:24:16 +00:00
|
|
|
} else {
|
|
|
|
value = title.getPrefixedText();
|
|
|
|
}
|
2014-11-22 01:40:00 +00:00
|
|
|
items.push( new OO.ui.MenuOptionWidget( {
|
|
|
|
$: menu$,
|
|
|
|
data: value,
|
|
|
|
label: value
|
|
|
|
} ) );
|
2013-06-18 21:24:16 +00:00
|
|
|
}
|
2014-09-09 22:08:14 +00:00
|
|
|
ve.init.platform.linkCache.set( linkCacheUpdate );
|
2013-06-18 21:24:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return items;
|
|
|
|
};
|
2014-10-28 22:12:22 +00:00
|
|
|
|
|
|
|
/**
|
2014-10-31 19:08:02 +00:00
|
|
|
* Get title object corresponding to #getValue
|
2014-10-28 22:12:22 +00:00
|
|
|
*
|
2014-10-31 19:08:02 +00:00
|
|
|
* @returns {mw.Title|null} Title object, or null if value is invalid
|
2014-10-28 22:12:22 +00:00
|
|
|
*/
|
|
|
|
ve.ui.MWTitleInputWidget.prototype.getTitle = function () {
|
|
|
|
var title = this.getValue(),
|
2014-10-30 16:24:04 +00:00
|
|
|
//mw.Title doesn't handle null well
|
|
|
|
titleObj = mw.Title.newFromText( title, this.namespace !== null ? this.namespace : undefined );
|
2014-10-28 22:12:22 +00:00
|
|
|
|
|
|
|
return titleObj;
|
|
|
|
};
|
2014-10-28 23:38:28 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritdoc
|
|
|
|
*/
|
|
|
|
ve.ui.MWTitleInputWidget.prototype.isValid = function () {
|
|
|
|
return $.Deferred().resolve( !!this.getTitle() ).promise();
|
|
|
|
};
|