2020-09-25 16:08:27 +03:00
|
|
|
/*
|
|
|
|
Copyright 2020 The Matrix.org Foundation C.I.C.
|
|
|
|
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
import * as React from 'react';
|
|
|
|
import BaseDialog from './BaseDialog';
|
|
|
|
import { _t } from '../../../languageHandler';
|
|
|
|
import AccessibleButton from "../elements/AccessibleButton";
|
2020-10-19 22:39:43 +03:00
|
|
|
import {
|
|
|
|
ClientWidgetApi,
|
|
|
|
IModalWidgetCloseRequest,
|
|
|
|
IModalWidgetOpenRequestData,
|
|
|
|
IModalWidgetReturnData,
|
|
|
|
ModalButtonKind,
|
|
|
|
Widget,
|
|
|
|
WidgetApiFromWidgetAction,
|
|
|
|
} from "matrix-widget-api";
|
|
|
|
import {StopGapWidgetDriver} from "../../../stores/widgets/StopGapWidgetDriver";
|
|
|
|
import {MatrixClientPeg} from "../../../MatrixClientPeg";
|
|
|
|
import RoomViewStore from "../../../stores/RoomViewStore";
|
|
|
|
import {OwnProfileStore} from "../../../stores/OwnProfileStore";
|
2020-09-25 16:08:27 +03:00
|
|
|
|
|
|
|
interface IProps {
|
2020-10-19 22:39:43 +03:00
|
|
|
widgetDefinition: IModalWidgetOpenRequestData;
|
2020-09-25 16:08:27 +03:00
|
|
|
sourceWidgetId: string;
|
2020-10-19 22:39:43 +03:00
|
|
|
onFinished(success: boolean, data?: IModalWidgetReturnData): void;
|
2020-09-25 16:08:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
interface IState {
|
2020-10-19 22:39:43 +03:00
|
|
|
messaging?: ClientWidgetApi;
|
2020-09-25 16:08:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
const MAX_BUTTONS = 3;
|
|
|
|
|
|
|
|
export default class ModalWidgetDialog extends React.PureComponent<IProps, IState> {
|
2020-10-19 22:39:43 +03:00
|
|
|
private readonly widget: Widget;
|
2020-09-25 16:08:27 +03:00
|
|
|
private appFrame: React.RefObject<HTMLIFrameElement> = React.createRef();
|
|
|
|
|
|
|
|
state: IState = {};
|
|
|
|
|
2020-10-19 22:39:43 +03:00
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
|
|
|
|
this.widget = new Widget({
|
|
|
|
...this.props.widgetDefinition,
|
|
|
|
creatorUserId: MatrixClientPeg.get().getUserId(),
|
|
|
|
id: `modal_${this.props.sourceWidgetId}`,
|
|
|
|
});
|
2020-09-25 16:08:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public componentDidMount() {
|
2020-10-19 22:39:43 +03:00
|
|
|
const driver = new StopGapWidgetDriver( []);
|
|
|
|
const messaging = new ClientWidgetApi(this.widget, this.appFrame.current, driver);
|
2020-09-25 16:08:27 +03:00
|
|
|
this.setState({messaging});
|
|
|
|
}
|
|
|
|
|
|
|
|
public componentWillUnmount() {
|
2020-10-19 22:39:43 +03:00
|
|
|
this.state.messaging.off("ready", this.onReady);
|
|
|
|
this.state.messaging.off(`action:${WidgetApiFromWidgetAction.CloseModalWidget}`, this.onWidgetClose);
|
2020-09-25 16:08:27 +03:00
|
|
|
this.state.messaging.stop();
|
|
|
|
}
|
|
|
|
|
2020-10-19 22:39:43 +03:00
|
|
|
private onReady = () => {
|
|
|
|
this.state.messaging.sendWidgetConfig(this.props.widgetDefinition);
|
|
|
|
};
|
|
|
|
|
2020-09-25 16:08:27 +03:00
|
|
|
private onLoad = () => {
|
2020-10-19 22:39:43 +03:00
|
|
|
this.state.messaging.once("ready", this.onReady);
|
|
|
|
this.state.messaging.on(`action:${WidgetApiFromWidgetAction.CloseModalWidget}`, this.onWidgetClose);
|
2020-09-25 16:08:27 +03:00
|
|
|
};
|
|
|
|
|
2020-10-19 22:39:43 +03:00
|
|
|
private onWidgetClose = (ev: CustomEvent<IModalWidgetCloseRequest>) => {
|
|
|
|
this.props.onFinished(true, ev.detail.data);
|
2020-09-25 16:08:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public render() {
|
2020-10-19 22:39:43 +03:00
|
|
|
const templated = this.widget.getCompleteUrl({
|
|
|
|
currentRoomId: RoomViewStore.getRoomId(),
|
|
|
|
currentUserId: MatrixClientPeg.get().getUserId(),
|
|
|
|
userDisplayName: OwnProfileStore.instance.displayName,
|
|
|
|
userHttpAvatarUrl: OwnProfileStore.instance.getHttpAvatarUrl(),
|
|
|
|
});
|
2020-09-25 16:08:27 +03:00
|
|
|
|
2020-10-19 22:39:43 +03:00
|
|
|
const parsed = new URL(templated);
|
|
|
|
|
|
|
|
// Add in some legacy support sprinkles (for non-popout widgets)
|
2020-09-28 12:50:31 +03:00
|
|
|
// TODO: Replace these with proper widget params
|
|
|
|
// See https://github.com/matrix-org/matrix-doc/pull/1958/files#r405714833
|
2020-10-19 22:39:43 +03:00
|
|
|
parsed.searchParams.set('widgetId', this.widget.id);
|
|
|
|
parsed.searchParams.set('parentUrl', window.location.href.split('#', 2)[0]);
|
|
|
|
|
|
|
|
// Replace the encoded dollar signs back to dollar signs. They have no special meaning
|
|
|
|
// in HTTP, but URL parsers encode them anyways.
|
|
|
|
const widgetUrl = parsed.toString().replace(/%24/g, '$');
|
2020-09-25 16:08:27 +03:00
|
|
|
|
|
|
|
let buttons;
|
|
|
|
if (this.props.widgetDefinition.buttons) {
|
|
|
|
// show first button rightmost for a more natural specification
|
|
|
|
buttons = this.props.widgetDefinition.buttons.slice(0, MAX_BUTTONS).reverse().map(def => {
|
|
|
|
let kind = "secondary";
|
|
|
|
switch (def.kind) {
|
2020-10-19 22:39:43 +03:00
|
|
|
case ModalButtonKind.Primary:
|
2020-09-25 16:08:27 +03:00
|
|
|
kind = "primary";
|
|
|
|
break;
|
2020-10-19 22:39:43 +03:00
|
|
|
case ModalButtonKind.Secondary:
|
2020-09-25 16:08:27 +03:00
|
|
|
kind = "primary_outline";
|
|
|
|
break
|
2020-10-19 22:39:43 +03:00
|
|
|
case ModalButtonKind.Danger:
|
2020-09-25 16:08:27 +03:00
|
|
|
kind = "danger";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
const onClick = () => {
|
2020-10-19 22:39:43 +03:00
|
|
|
this.state.messaging.notifyModalWidgetButtonClicked(def.id);
|
2020-09-25 16:08:27 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
return <AccessibleButton key={def.id} kind={kind} onClick={onClick}>
|
|
|
|
{ def.label }
|
|
|
|
</AccessibleButton>;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return <BaseDialog
|
|
|
|
title={this.props.widgetDefinition.name || _t("Modal Widget")}
|
|
|
|
className="mx_ModalWidgetDialog"
|
|
|
|
contentId="mx_Dialog_content"
|
|
|
|
onFinished={this.props.onFinished}
|
|
|
|
>
|
2020-10-20 17:05:22 +03:00
|
|
|
<div className="mx_ModalWidgetDialog_warning">
|
|
|
|
<img
|
|
|
|
src={require("../../../../res/img/element-icons/warning-badge.svg")}
|
|
|
|
height="16"
|
|
|
|
width="16"
|
|
|
|
alt=""
|
|
|
|
/>
|
|
|
|
{_t("Data on this screen is shared with %(widgetDomain)s", {
|
|
|
|
widgetDomain: parsed.hostname,
|
|
|
|
})}
|
|
|
|
</div>
|
2020-09-25 16:08:27 +03:00
|
|
|
<div>
|
|
|
|
<iframe
|
|
|
|
ref={this.appFrame}
|
2020-10-20 17:05:22 +03:00
|
|
|
sandbox="allow-forms allow-scripts"
|
|
|
|
src={widgetUrl}
|
2020-09-25 16:08:27 +03:00
|
|
|
onLoad={this.onLoad}
|
|
|
|
/>
|
|
|
|
</div>
|
2020-10-20 17:05:22 +03:00
|
|
|
<div className="mx_ModalWidgetDialog_buttons">
|
2020-09-25 16:08:27 +03:00
|
|
|
{ buttons }
|
|
|
|
</div>
|
|
|
|
</BaseDialog>;
|
|
|
|
}
|
|
|
|
}
|