mirror of
https://github.com/element-hq/element-web.git
synced 2024-12-16 03:41:30 +03:00
9fa6e8b1e2
As I've now added code to this file
128 lines
4.3 KiB
JavaScript
128 lines
4.3 KiB
JavaScript
/*
|
|
Copyright 2017 Marcel Radzio (MTRNord)
|
|
Copyright 2017 Vector Creations Ltd.
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
you may not use this file except in compliance with the License.
|
|
You may obtain a copy of the License at
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
See the License for the specific language governing permissions and
|
|
limitations under the License.
|
|
*/
|
|
|
|
import React from 'react';
|
|
|
|
import sdk from '../../../index';
|
|
import UserSettingsStore from '../../../UserSettingsStore';
|
|
import { _t } from '../../../languageHandler';
|
|
import * as languageHandler from '../../../languageHandler';
|
|
|
|
function languageMatchesSearchQuery(query, language) {
|
|
if (language.label.toUpperCase().indexOf(query.toUpperCase()) == 0) return true;
|
|
if (language.value.toUpperCase() == query.toUpperCase()) return true;
|
|
return false;
|
|
}
|
|
|
|
export default class LanguageDropdown extends React.Component {
|
|
constructor(props) {
|
|
super(props);
|
|
this._onSearchChange = this._onSearchChange.bind(this);
|
|
|
|
this.state = {
|
|
searchQuery: '',
|
|
langs: null,
|
|
}
|
|
}
|
|
|
|
componentWillMount() {
|
|
languageHandler.getAllLanguageKeysFromJson().then((langKeys) => {
|
|
const langs = [];
|
|
langKeys.forEach((languageKey) => {
|
|
langs.push({
|
|
value: languageKey,
|
|
label: _t(languageKey)
|
|
});
|
|
});
|
|
langs.sort(function(a, b){
|
|
if(a.label < b.label) return -1;
|
|
if(a.label > b.label) return 1;
|
|
return 0;
|
|
});
|
|
this.setState({langs});
|
|
}).catch(() => {
|
|
this.setState({langs: ['en']});
|
|
}).done();
|
|
|
|
if (!this.props.value) {
|
|
// If no value is given, we start with the first
|
|
// country selected, but our parent component
|
|
// doesn't know this, therefore we do this.
|
|
const _localSettings = UserSettingsStore.getLocalSettings();
|
|
if (_localSettings.hasOwnProperty('language')) {
|
|
this.props.onOptionChange(_localSettings.language);
|
|
}else {
|
|
const language = languageHandler.normalizeLanguageKey(languageHandler.getLanguageFromBrowser());
|
|
this.props.onOptionChange(language);
|
|
}
|
|
}
|
|
}
|
|
|
|
_onSearchChange(search) {
|
|
this.setState({
|
|
searchQuery: search,
|
|
});
|
|
}
|
|
|
|
render() {
|
|
if (this.state.langs === null) {
|
|
const Spinner = sdk.getComponent('elements.Spinner');
|
|
return <Spinner />;
|
|
}
|
|
|
|
const Dropdown = sdk.getComponent('elements.Dropdown');
|
|
|
|
let displayedLanguages;
|
|
if (this.state.searchQuery) {
|
|
displayedLanguages = this.state.langs.filter((lang) => {
|
|
return languageMatchesSearchQuery(this.state.searchQuery, lang);
|
|
});
|
|
} else {
|
|
displayedLanguages = this.state.langs;
|
|
}
|
|
|
|
const options = displayedLanguages.map((language) => {
|
|
return <div key={language.value}>
|
|
{language.label}
|
|
</div>;
|
|
});
|
|
|
|
// default value here too, otherwise we need to handle null / undefined
|
|
// values between mounting and the initial value propgating
|
|
let value = null;
|
|
const _localSettings = UserSettingsStore.getLocalSettings();
|
|
if (_localSettings.hasOwnProperty('language')) {
|
|
value = this.props.value || _localSettings.language;
|
|
} else {
|
|
const language = navigator.language || navigator.userLanguage;
|
|
value = this.props.value || language;
|
|
}
|
|
|
|
return <Dropdown className={this.props.className}
|
|
onOptionChange={this.props.onOptionChange} onSearchChange={this._onSearchChange}
|
|
searchEnabled={true} value={value}
|
|
>
|
|
{options}
|
|
</Dropdown>
|
|
}
|
|
}
|
|
|
|
LanguageDropdown.propTypes = {
|
|
className: React.PropTypes.string,
|
|
onOptionChange: React.PropTypes.func.isRequired,
|
|
value: React.PropTypes.string,
|
|
};
|