2013-06-18 21:24:16 +00:00
|
|
|
/*!
|
|
|
|
* VisualEditor UserInterface MWTitleInputWidget class.
|
|
|
|
*
|
2015-01-08 23:54:03 +00:00
|
|
|
* @copyright 2011-2015 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
|
2015-01-15 22:07:34 +00:00
|
|
|
* @mixins OO.ui.LookupElement
|
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
|
2015-01-15 22:07:34 +00:00
|
|
|
OO.ui.LookupElement.call( this, config );
|
2013-06-18 21:24:16 +00:00
|
|
|
|
|
|
|
// Properties
|
|
|
|
this.namespace = config.namespace || null;
|
|
|
|
|
|
|
|
// 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
|
|
|
|
2015-01-15 22:07:34 +00:00
|
|
|
OO.mixinClass( ve.ui.MWTitleInputWidget, OO.ui.LookupElement );
|
2013-06-18 21:24:16 +00:00
|
|
|
|
|
|
|
/* Methods */
|
|
|
|
|
|
|
|
/**
|
2015-01-15 22:07:34 +00:00
|
|
|
* @inheritdoc
|
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();
|
2015-03-27 10:51:06 +00:00
|
|
|
this.setLookupsDisabled( true );
|
|
|
|
this.setValue( item.getData() );
|
|
|
|
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 ) === ':' ) {
|
2014-12-06 19:11:02 +00:00
|
|
|
value = value.slice( 1 );
|
2013-06-18 21:24:16 +00:00
|
|
|
}
|
|
|
|
|
2015-01-24 00:22:17 +00:00
|
|
|
return new mw.Api().get( {
|
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
|
|
|
*/
|
2015-01-15 22:07:34 +00:00
|
|
|
ve.ui.MWTitleInputWidget.prototype.getLookupCacheDataFromResponse = 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
|
|
|
*/
|
2015-01-15 22:07:34 +00:00
|
|
|
ve.ui.MWTitleInputWidget.prototype.getLookupMenuOptionsFromData = function ( data ) {
|
2013-06-18 21:24:16 +00:00
|
|
|
var i, len, title, value,
|
|
|
|
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( {
|
|
|
|
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(),
|
2015-01-31 00:41:37 +00:00
|
|
|
// mw.Title doesn't handle null well
|
2014-10-30 16:24:04 +00:00
|
|
|
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();
|
|
|
|
};
|