Merge "Update tests/phpunit/MediaWikiTestCase.php with support for apcu"
[mediawiki.git] / resources / src / mediawiki.widgets / mw.widgets.TitleInputWidget.js
bloba78ad82b4ab7a5730e892a4b314af13306cef072
1 /*!
2 * MediaWiki Widgets - TitleInputWidget class.
4 * @copyright 2011-2015 MediaWiki Widgets Team and others; see AUTHORS.txt
5 * @license The MIT License (MIT); see LICENSE.txt
6 */
7 ( function ( $, mw ) {
9 /**
10 * Creates an mw.widgets.TitleInputWidget object.
12 * @class
13 * @extends OO.ui.TextInputWidget
14 * @mixins mw.widgets.TitleWidget
15 * @mixins OO.ui.mixin.LookupElement
17 * @constructor
18 * @param {Object} [config] Configuration options
19 * @cfg {boolean} [suggestions=true] Display search suggestions
20 * @cfg {RegExp|Function|string} [validate] Perform title validation
22 mw.widgets.TitleInputWidget = function MwWidgetsTitleInputWidget( config ) {
23 config = config || {};
25 // Parent constructor
26 mw.widgets.TitleInputWidget.parent.call( this, $.extend( {}, config, {
27 validate: config.validate !== undefined ? config.validate : this.isQueryValid.bind( this ),
28 autocomplete: false
29 } ) );
31 // Mixin constructors
32 mw.widgets.TitleWidget.call( this, config );
33 OO.ui.mixin.LookupElement.call( this, config );
35 // Properties
36 this.suggestions = config.suggestions !== undefined ? config.suggestions : true;
38 // Initialization
39 this.$element.addClass( 'mw-widget-titleInputWidget' );
40 this.lookupMenu.$element.addClass( 'mw-widget-titleWidget-menu' );
41 if ( this.showImages ) {
42 this.lookupMenu.$element.addClass( 'mw-widget-titleWidget-menu-withImages' );
44 if ( this.showDescriptions ) {
45 this.lookupMenu.$element.addClass( 'mw-widget-titleWidget-menu-withDescriptions' );
47 this.setLookupsDisabled( !this.suggestions );
50 /* Setup */
52 OO.inheritClass( mw.widgets.TitleInputWidget, OO.ui.TextInputWidget );
53 OO.mixinClass( mw.widgets.TitleInputWidget, mw.widgets.TitleWidget );
54 OO.mixinClass( mw.widgets.TitleInputWidget, OO.ui.mixin.LookupElement );
56 /* Methods */
58 /**
59 * @inheritdoc mw.widgets.TitleWidget
61 mw.widgets.TitleInputWidget.prototype.getQueryValue = function () {
62 return this.getValue();
65 /**
66 * @inheritdoc mw.widgets.TitleWidget
68 mw.widgets.TitleInputWidget.prototype.setNamespace = function ( namespace ) {
69 // Mixin method
70 mw.widgets.TitleWidget.prototype.setNamespace.call( this, namespace );
72 this.lookupCache = {};
73 this.closeLookupMenu();
76 /**
77 * @inheritdoc
79 mw.widgets.TitleInputWidget.prototype.getLookupRequest = function () {
80 return this.getSuggestionsPromise();
83 /**
84 * @inheritdoc OO.ui.mixin.LookupElement
86 mw.widgets.TitleInputWidget.prototype.getLookupCacheDataFromResponse = function ( response ) {
87 return response.query || {};
90 /**
91 * @inheritdoc OO.ui.mixin.LookupElement
93 mw.widgets.TitleInputWidget.prototype.getLookupMenuOptionsFromData = function ( response ) {
94 return this.getOptionsFromData( response );
97 /**
98 * @inheritdoc
100 mw.widgets.TitleInputWidget.prototype.onLookupMenuItemChoose = function ( item ) {
101 this.closeLookupMenu();
102 this.setLookupsDisabled( true );
103 this.setValue( item.getData() );
104 this.setLookupsDisabled( !this.suggestions );
108 * @inheritdoc
110 mw.widgets.TitleInputWidget.prototype.focus = function () {
111 var retval;
113 // Prevent programmatic focus from opening the menu
114 this.setLookupsDisabled( true );
116 // Parent method
117 retval = mw.widgets.TitleInputWidget.parent.prototype.focus.apply( this, arguments );
119 this.setLookupsDisabled( !this.suggestions );
121 return retval;
125 * @inheritdoc
127 mw.widgets.TitleInputWidget.prototype.cleanUpValue = function ( value ) {
128 var widget = this;
130 // Parent method
131 value = mw.widgets.TitleInputWidget.parent.prototype.cleanUpValue.call( this, value );
133 return $.trimByteLength( this.value, value, this.maxLength, function ( value ) {
134 var title = widget.getTitle( value );
135 return title ? title.getMain() : value;
136 } ).newVal;
139 }( jQuery, mediaWiki ) );