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";
|
|
|
|
import {et2_button} from "../et2_widget_button";
|
|
|
|
import {LionDialog} from "@lion/dialog";
|
2022-03-17 22:21:15 +01:00
|
|
|
import {et2_widget} from "../et2_core_widget";
|
2022-03-15 00:06:42 +01:00
|
|
|
import {html, LitElement, ScopedElementsMixin, SlotMixin} from "@lion/core";
|
|
|
|
import {Et2DialogOverlay} from "./Et2DialogOverlay";
|
|
|
|
import {Et2DialogContent} from "./Et2DialogContent";
|
2022-03-16 20:53:50 +01:00
|
|
|
import {et2_template} from "../et2_widget_template";
|
|
|
|
import {etemplate2} from "../etemplate2";
|
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
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Et2Dialog widget
|
|
|
|
*
|
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
|
|
|
|
* let callback = function (button_id)
|
|
|
|
* {
|
|
|
|
* 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:
|
|
|
|
* callback - function called when the dialog closes, or false/null.
|
|
|
|
* 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.
|
|
|
|
* 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
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
* // Pass egw in the constructor
|
|
|
|
* 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
|
|
|
*
|
|
|
|
* Because of how LionDialog does its layout and rendering, it's easiest to separate the dialog popup from
|
2022-03-21 16:52:09 +01:00
|
|
|
* the dialog content. This allows us to easily preserve the WebComponent styling. You should interact with the
|
|
|
|
* Et2Dialog though, and ignore the Et2DialogOverlay & Et2DialogContent.
|
2022-03-15 00:06:42 +01:00
|
|
|
*/
|
|
|
|
export class Et2Dialog extends Et2Widget(ScopedElementsMixin(SlotMixin(LionDialog)))
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Dialogs don't always get added to an etemplate, so we keep our own egw
|
|
|
|
*
|
|
|
|
* @type {IegwAppLocal}
|
|
|
|
* @protected
|
|
|
|
*/
|
|
|
|
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
|
|
|
|
*/
|
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.
|
|
|
|
*/
|
|
|
|
protected _complete_promise : Promise<[number, Object]>;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The ID of the button that was clicked. Always one of the button constants,
|
|
|
|
* unless custom buttons were used
|
|
|
|
*
|
|
|
|
* @type {number|null}
|
|
|
|
* @protected
|
|
|
|
*/
|
|
|
|
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-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-03-15 00:06:42 +01:00
|
|
|
modal: Boolean,
|
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.
|
|
|
|
*/
|
|
|
|
destroy_on_close: 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
|
|
|
|
*/
|
|
|
|
noCloseButton: Boolean
|
2022-03-15 00:06:42 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
get slots()
|
|
|
|
{
|
|
|
|
return {
|
|
|
|
...super.slots
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Still not sure what this does, but it's important.
|
|
|
|
// Seems to be related to the constructor, and what's available during the "creation"
|
|
|
|
static get scopedElements()
|
|
|
|
{
|
|
|
|
return {
|
|
|
|
...super.scopedElements,
|
|
|
|
'et2-dialog-overlay-frame': Et2DialogOverlay,
|
|
|
|
'et2-dialog-content': Et2DialogContent
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
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-04-06 23:13:51 +02:00
|
|
|
this.modal = true;
|
2022-03-22 22:49:42 +01:00
|
|
|
this.dialog_type = Et2Dialog.PLAIN_MESSAGE;
|
|
|
|
this.destroy_on_close = 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-03-17 18:19:47 +01:00
|
|
|
this._onOpen = this._onOpen.bind(this);
|
2022-03-15 00:06:42 +01:00
|
|
|
this._onClose = this._onClose.bind(this);
|
|
|
|
this._onClick = this._onClick.bind(this);
|
2022-03-24 18:21:32 +01:00
|
|
|
this._adoptTemplateButtons = this._adoptTemplateButtons.bind(this);
|
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
|
|
|
|
|
|
|
// Wait for everything to complete, then auto-open
|
|
|
|
this.getUpdateComplete().then(() =>
|
|
|
|
{
|
2022-03-17 18:19:47 +01:00
|
|
|
// _overlayCtrl is not available earlier
|
|
|
|
this._overlayCtrl?.addEventListener("show", this._onOpen);
|
|
|
|
this._overlayCtrl.addEventListener('hide', this._onClose);
|
|
|
|
|
2022-03-16 20:53:50 +01:00
|
|
|
window.setTimeout(this.open, 0);
|
|
|
|
});
|
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();
|
|
|
|
this._overlayCtrl.removeEventListener("hide", this._onClose);
|
|
|
|
this._overlayCtrl.removeEventListener("show", this._onOpen);
|
|
|
|
}
|
|
|
|
|
2022-03-15 21:10:53 +01:00
|
|
|
// Need to wait for Overlay
|
|
|
|
async getUpdateComplete()
|
|
|
|
{
|
|
|
|
await super.getUpdateComplete();
|
|
|
|
await this._overlayContentNode.getUpdateComplete();
|
|
|
|
|
2022-03-16 20:53:50 +01:00
|
|
|
// Wait for template to finish loading
|
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
|
|
|
await this._template_promise;
|
2022-03-16 20:53:50 +01:00
|
|
|
}
|
|
|
|
|
2022-03-15 21:10:53 +01:00
|
|
|
// This calls _onClose() when the dialog is closed
|
2022-03-16 20:53:50 +01:00
|
|
|
this._overlayContentNode.querySelectorAll("et2-button").forEach((button) => button.addEventListener("click", this._onClick));
|
2022-03-15 00:06:42 +01:00
|
|
|
}
|
|
|
|
|
2022-03-17 18:19:47 +01:00
|
|
|
getComplete() : Promise<[number, Object]>
|
|
|
|
{
|
|
|
|
return this._complete_promise;
|
|
|
|
}
|
|
|
|
|
|
|
|
_onOpen()
|
|
|
|
{
|
|
|
|
this._button_id = null;
|
|
|
|
this._complete_promise = this._complete_promise || new Promise<[number, Object]>((resolve) => this._completeResolver);
|
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-03-17 18:19:47 +01:00
|
|
|
}
|
|
|
|
|
2022-03-15 00:06:42 +01:00
|
|
|
_onClose(ev : PointerEvent)
|
|
|
|
{
|
2022-03-17 18:19:47 +01:00
|
|
|
this._completeResolver([this._button_id, this.value]);
|
|
|
|
this._button_id = null;
|
|
|
|
this._complete_promise = undefined;
|
|
|
|
|
2022-03-17 22:21:15 +01:00
|
|
|
this.dispatchEvent(new Event('close'));
|
|
|
|
|
2022-03-22 22:49:42 +01:00
|
|
|
if(this.destroy_on_close)
|
2022-03-18 21:05:57 +01:00
|
|
|
{
|
2022-03-22 22:49:42 +01:00
|
|
|
if(this._template_widget)
|
|
|
|
{
|
|
|
|
this._template_widget.clear();
|
|
|
|
}
|
|
|
|
this._overlayCtrl.teardown();
|
|
|
|
this.remove();
|
2022-03-18 21:05:57 +01:00
|
|
|
}
|
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
|
|
|
|
|
|
|
// 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);
|
|
|
|
if(!et2_widget_result)
|
|
|
|
{
|
|
|
|
ev.preventDefault();
|
|
|
|
ev.stopPropagation();
|
|
|
|
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-03-17 18:19:47 +01:00
|
|
|
let callback_result = this.callback ? this.callback(this._button_id, this.value) : true;
|
2022-03-16 20:53:50 +01:00
|
|
|
if(callback_result === false)
|
|
|
|
{
|
|
|
|
ev.preventDefault();
|
|
|
|
ev.stopPropagation();
|
|
|
|
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
|
|
|
}
|
|
|
|
this.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
/**
|
|
|
|
* Fire the close-overlay event to use all registered listeners
|
|
|
|
* @deprecated
|
|
|
|
*/
|
|
|
|
destroy()
|
|
|
|
{
|
|
|
|
this._overlayContentNode.dispatchEvent(new Event('close-overlay'));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @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;
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
updated(changedProperties)
|
|
|
|
{
|
|
|
|
super.updated(changedProperties);
|
|
|
|
if(changedProperties.has("template"))
|
|
|
|
{
|
|
|
|
this._loadTemplate();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_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-03-23 19:01:59 +01:00
|
|
|
|
|
|
|
// Etemplate wants a content
|
|
|
|
if(typeof this.__value.content === "undefined")
|
|
|
|
{
|
|
|
|
this.__value.content = {};
|
|
|
|
}
|
2022-03-17 18:19:47 +01:00
|
|
|
this._template_widget = new etemplate2(this._overlayContentNode._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
|
|
|
{
|
|
|
|
// File name provided, fetch from server
|
2022-03-17 22:21:15 +01:00
|
|
|
this._template_promise = this._template_widget.load("", this.__template, this.__value || {content: {}},);
|
2022-03-16 20:53:50 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Just template name, it better be loaded already
|
2022-03-17 22:21:15 +01:00
|
|
|
this._template_promise = 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
|
|
|
|
undefined, undefined, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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-03-30 23:11:15 +02:00
|
|
|
this._overlayContentNode._contentNode.addEventListener("load", this._adoptTemplateButtons);
|
2022-03-16 20:53:50 +01:00
|
|
|
}
|
|
|
|
|
2022-03-15 00:06:42 +01:00
|
|
|
render()
|
|
|
|
{
|
|
|
|
return this._overlayTemplate();
|
|
|
|
}
|
|
|
|
|
2022-03-22 22:49:42 +01:00
|
|
|
/**
|
|
|
|
* Don't allow any children here, pass them on to the content node
|
|
|
|
*
|
|
|
|
* @param {HTMLElement} node
|
|
|
|
* @returns {any}
|
|
|
|
*/
|
|
|
|
appendChild(node : HTMLElement)
|
|
|
|
{
|
|
|
|
return this._overlayContentNode.appendChild(node);
|
|
|
|
}
|
|
|
|
|
2022-03-15 00:06:42 +01:00
|
|
|
/**
|
|
|
|
* Defining this overlay as a templates from OverlayMixin
|
|
|
|
* this is our source to give as .contentNode to OverlayController.
|
|
|
|
* @protected
|
|
|
|
*/
|
|
|
|
protected _overlayTemplate()
|
|
|
|
{
|
|
|
|
return html`
|
|
|
|
<div id="overlay-content-node-wrapper">
|
|
|
|
<et2-dialog-overlay-frame class="dialog__overlay-frame"
|
2022-03-16 20:53:50 +01:00
|
|
|
.width=${this.width}
|
|
|
|
.height=${this.height}
|
2022-03-15 21:10:53 +01:00
|
|
|
._dialog=${this}
|
2022-03-15 00:06:42 +01:00
|
|
|
.buttons=${this._getButtons()}>
|
|
|
|
<span slot="heading">${this.title}</span>
|
|
|
|
${this._contentTemplate()}
|
|
|
|
|
|
|
|
</et2-dialog-overlay-frame>
|
|
|
|
</div>
|
|
|
|
`;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @override Configures OverlayMixin
|
|
|
|
*/
|
|
|
|
get _overlayContentNode()
|
|
|
|
{
|
|
|
|
if(this._cachedOverlayContentNode)
|
|
|
|
{
|
|
|
|
return this._cachedOverlayContentNode;
|
|
|
|
}
|
|
|
|
this._cachedOverlayContentNode = /** @type {HTMLElement} */ (
|
|
|
|
/** @type {ShadowRoot} */ (this.shadowRoot).querySelector('.dialog__overlay-frame')
|
|
|
|
);
|
|
|
|
return this._cachedOverlayContentNode;
|
|
|
|
}
|
|
|
|
|
|
|
|
_contentTemplate()
|
|
|
|
{
|
2022-03-17 22:21:15 +01:00
|
|
|
if(this.__template)
|
2022-03-16 20:53:50 +01:00
|
|
|
{
|
|
|
|
return html`
|
|
|
|
<div slot="content"></div>`;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return html`
|
|
|
|
<et2-dialog-content slot="content" ?icon=${this.icon}
|
|
|
|
.dialog_type=${this.dialog_type}
|
|
|
|
.value=${this.value}
|
|
|
|
>
|
|
|
|
${this.message}
|
|
|
|
</et2-dialog-content>
|
|
|
|
`;
|
|
|
|
}
|
2022-03-15 00:06:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
_getButtons()
|
|
|
|
{
|
|
|
|
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
|
|
|
|
let search_in = this._template_widget?.DOMContainer || this._overlayContentNode._contentNode;
|
|
|
|
let template_buttons = search_in.querySelectorAll('[slot="buttons"]') ||
|
|
|
|
// Look for a dialog footer, which will contain several buttons and possible other widgets
|
|
|
|
search_in.querySelectorAll(".dialogFooterToolbar") ||
|
|
|
|
// Look for buttons anywhere
|
|
|
|
search_in.querySelectorAll("et2-button");
|
|
|
|
if(template_buttons)
|
2022-03-15 00:06:42 +01:00
|
|
|
{
|
2022-03-24 18:21:32 +01:00
|
|
|
template_buttons.forEach((button) =>
|
|
|
|
{
|
|
|
|
button.setAttribute("slot", "buttons");
|
|
|
|
this._overlayContentNode.appendChild(button);
|
|
|
|
})
|
2022-03-15 00:06:42 +01:00
|
|
|
}
|
2022-03-24 18:21:32 +01:00
|
|
|
return template_buttons;
|
2022-03-15 00:06:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @override Configures OverlayMixin
|
|
|
|
* @desc overrides default configuration options for this component
|
|
|
|
* @returns {Object}
|
|
|
|
*/
|
|
|
|
_defineOverlayConfig()
|
|
|
|
{
|
|
|
|
let not_modal = {
|
|
|
|
hasBackdrop: false,
|
|
|
|
preventsScroll: false,
|
|
|
|
trapsKeyboardFocus: false,
|
|
|
|
}
|
|
|
|
return {
|
|
|
|
...super._defineOverlayConfig(),
|
2022-04-29 14:28:32 +02:00
|
|
|
hidesOnEscape: this.hideOnEscape,
|
2022-03-15 00:06:42 +01:00
|
|
|
...(this.modal ? {} : not_modal)
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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,
|
|
|
|
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,
|
|
|
|
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-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
|
|
|
|
* @param {String} _dialogMsg message shows in dialog box
|
|
|
|
* @param {String} _titleMsg message shows as a title of the dialog box
|
|
|
|
* @param {Bool} _postSubmit true: use postSubmit instead of submit
|
|
|
|
*
|
|
|
|
* @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;
|
|
|
|
let buttonId = _senders.id;
|
|
|
|
let dialogMsg = (typeof _dialogMsg != "undefined") ? _dialogMsg : '';
|
|
|
|
let titleMsg = (typeof _titleMsg != "undefined") ? _titleMsg : '';
|
|
|
|
let egw = _senders instanceof et2_widget ? _senders.egw() : undefined;
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
senders.getRoot().getInstanceManager().postSubmit(buttonId);
|
|
|
|
}
|
|
|
|
else if(senders.instanceOf(et2_button) && senders.getType() !== "buttononly")
|
|
|
|
{
|
|
|
|
senders.clicked = true;
|
|
|
|
senders.getInstanceManager().submit(senders, false, senders.options.novalidate);
|
|
|
|
senders.clicked = false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
senders.getRoot().getInstanceManager().submit(buttonId);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
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");
|
2022-03-15 00:06:42 +01:00
|
|
|
update.call(_list.length, '');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
];
|
2022-03-17 22:21:15 +01:00
|
|
|
let dialog = new Et2Dialog(_egw_or_appname);
|
|
|
|
dialog.transformAttributes({
|
2022-03-30 21:24:52 +02:00
|
|
|
template: 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-03-15 00:06:42 +01:00
|
|
|
buttons: buttons
|
2022-03-17 22:21:15 +01:00
|
|
|
});
|
|
|
|
dialog.egw().window.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
|
|
|
|
};
|
|
|
|
|
|
|
|
// Updates progressbar & log, calls next step
|
|
|
|
let update = function(response)
|
|
|
|
{
|
|
|
|
// context is index
|
|
|
|
let index = this || 0;
|
|
|
|
|
|
|
|
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());
|
|
|
|
retry.transformAttributes({
|
2022-03-15 00:06:42 +01:00
|
|
|
callback: function(button)
|
|
|
|
{
|
2022-03-17 22:21:15 +01:00
|
|
|
debugger;
|
2022-03-15 00:06:42 +01:00
|
|
|
switch(button)
|
|
|
|
{
|
|
|
|
case 'dialog[cancel]':
|
|
|
|
cancel = true;
|
|
|
|
return update.call(index, '');
|
|
|
|
case 'dialog[skip]':
|
|
|
|
// Continue with next index
|
|
|
|
totals.skipped++;
|
|
|
|
return update.call(index, '');
|
|
|
|
default:
|
|
|
|
// Try again with previous index
|
|
|
|
return update.call(index - 1, '');
|
|
|
|
}
|
|
|
|
|
|
|
|
},
|
|
|
|
message: response.data,
|
|
|
|
title: '',
|
|
|
|
buttons: [
|
|
|
|
// These ones will use the callback, just like normal
|
2022-03-17 22:21:15 +01:00
|
|
|
{label: egw.lang("Abort"), id: 'dialog[cancel]'},
|
|
|
|
{label: egw.lang("Retry"), id: 'dialog[retry]'},
|
|
|
|
{label: 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
|
|
|
|
return;
|
|
|
|
default:
|
|
|
|
if(response && typeof response === "string")
|
|
|
|
{
|
|
|
|
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
|
|
|
|
totals.widget.set_value(egw.lang(
|
|
|
|
"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)
|
|
|
|
{
|
|
|
|
var parameters = _list[index];
|
|
|
|
if(typeof parameters != 'object')
|
|
|
|
{
|
|
|
|
parameters = [parameters];
|
|
|
|
}
|
|
|
|
|
|
|
|
// Async request, we'll take the next step in the callback
|
|
|
|
// We can't pass index = 0, it looks like false and causes issues
|
|
|
|
egw.json(_menuaction, parameters, update, index + 1, true, index + 1).sendRequest();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// All done
|
|
|
|
if(!cancel)
|
|
|
|
{
|
|
|
|
progressbar.set_value(100);
|
|
|
|
}
|
2022-03-17 22:21:15 +01:00
|
|
|
|
|
|
|
// Disable cancel (it's too late), enable OK
|
|
|
|
dialog._overlayContentNode.querySelector('et2-button[button_id="' + Et2Dialog.CANCEL_BUTTON + '"]').setAttribute("disabled")
|
|
|
|
dialog._overlayContentNode.querySelector('et2-button[button_id="' + Et2Dialog.OK_BUTTON + '"]').removeAttribute("disabled")
|
2022-03-15 00:06:42 +01:00
|
|
|
if(!cancel && typeof _callback == "function")
|
|
|
|
{
|
|
|
|
_callback.call(dialog, true, response);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-03-17 22:21:15 +01:00
|
|
|
// Wait for dialog, then start the process
|
|
|
|
dialog.getUpdateComplete().then(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');
|
|
|
|
|
|
|
|
// Start
|
|
|
|
window.setTimeout(function()
|
|
|
|
{
|
|
|
|
update.call(0, '');
|
|
|
|
}, 0);
|
|
|
|
});
|
|
|
|
|
|
|
|
return dialog;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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-03-15 00:06:42 +01:00
|
|
|
}
|