2017-05-17 22:21:02 +00:00
|
|
|
/*
|
|
|
|
Copyright 2015, 2016 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';
|
|
|
|
|
|
|
|
const React = require('react');
|
2017-06-13 13:28:37 +00:00
|
|
|
const MatrixClientPeg = require('../../../MatrixClientPeg');
|
2017-05-17 22:21:02 +00:00
|
|
|
const AddAppDialog = require('../dialogs/AddAppDialog');
|
2017-05-22 11:34:27 +00:00
|
|
|
const AppTile = require('../elements/AppTile');
|
|
|
|
const Modal = require("../../../Modal");
|
2017-06-13 13:28:37 +00:00
|
|
|
const dis = require('../../../dispatcher');
|
2017-05-30 12:47:17 +00:00
|
|
|
|
2017-05-17 22:21:02 +00:00
|
|
|
module.exports = React.createClass({
|
|
|
|
displayName: 'AppsDrawer',
|
|
|
|
|
|
|
|
propTypes: {
|
2017-06-13 13:28:37 +00:00
|
|
|
room: React.PropTypes.object.isRequired,
|
|
|
|
},
|
|
|
|
|
|
|
|
componentWillMount: function() {
|
|
|
|
MatrixClientPeg.get().on("RoomState.events", this.onRoomStateEvents);
|
2017-05-17 22:21:02 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
componentDidMount: function() {
|
2017-06-14 13:05:29 +00:00
|
|
|
if (this.state.apps && this.state.apps.length < 1) {
|
|
|
|
this.onClickAddWidget();
|
|
|
|
}
|
2017-05-17 22:21:02 +00:00
|
|
|
},
|
|
|
|
|
2017-06-13 13:28:37 +00:00
|
|
|
componentWillUnmount: function() {
|
|
|
|
if (MatrixClientPeg.get()) {
|
|
|
|
MatrixClientPeg.get().removeListener("RoomState.events", this.onRoomStateEvents);
|
|
|
|
}
|
2017-06-05 17:21:31 +00:00
|
|
|
},
|
|
|
|
|
2017-06-13 13:28:37 +00:00
|
|
|
_initAppConfig: function(appId, app) {
|
|
|
|
console.log("App props: ", this.props);
|
|
|
|
app.id = appId;
|
|
|
|
app.name = app.type;
|
|
|
|
|
|
|
|
switch(app.type) {
|
|
|
|
case 'etherpad':
|
2017-06-14 11:26:43 +00:00
|
|
|
app.queryParams = '?userName=' + this.props.userId +
|
2017-06-13 13:28:37 +00:00
|
|
|
'&padId=' + this.props.room.roomId;
|
|
|
|
break;
|
|
|
|
case 'jitsi': {
|
|
|
|
const user = MatrixClientPeg.get().getUser(this.props.userId);
|
2017-06-14 11:26:43 +00:00
|
|
|
app.queryParams = '?confId=' + app.data.confId +
|
2017-06-13 13:28:37 +00:00
|
|
|
'&displayName=' + encodeURIComponent(user.displayName) +
|
|
|
|
'&avatarUrl=' + encodeURIComponent(MatrixClientPeg.get().mxcUrlToHttp(user.avatarUrl)) +
|
|
|
|
'&email=' + encodeURIComponent(this.props.userId) +
|
|
|
|
'&isAudioConf=' + app.data.isAudioConf;
|
|
|
|
|
|
|
|
app.name += ' - ' + app.data.confId;
|
|
|
|
break;
|
2017-05-30 12:47:17 +00:00
|
|
|
}
|
2017-06-14 11:05:43 +00:00
|
|
|
case 'vrdemo':
|
2017-06-14 11:27:15 +00:00
|
|
|
app.name = 'Matrix VR Demo - ' + app.data.roomAlias;
|
|
|
|
app.queryParams = '?roomAlias=' + encodeURIComponent(app.data.roomAlias);
|
2017-06-14 11:05:43 +00:00
|
|
|
break;
|
2017-05-30 12:47:17 +00:00
|
|
|
}
|
2017-06-13 13:28:37 +00:00
|
|
|
|
|
|
|
return app;
|
|
|
|
},
|
|
|
|
|
|
|
|
getInitialState: function() {
|
2017-05-17 22:21:02 +00:00
|
|
|
return {
|
2017-06-13 13:28:37 +00:00
|
|
|
apps: this._getApps(),
|
2017-05-17 22:21:02 +00:00
|
|
|
};
|
|
|
|
},
|
|
|
|
|
2017-06-13 13:28:37 +00:00
|
|
|
onRoomStateEvents: function(ev, state) {
|
|
|
|
if (ev.getRoomId() !== this.props.room.roomId || ev.getType() !== 'im.vector.modular.widgets') {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this._updateApps();
|
|
|
|
},
|
|
|
|
|
|
|
|
_getApps: function() {
|
|
|
|
const appsStateEvents = this.props.room.currentState.getStateEvents('im.vector.modular.widgets', '');
|
|
|
|
if (!appsStateEvents) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
const appsStateEvent = appsStateEvents.getContent();
|
|
|
|
if (Object.keys(appsStateEvent).length < 1) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
return Object.keys(appsStateEvent).map((appId) => {
|
|
|
|
return this._initAppConfig(appId, appsStateEvent[appId]);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
_updateApps: function() {
|
|
|
|
const apps = this._getApps();
|
|
|
|
if (apps.length < 1) {
|
|
|
|
dis.dispatch({
|
|
|
|
action: 'appsDrawer',
|
|
|
|
show: false,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
this.setState({
|
|
|
|
apps: this._getApps(),
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2017-05-17 22:21:02 +00:00
|
|
|
onClickAddWidget: function() {
|
2017-05-22 11:34:27 +00:00
|
|
|
Modal.createDialog(AddAppDialog, {
|
2017-06-13 13:31:37 +00:00
|
|
|
onFinished: (proceed, type, value) => {
|
|
|
|
if (!proceed || !type) return;
|
|
|
|
if (type === 'custom' && !value) return;
|
|
|
|
|
|
|
|
const appsStateEvents = this.props.room.currentState.getStateEvents('im.vector.modular.widgets', '');
|
|
|
|
let appsStateEvent = {};
|
|
|
|
if (appsStateEvents) {
|
|
|
|
appsStateEvent = appsStateEvents.getContent();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (appsStateEvent[type]) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (type) {
|
|
|
|
case 'etherpad':
|
|
|
|
appsStateEvent.etherpad = {
|
|
|
|
type: type,
|
|
|
|
url: 'http://localhost:8000/etherpad.html',
|
|
|
|
};
|
|
|
|
break;
|
|
|
|
case 'grafana':
|
|
|
|
appsStateEvent.grafana = {
|
|
|
|
type: type,
|
|
|
|
url: 'http://localhost:8000/grafana.html',
|
|
|
|
};
|
|
|
|
break;
|
|
|
|
case 'jitsi':
|
|
|
|
appsStateEvent.videoConf = {
|
|
|
|
type: type,
|
|
|
|
url: 'http://localhost:8000/jitsi.html',
|
|
|
|
data: {
|
|
|
|
confId: this.props.room.roomId.replace(/[^A-Za-z0-9]/g, '_') + Date.now(),
|
|
|
|
},
|
|
|
|
};
|
|
|
|
break;
|
2017-06-14 11:05:43 +00:00
|
|
|
case 'vrdemo':
|
|
|
|
appsStateEvent.vrDemo = {
|
|
|
|
type: type,
|
|
|
|
url: 'http://localhost:8000/vrdemo.html',
|
2017-06-14 11:27:15 +00:00
|
|
|
data: {
|
|
|
|
roomAlias: '#vrvc' + this.props.room.roomId.replace(/[^A-Za-z0-9]/g, '_') + Date.now(),
|
|
|
|
},
|
2017-06-14 11:05:43 +00:00
|
|
|
};
|
|
|
|
break;
|
2017-06-13 13:31:37 +00:00
|
|
|
case 'custom':
|
|
|
|
appsStateEvent.custom = {
|
|
|
|
type: type,
|
|
|
|
url: value,
|
|
|
|
};
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
console.warn('Unsupported app type:', type);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
MatrixClientPeg.get().sendStateEvent(
|
|
|
|
this.props.room.roomId,
|
|
|
|
'im.vector.modular.widgets',
|
|
|
|
appsStateEvent,
|
|
|
|
'',
|
|
|
|
);
|
2017-05-22 11:34:27 +00:00
|
|
|
},
|
2017-05-17 22:21:02 +00:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
render: function() {
|
2017-05-22 11:34:27 +00:00
|
|
|
const apps = this.state.apps.map(
|
2017-06-14 11:26:43 +00:00
|
|
|
(app, index, arr) => {
|
|
|
|
let appUrl = app.url;
|
|
|
|
if (app.queryParams) {
|
|
|
|
appUrl += app.queryParams;
|
|
|
|
}
|
|
|
|
return <AppTile
|
|
|
|
key={app.name}
|
|
|
|
id={app.id}
|
|
|
|
url={appUrl}
|
|
|
|
name={app.name}
|
|
|
|
fullWidth={arr.length<2 ? true : false}
|
|
|
|
room={this.props.room}
|
|
|
|
userId={this.props.userId}
|
|
|
|
/>;
|
|
|
|
});
|
2017-05-22 11:34:27 +00:00
|
|
|
|
2017-06-06 13:50:43 +00:00
|
|
|
const addWidget = this.state.apps && this.state.apps.length < 2 &&
|
|
|
|
(<div onClick={this.onClickAddWidget}
|
|
|
|
role="button"
|
|
|
|
tabIndex="0"
|
|
|
|
className="mx_AddWidget_button"
|
|
|
|
title="Add a widget">
|
|
|
|
[+] Add a widget
|
|
|
|
</div>);
|
|
|
|
|
2017-05-17 22:21:02 +00:00
|
|
|
return (
|
|
|
|
<div className="mx_AppsDrawer">
|
2017-05-22 11:34:27 +00:00
|
|
|
<div id="apps" className="mx_AppsContainer">
|
|
|
|
{apps}
|
|
|
|
</div>
|
2017-06-06 13:50:43 +00:00
|
|
|
{addWidget}
|
2017-05-17 22:21:02 +00:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
},
|
|
|
|
});
|