-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2219 from vector-im/dbkr/directory_network_selector
Directory network selector
- Loading branch information
Showing
6 changed files
with
325 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,150 @@ | ||
/* | ||
Copyright 2016 OpenMarket Ltd | ||
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'; | ||
|
||
export default class NetworkDropdown extends React.Component { | ||
constructor() { | ||
super(); | ||
|
||
this.dropdownRootElement = null; | ||
this.ignoreEvent = null; | ||
|
||
this.onInputClick = this.onInputClick.bind(this); | ||
this.onRootClick = this.onRootClick.bind(this); | ||
this.onDocumentClick = this.onDocumentClick.bind(this); | ||
this.onNetworkClick = this.onNetworkClick.bind(this); | ||
this.collectRoot = this.collectRoot.bind(this); | ||
|
||
this.state = { | ||
expanded: false, | ||
selectedNetwork: null, | ||
}; | ||
} | ||
|
||
componentWillMount() { | ||
// Listen for all clicks on the document so we can close the | ||
// menu when the user clicks somewhere else | ||
document.addEventListener('click', this.onDocumentClick, false); | ||
} | ||
|
||
componentWillUnmount() { | ||
document.removeEventListener('click', this.onDocumentClick, false); | ||
} | ||
|
||
onDocumentClick(ev) { | ||
// Close the dropdown if the user clicks anywhere that isn't | ||
// within our root element | ||
if (ev !== this.ignoreEvent) { | ||
this.setState({ | ||
expanded: false, | ||
}); | ||
} | ||
} | ||
|
||
onRootClick(ev) { | ||
// This captures any clicks that happen within our elements, | ||
// such that we can then ignore them when they're seen by the | ||
// click listener on the document handler, ie. not close the | ||
// dropdown immediately after opening it. | ||
// NB. We can't just stopPropagation() because then the event | ||
// doesn't reach the React onClick(). | ||
this.ignoreEvent = ev; | ||
} | ||
|
||
onInputClick(ev) { | ||
this.setState({ | ||
expanded: !this.state.expanded, | ||
}); | ||
ev.preventDefault(); | ||
} | ||
|
||
onNetworkClick(network, ev) { | ||
this.setState({ | ||
expanded: false, | ||
selectedNetwork: network, | ||
}); | ||
this.props.onNetworkChange(network); | ||
} | ||
|
||
collectRoot(e) { | ||
if (this.dropdownRootElement) { | ||
this.dropdownRootElement.removeEventListener('click', this.onRootClick, false); | ||
} | ||
if (e) { | ||
e.addEventListener('click', this.onRootClick, false); | ||
} | ||
this.dropdownRootElement = e; | ||
} | ||
|
||
_optionForNetwork(network, wire_onclick) { | ||
if (wire_onclick === undefined) wire_onclick = true; | ||
let icon; | ||
let name; | ||
let span_class; | ||
|
||
if (network === null) { | ||
name = 'All networks'; | ||
span_class = 'mx_NetworkDropdown_menu_all'; | ||
} else { | ||
name = this.props.config.networkNames[network]; | ||
icon = <img src={this.props.config.networkIcons[network]} />; | ||
span_class = 'mx_NetworkDropdown_menu_network'; | ||
} | ||
|
||
const click_handler = wire_onclick ? this.onNetworkClick.bind(this, network) : null; | ||
|
||
return <div key={network} className="mx_NetworkDropdown_networkoption" onClick={click_handler}> | ||
{icon} | ||
<span className={span_class}>{name}</span> | ||
</div>; | ||
} | ||
|
||
render() { | ||
const current_value = this._optionForNetwork(this.state.selectedNetwork, false); | ||
|
||
let menu; | ||
if (this.state.expanded) { | ||
const menu_options = [this._optionForNetwork(null)]; | ||
for (const network of this.props.config.networks) { | ||
menu_options.push(this._optionForNetwork(network)); | ||
} | ||
menu = <div className="mx_NetworkDropdown_menu"> | ||
{menu_options} | ||
</div>; | ||
} | ||
|
||
return <div className="mx_NetworkDropdown" ref={this.collectRoot}> | ||
<div className="mx_NetworkDropdown_input" onClick={this.onInputClick}> | ||
{current_value} | ||
<span className="mx_NetworkDropdown_arrow"></span> | ||
{menu} | ||
</div> | ||
</div>; | ||
} | ||
} | ||
|
||
NetworkDropdown.propTypes = { | ||
onNetworkChange: React.PropTypes.func.isRequired, | ||
config: React.PropTypes.object, | ||
}; | ||
|
||
NetworkDropdown.defaultProps = { | ||
config: { | ||
networks: [], | ||
} | ||
}; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
src/skins/vector/css/vector-web/views/directory/NetworkDropdown.css
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
/* | ||
Copyright 2015, 2016 OpenMarket Ltd | ||
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. | ||
*/ | ||
|
||
.mx_NetworkDropdown { | ||
position: relative; | ||
} | ||
|
||
.mx_NetworkDropdown_input { | ||
position: relative; | ||
border-radius: 3px; | ||
border: 1px solid #c7c7c7; | ||
font-weight: 300; | ||
font-size: 13px; | ||
margin-top: 12px; | ||
margin-bottom: 12px; | ||
user-select: none; | ||
} | ||
|
||
.mx_NetworkDropdown_arrow { | ||
border-color: #4a4a4a transparent transparent; | ||
border-style: solid; | ||
border-width: 5px 5px 0; | ||
display: block; | ||
height: 0; | ||
position: absolute; | ||
right: 10px; | ||
top: 14px; | ||
width: 0 | ||
} | ||
|
||
.mx_NetworkDropdown_networkoption { | ||
height: 35px; | ||
line-height: 35px; | ||
padding-left: 8px; | ||
padding-right: 8px; | ||
} | ||
|
||
.mx_NetworkDropdown_networkoption img { | ||
margin: 5px; | ||
width: 25px; | ||
vertical-align: middle; | ||
} | ||
|
||
.mx_NetworkDropdown_menu { | ||
position: absolute; | ||
left: -1px; | ||
right: -1px; | ||
top: 100%; | ||
z-index: 2; | ||
margin: 0; | ||
padding: 0px; | ||
border-radius: 3px; | ||
border: 1px solid #76cfa6; | ||
background-color: white; | ||
} | ||
|
||
.mx_NetworkDropdown_menu .mx_NetworkDropdown_networkoption:hover { | ||
background-color: #ddd; | ||
} | ||
|
||
.mx_NetworkDropdown_menu_network { | ||
font-weight: bold; | ||
} | ||
|
Oops, something went wrong.