2018-10-30 05:02:23 +00:00
|
|
|
/* @flow */
|
|
|
|
|
2021-01-14 16:12:08 +00:00
|
|
|
import DropdownMenu, {
|
|
|
|
DropdownItem,
|
|
|
|
DropdownItemGroup
|
|
|
|
} from '@atlaskit/dropdown-menu';
|
2017-03-29 17:43:30 +00:00
|
|
|
import React, { Component } from 'react';
|
|
|
|
|
2019-09-13 13:03:40 +00:00
|
|
|
import { translate } from '../../base/i18n/functions';
|
2017-03-29 17:43:30 +00:00
|
|
|
|
2018-10-30 05:02:23 +00:00
|
|
|
/**
|
|
|
|
* The type of the React {@code Component} props of {@link DeviceSelector}.
|
|
|
|
*/
|
|
|
|
type Props = {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* MediaDeviceInfos used for display in the select element.
|
|
|
|
*/
|
|
|
|
devices: Array<Object>,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* If false, will return a selector with no selection options.
|
|
|
|
*/
|
|
|
|
hasPermission: boolean,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* CSS class for the icon to the left of the dropdown trigger.
|
|
|
|
*/
|
|
|
|
icon: string,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* If true, will render the selector disabled with a default selection.
|
|
|
|
*/
|
|
|
|
isDisabled: boolean,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The translation key to display as a menu label.
|
|
|
|
*/
|
|
|
|
label: string,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The callback to invoke when a selection is made.
|
|
|
|
*/
|
|
|
|
onSelect: Function,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The default device to display as selected.
|
|
|
|
*/
|
|
|
|
selectedDeviceId: string,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Invoked to obtain translated strings.
|
|
|
|
*/
|
2021-06-10 12:48:44 +00:00
|
|
|
t: Function,
|
|
|
|
|
|
|
|
/**
|
2021-11-04 21:10:43 +00:00
|
|
|
* The id of the dropdown element.
|
2021-06-10 12:48:44 +00:00
|
|
|
*/
|
|
|
|
id: string
|
2018-10-30 05:02:23 +00:00
|
|
|
};
|
|
|
|
|
2017-03-29 17:43:30 +00:00
|
|
|
/**
|
2017-04-17 21:02:36 +00:00
|
|
|
* React component for selecting a device from a select element. Wraps
|
|
|
|
* AKDropdownMenu with device selection specific logic.
|
2017-03-29 17:43:30 +00:00
|
|
|
*
|
2021-11-04 21:10:43 +00:00
|
|
|
* @augments Component
|
2017-03-29 17:43:30 +00:00
|
|
|
*/
|
2018-10-30 05:02:23 +00:00
|
|
|
class DeviceSelector extends Component<Props> {
|
2017-03-29 17:43:30 +00:00
|
|
|
/**
|
|
|
|
* Initializes a new DeviceSelector instance.
|
|
|
|
*
|
|
|
|
* @param {Object} props - The read-only React Component props with which
|
|
|
|
* the new instance is to be initialized.
|
|
|
|
*/
|
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
|
|
|
|
this._onSelect = this._onSelect.bind(this);
|
2021-01-14 16:12:08 +00:00
|
|
|
this._createDropdownItem = this._createDropdownItem.bind(this);
|
2017-03-29 17:43:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Implements React's {@link Component#render()}.
|
|
|
|
*
|
|
|
|
* @inheritdoc
|
|
|
|
* @returns {ReactElement}
|
|
|
|
*/
|
|
|
|
render() {
|
2021-06-10 12:48:44 +00:00
|
|
|
if (this.props.hasPermission === undefined) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2017-03-29 17:43:30 +00:00
|
|
|
if (!this.props.hasPermission) {
|
|
|
|
return this._renderNoPermission();
|
|
|
|
}
|
|
|
|
|
2017-06-04 03:12:04 +00:00
|
|
|
if (!this.props.devices || !this.props.devices.length) {
|
2017-03-29 17:43:30 +00:00
|
|
|
return this._renderNoDevices();
|
|
|
|
}
|
|
|
|
|
2017-04-17 21:02:36 +00:00
|
|
|
const items = this.props.devices.map(this._createDropdownItem);
|
2021-01-14 16:12:08 +00:00
|
|
|
const defaultSelected = this.props.devices.find(item =>
|
|
|
|
item.deviceId === this.props.selectedDeviceId
|
2017-03-29 17:43:30 +00:00
|
|
|
);
|
|
|
|
|
2017-04-17 21:02:36 +00:00
|
|
|
return this._createDropdown({
|
2017-03-29 17:43:30 +00:00
|
|
|
defaultSelected,
|
|
|
|
isDisabled: this.props.isDisabled,
|
|
|
|
items,
|
2017-04-21 15:58:34 +00:00
|
|
|
placeholder: this.props.t('deviceSelection.selectADevice')
|
2017-03-29 17:43:30 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-04-21 02:24:55 +00:00
|
|
|
* Creates a React Element for displaying the passed in text surrounded by
|
|
|
|
* two icons. The left icon is the icon class passed in through props and
|
|
|
|
* the right icon is AtlasKit ExpandIcon.
|
2017-04-17 21:02:36 +00:00
|
|
|
*
|
2017-04-21 02:24:55 +00:00
|
|
|
* @param {string} triggerText - The text to display within the element.
|
2017-04-17 21:02:36 +00:00
|
|
|
* @private
|
|
|
|
* @returns {ReactElement}
|
|
|
|
*/
|
2017-04-21 02:24:55 +00:00
|
|
|
_createDropdownTrigger(triggerText) {
|
2017-04-17 21:02:36 +00:00
|
|
|
return (
|
2017-04-21 02:24:55 +00:00
|
|
|
<div className = 'device-selector-trigger'>
|
|
|
|
<span className = 'device-selector-trigger-text'>
|
|
|
|
{ triggerText }
|
|
|
|
</span>
|
|
|
|
</div>
|
2017-04-17 21:02:36 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-01-14 16:12:08 +00:00
|
|
|
_createDropdownItem: (Object) => void;
|
|
|
|
|
2017-04-17 21:02:36 +00:00
|
|
|
/**
|
|
|
|
* Creates an object in the format expected by AKDropdownMenu for an option.
|
2017-03-29 17:43:30 +00:00
|
|
|
*
|
|
|
|
* @param {MediaDeviceInfo} device - An object with a label and a deviceId.
|
|
|
|
* @private
|
|
|
|
* @returns {Object} The passed in media device description converted to a
|
2017-04-17 21:02:36 +00:00
|
|
|
* format recognized as a valid AKDropdownMenu item.
|
2017-03-29 17:43:30 +00:00
|
|
|
*/
|
2017-04-17 21:02:36 +00:00
|
|
|
_createDropdownItem(device) {
|
2021-01-14 16:12:08 +00:00
|
|
|
return (
|
|
|
|
<DropdownItem
|
2021-06-10 12:48:44 +00:00
|
|
|
data-deviceid = { device.deviceId }
|
|
|
|
isSelected = { device.deviceId === this.props.selectedDeviceId }
|
2021-01-14 16:12:08 +00:00
|
|
|
key = { device.deviceId }
|
2021-06-10 12:48:44 +00:00
|
|
|
onClick = { this._onSelect }>
|
2021-01-14 16:12:08 +00:00
|
|
|
{ device.label || device.deviceId }
|
|
|
|
</DropdownItem>
|
|
|
|
);
|
2017-03-29 17:43:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-04-25 16:15:15 +00:00
|
|
|
* Creates a AKDropdownMenu Component using passed in props and options. If
|
|
|
|
* the dropdown needs to be disabled, then only the AKDropdownMenu trigger
|
|
|
|
* element is returned to simulate a disabled state.
|
2017-03-29 17:43:30 +00:00
|
|
|
*
|
2017-04-17 21:02:36 +00:00
|
|
|
* @param {Object} options - Additional configuration for display.
|
2017-03-29 17:43:30 +00:00
|
|
|
* @param {Object} options.defaultSelected - The option that should be set
|
|
|
|
* as currently chosen.
|
2017-04-25 16:15:15 +00:00
|
|
|
* @param {boolean} options.isDisabled - If true, only the AKDropdownMenu
|
|
|
|
* trigger component will be returned to simulate a disabled dropdown.
|
2017-03-29 17:43:30 +00:00
|
|
|
* @param {Array} options.items - All the selectable options to display.
|
|
|
|
* @param {string} options.placeholder - The translation key to display when
|
|
|
|
* no selection has been made.
|
|
|
|
* @private
|
|
|
|
* @returns {ReactElement}
|
|
|
|
*/
|
2017-04-17 21:02:36 +00:00
|
|
|
_createDropdown(options) {
|
|
|
|
const triggerText
|
2021-01-14 16:12:08 +00:00
|
|
|
= (options.defaultSelected && (options.defaultSelected.label || options.defaultSelected.deviceId))
|
2017-04-17 21:02:36 +00:00
|
|
|
|| options.placeholder;
|
2017-04-25 16:15:15 +00:00
|
|
|
const trigger = this._createDropdownTrigger(triggerText);
|
|
|
|
|
2021-01-14 16:12:08 +00:00
|
|
|
if (options.isDisabled || !options.items.length) {
|
2017-04-25 16:15:15 +00:00
|
|
|
return (
|
|
|
|
<div className = 'device-selector-trigger-disabled'>
|
|
|
|
{ trigger }
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
2017-04-17 21:02:36 +00:00
|
|
|
|
2017-03-29 17:43:30 +00:00
|
|
|
return (
|
2021-01-14 16:12:08 +00:00
|
|
|
<div className = 'dropdown-menu'>
|
|
|
|
<DropdownMenu
|
|
|
|
shouldFitContainer = { true }
|
|
|
|
trigger = { triggerText }
|
|
|
|
triggerButtonProps = {{
|
2021-06-10 12:48:44 +00:00
|
|
|
shouldFitContainer: true,
|
|
|
|
id: this.props.id
|
2021-01-14 16:12:08 +00:00
|
|
|
}}
|
|
|
|
triggerType = 'button'>
|
|
|
|
<DropdownItemGroup>
|
|
|
|
{ options.items }
|
|
|
|
</DropdownItemGroup>
|
|
|
|
</DropdownMenu>
|
|
|
|
</div>
|
2017-03-29 17:43:30 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-10-30 05:02:23 +00:00
|
|
|
_onSelect: (Object) => void;
|
|
|
|
|
2017-03-29 17:43:30 +00:00
|
|
|
/**
|
|
|
|
* Invokes the passed in callback to notify of selection changes.
|
|
|
|
*
|
2021-06-10 12:48:44 +00:00
|
|
|
* @param {Object} e - The key event to handle.
|
|
|
|
*
|
2017-03-29 17:43:30 +00:00
|
|
|
* @private
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
2021-06-10 12:48:44 +00:00
|
|
|
_onSelect(e) {
|
|
|
|
const deviceId = e.currentTarget.getAttribute('data-deviceid');
|
|
|
|
|
|
|
|
if (this.props.selectedDeviceId !== deviceId) {
|
|
|
|
this.props.onSelect(deviceId);
|
2017-04-17 21:02:36 +00:00
|
|
|
}
|
2017-03-29 17:43:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a Select Component that is disabled and has a placeholder
|
|
|
|
* indicating there are no devices to select.
|
|
|
|
*
|
|
|
|
* @private
|
|
|
|
* @returns {ReactElement}
|
|
|
|
*/
|
|
|
|
_renderNoDevices() {
|
2017-04-17 21:02:36 +00:00
|
|
|
return this._createDropdown({
|
2017-03-29 17:43:30 +00:00
|
|
|
isDisabled: true,
|
2017-04-17 21:02:36 +00:00
|
|
|
placeholder: this.props.t('settings.noDevice')
|
2017-03-29 17:43:30 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-04-17 21:02:36 +00:00
|
|
|
* Creates a AKDropdownMenu Component that is disabled and has a placeholder
|
|
|
|
* stating there is no permission to display the devices.
|
2017-03-29 17:43:30 +00:00
|
|
|
*
|
|
|
|
* @private
|
|
|
|
* @returns {ReactElement}
|
|
|
|
*/
|
|
|
|
_renderNoPermission() {
|
2017-04-17 21:02:36 +00:00
|
|
|
return this._createDropdown({
|
2017-03-29 17:43:30 +00:00
|
|
|
isDisabled: true,
|
2017-04-25 16:15:15 +00:00
|
|
|
placeholder: this.props.t('deviceSelection.noPermission')
|
2017-03-29 17:43:30 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default translate(DeviceSelector);
|