Put network list config into config file

This commit is contained in:
David Baker 2016-09-15 17:20:13 +01:00
parent f3cbb9fe90
commit c1e83da35d
2 changed files with 33 additions and 37 deletions

View File

@ -32,16 +32,21 @@ var sanitizeHtml = require('sanitize-html');
linkifyMatrix(linkify); linkifyMatrix(linkify);
const NETWORK_PATTERNS = {
'gitter': /#gitter_.*/,
'irc:freenode': /#freenode_.*:.*/,
'irc:mozilla': /#mozilla_.*:.*/,
'irc:w3c': /@w3c_.*:.*/,
};
module.exports = React.createClass({ module.exports = React.createClass({
displayName: 'RoomDirectory', displayName: 'RoomDirectory',
propTypes: {
config: React.PropTypes.object,
},
getDefaultProps: function() {
return {
config: {
networks: [],
},
}
},
getInitialState: function() { getInitialState: function() {
return { return {
publicRooms: [], publicRooms: [],
@ -52,6 +57,14 @@ module.exports = React.createClass({
}, },
componentWillMount: function() { componentWillMount: function() {
// precompile Regexps
this.networkPatterns = {};
if (this.props.config.networkPatterns) {
for (const network of Object.keys(this.props.config.networkPatterns)) {
this.networkPatterns[network] = new RegExp(this.props.config.networkPatterns[network]);
}
}
// dis.dispatch({ // dis.dispatch({
// action: 'ui_opacity', // action: 'ui_opacity',
// sideOpacity: 0.3, // sideOpacity: 0.3,
@ -291,8 +304,8 @@ module.exports = React.createClass({
_networkForRoom(room) { _networkForRoom(room) {
if (room.aliases) { if (room.aliases) {
for (const alias of room.aliases) { for (const alias of room.aliases) {
for (const network of Object.keys(NETWORK_PATTERNS)) { for (const network of Object.keys(this.networkPatterns)) {
if (NETWORK_PATTERNS[network].test(alias)) return network; if (this.networkPatterns[network].test(alias)) return network;
} }
} }
} }
@ -318,7 +331,7 @@ module.exports = React.createClass({
<div className="mx_RoomDirectory_list"> <div className="mx_RoomDirectory_list">
<div className="mx_RoomDirectory_listheader"> <div className="mx_RoomDirectory_listheader">
<input ref="roomAlias" placeholder="Join a room (e.g. #foo:domain.com)" className="mx_RoomDirectory_input" size="64" onKeyUp={ this.onKeyUp }/> <input ref="roomAlias" placeholder="Join a room (e.g. #foo:domain.com)" className="mx_RoomDirectory_input" size="64" onKeyUp={ this.onKeyUp }/>
<NetworkDropdown onNetworkChange={this.onNetworkChange} /> <NetworkDropdown config={this.props.config} onNetworkChange={this.onNetworkChange} />
</div> </div>
<GeminiScrollbar className="mx_RoomDirectory_tableWrapper"> <GeminiScrollbar className="mx_RoomDirectory_tableWrapper">
<table ref="directory_table" className="mx_RoomDirectory_table"> <table ref="directory_table" className="mx_RoomDirectory_table">

View File

@ -33,30 +33,6 @@ export default class NetworkDropdown extends React.Component {
expanded: false, expanded: false,
selectedNetwork: null, selectedNetwork: null,
}; };
this.networks = [
'matrix:matrix_org',
'gitter',
'irc:freenode',
'irc:mozilla',
'irc:w3c',
];
this.networkNames = {
'matrix:matrix_org': 'matrix.org',
'irc:freenode': 'Freenode',
'irc:mozilla': 'Mozilla',
'irc:w3c': 'W3C',
'gitter': 'Gitter',
};
this.networkIcons = {
'matrix:matrix_org': '//matrix.org/favicon.ico',
'irc:freenode': '//matrix.org/_matrix/media/v1/download/matrix.org/DHLHpDDgWNNejFmrewvwEAHX',
'irc:mozilla': '//matrix.org/_matrix/media/v1/download/matrix.org/DHLHpDDgWNNejFmrewvwEAHX',
'irc:w3c': '//matrix.org/_matrix/media/v1/download/matrix.org/DHLHpDDgWNNejFmrewvwEAHX',
'gitter': '//gitter.im/favicon.ico',
};
} }
componentWillMount() { componentWillMount() {
@ -124,8 +100,8 @@ export default class NetworkDropdown extends React.Component {
name = 'All networks'; name = 'All networks';
span_class = 'mx_NetworkDropdown_menu_all'; span_class = 'mx_NetworkDropdown_menu_all';
} else { } else {
name = this.networkNames[network]; name = this.props.config.networkNames[network];
icon = <img src={this.networkIcons[network]} />; icon = <img src={this.props.config.networkIcons[network]} />;
span_class = 'mx_NetworkDropdown_menu_network'; span_class = 'mx_NetworkDropdown_menu_network';
} }
@ -143,7 +119,7 @@ export default class NetworkDropdown extends React.Component {
let menu; let menu;
if (this.state.expanded) { if (this.state.expanded) {
const menu_options = [this._optionForNetwork(null)]; const menu_options = [this._optionForNetwork(null)];
for (const network of this.networks) { for (const network of this.props.config.networks) {
menu_options.push(this._optionForNetwork(network)); menu_options.push(this._optionForNetwork(network));
} }
menu = <div className="mx_NetworkDropdown_menu"> menu = <div className="mx_NetworkDropdown_menu">
@ -163,5 +139,12 @@ export default class NetworkDropdown extends React.Component {
NetworkDropdown.propTypes = { NetworkDropdown.propTypes = {
onNetworkChange: React.PropTypes.func.isRequired, onNetworkChange: React.PropTypes.func.isRequired,
config: React.PropTypes.object,
};
NetworkDropdown.defaultProps = {
config: {
networks: [],
}
}; };