import React from 'react'; import TranslatedComponent from './TranslatedComponent'; import Ship from '../shipyard/Ship'; import { Ships } from 'coriolis-data/dist'; import { Rocket } from './SvgIcons'; import Persist from '../stores/Persist'; import cn from 'classnames'; /** * Ship picker * Requires an onChange() function of the form onChange(ship), providing the ship, which is triggered on ship change */ export default class ShipPicker extends TranslatedComponent { static propTypes = { onChange: React.PropTypes.func.isRequired, ship: React.PropTypes.string.isRequired, build: React.PropTypes.string }; static defaultProps = { ship: 'eagle' } /** * Constructor * @param {Object} props React Component properties * @param {Object} context React Component context */ constructor(props, context) { super(props); this.shipOrder = Object.keys(Ships).sort(); this._toggleMenu = this._toggleMenu.bind(this); this._closeMenu = this._closeMenu.bind(this); this.state = { menuOpen: false }; } /** * Update ship * @param {object} ship the ship * @param {string} build the build, if present */ _shipChange(ship, build) { this._closeMenu(); // Ensure that the ship has changed if (ship !== this.props.ship || build !== this.props.build) { this.props.onChange(ship, build); } } /** * Render the menu for the picker * @returns {object} the picker menu */ _renderPickerMenu() { const { ship, build } = this.props; const _shipChange = this._shipChange; const builds = Persist.getBuilds(); const buildList = []; for (let shipId of this.shipOrder) { const shipBuilds = []; // Add stock build const stockSelected = (ship == shipId && !build); shipBuilds.push(