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