@@ -15,6 +15,7 @@ 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 {
2021 constructor ( ) {
@@ -26,19 +27,27 @@ export default class NetworkDropdown extends React.Component {
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 ;
3136
3237 this . state = {
3338 expanded : false ,
34- selectedNetwork : null ,
39+ selectedServer : MatrixClientPeg . getHomeServerName ( ) ,
40+ selectedNetwork : '_matrix' ,
3541 } ;
3642 }
3743
3844 componentWillMount ( ) {
3945 // Listen for all clicks on the document so we can close the
4046 // menu when the user clicks somewhere else
4147 document . addEventListener ( 'click' , this . onDocumentClick , false ) ;
48+
49+ // fire this now so the defaults can be set up
50+ this . props . onOptionChange ( this . state . selectedServer , this . state . selectedNetwork ) ;
4251 }
4352
4453 componentWillUnmount ( ) {
@@ -72,12 +81,24 @@ export default class NetworkDropdown extends React.Component {
7281 ev . preventDefault ( ) ;
7382 }
7483
75- onNetworkClick ( network , ev ) {
84+ onMenuOptionClick ( server , network , ev ) {
7685 this . setState ( {
7786 expanded : false ,
87+ selectedServer : server ,
7888 selectedNetwork : network ,
7989 } ) ;
80- this . props . onNetworkChange ( network ) ;
90+ this . props . onOptionChange ( server , network ) ;
91+ }
92+
93+ onInputKeyUp ( e ) {
94+ if ( e . key == 'Enter' ) {
95+ this . setState ( {
96+ expanded : false ,
97+ selectedServer : e . target . value ,
98+ selectedNetwork : null ,
99+ } ) ;
100+ this . props . onOptionChange ( e . target . value , null ) ;
101+ }
81102 }
82103
83104 collectRoot ( e ) {
@@ -90,41 +111,86 @@ export default class NetworkDropdown extends React.Component {
90111 this . dropdownRootElement = e ;
91112 }
92113
93- _optionForNetwork ( network , wire_onclick ) {
114+ collectInputTextBox ( e ) {
115+ this . inputTextBox = e ;
116+ }
117+
118+ _getMenuOptions ( ) {
119+ const options = [ ] ;
120+
121+ let servers = [ ] ;
122+ if ( this . props . config . servers ) {
123+ servers = servers . concat ( this . props . config . servers ) ;
124+ }
125+
126+ if ( servers . indexOf ( MatrixClientPeg . getHomeServerName ( ) ) == - 1 ) {
127+ servers . unshift ( MatrixClientPeg . getHomeServerName ( ) ) ;
128+ }
129+
130+ for ( const server of servers ) {
131+ options . push ( this . _makeMenuOption ( server , null ) ) ;
132+ if ( this . props . config . serverConfig && this . props . config . serverConfig [ server ] && this . props . config . serverConfig [ server ] . networks ) {
133+ for ( const network of this . props . config . serverConfig [ server ] . networks ) {
134+ options . push ( this . _makeMenuOption ( server , network ) ) ;
135+ }
136+ }
137+ }
138+
139+ return options ;
140+ }
141+
142+ _makeMenuOption ( server , network , wire_onclick ) {
94143 if ( wire_onclick === undefined ) wire_onclick = true ;
95144 let icon ;
96145 let name ;
97146 let span_class ;
98147
99148 if ( network === null ) {
100- name = 'All networks' ;
149+ name = server ;
101150 span_class = 'mx_NetworkDropdown_menu_all' ;
151+ } else if ( network == '_matrix' ) {
152+ name = 'Matrix' ;
153+ icon = < img src = "/img/network-matrix.svg" width = "16" height = "16" /> ;
154+ span_class = 'mx_NetworkDropdown_menu_network' ;
102155 } else {
103156 name = this . props . config . networkNames [ network ] ;
104157 icon = < img src = { this . props . config . networkIcons [ network ] } /> ;
105158 span_class = 'mx_NetworkDropdown_menu_network' ;
106159 }
107160
108- const click_handler = wire_onclick ? this . onNetworkClick . bind ( this , network ) : null ;
161+ const click_handler = wire_onclick ? this . onMenuOptionClick . bind ( this , server , network ) : null ;
109162
110- return < div key = { network } className = "mx_NetworkDropdown_networkoption" onClick = { click_handler } >
163+ let key = server ;
164+ if ( network !== null ) {
165+ key += '_' + network ;
166+ }
167+
168+ return < div key = { key } className = "mx_NetworkDropdown_networkoption" onClick = { click_handler } >
111169 { icon }
112170 < span className = { span_class } > { name } </ span >
113171 </ div > ;
114172 }
115173
174+ componentDidUpdate ( ) {
175+ if ( this . state . expanded && this . inputTextBox ) {
176+ this . inputTextBox . focus ( ) ;
177+ }
178+ }
179+
116180 render ( ) {
117- const current_value = this . _optionForNetwork ( this . state . selectedNetwork , false ) ;
181+ let current_value = this . _makeMenuOption (
182+ this . state . selectedServer , this . state . selectedNetwork , false
183+ ) ;
118184
119185 let menu ;
120186 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- }
187+ const menu_options = this . _getMenuOptions ( ) ;
125188 menu = < div className = "mx_NetworkDropdown_menu" >
126189 { menu_options }
127190 </ div > ;
191+ current_value = < input type = "text" className = "mx_NetworkDropdown_networkoption"
192+ ref = { this . collectInputTextBox } onKeyUp = { this . onInputKeyUp }
193+ />
128194 }
129195
130196 return < div className = "mx_NetworkDropdown" ref = { this . collectRoot } >
@@ -138,7 +204,7 @@ export default class NetworkDropdown extends React.Component {
138204}
139205
140206NetworkDropdown . propTypes = {
141- onNetworkChange : React . PropTypes . func . isRequired ,
207+ onOptionChange : React . PropTypes . func . isRequired ,
142208 config : React . PropTypes . object ,
143209} ;
144210
0 commit comments