2021-12-14 16:06:54 +01:00
|
|
|
|
|
|
|
|
import emitter from './eventhub';
|
|
|
|
|
import { extendObject } from '../utils/objects';
|
|
|
|
|
import { generateId } from '../utils/numbers';
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export const event_showOverlay = 'zero.overlay.show';
|
|
|
|
|
export const event_closeOverlays = 'zero.overlay.closeall';
|
|
|
|
|
export const event_closeOverlay = 'zero.overlay.close';
|
|
|
|
|
export const event_finalizeOverlay = 'zero.overlay.finalize';
|
|
|
|
|
|
|
|
|
|
export type OverlayDisplay = 'dialog' | 'editor';
|
|
|
|
|
export type OverlayTheme = 'default' | 'light' | 'dark';
|
|
|
|
|
|
2021-12-15 14:36:45 +01:00
|
|
|
export interface OverlaySelectItem
|
|
|
|
|
{
|
|
|
|
|
name: string;
|
|
|
|
|
description?: string;
|
|
|
|
|
icon: string;
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-14 16:06:54 +01:00
|
|
|
export interface OverlayOptions
|
|
|
|
|
{
|
2021-12-15 14:36:45 +01:00
|
|
|
alias?: string;
|
|
|
|
|
display?: OverlayDisplay;
|
|
|
|
|
width?: number;
|
|
|
|
|
autoclose?: boolean;
|
|
|
|
|
softdismiss?: boolean;
|
|
|
|
|
component?: any;
|
|
|
|
|
theme?: OverlayTheme;
|
|
|
|
|
model?: any;
|
2021-12-14 16:06:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export interface OverlayInstance extends OverlayOptions
|
|
|
|
|
{
|
2021-12-15 14:36:45 +01:00
|
|
|
id: string;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function select(title: string, items: OverlaySelectItem[] | Promise<OverlaySelectItem[]>)
|
|
|
|
|
{
|
|
|
|
|
return open({
|
|
|
|
|
alias: 'select',
|
|
|
|
|
model: {
|
|
|
|
|
title,
|
|
|
|
|
items
|
|
|
|
|
},
|
|
|
|
|
autoclose: true,
|
|
|
|
|
softdismiss: true,
|
|
|
|
|
component: () => import('../ui/overlays/select.vue')
|
|
|
|
|
});
|
2021-12-14 16:06:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2021-12-17 00:26:06 +01:00
|
|
|
export function confirmDelete(title?: string, text?: string, warningText?: string, options?: OverlayOptions)
|
2021-12-14 16:06:54 +01:00
|
|
|
{
|
2021-12-17 00:26:06 +01:00
|
|
|
return open(extendObject({
|
2021-12-14 16:06:54 +01:00
|
|
|
alias: 'confirm',
|
|
|
|
|
model: {
|
|
|
|
|
title: typeof title === 'string' ? title : '@deleteoverlay.title',
|
2021-12-17 00:26:06 +01:00
|
|
|
warning: warningText,
|
|
|
|
|
text: typeof text === 'string' ? text : '@deleteoverlay.text',
|
2021-12-14 16:06:54 +01:00
|
|
|
confirmLabel: '@deleteoverlay.confirm',
|
|
|
|
|
confirmType: 'danger',
|
|
|
|
|
closeLabel: '@deleteoverlay.close',
|
|
|
|
|
},
|
|
|
|
|
autoclose: false,
|
|
|
|
|
softdismiss: true,
|
|
|
|
|
component: () => import('../ui/overlays/confirm.vue')
|
2021-12-17 00:26:06 +01:00
|
|
|
}, options || {}));
|
2021-12-14 16:06:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2022-01-10 16:13:59 +01:00
|
|
|
export function confirm(title: string, text?: string, softdismiss?: boolean)
|
2021-12-14 16:06:54 +01:00
|
|
|
{
|
|
|
|
|
return open({
|
|
|
|
|
alias: 'confirm',
|
|
|
|
|
model: {
|
|
|
|
|
title,
|
|
|
|
|
text,
|
|
|
|
|
confirmLabel: '@ui.confirm',
|
|
|
|
|
confirmType: 'default',
|
2022-01-10 16:13:59 +01:00
|
|
|
closeLabel: '@ui.cancel',
|
2021-12-14 16:06:54 +01:00
|
|
|
},
|
|
|
|
|
autoclose: false,
|
2022-01-10 16:13:59 +01:00
|
|
|
softdismiss: typeof softdismiss !== 'undefined' ? softdismiss : true,
|
2021-12-14 16:06:54 +01:00
|
|
|
component: () => import('../ui/overlays/confirm.vue')
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2021-12-15 14:36:45 +01:00
|
|
|
export function message(title: string, text?: string)
|
2021-12-14 16:06:54 +01:00
|
|
|
{
|
|
|
|
|
return open({
|
|
|
|
|
alias: 'message',
|
|
|
|
|
model: {
|
|
|
|
|
title: title,
|
|
|
|
|
text: text,
|
|
|
|
|
},
|
|
|
|
|
autoclose: true,
|
|
|
|
|
softdismiss: true,
|
|
|
|
|
component: () => import('../ui/overlays/message.vue')
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2022-01-10 16:13:59 +01:00
|
|
|
export function editor(editorAlias: string, model: any, title?: string, options?: OverlayOptions)
|
|
|
|
|
{
|
|
|
|
|
return open(extendObject({
|
|
|
|
|
alias: 'editor',
|
|
|
|
|
display: 'editor',
|
|
|
|
|
model: {
|
|
|
|
|
editor: editorAlias,
|
|
|
|
|
value: model,
|
|
|
|
|
title: title || '@ui.edit.title',
|
|
|
|
|
},
|
|
|
|
|
autoclose: false,
|
|
|
|
|
softdismiss: false,
|
|
|
|
|
component: () => import('../editor/ui-editor-overlay.vue'),
|
|
|
|
|
width: 820
|
|
|
|
|
}, options || {}));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2021-12-14 16:06:54 +01:00
|
|
|
export function closeAll()
|
|
|
|
|
{
|
|
|
|
|
emitter.emit(event_closeOverlays);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function open(options: OverlayOptions)
|
|
|
|
|
{
|
|
|
|
|
options = extendObject({
|
|
|
|
|
alias: 'default',
|
|
|
|
|
display: 'dialog',
|
|
|
|
|
width: options.display === 'editor' ? 560 : 460,
|
|
|
|
|
//hide: this.close,
|
|
|
|
|
autoclose: true,
|
|
|
|
|
softdismiss: options.display !== 'editor',
|
|
|
|
|
theme: 'default',
|
|
|
|
|
model: null,
|
|
|
|
|
component: null
|
|
|
|
|
} as OverlayOptions, options) as OverlayOptions;
|
|
|
|
|
|
|
|
|
|
const instance = {
|
|
|
|
|
id: generateId(),
|
|
|
|
|
...options,
|
|
|
|
|
close(force?: boolean)
|
|
|
|
|
{
|
|
|
|
|
emitter.emit(event_finalizeOverlay, { eventType: 'close', instance, force: force });
|
|
|
|
|
},
|
|
|
|
|
confirm(value: any)
|
|
|
|
|
{
|
|
|
|
|
emitter.emit(event_finalizeOverlay, { eventType: 'confirm', instance, value });
|
|
|
|
|
}
|
|
|
|
|
} as OverlayInstance;
|
|
|
|
|
|
|
|
|
|
emitter.emit(event_showOverlay, instance);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return new Promise((resolve, reject) =>
|
|
|
|
|
{
|
|
|
|
|
emitter.on(event_finalizeOverlay, (opts: any) =>
|
|
|
|
|
{
|
2022-01-13 16:01:45 +01:00
|
|
|
if (opts.instance.id == instance.id)
|
2021-12-14 16:06:54 +01:00
|
|
|
{
|
2022-01-13 16:01:45 +01:00
|
|
|
opts.close = () =>
|
|
|
|
|
{
|
|
|
|
|
emitter.emit(event_closeOverlay, instance);
|
|
|
|
|
};
|
|
|
|
|
resolve(opts);
|
|
|
|
|
}
|
2021-12-14 16:06:54 +01:00
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
//return new Promise((resolve, reject) =>
|
|
|
|
|
//{
|
|
|
|
|
// options.close = () =>
|
|
|
|
|
// {
|
|
|
|
|
// this.close(options);
|
|
|
|
|
// reject(options);
|
|
|
|
|
// // TODO should we move to resolve here, so we don't trigger errors in case the implementation does not catch them?
|
|
|
|
|
// // this will at least need some tests if the .then callback does not catch null values
|
|
|
|
|
// };
|
|
|
|
|
// options.hide = options.close;
|
|
|
|
|
|
|
|
|
|
// options.confirm = data =>
|
|
|
|
|
// {
|
|
|
|
|
// if (options.autoclose)
|
|
|
|
|
// {
|
|
|
|
|
// this.close(options);
|
|
|
|
|
// }
|
|
|
|
|
// resolve(data, options);
|
|
|
|
|
// };
|
|
|
|
|
//});
|
|
|
|
|
|
|
|
|
|
//options = extendObject({
|
|
|
|
|
|
|
|
|
|
// type: 'default',
|
|
|
|
|
// label: null,
|
|
|
|
|
// text: null,
|
|
|
|
|
// persistent: false,
|
|
|
|
|
// duration: 3000
|
|
|
|
|
//}, options) as OverlayOptions;
|
|
|
|
|
}
|