2017-05-18 01:21:02 +03:00
|
|
|
/*
|
2017-06-28 14:26:05 +03:00
|
|
|
Copyright 2017 Vector Creations Ltd
|
2017-05-18 01:21:02 +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';
|
|
|
|
|
2017-06-20 12:54:41 +03:00
|
|
|
import React from 'react';
|
|
|
|
import MatrixClientPeg from '../../../MatrixClientPeg';
|
|
|
|
import AppTile from '../elements/AppTile';
|
|
|
|
import Modal from '../../../Modal';
|
|
|
|
import dis from '../../../dispatcher';
|
|
|
|
import sdk from '../../../index';
|
|
|
|
import SdkConfig from '../../../SdkConfig';
|
|
|
|
import ScalarAuthClient from '../../../ScalarAuthClient';
|
2017-06-20 19:56:45 +03:00
|
|
|
import ScalarMessaging from '../../../ScalarMessaging';
|
2017-05-30 15:47:17 +03:00
|
|
|
|
2017-05-18 01:21:02 +03:00
|
|
|
module.exports = React.createClass({
|
|
|
|
displayName: 'AppsDrawer',
|
|
|
|
|
|
|
|
propTypes: {
|
2017-06-13 16:28:37 +03:00
|
|
|
room: React.PropTypes.object.isRequired,
|
|
|
|
},
|
|
|
|
|
2017-06-28 14:54:47 +03:00
|
|
|
getInitialState: function() {
|
|
|
|
return {
|
|
|
|
apps: this._getApps(),
|
|
|
|
};
|
|
|
|
},
|
|
|
|
|
2017-06-13 16:28:37 +03:00
|
|
|
componentWillMount: function() {
|
2017-06-20 19:56:45 +03:00
|
|
|
ScalarMessaging.startListening();
|
2017-06-13 16:28:37 +03:00
|
|
|
MatrixClientPeg.get().on("RoomState.events", this.onRoomStateEvents);
|
2017-05-18 01:21:02 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
componentDidMount: function() {
|
2017-06-20 12:54:41 +03:00
|
|
|
this.scalarClient = null;
|
|
|
|
if (SdkConfig.get().integrations_ui_url && SdkConfig.get().integrations_rest_url) {
|
|
|
|
this.scalarClient = new ScalarAuthClient();
|
|
|
|
this.scalarClient.connect().done(() => {
|
|
|
|
this.forceUpdate();
|
2017-06-28 14:32:38 +03:00
|
|
|
if (this.state.apps && this.state.apps.length < 1) {
|
|
|
|
this.onClickAddWidget();
|
2017-06-20 12:54:41 +03:00
|
|
|
}
|
|
|
|
}, (err) => {
|
|
|
|
this.setState({
|
|
|
|
scalar_error: err,
|
|
|
|
});
|
|
|
|
});
|
2017-06-14 16:05:29 +03:00
|
|
|
}
|
2017-05-18 01:21:02 +03:00
|
|
|
},
|
|
|
|
|
2017-06-13 16:28:37 +03:00
|
|
|
componentWillUnmount: function() {
|
2017-06-27 13:28:38 +03:00
|
|
|
ScalarMessaging.stopListening();
|
2017-06-13 16:28:37 +03:00
|
|
|
if (MatrixClientPeg.get()) {
|
|
|
|
MatrixClientPeg.get().removeListener("RoomState.events", this.onRoomStateEvents);
|
|
|
|
}
|
2017-06-05 20:21:31 +03:00
|
|
|
},
|
|
|
|
|
2017-06-27 14:26:13 +03:00
|
|
|
/**
|
|
|
|
* Encodes a URI according to a set of template variables. Variables will be
|
|
|
|
* passed through encodeURIComponent.
|
|
|
|
* @param {string} pathTemplate The path with template variables e.g. '/foo/$bar'.
|
|
|
|
* @param {Object} variables The key/value pairs to replace the template
|
|
|
|
* variables with. E.g. { "$bar": "baz" }.
|
|
|
|
* @return {string} The result of replacing all template variables e.g. '/foo/baz'.
|
|
|
|
*/
|
|
|
|
encodeUri: function(pathTemplate, variables) {
|
|
|
|
for (const key in variables) {
|
|
|
|
if (!variables.hasOwnProperty(key)) {
|
|
|
|
continue;
|
2017-05-30 15:47:17 +03:00
|
|
|
}
|
2017-06-27 14:26:13 +03:00
|
|
|
pathTemplate = pathTemplate.replace(
|
|
|
|
key, encodeURIComponent(variables[key]),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return pathTemplate;
|
|
|
|
},
|
|
|
|
|
|
|
|
_initAppConfig: function(appId, app) {
|
|
|
|
const user = MatrixClientPeg.get().getUser(this.props.userId);
|
|
|
|
const params = {
|
|
|
|
'$matrix_user_id': this.props.userId,
|
|
|
|
'$matrix_room_id': this.props.room.roomId,
|
|
|
|
'$matrix_display_name': user ? user.displayName : this.props.userId,
|
|
|
|
'$matrix_avatar_url': user ? MatrixClientPeg.get().mxcUrlToHttp(user.avatarUrl) : '',
|
|
|
|
};
|
|
|
|
|
|
|
|
if(app.data) {
|
|
|
|
Object.keys(app.data).forEach((key) => {
|
|
|
|
params['$' + key] = app.data[key];
|
|
|
|
});
|
2017-05-30 15:47:17 +03:00
|
|
|
}
|
2017-06-13 16:28:37 +03:00
|
|
|
|
2017-06-27 14:26:13 +03:00
|
|
|
app.id = appId;
|
|
|
|
app.name = app.name || app.type;
|
|
|
|
app.url = this.encodeUri(app.url, params);
|
|
|
|
|
|
|
|
// switch(app.type) {
|
|
|
|
// case 'etherpad':
|
|
|
|
// app.queryParams = '?userName=' + this.props.userId +
|
|
|
|
// '&padId=' + this.props.room.roomId;
|
|
|
|
// break;
|
|
|
|
// case 'jitsi': {
|
|
|
|
//
|
|
|
|
// app.queryParams = '?confId=' + app.data.confId +
|
|
|
|
// '&displayName=' + encodeURIComponent(user.displayName) +
|
|
|
|
// '&avatarUrl=' + encodeURIComponent(MatrixClientPeg.get().mxcUrlToHttp(user.avatarUrl)) +
|
|
|
|
// '&email=' + encodeURIComponent(this.props.userId) +
|
|
|
|
// '&isAudioConf=' + app.data.isAudioConf;
|
|
|
|
//
|
|
|
|
// break;
|
|
|
|
// }
|
|
|
|
// case 'vrdemo':
|
|
|
|
// app.queryParams = '?roomAlias=' + encodeURIComponent(app.data.roomAlias);
|
|
|
|
// break;
|
|
|
|
// }
|
|
|
|
|
2017-06-13 16:28:37 +03:00
|
|
|
return app;
|
|
|
|
},
|
|
|
|
|
|
|
|
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-06-20 12:54:41 +03:00
|
|
|
onClickAddWidget: function(e) {
|
|
|
|
if (e) {
|
|
|
|
e.preventDefault();
|
|
|
|
}
|
2017-06-13 16:31:37 +03:00
|
|
|
|
2017-06-20 12:54:41 +03:00
|
|
|
const IntegrationsManager = sdk.getComponent("views.settings.IntegrationsManager");
|
|
|
|
const src = (this.scalarClient !== null && this.scalarClient.hasCredentials()) ?
|
|
|
|
this.scalarClient.getScalarInterfaceUrlForRoom(this.props.room.roomId) :
|
|
|
|
null;
|
|
|
|
Modal.createDialog(IntegrationsManager, {
|
|
|
|
src: src,
|
|
|
|
onFinished: ()=>{
|
2017-06-20 19:56:45 +03:00
|
|
|
if (e) {
|
|
|
|
this.props.onCancelClick(e);
|
2017-06-13 16:31:37 +03:00
|
|
|
}
|
2017-05-22 14:34:27 +03:00
|
|
|
},
|
2017-06-20 12:54:41 +03:00
|
|
|
}, "mx_IntegrationsManager");
|
2017-05-18 01:21:02 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
render: function() {
|
2017-05-22 14:34:27 +03:00
|
|
|
const apps = this.state.apps.map(
|
2017-06-14 14:26:43 +03:00
|
|
|
(app, index, arr) => {
|
|
|
|
return <AppTile
|
|
|
|
key={app.name}
|
|
|
|
id={app.id}
|
2017-06-27 14:26:13 +03:00
|
|
|
url={app.url}
|
2017-06-14 14:26:43 +03:00
|
|
|
name={app.name}
|
|
|
|
fullWidth={arr.length<2 ? true : false}
|
|
|
|
room={this.props.room}
|
|
|
|
userId={this.props.userId}
|
|
|
|
/>;
|
|
|
|
});
|
2017-05-22 14:34:27 +03:00
|
|
|
|
2017-06-06 16:50:43 +03: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-18 01:21:02 +03:00
|
|
|
return (
|
|
|
|
<div className="mx_AppsDrawer">
|
2017-05-22 14:34:27 +03:00
|
|
|
<div id="apps" className="mx_AppsContainer">
|
|
|
|
{apps}
|
|
|
|
</div>
|
2017-06-06 16:50:43 +03:00
|
|
|
{addWidget}
|
2017-05-18 01:21:02 +03:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
},
|
|
|
|
});
|