Debian Codegraph @

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

raw symbols SCIP
1 /*!
2 * VisualEditor UserInterface LanguageSearchWidget class.
3 *
4 * @copyright See AUTHORS.txt
5 */
6
7 /**
8 * Creates an ve.ui.LanguageSearchWidget object.
9 *
10 * @class
11 * @extends OO.ui.SearchWidget
12 *
13 * @constructor
14 * @param {Object} [config] Configuration options
15 */
16 ve.ui.LanguageSearchWidget = function VeUiLanguageSearchWidget( config ) {
17 // Configuration initialization
18 config = ve.extendObject( {
19 placeholder: ve.msg( 'visualeditor-language-search-input-placeholder' )
20 }, config );
21
22 // Parent constructor
23 ve.ui.LanguageSearchWidget.super.call( this, config );
24
25 // Properties
26 this.filteredLanguageResultWidgets = [];
27 this.languageResultWidgets = ve.init.platform.getLanguageCodes()
28 .sort()
29 .map( ( languageCode ) => new ve.ui.LanguageResultWidget( { data: {
30 code: languageCode,
31 name: ve.init.platform.getLanguageName( languageCode ),
32 autonym: ve.init.platform.getLanguageAutonym( languageCode )
33 } } ) );
34
35 this.setAvailableLanguages();
36
37 // Initialization
38 this.$element.addClass( 've-ui-languageSearchWidget' );
39 };
40
41 /* Inheritance */
42
43 OO.inheritClass( ve.ui.LanguageSearchWidget, OO.ui.SearchWidget );
44
45 /* Methods */
46
47 /**
48 * @inheritdoc
49 */
50 ve.ui.LanguageSearchWidget.prototype.onQueryChange = function () {
51 // Parent method
52 ve.ui.LanguageSearchWidget.super.prototype.onQueryChange.apply( this, arguments );
53
54 // Populate
55 this.addResults();
56 };
57
58 /**
59 * Set available languages to show
60 *
61 * @param {string[]} availableLanguages Available language codes to show, all if undefined
62 */
63 ve.ui.LanguageSearchWidget.prototype.setAvailableLanguages = function ( availableLanguages ) {
64 if ( !availableLanguages ) {
65 this.filteredLanguageResultWidgets = this.languageResultWidgets.slice();
66 return;
67 }
68
69 this.filteredLanguageResultWidgets = [];
70
71 for ( let i = 0, iLen = this.languageResultWidgets.length; i < iLen; i++ ) {
72 const languageResult = this.languageResultWidgets[ i ];
73 const data = languageResult.getData();
74 if ( availableLanguages.indexOf( data.code ) !== -1 ) {
75 this.filteredLanguageResultWidgets.push( languageResult );
76 }
77 }
78 };
79
80 /**
81 * Update search results from current query
82 */
83 ve.ui.LanguageSearchWidget.prototype.addResults = function () {
84 const matchProperties = [ 'name', 'autonym', 'code' ],
85 query = this.query.getValue().trim(),
86 compare = new Intl.Collator( this.lang, { sensitivity: 'base' } ).compare,
87 hasQuery = !!query.length,
88 items = [];
89
90 this.results.clearItems();
91
92 for ( let i = 0, iLen = this.filteredLanguageResultWidgets.length; i < iLen; i++ ) {
93 const languageResult = this.filteredLanguageResultWidgets[ i ];
94 const data = languageResult.getData();
95 let matchedProperty = null;
96
97 for ( let j = 0, jLen = matchProperties.length; j < jLen; j++ ) {
98 if ( data[ matchProperties[ j ] ] && compare( data[ matchProperties[ j ] ].slice( 0, query.length ), query ) === 0 ) {
99 matchedProperty = matchProperties[ j ];
100 break;
101 }
102 }
103
104 if ( query === '' || matchedProperty ) {
105 items.push(
106 languageResult
107 .updateLabel( query, matchedProperty, compare )
108 .setSelected( false )
109 .setHighlighted( false )
110 );
111 }
112 }
113
114 this.results.addItems( items );
115 if ( hasQuery ) {
116 this.results.highlightItem( this.results.findFirstSelectableItem() );
117 }
118 };