2015-11-30 21:11:04 +03:00
|
|
|
/*
|
2016-01-07 07:06:39 +03:00
|
|
|
Copyright 2015, 2016 OpenMarket Ltd
|
2015-11-30 21:11:04 +03:00
|
|
|
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
var React = require('react');
|
|
|
|
var Modal = require('../../../Modal');
|
|
|
|
var sdk = require('../../../index');
|
2017-05-30 17:09:57 +03:00
|
|
|
import { _t } from '../../../languageHandler';
|
2015-11-30 21:11:04 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* A pure UI component which displays the HS and IS to use.
|
|
|
|
*/
|
|
|
|
module.exports = React.createClass({
|
|
|
|
displayName: 'ServerConfig',
|
|
|
|
|
|
|
|
propTypes: {
|
2017-04-21 13:37:08 +03:00
|
|
|
onServerConfigChange: React.PropTypes.func,
|
2016-03-15 16:48:46 +03:00
|
|
|
|
|
|
|
// default URLs are defined in config.json (or the hardcoded defaults)
|
|
|
|
// they are used if the user has not overridden them with a custom URL.
|
|
|
|
// In other words, if the custom URL is blank, the default is used.
|
2016-03-06 22:33:36 +03:00
|
|
|
defaultHsUrl: React.PropTypes.string, // e.g. https://matrix.org
|
|
|
|
defaultIsUrl: React.PropTypes.string, // e.g. https://vector.im
|
2016-03-15 16:48:46 +03:00
|
|
|
|
|
|
|
// custom URLs are explicitly provided by the user and override the
|
|
|
|
// default URLs. The user enters them via the component's input fields,
|
|
|
|
// which is reflected on these properties whenever on..UrlChanged fires.
|
|
|
|
// They are persisted in localStorage by MatrixClientPeg, and so can
|
|
|
|
// override the default URLs when the component initially loads.
|
|
|
|
customHsUrl: React.PropTypes.string,
|
|
|
|
customIsUrl: React.PropTypes.string,
|
|
|
|
|
2015-11-30 21:11:04 +03:00
|
|
|
withToggleButton: React.PropTypes.bool,
|
|
|
|
delayTimeMs: React.PropTypes.number // time to wait before invoking onChanged
|
|
|
|
},
|
|
|
|
|
|
|
|
getDefaultProps: function() {
|
|
|
|
return {
|
2017-04-21 13:37:08 +03:00
|
|
|
onServerConfigChange: function() {},
|
2016-03-15 18:20:51 +03:00
|
|
|
customHsUrl: "",
|
|
|
|
customIsUrl: "",
|
2015-11-30 21:11:04 +03:00
|
|
|
withToggleButton: false,
|
|
|
|
delayTimeMs: 0
|
|
|
|
};
|
|
|
|
},
|
|
|
|
|
|
|
|
getInitialState: function() {
|
|
|
|
return {
|
2016-03-15 16:48:46 +03:00
|
|
|
hs_url: this.props.customHsUrl,
|
|
|
|
is_url: this.props.customIsUrl,
|
2016-03-06 22:33:36 +03:00
|
|
|
// if withToggleButton is false, then show the config all the time given we have no way otherwise of making it visible
|
2017-01-20 17:22:27 +03:00
|
|
|
configVisible: !this.props.withToggleButton ||
|
2016-03-15 16:48:46 +03:00
|
|
|
(this.props.customHsUrl !== this.props.defaultHsUrl) ||
|
|
|
|
(this.props.customIsUrl !== this.props.defaultIsUrl)
|
2017-01-20 17:22:27 +03:00
|
|
|
};
|
2015-11-30 21:11:04 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
onHomeserverChanged: function(ev) {
|
|
|
|
this.setState({hs_url: ev.target.value}, function() {
|
|
|
|
this._hsTimeoutId = this._waitThenInvoke(this._hsTimeoutId, function() {
|
2016-03-06 22:33:36 +03:00
|
|
|
var hsUrl = this.state.hs_url.trim().replace(/\/$/, "");
|
|
|
|
if (hsUrl === "") hsUrl = this.props.defaultHsUrl;
|
2017-04-21 13:37:08 +03:00
|
|
|
this.props.onServerConfigChange({
|
|
|
|
hsUrl : this.state.hs_url,
|
|
|
|
isUrl : this.state.is_url,
|
|
|
|
});
|
2015-11-30 21:11:04 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
onIdentityServerChanged: function(ev) {
|
|
|
|
this.setState({is_url: ev.target.value}, function() {
|
|
|
|
this._isTimeoutId = this._waitThenInvoke(this._isTimeoutId, function() {
|
2016-03-06 22:33:36 +03:00
|
|
|
var isUrl = this.state.is_url.trim().replace(/\/$/, "");
|
|
|
|
if (isUrl === "") isUrl = this.props.defaultIsUrl;
|
2017-04-21 13:37:08 +03:00
|
|
|
this.props.onServerConfigChange({
|
|
|
|
hsUrl : this.state.hs_url,
|
|
|
|
isUrl : this.state.is_url,
|
|
|
|
});
|
2015-11-30 21:11:04 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
_waitThenInvoke: function(existingTimeoutId, fn) {
|
|
|
|
if (existingTimeoutId) {
|
|
|
|
clearTimeout(existingTimeoutId);
|
|
|
|
}
|
|
|
|
return setTimeout(fn.bind(this), this.props.delayTimeMs);
|
|
|
|
},
|
|
|
|
|
2016-03-15 16:48:46 +03:00
|
|
|
onServerConfigVisibleChange: function(visible, ev) {
|
2015-11-30 21:11:04 +03:00
|
|
|
this.setState({
|
2016-03-15 16:48:46 +03:00
|
|
|
configVisible: visible
|
2015-11-30 21:11:04 +03:00
|
|
|
});
|
2016-03-15 16:48:46 +03:00
|
|
|
if (!visible) {
|
2017-04-21 13:37:08 +03:00
|
|
|
this.props.onServerConfigChange({
|
|
|
|
hsUrl : this.props.defaultHsUrl,
|
|
|
|
isUrl : this.props.defaultIsUrl,
|
|
|
|
});
|
2016-03-06 22:33:36 +03:00
|
|
|
}
|
|
|
|
else {
|
2017-04-21 13:37:08 +03:00
|
|
|
this.props.onServerConfigChange({
|
|
|
|
hsUrl : this.state.hs_url,
|
|
|
|
isUrl : this.state.is_url,
|
|
|
|
});
|
2016-03-06 22:33:36 +03:00
|
|
|
}
|
2015-11-30 21:11:04 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
showHelpPopup: function() {
|
2015-12-01 19:49:15 +03:00
|
|
|
var CustomServerDialog = sdk.getComponent('login.CustomServerDialog');
|
|
|
|
Modal.createDialog(CustomServerDialog);
|
2015-11-30 21:11:04 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
render: function() {
|
|
|
|
var serverConfigStyle = {};
|
|
|
|
serverConfigStyle.display = this.state.configVisible ? 'block' : 'none';
|
|
|
|
|
|
|
|
var toggleButton;
|
|
|
|
if (this.props.withToggleButton) {
|
|
|
|
toggleButton = (
|
2017-06-02 02:21:00 +03:00
|
|
|
<div className="mx_ServerConfig_selector">
|
2016-03-15 16:48:46 +03:00
|
|
|
<input className="mx_Login_radio" id="basic" name="configVisible" type="radio"
|
|
|
|
checked={!this.state.configVisible}
|
|
|
|
onChange={this.onServerConfigVisibleChange.bind(this, false)} />
|
|
|
|
<label className="mx_Login_label" htmlFor="basic">
|
2017-05-30 17:09:57 +03:00
|
|
|
{_t("Default server")}
|
2016-03-15 16:48:46 +03:00
|
|
|
</label>
|
|
|
|
|
|
|
|
<input className="mx_Login_radio" id="advanced" name="configVisible" type="radio"
|
2015-11-30 21:11:04 +03:00
|
|
|
checked={this.state.configVisible}
|
2016-03-15 16:48:46 +03:00
|
|
|
onChange={this.onServerConfigVisibleChange.bind(this, true)} />
|
2015-11-30 21:11:04 +03:00
|
|
|
<label className="mx_Login_label" htmlFor="advanced">
|
2017-05-30 17:09:57 +03:00
|
|
|
{_t("Custom server")}
|
2015-11-30 21:11:04 +03:00
|
|
|
</label>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div>
|
|
|
|
{toggleButton}
|
|
|
|
<div style={serverConfigStyle}>
|
|
|
|
<div className="mx_ServerConfig">
|
|
|
|
<label className="mx_Login_label mx_ServerConfig_hslabel" htmlFor="hsurl">
|
2017-05-30 17:09:57 +03:00
|
|
|
{_t("Home server URL")}
|
2015-11-30 21:11:04 +03:00
|
|
|
</label>
|
|
|
|
<input className="mx_Login_field" id="hsurl" type="text"
|
2016-03-06 22:33:36 +03:00
|
|
|
placeholder={this.props.defaultHsUrl}
|
2016-03-16 22:14:28 +03:00
|
|
|
disabled={!this.props.withToggleButton}
|
2015-11-30 21:11:04 +03:00
|
|
|
value={this.state.hs_url}
|
|
|
|
onChange={this.onHomeserverChanged} />
|
|
|
|
<label className="mx_Login_label mx_ServerConfig_islabel" htmlFor="isurl">
|
2017-05-30 17:09:57 +03:00
|
|
|
{_t("Identity server URL")}
|
2015-11-30 21:11:04 +03:00
|
|
|
</label>
|
|
|
|
<input className="mx_Login_field" id="isurl" type="text"
|
2016-03-06 22:33:36 +03:00
|
|
|
placeholder={this.props.defaultIsUrl}
|
2016-03-16 22:14:28 +03:00
|
|
|
disabled={!this.props.withToggleButton}
|
2015-11-30 21:11:04 +03:00
|
|
|
value={this.state.is_url}
|
|
|
|
onChange={this.onIdentityServerChanged} />
|
|
|
|
<a className="mx_ServerConfig_help" href="#" onClick={this.showHelpPopup}>
|
2017-05-30 17:09:57 +03:00
|
|
|
{_t("What does this mean?")}
|
2015-11-30 21:11:04 +03:00
|
|
|
</a>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
});
|