|
| 1 | +/* |
| 2 | +Copyright 2016 OpenMarket Ltd |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +import React from 'react'; |
| 18 | + |
| 19 | +export default class NetworkDropdown extends React.Component { |
| 20 | + constructor() { |
| 21 | + super(); |
| 22 | + |
| 23 | + this.dropdownRootElement = null; |
| 24 | + this.ignoreEvent = null; |
| 25 | + |
| 26 | + this.onInputClick = this.onInputClick.bind(this); |
| 27 | + this.onRootClick = this.onRootClick.bind(this); |
| 28 | + this.onDocumentClick = this.onDocumentClick.bind(this); |
| 29 | + this.onNetworkClick = this.onNetworkClick.bind(this); |
| 30 | + this.collectRoot = this.collectRoot.bind(this); |
| 31 | + |
| 32 | + this.state = { |
| 33 | + expanded: false, |
| 34 | + selectedNetwork: null, |
| 35 | + }; |
| 36 | + } |
| 37 | + |
| 38 | + componentWillMount() { |
| 39 | + // Listen for all clicks on the document so we can close the |
| 40 | + // menu when the user clicks somewhere else |
| 41 | + document.addEventListener('click', this.onDocumentClick, false); |
| 42 | + } |
| 43 | + |
| 44 | + componentWillUnmount() { |
| 45 | + document.removeEventListener('click', this.onDocumentClick, false); |
| 46 | + } |
| 47 | + |
| 48 | + onDocumentClick(ev) { |
| 49 | + // Close the dropdown if the user clicks anywhere that isn't |
| 50 | + // within our root element |
| 51 | + if (ev !== this.ignoreEvent) { |
| 52 | + this.setState({ |
| 53 | + expanded: false, |
| 54 | + }); |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + onRootClick(ev) { |
| 59 | + // This captures any clicks that happen within our elements, |
| 60 | + // such that we can then ignore them when they're seen by the |
| 61 | + // click listener on the document handler, ie. not close the |
| 62 | + // dropdown immediately after opening it. |
| 63 | + // NB. We can't just stopPropagation() because then the event |
| 64 | + // doesn't reach the React onClick(). |
| 65 | + this.ignoreEvent = ev; |
| 66 | + } |
| 67 | + |
| 68 | + onInputClick(ev) { |
| 69 | + this.setState({ |
| 70 | + expanded: !this.state.expanded, |
| 71 | + }); |
| 72 | + ev.preventDefault(); |
| 73 | + } |
| 74 | + |
| 75 | + onNetworkClick(network, ev) { |
| 76 | + this.setState({ |
| 77 | + expanded: false, |
| 78 | + selectedNetwork: network, |
| 79 | + }); |
| 80 | + this.props.onNetworkChange(network); |
| 81 | + } |
| 82 | + |
| 83 | + collectRoot(e) { |
| 84 | + if (this.dropdownRootElement) { |
| 85 | + this.dropdownRootElement.removeEventListener('click', this.onRootClick, false); |
| 86 | + } |
| 87 | + if (e) { |
| 88 | + e.addEventListener('click', this.onRootClick, false); |
| 89 | + } |
| 90 | + this.dropdownRootElement = e; |
| 91 | + } |
| 92 | + |
| 93 | + _optionForNetwork(network, wire_onclick) { |
| 94 | + if (wire_onclick === undefined) wire_onclick = true; |
| 95 | + let icon; |
| 96 | + let name; |
| 97 | + let span_class; |
| 98 | + |
| 99 | + if (network === null) { |
| 100 | + name = 'All networks'; |
| 101 | + span_class = 'mx_NetworkDropdown_menu_all'; |
| 102 | + } else { |
| 103 | + name = this.props.config.networkNames[network]; |
| 104 | + icon = <img src={this.props.config.networkIcons[network]} />; |
| 105 | + span_class = 'mx_NetworkDropdown_menu_network'; |
| 106 | + } |
| 107 | + |
| 108 | + const click_handler = wire_onclick ? this.onNetworkClick.bind(this, network) : null; |
| 109 | + |
| 110 | + return <div key={network} className="mx_NetworkDropdown_networkoption" onClick={click_handler}> |
| 111 | + {icon} |
| 112 | + <span className={span_class}>{name}</span> |
| 113 | + </div>; |
| 114 | + } |
| 115 | + |
| 116 | + render() { |
| 117 | + const current_value = this._optionForNetwork(this.state.selectedNetwork, false); |
| 118 | + |
| 119 | + let menu; |
| 120 | + if (this.state.expanded) { |
| 121 | + const menu_options = [this._optionForNetwork(null)]; |
| 122 | + for (const network of this.props.config.networks) { |
| 123 | + menu_options.push(this._optionForNetwork(network)); |
| 124 | + } |
| 125 | + menu = <div className="mx_NetworkDropdown_menu"> |
| 126 | + {menu_options} |
| 127 | + </div>; |
| 128 | + } |
| 129 | + |
| 130 | + return <div className="mx_NetworkDropdown" ref={this.collectRoot}> |
| 131 | + <div className="mx_NetworkDropdown_input" onClick={this.onInputClick}> |
| 132 | + {current_value} |
| 133 | + <span className="mx_NetworkDropdown_arrow"></span> |
| 134 | + {menu} |
| 135 | + </div> |
| 136 | + </div>; |
| 137 | + } |
| 138 | +} |
| 139 | + |
| 140 | +NetworkDropdown.propTypes = { |
| 141 | + onNetworkChange: React.PropTypes.func.isRequired, |
| 142 | + config: React.PropTypes.object, |
| 143 | +}; |
| 144 | + |
| 145 | +NetworkDropdown.defaultProps = { |
| 146 | + config: { |
| 147 | + networks: [], |
| 148 | + } |
| 149 | +}; |
| 150 | + |
0 commit comments