Debian Codegraph @

extensions/VisualEditor/lib/ve/src/ui/widgets/ve.ui.LanguageInputWidget.js

raw symbols SCIP
1 /*!
2 * VisualEditor UserInterface LanguageInputWidget class.
3 *
4 * @copyright See AUTHORS.txt
5 */
6
7 /**
8 * Creates an ve.ui.LanguageInputWidget object.
9 *
10 * @class
11 * @extends OO.ui.Widget
12 *
13 * @constructor
14 * @param {Object} [config] Configuration options
15 * @param {string} [config.dirInput='auto'] How to display the directionality input. Options are:
16 * - none: Directionality input is hidden.
17 * - no-auto: Directionality input is visible and options are LTR or RTL.
18 * - auto: Directionality input is visible and options include "auto" in
19 * addition to LTR and RTL.
20 * @param {boolean} [config.readOnly=false] Prevent changes to the value of the input.
21 * @param {boolean} [config.hideCodeInput] Prevent user from entering a language code as free text
22 * @param {ve.ui.WindowManager} [config.dialogManager] Window manager to launch the language search dialog in
23 * @param {string[]} [config.availableLanguages] Available language codes to show in search dialog
24 */
25 ve.ui.LanguageInputWidget = function VeUiLanguageInputWidget( config ) {
26 // Configuration initialization
27 config = config || {};
28
29 // Parent constructor
30 ve.ui.LanguageInputWidget.super.call( this, config );
31
32 // Properties
33 this.lang = null;
34 this.dir = null;
35 this.setReadOnly( !!config.readOnly );
36
37 this.dialogs = config.dialogManager || new ve.ui.WindowManager( { factory: ve.ui.windowFactory } );
38 this.availableLanguages = config.availableLanguages;
39
40 this.findLanguageButton = new OO.ui.ButtonWidget( {
41 classes: [ 've-ui-languageInputWidget-findLanguageButton' ],
42 icon: 'ellipsis'
43 } );
44 this.findLanguageButton.$button.attr( 'aria-label', ve.msg( 'visualeditor-dialog-language-search-title' ) );
45 this.selectedLanguageLabel = new OO.ui.LabelWidget( {
46 classes: [ 've-ui-languageInputWidget-selectedLanguageLabel' ],
47 label: ve.msg( 'visualeditor-languageinspector-widget-changelang' )
48 } );
49 this.languageCodeTextInput = new OO.ui.TextInputWidget( {
50 classes: [ 've-ui-languageInputWidget-languageCodeTextInput' ]
51 } );
52 this.languageCodeTextInput.$input.attr( 'aria-label', ve.msg( 'visualeditor-languageinspector-widget-label-langcode' ) );
53
54 this.directionSelect = new OO.ui.ButtonSelectWidget( {
55 classes: [ 've-ui-languageInputWidget-directionSelect' ]
56 } );
57 this.directionLabel = new OO.ui.LabelWidget( {
58 classes: [ 've-ui-languageInputWidget-directionLabel' ],
59 label: ve.msg( 'visualeditor-languageinspector-widget-label-direction' )
60 } );
61
62 const $language = $( '<div>' ).addClass( 've-ui-languageInputWidget-languageInput' );
63 $language.append(
64 this.findLanguageButton.$element
65 );
66 if ( !config.hideCodeInput ) {
67 $language.prepend( this.languageCodeTextInput.$element );
68 }
69 this.findLanguageButton.$element.before( this.selectedLanguageLabel.$element );
70
71 // Events
72 this.findLanguageButton.connect( this, { click: 'onFindLanguageButtonClick' } );
73 this.languageCodeTextInput.connect( this, { change: 'onChange' } );
74 this.directionSelect.connect( this, { select: 'onChange' } );
75
76 // Initialization
77 const dirItems = [
78 new OO.ui.ButtonOptionWidget( {
79 data: 'rtl',
80 icon: 'textDirRTL'
81 } ),
82 new OO.ui.ButtonOptionWidget( {
83 data: 'ltr',
84 icon: 'textDirLTR'
85 } )
86 ];
87 const dirInput = ( config.dirInput === undefined ) ? 'auto' : config.dirInput;
88
89 if ( dirInput === 'auto' ) {
90 dirItems.splice(
91 1, 0, new OO.ui.ButtonOptionWidget( {
92 data: null,
93 label: ve.msg( 'visualeditor-dialog-language-auto-direction' )
94 } )
95 );
96 }
97 this.directionSelect.addItems( dirItems );
98 $( OO.ui.getTeleportTarget() ).append( this.dialogs.$element );
99
100 this.$element
101 .addClass( 've-ui-languageInputWidget' )
102 .append( $language );
103
104 if ( dirInput !== 'none' ) {
105 this.$element.append( this.directionLabel.$element, this.directionSelect.$element );
106 }
107 };
108
109 /* Inheritance */
110
111 OO.inheritClass( ve.ui.LanguageInputWidget, OO.ui.Widget );
112
113 /* Events */
114
115 /**
116 * @event ve.ui.LanguageInputWidget#change
117 * @param {string} lang Language code
118 * @param {string} dir Directionality
119 */
120
121 /* Methods */
122
123 /**
124 * Handle find language button click events.
125 */
126 ve.ui.LanguageInputWidget.prototype.onFindLanguageButtonClick = function () {
127 this.dialogs.openWindow( 'languageSearch', {
128 availableLanguages: this.availableLanguages,
129 $returnFocusTo: null
130 } ).closing.then( ( data ) => {
131 data = data || {};
132 if ( data.action === 'done' ) {
133 this.setLangAndDir( data.lang, data.dir );
134 }
135 } );
136 };
137
138 /**
139 * Handle input widget change events.
140 */
141 ve.ui.LanguageInputWidget.prototype.onChange = function () {
142 if ( this.updating ) {
143 return;
144 }
145
146 const selectedItem = this.directionSelect.findSelectedItem();
147 this.setLangAndDir(
148 this.languageCodeTextInput.getValue(),
149 selectedItem ? selectedItem.getData() : null
150 );
151 };
152
153 /**
154 * Set language and directionality
155 *
156 * The inputs value will automatically be updated.
157 *
158 * @param {string|null} lang Language code
159 * @param {string|null} dir Directionality
160 * @fires ve.ui.LanguageInputWidget#change
161 * @return {ve.ui.LanguageInputWidget}
162 * @chainable
163 */
164 ve.ui.LanguageInputWidget.prototype.setLangAndDir = function ( lang, dir ) {
165 if ( lang === this.lang && dir === this.dir ) {
166 // No change
167 return this;
168 }
169
170 // Set state flag while programmatically changing input widget values
171 this.updating = true;
172 if ( lang || dir ) {
173 lang = lang || '';
174 this.languageCodeTextInput.setValue( lang );
175 this.selectedLanguageLabel.setLabel(
176 ve.init.platform.hasLanguageCode( lang.toLowerCase() ) ?
177 ve.init.platform.getLanguageName( lang.toLowerCase() ) :
178 ve.msg( 'visualeditor-languageinspector-widget-changelang' )
179 );
180 this.directionSelect.selectItemByData( dir );
181 } else {
182 this.languageCodeTextInput.setValue( '' );
183 this.selectedLanguageLabel.setLabel(
184 ve.msg( 'visualeditor-languageinspector-widget-changelang' )
185 );
186 this.directionSelect.selectItem( null );
187 }
188 // Set title as long language may be truncated
189 this.selectedLanguageLabel.setTitle( this.selectedLanguageLabel.$label.text() );
190 this.updating = false;
191
192 this.lang = lang;
193 this.dir = dir;
194 this.emit( 'change', lang, dir );
195 return this;
196 };
197
198 /**
199 * Get the language
200 *
201 * @return {string|null} Language code
202 */
203 ve.ui.LanguageInputWidget.prototype.getLang = function () {
204 return this.lang;
205 };
206
207 /**
208 * Get the directionality
209 *
210 * @return {string|null} Directionality (ltr/rtl)
211 */
212 ve.ui.LanguageInputWidget.prototype.getDir = function () {
213 return this.dir;
214 };
215
216 /**
217 * Update the disabled state of the controls
218 *
219 * @chainable
220 * @protected
221 * @return {OO.ui.NumberInputWidget} The widget, for chaining
222 */
223 ve.ui.LanguageInputWidget.prototype.updateControlsDisabled = function () {
224 const disabled = this.isDisabled() || this.isReadOnly();
225 if ( this.findLanguageButton ) {
226 this.findLanguageButton.setDisabled( disabled );
227 }
228 if ( this.directionSelect ) {
229 this.directionSelect.setDisabled( disabled );
230 }
231 return this;
232 };
233
234 /**
235 * Disable or enable the inputs
236 *
237 * @param {boolean} disabled Set disabled or enabled
238 * @return {ve.ui.LanguageInputWidget}
239 * @chainable
240 */
241 ve.ui.LanguageInputWidget.prototype.setDisabled = function ( disabled ) {
242 // Parent method
243 ve.ui.LanguageInputWidget.super.prototype.setDisabled.call( this, disabled );
244
245 // The 'setDisabled' method runs in the constructor before the
246 // inputs are initialized
247 if ( this.languageCodeTextInput ) {
248 this.languageCodeTextInput.setDisabled( disabled );
249 }
250 this.updateControlsDisabled();
251 return this;
252 };
253
254 /**
255 * Check if the widget is read-only
256 *
257 * @return {boolean}
258 */
259 ve.ui.LanguageInputWidget.prototype.isReadOnly = function () {
260 return this.readOnly;
261 };
262
263 /**
264 * Set the read-only state of the widget
265 *
266 * @param {boolean} readOnly Make widget read-only
267 * @return {ve.ui.LanguageInputWidget}
268 * @chainable
269 */
270 ve.ui.LanguageInputWidget.prototype.setReadOnly = function ( readOnly ) {
271 this.readOnly = readOnly;
272 if ( this.languageCodeTextInput ) {
273 this.languageCodeTextInput.setReadOnly( readOnly );
274 }
275 this.updateControlsDisabled();
276 return this;
277 };