/* * Copyright 2019 Red Hat, Inc. and/or its affiliates. * * 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 * as React from 'react'; import {withRouter, RouteComponentProps} from 'react-router-dom'; import {Msg} from './Msg'; import { Dropdown, DropdownItem, DropdownToggle, Nav, NavExpandable, NavItem, NavList } from '@patternfly/react-core'; declare const locale: string; declare const baseUrl: string; declare const referrer: string; declare const referrerUri: string; interface AvailableLocale { locale: string; label: string; }; declare const availableLocales: [AvailableLocale]; // remove entry for current locale const availLocales = availableLocales.filter((availableLocale: AvailableLocale) => availableLocale.locale !== locale); let referrerFragment = ''; if ((typeof referrer !== 'undefined') && (typeof referrerUri !== 'undefined')) { referrerFragment = '&referrer=' + referrer + '&referrer_uri=' + encodeURIComponent(referrerUri); } interface LocaleKebabItemProps extends RouteComponentProps {} interface LocaleKebabItemState {activeGroup: string; activeItem: string} class LocaleKebabItem extends React.Component { public constructor(props: LocaleKebabItemProps) { super(props); this.state = { activeGroup: 'locale-group', activeItem: '' }; } public render(): React.ReactNode { const appPath = this.props.location.pathname; const localeNavItems = availLocales.map((availableLocale: AvailableLocale) => { const url = baseUrl + '?kc_locale=' + availableLocale.locale + referrerFragment + '#' + appPath; return ( {availableLocale.label} ); }); return ( ); } }; interface LocaleDropdownComponentProps extends RouteComponentProps {} interface LocaleDropdownComponentState {isDropdownOpen: boolean} class LocaleDropdownComponent extends React.Component { public constructor(props: LocaleDropdownComponentProps) { super(props); this.state = {isDropdownOpen: false}; } private onDropdownToggle = (isDropdownOpen: boolean) => { this.setState({ isDropdownOpen }); }; private onDropdownSelect = () => { this.setState({ isDropdownOpen: !this.state.isDropdownOpen }); }; public render(): React.ReactNode { const appPath = this.props.location.pathname; const localeDropdownItems = availLocales.map((availableLocale: AvailableLocale) => { const url = baseUrl + '?kc_locale=' + availableLocale.locale + referrerFragment + '#' + appPath; return ( {availableLocale.label} ); }); if (localeDropdownItems.length < 2) return (); return ( } dropdownItems={localeDropdownItems} /> ); } }; export const LocaleDropdown = withRouter(LocaleDropdownComponent); export const LocaleNav = withRouter(LocaleKebabItem);