Debian Codegraph @

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

raw symbols SCIP
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 () {
8
9 const trimByteLength = require( 'mediawiki.String' ).trimByteLength;
10
11 /**
12 * @classdesc Title input widget.
13 *
14 * @class
15 * @extends OO.ui.TextInputWidget
16 * @mixes mw.widgets.TitleWidget
17 * @mixes OO.ui.mixin.LookupElement
18 *
19 * @constructor
20 * @description Create an mw.widgets.TitleInputWidget object.
21 * @param {Object} [config] Configuration options
22 * @param {boolean} [config.suggestions=true] Display search suggestions
23 * @param {RegExp|Function|string} [config.validate] Perform title validation
24 */
25 mw.widgets.TitleInputWidget = function MwWidgetsTitleInputWidget( config ) {
26 config = config || {};
27
28 // Parent constructor
29 mw.widgets.TitleInputWidget.super.call( this, Object.assign( {}, config, {
30 validate: config.validate !== undefined ? config.validate : this.isQueryValid.bind( this ),
31 autocomplete: false
32 } ) );
33
34 // Mixin constructors
35 mw.widgets.TitleWidget.call( this, config );
36 OO.ui.mixin.LookupElement.call( this, config );
37
38 // Properties
39 this.suggestions = config.suggestions !== undefined ? config.suggestions : true;
40
41 // Initialization
42 this.$element.addClass( 'mw-widget-titleInputWidget' );
43 this.lookupMenu.$element.addClass( 'mw-widget-titleWidget-menu' );
44 if ( this.showImages ) {
45 this.lookupMenu.$element.addClass( 'mw-widget-titleWidget-menu-withImages' );
46 }
47 if ( this.showDescriptions ) {
48 this.lookupMenu.$element.addClass( 'mw-widget-titleWidget-menu-withDescriptions' );
49 }
50 this.setLookupsDisabled( !this.suggestions );
51 };
52
53 /* Setup */
54
55 OO.inheritClass( mw.widgets.TitleInputWidget, OO.ui.TextInputWidget );
56 OO.mixinClass( mw.widgets.TitleInputWidget, mw.widgets.TitleWidget );
57 OO.mixinClass( mw.widgets.TitleInputWidget, OO.ui.mixin.LookupElement );
58
59 /* Methods */
60
61 /**
62 * @inheritdoc
63 */
64 mw.widgets.TitleInputWidget.prototype.getQueryValue = function () {
65 return this.getValue();
66 };
67
68 /**
69 * @inheritdoc
70 */
71 mw.widgets.TitleInputWidget.prototype.setNamespace = function ( namespace ) {
72 // Mixin method
73 mw.widgets.TitleWidget.prototype.setNamespace.call( this, namespace );
74
75 this.lookupCache = {};
76 this.closeLookupMenu();
77 };
78
79 /**
80 * @inheritdoc
81 */
82 mw.widgets.TitleInputWidget.prototype.getLookupRequest = function () {
83 return this.getSuggestionsPromise();
84 };
85
86 /**
87 * @inheritdoc
88 */
89 mw.widgets.TitleInputWidget.prototype.getLookupCacheDataFromResponse = function ( response ) {
90 return response.query || {};
91 };
92
93 /**
94 * @inheritdoc
95 */
96 mw.widgets.TitleInputWidget.prototype.getLookupMenuOptionsFromData = function ( response ) {
97 return this.getOptionsFromData( response );
98 };
99
100 /**
101 * Handle menu item 'choose' event, updating the text input value to the value of the clicked item.
102 *
103 * TODO: Replace this with an override of onLookupMenuChoose()
104 *
105 * @param {OO.ui.MenuOptionWidget} item Selected item
106 */
107 mw.widgets.TitleInputWidget.prototype.onLookupMenuChoose = function ( item ) {
108 this.closeLookupMenu();
109 this.setLookupsDisabled( true );
110 this.setValue( item.getData() );
111 this.setLookupsDisabled( !this.suggestions );
112 };
113
114 /**
115 * @inheritdoc
116 */
117 mw.widgets.TitleInputWidget.prototype.focus = function () {
118 // Prevent programmatic focus from opening the menu
119 this.setLookupsDisabled( true );
120
121 // Parent method
122 const retval = mw.widgets.TitleInputWidget.super.prototype.focus.apply( this, arguments );
123
124 this.setLookupsDisabled( !this.suggestions );
125
126 return retval;
127 };
128
129 /**
130 * @inheritdoc
131 */
132 mw.widgets.TitleInputWidget.prototype.cleanUpValue = function ( value ) {
133 // Parent method
134 value = mw.widgets.TitleInputWidget.super.prototype.cleanUpValue.call( this, value );
135
136 return trimByteLength( this.value, value, this.maxLength, ( val ) => {
137 const title = this.getMWTitle( val );
138 return title ? title.getMain() : val;
139 } ).newVal;
140 };
141
142 }() );