2015-06-23 15:41:25 +00: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-16 17:18:55 +00:00
|
|
|
// should be atomised
|
2015-06-12 13:59:33 +00:00
|
|
|
var Loader = require("react-loader");
|
2015-06-09 16:40:42 +00:00
|
|
|
|
2015-07-02 12:18:03 +00:00
|
|
|
var MatrixClientPeg = require("../../MatrixClientPeg");
|
2015-07-08 17:18:03 +00:00
|
|
|
var RoomListSorter = require("../../RoomListSorter");
|
2015-06-09 16:40:42 +00:00
|
|
|
|
2015-06-19 11:53:48 +00:00
|
|
|
var dis = require("../../dispatcher");
|
2015-06-09 16:40:42 +00:00
|
|
|
|
2015-07-03 10:12:54 +00:00
|
|
|
var ComponentBroker = require('../../ComponentBroker');
|
|
|
|
|
|
|
|
var Notifier = ComponentBroker.get('organisms/Notifier');
|
|
|
|
|
2015-06-19 11:53:48 +00:00
|
|
|
module.exports = {
|
2015-06-11 17:25:29 +00:00
|
|
|
getInitialState: function() {
|
|
|
|
return {
|
2015-07-03 10:12:54 +00:00
|
|
|
logged_in: !!(MatrixClientPeg.get() && MatrixClientPeg.get().credentials),
|
2015-06-12 13:59:33 +00:00
|
|
|
ready: false
|
2015-06-11 17:25:29 +00:00
|
|
|
};
|
|
|
|
},
|
|
|
|
|
2015-06-12 13:59:33 +00:00
|
|
|
componentDidMount: function() {
|
2015-06-12 16:34:17 +00:00
|
|
|
this.dispatcherRef = dis.register(this.onAction);
|
2015-06-12 13:59:33 +00:00
|
|
|
if (this.state.logged_in) {
|
|
|
|
this.startMatrixClient();
|
|
|
|
}
|
2015-06-18 14:03:57 +00:00
|
|
|
this.focusComposer = false;
|
2015-06-25 16:41:55 +00:00
|
|
|
document.addEventListener("keydown", this.onKeyDown);
|
2015-07-07 12:30:38 +00:00
|
|
|
window.addEventListener("focus", this.onFocus);
|
2015-07-15 18:25:36 +00:00
|
|
|
if (this.state.logged_in) {
|
|
|
|
this.notifyNewScreen('');
|
|
|
|
} else {
|
|
|
|
this.notifyNewScreen('login');
|
|
|
|
}
|
2015-06-12 13:59:33 +00:00
|
|
|
},
|
|
|
|
|
2015-06-12 16:34:17 +00:00
|
|
|
componentWillUnmount: function() {
|
|
|
|
dis.unregister(this.dispatcherRef);
|
2015-06-25 16:41:55 +00:00
|
|
|
document.removeEventListener("keydown", this.onKeyDown);
|
2015-06-12 16:34:17 +00:00
|
|
|
},
|
|
|
|
|
2015-06-18 14:03:57 +00:00
|
|
|
componentDidUpdate: function() {
|
|
|
|
if (this.focusComposer) {
|
|
|
|
dis.dispatch({action: 'focus_composer'});
|
|
|
|
this.focusComposer = false;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2015-06-12 16:34:17 +00:00
|
|
|
onAction: function(payload) {
|
2015-06-25 16:41:55 +00:00
|
|
|
var roomIndexDelta = 1;
|
|
|
|
|
2015-06-12 16:34:17 +00:00
|
|
|
switch (payload.action) {
|
|
|
|
case 'logout':
|
2015-07-15 18:25:36 +00:00
|
|
|
this.replaceState({
|
2015-06-12 16:34:17 +00:00
|
|
|
logged_in: false,
|
|
|
|
ready: false
|
|
|
|
});
|
2015-07-13 18:14:02 +00:00
|
|
|
localStorage.removeItem("mx_hs_url");
|
|
|
|
localStorage.removeItem("mx_user_id");
|
|
|
|
localStorage.removeItem("mx_access_token");
|
2015-07-03 10:12:54 +00:00
|
|
|
Notifier.stop();
|
2015-07-02 12:18:03 +00:00
|
|
|
MatrixClientPeg.get().removeAllListeners();
|
|
|
|
MatrixClientPeg.replace(null);
|
2015-06-12 16:34:17 +00:00
|
|
|
break;
|
2015-07-13 18:14:02 +00:00
|
|
|
case 'start_registration':
|
|
|
|
if (this.state.logged_in) return;
|
2015-07-15 18:25:36 +00:00
|
|
|
this.replaceState({
|
2015-07-13 18:14:02 +00:00
|
|
|
screen: 'register'
|
|
|
|
});
|
2015-07-15 18:25:36 +00:00
|
|
|
this.notifyNewScreen('register');
|
2015-07-13 18:14:02 +00:00
|
|
|
break;
|
|
|
|
case 'start_login':
|
|
|
|
if (this.state.logged_in) return;
|
2015-07-15 18:25:36 +00:00
|
|
|
this.replaceState({
|
2015-07-13 18:14:02 +00:00
|
|
|
screen: 'login'
|
|
|
|
});
|
2015-07-15 18:25:36 +00:00
|
|
|
this.notifyNewScreen('login');
|
2015-07-13 18:14:02 +00:00
|
|
|
break;
|
2015-06-12 17:01:38 +00:00
|
|
|
case 'view_room':
|
2015-07-03 10:12:54 +00:00
|
|
|
this.focusComposer = true;
|
2015-06-12 17:01:38 +00:00
|
|
|
this.setState({
|
|
|
|
currentRoom: payload.room_id
|
|
|
|
});
|
|
|
|
break;
|
2015-06-25 16:41:55 +00:00
|
|
|
case 'view_prev_room':
|
|
|
|
roomIndexDelta = -1;
|
|
|
|
case 'view_next_room':
|
2015-07-08 18:47:58 +00:00
|
|
|
var allRooms = RoomListSorter.mostRecentActivityFirst(
|
|
|
|
MatrixClientPeg.get().getRooms()
|
|
|
|
);
|
2015-06-25 16:41:55 +00:00
|
|
|
var roomIndex = -1;
|
|
|
|
for (var i = 0; i < allRooms.length; ++i) {
|
|
|
|
if (allRooms[i].roomId == this.state.currentRoom) {
|
|
|
|
roomIndex = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
roomIndex = (roomIndex + roomIndexDelta) % allRooms.length;
|
|
|
|
this.setState({
|
|
|
|
currentRoom: allRooms[roomIndex].roomId
|
|
|
|
});
|
|
|
|
break;
|
2015-06-12 16:34:17 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2015-06-12 12:45:22 +00:00
|
|
|
onLoggedIn: function() {
|
2015-07-13 18:14:02 +00:00
|
|
|
this.setState({
|
2015-07-15 18:25:36 +00:00
|
|
|
screen: undefined,
|
2015-07-13 18:14:02 +00:00
|
|
|
logged_in: true
|
|
|
|
});
|
2015-06-12 13:59:33 +00:00
|
|
|
this.startMatrixClient();
|
2015-07-15 18:25:36 +00:00
|
|
|
this.notifyNewScreen('');
|
2015-06-12 13:59:33 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
startMatrixClient: function() {
|
2015-07-02 12:18:03 +00:00
|
|
|
var cli = MatrixClientPeg.get();
|
2015-06-12 13:59:33 +00:00
|
|
|
var that = this;
|
|
|
|
cli.on('syncComplete', function() {
|
2015-06-12 16:34:17 +00:00
|
|
|
var firstRoom = null;
|
|
|
|
if (cli.getRooms() && cli.getRooms().length) {
|
2015-07-08 17:21:57 +00:00
|
|
|
firstRoom = RoomListSorter.mostRecentActivityFirst(
|
2015-07-08 17:18:03 +00:00
|
|
|
cli.getRooms()
|
|
|
|
)[0].roomId;
|
2015-06-12 16:34:17 +00:00
|
|
|
}
|
|
|
|
that.setState({ready: true, currentRoom: firstRoom});
|
2015-06-18 14:03:57 +00:00
|
|
|
dis.dispatch({action: 'focus_composer'});
|
2015-06-12 13:59:33 +00:00
|
|
|
});
|
2015-07-03 10:12:54 +00:00
|
|
|
Notifier.start();
|
2015-06-12 13:59:33 +00:00
|
|
|
cli.startClient();
|
2015-06-11 17:25:29 +00:00
|
|
|
},
|
2015-06-25 16:41:55 +00:00
|
|
|
|
|
|
|
onKeyDown: function(ev) {
|
|
|
|
if (ev.altKey) {
|
|
|
|
switch (ev.keyCode) {
|
|
|
|
case 38:
|
|
|
|
dis.dispatch({action: 'view_prev_room'});
|
|
|
|
ev.stopPropagation();
|
|
|
|
break;
|
|
|
|
case 40:
|
|
|
|
dis.dispatch({action: 'view_next_room'});
|
|
|
|
ev.stopPropagation();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2015-07-07 12:30:38 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
onFocus: function(ev) {
|
|
|
|
dis.dispatch({action: 'focus_composer'});
|
2015-07-15 18:25:36 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
resumeRegistration(params) {
|
|
|
|
if (!params.hs_url) return false;
|
|
|
|
if (!params.is_url) return false;
|
|
|
|
if (!params.client_secret) return false;
|
|
|
|
if (!params.session_id) return false;
|
|
|
|
if (!params.sid) return false;
|
|
|
|
if (this.state.logged_in) return false;
|
|
|
|
|
|
|
|
this.setState({
|
|
|
|
screen: 'register',
|
|
|
|
register_client_secret: params.client_secret,
|
|
|
|
register_session_id: params.session_id,
|
|
|
|
register_hs_url: params.hs_url,
|
|
|
|
register_is_url: params.is_url,
|
|
|
|
register_id_sid: params.sid
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
notifyNewScreen: function(screen) {
|
|
|
|
if (this.props.onNewScreen) {
|
|
|
|
this.props.onNewScreen(screen);
|
|
|
|
}
|
2015-06-25 16:41:55 +00:00
|
|
|
}
|
2015-06-19 11:53:48 +00:00
|
|
|
};
|
2015-06-09 16:40:42 +00:00
|
|
|
|