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,
|
2020-11-10 07:14:20 +03:00
|
|
|
ISetModalButtonEnabledActionRequest,
|
|
|
|
IWidgetApiAcknowledgeResponseData,
|
|
|
|
IWidgetApiErrorResponseData,
|
|
|
|
BuiltInModalButtonID,
|
|
|
|
ModalButtonID,
|
2020-10-19 22:39:43 +03:00
|
|
|
ModalButtonKind,
|
|
|
|
Widget,
|
|
|
|
WidgetApiFromWidgetAction,
|
2020-11-19 21:24:17 +03:00
|
|
|
WidgetKind,
|
2020-10-19 22:39:43 +03:00
|
|
|
} 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-11-10 07:14:20 +03:00
|
|
|
import { arrayFastClone } from "../../../utils/arrays";
|
2020-11-26 04:51:27 +03:00
|
|
|
import { ElementWidget } from "../../../stores/widgets/StopGapWidget";
|
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-11-10 07:14:20 +03:00
|
|
|
disabledButtonIds: ModalButtonID[];
|
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-11-10 07:14:20 +03:00
|
|
|
private readonly possibleButtons: ModalButtonID[];
|
2020-09-25 16:08:27 +03:00
|
|
|
private appFrame: React.RefObject<HTMLIFrameElement> = React.createRef();
|
|
|
|
|
2020-11-10 07:14:20 +03:00
|
|
|
state: IState = {
|
|
|
|
disabledButtonIds: [],
|
|
|
|
};
|
2020-09-25 16:08:27 +03:00
|
|
|
|
2020-10-19 22:39:43 +03:00
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
|
2020-11-26 04:51:27 +03:00
|
|
|
this.widget = new ElementWidget({
|
2020-10-19 22:39:43 +03:00
|
|
|
...this.props.widgetDefinition,
|
|
|
|
creatorUserId: MatrixClientPeg.get().getUserId(),
|
|
|
|
id: `modal_${this.props.sourceWidgetId}`,
|
|
|
|
});
|
2020-11-10 07:14:20 +03:00
|
|
|
this.possibleButtons = (this.props.widgetDefinition.buttons || []).map(b => b.id);
|
2020-09-25 16:08:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public componentDidMount() {
|
2020-11-19 21:24:17 +03:00
|
|
|
const driver = new StopGapWidgetDriver( [], this.widget, WidgetKind.Modal);
|
2020-10-19 22:39:43 +03:00
|
|
|
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-11-10 07:14:20 +03:00
|
|
|
this.state.messaging.on(`action:${WidgetApiFromWidgetAction.SetModalButtonEnabled}`, this.onButtonEnableToggle);
|
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
|
|
|
}
|
|
|
|
|
2020-11-10 07:14:20 +03:00
|
|
|
private onButtonEnableToggle = (ev: CustomEvent<ISetModalButtonEnabledActionRequest>) => {
|
|
|
|
ev.preventDefault();
|
|
|
|
const isClose = ev.detail.data.button === BuiltInModalButtonID.Close;
|
|
|
|
if (isClose || !this.possibleButtons.includes(ev.detail.data.button)) {
|
|
|
|
return this.state.messaging.transport.reply(ev.detail, {
|
|
|
|
error: {message: "Invalid button"},
|
|
|
|
} as IWidgetApiErrorResponseData);
|
|
|
|
}
|
|
|
|
|
|
|
|
let buttonIds: ModalButtonID[];
|
|
|
|
if (ev.detail.data.enabled) {
|
|
|
|
buttonIds = arrayFastClone(this.state.disabledButtonIds).filter(i => i !== ev.detail.data.button);
|
|
|
|
} else {
|
|
|
|
// use a set to swap the operation to avoid memory leaky arrays.
|
|
|
|
const tempSet = new Set(this.state.disabledButtonIds);
|
|
|
|
tempSet.add(ev.detail.data.button);
|
|
|
|
buttonIds = Array.from(tempSet);
|
|
|
|
}
|
|
|
|
this.setState({disabledButtonIds: buttonIds});
|
|
|
|
this.state.messaging.transport.reply(ev.detail, {} as IWidgetApiAcknowledgeResponseData);
|
|
|
|
};
|
|
|
|
|
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
|
|
|
};
|
|
|
|
|
2020-11-26 04:48:18 +03:00
|
|
|
const isDisabled = this.state.disabledButtonIds.includes(def.id);
|
|
|
|
|
|
|
|
return <AccessibleButton key={def.id} kind={kind} onClick={onClick} disabled={isDisabled}>
|
2020-09-25 16:08:27 +03:00
|
|
|
{ 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-23 00:09:16 +03:00
|
|
|
sandbox="allow-forms allow-scripts allow-same-origin"
|
2020-10-20 17:05:22 +03:00
|
|
|
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>;
|
|
|
|
}
|
|
|
|
}
|