Merge "Fix small typos"
[lhc/web/wiklou.git] / resources / src / mediawiki.widgets / mw.widgets.TitleSearchWidget.js
1 /*!
2 * MediaWiki Widgets - TitleSearchWidget 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 () {
8
9 /**
10 * Creates an mw.widgets.TitleSearchWidget object.
11 *
12 * @class
13 * @extends OO.ui.SearchWidget
14 * @mixins OO.ui.mixin.RequestManager
15 * @mixins mw.widgets.TitleWidget
16 *
17 * @constructor
18 * @param {Object} [config] Configuration options
19 */
20 mw.widgets.TitleSearchWidget = function MwWidgetsTitleSearchWidget( config ) {
21 config = config || {};
22
23 // Parent constructor
24 mw.widgets.TitleSearchWidget.parent.call( this, config );
25
26 // Mixin constructors
27 mw.widgets.TitleWidget.call( this, config );
28 OO.ui.mixin.RequestManager.call( this, config );
29
30 this.query.setValidation( this.isQueryValid.bind( this ) );
31
32 // Events
33 this.results.connect( this, { choose: 'onTitleSearchResultsChoose' } );
34
35 // Initialization
36 this.$element.addClass( 'mw-widget-titleSearchWidget' );
37 this.results.$element.addClass( 'mw-widget-titleWidget-menu' );
38 if ( this.showImages ) {
39 this.results.$element.addClass( 'mw-widget-titleWidget-menu-withImages' );
40 }
41 if ( this.showDescriptions ) {
42 this.results.$element.addClass( 'mw-widget-titleWidget-menu-withDescriptions' );
43 }
44 if ( this.maxLength !== undefined ) {
45 this.getQuery().$input.attr( 'maxlength', this.maxLength );
46 }
47 };
48
49 /* Setup */
50
51 OO.inheritClass( mw.widgets.TitleSearchWidget, OO.ui.SearchWidget );
52 OO.mixinClass( mw.widgets.TitleSearchWidget, OO.ui.mixin.RequestManager );
53 OO.mixinClass( mw.widgets.TitleSearchWidget, mw.widgets.TitleWidget );
54
55 /* Methods */
56
57 /**
58 * @inheritdoc mw.widgets.TitleWidget
59 */
60 mw.widgets.TitleSearchWidget.prototype.getQueryValue = function () {
61 return this.getQuery().getValue();
62 };
63
64 /**
65 * Handle choose events from the result widget
66 *
67 * @param {OO.ui.OptionWidget} item Chosen item
68 */
69 mw.widgets.TitleSearchWidget.prototype.onTitleSearchResultsChoose = function ( item ) {
70 this.getQuery().setValue( item.getData() );
71 };
72
73 /**
74 * @inheritdoc
75 */
76 mw.widgets.TitleSearchWidget.prototype.onQueryChange = function () {
77 var widget = this;
78
79 this.getRequestData().done( function ( data ) {
80 if ( widget.query.isReadOnly() ) {
81 // The request object is always abortable, so just
82 // prevent the results from displaying
83 return;
84 }
85 // Parent method
86 mw.widgets.TitleSearchWidget.parent.prototype.onQueryChange.call( widget );
87 widget.results.addItems( widget.getOptionsFromData( data ) );
88 } );
89 };
90
91 /**
92 * @inheritdoc OO.ui.mixin.RequestManager
93 */
94 mw.widgets.TitleSearchWidget.prototype.getRequestQuery = function () {
95 return this.getQueryValue();
96 };
97 /**
98 * @inheritdoc OO.ui.mixin.RequestManager
99 */
100 mw.widgets.TitleSearchWidget.prototype.getRequest = function () {
101 return this.getSuggestionsPromise();
102 };
103 /**
104 * @inheritdoc OO.ui.mixin.RequestManager
105 */
106 mw.widgets.TitleSearchWidget.prototype.getRequestCacheDataFromResponse = function ( response ) {
107 return response.query || {};
108 };
109
110 /**
111 * Check if the widget is read-only.
112 *
113 * @return {boolean}
114 */
115 mw.widgets.TitleSearchWidget.prototype.isReadOnly = function () {
116 return this.query.isReadOnly();
117 };
118
119 /**
120 * Set the read-only state of the widget.
121 *
122 * @param {boolean} readOnly Make input read-only
123 * @chainable
124 * @return {mw.widgets.TitleSearchWidget} The widget, for chaining
125 */
126 mw.widgets.TitleSearchWidget.prototype.setReadOnly = function ( readOnly ) {
127 this.query.setReadOnly( readOnly );
128 if ( readOnly ) {
129 // Hide results
130 this.results.clearItems();
131 }
132 return this;
133 };
134
135 }() );