OpenSlides/client/src/app/core/ui-services/choice.service.ts

59 lines
1.8 KiB
TypeScript
Raw Normal View History

2018-11-27 22:44:37 +01:00
import { Injectable } from '@angular/core';
import { MatDialog } from '@angular/material/dialog';
2019-02-08 17:24:32 +01:00
2018-11-29 17:36:22 +01:00
import {
ChoiceDialogComponent,
ChoiceDialogOptions,
ChoiceAnswer
} from '../../shared/components/choice-dialog/choice-dialog.component';
2018-11-27 22:44:37 +01:00
/**
* A service for prompting the user to select a choice.
*/
@Injectable({
2018-11-29 17:36:22 +01:00
providedIn: 'root'
2018-11-27 22:44:37 +01:00
})
2019-02-08 17:24:32 +01:00
export class ChoiceService {
2018-11-27 22:44:37 +01:00
/**
* Ctor.
*
* @param dialog For opening the ChoiceDialog
*/
2019-02-08 17:24:32 +01:00
public constructor(private dialog: MatDialog) {}
2018-11-27 22:44:37 +01:00
/**
* Opens the dialog. Returns the chosen value after the user accepts.
* @param title The title to display in the dialog
* @param choices The available choices
* @param multiSelect turn on the option to select multiple entries.
* The answer.items will then be an array.
* @param actions optional strings for buttons replacing the regular confirmation.
* The answer.action will reflect the button selected
* @param clearChoice A string for an extra, visually slightly separated
* choice for 'explicitly set an empty selection'. The answer's action may
* have this string's value
2018-11-27 22:44:37 +01:00
* @returns an answer {@link ChoiceAnswer}
*/
2018-11-29 17:36:22 +01:00
public async open(
title: string,
choices: ChoiceDialogOptions,
multiSelect: boolean = false,
actions?: string[],
clearChoice?: string
2018-11-29 17:36:22 +01:00
): Promise<ChoiceAnswer> {
2018-11-27 22:44:37 +01:00
const dialogRef = this.dialog.open(ChoiceDialogComponent, {
maxWidth: '90vw',
maxHeight: '90vh',
disableClose: true,
data: {
title: title,
choices: choices,
multiSelect: multiSelect,
actionButtons: actions,
clearChoice: clearChoice
}
2018-11-27 22:44:37 +01:00
});
return dialogRef.afterClosed().toPromise();
}
}