2015-06-23 17:41:25 +02:00
/ *
Copyright 2015 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 .
* /
'use strict' ;
2015-06-19 13:53:48 +02:00
var React = require ( 'react' ) ;
2015-06-23 15:40:50 +02:00
var ComponentBroker = require ( "../../../../src/ComponentBroker" ) ;
2015-07-16 22:46:39 +02:00
var MatrixClientPeg = require ( "../../../../src/MatrixClientPeg" ) ;
2015-06-19 13:53:48 +02:00
var ProgressBar = ComponentBroker . get ( "molecules/ProgressBar" ) ;
var Loader = require ( "react-loader" ) ;
2015-06-23 15:40:50 +02:00
var LoginController = require ( "../../../../src/controllers/templates/Login" ) ;
2015-06-19 13:53:48 +02:00
2015-07-16 13:44:04 +02:00
var ServerConfig = ComponentBroker . get ( "molecules/ServerConfig" ) ;
2015-06-19 13:53:48 +02:00
module . exports = React . createClass ( {
2015-07-16 18:51:21 +02:00
DEFAULT _HS _URL : 'https://matrix.org' ,
DEFAULT _IS _URL : 'https://matrix.org' ,
2015-06-19 17:21:09 +02:00
displayName : 'Login' ,
2015-06-19 13:53:48 +02:00
mixins : [ LoginController ] ,
2015-07-16 18:51:21 +02:00
getInitialState : function ( ) {
return {
serverConfigVisible : false
} ;
} ,
componentWillMount : function ( ) {
this . onHSChosen ( ) ;
this . customHsUrl = this . DEFAULT _HS _URL ;
this . customIsUrl = this . DEFAULT _IS _URL ;
} ,
2015-07-16 17:49:40 +02:00
getHsUrl : function ( ) {
2015-07-16 18:51:21 +02:00
if ( this . state . serverConfigVisible ) {
2015-07-16 22:46:39 +02:00
return this . customHsUrl ;
2015-07-16 18:51:21 +02:00
} else {
return this . DEFAULT _HS _URL ;
}
2015-07-16 17:49:40 +02:00
} ,
getIsUrl : function ( ) {
2015-07-16 18:51:21 +02:00
if ( this . state . serverConfigVisible ) {
2015-07-16 22:46:39 +02:00
return this . customIsUrl ;
2015-07-16 18:51:21 +02:00
} else {
return this . DEFAULT _IS _URL ;
}
} ,
onServerConfigVisibleChange : function ( ev ) {
this . setState ( {
serverConfigVisible : ev . target . checked
2015-07-16 22:46:39 +02:00
} , this . onHsUrlChanged ) ;
2015-07-16 17:49:40 +02:00
} ,
/ * *
* Gets the form field values for the current login stage
* /
getFormVals : function ( ) {
return {
'username' : this . refs . user . getDOMNode ( ) . value ,
'password' : this . refs . pass . getDOMNode ( ) . value
} ;
} ,
2015-07-16 18:51:21 +02:00
onHsUrlChanged : function ( ) {
2015-07-16 22:46:39 +02:00
this . customHsUrl = this . refs . serverConfig . getHsUrl ( ) ;
this . customIsUrl = this . refs . serverConfig . getIsUrl ( ) ;
MatrixClientPeg . replaceUsingUrls (
this . getHsUrl ( ) ,
this . getIsUrl ( )
) ;
this . setState ( {
hs _url : this . getHsUrl ( ) ,
is _url : this . getIsUrl ( )
} ) ;
// XXX: HSes do not have to offer password auth, so we
// need to update and maybe show a different component
// when a new HS is entered.
/ * i f ( t h i s . u p d a t e H s T i m e o u t ) {
2015-07-16 18:51:21 +02:00
clearTimeout ( this . updateHsTimeout ) ;
}
2015-07-16 22:46:39 +02:00
var self = this ;
2015-07-16 18:51:21 +02:00
this . updateHsTimeout = setTimeout ( function ( ) {
self . onHSChosen ( ) ;
} , 500 ) ; * /
} ,
2015-07-16 13:44:04 +02:00
componentForStep : function ( step ) {
switch ( step ) {
case 'choose_hs' :
2015-07-16 18:51:21 +02:00
var serverConfigStyle = { } ;
2015-07-19 04:30:41 +02:00
serverConfigStyle . display = this . state . serverConfigVisible ? 'block' : 'none' ;
2015-07-16 13:44:04 +02:00
return (
< div >
2015-07-19 04:30:41 +02:00
< input className = "mx_Login_checkbox" id = "advanced" type = "checkbox" value = { this . state . serverConfigVisible } onChange = { this . onServerConfigVisibleChange } / >
2015-07-19 04:19:37 +02:00
< label className = "mx_Login_label" htmlFor = "advanced" > Use custom server options ( advanced ) < / l a b e l >
2015-07-16 18:51:21 +02:00
< div style = { serverConfigStyle } >
2015-07-19 04:19:37 +02:00
< ServerConfig ref = "serverConfig"
defaultHsUrl = { this . customHsUrl } defaultIsUrl = { this . customIsUrl }
onHsUrlChanged = { this . onHsUrlChanged }
/ >
2015-07-16 18:51:21 +02:00
< / d i v >
2015-07-16 13:44:04 +02:00
< / d i v >
) ;
// XXX: clearly these should be separate organisms
case 'stage_m.login.password' :
return (
< div >
< form onSubmit = { this . onUserPassEntered } >
2015-07-20 00:00:46 +02:00
< input className = "mx_Login_field" ref = "user" type = "text" value = { this . state . username } onChange = { this . onUsernameChanged } placeholder = "Email or user name" / > < br / >
< input className = "mx_Login_field" ref = "pass" type = "password" value = { this . state . password } onChange = { this . onPasswordChanged } placeholder = "Password" / > < br / >
2015-07-16 18:51:21 +02:00
{ this . componentForStep ( 'choose_hs' ) }
2015-07-19 04:19:37 +02:00
< input className = "mx_Login_submit" type = "submit" value = "Log in" / >
2015-07-16 13:44:04 +02:00
< / f o r m >
< / d i v >
) ;
}
} ,
2015-07-20 00:00:46 +02:00
onUsernameChanged : function ( ev ) {
this . setState ( { username : ev . target . value } ) ;
} ,
onPasswordChanged : function ( ev ) {
this . setState ( { password : ev . target . value } ) ;
} ,
2015-06-19 13:53:48 +02:00
loginContent : function ( ) {
if ( this . state . busy ) {
return (
< Loader / >
) ;
} else {
return (
< div >
2015-07-19 04:19:37 +02:00
< h2 > Sign in < / h 2 >
2015-06-19 13:53:48 +02:00
{ this . componentForStep ( this . state . step ) }
2015-07-19 04:19:37 +02:00
< div className = "mx_Login_error" > { this . state . errorText } < / d i v >
< a className = "mx_Login_create" onClick = { this . showRegister } href = "#" > Create a new account < / a >
2015-06-19 13:53:48 +02:00
< / d i v >
) ;
}
} ,
render : function ( ) {
return (
< div className = "mx_Login" >
2015-07-19 04:19:37 +02:00
< div className = "mx_Login_box" >
< div className = "mx_Login_logo" >
2015-07-23 19:24:34 +02:00
< img src = "img/logo.png" width = "249" height = "78" alt = "vector" / >
2015-07-19 04:19:37 +02:00
< / d i v >
{ this . loginContent ( ) }
< / d i v >
2015-06-19 13:53:48 +02:00
< / d i v >
) ;
}
} ) ;