|
| 1 | +import { autoinject, PLATFORM } from 'aurelia-framework'; |
| 2 | +import { DialogService, DialogOpenPromise, DialogCancellableOpenResult } from 'aurelia-dialog'; |
| 3 | +import * as _ from 'lodash'; |
| 4 | + |
| 5 | +type ButtonAction<T> = (element: T) => Promise<any>; |
| 6 | +type IsEnabled<T> = (element: T) => boolean; |
| 7 | + |
| 8 | +export interface IDialogConfiguration<T> { |
| 9 | + title: string; |
| 10 | + buttons: IButtonDescriptor<T>[]; |
| 11 | + contentViewModel: string; |
| 12 | + contentModel?: any; |
| 13 | +} |
| 14 | + |
| 15 | +export enum ButtonType { |
| 16 | + OK, CANCEL, OTHER, ERROR |
| 17 | +} |
| 18 | + |
| 19 | +export interface IButtonDescriptor<T> { |
| 20 | + title: string; |
| 21 | + type: ButtonType; |
| 22 | + onAction: ButtonAction<T>; |
| 23 | + classes: string; |
| 24 | + enabled: IsEnabled<T>; |
| 25 | +} |
| 26 | + |
| 27 | +interface IDialogButton { |
| 28 | + execute: () => void; |
| 29 | + isEnabled: boolean; |
| 30 | + classes: string; |
| 31 | + title: string; |
| 32 | +} |
| 33 | + |
| 34 | +export interface IActionHandlers<T> { |
| 35 | + ok: (output: any) => T; |
| 36 | + cancel: (output: any) => T; |
| 37 | + error: (output: any) => T; |
| 38 | +} |
| 39 | + |
| 40 | +export class DialogButton<T> implements IDialogButton { |
| 41 | + public enabled: boolean = true; |
| 42 | + |
| 43 | + public constructor( |
| 44 | + private descriptor: IButtonDescriptor<T>, |
| 45 | + private context: T, |
| 46 | + private actions: IActionHandlers<T> |
| 47 | + ) { |
| 48 | + } |
| 49 | + |
| 50 | + public get isEnabled(): boolean { |
| 51 | + return this.enabled && this.descriptor.enabled(this.context); |
| 52 | + } |
| 53 | + |
| 54 | + public get title(): string { |
| 55 | + return this.descriptor.title; |
| 56 | + } |
| 57 | + |
| 58 | + public get classes(): string { |
| 59 | + return this.descriptor.classes; |
| 60 | + } |
| 61 | + |
| 62 | + public async execute(): Promise<void> { |
| 63 | + try { |
| 64 | + const result = await this.descriptor.onAction(this.context); |
| 65 | + switch (this.descriptor.type) { |
| 66 | + case ButtonType.OK: |
| 67 | + this.actions.ok(result); |
| 68 | + break; |
| 69 | + case ButtonType.CANCEL: |
| 70 | + this.actions.cancel(result); |
| 71 | + break; |
| 72 | + case ButtonType.ERROR: |
| 73 | + this.actions.error(result); |
| 74 | + break; |
| 75 | + default: |
| 76 | + break; |
| 77 | + } |
| 78 | + } finally { |
| 79 | + return Promise.resolve(); |
| 80 | + } |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +@autoinject |
| 85 | +export class GenericDialogService { |
| 86 | + |
| 87 | + public constructor( |
| 88 | + public dialogService: DialogService |
| 89 | + ) { |
| 90 | + } |
| 91 | + |
| 92 | + private static createButton<T>( |
| 93 | + title: string, |
| 94 | + onAction: ButtonAction<T>, |
| 95 | + type: ButtonType, |
| 96 | + enabled: IsEnabled<T> = () => true, |
| 97 | + classes = ''): IButtonDescriptor<T> { |
| 98 | + return { title, type, onAction, classes, enabled }; |
| 99 | + } |
| 100 | + |
| 101 | + public static createSaveButton<T>(action: ButtonAction<T>, enabled?: IsEnabled<T>, classes = ''): IButtonDescriptor<T> { |
| 102 | + return GenericDialogService.createButton<T>( |
| 103 | + 'ACTIONS.SAVE', action, ButtonType.OK, enabled, classes + ' btn-primary' |
| 104 | + ); |
| 105 | + } |
| 106 | + public static createCancelButton<T>(action: ButtonAction<T>, enabled?: IsEnabled<T>, classes = ''): IButtonDescriptor<T> { |
| 107 | + return GenericDialogService.createButton<T>( |
| 108 | + 'ACTIONS.CANCEL', action, ButtonType.CANCEL, enabled, classes + ' btn-outline-secondary' |
| 109 | + ); |
| 110 | + } |
| 111 | + public static createCloseButton<T>(action: ButtonAction<T>, enabled?: IsEnabled<T>, classes = ''): IButtonDescriptor<T> { |
| 112 | + return GenericDialogService.createButton<T>( |
| 113 | + 'ACTIONS.CLOSE', action, ButtonType.CANCEL, enabled, classes + ' btn-outline-secondary' |
| 114 | + ); |
| 115 | + } |
| 116 | + public static createOtherButton<T>(title: string, action: ButtonAction<T>, enabled?: IsEnabled<T>, classes = ''): IButtonDescriptor<T> { |
| 117 | + return GenericDialogService.createButton<T>(title, action, ButtonType.OTHER, enabled, classes); |
| 118 | + } |
| 119 | + public static createErrorButton<T>(title: string, action: ButtonAction<T>, enabled?: IsEnabled<T>, classes = ''): IButtonDescriptor<T> { |
| 120 | + return GenericDialogService.createButton<T>(title, action, ButtonType.ERROR, enabled, classes); |
| 121 | + } |
| 122 | + |
| 123 | + public showDialog<T>(model: IDialogConfiguration<T>): DialogOpenPromise<DialogCancellableOpenResult> { // TODO: change type |
| 124 | + if (model.contentModel) { |
| 125 | + Object.keys(model.contentModel).forEach(key => { |
| 126 | + if (!_.isFunction(model.contentModel[key])) { |
| 127 | + model.contentModel[key] = _.cloneDeep(model.contentModel[key]); |
| 128 | + } |
| 129 | + }); |
| 130 | + } |
| 131 | + return this.dialogService.open({ viewModel: PLATFORM.moduleName('resources/elements/dialog/dialog.element'), model }); |
| 132 | + } |
| 133 | +} |
0 commit comments