Merge "Added release notes for 'ContentHandler::runLegacyHooks' removal"
[mediawiki.git] / resources / src / mediawiki.widgets / mw.widgets.TitleInputWidget.js
bloba78ad82b4ab7a5730e892a4b314af13306cef072
1 /*!
2  * MediaWiki Widgets - TitleInputWidget class.
3  *
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.
11          *
12          * @class
13          * @extends OO.ui.TextInputWidget
14          * @mixins mw.widgets.TitleWidget
15          * @mixins OO.ui.mixin.LookupElement
16          *
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
21          */
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' );
43                 }
44                 if ( this.showDescriptions ) {
45                         this.lookupMenu.$element.addClass( 'mw-widget-titleWidget-menu-withDescriptions' );
46                 }
47                 this.setLookupsDisabled( !this.suggestions );
48         };
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
60          */
61         mw.widgets.TitleInputWidget.prototype.getQueryValue = function () {
62                 return this.getValue();
63         };
65         /**
66          * @inheritdoc mw.widgets.TitleWidget
67          */
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();
74         };
76         /**
77          * @inheritdoc
78          */
79         mw.widgets.TitleInputWidget.prototype.getLookupRequest = function () {
80                 return this.getSuggestionsPromise();
81         };
83         /**
84          * @inheritdoc OO.ui.mixin.LookupElement
85          */
86         mw.widgets.TitleInputWidget.prototype.getLookupCacheDataFromResponse = function ( response ) {
87                 return response.query || {};
88         };
90         /**
91          * @inheritdoc OO.ui.mixin.LookupElement
92          */
93         mw.widgets.TitleInputWidget.prototype.getLookupMenuOptionsFromData = function ( response ) {
94                 return this.getOptionsFromData( response );
95         };
97         /**
98          * @inheritdoc
99          */
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 );
105         };
107         /**
108          * @inheritdoc
109          */
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;
122         };
124         /**
125          * @inheritdoc
126          */
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;
137         };
139 }( jQuery, mediaWiki ) );