2022-03-15 00:06:42 +01:00
|
|
|
/**
|
|
|
|
* EGroupware eTemplate2 - Box widget
|
|
|
|
*
|
|
|
|
* @license http://opensource.org/licenses/gpl-license.php GPL - GNU General Public License
|
|
|
|
* @package etemplate
|
|
|
|
* @subpackage api
|
|
|
|
* @link https://www.egroupware.org
|
|
|
|
* @author Nathan Gray
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
import {Et2Widget} from "../Et2Widget/Et2Widget";
|
2023-09-13 19:55:33 +02:00
|
|
|
import {css, html, LitElement, render} from "lit";
|
|
|
|
import {classMap} from "lit/directives/class-map.js";
|
|
|
|
import {ifDefined} from "lit/directives/if-defined.js";
|
|
|
|
import {repeat} from "lit/directives/repeat.js";
|
|
|
|
import {styleMap} from "lit/directives/style-map.js";
|
2022-03-16 20:53:50 +01:00
|
|
|
import {et2_template} from "../et2_widget_template";
|
2024-04-12 23:09:45 +02:00
|
|
|
import type {etemplate2} from "../etemplate2";
|
2022-11-30 23:59:25 +01:00
|
|
|
import {egw, IegwAppLocal} from "../../jsapi/egw_global";
|
2022-05-23 11:35:14 +02:00
|
|
|
import interact from "@interactjs/interactjs";
|
2022-05-20 19:40:14 +02:00
|
|
|
import type {InteractEvent} from "@interactjs/core/InteractEvent";
|
2022-10-28 18:05:03 +02:00
|
|
|
import {Et2Button} from "../Et2Button/Et2Button";
|
2022-11-30 23:59:25 +01:00
|
|
|
import shoelace from "../Styles/shoelace";
|
|
|
|
import {SlDialog} from "@shoelace-style/shoelace";
|
|
|
|
import {egwIsMobile} from "../../egw_action/egw_action_common";
|
2024-01-25 00:42:22 +01:00
|
|
|
import {waitForEvent} from "../Et2Widget/event";
|
2022-03-15 00:06:42 +01:00
|
|
|
|
|
|
|
export interface DialogButton
|
|
|
|
{
|
|
|
|
id : string,
|
|
|
|
button_id? : number,
|
2022-03-17 22:21:15 +01:00
|
|
|
label : string,
|
2022-03-15 00:06:42 +01:00
|
|
|
image? : string,
|
|
|
|
default? : boolean
|
2022-03-17 22:21:15 +01:00
|
|
|
align? : string,
|
|
|
|
disabled? : boolean
|
2022-03-15 00:06:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-03-21 16:52:09 +01:00
|
|
|
* A common dialog widget that makes it easy to inform users or prompt for information.
|
|
|
|
*
|
|
|
|
* It is possible to have a custom dialog by using a template, but you can also use
|
|
|
|
* the static method Et2Dialog.show_dialog(). At its simplest, you can just use:
|
|
|
|
* ```ts
|
|
|
|
* Et2Dialog.show_dialog(false, "Operation completed");
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* Or a more complete example:
|
|
|
|
* ```js
|
2023-10-16 21:33:55 +02:00
|
|
|
* let callback = function (button_id)
|
2022-03-21 16:52:09 +01:00
|
|
|
* {
|
|
|
|
* if(button_id == Et2Dialog.YES_BUTTON)
|
|
|
|
* {
|
|
|
|
* // Do stuff
|
|
|
|
* }
|
|
|
|
* else if (button_id == Et2Dialog.NO_BUTTON)
|
|
|
|
* {
|
|
|
|
* // Other stuff
|
|
|
|
* }
|
|
|
|
* else if (button_id == Et2Dialog.CANCEL_BUTTON)
|
|
|
|
* {
|
|
|
|
* // Abort
|
|
|
|
* }
|
|
|
|
* }.
|
|
|
|
* let dialog = Et2Dialog.show_dialog(
|
|
|
|
* callback, "Erase the entire database?","Break things", {} // value
|
|
|
|
* et2_dialog.BUTTONS_YES_NO_CANCEL, et2_dialog.WARNING_MESSAGE
|
|
|
|
* );
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* Or, using Promises instead of a callback:
|
|
|
|
* ```ts
|
|
|
|
* let result = await Et2Dialog.show_prompt(null, "Name").getComplete();
|
|
|
|
* if(result.button_id == Et2Dialog.OK_BUTTON)
|
|
|
|
* {
|
|
|
|
* // Do stuff with result.value
|
|
|
|
* }
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* The parameters for the above are all optional, except callback (which can be null) and message:
|
2023-10-16 21:33:55 +02:00
|
|
|
* - callback - function called when the dialog closes, or false/null.
|
2022-03-21 16:52:09 +01:00
|
|
|
* The ID of the button will be passed. Button ID will be one of the Et2Dialog.*_BUTTON constants.
|
|
|
|
* The callback is _not_ called if the user closes the dialog with the X in the corner, or presses ESC.
|
2023-10-16 21:33:55 +02:00
|
|
|
* - message - (plain) text to display
|
|
|
|
* - title - Dialog title
|
|
|
|
* - value (for prompt)
|
|
|
|
* - buttons - Et2Dialog BUTTONS_* constant, or an array of button settings. Use DialogButton interface.
|
|
|
|
* - dialog_type - Et2Dialog *_MESSAGE constant
|
|
|
|
* - icon - URL of icon
|
2022-03-21 16:52:09 +01:00
|
|
|
*
|
|
|
|
* Note that these methods will _not_ block program flow while waiting for user input unless you use "await" on getComplete().
|
|
|
|
* The user's input will be provided to the callback.
|
|
|
|
*
|
|
|
|
* You can also create a custom dialog using an etemplate, even setting all the buttons yourself.
|
|
|
|
* ```ts
|
2023-10-16 21:33:55 +02:00
|
|
|
* // Pass egw in the constructor
|
2022-03-21 16:52:09 +01:00
|
|
|
* let dialog = new Et2Dialog(my_egw_reference);
|
|
|
|
*
|
|
|
|
* // Set attributes. They can be set in any way, but this is convenient.
|
|
|
|
* dialog.transformAttributes({
|
|
|
|
* // If you use a template, the second parameter will be the value of the template, as if it were submitted.
|
|
|
|
* callback: function(button_id, value) {...}, // return false to prevent dialog closing
|
|
|
|
* buttons: [
|
|
|
|
* // These ones will use the callback, just like normal. Use DialogButton interface.
|
|
|
|
* {label: egw.lang("OK"),id:"OK", default: true},
|
|
|
|
* {label: egw.lang("Yes"),id:"Yes"},
|
|
|
|
* {label: egw.lang("Sure"),id:"Sure"},
|
|
|
|
* {label: egw.lang("Maybe"),click: function() {
|
|
|
|
* // If you override, 'this' will be the dialog DOMNode.
|
|
|
|
* // Things get more complicated.
|
|
|
|
* // Do what you like here
|
|
|
|
* }},
|
|
|
|
*
|
|
|
|
* ],
|
|
|
|
* title: 'Why would you want to do this?',
|
|
|
|
* template:"/egroupware/addressbook/templates/default/edit.xet",
|
|
|
|
* value: { content: {...default values}, sel_options: {...}...}
|
|
|
|
* });
|
|
|
|
* // Add to DOM, dialog will auto-open
|
|
|
|
* document.body.appendChild(dialog);
|
|
|
|
* // If you want, wait for close
|
|
|
|
* let result = await dialog.getComplete();
|
|
|
|
*```
|
2022-03-15 00:06:42 +01:00
|
|
|
*
|
2022-11-30 23:59:25 +01:00
|
|
|
* Customize initial focus by setting the "autofocus" attribute on a control, otherwise first input will have focus
|
2022-03-15 00:06:42 +01:00
|
|
|
*/
|
2024-05-07 22:46:44 +02:00
|
|
|
export class Et2Dialog extends Et2Widget(SlDialog)
|
2022-03-15 00:06:42 +01:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Dialogs don't always get added to an etemplate, so we keep our own egw
|
|
|
|
*
|
|
|
|
* @type {IegwAppLocal}
|
|
|
|
* @protected
|
2023-10-16 21:33:55 +02:00
|
|
|
* @internal
|
2022-03-15 00:06:42 +01:00
|
|
|
*/
|
|
|
|
protected __egw : IegwAppLocal
|
|
|
|
|
2022-03-16 20:53:50 +01:00
|
|
|
/**
|
|
|
|
* As long as the template is a legacy widget, we want to hold on to the widget
|
|
|
|
* When it becomes a WebComponent, we can just include it in render()
|
|
|
|
*
|
|
|
|
* @type {et2_template | null}
|
|
|
|
* @protected
|
2023-10-16 21:33:55 +02:00
|
|
|
* @internal
|
2022-03-16 20:53:50 +01:00
|
|
|
*/
|
2022-03-17 18:19:47 +01:00
|
|
|
protected _template_widget : etemplate2 | null;
|
|
|
|
protected _template_promise : Promise<boolean>;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Treat the dialog as an atomic operation, and use this promise to notify when
|
|
|
|
* "done" instead of (or in addition to) using the callback function.
|
|
|
|
* It gives the button ID and the dialog value.
|
2023-10-16 21:33:55 +02:00
|
|
|
* @internal
|
2022-03-17 18:19:47 +01:00
|
|
|
*/
|
|
|
|
protected _complete_promise : Promise<[number, Object]>;
|
|
|
|
|
2022-12-14 18:39:15 +01:00
|
|
|
/**
|
|
|
|
* Resolve the template promise
|
|
|
|
*/
|
|
|
|
private _templateResolver : (value) => void;
|
|
|
|
/**
|
|
|
|
* Resolve the dialog complete promise
|
|
|
|
*/
|
|
|
|
private _completeResolver : (value) => [number, Object];
|
|
|
|
|
2022-03-17 18:19:47 +01:00
|
|
|
/**
|
|
|
|
* The ID of the button that was clicked. Always one of the button constants,
|
|
|
|
* unless custom buttons were used
|
|
|
|
*
|
|
|
|
* @type {number|null}
|
|
|
|
* @protected
|
2023-10-16 21:33:55 +02:00
|
|
|
* @internal
|
2022-03-17 18:19:47 +01:00
|
|
|
*/
|
|
|
|
protected _button_id : number | null;
|
2022-03-16 20:53:50 +01:00
|
|
|
|
2022-03-15 00:06:42 +01:00
|
|
|
/**
|
|
|
|
* Types
|
|
|
|
* @constant
|
|
|
|
*/
|
2022-03-15 21:10:53 +01:00
|
|
|
public static readonly PLAIN_MESSAGE : number = 0;
|
|
|
|
public static readonly INFORMATION_MESSAGE : number = 1;
|
|
|
|
public static readonly QUESTION_MESSAGE : number = 2;
|
|
|
|
public static readonly WARNING_MESSAGE : number = 3;
|
|
|
|
public static readonly ERROR_MESSAGE : number = 4;
|
2022-03-15 00:06:42 +01:00
|
|
|
|
|
|
|
/* Pre-defined Button combos */
|
2022-03-15 21:10:53 +01:00
|
|
|
public static readonly BUTTONS_OK : number = 0;
|
|
|
|
public static readonly BUTTONS_OK_CANCEL : number = 1;
|
|
|
|
public static readonly BUTTONS_YES_NO : number = 2;
|
|
|
|
public static readonly BUTTONS_YES_NO_CANCEL : number = 3;
|
2022-03-15 00:06:42 +01:00
|
|
|
|
|
|
|
/* Button constants */
|
2022-03-15 21:10:53 +01:00
|
|
|
public static readonly CANCEL_BUTTON : number = 0;
|
|
|
|
public static readonly OK_BUTTON : number = 1;
|
|
|
|
public static readonly YES_BUTTON : number = 2;
|
|
|
|
public static readonly NO_BUTTON : number = 3;
|
2022-03-15 00:06:42 +01:00
|
|
|
|
2022-11-30 23:59:25 +01:00
|
|
|
|
|
|
|
static get styles()
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
...shoelace,
|
|
|
|
...(super.styles || []),
|
|
|
|
css`
|
|
|
|
:host {
|
|
|
|
--header-spacing: var(--sl-spacing-medium);
|
2023-03-29 17:21:41 +02:00
|
|
|
--body-spacing: var(--sl-spacing-medium);
|
|
|
|
--width: auto;
|
2022-11-30 23:59:25 +01:00
|
|
|
}
|
2022-12-01 21:02:40 +01:00
|
|
|
.dialog__panel {
|
|
|
|
border: 1px solid silver;
|
|
|
|
box-shadow: -2px 1px 9px 3px #b4b4b4;
|
|
|
|
min-width: 250px;
|
|
|
|
touch-action: none;
|
|
|
|
}
|
|
|
|
.dialog__header {
|
|
|
|
display: flex;
|
|
|
|
border-bottom: 1px inset;
|
|
|
|
}
|
2022-11-30 23:59:25 +01:00
|
|
|
.dialog__title {
|
2022-12-02 19:23:09 +01:00
|
|
|
font-size: var(--sl-font-size-medium);
|
2022-11-30 23:59:25 +01:00
|
|
|
user-select: none;
|
|
|
|
}
|
2022-12-01 21:02:40 +01:00
|
|
|
.dialog__close {
|
|
|
|
background-color: #f3f3f3;
|
|
|
|
padding: 0;
|
|
|
|
order: 99;
|
|
|
|
}
|
2022-11-30 23:59:25 +01:00
|
|
|
.dialog__footer {
|
2022-12-01 21:02:40 +01:00
|
|
|
--footer-spacing: 5px;
|
2022-11-30 23:59:25 +01:00
|
|
|
display: flex;
|
|
|
|
flex-wrap: nowrap;
|
|
|
|
justify-content: flex-start;
|
|
|
|
align-items: stretch;
|
|
|
|
gap: 5px;
|
2022-12-01 21:02:40 +01:00
|
|
|
border-top: 1px solid silver;
|
|
|
|
margin-top: 0.5em;
|
2022-11-30 23:59:25 +01:00
|
|
|
}
|
2023-02-22 22:51:21 +01:00
|
|
|
|
2023-11-02 20:03:56 +01:00
|
|
|
.dialog_content {
|
|
|
|
height: var(--height, auto);
|
|
|
|
}
|
|
|
|
|
2023-02-22 22:51:21 +01:00
|
|
|
/* Non-modal dialogs don't have an overlay */
|
|
|
|
|
2023-06-30 19:01:22 +02:00
|
|
|
:host(:not([ismodal])) .dialog, :host(:not([isModal])) .dialog__overlay {
|
2023-02-22 22:51:21 +01:00
|
|
|
pointer-events: none;
|
|
|
|
background: transparent;
|
|
|
|
}
|
2023-06-30 19:01:22 +02:00
|
|
|
|
|
|
|
:host(:not([ismodal])) .dialog__panel {
|
2023-02-22 22:51:21 +01:00
|
|
|
pointer-events: auto;
|
|
|
|
}
|
|
|
|
|
2023-06-30 19:01:22 +02:00
|
|
|
/* Hide close button when set */
|
|
|
|
|
|
|
|
:host([noclosebutton]) .dialog__close {
|
|
|
|
display: none;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Button alignments */
|
|
|
|
|
2023-02-22 22:51:21 +01:00
|
|
|
::slotted([align="left"]) {
|
|
|
|
margin-right: auto;
|
|
|
|
order: -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
::slotted([align="right"]) {
|
|
|
|
margin-left: auto;
|
|
|
|
order: 1;
|
|
|
|
}
|
2022-11-30 23:59:25 +01:00
|
|
|
`
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2022-03-23 00:19:53 +01:00
|
|
|
static get properties()
|
2022-03-15 00:06:42 +01:00
|
|
|
{
|
|
|
|
return {
|
2022-03-23 00:19:53 +01:00
|
|
|
...super.properties,
|
2022-03-15 00:06:42 +01:00
|
|
|
callback: Function,
|
2022-03-16 20:53:50 +01:00
|
|
|
|
2022-04-06 23:13:51 +02:00
|
|
|
/**
|
|
|
|
* Allow other controls to be accessed while the dialog is visible
|
2022-12-07 21:45:38 +01:00
|
|
|
* while not conflicting with internal attribute
|
2022-04-06 23:13:51 +02:00
|
|
|
*/
|
2022-12-07 21:45:38 +01:00
|
|
|
isModal: {type: Boolean, reflect: true},
|
2022-03-22 22:49:42 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Title for the dialog, goes in the header
|
|
|
|
*/
|
2022-03-15 21:10:53 +01:00
|
|
|
title: String,
|
2022-03-22 22:49:42 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Pre-defined group of buttons, one of the BUTTONS_*
|
|
|
|
*/
|
2022-03-15 00:06:42 +01:00
|
|
|
buttons: Number,
|
|
|
|
|
2022-03-22 22:49:42 +01:00
|
|
|
/**
|
|
|
|
* Instead of a message, show this template file instead
|
|
|
|
*/
|
2022-03-16 20:53:50 +01:00
|
|
|
template: String,
|
|
|
|
|
2022-03-22 22:49:42 +01:00
|
|
|
// Force size on the dialog. Normally it sizes to content.
|
2022-03-16 20:53:50 +01:00
|
|
|
width: Number,
|
|
|
|
height: Number,
|
|
|
|
|
2022-03-15 00:06:42 +01:00
|
|
|
// We just pass these on to Et2DialogContent
|
|
|
|
message: String,
|
|
|
|
dialog_type: Number,
|
|
|
|
icon: String,
|
|
|
|
value: Object,
|
|
|
|
|
2022-03-22 22:49:42 +01:00
|
|
|
/**
|
|
|
|
* Automatically destroy the dialog when it closes. Set to false to keep the dialog around.
|
|
|
|
*/
|
2022-07-26 00:12:09 +02:00
|
|
|
destroyOnClose: Boolean,
|
2022-04-29 14:28:32 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Legacy-option for appending dialog into a specific dom node
|
|
|
|
*/
|
|
|
|
appendTo: String,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* When it's set to false dialog won't get closed by hitting Esc
|
|
|
|
*/
|
|
|
|
hideOnEscape: Boolean,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* When set to true it removes the close button from dialog's header
|
|
|
|
*/
|
2023-06-30 19:01:22 +02:00
|
|
|
noCloseButton: {type: Boolean, reflect: true}
|
2022-03-15 00:06:42 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-17 22:21:15 +01:00
|
|
|
/*
|
|
|
|
* List of properties that get translated
|
|
|
|
* Done separately to not interfere with properties - if we re-define label property,
|
|
|
|
* labels go missing.
|
|
|
|
*/
|
|
|
|
static get translate()
|
|
|
|
{
|
|
|
|
return {
|
|
|
|
...super.translate,
|
|
|
|
title: true,
|
|
|
|
message: true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-15 00:06:42 +01:00
|
|
|
private readonly _buttons : DialogButton[][] = [
|
|
|
|
/*
|
|
|
|
Pre-defined Button combos
|
|
|
|
*/
|
|
|
|
//BUTTONS_OK: 0,
|
2022-03-17 22:21:15 +01:00
|
|
|
[{"button_id": Et2Dialog.OK_BUTTON, label: 'ok', id: 'dialog[ok]', image: 'check', "default": true}],
|
2022-03-15 00:06:42 +01:00
|
|
|
//BUTTONS_OK_CANCEL: 1,
|
|
|
|
[
|
2022-03-17 22:21:15 +01:00
|
|
|
{"button_id": Et2Dialog.OK_BUTTON, label: 'ok', id: 'dialog[ok]', image: 'check', "default": true},
|
2022-03-15 21:10:53 +01:00
|
|
|
{
|
|
|
|
"button_id": Et2Dialog.CANCEL_BUTTON,
|
2022-03-17 22:21:15 +01:00
|
|
|
label: 'cancel',
|
2022-03-15 21:10:53 +01:00
|
|
|
id: 'dialog[cancel]',
|
|
|
|
image: 'cancel',
|
|
|
|
align: "right"
|
|
|
|
}
|
2022-03-15 00:06:42 +01:00
|
|
|
],
|
|
|
|
//BUTTONS_YES_NO: 2,
|
|
|
|
[
|
2022-03-17 22:21:15 +01:00
|
|
|
{"button_id": Et2Dialog.YES_BUTTON, label: 'yes', id: 'dialog[yes]', image: 'check', "default": true},
|
|
|
|
{"button_id": Et2Dialog.NO_BUTTON, label: 'no', id: 'dialog[no]', image: 'cancel'}
|
2022-03-15 00:06:42 +01:00
|
|
|
],
|
|
|
|
//BUTTONS_YES_NO_CANCEL: 3,
|
|
|
|
[
|
2022-03-17 22:21:15 +01:00
|
|
|
{"button_id": Et2Dialog.YES_BUTTON, label: 'yes', id: 'dialog[yes]', image: 'check', "default": true},
|
|
|
|
{"button_id": Et2Dialog.NO_BUTTON, label: 'no', id: 'dialog[no]', image: 'cancelled'},
|
2022-03-15 21:10:53 +01:00
|
|
|
{
|
|
|
|
"button_id": Et2Dialog.CANCEL_BUTTON,
|
2022-03-17 22:21:15 +01:00
|
|
|
label: 'cancel',
|
2022-03-15 21:10:53 +01:00
|
|
|
id: 'dialog[cancel]',
|
|
|
|
image: 'cancel',
|
2022-03-17 22:21:15 +01:00
|
|
|
align: "right"
|
2022-03-15 21:10:53 +01:00
|
|
|
}
|
2022-03-15 00:06:42 +01:00
|
|
|
]
|
|
|
|
];
|
|
|
|
|
2022-03-16 20:53:50 +01:00
|
|
|
constructor(parent_egw? : string | IegwAppLocal)
|
2022-03-15 00:06:42 +01:00
|
|
|
{
|
|
|
|
super();
|
|
|
|
|
2022-03-16 20:53:50 +01:00
|
|
|
if(parent_egw)
|
|
|
|
{
|
|
|
|
this._setApiInstance(parent_egw);
|
|
|
|
}
|
2022-12-07 21:45:38 +01:00
|
|
|
this.isModal = false;
|
2022-03-22 22:49:42 +01:00
|
|
|
this.dialog_type = Et2Dialog.PLAIN_MESSAGE;
|
2022-07-26 00:12:09 +02:00
|
|
|
this.destroyOnClose = true;
|
2022-04-29 14:28:32 +02:00
|
|
|
this.hideOnEscape = this.hideOnEscape === false ? false : true;
|
2022-03-15 00:06:42 +01:00
|
|
|
this.__value = {};
|
2022-11-30 23:59:25 +01:00
|
|
|
this.open = true;
|
2022-03-15 00:06:42 +01:00
|
|
|
|
2022-11-30 23:59:25 +01:00
|
|
|
this.handleOpen = this.handleOpen.bind(this);
|
|
|
|
this.handleClose = this.handleClose.bind(this);
|
2022-03-15 00:06:42 +01:00
|
|
|
this._onClick = this._onClick.bind(this);
|
2022-10-28 18:05:03 +02:00
|
|
|
this._onButtonClick = this._onButtonClick.bind(this);
|
2022-05-20 19:40:14 +02:00
|
|
|
this._onMoveResize = this._onMoveResize.bind(this);
|
2023-04-07 00:13:00 +02:00
|
|
|
this.handleKeyUp = this.handleKeyUp.bind(this);
|
2022-03-24 18:21:32 +01:00
|
|
|
this._adoptTemplateButtons = this._adoptTemplateButtons.bind(this);
|
2022-03-17 18:19:47 +01:00
|
|
|
|
2022-11-30 23:59:25 +01:00
|
|
|
// Don't leave it undefined, it's easier to deal with if it's just already resolved.
|
|
|
|
// It will be re-set if a template is loaded
|
|
|
|
this._template_promise = Promise.resolve(false);
|
|
|
|
|
2022-03-17 18:19:47 +01:00
|
|
|
// Create this here so we have something, otherwise the creator might continue with undefined while we
|
|
|
|
// wait for the dialog to complete & open
|
|
|
|
this._complete_promise = new Promise<[number, Object]>((resolve) =>
|
|
|
|
{
|
|
|
|
this._completeResolver = value => resolve(value);
|
|
|
|
});
|
2022-03-15 00:06:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
connectedCallback()
|
|
|
|
{
|
|
|
|
super.connectedCallback();
|
2022-03-16 20:53:50 +01:00
|
|
|
|
2023-04-07 00:13:00 +02:00
|
|
|
this.addEventListener("keyup", this.handleKeyUp);
|
|
|
|
|
2022-11-30 23:59:25 +01:00
|
|
|
// Prevent close if they click the overlay when the dialog is modal
|
|
|
|
this.addEventListener('sl-request-close', event =>
|
2022-03-16 20:53:50 +01:00
|
|
|
{
|
2023-06-30 19:01:22 +02:00
|
|
|
// Prevent close on clicking somewhere else
|
2022-12-07 21:45:38 +01:00
|
|
|
if(this.isModal && event.detail.source === 'overlay')
|
2022-11-30 23:59:25 +01:00
|
|
|
{
|
|
|
|
event.preventDefault();
|
2023-06-30 19:01:22 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Prevent close on escape
|
|
|
|
if(!this.hideOnEscape && event.detail.source === 'keyboard')
|
|
|
|
{
|
|
|
|
event.preventDefault();
|
|
|
|
return;
|
2022-11-30 23:59:25 +01:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
this.addEventListener("sl-after-show", this.handleOpen);
|
|
|
|
this.addEventListener('sl-hide', this.handleClose);
|
2022-03-17 18:19:47 +01:00
|
|
|
|
2022-03-15 21:10:53 +01:00
|
|
|
}
|
2022-03-15 00:06:42 +01:00
|
|
|
|
2022-03-17 18:19:47 +01:00
|
|
|
disconnectedCallback()
|
|
|
|
{
|
|
|
|
super.disconnectedCallback();
|
2023-04-07 00:13:00 +02:00
|
|
|
this.removeEventListener("keyup", this.handleKeyUp);
|
2022-11-30 23:59:25 +01:00
|
|
|
this.removeEventListener("sl-hide", this.handleClose);
|
|
|
|
this.removeEventListener("sl-after-show", this.handleOpen);
|
|
|
|
}
|
|
|
|
|
2022-12-01 18:24:17 +01:00
|
|
|
destroy()
|
|
|
|
{
|
|
|
|
if(this.template)
|
|
|
|
{
|
|
|
|
this.template.clear(true);
|
|
|
|
}
|
|
|
|
this.remove();
|
|
|
|
}
|
|
|
|
|
2022-12-01 19:38:02 +01:00
|
|
|
/**
|
|
|
|
* Hide the dialog.
|
|
|
|
* Depending on destroyOnClose, it may be removed as well
|
|
|
|
*
|
|
|
|
* N.B. We can't have open() because it conflicts with SlDialog. Use show() instead.
|
|
|
|
*/
|
|
|
|
close()
|
|
|
|
{
|
2023-07-14 00:18:11 +02:00
|
|
|
return this.hide();
|
2022-12-01 19:38:02 +01:00
|
|
|
}
|
|
|
|
|
2022-11-30 23:59:25 +01:00
|
|
|
addOpenListeners()
|
|
|
|
{
|
|
|
|
//super.addOpenListeners();
|
|
|
|
|
|
|
|
// Bind on the ancestor, not the buttons, so their click handler gets a chance to run
|
|
|
|
this.addEventListener("click", this._onButtonClick);
|
|
|
|
this.addEventListener("keydown", this.handleKeyDown);
|
|
|
|
}
|
|
|
|
|
|
|
|
removeOpenListeners()
|
|
|
|
{
|
|
|
|
//super.removeOpenListeners();
|
|
|
|
this.removeEventListener("click", this._onButtonClick);
|
|
|
|
this.removeEventListener("keydown", this.handleKeyDown);
|
|
|
|
}
|
|
|
|
|
2023-04-07 00:13:00 +02:00
|
|
|
handleKeyUp(event : KeyboardEvent)
|
2022-11-30 23:59:25 +01:00
|
|
|
{
|
|
|
|
// Trigger the "primary" or first button
|
|
|
|
if(this.open && event.key === 'Enter')
|
|
|
|
{
|
|
|
|
let button = this.querySelectorAll("[varient='primary']");
|
|
|
|
if(button.length == 0)
|
|
|
|
{
|
|
|
|
// Nothing explicitly marked, check for buttons in the footer
|
|
|
|
button = this.querySelectorAll("et2-button[slot='footer']");
|
|
|
|
}
|
|
|
|
if(button && button[0])
|
|
|
|
{
|
|
|
|
event.stopPropagation();
|
|
|
|
button[0].dispatchEvent(new CustomEvent('click', {bubbles: true}));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-07 22:46:44 +02:00
|
|
|
firstUpdated(changedProperties)
|
2022-11-30 23:59:25 +01:00
|
|
|
{
|
2024-05-07 22:46:44 +02:00
|
|
|
super.firstUpdated(changedProperties);
|
|
|
|
|
|
|
|
render(this._contentTemplate(), this);
|
2022-11-30 23:59:25 +01:00
|
|
|
|
|
|
|
// If we start open, fire handler to get setup done
|
|
|
|
if(this.open)
|
|
|
|
{
|
|
|
|
this.handleOpenChange();
|
|
|
|
}
|
|
|
|
|
|
|
|
this.updateComplete.then(() => this._setDefaultAutofocus());
|
2022-03-17 18:19:47 +01:00
|
|
|
}
|
|
|
|
|
2022-03-15 21:10:53 +01:00
|
|
|
// Need to wait for Overlay
|
|
|
|
async getUpdateComplete()
|
|
|
|
{
|
|
|
|
await super.getUpdateComplete();
|
|
|
|
|
2022-03-16 20:53:50 +01:00
|
|
|
// Wait for template to finish loading
|
2022-12-14 18:39:15 +01:00
|
|
|
await this._template_promise;
|
2022-03-15 00:06:42 +01:00
|
|
|
}
|
|
|
|
|
2022-03-17 18:19:47 +01:00
|
|
|
getComplete() : Promise<[number, Object]>
|
|
|
|
{
|
|
|
|
return this._complete_promise;
|
|
|
|
}
|
|
|
|
|
2023-08-03 22:39:47 +02:00
|
|
|
handleOpen(event)
|
2022-03-17 18:19:47 +01:00
|
|
|
{
|
2023-08-03 22:39:47 +02:00
|
|
|
if(event.target !== this)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-11-30 23:59:25 +01:00
|
|
|
this.addOpenListeners();
|
2022-03-17 18:19:47 +01:00
|
|
|
this._button_id = null;
|
2024-01-25 00:42:22 +01:00
|
|
|
this._complete_promise = this._complete_promise || new Promise<[number, Object]>((resolve) =>
|
|
|
|
{
|
|
|
|
this._completeResolver = value => resolve(value);
|
|
|
|
});
|
2022-03-17 22:21:15 +01:00
|
|
|
|
|
|
|
// Now consumers can listen for "open" event, though getUpdateComplete().then(...) also works
|
|
|
|
this.dispatchEvent(new Event('open'));
|
2022-11-30 23:59:25 +01:00
|
|
|
|
|
|
|
Promise.all([this._template_promise, this.updateComplete])
|
|
|
|
.then(() => this._setupMoveResize());
|
2022-03-17 18:19:47 +01:00
|
|
|
}
|
|
|
|
|
2022-11-30 23:59:25 +01:00
|
|
|
handleClose(ev : PointerEvent)
|
2022-03-15 00:06:42 +01:00
|
|
|
{
|
2022-11-30 23:59:25 +01:00
|
|
|
// Avoid closing if a selectbox is closed
|
|
|
|
if(ev.target !== this)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.removeOpenListeners();
|
2022-03-17 18:19:47 +01:00
|
|
|
this._completeResolver([this._button_id, this.value]);
|
|
|
|
|
2022-03-17 22:21:15 +01:00
|
|
|
this.dispatchEvent(new Event('close'));
|
|
|
|
|
2024-01-25 00:42:22 +01:00
|
|
|
waitForEvent(this, 'sl-after-hide').then(() =>
|
2022-03-18 21:05:57 +01:00
|
|
|
{
|
2024-01-25 00:42:22 +01:00
|
|
|
this._button_id = null;
|
|
|
|
|
|
|
|
this._complete_promise = undefined;
|
|
|
|
if(this.destroyOnClose)
|
2022-03-22 22:49:42 +01:00
|
|
|
{
|
2024-01-25 00:42:22 +01:00
|
|
|
if(this._template_widget)
|
|
|
|
{
|
|
|
|
this._template_widget.clear();
|
|
|
|
}
|
|
|
|
this.remove();
|
2022-03-22 22:49:42 +01:00
|
|
|
}
|
2024-01-25 00:42:22 +01:00
|
|
|
});
|
2022-03-15 00:06:42 +01:00
|
|
|
}
|
|
|
|
|
2022-10-28 18:05:03 +02:00
|
|
|
/**
|
2022-12-06 17:44:56 +01:00
|
|
|
* Only internally do our onClick on buttons in the footer
|
2022-10-28 18:05:03 +02:00
|
|
|
* This calls _onClose() when the dialog is closed
|
|
|
|
*
|
|
|
|
* @param {MouseEvent} ev
|
|
|
|
* @returns {boolean}
|
|
|
|
*/
|
|
|
|
_onButtonClick(ev : MouseEvent)
|
|
|
|
{
|
2022-12-06 17:44:56 +01:00
|
|
|
if(ev.target instanceof Et2Button && ev.target.slot == 'footer')
|
2022-10-28 18:05:03 +02:00
|
|
|
{
|
|
|
|
return this._onClick(ev);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-15 00:06:42 +01:00
|
|
|
_onClick(ev : MouseEvent)
|
|
|
|
{
|
|
|
|
// @ts-ignore
|
2022-03-17 18:19:47 +01:00
|
|
|
this._button_id = ev.target?.getAttribute("button_id") ? parseInt(ev.target?.getAttribute("button_id")) : (ev.target?.getAttribute("id") || null);
|
2022-03-15 00:06:42 +01:00
|
|
|
|
2022-10-07 14:48:20 +02:00
|
|
|
// we need to consider still buttons used in dialogs that may actually submit and have server-side interactions(eg.vfsSelect)
|
2022-10-28 18:05:03 +02:00
|
|
|
if(!ev.target?.getInstanceManager()?._etemplate_exec_id)
|
2022-10-07 14:48:20 +02:00
|
|
|
{
|
|
|
|
// we always need to stop the event as otherwise the result would be submitted to server-side eT2 handler
|
|
|
|
// which does not know what to do with it, as the dialog was initiated from client-side (no eT2 request)
|
|
|
|
ev.preventDefault();
|
|
|
|
ev.stopPropagation();
|
|
|
|
}
|
|
|
|
|
2022-10-06 13:55:24 +02:00
|
|
|
|
2022-03-15 00:06:42 +01:00
|
|
|
// Handle anything bound via et2 onclick property
|
2022-03-16 20:53:50 +01:00
|
|
|
try
|
2022-03-15 00:06:42 +01:00
|
|
|
{
|
2022-03-16 20:53:50 +01:00
|
|
|
let et2_widget_result = super._handleClick(ev);
|
2022-10-05 09:53:45 +02:00
|
|
|
if(et2_widget_result === false)
|
2022-03-16 20:53:50 +01:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch(e)
|
|
|
|
{
|
|
|
|
console.log(e);
|
2022-03-15 00:06:42 +01:00
|
|
|
}
|
|
|
|
|
2022-03-16 20:53:50 +01:00
|
|
|
// Callback expects (button_id, value)
|
|
|
|
try
|
|
|
|
{
|
2022-10-05 09:53:45 +02:00
|
|
|
let callback_result = this.callback ? this.callback(this._button_id, this.value, ev) : true;
|
2022-03-16 20:53:50 +01:00
|
|
|
if(callback_result === false)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch(e)
|
2022-03-15 00:06:42 +01:00
|
|
|
{
|
2022-03-16 20:53:50 +01:00
|
|
|
console.log(e);
|
2022-03-15 00:06:42 +01:00
|
|
|
}
|
2022-11-30 23:59:25 +01:00
|
|
|
this.hide();
|
2022-03-15 00:06:42 +01:00
|
|
|
}
|
|
|
|
|
2022-05-20 19:40:14 +02:00
|
|
|
/**
|
|
|
|
* Handle moving and resizing
|
|
|
|
*
|
|
|
|
* @param event
|
|
|
|
*/
|
|
|
|
_onMoveResize(event : InteractEvent)
|
|
|
|
{
|
|
|
|
let target = event.target
|
|
|
|
let x = (parseFloat(target.getAttribute('data-x')) || 0)
|
|
|
|
let y = (parseFloat(target.getAttribute('data-y')) || 0)
|
|
|
|
|
|
|
|
// update the element's style
|
|
|
|
target.style.width = event.rect.width + 'px'
|
|
|
|
target.style.height = event.rect.height + 'px'
|
|
|
|
|
|
|
|
// translate when resizing from top or left edges
|
|
|
|
if(event.type == "resizemove")
|
|
|
|
{
|
|
|
|
x += event.deltaRect.left
|
|
|
|
y += event.deltaRect.top
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
x += event.delta.x;
|
|
|
|
y += event.delta.y;
|
|
|
|
}
|
|
|
|
|
|
|
|
target.style.transform = 'translate(' + x + 'px,' + y + 'px)'
|
|
|
|
|
|
|
|
target.setAttribute('data-x', x)
|
|
|
|
target.setAttribute('data-y', y)
|
|
|
|
}
|
|
|
|
|
2022-03-15 00:06:42 +01:00
|
|
|
/**
|
|
|
|
* Returns the values of any widgets in the dialog. This does not include
|
|
|
|
* the buttons, which are only supplied for the callback.
|
|
|
|
*/
|
|
|
|
get value() : Object
|
|
|
|
{
|
|
|
|
let value = this.__value;
|
2022-04-28 18:00:37 +02:00
|
|
|
if(this._template_widget && this._template_widget.widgetContainer)
|
2022-03-15 00:06:42 +01:00
|
|
|
{
|
2022-03-17 18:19:47 +01:00
|
|
|
value = this._template_widget.getValues(this._template_widget.widgetContainer);
|
2022-03-15 00:06:42 +01:00
|
|
|
}
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
2022-03-16 20:53:50 +01:00
|
|
|
/**
|
|
|
|
* @deprecated
|
|
|
|
* @returns {Object}
|
|
|
|
*/
|
|
|
|
get_value() : Object
|
|
|
|
{
|
|
|
|
console.warn("Deprecated get_value() called");
|
|
|
|
return this.value;
|
|
|
|
}
|
|
|
|
|
2022-03-15 00:06:42 +01:00
|
|
|
set value(new_value)
|
|
|
|
{
|
|
|
|
this.__value = new_value;
|
|
|
|
}
|
|
|
|
|
2022-03-16 20:53:50 +01:00
|
|
|
set template(new_template_name)
|
|
|
|
{
|
2022-03-17 22:21:15 +01:00
|
|
|
let old_template = this.__template;
|
2022-03-16 20:53:50 +01:00
|
|
|
this.__template = new_template_name;
|
2022-12-14 18:39:15 +01:00
|
|
|
|
|
|
|
// Create the new promise here so we can wait for it immediately, not in update
|
|
|
|
this._template_promise = new Promise<boolean>((resolve) =>
|
|
|
|
{
|
|
|
|
this._templateResolver = value => resolve(value);
|
|
|
|
});
|
|
|
|
if(!this.__template)
|
|
|
|
{
|
|
|
|
this._templateResolver(true);
|
|
|
|
}
|
2022-03-16 20:53:50 +01:00
|
|
|
this.requestUpdate("template", old_template);
|
|
|
|
}
|
|
|
|
|
|
|
|
get template()
|
|
|
|
{
|
2022-03-17 22:21:15 +01:00
|
|
|
// Can't return undefined or requestUpdate() will not notice a change
|
|
|
|
return this._template_widget || null;
|
2022-03-16 20:53:50 +01:00
|
|
|
}
|
|
|
|
|
2022-11-30 23:59:25 +01:00
|
|
|
get title() : string { return this.label }
|
|
|
|
|
|
|
|
set title(new_title : string)
|
|
|
|
{
|
|
|
|
this.label = new_title;
|
|
|
|
}
|
2022-03-16 20:53:50 +01:00
|
|
|
|
|
|
|
updated(changedProperties)
|
|
|
|
{
|
|
|
|
super.updated(changedProperties);
|
|
|
|
if(changedProperties.has("template"))
|
|
|
|
{
|
2022-12-12 22:43:47 +01:00
|
|
|
// Wait until update is finished to avoid an error in Safari
|
2022-12-14 18:39:15 +01:00
|
|
|
super.getUpdateComplete().then(() => this._loadTemplate());
|
2022-03-16 20:53:50 +01:00
|
|
|
}
|
2022-11-30 23:59:25 +01:00
|
|
|
if(changedProperties.has("buttons"))
|
|
|
|
{
|
2024-05-07 22:46:44 +02:00
|
|
|
//render(this._buttonsTemplate(), this);
|
2022-11-30 23:59:25 +01:00
|
|
|
this.requestUpdate();
|
|
|
|
}
|
2022-12-02 17:37:22 +01:00
|
|
|
if(changedProperties.has("width"))
|
|
|
|
{
|
|
|
|
this.style.setProperty("--width", this.width ? this.width + "px" : "initial");
|
|
|
|
}
|
2022-03-16 20:53:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
_loadTemplate()
|
|
|
|
{
|
2022-03-17 18:19:47 +01:00
|
|
|
if(this._template_widget)
|
2022-03-16 20:53:50 +01:00
|
|
|
{
|
2022-03-17 18:19:47 +01:00
|
|
|
this._template_widget.clear();
|
2022-03-16 20:53:50 +01:00
|
|
|
}
|
2022-11-30 23:59:25 +01:00
|
|
|
this._contentNode.replaceChildren();
|
2022-03-23 19:01:59 +01:00
|
|
|
|
|
|
|
// Etemplate wants a content
|
|
|
|
if(typeof this.__value.content === "undefined")
|
|
|
|
{
|
|
|
|
this.__value.content = {};
|
|
|
|
}
|
2022-11-30 23:59:25 +01:00
|
|
|
this._template_widget = new etemplate2(this._contentNode);
|
2022-03-24 20:01:41 +01:00
|
|
|
|
|
|
|
// Fire an event so consumers can do their thing - etemplate will fire its own load event when its done
|
|
|
|
if(!this.dispatchEvent(new CustomEvent("before-load", {
|
|
|
|
bubbles: true,
|
|
|
|
cancelable: true,
|
|
|
|
detail: this._template_widget
|
|
|
|
})))
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-03-17 22:21:15 +01:00
|
|
|
if(this.__template.indexOf('.xet') > 0)
|
2022-03-16 20:53:50 +01:00
|
|
|
{
|
2022-08-05 00:15:04 +02:00
|
|
|
let template = this.__template;
|
|
|
|
// inject preprocessor, if not already in template-url
|
|
|
|
const webserverUrl = this.egw().webserverUrl;
|
|
|
|
if(!template.match(new RegExp(webserverUrl + '/api/etemplate.php')))
|
|
|
|
{
|
|
|
|
template = template.replace(new RegExp(webserverUrl), webserverUrl + '/api/etemplate.php');
|
|
|
|
}
|
|
|
|
// if we have no cache-buster, reload daily
|
|
|
|
if(template.indexOf('?') === -1)
|
|
|
|
{
|
|
|
|
template += '?' + ((new Date).valueOf() / 86400 | 0).toString();
|
|
|
|
}
|
2022-03-16 20:53:50 +01:00
|
|
|
// File name provided, fetch from server
|
2022-12-14 18:39:15 +01:00
|
|
|
this._template_widget.load("", template, this.__value || {content: {}},)
|
|
|
|
.then(() =>
|
|
|
|
{
|
|
|
|
this._templateResolver(true);
|
|
|
|
});
|
2022-03-16 20:53:50 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Just template name, it better be loaded already
|
2022-12-14 18:39:15 +01:00
|
|
|
this._template_widget.load(this.__template, '', this.__value || {},
|
2022-03-16 20:53:50 +01:00
|
|
|
// true: do NOT call et2_ready, as it would overwrite this.et2 in app.js
|
2022-12-14 18:39:15 +01:00
|
|
|
undefined, undefined, true)
|
|
|
|
.then(() =>
|
|
|
|
{
|
|
|
|
this._templateResolver(true);
|
|
|
|
});
|
2022-03-16 20:53:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Don't let dialog closing destroy the parent session
|
2022-03-17 18:19:47 +01:00
|
|
|
if(this._template_widget.etemplate_exec_id && this._template_widget.app)
|
2022-03-16 20:53:50 +01:00
|
|
|
{
|
2022-03-17 18:19:47 +01:00
|
|
|
for(let et of etemplate2.getByApplication(this._template_widget.app))
|
2022-03-16 20:53:50 +01:00
|
|
|
{
|
2022-03-17 18:19:47 +01:00
|
|
|
if(et !== this._template_widget && et.etemplate_exec_id === this._template_widget.etemplate_exec_id)
|
2022-03-16 20:53:50 +01:00
|
|
|
{
|
|
|
|
// Found another template using that exec_id, don't destroy when dialog closes.
|
2022-03-17 18:19:47 +01:00
|
|
|
this._template_widget.unbind_unload();
|
2022-03-16 20:53:50 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// set template-name as id, to allow to style dialogs
|
2022-03-30 21:24:52 +02:00
|
|
|
this._template_widget.DOMContainer.setAttribute('id', this.__template.replace(/^(.*\/)?([^/]+?)(\.xet)?(\?.*)?$/, '$2').replace(/\./g, '-'));
|
2022-03-16 20:53:50 +01:00
|
|
|
|
2022-03-24 18:21:32 +01:00
|
|
|
// Look for buttons after load
|
2022-11-30 23:59:25 +01:00
|
|
|
this._contentNode.addEventListener("load", this._adoptTemplateButtons);
|
2022-03-16 20:53:50 +01:00
|
|
|
|
2022-11-30 23:59:25 +01:00
|
|
|
// Default autofocus to first input if autofocus is not set
|
|
|
|
this._contentNode.addEventListener("load", this._setDefaultAutofocus);
|
2022-03-15 00:06:42 +01:00
|
|
|
|
2022-11-30 23:59:25 +01:00
|
|
|
// Need to update to pick up changes
|
|
|
|
this.requestUpdate();
|
2022-03-22 22:49:42 +01:00
|
|
|
}
|
|
|
|
|
2022-11-30 23:59:25 +01:00
|
|
|
_contentTemplate()
|
2022-03-15 00:06:42 +01:00
|
|
|
{
|
2022-11-30 23:59:25 +01:00
|
|
|
/**
|
|
|
|
* Classes for dialog type options
|
|
|
|
*/
|
|
|
|
const _dialogTypes : any = [
|
|
|
|
//PLAIN_MESSAGE: 0
|
|
|
|
"",
|
|
|
|
//INFORMATION_MESSAGE: 1,
|
|
|
|
"dialog_info",
|
|
|
|
//QUESTION_MESSAGE: 2,
|
|
|
|
"dialog_help",
|
|
|
|
//WARNING_MESSAGE: 3,
|
|
|
|
"dialog_warning",
|
|
|
|
//ERROR_MESSAGE: 4,
|
|
|
|
"dialog_error"
|
|
|
|
];
|
|
|
|
let icon = this.icon || this.egw().image(_dialogTypes[this.dialogType] || "") || "";
|
|
|
|
let type = _dialogTypes[this.dialogType];
|
|
|
|
let classes = {
|
|
|
|
dialog_content: true,
|
|
|
|
"dialog--has_message": this.message,
|
|
|
|
"dialog--has_template": this.__template
|
|
|
|
};
|
|
|
|
if(type)
|
|
|
|
{
|
|
|
|
classes[type] = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add in styles set via property
|
|
|
|
let styles = {};
|
|
|
|
if(this.width)
|
|
|
|
{
|
|
|
|
styles["--width"] = this.width;
|
|
|
|
}
|
|
|
|
if(this.height)
|
|
|
|
{
|
2023-11-02 20:03:56 +01:00
|
|
|
styles["--height"] = this.height;
|
2022-11-30 23:59:25 +01:00
|
|
|
}
|
|
|
|
|
2022-03-15 00:06:42 +01:00
|
|
|
return html`
|
2022-11-30 23:59:25 +01:00
|
|
|
<div class=${classMap(classes)} style="${styleMap(styles)}">
|
|
|
|
${this.__template ? "" :
|
|
|
|
html` <img class="dialog_icon" src=${icon}/>
|
|
|
|
<slot>${this.message}</slot>`
|
|
|
|
}
|
|
|
|
|
2024-05-07 22:46:44 +02:00
|
|
|
</div>${this._buttonsTemplate()}`;
|
2022-03-15 00:06:42 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-11-30 23:59:25 +01:00
|
|
|
_buttonsTemplate()
|
2022-03-15 00:06:42 +01:00
|
|
|
{
|
2023-06-30 19:01:22 +02:00
|
|
|
// No buttons set, but careful with BUTTONS_OK
|
|
|
|
if(!this.buttons && this.buttons !== Et2Dialog.BUTTONS_OK)
|
2022-03-15 00:06:42 +01:00
|
|
|
{
|
2022-11-30 23:59:25 +01:00
|
|
|
return;
|
2022-03-15 00:06:42 +01:00
|
|
|
}
|
|
|
|
|
2022-11-30 23:59:25 +01:00
|
|
|
let buttons = this._getButtons();
|
|
|
|
let hasDefault = false;
|
|
|
|
buttons.forEach((button) =>
|
2022-03-16 20:53:50 +01:00
|
|
|
{
|
2022-11-30 23:59:25 +01:00
|
|
|
if(button.default)
|
|
|
|
{
|
|
|
|
hasDefault = true;
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
// Set button._parent here, otherwise button will have trouble finding our egw()
|
|
|
|
return html`${repeat(buttons, (button : DialogButton) => button.id, (button, index) =>
|
2022-03-16 20:53:50 +01:00
|
|
|
{
|
2022-11-30 23:59:25 +01:00
|
|
|
let isDefault = hasDefault && button.default || !hasDefault && index == 0;
|
2022-03-16 20:53:50 +01:00
|
|
|
return html`
|
2022-11-30 23:59:25 +01:00
|
|
|
<et2-button ._parent=${this} id=${button.id} button_id=${button.button_id}
|
|
|
|
label=${button.label}
|
|
|
|
slot="footer"
|
|
|
|
.image=${ifDefined(button.image)}
|
|
|
|
.noSubmit=${true}
|
|
|
|
?disabled=${button.disabled}
|
|
|
|
variant=${isDefault ? "primary" : "default"}
|
|
|
|
align=${ifDefined(button.align)}>
|
|
|
|
</et2-button>
|
|
|
|
`
|
|
|
|
})}`;
|
2022-03-15 00:06:42 +01:00
|
|
|
}
|
|
|
|
|
2022-11-30 23:59:25 +01:00
|
|
|
_getButtons() : DialogButton[]
|
2022-03-15 00:06:42 +01:00
|
|
|
{
|
|
|
|
if(Number.isInteger(this.buttons))
|
|
|
|
{
|
2022-03-17 22:21:15 +01:00
|
|
|
// Translate as needed, since we're not calling transformAttributes() on the buttons
|
|
|
|
// when we create them in a render()
|
|
|
|
return this._buttons[this.buttons].map((but) =>
|
|
|
|
{
|
|
|
|
but.label = this.egw().lang(but.label);
|
|
|
|
return but
|
|
|
|
});
|
2022-03-15 00:06:42 +01:00
|
|
|
}
|
|
|
|
else if(Array.isArray(this.buttons))
|
|
|
|
{
|
|
|
|
return this.buttons;
|
|
|
|
}
|
2022-03-24 18:21:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Search for buttons in the template, and try to slot them
|
|
|
|
*
|
|
|
|
* We don't want to just grab them all, as there may be other buttons.
|
|
|
|
*/
|
|
|
|
_adoptTemplateButtons()
|
|
|
|
{
|
|
|
|
// Check for something with buttons slot set
|
2023-07-12 01:07:51 +02:00
|
|
|
let search_in = <HTMLElement>(this._template_widget?.DOMContainer ?? this._contentNode);
|
2022-12-01 19:20:58 +01:00
|
|
|
if(!search_in)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2022-11-02 15:36:44 +01:00
|
|
|
let template_buttons = [
|
2022-12-01 19:20:58 +01:00
|
|
|
...search_in.querySelectorAll('[slot="footer"],[slot="buttons"]'),
|
2022-03-24 18:21:32 +01:00
|
|
|
// Look for a dialog footer, which will contain several buttons and possible other widgets
|
2023-07-11 13:38:09 +02:00
|
|
|
...search_in.querySelectorAll(".dialogFooterToolbar et2-button"),
|
2022-11-02 15:36:44 +01:00
|
|
|
// Look for buttons at high level (not everywhere, otherwise we can't have other buttons in the template)
|
|
|
|
...search_in.querySelectorAll(":scope > et2-button, :scope > * > et2-button")
|
|
|
|
];
|
2022-03-24 18:21:32 +01:00
|
|
|
if(template_buttons)
|
2022-03-15 00:06:42 +01:00
|
|
|
{
|
2022-12-01 21:02:40 +01:00
|
|
|
if(template_buttons[0]?.instanceOf(Et2Button))
|
2022-12-01 19:20:58 +01:00
|
|
|
{
|
|
|
|
template_buttons[0].variant = "primary";
|
|
|
|
}
|
2022-03-24 18:21:32 +01:00
|
|
|
template_buttons.forEach((button) =>
|
|
|
|
{
|
2022-11-30 23:59:25 +01:00
|
|
|
button.setAttribute("slot", "footer");
|
|
|
|
this.appendChild(button);
|
2022-12-01 19:20:58 +01:00
|
|
|
});
|
|
|
|
this.requestUpdate();
|
2022-03-15 00:06:42 +01:00
|
|
|
}
|
2022-11-02 13:50:50 +01:00
|
|
|
// do NOT submit dialog, if it has no etemplate_exec_id, it only gives and error on server-side
|
|
|
|
if (this._template_widget && !this._template_widget.widgetContainer.getInstanceManager().etemplate_exec_id)
|
|
|
|
{
|
|
|
|
this._template_widget?.DOMContainer.querySelectorAll('et2-button').forEach((button : Et2Button) =>
|
|
|
|
{
|
|
|
|
button.noSubmit = true;
|
|
|
|
});
|
|
|
|
}
|
2022-03-24 18:21:32 +01:00
|
|
|
return template_buttons;
|
2022-03-15 00:06:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-11-30 23:59:25 +01:00
|
|
|
* Set autofocus on first input element if nothing has autofocus
|
2022-03-15 00:06:42 +01:00
|
|
|
*/
|
2022-11-30 23:59:25 +01:00
|
|
|
_setDefaultAutofocus()
|
2022-03-15 00:06:42 +01:00
|
|
|
{
|
2022-11-30 23:59:25 +01:00
|
|
|
const autofocused = this.querySelector("[autofocus]");
|
|
|
|
if(autofocused)
|
|
|
|
{
|
|
|
|
return;
|
2022-03-15 00:06:42 +01:00
|
|
|
}
|
2022-11-30 23:59:25 +01:00
|
|
|
if(this.template)
|
|
|
|
{
|
|
|
|
this.template.focusOnFirstInput();
|
2022-03-15 00:06:42 +01:00
|
|
|
}
|
2022-11-30 23:59:25 +01:00
|
|
|
else
|
|
|
|
{
|
|
|
|
// Not a template, but maybe something?
|
|
|
|
const $input = jQuery('input:visible,et2-textbox:visible,et2-select-email:visible', this)
|
|
|
|
// Date fields open the calendar popup on focus
|
|
|
|
.not('.et2_date')
|
|
|
|
.filter(function()
|
|
|
|
{
|
|
|
|
// Skip inputs that are out of tab ordering
|
|
|
|
const $this = jQuery(this);
|
|
|
|
return !$this.attr('tabindex') || parseInt($this.attr('tabIndex')) >= 0;
|
|
|
|
}).first();
|
|
|
|
|
|
|
|
// mobile device, focus only if the field is empty (usually means new entry)
|
|
|
|
// should focus always for non-mobile one
|
|
|
|
if(egwIsMobile() && $input.val() == "" || !egwIsMobile())
|
|
|
|
{
|
|
|
|
$input.focus();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-03-15 00:06:42 +01:00
|
|
|
|
2022-11-30 23:59:25 +01:00
|
|
|
get _contentNode() : HTMLElement
|
|
|
|
{
|
2023-07-13 18:41:07 +02:00
|
|
|
return this.querySelector('.dialog_content');
|
2022-03-15 00:06:42 +01:00
|
|
|
}
|
|
|
|
|
2022-11-30 23:59:25 +01:00
|
|
|
_setupMoveResize()
|
2022-05-20 19:40:14 +02:00
|
|
|
{
|
|
|
|
// Quick calculation of min size - dialog is made up of header, content & buttons
|
|
|
|
let minHeight = 0;
|
2022-11-30 23:59:25 +01:00
|
|
|
for(let e of this.panel.children)
|
2022-05-20 19:40:14 +02:00
|
|
|
{
|
|
|
|
minHeight += e.getBoundingClientRect().height + parseFloat(getComputedStyle(e).marginTop) + parseFloat(getComputedStyle(e).marginBottom)
|
|
|
|
}
|
|
|
|
|
2022-11-30 23:59:25 +01:00
|
|
|
interact(this.panel)
|
2022-05-20 19:40:14 +02:00
|
|
|
.resizable({
|
|
|
|
edges: {bottom: true, right: true},
|
|
|
|
listeners: {
|
|
|
|
move: this._onMoveResize
|
|
|
|
},
|
|
|
|
modifiers: [
|
|
|
|
// keep the edges inside the parent
|
|
|
|
interact.modifiers.restrictEdges({
|
|
|
|
outer: 'parent'
|
|
|
|
}),
|
|
|
|
|
|
|
|
// minimum size
|
|
|
|
interact.modifiers.restrictSize({
|
|
|
|
min: {width: 100, height: minHeight}
|
|
|
|
})
|
|
|
|
]
|
|
|
|
})
|
|
|
|
|
|
|
|
.draggable({
|
2022-11-30 23:59:25 +01:00
|
|
|
allowFrom: ".dialog__header",
|
|
|
|
ignoreFrom: ".dialog__close",
|
2022-05-20 19:40:14 +02:00
|
|
|
listeners: {
|
|
|
|
move: this._onMoveResize
|
|
|
|
},
|
2022-12-07 21:45:38 +01:00
|
|
|
modifiers: (this.isModal ? [] : [
|
2022-12-07 19:30:14 +01:00
|
|
|
interact.modifiers.restrict({
|
|
|
|
restriction: 'parent',
|
|
|
|
endOnly: true
|
|
|
|
})
|
|
|
|
])
|
2022-05-20 19:40:14 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-03-15 00:06:42 +01:00
|
|
|
/**
|
|
|
|
* Inject application specific egw object with loaded translations into the dialog
|
|
|
|
*
|
|
|
|
* @param {string|egw} _egw_or_appname egw object with already loaded translations or application name to load translations for
|
|
|
|
*/
|
|
|
|
_setApiInstance(_egw_or_appname ? : string | IegwAppLocal)
|
|
|
|
{
|
|
|
|
if(typeof _egw_or_appname == 'undefined')
|
|
|
|
{
|
|
|
|
// @ts-ignore
|
|
|
|
_egw_or_appname = egw_appName;
|
|
|
|
}
|
|
|
|
// if egw object is passed in because called from et2, just use it
|
|
|
|
if(typeof _egw_or_appname != 'string')
|
|
|
|
{
|
|
|
|
this.__egw = _egw_or_appname;
|
|
|
|
}
|
|
|
|
// otherwise use given appname to create app-specific egw instance and load default translations
|
|
|
|
else
|
|
|
|
{
|
|
|
|
this.__egw = egw(_egw_or_appname);
|
|
|
|
this.egw().langRequireApp(this.egw().window, _egw_or_appname);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
egw() : IegwAppLocal
|
|
|
|
{
|
|
|
|
if(this.__egw)
|
|
|
|
{
|
|
|
|
return this.__egw;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return super.egw();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Show a confirmation dialog
|
|
|
|
*
|
|
|
|
* @param {function} _callback Function called when the user clicks a button. The context will be the et2_dialog widget, and the button constant is passed in.
|
|
|
|
* @param {string} _message Message to be place in the dialog.
|
|
|
|
* @param {string} _title Text in the top bar of the dialog.
|
|
|
|
* @param _value passed unchanged to callback as 2. parameter
|
|
|
|
* @param {integer|array} _buttons One of the BUTTONS_ constants defining the set of buttons at the bottom of the box
|
|
|
|
* @param {integer} _type One of the message constants. This defines the style of the message.
|
|
|
|
* @param {string} _icon URL of an icon to display. If not provided, a type-specific icon will be used.
|
|
|
|
* @param {string|egw} _egw_or_appname egw object with already laoded translations or application name to load translations for
|
2022-03-17 18:19:47 +01:00
|
|
|
*
|
|
|
|
* @return {Et2Dialog} You can use dialog.getComplete().then(...) to wait for the dialog to close.
|
2022-03-15 00:06:42 +01:00
|
|
|
*/
|
|
|
|
static show_dialog(_callback? : Function, _message? : string, _title? : string, _value? : object, _buttons?, _type? : number, _icon? : string, _egw_or_appname? : string | IegwAppLocal)
|
|
|
|
{
|
|
|
|
// Just pass them along, widget handles defaults & missing
|
|
|
|
let dialog = <Et2Dialog><unknown>document.createElement('et2-dialog');
|
|
|
|
dialog._setApiInstance(_egw_or_appname);
|
|
|
|
dialog.transformAttributes({
|
|
|
|
callback: _callback || function() {},
|
|
|
|
message: _message,
|
|
|
|
title: _title || dialog.egw().lang('Confirmation required'),
|
|
|
|
buttons: typeof _buttons != 'undefined' ? _buttons : Et2Dialog.BUTTONS_YES_NO,
|
2022-12-07 21:45:38 +01:00
|
|
|
isModal: true,
|
2022-03-15 00:06:42 +01:00
|
|
|
dialog_type: typeof _type != 'undefined' ? _type : Et2Dialog.QUESTION_MESSAGE,
|
|
|
|
icon: _icon,
|
2022-04-06 23:13:51 +02:00
|
|
|
value: _value
|
2022-03-15 00:06:42 +01:00
|
|
|
});
|
2022-03-17 18:19:47 +01:00
|
|
|
|
2022-03-15 00:06:42 +01:00
|
|
|
document.body.appendChild(<LitElement><unknown>dialog);
|
|
|
|
return dialog;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Show an alert message with OK button
|
|
|
|
*
|
|
|
|
* @param {string} _message Message to be place in the dialog.
|
|
|
|
* @param {string} _title Text in the top bar of the dialog.
|
|
|
|
* @param {integer} _type One of the message constants. This defines the style of the message.
|
2022-03-17 18:19:47 +01:00
|
|
|
*
|
|
|
|
* @return Promise<[ button_id : number, value : Object ]> will resolve when the dialog closes
|
2022-03-15 00:06:42 +01:00
|
|
|
*/
|
|
|
|
static alert(_message? : string, _title? : string, _type?)
|
|
|
|
{
|
2022-03-17 18:19:47 +01:00
|
|
|
let dialog = <Et2Dialog><unknown>document.createElement('et2-dialog');
|
|
|
|
dialog._setApiInstance();
|
|
|
|
dialog.transformAttributes({
|
|
|
|
callback: function() {},
|
2022-03-15 00:06:42 +01:00
|
|
|
message: _message,
|
|
|
|
title: _title,
|
2022-03-17 23:18:39 +01:00
|
|
|
buttons: Et2Dialog.BUTTONS_OK,
|
2022-12-07 21:45:38 +01:00
|
|
|
isModal: true,
|
2022-03-17 23:18:39 +01:00
|
|
|
dialog_type: _type || Et2Dialog.INFORMATION_MESSAGE
|
2022-03-17 18:19:47 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
document.body.appendChild(<LitElement><unknown>dialog);
|
|
|
|
|
|
|
|
return dialog.getComplete();
|
2022-03-15 00:06:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Show a prompt dialog
|
|
|
|
*
|
2022-03-17 18:19:47 +01:00
|
|
|
* @param {function} _callback Function called when the user clicks a button. The button constant is passed in along with the value.
|
2022-03-15 00:06:42 +01:00
|
|
|
* @param {string} _message Message to be place in the dialog.
|
|
|
|
* @param {string} _title Text in the top bar of the dialog.
|
|
|
|
* @param {string} _value for prompt, passed to callback as 2. parameter
|
|
|
|
* @param {integer|array} _buttons One of the BUTTONS_ constants defining the set of buttons at the bottom of the box
|
|
|
|
* @param {string|egw} _egw_or_appname egw object with already laoded translations or application name to load translations for
|
2022-03-17 18:19:47 +01:00
|
|
|
*
|
|
|
|
* @return {Et2Dialog} You can use dialog.getComplete().then(...) to wait for the dialog to close.
|
2022-03-15 00:06:42 +01:00
|
|
|
*/
|
|
|
|
static show_prompt(_callback, _message, _title?, _value?, _buttons?, _egw_or_appname?)
|
|
|
|
{
|
2022-03-17 18:19:47 +01:00
|
|
|
let dialog = <Et2Dialog><unknown>document.createElement('et2-dialog');
|
|
|
|
dialog._setApiInstance();
|
|
|
|
dialog.transformAttributes({
|
2022-03-18 21:39:12 +01:00
|
|
|
// Wrap callback to _only_ return _value.value, not the whole object like we normally would
|
|
|
|
callback: function(_button_id, _value)
|
|
|
|
{
|
|
|
|
if(typeof _callback == "function")
|
|
|
|
{
|
|
|
|
_callback.call(this, _button_id, _value.value);
|
|
|
|
}
|
|
|
|
},
|
2022-03-17 18:19:47 +01:00
|
|
|
title: _title || 'Input required',
|
2022-03-17 23:18:39 +01:00
|
|
|
buttons: _buttons || Et2Dialog.BUTTONS_OK_CANCEL,
|
2022-12-07 21:45:38 +01:00
|
|
|
isModal: true,
|
2022-03-15 00:06:42 +01:00
|
|
|
value: {
|
|
|
|
content: {
|
|
|
|
value: _value,
|
|
|
|
message: _message
|
|
|
|
}
|
|
|
|
},
|
2022-03-30 21:24:52 +02:00
|
|
|
template: egw.webserverUrl + '/api/etemplate.php/api/templates/default/prompt.xet',
|
2022-03-15 00:06:42 +01:00
|
|
|
class: "et2_prompt"
|
2022-03-17 18:19:47 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
document.body.appendChild(<LitElement><unknown>dialog);
|
|
|
|
|
|
|
|
return dialog
|
2022-03-15 00:06:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Method to build a confirmation dialog only with
|
|
|
|
* YES OR NO buttons and submit content back to server
|
|
|
|
*
|
|
|
|
* @param {widget} _senders widget that has been clicked
|
2022-05-20 19:40:14 +02:00
|
|
|
* @param {string} _dialogMsg message shows in dialog box
|
|
|
|
* @param {string} _titleMsg message shows as a title of the dialog box
|
|
|
|
* @param {boolean} _postSubmit true: use postSubmit instead of submit
|
2022-03-15 00:06:42 +01:00
|
|
|
*
|
|
|
|
* @description submit the form contents including the button that has been pressed
|
|
|
|
*/
|
2022-03-17 18:19:47 +01:00
|
|
|
static confirm(_senders, _dialogMsg, _titleMsg, _postSubmit?)
|
|
|
|
{
|
|
|
|
let senders = _senders;
|
2023-05-12 18:06:55 +02:00
|
|
|
let button = _senders;
|
2022-03-17 18:19:47 +01:00
|
|
|
let dialogMsg = (typeof _dialogMsg != "undefined") ? _dialogMsg : '';
|
|
|
|
let titleMsg = (typeof _titleMsg != "undefined") ? _titleMsg : '';
|
2024-04-17 11:41:47 +02:00
|
|
|
let egw = _senders?.egw();
|
2022-03-17 18:19:47 +01:00
|
|
|
let callbackDialog = function(button_id)
|
2022-03-15 00:06:42 +01:00
|
|
|
{
|
2022-03-17 18:19:47 +01:00
|
|
|
if(button_id == Et2Dialog.YES_BUTTON)
|
2022-03-15 00:06:42 +01:00
|
|
|
{
|
|
|
|
if(_postSubmit)
|
|
|
|
{
|
2023-05-12 18:06:55 +02:00
|
|
|
senders.getRoot().getInstanceManager().postSubmit(button);
|
2022-03-15 00:06:42 +01:00
|
|
|
}
|
2024-04-12 23:09:45 +02:00
|
|
|
else if(senders.instanceOf(Et2Button) && senders.getType() !== "buttononly")
|
2022-03-15 00:06:42 +01:00
|
|
|
{
|
|
|
|
senders.clicked = true;
|
2024-04-12 23:09:45 +02:00
|
|
|
senders.getInstanceManager().submit(senders, false, senders.novalidate);
|
2022-03-15 00:06:42 +01:00
|
|
|
senders.clicked = false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-05-12 18:06:55 +02:00
|
|
|
senders.clicked = true;
|
|
|
|
senders.getRoot().getInstanceManager().submit(button);
|
|
|
|
senders.clicked = false;
|
2022-03-15 00:06:42 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2022-03-17 18:19:47 +01:00
|
|
|
Et2Dialog.show_dialog(callbackDialog, dialogMsg, titleMsg, {},
|
2022-03-17 23:18:39 +01:00
|
|
|
Et2Dialog.BUTTONS_YES_NO, Et2Dialog.WARNING_MESSAGE, undefined, egw);
|
2022-03-15 00:06:42 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Show a dialog for a long-running, multi-part task
|
|
|
|
*
|
|
|
|
* Given a server url and a list of parameters, this will open a dialog with
|
|
|
|
* a progress bar, asynchronously call the url with each parameter, and update
|
|
|
|
* the progress bar.
|
|
|
|
* Any output from the server will be displayed in a box.
|
|
|
|
*
|
|
|
|
* When all tasks are done, the callback will be called with boolean true. It will
|
|
|
|
* also be called if the user clicks a button (OK or CANCEL), so be sure to
|
|
|
|
* check to avoid executing more than intended.
|
|
|
|
*
|
|
|
|
* @param {function} _callback Function called when the user clicks a button,
|
|
|
|
* or when the list is done processing. The context will be the et2_dialog
|
|
|
|
* widget, and the button constant is passed in.
|
|
|
|
* @param {string} _message Message to be place in the dialog. Usually just
|
|
|
|
* text, but DOM nodes will work too.
|
|
|
|
* @param {string} _title Text in the top bar of the dialog.
|
|
|
|
* @param {string} _menuaction the menuaction function which should be called and
|
|
|
|
* which handles the actual request. If the menuaction is a full featured
|
|
|
|
* url, this one will be used instead.
|
|
|
|
* @param {Array[]} _list - List of parameters, one for each call to the
|
|
|
|
* address. Multiple parameters are allowed, in an array.
|
|
|
|
* @param {string|egw} _egw_or_appname egw object with already laoded translations or application name to load translations for
|
|
|
|
*
|
|
|
|
* @return {et2_dialog}
|
|
|
|
*/
|
|
|
|
static long_task(_callback, _message, _title, _menuaction, _list, _egw_or_appname)
|
|
|
|
{
|
|
|
|
// Special action for cancel
|
|
|
|
let buttons = [
|
2022-03-17 22:21:15 +01:00
|
|
|
// OK starts disabled
|
2022-03-17 23:18:39 +01:00
|
|
|
{"button_id": Et2Dialog.OK_BUTTON, label: 'ok', "default": true, "disabled": true, image: "check"},
|
2022-03-15 00:06:42 +01:00
|
|
|
{
|
2022-03-17 23:18:39 +01:00
|
|
|
"button_id": Et2Dialog.CANCEL_BUTTON, label: 'cancel', image: "cancel", click: function()
|
2022-03-15 00:06:42 +01:00
|
|
|
{
|
|
|
|
// Cancel run
|
|
|
|
cancel = true;
|
2022-03-17 23:18:39 +01:00
|
|
|
jQuery("button[button_id=" + Et2Dialog.CANCEL_BUTTON + "]", dialog.div.parent()).button("disable");
|
2023-08-02 22:08:17 +02:00
|
|
|
updateUi.call(_list.length, '');
|
2022-03-15 00:06:42 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
];
|
2022-03-17 22:21:15 +01:00
|
|
|
let dialog = new Et2Dialog(_egw_or_appname);
|
|
|
|
dialog.transformAttributes({
|
2022-05-20 19:40:14 +02:00
|
|
|
template: dialog.egw().webserverUrl + '/api/etemplate.php/api/templates/default/long_task.xet',
|
2022-03-15 00:06:42 +01:00
|
|
|
value: {
|
|
|
|
content: {
|
|
|
|
message: _message
|
|
|
|
}
|
|
|
|
},
|
|
|
|
callback: function(_button_id, _value)
|
|
|
|
{
|
2022-03-17 22:21:15 +01:00
|
|
|
if(_button_id == Et2Dialog.CANCEL_BUTTON)
|
2022-03-15 00:06:42 +01:00
|
|
|
{
|
|
|
|
cancel = true;
|
|
|
|
}
|
|
|
|
if(typeof _callback == "function")
|
|
|
|
{
|
|
|
|
_callback.call(this, _button_id, _value.value);
|
|
|
|
}
|
|
|
|
},
|
2022-03-17 22:21:15 +01:00
|
|
|
title: _title || 'please wait...',
|
2022-12-07 21:45:38 +01:00
|
|
|
isModal: true,
|
2022-03-15 00:06:42 +01:00
|
|
|
buttons: buttons
|
2022-03-17 22:21:15 +01:00
|
|
|
});
|
2024-04-16 16:49:49 +02:00
|
|
|
document.body.appendChild(<LitElement><unknown>dialog);
|
2022-03-15 00:06:42 +01:00
|
|
|
|
|
|
|
let log = null;
|
|
|
|
let progressbar = null;
|
|
|
|
let cancel = false;
|
|
|
|
let totals = {
|
|
|
|
success: 0,
|
|
|
|
skipped: 0,
|
|
|
|
failed: 0,
|
|
|
|
widget: null
|
|
|
|
};
|
2023-08-02 22:08:17 +02:00
|
|
|
let success = [];
|
|
|
|
let retryDialog = null;
|
2022-03-15 00:06:42 +01:00
|
|
|
|
2023-08-02 22:08:17 +02:00
|
|
|
// Updates progressbar & log, returns next index
|
|
|
|
let updateUi = function(response, index = 0)
|
2022-03-15 00:06:42 +01:00
|
|
|
{
|
|
|
|
progressbar.set_value(100 * (index / _list.length));
|
|
|
|
progressbar.set_label(index + ' / ' + _list.length);
|
|
|
|
|
|
|
|
// Display response information
|
|
|
|
switch(response.type)
|
|
|
|
{
|
|
|
|
case 'error':
|
2022-03-17 22:21:15 +01:00
|
|
|
let div = document.createElement("DIV");
|
|
|
|
div.className = "message error";
|
|
|
|
div.textContent = response.data
|
|
|
|
log.appendChild(div);
|
2022-03-15 00:06:42 +01:00
|
|
|
|
|
|
|
totals.failed++;
|
|
|
|
|
|
|
|
// Ask to retry / ignore / abort
|
2022-03-17 22:21:15 +01:00
|
|
|
let retry = new Et2Dialog(dialog.egw());
|
2023-08-02 22:08:17 +02:00
|
|
|
let retry_index = null;
|
2022-03-17 22:21:15 +01:00
|
|
|
retry.transformAttributes({
|
2022-03-15 00:06:42 +01:00
|
|
|
callback: function(button)
|
|
|
|
{
|
|
|
|
switch(button)
|
|
|
|
{
|
|
|
|
case 'dialog[cancel]':
|
|
|
|
cancel = true;
|
2023-08-02 22:08:17 +02:00
|
|
|
break;
|
2022-03-15 00:06:42 +01:00
|
|
|
case 'dialog[skip]':
|
|
|
|
totals.skipped++;
|
2023-08-02 22:08:17 +02:00
|
|
|
break
|
2022-03-15 00:06:42 +01:00
|
|
|
default:
|
|
|
|
// Try again with previous index
|
2023-08-02 22:08:17 +02:00
|
|
|
retry_index = index - 1;
|
2022-03-15 00:06:42 +01:00
|
|
|
}
|
|
|
|
},
|
|
|
|
message: response.data,
|
|
|
|
title: '',
|
|
|
|
buttons: [
|
|
|
|
// These ones will use the callback, just like normal
|
2022-05-20 19:40:14 +02:00
|
|
|
{label: dialog.egw().lang("Abort"), id: 'dialog[cancel]'},
|
|
|
|
{label: dialog.egw().lang("Retry"), id: 'dialog[retry]'},
|
|
|
|
{label: dialog.egw().lang("Skip"), id: 'dialog[skip]', default: true}
|
2022-03-15 00:06:42 +01:00
|
|
|
],
|
2022-03-17 23:18:39 +01:00
|
|
|
dialog_type: Et2Dialog.ERROR_MESSAGE
|
2022-03-17 22:21:15 +01:00
|
|
|
});
|
|
|
|
dialog.egw().window.document.body.appendChild(<LitElement><unknown>retry);
|
2022-03-15 00:06:42 +01:00
|
|
|
// Early exit
|
2023-08-02 22:08:17 +02:00
|
|
|
retryDialog = retry.getComplete().then(() =>
|
|
|
|
{
|
|
|
|
retryDialog = null;
|
|
|
|
if(retry_index !== null)
|
|
|
|
{
|
|
|
|
sendRequest(retry_index)
|
|
|
|
}
|
|
|
|
});
|
2022-03-15 00:06:42 +01:00
|
|
|
default:
|
|
|
|
if(response && typeof response === "string")
|
|
|
|
{
|
2023-08-02 22:08:17 +02:00
|
|
|
success.push(_list[index - 1]);
|
2022-03-15 00:06:42 +01:00
|
|
|
totals.success++;
|
2022-03-17 22:21:15 +01:00
|
|
|
let div = document.createElement("DIV");
|
|
|
|
div.className = "message";
|
|
|
|
div.textContent = response
|
|
|
|
log.appendChild(div);
|
2022-03-15 00:06:42 +01:00
|
|
|
}
|
2022-03-17 22:21:15 +01:00
|
|
|
else if(response)
|
2022-03-15 00:06:42 +01:00
|
|
|
{
|
2022-03-17 22:21:15 +01:00
|
|
|
let div = document.createElement("DIV");
|
|
|
|
div.className = "message error";
|
|
|
|
div.textContent = JSON.stringify(response)
|
|
|
|
log.appendChild(div);
|
2022-03-15 00:06:42 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// Scroll to bottom
|
2022-03-17 22:21:15 +01:00
|
|
|
let height = log.scrollHeight;
|
|
|
|
log.scrollTop = height;
|
2022-03-15 00:06:42 +01:00
|
|
|
|
|
|
|
// Update totals
|
2022-05-20 19:40:14 +02:00
|
|
|
totals.widget.set_value(dialog.egw().lang(
|
2022-03-15 00:06:42 +01:00
|
|
|
"Total: %1 Successful: %2 Failed: %3 Skipped: %4",
|
|
|
|
_list.length, <string><unknown>totals.success, <string><unknown>totals.failed, <string><unknown>totals.skipped
|
|
|
|
));
|
|
|
|
|
|
|
|
// Fire next step
|
|
|
|
if(!cancel && index < _list.length)
|
|
|
|
{
|
2023-08-02 22:08:17 +02:00
|
|
|
return Promise.resolve(index);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Send off the request for one item */
|
|
|
|
let sendRequest = function(index)
|
|
|
|
{
|
|
|
|
let request = null;
|
|
|
|
let parameters = _list[index];
|
|
|
|
if(typeof parameters != 'object')
|
|
|
|
{
|
|
|
|
parameters = [parameters];
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set up timeout for 30 seconds
|
|
|
|
const timeout_id = window.setTimeout(() =>
|
|
|
|
{
|
|
|
|
// Abort request, we'll either skip it or try again
|
|
|
|
if(request && request.abort)
|
2022-03-15 00:06:42 +01:00
|
|
|
{
|
2023-08-02 22:08:17 +02:00
|
|
|
request.abort();
|
2022-03-15 00:06:42 +01:00
|
|
|
}
|
2023-08-02 22:08:17 +02:00
|
|
|
updateUi({type: 'error', data: dialog.egw().lang("failed") + " " + parameters.join(" ")}, index + 1)
|
|
|
|
}, 30000);
|
2022-03-15 00:06:42 +01:00
|
|
|
|
|
|
|
// Async request, we'll take the next step in the callback
|
|
|
|
// We can't pass index = 0, it looks like false and causes issues
|
2023-08-02 22:08:17 +02:00
|
|
|
try
|
|
|
|
{
|
|
|
|
request = dialog.egw().json(_menuaction, parameters).sendRequest()
|
|
|
|
.then(async(response) =>
|
|
|
|
{
|
|
|
|
if(response && response.response)
|
|
|
|
{
|
|
|
|
clearTimeout(timeout_id);
|
|
|
|
for(let value of response.response)
|
|
|
|
{
|
|
|
|
await updateUi(value.type == "data" ? value.data : value, index + 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.catch(async(response) =>
|
|
|
|
{
|
|
|
|
clearTimeout(timeout_id);
|
|
|
|
updateUi({type: 'error', data: response.message ?? response}, index + 1);
|
|
|
|
});
|
2022-03-15 00:06:42 +01:00
|
|
|
}
|
2023-08-02 22:08:17 +02:00
|
|
|
catch(e)
|
2022-03-15 00:06:42 +01:00
|
|
|
{
|
2023-08-02 22:08:17 +02:00
|
|
|
clearTimeout(timeout_id);
|
|
|
|
request.abort();
|
|
|
|
updateUi({type: 'error', data: dialog.egw().lang("No response from server: your data is probably NOT saved")}, index + 1);
|
2022-03-15 00:06:42 +01:00
|
|
|
}
|
2023-08-02 22:08:17 +02:00
|
|
|
return request;
|
2022-03-15 00:06:42 +01:00
|
|
|
};
|
2022-03-17 22:21:15 +01:00
|
|
|
// Wait for dialog, then start the process
|
2023-08-02 22:08:17 +02:00
|
|
|
dialog.getUpdateComplete().then(async function()
|
2022-03-15 00:06:42 +01:00
|
|
|
{
|
|
|
|
// Get access to template widgets
|
2022-03-17 22:21:15 +01:00
|
|
|
log = dialog.template.widgetContainer.getDOMWidgetById('log').getDOMNode();
|
2022-03-15 00:06:42 +01:00
|
|
|
progressbar = dialog.template.widgetContainer.getWidgetById('progressbar');
|
|
|
|
progressbar.set_label('0 / ' + _list.length);
|
|
|
|
totals.widget = dialog.template.widgetContainer.getWidgetById('totals');
|
|
|
|
|
2023-08-02 22:08:17 +02:00
|
|
|
for(let index = 0; index < _list.length && !cancel; index++)
|
2022-03-15 00:06:42 +01:00
|
|
|
{
|
2023-08-02 22:08:17 +02:00
|
|
|
await sendRequest(index);
|
|
|
|
if(retryDialog)
|
|
|
|
{
|
|
|
|
await retryDialog;
|
|
|
|
}
|
|
|
|
}
|
2022-03-15 00:06:42 +01:00
|
|
|
|
2023-08-02 22:08:17 +02:00
|
|
|
// All done
|
|
|
|
if(!cancel)
|
|
|
|
{
|
|
|
|
progressbar.set_value(100);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Disable cancel (it's too late), enable OK
|
|
|
|
dialog.querySelector('et2-button[button_id="' + Et2Dialog.CANCEL_BUTTON + '"]').setAttribute("disabled", "")
|
|
|
|
dialog.querySelector('et2-button[button_id="' + Et2Dialog.OK_BUTTON + '"]').removeAttribute("disabled")
|
|
|
|
if(!cancel && typeof _callback == "function")
|
|
|
|
{
|
|
|
|
_callback.call(dialog, true, success);
|
|
|
|
}
|
|
|
|
});
|
2022-03-15 00:06:42 +01:00
|
|
|
return dialog;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-20 19:40:14 +02:00
|
|
|
//@ts-ignore TS doesn't recognize Et2Dialog as HTMLEntry
|
2022-03-15 00:06:42 +01:00
|
|
|
customElements.define("et2-dialog", Et2Dialog);
|
|
|
|
// make et2_dialog publicly available as we need to call it from templates
|
|
|
|
{
|
|
|
|
window['et2_dialog'] = Et2Dialog;
|
2022-03-17 18:19:47 +01:00
|
|
|
window['Et2Dialog'] = Et2Dialog;
|
2022-07-27 17:53:56 +02:00
|
|
|
}
|