{"version":3,"file":"sowatech-shared-operators.mjs","sources":["../../../projects/shared/operators/operators.ts","../../../projects/shared/operators/sowatech-shared-operators.ts"],"sourcesContent":["import { inject } from '@angular/core';\r\nimport { NavigationExtras, Router } from '@angular/router';\r\nimport { MessageBoxService } from '@sowatech/shared/dialogs';\r\nimport { DialogDynamicList, GenericEditDialogResult, GenericEditDialogService, GridColumnButtonEvent, IDialogConfiguration } from '@sowatech/shared/swt-generic';\r\nimport { from, MonoTypeOperatorFunction, of, OperatorFunction, pipe, switchMap, tap } from 'rxjs';\r\nimport { catchError, filter, map } from 'rxjs/operators';\r\n\r\ntype ValueOrFunction<T, Parameters> = T | ((params: Parameters) => T);\r\n\r\n/** Um aus einem Parameter, der entweder ein Wert, oder eine Funktion ist, den Wert abzuleiten */\r\nfunction evaluateValueOrFunction<T, Parameters>(valueOrFunction: ValueOrFunction<T, Parameters>, parameters: Parameters): T {\r\n    const condition = (x: ValueOrFunction<T, Parameters>): x is T => typeof x !== 'function';\r\n    return condition(valueOrFunction) ? valueOrFunction : valueOrFunction(parameters);\r\n}\r\n/** Wert, der im Fehlerfall verwendet werden kann, um Fehler zu erkennen und im Observable Stream zu ignorieren */\r\nexport const ERROR_VALUE = Object.freeze({ _error: true });\r\ntype ErrorValue = typeof ERROR_VALUE;\r\n\r\n/**\r\n * Operator, um im Fehlerfall den Edit Dialog zu öffnen und den Fehler dort anzuzeigen.\r\n * Beispiel:\r\n * ```ts\r\n * create$.pipe(\r\n *     showDialog(new CreateDialog()),\r\n *     webApiCall(dto => this.webApi.create(dto), catchAndShowDialogErrors()),\r\n * );\r\n * ```\r\n */\r\nexport function catchAndShowDialogErrors<T>(dialog = inject(GenericEditDialogService)): MonoTypeOperatorFunction<T> {\r\n    return pipe(\r\n        catchError(err => {\r\n            const message = err.error?.detailKey ?? err.error?.detail ?? err.message;\r\n            dialog.showErrors([message]);\r\n            return of(ERROR_VALUE);\r\n        }),\r\n        filter((v: T | ErrorValue): v is T => v !== ERROR_VALUE),\r\n    );\r\n}\r\n\r\n/**\r\n * Zur Anzeige eines Confirm Dialogs. Texte können als konstanter String oder als Funktion, die jedes Mal evaluiert wird, angegeben werden.\r\n * Beispiel:\r\n * ```ts\r\n * delete$.pipe(\r\n *     showConfirm(({ name }) => `Möchten Sie ${name} wirklich löschen?`, 'Löschen?'),\r\n *     switchMap(dto => this.webApi.delete(dto)),\r\n * );\r\n * ```\r\n */\r\nexport function showConfirm<T>(text: ValueOrFunction<string, T>, title?: ValueOrFunction<string, T>, messageBox = inject(MessageBoxService)): MonoTypeOperatorFunction<T> {\r\n    return switchMap(async x => {\r\n        const evaluatedTitle = evaluateValueOrFunction(title, x);\r\n        const evaluatedText = evaluateValueOrFunction(text, x);\r\n        await messageBox.confirmDialog(evaluatedText, evaluatedTitle);\r\n        return x;\r\n    });\r\n}\r\n\r\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\r\n/**\r\n * @deprecated Nur für debugging\r\n */\r\nexport function consoleWarn<T>(label?: any): MonoTypeOperatorFunction<T> {\r\n    if (!label) tap(console.warn);\r\n    return tap(input => console.warn(label, input));\r\n}\r\n\r\n/**\r\n * @deprecated Nur für debugging\r\n */\r\nexport function consoleCount<T>(label: string): MonoTypeOperatorFunction<T> {\r\n    return tap(() => console.count(label));\r\n}\r\n\r\n/**\r\n * Verkürzt das Mappen aller Items einer Liste.\r\n *\r\n * Ohne mapEach: `map(list => list.map(item => new Model(item)))`\r\n *\r\n * Mit mapEach: `mapEach(item => new Model(item))`\r\n */\r\nexport function mapEach<In, Out>(fn: (source: In) => Out): OperatorFunction<In[], Out[]> {\r\n    return map((sources: In[]) => sources.map(fn));\r\n}\r\n\r\nexport function notUndefined<T>(): OperatorFunction<T | undefined, T> {\r\n    return filter((x: T | undefined): x is T => x !== undefined);\r\n}\r\n\r\nexport function notNull<T>(): OperatorFunction<T | null, T> {\r\n    return filter((x: T | null): x is T => x !== null);\r\n}\r\n\r\n/**\r\n * Filtert ein Observable von `GridColumnButtonEvent` nach dem ident.\r\n * @param ident\r\n */\r\nexport function filterButtonIdent(ident: string) {\r\n    return filter<GridColumnButtonEvent>(({ buttonIdent }) => buttonIdent === ident);\r\n}\r\n\r\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\r\ntype DialogModeType = any;\r\n\r\n/**\r\n * Examples:\r\n * ```ts\r\n * showDialog(new MyDialog()) // Dialog anzeigen\r\n * showDialog(dto => new MyDialog(dto.flag)) // Dialog anhand der Daten modifizieren\r\n * showDialog(new MyDialog(), dto => ({ value: dto.a, text: dto.b })) // Dialog vorausfüllen\r\n * showDialog(new MyDialog(), null, () => ({ lookups: [{ key: 'datasource', datasource: this.lookupDatasource }] })) // Datasource für Grid\r\n * ```\r\n */\r\nexport function showDialog<Input, Model extends object>(\r\n    config: ValueOrFunction<IDialogConfiguration<Model>, Input>,\r\n    dto: ((input: Input) => Partial<Model> | null) | null = null,\r\n    options: ((input: Input) => { mode?: DialogModeType; lookups: DialogDynamicList[]; }) | null = null,\r\n    dialog = inject(GenericEditDialogService)\r\n): OperatorFunction<Input, GenericEditDialogResult<Model, DialogModeType>> {\r\n    return switchMap(input => {\r\n        const evaluatedConfig = evaluateValueOrFunction(config, input);\r\n        const optionsResult = options?.(input) ?? { mode: null, lookups: [] };\r\n        return dialog.show(evaluatedConfig, dto?.(input) ?? null, optionsResult.mode, optionsResult.lookups);\r\n    });\r\n}\r\n\r\n/**\r\n * Wrapper für einen WebApi Call, mit dem man Fehler behandeln kann, ohne die äußere Subscription zu beenden.\r\n * Sollte mit einem Error Handler benutzt werden.\r\n * ```ts\r\n * webApiCall(() => this.webApi.getList(), handleError('Laden der Liste'))\r\n * webApiCall(dto => this.webApi.create(dto), catchAndShowDialogErrors())\r\n * ```\r\n */\r\nexport function webApiCall<Input, Response>(fn: (input: Input) => Promise<Response>, ...pipes: [MonoTypeOperatorFunction<Response>, ...MonoTypeOperatorFunction<Response>[]]) {\r\n    // eslint-disable-next-line @typescript-eslint/ban-ts-comment\r\n    // @ts-ignore\r\n    const _pipe = pipe(...pipes);\r\n    return switchMap((input: Input) => from(fn(input)).pipe(_pipe));\r\n}\r\n\r\nexport function navigateTo<T>(fn: (input: T) => (string | number)[], extrasFn?: (input: T) => NavigationExtras, router = inject(Router)): MonoTypeOperatorFunction<T> {\r\n    return tap(x => router.navigate(fn(x), extrasFn?.(x)));\r\n}\r\n\r\n/**\r\n * Wenn die Bedingung **nicht** erfüllt ist, wird die angegebene Fehlermeldung im Dialog angezeigt.\r\n * Beispiel:\r\n * ```ts\r\n * pipe(\r\n *     showDialog(new CreateDialog()),\r\n *     dialogAssert(({ start, end } => end >= start, 'Das Ende darf nicht vor dem Start liegen'),\r\n * )\r\n * ```\r\n */\r\nexport function dialogAssert<T>(condition: (input: T) => boolean, errorMessage: string, dialog = inject(GenericEditDialogService)): MonoTypeOperatorFunction<T> {\r\n    return filter(x => {\r\n        const result = condition(x);\r\n        if (!result) dialog.showErrors([errorMessage]);\r\n        return result;\r\n    });\r\n}\r\n","/**\n * Generated bundle index. Do not edit.\n */\n\nexport * from './index';\n"],"names":[],"mappings":";;;;;;;AASA;AACA,SAAS,uBAAuB,CAAgB,eAA+C,EAAE,UAAsB,EAAA;IACnH,MAAM,SAAS,GAAG,CAAC,CAAiC,KAAa,OAAO,CAAC,KAAK,UAAU;AACxF,IAAA,OAAO,SAAS,CAAC,eAAe,CAAC,GAAG,eAAe,GAAG,eAAe,CAAC,UAAU,CAAC;AACrF;AACA;AACO,MAAM,WAAW,GAAG,MAAM,CAAC,MAAM,CAAC,EAAE,MAAM,EAAE,IAAI,EAAE;AAGzD;;;;;;;;;AASG;AACG,SAAU,wBAAwB,CAAI,MAAM,GAAG,MAAM,CAAC,wBAAwB,CAAC,EAAA;AACjF,IAAA,OAAO,IAAI,CACP,UAAU,CAAC,GAAG,IAAG;AACb,QAAA,MAAM,OAAO,GAAG,GAAG,CAAC,KAAK,EAAE,SAAS,IAAI,GAAG,CAAC,KAAK,EAAE,MAAM,IAAI,GAAG,CAAC,OAAO;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