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-06-19 11:53:48 +00:00
|
|
|
var mxCliPeg = require("../../MatrixClientPeg");
|
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-06-19 11:53:48 +00:00
|
|
|
module.exports = {
|
2015-06-11 17:25:29 +00:00
|
|
|
getInitialState: function() {
|
|
|
|
return {
|
2015-06-12 13:59:33 +00:00
|
|
|
logged_in: !!(mxCliPeg.get() && mxCliPeg.get().credentials),
|
|
|
|
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-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':
|
|
|
|
this.setState({
|
|
|
|
logged_in: false,
|
|
|
|
ready: false
|
|
|
|
});
|
2015-06-16 13:37:15 +00:00
|
|
|
mxCliPeg.get().removeAllListeners();
|
|
|
|
mxCliPeg.replace(null);
|
2015-06-12 16:34:17 +00:00
|
|
|
break;
|
2015-06-12 17:01:38 +00:00
|
|
|
case 'view_room':
|
|
|
|
this.setState({
|
|
|
|
currentRoom: payload.room_id
|
|
|
|
});
|
2015-06-18 14:03:57 +00:00
|
|
|
this.focusComposer = true;
|
2015-06-12 17:01:38 +00:00
|
|
|
break;
|
2015-06-25 16:41:55 +00:00
|
|
|
case 'view_prev_room':
|
|
|
|
roomIndexDelta = -1;
|
|
|
|
case 'view_next_room':
|
|
|
|
var allRooms = mxCliPeg.get().getRooms();
|
|
|
|
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() {
|
|
|
|
this.setState({logged_in: true});
|
2015-06-12 13:59:33 +00:00
|
|
|
this.startMatrixClient();
|
|
|
|
},
|
|
|
|
|
|
|
|
startMatrixClient: function() {
|
|
|
|
var cli = mxCliPeg.get();
|
|
|
|
var that = this;
|
|
|
|
cli.on('syncComplete', function() {
|
2015-06-12 16:34:17 +00:00
|
|
|
var firstRoom = null;
|
|
|
|
if (cli.getRooms() && cli.getRooms().length) {
|
|
|
|
firstRoom = cli.getRooms()[0].roomId;
|
|
|
|
}
|
|
|
|
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
|
|
|
});
|
|
|
|
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-06-19 11:53:48 +00:00
|
|
|
};
|
2015-06-09 16:40:42 +00:00
|
|
|
|