@@ -15,36 +15,61 @@ limitations under the License.
1515*/
1616
1717import React from 'react' ;
18+ import MatrixClientPeg from 'matrix-react-sdk/lib/MatrixClientPeg' ;
1819
1920export default class NetworkDropdown extends React . Component {
20- constructor ( ) {
21- super ( ) ;
21+ constructor ( props ) {
22+ super ( props ) ;
2223
2324 this . dropdownRootElement = null ;
2425 this . ignoreEvent = null ;
2526
2627 this . onInputClick = this . onInputClick . bind ( this ) ;
2728 this . onRootClick = this . onRootClick . bind ( this ) ;
2829 this . onDocumentClick = this . onDocumentClick . bind ( this ) ;
29- this . onNetworkClick = this . onNetworkClick . bind ( this ) ;
30+ this . onMenuOptionClick = this . onMenuOptionClick . bind ( this ) ;
31+ this . onInputKeyUp = this . onInputKeyUp . bind ( this ) ;
3032 this . collectRoot = this . collectRoot . bind ( this ) ;
33+ this . collectInputTextBox = this . collectInputTextBox . bind ( this ) ;
34+
35+ this . inputTextBox = null ;
36+
37+ let defaultNetwork = null ;
38+ if (
39+ this . props . config . serverConfig &&
40+ this . props . config . serverConfig [ server ] &&
41+ this . props . config . serverConfig [ server ] . networks &&
42+ '_matrix' in this . props . config . serverConfig [ server ] . networks
43+ ) {
44+ defaultNetwork = '_matrix' ;
45+ }
3146
3247 this . state = {
3348 expanded : false ,
34- selectedNetwork : null ,
49+ selectedServer : MatrixClientPeg . getHomeServerName ( ) ,
50+ selectedNetwork : defaultNetwork ,
3551 } ;
3652 }
3753
3854 componentWillMount ( ) {
3955 // Listen for all clicks on the document so we can close the
4056 // menu when the user clicks somewhere else
4157 document . addEventListener ( 'click' , this . onDocumentClick , false ) ;
58+
59+ // fire this now so the defaults can be set up
60+ this . props . onOptionChange ( this . state . selectedServer , this . state . selectedNetwork ) ;
4261 }
4362
4463 componentWillUnmount ( ) {
4564 document . removeEventListener ( 'click' , this . onDocumentClick , false ) ;
4665 }
4766
67+ componentDidUpdate ( ) {
68+ if ( this . state . expanded && this . inputTextBox ) {
69+ this . inputTextBox . focus ( ) ;
70+ }
71+ }
72+
4873 onDocumentClick ( ev ) {
4974 // Close the dropdown if the user clicks anywhere that isn't
5075 // within our root element
@@ -72,12 +97,24 @@ export default class NetworkDropdown extends React.Component {
7297 ev . preventDefault ( ) ;
7398 }
7499
75- onNetworkClick ( network , ev ) {
100+ onMenuOptionClick ( server , network , ev ) {
76101 this . setState ( {
77102 expanded : false ,
103+ selectedServer : server ,
78104 selectedNetwork : network ,
79105 } ) ;
80- this . props . onNetworkChange ( network ) ;
106+ this . props . onOptionChange ( server , network ) ;
107+ }
108+
109+ onInputKeyUp ( e ) {
110+ if ( e . key == 'Enter' ) {
111+ this . setState ( {
112+ expanded : false ,
113+ selectedServer : e . target . value ,
114+ selectedNetwork : null ,
115+ } ) ;
116+ this . props . onOptionChange ( e . target . value , null ) ;
117+ }
81118 }
82119
83120 collectRoot ( e ) {
@@ -90,41 +127,82 @@ export default class NetworkDropdown extends React.Component {
90127 this . dropdownRootElement = e ;
91128 }
92129
93- _optionForNetwork ( network , wire_onclick ) {
130+ collectInputTextBox ( e ) {
131+ this . inputTextBox = e ;
132+ }
133+
134+ _getMenuOptions ( ) {
135+ const options = [ ] ;
136+
137+ let servers = [ ] ;
138+ if ( this . props . config . servers ) {
139+ servers = servers . concat ( this . props . config . servers ) ;
140+ }
141+
142+ if ( servers . indexOf ( MatrixClientPeg . getHomeServerName ( ) ) == - 1 ) {
143+ servers . unshift ( MatrixClientPeg . getHomeServerName ( ) ) ;
144+ }
145+
146+ for ( const server of servers ) {
147+ options . push ( this . _makeMenuOption ( server , null ) ) ;
148+ if ( this . props . config . serverConfig && this . props . config . serverConfig [ server ] && this . props . config . serverConfig [ server ] . networks ) {
149+ for ( const network of this . props . config . serverConfig [ server ] . networks ) {
150+ options . push ( this . _makeMenuOption ( server , network ) ) ;
151+ }
152+ }
153+ }
154+
155+ return options ;
156+ }
157+
158+ _makeMenuOption ( server , network , wire_onclick ) {
94159 if ( wire_onclick === undefined ) wire_onclick = true ;
95160 let icon ;
96161 let name ;
97162 let span_class ;
98163
99164 if ( network === null ) {
100- name = 'All networks' ;
165+ name = server ;
101166 span_class = 'mx_NetworkDropdown_menu_all' ;
167+ } else if ( network == '_matrix' ) {
168+ name = 'Matrix' ;
169+ icon = < img src = "/img/network-matrix.svg" width = "16" height = "16" /> ;
170+ span_class = 'mx_NetworkDropdown_menu_network' ;
102171 } else {
103172 name = this . props . config . networkNames [ network ] ;
104173 icon = < img src = { this . props . config . networkIcons [ network ] } /> ;
105174 span_class = 'mx_NetworkDropdown_menu_network' ;
106175 }
107176
108- const click_handler = wire_onclick ? this . onNetworkClick . bind ( this , network ) : null ;
177+ const click_handler = wire_onclick ? this . onMenuOptionClick . bind ( this , server , network ) : null ;
109178
110- return < div key = { network } className = "mx_NetworkDropdown_networkoption" onClick = { click_handler } >
179+ let key = server ;
180+ if ( network !== null ) {
181+ key += '_' + network ;
182+ }
183+
184+ return < div key = { key } className = "mx_NetworkDropdown_networkoption" onClick = { click_handler } >
111185 { icon }
112186 < span className = { span_class } > { name } </ span >
113187 </ div > ;
114188 }
115189
116190 render ( ) {
117- const current_value = this . _optionForNetwork ( this . state . selectedNetwork , false ) ;
191+ let current_value ;
118192
119193 let menu ;
120194 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- }
195+ const menu_options = this . _getMenuOptions ( ) ;
125196 menu = < div className = "mx_NetworkDropdown_menu" >
126197 { menu_options }
127198 </ div > ;
199+ current_value = < input type = "text" className = "mx_NetworkDropdown_networkoption"
200+ ref = { this . collectInputTextBox } onKeyUp = { this . onInputKeyUp }
201+ />
202+ } else {
203+ current_value = this . _makeMenuOption (
204+ this . state . selectedServer , this . state . selectedNetwork , false
205+ ) ;
128206 }
129207
130208 return < div className = "mx_NetworkDropdown" ref = { this . collectRoot } >
@@ -138,7 +216,7 @@ export default class NetworkDropdown extends React.Component {
138216}
139217
140218NetworkDropdown . propTypes = {
141- onNetworkChange : React . PropTypes . func . isRequired ,
219+ onOptionChange : React . PropTypes . func . isRequired ,
142220 config : React . PropTypes . object ,
143221} ;
144222
0 commit comments