Import: Handle uploads with sha1 starting with 0 properly
[mediawiki.git] / resources / src / mediawiki.widgets / mw.widgets.TitleInputWidget.js
blobfc1007e4505667b200f36996d1f9fb8c3ab0645d
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          * @cfg {boolean} [suggestions=true] Display search suggestions
19          */
20         mw.widgets.TitleInputWidget = function MwWidgetsTitleInputWidget( config ) {
21                 config = config || {};
23                 // Parent constructor
24                 mw.widgets.TitleInputWidget.parent.call( this, $.extend( {}, config, {
25                         validate: this.isQueryValid.bind( this ),
26                         autocomplete: false
27                 } ) );
29                 // Mixin constructors
30                 mw.widgets.TitleWidget.call( this, config );
31                 OO.ui.mixin.LookupElement.call( this, config );
33                 // Properties
34                 this.suggestions = config.suggestions !== undefined ? config.suggestions : true;
36                 // Initialization
37                 this.$element.addClass( 'mw-widget-titleInputWidget' );
38                 this.lookupMenu.$element.addClass( 'mw-widget-titleWidget-menu' );
39                 if ( this.showImages ) {
40                         this.lookupMenu.$element.addClass( 'mw-widget-titleWidget-menu-withImages' );
41                 }
42                 if ( this.showDescriptions ) {
43                         this.lookupMenu.$element.addClass( 'mw-widget-titleWidget-menu-withDescriptions' );
44                 }
45                 this.setLookupsDisabled( !this.suggestions );
46         };
48         /* Setup */
50         OO.inheritClass( mw.widgets.TitleInputWidget, OO.ui.TextInputWidget );
51         OO.mixinClass( mw.widgets.TitleInputWidget, mw.widgets.TitleWidget );
52         OO.mixinClass( mw.widgets.TitleInputWidget, OO.ui.mixin.LookupElement );
54         /* Methods */
56         /**
57          * @inheritdoc mw.widgets.TitleWidget
58          */
59         mw.widgets.TitleInputWidget.prototype.getQueryValue = function () {
60                 return this.getValue();
61         };
63         /**
64          * @inheritdoc mw.widgets.TitleWidget
65          */
66         mw.widgets.TitleInputWidget.prototype.setNamespace = function ( namespace ) {
67                 // Mixin method
68                 mw.widgets.TitleWidget.prototype.setNamespace.call( this, namespace );
70                 this.lookupCache = {};
71                 this.closeLookupMenu();
72         };
74         /**
75          * @inheritdoc
76          */
77         mw.widgets.TitleInputWidget.prototype.getLookupRequest = function () {
78                 return this.getSuggestionsPromise();
79         };
81         /**
82          * @inheritdoc OO.ui.mixin.LookupElement
83          */
84         mw.widgets.TitleInputWidget.prototype.getLookupCacheDataFromResponse = function ( response ) {
85                 return response.query || {};
86         };
88         /**
89          * @inheritdoc OO.ui.mixin.LookupElement
90          */
91         mw.widgets.TitleInputWidget.prototype.getLookupMenuOptionsFromData = function ( response ) {
92                 return this.getOptionsFromData( response );
93         };
95         /**
96          * @inheritdoc
97          */
98         mw.widgets.TitleInputWidget.prototype.onLookupMenuItemChoose = function ( item ) {
99                 this.closeLookupMenu();
100                 this.setLookupsDisabled( true );
101                 this.setValue( item.getData() );
102                 this.setLookupsDisabled( !this.suggestions );
103         };
105         /**
106          * @inheritdoc
107          */
108         mw.widgets.TitleInputWidget.prototype.focus = function () {
109                 var retval;
111                 // Prevent programmatic focus from opening the menu
112                 this.setLookupsDisabled( true );
114                 // Parent method
115                 retval = mw.widgets.TitleInputWidget.parent.prototype.focus.apply( this, arguments );
117                 this.setLookupsDisabled( !this.suggestions );
119                 return retval;
120         };
122         /**
123          * @inheritdoc
124          */
125         mw.widgets.TitleInputWidget.prototype.cleanUpValue = function ( value ) {
126                 var widget = this;
128                 // Parent method
129                 value = mw.widgets.TitleInputWidget.parent.prototype.cleanUpValue.call( this, value );
131                 return $.trimByteLength( this.value, value, this.maxLength, function ( value ) {
132                         var title = widget.getTitle( value );
133                         return title ? title.getMain() : value;
134                 } ).newVal;
135         };
137 }( jQuery, mediaWiki ) );