Debian Codegraph @

resources/src/mediawiki.widgets/mw.widgets.TitleSearchWidget.js

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