Skip to content

Support being able to disable modal widget buttons (MSC2790 extension) #11

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions src/WidgetApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,10 @@ import {
IModalWidgetOpenRequestData,
IModalWidgetOpenRequestDataButton,
IModalWidgetReturnData,
BuiltInModalButtonID,
ModalButtonID,
} from "./interfaces/ModalWidgetActions";
import { ISetModalButtonEnabledActionRequestData } from "./interfaces/SetModalButtonEnabledAction";
import { ISendEventFromWidgetRequestData, ISendEventFromWidgetResponseData } from "./interfaces/SendEventAction";

/**
Expand Down Expand Up @@ -188,6 +191,15 @@ export class WidgetApi extends EventEmitter {
).then(res => res.success);
}

/**
* Opens a modal widget.
* @param {string} url The URL to the modal widget.
* @param {string} name The name of the widget.
* @param {IModalWidgetOpenRequestDataButton[]} buttons The buttons to have on the widget.
* @param {IModalWidgetCreateData} data Data to supply to the modal widget.
* @param {WidgetType} type The type of modal widget.
* @returns {Promise<void>} Resolves when the modal widget has been opened.
*/
public openModalWidget(
url: string,
name: string,
Expand All @@ -200,6 +212,11 @@ export class WidgetApi extends EventEmitter {
).then();
}

/**
* Closes the modal widget. The widget's session will be terminated shortly after.
* @param {IModalWidgetReturnData} data Optional data to close the modal widget with.
* @returns {Promise<void>} Resolves when complete.
*/
public closeModalWidget(data: IModalWidgetReturnData = {}): Promise<void> {
return this.transport.send<IModalWidgetReturnData>(WidgetApiFromWidgetAction.CloseModalWidget, data).then();
}
Expand All @@ -222,6 +239,22 @@ export class WidgetApi extends EventEmitter {
);
}

/**
* Sets a button as disabled or enabled on the modal widget. Buttons are enabled by default.
* @param {ModalButtonID} buttonId The button ID to enable/disable.
* @param {boolean} isEnabled Whether or not the button is enabled.
* @returns {Promise<void>} Resolves when complete.
* @throws Throws if the button cannot be disabled, or the client refuses to disable the button.
*/
public setModalButtonEnabled(buttonId: ModalButtonID, isEnabled: boolean): Promise<void> {
if (buttonId === BuiltInModalButtonID.Close) {
throw new Error("The close button cannot be disabled");
}
return this.transport.send<ISetModalButtonEnabledActionRequestData>(
WidgetApiFromWidgetAction.SetModalButtonEnabled, {button: buttonId, enabled: isEnabled},
).then();
}

/**
* Starts the communication channel. This should be done early to ensure
* that messages are not missed. Communication can only be stopped by the client.
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ export * from "./interfaces/OpenIDCredentialsAction";
export * from "./interfaces/WidgetKind";
export * from "./interfaces/ModalButtonKind";
export * from "./interfaces/ModalWidgetActions";
export * from "./interfaces/SetModalButtonEnabledAction";
export * from "./interfaces/WidgetConfigAction";
export * from "./interfaces/SendEventAction";
export * from "./interfaces/IRoomEvent";
Expand Down
7 changes: 6 additions & 1 deletion src/interfaces/ModalWidgetActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ import { IWidgetApiAcknowledgeResponseData, IWidgetApiResponse } from "./IWidget
import { IWidget } from "./IWidget";
import { ModalButtonKind } from "./ModalButtonKind";

export enum BuiltInModalButtonID {
Close = "m.close",
}
export type ModalButtonID = BuiltInModalButtonID | string;

export interface IModalWidgetCreateData extends IWidgetApiRequestData {
[key: string]: unknown;
}
Expand All @@ -30,7 +35,7 @@ export interface IModalWidgetReturnData {

// Types for a normal modal requesting the opening a modal widget
export interface IModalWidgetOpenRequestDataButton {
id: "m.close" | string;
id: ModalButtonID;
label: string;
kind: ModalButtonKind | string;
}
Expand Down
34 changes: 34 additions & 0 deletions src/interfaces/SetModalButtonEnabledAction.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* 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 { IWidgetApiRequest, IWidgetApiRequestData } from "./IWidgetApiRequest";
import { WidgetApiFromWidgetAction } from "./WidgetApiAction";
import { IWidgetApiAcknowledgeResponseData } from "./IWidgetApiResponse";
import { ModalButtonID } from "./ModalWidgetActions";

export interface ISetModalButtonEnabledActionRequestData extends IWidgetApiRequestData {
enabled: boolean;
button: ModalButtonID;
}

export interface ISetModalButtonEnabledActionRequest extends IWidgetApiRequest {
action: WidgetApiFromWidgetAction.SetModalButtonEnabled;
data: ISetModalButtonEnabledActionRequestData;
}

export interface ISetModalButtonEnabledActionResponse extends ISetModalButtonEnabledActionRequest {
response: IWidgetApiAcknowledgeResponseData;
}
1 change: 1 addition & 0 deletions src/interfaces/WidgetApiAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export enum WidgetApiFromWidgetAction {
GetOpenIDCredentials = "get_openid",
CloseModalWidget = "close_modal",
OpenModalWidget = "open_modal",
SetModalButtonEnabled = "set_button_enabled",
SendEvent = "send_event",
}

Expand Down