2017-05-23 17:16:31 +03:00
|
|
|
/*
|
|
|
|
Copyright 2017 Marcel Radzio (MTRNord)
|
2017-05-25 14:02:05 +03:00
|
|
|
Copyright 2017 Vector Creations Ltd.
|
2017-05-23 17:16:31 +03:00
|
|
|
|
|
|
|
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';
|
2017-12-26 04:03:18 +03:00
|
|
|
import PropTypes from 'prop-types';
|
2017-05-23 17:16:31 +03:00
|
|
|
|
|
|
|
import sdk from '../../../index';
|
2017-05-23 20:32:45 +03:00
|
|
|
import * as languageHandler from '../../../languageHandler';
|
2017-10-30 01:53:00 +03:00
|
|
|
import SettingsStore from "../../../settings/SettingsStore";
|
2017-05-23 17:16:31 +03:00
|
|
|
|
|
|
|
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: '',
|
2017-05-24 13:25:06 +03:00
|
|
|
langs: null,
|
2017-10-11 19:56:17 +03:00
|
|
|
};
|
2017-05-23 17:16:31 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
componentWillMount() {
|
2017-06-01 21:46:25 +03:00
|
|
|
languageHandler.getAllLanguagesFromJson().then((langs) => {
|
2017-10-11 19:56:17 +03:00
|
|
|
langs.sort(function(a, b) {
|
2017-11-16 16:19:36 +03:00
|
|
|
if (a.label < b.label) return -1;
|
|
|
|
if (a.label > b.label) return 1;
|
2017-05-24 13:25:06 +03:00
|
|
|
return 0;
|
|
|
|
});
|
|
|
|
this.setState({langs});
|
|
|
|
}).catch(() => {
|
|
|
|
this.setState({langs: ['en']});
|
|
|
|
}).done();
|
2017-05-23 17:16:31 +03:00
|
|
|
|
|
|
|
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.
|
2017-11-05 01:38:26 +03:00
|
|
|
const language = SettingsStore.getValue("language", null, /*excludeDefault:*/true);
|
2017-10-30 01:53:00 +03:00
|
|
|
if (language) {
|
|
|
|
this.props.onOptionChange(language);
|
2017-11-16 16:19:36 +03:00
|
|
|
} else {
|
2017-05-23 17:16:31 +03:00
|
|
|
const language = languageHandler.normalizeLanguageKey(languageHandler.getLanguageFromBrowser());
|
|
|
|
this.props.onOptionChange(language);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_onSearchChange(search) {
|
|
|
|
this.setState({
|
|
|
|
searchQuery: search,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
2017-05-24 13:25:06 +03:00
|
|
|
if (this.state.langs === null) {
|
|
|
|
const Spinner = sdk.getComponent('elements.Spinner');
|
|
|
|
return <Spinner />;
|
|
|
|
}
|
|
|
|
|
2017-05-23 17:16:31 +03:00
|
|
|
const Dropdown = sdk.getComponent('elements.Dropdown');
|
|
|
|
|
|
|
|
let displayedLanguages;
|
|
|
|
if (this.state.searchQuery) {
|
2017-05-24 13:25:06 +03:00
|
|
|
displayedLanguages = this.state.langs.filter((lang) => {
|
|
|
|
return languageMatchesSearchQuery(this.state.searchQuery, lang);
|
|
|
|
});
|
2017-05-23 17:16:31 +03:00
|
|
|
} else {
|
2017-05-24 13:25:06 +03:00
|
|
|
displayedLanguages = this.state.langs;
|
2017-05-23 17:16:31 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
const options = displayedLanguages.map((language) => {
|
|
|
|
return <div key={language.value}>
|
2017-10-11 19:56:17 +03:00
|
|
|
{ language.label }
|
2017-05-23 17:16:31 +03:00
|
|
|
</div>;
|
|
|
|
});
|
|
|
|
|
|
|
|
// default value here too, otherwise we need to handle null / undefined
|
|
|
|
// values between mounting and the initial value propgating
|
2017-11-05 01:38:26 +03:00
|
|
|
let language = SettingsStore.getValue("language", null, /*excludeDefault:*/true);
|
2017-05-23 17:16:31 +03:00
|
|
|
let value = null;
|
2017-10-30 01:53:00 +03:00
|
|
|
if (language) {
|
|
|
|
value = this.props.value || language;
|
2017-05-23 17:16:31 +03:00
|
|
|
} else {
|
2017-10-30 01:53:00 +03:00
|
|
|
language = navigator.language || navigator.userLanguage;
|
2017-05-23 17:16:31 +03:00
|
|
|
value = this.props.value || language;
|
|
|
|
}
|
|
|
|
|
|
|
|
return <Dropdown className={this.props.className}
|
|
|
|
onOptionChange={this.props.onOptionChange} onSearchChange={this._onSearchChange}
|
2017-05-24 13:25:06 +03:00
|
|
|
searchEnabled={true} value={value}
|
2017-05-23 17:16:31 +03:00
|
|
|
>
|
2017-10-11 19:56:17 +03:00
|
|
|
{ options }
|
|
|
|
</Dropdown>;
|
2017-05-23 17:16:31 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
LanguageDropdown.propTypes = {
|
2017-12-26 04:03:18 +03:00
|
|
|
className: PropTypes.string,
|
|
|
|
onOptionChange: PropTypes.func.isRequired,
|
|
|
|
value: PropTypes.string,
|
2017-05-23 17:16:31 +03:00
|
|
|
};
|