2021-08-25 23:35:06 +02:00
|
|
|
import {et2_IDOMNode, et2_implements_registry} from "../et2_core_interfaces";
|
|
|
|
import {et2_arrayMgr} from "../et2_core_arrayMgr";
|
|
|
|
import {et2_attribute_registry, et2_registry, et2_widget} from "../et2_core_widget";
|
|
|
|
import type {etemplate2} from "../etemplate2";
|
|
|
|
import {et2_compileLegacyJS} from "../et2_core_legacyJSFunctions";
|
|
|
|
import {et2_cloneObject, et2_csvSplit} from "../et2_core_common";
|
2021-08-10 23:02:52 +02:00
|
|
|
// @ts-ignore
|
2021-08-25 23:35:06 +02:00
|
|
|
import type {IegwAppLocal} from "../../jsapi/egw_global";
|
2022-06-08 00:39:04 +02:00
|
|
|
import {egw} from "../../jsapi/egw_global";
|
2021-08-25 23:35:06 +02:00
|
|
|
import {ClassWithAttributes, ClassWithInterfaces} from "../et2_core_inheritance";
|
2022-06-10 18:25:31 +02:00
|
|
|
import {css, dedupeMixin, LitElement, PropertyValues, unsafeCSS} from "@lion/core";
|
2021-08-25 23:35:06 +02:00
|
|
|
import type {et2_container} from "../et2_core_baseWidget";
|
2021-09-17 21:12:24 +02:00
|
|
|
import type {et2_DOMWidget} from "../et2_core_DOMWidget";
|
2021-08-10 23:02:52 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* This mixin will allow any LitElement to become an Et2Widget
|
|
|
|
*
|
|
|
|
* Usage:
|
|
|
|
* @example
|
|
|
|
* export class Et2Loading extends Et2Widget(BXLoading) { ... }
|
|
|
|
* @example
|
|
|
|
* export class Et2Button extends Et2InputWidget(Et2Widget(BXButton)) { ... }
|
|
|
|
*
|
|
|
|
* @see Mixin explanation https://lit.dev/docs/composition/mixins/
|
|
|
|
*/
|
2021-08-26 20:59:13 +02:00
|
|
|
function applyMixins(derivedCtor : any, baseCtors : any[])
|
|
|
|
{
|
|
|
|
baseCtors.forEach(baseCtor =>
|
|
|
|
{
|
|
|
|
Object.getOwnPropertyNames(baseCtor.prototype).forEach(name =>
|
|
|
|
{
|
|
|
|
if(name !== 'constructor')
|
|
|
|
{
|
|
|
|
derivedCtor.prototype[name] = baseCtor.prototype[name];
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
2021-08-10 23:02:52 +02:00
|
|
|
|
2022-06-16 21:59:31 +02:00
|
|
|
type Constructor<T = LitElement> = new (...args : any[]) => T;
|
|
|
|
const Et2WidgetMixin = <T extends Constructor>(superClass : T) =>
|
2021-08-10 23:02:52 +02:00
|
|
|
{
|
2021-08-13 23:26:18 +02:00
|
|
|
class Et2WidgetClass extends superClass implements et2_IDOMNode
|
|
|
|
{
|
2021-08-10 23:02:52 +02:00
|
|
|
|
2021-08-19 01:41:23 +02:00
|
|
|
protected _mgrs : et2_arrayMgr[] = [];
|
|
|
|
protected _parent : Et2WidgetClass | et2_widget | null = null;
|
|
|
|
private _inst : etemplate2 | null = null;
|
2022-01-05 21:14:28 +01:00
|
|
|
|
|
|
|
/** et2_widget compatability **/
|
|
|
|
// @ts-ignore Some legacy widgets check their parent to see whats allowed
|
|
|
|
public supportedWidgetClasses = [];
|
2021-08-10 23:02:52 +02:00
|
|
|
|
2021-11-10 21:41:23 +01:00
|
|
|
/**
|
|
|
|
* If we put the widget somewhere other than as a child of its parent, we need to record that so
|
|
|
|
* we don't move it back to the parent.
|
|
|
|
* @type {Element}
|
|
|
|
* @protected
|
|
|
|
*/
|
|
|
|
protected _parent_node : Element;
|
2021-08-13 23:26:18 +02:00
|
|
|
/**
|
|
|
|
* Not actually required by et2_widget, but needed to keep track of non-webComponent children
|
|
|
|
*/
|
2021-08-19 01:41:23 +02:00
|
|
|
private _legacy_children : et2_widget[] = [];
|
2021-08-10 23:02:52 +02:00
|
|
|
|
2021-09-02 21:40:26 +02:00
|
|
|
/**
|
|
|
|
* Keep track of child widgets
|
|
|
|
* This can differ from this.children, as it only includes the widgets where this.children will be child DOM nodes,
|
|
|
|
* not guaranteed to be widgets
|
|
|
|
*/
|
|
|
|
private _children : (et2_widget | Et2WidgetClass)[] = [];
|
|
|
|
|
2021-08-13 23:26:18 +02:00
|
|
|
/**
|
2022-01-12 19:41:13 +01:00
|
|
|
* Internal Properties - default values, and actually creating them as fields
|
|
|
|
* Do not include public property defined in properties()
|
2021-08-13 23:26:18 +02:00
|
|
|
*/
|
2021-09-15 00:01:22 +02:00
|
|
|
protected _widget_id : string = "";
|
|
|
|
protected _dom_id : string = "";
|
2021-08-10 23:02:52 +02:00
|
|
|
|
2022-03-09 22:23:00 +01:00
|
|
|
/**
|
|
|
|
* TypeScript & LitElement ensure type correctness, so we can't have a string value like "$row_cont[disable_me]"
|
|
|
|
* as a boolean property so we store them here, and parse them when expanding. Strings do not have this problem,
|
|
|
|
* since $row_cont[disable_me] is still a valid string.
|
|
|
|
*/
|
|
|
|
protected _deferred_properties : { [key : string] : string } = {};
|
|
|
|
|
2021-08-10 23:02:52 +02:00
|
|
|
|
2021-08-13 23:26:18 +02:00
|
|
|
/** WebComponent **/
|
2021-09-15 22:59:31 +02:00
|
|
|
static get styles()
|
|
|
|
{
|
|
|
|
return [
|
2022-06-08 00:39:04 +02:00
|
|
|
...(super.styles ? (Array.isArray(super.styles) ? super.styles : [super.styles]) : []),
|
2021-09-15 22:59:31 +02:00
|
|
|
css`
|
2023-03-10 22:50:05 +01:00
|
|
|
:host([disabled]) {
|
2021-09-15 22:59:31 +02:00
|
|
|
display: none;
|
2023-03-10 22:50:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* CSS to align internal inputs according to box alignment */
|
|
|
|
:host([align="center"]) .input-group__input {
|
2022-02-23 19:15:55 +01:00
|
|
|
justify-content: center;
|
2023-03-10 22:50:05 +01:00
|
|
|
}
|
|
|
|
:host([align="right"]) .input-group__input {
|
2022-02-23 19:15:55 +01:00
|
|
|
justify-content: flex-end;
|
2023-03-10 22:50:05 +01:00
|
|
|
}
|
|
|
|
`];
|
2021-09-15 22:59:31 +02:00
|
|
|
}
|
|
|
|
|
2021-08-13 23:26:18 +02:00
|
|
|
static get properties()
|
|
|
|
{
|
|
|
|
return {
|
|
|
|
...super.properties,
|
2021-08-10 23:02:52 +02:00
|
|
|
|
2021-12-15 00:55:02 +01:00
|
|
|
/**
|
|
|
|
* Widget ID. Optional, and not always the same as the DOM ID if the widget is inside something
|
|
|
|
* else that also has an ID.
|
2021-12-16 19:27:28 +01:00
|
|
|
* Putting this in the properties() list causes the parent portion of the DOM ID to be duplicated
|
|
|
|
* due to how LitElement processes the change
|
2021-12-15 00:55:02 +01:00
|
|
|
*/
|
2021-12-16 19:27:28 +01:00
|
|
|
//id: {type: String, reflect: false},
|
2021-12-15 00:55:02 +01:00
|
|
|
|
2021-09-03 22:44:52 +02:00
|
|
|
/**
|
|
|
|
* CSS Class. This class is applied to the _outside_, on the web component itself.
|
|
|
|
* Due to how WebComponents work, this might not change anything inside the component.
|
|
|
|
*/
|
|
|
|
class: {type: String, reflect: true},
|
2021-09-03 22:53:51 +02:00
|
|
|
|
2021-08-19 18:54:32 +02:00
|
|
|
/**
|
|
|
|
* Defines whether this widget is visible.
|
|
|
|
* Not to be confused with an input widget's HTML attribute 'disabled'.",
|
|
|
|
*/
|
|
|
|
disabled: {
|
|
|
|
type: Boolean,
|
|
|
|
reflect: true
|
|
|
|
},
|
|
|
|
|
2022-04-05 18:09:49 +02:00
|
|
|
/**
|
|
|
|
* Accesskey provides a hint for generating a keyboard shortcut for the current element.
|
|
|
|
* The attribute value must consist of a single printable character.
|
|
|
|
* @see https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/accesskey
|
|
|
|
*/
|
|
|
|
accesskey: {type: String, reflect: true},
|
|
|
|
|
2021-11-10 21:41:23 +01:00
|
|
|
/**
|
|
|
|
* Widget ID of another node to insert this node into instead of the normal location
|
|
|
|
* This isn't a normal property...
|
|
|
|
*/
|
2022-09-08 21:14:55 +02:00
|
|
|
parentId: {type: String, noAccessor: true},
|
2021-11-10 21:41:23 +01:00
|
|
|
|
2021-08-10 23:02:52 +02:00
|
|
|
/**
|
2021-08-13 23:26:18 +02:00
|
|
|
* Tooltip which is shown for this element on hover
|
2021-08-10 23:02:52 +02:00
|
|
|
*/
|
2022-03-15 13:39:42 +01:00
|
|
|
statustext: {
|
|
|
|
type: String,
|
2023-02-13 21:28:48 +01:00
|
|
|
reflect: true
|
2022-03-15 13:39:42 +01:00
|
|
|
},
|
2021-08-13 23:26:18 +02:00
|
|
|
|
2021-09-16 22:56:13 +02:00
|
|
|
/**
|
|
|
|
* The label of the widget
|
|
|
|
* This is usually displayed in some way. It's also important for accessability.
|
|
|
|
* This is defined in the parent somewhere, and re-defining it causes labels to disappear
|
2023-02-13 21:28:48 +01:00
|
|
|
*/
|
2021-09-16 22:56:13 +02:00
|
|
|
label: {
|
2023-02-13 21:28:48 +01:00
|
|
|
type: String
|
2021-09-16 22:56:13 +02:00
|
|
|
},
|
2021-08-20 18:37:41 +02:00
|
|
|
|
2021-08-13 23:26:18 +02:00
|
|
|
onclick: {
|
2021-08-20 18:37:41 +02:00
|
|
|
type: Function
|
2021-09-02 17:39:17 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
/*** Style type attributes ***/
|
2021-12-17 22:47:19 +01:00
|
|
|
/**
|
|
|
|
* Disable any translations for the widget
|
|
|
|
*/
|
2022-07-21 17:57:50 +02:00
|
|
|
noLang: {
|
2021-12-17 22:47:19 +01:00
|
|
|
type: Boolean,
|
|
|
|
reflect: false
|
|
|
|
},
|
|
|
|
|
2021-09-02 17:39:17 +02:00
|
|
|
/**
|
|
|
|
* Used by Et2Box to determine alignment.
|
|
|
|
* Allowed values are left, right
|
|
|
|
*/
|
|
|
|
align: {
|
|
|
|
type: String,
|
|
|
|
reflect: true
|
2022-11-23 18:30:22 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* comma-separated name:value pairs set as data attributes on DOM node
|
|
|
|
* data="mime:${row}[mime]" would generate data-mime="..." in DOM, eg. to use it in CSS on a parent
|
|
|
|
*/
|
|
|
|
data: {
|
|
|
|
type: String,
|
|
|
|
reflect: false
|
2021-08-10 23:02:52 +02:00
|
|
|
}
|
2021-08-13 23:26:18 +02:00
|
|
|
};
|
|
|
|
}
|
2021-08-10 23:02:52 +02:00
|
|
|
|
2021-09-16 22:56:13 +02:00
|
|
|
/**
|
|
|
|
* List of properties that get translated
|
|
|
|
* Done separately to not interfere with properties - if we re-define label property,
|
|
|
|
* labels go missing.
|
|
|
|
* @returns {{statustext : boolean, label : boolean}}
|
|
|
|
*/
|
|
|
|
static get translate()
|
|
|
|
{
|
|
|
|
return {
|
|
|
|
label: true,
|
|
|
|
statustext: true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-13 23:26:18 +02:00
|
|
|
/**
|
|
|
|
* Widget Mixin constructor
|
|
|
|
*
|
|
|
|
* Note the ...args parameter and super() call
|
|
|
|
*
|
|
|
|
* @param args
|
|
|
|
*/
|
2021-08-19 01:41:23 +02:00
|
|
|
constructor(...args : any[])
|
2021-08-13 23:26:18 +02:00
|
|
|
{
|
|
|
|
super(...args);
|
2021-08-20 18:37:41 +02:00
|
|
|
|
2022-01-10 17:32:31 +01:00
|
|
|
this.disabled = false;
|
2022-05-12 23:22:49 +02:00
|
|
|
this._handleClick = this._handleClick.bind(this);
|
2022-08-22 13:59:07 +02:00
|
|
|
|
|
|
|
// make all sizable widgets large by default on mobile template
|
2023-01-04 20:00:25 +01:00
|
|
|
if(typeof egwIsMobile == "function" && egwIsMobile())
|
2022-08-22 13:59:07 +02:00
|
|
|
{
|
|
|
|
this.size = "large";
|
|
|
|
}
|
2021-08-13 23:26:18 +02:00
|
|
|
}
|
2021-08-10 23:02:52 +02:00
|
|
|
|
2021-08-13 23:26:18 +02:00
|
|
|
connectedCallback()
|
|
|
|
{
|
|
|
|
super.connectedCallback();
|
2021-08-10 23:02:52 +02:00
|
|
|
|
2022-05-13 17:18:00 +02:00
|
|
|
this.addEventListener("click", this._handleClick);
|
|
|
|
|
2021-08-19 01:41:23 +02:00
|
|
|
if(this.statustext)
|
2021-08-13 23:26:18 +02:00
|
|
|
{
|
2022-07-26 15:06:07 +02:00
|
|
|
this.egw().tooltipBind(this, this.egw().lang(this.statustext));
|
2021-08-13 23:26:18 +02:00
|
|
|
}
|
|
|
|
}
|
2021-08-10 23:02:52 +02:00
|
|
|
|
2021-08-13 23:26:18 +02:00
|
|
|
disconnectedCallback()
|
|
|
|
{
|
2021-08-24 22:52:09 +02:00
|
|
|
this.egw()?.tooltipUnbind(this);
|
2021-08-19 18:54:32 +02:00
|
|
|
|
2022-05-12 23:22:49 +02:00
|
|
|
this.removeEventListener("click", this._handleClick);
|
2021-08-13 23:26:18 +02:00
|
|
|
}
|
2021-08-10 23:02:52 +02:00
|
|
|
|
2021-08-13 23:26:18 +02:00
|
|
|
/**
|
|
|
|
* NOT the setter, since we cannot add to the DOM before connectedCallback()
|
|
|
|
*
|
|
|
|
* TODO: This is not best practice. Should just set property, DOM modification should be done in render
|
|
|
|
* https://lit-element.polymer-project.org/guide/templates#design-a-performant-template
|
|
|
|
*
|
|
|
|
* @param value
|
|
|
|
*/
|
2021-08-25 19:32:15 +02:00
|
|
|
set_label(value : string)
|
2021-08-13 23:26:18 +02:00
|
|
|
{
|
2022-03-02 00:56:58 +01:00
|
|
|
let oldValue = this.label;
|
2021-08-13 23:26:18 +02:00
|
|
|
|
|
|
|
// Remove old
|
|
|
|
let oldLabels = this.getElementsByClassName("et2_label");
|
2021-08-19 01:41:23 +02:00
|
|
|
while(oldLabels[0])
|
2021-08-13 23:26:18 +02:00
|
|
|
{
|
|
|
|
this.removeChild(oldLabels[0]);
|
|
|
|
}
|
|
|
|
|
2022-03-02 00:56:58 +01:00
|
|
|
this.__label = value;
|
2021-08-19 01:41:23 +02:00
|
|
|
if(value)
|
2021-08-13 23:26:18 +02:00
|
|
|
{
|
2021-10-19 00:03:05 +02:00
|
|
|
if(this._labelNode)
|
|
|
|
{
|
2022-03-02 00:56:58 +01:00
|
|
|
this._labelNode.textContent = this.__label;
|
2021-10-19 00:03:05 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
let label = document.createElement("span");
|
|
|
|
label.classList.add("et2_label");
|
2022-03-02 00:56:58 +01:00
|
|
|
label.textContent = this.__label;
|
2021-10-19 00:03:05 +02:00
|
|
|
// We should have a slot in the template for the label
|
2022-06-08 00:55:58 +02:00
|
|
|
label.slot = "label";
|
2021-10-19 00:03:05 +02:00
|
|
|
this.appendChild(label);
|
|
|
|
this.requestUpdate('label', oldValue);
|
|
|
|
}
|
2021-08-13 23:26:18 +02:00
|
|
|
}
|
|
|
|
}
|
2021-08-10 23:02:52 +02:00
|
|
|
|
2022-03-11 14:22:04 +01:00
|
|
|
/**
|
|
|
|
* supports legacy set_statustext
|
2022-03-15 13:39:42 +01:00
|
|
|
* @deprecated use this.statustext
|
2022-03-11 14:22:04 +01:00
|
|
|
* @param value
|
|
|
|
*/
|
|
|
|
set_statustext(value : string)
|
|
|
|
{
|
|
|
|
this.statustext = value;
|
2022-03-15 13:39:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
set statustext(value : string)
|
|
|
|
{
|
|
|
|
let oldValue = this.__statustext;
|
|
|
|
this.__statustext = value;
|
2022-03-11 14:22:04 +01:00
|
|
|
this.requestUpdate("statustext", oldValue);
|
|
|
|
}
|
|
|
|
|
2022-03-15 13:39:42 +01:00
|
|
|
get statustext() : string
|
|
|
|
{
|
|
|
|
return this.__statustext;
|
|
|
|
}
|
|
|
|
|
2021-09-14 19:50:10 +02:00
|
|
|
/**
|
|
|
|
* Wrapper on this.disabled because legacy had it.
|
|
|
|
*
|
|
|
|
* @param {boolean} value
|
|
|
|
*/
|
|
|
|
set_disabled(value : boolean)
|
|
|
|
{
|
2022-01-12 21:55:58 +01:00
|
|
|
let oldValue = this.disabled;
|
2022-01-10 17:32:31 +01:00
|
|
|
this.disabled = value;
|
2022-01-12 21:55:58 +01:00
|
|
|
this.requestUpdate("disabled", oldValue);
|
2021-09-14 19:50:10 +02:00
|
|
|
}
|
|
|
|
|
2021-09-15 00:01:22 +02:00
|
|
|
/**
|
|
|
|
* Get the actual DOM ID, which has been prefixed to make sure it's unique.
|
|
|
|
*
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
|
|
|
get dom_id()
|
|
|
|
{
|
|
|
|
return this.getAttribute("id");
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the ID of the widget
|
|
|
|
*
|
|
|
|
* This is the "widget" ID, which is used as an index into the managed arrays (content, etc) and when
|
|
|
|
* trying to find widgets by ID.
|
|
|
|
*
|
|
|
|
* This is not the DOM ID.
|
|
|
|
*
|
|
|
|
* @param {string} value
|
|
|
|
*/
|
|
|
|
set id(value)
|
|
|
|
{
|
|
|
|
this._widget_id = value;
|
2021-12-16 19:27:28 +01:00
|
|
|
let dom_id = "";
|
|
|
|
if(this._widget_id)
|
|
|
|
{
|
2022-03-22 22:42:39 +01:00
|
|
|
// Create a namespace for this object with new ID
|
|
|
|
if(this._createNamespace())
|
|
|
|
{
|
|
|
|
this.checkCreateNamespace();
|
|
|
|
}
|
|
|
|
|
2021-12-16 19:27:28 +01:00
|
|
|
let path = this.getPath();
|
|
|
|
if(this.getInstanceManager())
|
|
|
|
{
|
|
|
|
path.unshift(this.getInstanceManager().uniqueId);
|
|
|
|
}
|
|
|
|
path.push(value);
|
|
|
|
dom_id = path.join("_");
|
|
|
|
}
|
|
|
|
this.setAttribute("id", dom_id);
|
2022-01-24 12:49:59 +01:00
|
|
|
this.requestUpdate("id");
|
2021-09-15 00:01:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the ID of the widget
|
|
|
|
*
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
|
|
|
get id()
|
|
|
|
{
|
|
|
|
return this._widget_id;
|
|
|
|
}
|
|
|
|
|
2022-11-23 18:30:22 +01:00
|
|
|
/**
|
|
|
|
* Set the dataset from a CSV
|
|
|
|
* @param {string} value
|
|
|
|
*/
|
|
|
|
set data(value : string)
|
|
|
|
{
|
|
|
|
// Clear existing
|
|
|
|
Object.keys(this.dataset).forEach(dataKey =>
|
|
|
|
{
|
|
|
|
delete this.dataset[dataKey];
|
|
|
|
});
|
|
|
|
|
|
|
|
let data = value.split(",");
|
|
|
|
data.forEach((field) =>
|
|
|
|
{
|
|
|
|
let f = field.split(":");
|
|
|
|
if(f[0] && typeof f[1] !== "undefined")
|
|
|
|
{
|
|
|
|
this.dataset[f[0]] = f[1];
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
get data() : string
|
|
|
|
{
|
|
|
|
let data = [];
|
|
|
|
Object.keys(this.dataset).forEach((k) =>
|
|
|
|
{
|
|
|
|
data.push(k + ":" + this.dataset[k]);
|
|
|
|
})
|
|
|
|
return data.join(",");
|
|
|
|
}
|
|
|
|
|
2022-03-02 00:56:58 +01:00
|
|
|
/**
|
|
|
|
* A property has changed, and we want to make adjustments to other things
|
|
|
|
* based on that
|
|
|
|
*
|
|
|
|
* @param {import('@lion/core').PropertyValues } changedProperties
|
|
|
|
*/
|
|
|
|
updated(changedProperties : PropertyValues)
|
2021-09-02 17:39:17 +02:00
|
|
|
{
|
2022-03-02 00:56:58 +01:00
|
|
|
super.updated(changedProperties);
|
|
|
|
|
|
|
|
// required changed, add / remove validator
|
|
|
|
if(changedProperties.has('label'))
|
|
|
|
{
|
|
|
|
this._set_label(this.label);
|
|
|
|
}
|
2022-05-09 18:02:07 +02:00
|
|
|
if(changedProperties.has("statustext"))
|
|
|
|
{
|
|
|
|
this.egw().tooltipUnbind(this);
|
|
|
|
if(this.statustext)
|
|
|
|
{
|
|
|
|
this.egw().tooltipBind(this, this.statustext);
|
|
|
|
}
|
|
|
|
}
|
2022-10-17 23:18:13 +02:00
|
|
|
if(changedProperties.has("onclick"))
|
|
|
|
{
|
|
|
|
this.classList.toggle("et2_clickable", this.onclick != null && typeof this.onclick != "undefined");
|
|
|
|
}
|
2022-03-02 00:56:58 +01:00
|
|
|
}
|
|
|
|
|
2022-03-09 22:23:00 +01:00
|
|
|
/**
|
|
|
|
* Any attribute that refers to row content cannot be resolved immediately, but some like booleans cannot stay a
|
|
|
|
* string because it's a boolean attribute. We store them for later, and parse when they're fully in their row.
|
|
|
|
*/
|
|
|
|
get deferredProperties()
|
|
|
|
{
|
|
|
|
return this._deferred_properties;
|
|
|
|
}
|
|
|
|
|
|
|
|
set deferredProperties(value)
|
|
|
|
{
|
|
|
|
this._deferred_properties = value;
|
|
|
|
}
|
|
|
|
|
2022-03-02 00:56:58 +01:00
|
|
|
/**
|
|
|
|
* Do some fancy stuff on the label, splitting it up if there's a %s in it
|
|
|
|
*
|
|
|
|
* Normally called from updated(), the "normal" setter stuff has already been run before
|
|
|
|
* this is called. We only override our special cases (%s) because the normal label has
|
|
|
|
* been set by the parent
|
|
|
|
*
|
|
|
|
* @param value
|
|
|
|
* @protected
|
|
|
|
*/
|
|
|
|
protected _set_label(value : string)
|
|
|
|
{
|
|
|
|
if(!this._labelNode)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Remove any existing post label
|
|
|
|
let existing = (Array.from(this.children)).find(
|
|
|
|
(el : Element) => el.slot === "after" && el.tagName === "LABEL",
|
|
|
|
)
|
|
|
|
if(existing)
|
|
|
|
{
|
|
|
|
this.removeChild(existing);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Split the label at the "%s"
|
|
|
|
let parts = et2_csvSplit(value, 2, "%s");
|
|
|
|
if(parts.length > 1)
|
|
|
|
{
|
|
|
|
let after = document.createElement("label");
|
|
|
|
after.slot = "after";
|
|
|
|
after.textContent = parts[1];
|
|
|
|
this.appendChild(after);
|
|
|
|
|
|
|
|
this._labelNode.textContent = parts[0];
|
|
|
|
}
|
2021-09-02 17:39:17 +02:00
|
|
|
}
|
|
|
|
|
2021-09-03 22:44:52 +02:00
|
|
|
set class(value : string)
|
|
|
|
{
|
|
|
|
let oldValue = this.classList.value;
|
|
|
|
this.classList.value = value;
|
|
|
|
|
|
|
|
this.requestUpdate('class', oldValue);
|
|
|
|
}
|
|
|
|
|
|
|
|
get class()
|
|
|
|
{
|
|
|
|
return this.classList.value;
|
|
|
|
}
|
|
|
|
|
2022-03-08 23:11:32 +01:00
|
|
|
/**
|
|
|
|
* Set the widget class
|
|
|
|
*
|
|
|
|
* @deprecated Use this.class or this.classList instead
|
|
|
|
* @param {string} new_class
|
|
|
|
*/
|
|
|
|
set_class(new_class : string)
|
|
|
|
{
|
|
|
|
this.class = new_class;
|
|
|
|
}
|
|
|
|
|
2021-08-13 23:26:18 +02:00
|
|
|
/**
|
|
|
|
* Event handlers
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Click handler calling custom handler set via onclick attribute to this.onclick
|
|
|
|
*
|
|
|
|
* @param _ev
|
|
|
|
* @returns
|
|
|
|
*/
|
2021-08-19 01:41:23 +02:00
|
|
|
_handleClick(_ev : MouseEvent) : boolean
|
2021-08-13 23:26:18 +02:00
|
|
|
{
|
2021-12-09 18:54:37 +01:00
|
|
|
if(typeof this.onclick == 'function')
|
2021-08-13 23:26:18 +02:00
|
|
|
{
|
|
|
|
// Make sure function gets a reference to the widget, splice it in as 2. argument if not
|
2022-01-05 21:14:28 +01:00
|
|
|
let args = Array.prototype.slice.call(arguments);
|
2021-12-09 18:54:37 +01:00
|
|
|
if(args.indexOf(this) == -1)
|
|
|
|
{
|
|
|
|
args.splice(1, 0, this);
|
|
|
|
}
|
2021-08-10 23:02:52 +02:00
|
|
|
|
2021-09-14 21:23:17 +02:00
|
|
|
return this.onclick(...args);
|
2021-08-13 23:26:18 +02:00
|
|
|
}
|
2021-08-10 23:02:52 +02:00
|
|
|
|
2021-08-13 23:26:18 +02:00
|
|
|
return true;
|
|
|
|
}
|
2021-08-10 23:02:52 +02:00
|
|
|
|
2021-08-13 23:26:18 +02:00
|
|
|
/** et2_widget compatability **/
|
|
|
|
destroy()
|
|
|
|
{
|
|
|
|
// Not really needed, use the disconnectedCallback() and let the browser handle it
|
2023-02-28 18:39:10 +01:00
|
|
|
|
|
|
|
// Call the destructor of all children so any legacy widgets get destroyed
|
|
|
|
for(let i = this.getChildren().length - 1; i >= 0; i--)
|
|
|
|
{
|
|
|
|
this.getChildren()[i].destroy();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Free the array managers if they belong to this widget
|
|
|
|
for(let key in this._mgrs)
|
|
|
|
{
|
|
|
|
if(this._mgrs[key] && this._mgrs[key].owner == this)
|
|
|
|
{
|
|
|
|
delete this._mgrs[key];
|
|
|
|
}
|
|
|
|
}
|
2021-08-13 23:26:18 +02:00
|
|
|
}
|
2021-08-10 23:02:52 +02:00
|
|
|
|
2021-08-19 01:41:23 +02:00
|
|
|
isInTree() : boolean
|
2021-08-13 23:26:18 +02:00
|
|
|
{
|
|
|
|
// TODO: Probably should watch the state or something
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-03-02 00:56:58 +01:00
|
|
|
/**
|
|
|
|
* Get property-values as object
|
|
|
|
*
|
|
|
|
* @deprecated use widget methods
|
|
|
|
*/
|
|
|
|
get options() : object
|
|
|
|
{
|
|
|
|
const options : { [key : string] : any } = {};
|
|
|
|
// @ts-ignore not sure how to tell TS this is a ReactiveElement and properties is a static getter
|
|
|
|
for(const name in this.constructor.properties)
|
|
|
|
{
|
|
|
|
options[name] = this[name];
|
|
|
|
}
|
|
|
|
// adding attributes too
|
|
|
|
this.getAttributeNames().forEach(name =>
|
|
|
|
{
|
|
|
|
options[name] = this.getAttribute(name);
|
|
|
|
});
|
|
|
|
// add some (not declared) known properties
|
|
|
|
if(typeof this.get_value === 'function')
|
|
|
|
{
|
|
|
|
options.value = this.get_value();
|
|
|
|
}
|
|
|
|
console.groupCollapsed("Deprecated widget.options use")
|
|
|
|
console.trace("Something called widget.options on ", this);
|
|
|
|
console.groupEnd();
|
|
|
|
return options;
|
|
|
|
}
|
|
|
|
|
2021-08-13 23:26:18 +02:00
|
|
|
/**
|
|
|
|
* Loads the widget tree from an XML node
|
|
|
|
*
|
|
|
|
* @param _node xml node
|
|
|
|
*/
|
|
|
|
loadFromXML(_node)
|
|
|
|
{
|
|
|
|
// Load the child nodes.
|
2022-01-05 21:14:28 +01:00
|
|
|
for(let i = 0; i < _node.childNodes.length; i++)
|
2021-08-13 23:26:18 +02:00
|
|
|
{
|
2022-01-05 21:14:28 +01:00
|
|
|
let node = _node.childNodes[i];
|
|
|
|
let widgetType = node.nodeName.toLowerCase();
|
2021-08-10 23:02:52 +02:00
|
|
|
|
2021-08-19 01:41:23 +02:00
|
|
|
if(widgetType == "#comment")
|
2021-08-10 23:02:52 +02:00
|
|
|
{
|
2021-08-13 23:26:18 +02:00
|
|
|
continue;
|
2021-08-10 23:02:52 +02:00
|
|
|
}
|
|
|
|
|
2021-08-19 01:41:23 +02:00
|
|
|
if(widgetType == "#text")
|
2021-08-10 23:02:52 +02:00
|
|
|
{
|
2021-08-19 01:41:23 +02:00
|
|
|
if(node.data.replace(/^\s+|\s+$/g, ''))
|
2021-08-13 23:26:18 +02:00
|
|
|
{
|
|
|
|
this.innerText = node.data;
|
|
|
|
}
|
|
|
|
continue;
|
2021-08-10 23:02:52 +02:00
|
|
|
}
|
|
|
|
|
2021-08-13 23:26:18 +02:00
|
|
|
// Create the new element
|
|
|
|
this.createElementFromNode(node);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a et2_widget from an XML node.
|
|
|
|
*
|
|
|
|
* First the type and attributes are read from the node. Then the readonly & modifications
|
|
|
|
* arrays are checked for changes specific to the loaded data. Then the appropriate
|
|
|
|
* constructor is called. After the constructor returns, the widget has a chance to
|
|
|
|
* further initialize itself from the XML node when the widget's loadFromXML() method
|
|
|
|
* is called with the node.
|
|
|
|
*
|
|
|
|
* @param _node XML node to read
|
|
|
|
* @param _name XML node name
|
|
|
|
*
|
|
|
|
* @return et2_widget
|
|
|
|
*/
|
|
|
|
createElementFromNode(_node, _name?)
|
|
|
|
{
|
2022-01-05 21:14:28 +01:00
|
|
|
let attributes = {};
|
2021-08-19 01:41:23 +02:00
|
|
|
|
2021-08-13 23:26:18 +02:00
|
|
|
// Parse the "readonly" and "type" flag for this element here, as they
|
|
|
|
// determine which constructor is used
|
2021-08-19 18:54:32 +02:00
|
|
|
let _nodeName = attributes["type"] = _node.getAttribute("type") ?
|
2021-08-13 23:26:18 +02:00
|
|
|
_node.getAttribute("type") : _node.nodeName.toLowerCase();
|
2021-08-19 18:54:32 +02:00
|
|
|
const readonly = attributes["readonly"] = this.getArrayMgr("readonlys") ?
|
|
|
|
(<any>this.getArrayMgr("readonlys")).isReadOnly(
|
|
|
|
_node.getAttribute("id"), _node.getAttribute("readonly"),
|
|
|
|
typeof this.readonly !== "undefined" ? this.readonly : false) : false;
|
2021-08-13 23:26:18 +02:00
|
|
|
|
|
|
|
// Check to see if modifications change type
|
2022-01-05 21:14:28 +01:00
|
|
|
let modifications = this.getArrayMgr("modifications");
|
2021-08-19 01:41:23 +02:00
|
|
|
if(modifications && _node.getAttribute("id"))
|
2021-08-13 23:26:18 +02:00
|
|
|
{
|
2021-08-19 01:41:23 +02:00
|
|
|
let entry : any = modifications.getEntry(_node.getAttribute("id"));
|
|
|
|
if(entry == null)
|
2021-08-10 23:02:52 +02:00
|
|
|
{
|
2021-08-13 23:26:18 +02:00
|
|
|
// Try again, but skip the fancy stuff
|
|
|
|
// TODO: Figure out why the getEntry() call doesn't always work
|
|
|
|
entry = modifications.data[_node.getAttribute("id")];
|
2021-08-19 01:41:23 +02:00
|
|
|
if(entry)
|
2021-08-13 23:26:18 +02:00
|
|
|
{
|
|
|
|
this.egw().debug("warn", "getEntry(" + _node.getAttribute("id") + ") failed, but the data is there.", modifications, entry);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Try the root, in case a namespace got missed
|
|
|
|
entry = modifications.getRoot().getEntry(_node.getAttribute("id"));
|
|
|
|
}
|
2021-08-10 23:02:52 +02:00
|
|
|
}
|
2021-08-19 01:41:23 +02:00
|
|
|
if(entry && entry.type && typeof entry.type === 'string')
|
2021-08-10 23:02:52 +02:00
|
|
|
{
|
2021-08-13 23:26:18 +02:00
|
|
|
_nodeName = attributes["type"] = entry.type;
|
|
|
|
}
|
|
|
|
entry = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
// if _nodeName / type-attribute contains something to expand (eg. type="@${row}[type]"),
|
|
|
|
// we need to expand it now as it defines the constructor and by that attributes parsed via parseXMLAttrs!
|
2021-08-19 01:41:23 +02:00
|
|
|
if(_nodeName.charAt(0) == '@' || _nodeName.indexOf('$') >= 0)
|
2021-08-13 23:26:18 +02:00
|
|
|
{
|
|
|
|
_nodeName = attributes["type"] = this.getArrayMgr('content').expandName(_nodeName);
|
|
|
|
}
|
|
|
|
|
2022-01-05 21:14:28 +01:00
|
|
|
let widget;
|
2022-05-13 17:38:23 +02:00
|
|
|
if(undefined == window.customElements.get(_nodeName))
|
2021-08-13 23:26:18 +02:00
|
|
|
{
|
|
|
|
// Get the constructor - if the widget is readonly, use the special "_ro"
|
|
|
|
// constructor if it is available
|
2022-05-13 15:32:36 +02:00
|
|
|
if (typeof et2_registry[_nodeName] === "undefined")
|
|
|
|
{
|
|
|
|
_nodeName = 'placeholder';
|
|
|
|
}
|
|
|
|
let constructor = et2_registry[_nodeName];
|
2021-08-19 01:41:23 +02:00
|
|
|
if(readonly === true && typeof et2_registry[_nodeName + "_ro"] != "undefined")
|
2021-08-13 23:26:18 +02:00
|
|
|
{
|
|
|
|
constructor = et2_registry[_nodeName + "_ro"];
|
|
|
|
}
|
2021-08-10 23:02:52 +02:00
|
|
|
|
2021-08-13 23:26:18 +02:00
|
|
|
// Parse the attributes from the given XML attributes object
|
|
|
|
this.parseXMLAttrs(_node.attributes, attributes, constructor.prototype);
|
2021-08-10 23:02:52 +02:00
|
|
|
|
2021-08-13 23:26:18 +02:00
|
|
|
// Do an sanity check for the attributes
|
|
|
|
ClassWithAttributes.generateAttributeSet(et2_attribute_registry[constructor.name], attributes);
|
2021-08-10 23:02:52 +02:00
|
|
|
|
2021-08-13 23:26:18 +02:00
|
|
|
// Creates the new widget, passes this widget as an instance and
|
|
|
|
// passes the widgetType. Then it goes on loading the XML for it.
|
|
|
|
widget = new constructor(this, attributes);
|
|
|
|
|
|
|
|
// Load the widget itself from XML
|
|
|
|
widget.loadFromXML(_node);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-08-19 18:54:32 +02:00
|
|
|
widget = loadWebComponent(_nodeName, _node, this);
|
2021-08-13 23:26:18 +02:00
|
|
|
|
2021-08-19 01:41:23 +02:00
|
|
|
if(this.addChild)
|
2021-08-13 23:26:18 +02:00
|
|
|
{
|
|
|
|
// webcomponent going into old et2_widget
|
|
|
|
this.addChild(widget);
|
2021-08-10 23:02:52 +02:00
|
|
|
}
|
2021-08-13 23:26:18 +02:00
|
|
|
}
|
|
|
|
return widget;
|
|
|
|
}
|
2021-08-10 23:02:52 +02:00
|
|
|
|
|
|
|
|
2021-08-13 23:26:18 +02:00
|
|
|
/**
|
|
|
|
* The parseXMLAttrs function takes an XML DOM attributes object
|
|
|
|
* and adds the given attributes to the _target associative array. This
|
|
|
|
* function also parses the legacyOptions.
|
|
|
|
*
|
2022-01-05 21:14:28 +01:00
|
|
|
* N.B. This is only used for legacy widgets. WebComponents use transformAttributes() and
|
|
|
|
* do their own handling of attributes.
|
|
|
|
*
|
2021-08-13 23:26:18 +02:00
|
|
|
* @param _attrsObj is the XML DOM attributes object
|
|
|
|
* @param {object} _target is the object to which the attributes should be written.
|
|
|
|
* @param {et2_widget} _proto prototype with attributes and legacyOptions attribute
|
|
|
|
*/
|
|
|
|
parseXMLAttrs(_attrsObj, _target, _proto)
|
|
|
|
{
|
|
|
|
// Check whether the attributes object is really existing, if not abort
|
2021-08-19 01:41:23 +02:00
|
|
|
if(typeof _attrsObj == "undefined")
|
2021-08-13 23:26:18 +02:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Iterate over the given attributes and parse them
|
2022-01-05 21:14:28 +01:00
|
|
|
let mgr = this.getArrayMgr("content");
|
|
|
|
for(let i = 0; i < _attrsObj.length; i++)
|
2021-08-13 23:26:18 +02:00
|
|
|
{
|
2022-01-05 21:14:28 +01:00
|
|
|
let attrName = _attrsObj[i].name;
|
|
|
|
let attrValue = _attrsObj[i].value;
|
2021-08-13 23:26:18 +02:00
|
|
|
|
|
|
|
// Special handling for the legacy options
|
2021-08-19 01:41:23 +02:00
|
|
|
if(attrName == "options" && _proto.constructor.legacyOptions && _proto.constructor.legacyOptions.length > 0)
|
2021-08-10 23:02:52 +02:00
|
|
|
{
|
2021-08-13 23:26:18 +02:00
|
|
|
let legacy = _proto.constructor.legacyOptions || [];
|
|
|
|
// Check for modifications on legacy options here. Normal modifications
|
|
|
|
// are handled in widget constructor, but it's too late for legacy options then
|
2021-08-19 01:41:23 +02:00
|
|
|
if(_target.id && this.getArrayMgr("modifications").getEntry(_target.id))
|
2021-08-13 23:26:18 +02:00
|
|
|
{
|
2022-01-05 21:14:28 +01:00
|
|
|
let mod : any = this.getArrayMgr("modifications").getEntry(_target.id);
|
2021-12-09 18:54:37 +01:00
|
|
|
if(typeof mod.options != "undefined")
|
|
|
|
{
|
|
|
|
attrValue = _attrsObj[i].value = mod.options;
|
|
|
|
}
|
2021-08-13 23:26:18 +02:00
|
|
|
}
|
|
|
|
// expand legacyOptions with content
|
2021-08-19 01:41:23 +02:00
|
|
|
if(attrValue.charAt(0) == '@' || attrValue.indexOf('$') != -1)
|
2021-08-13 23:26:18 +02:00
|
|
|
{
|
|
|
|
attrValue = mgr.expandName(attrValue);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parse the legacy options (as a string, other types not allowed)
|
2022-01-05 21:14:28 +01:00
|
|
|
let splitted = et2_csvSplit(attrValue + "");
|
2021-08-13 23:26:18 +02:00
|
|
|
|
2022-01-05 21:14:28 +01:00
|
|
|
for(let j = 0; j < splitted.length && j < legacy.length; j++)
|
2021-08-13 23:26:18 +02:00
|
|
|
{
|
|
|
|
// Blank = not set, unless there's more legacy options provided after
|
2021-12-09 18:54:37 +01:00
|
|
|
if(splitted[j].trim().length === 0 && legacy.length >= splitted.length)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
2021-08-13 23:26:18 +02:00
|
|
|
|
|
|
|
// Check to make sure we don't overwrite a current option with a legacy option
|
2021-08-19 01:41:23 +02:00
|
|
|
if(typeof _target[legacy[j]] === "undefined")
|
2021-08-13 23:26:18 +02:00
|
|
|
{
|
|
|
|
attrValue = splitted[j];
|
|
|
|
|
|
|
|
/**
|
2021-08-10 23:02:52 +02:00
|
|
|
If more legacy options than expected, stuff them all in the last legacy option
|
|
|
|
Some legacy options take a comma separated list.
|
2021-08-13 23:26:18 +02:00
|
|
|
*/
|
2021-08-19 01:41:23 +02:00
|
|
|
if(j == legacy.length - 1 && splitted.length > legacy.length)
|
2021-08-13 23:26:18 +02:00
|
|
|
{
|
|
|
|
attrValue = splitted.slice(j);
|
|
|
|
}
|
|
|
|
|
2022-01-05 21:14:28 +01:00
|
|
|
let attr = et2_attribute_registry[_proto.constructor.name][legacy[j]] || {};
|
2021-08-13 23:26:18 +02:00
|
|
|
|
|
|
|
// If the attribute is marked as boolean, parse the
|
|
|
|
// expression as bool expression.
|
2021-08-19 01:41:23 +02:00
|
|
|
if(attr.type == "boolean")
|
2021-08-13 23:26:18 +02:00
|
|
|
{
|
|
|
|
attrValue = mgr.parseBoolExpression(attrValue);
|
|
|
|
}
|
2021-08-19 01:41:23 +02:00
|
|
|
else if(typeof attrValue != "object")
|
2021-08-13 23:26:18 +02:00
|
|
|
{
|
|
|
|
attrValue = mgr.expandName(attrValue);
|
|
|
|
}
|
|
|
|
_target[legacy[j]] = attrValue;
|
2021-08-10 23:02:52 +02:00
|
|
|
}
|
2021-08-13 23:26:18 +02:00
|
|
|
}
|
2021-08-10 23:02:52 +02:00
|
|
|
}
|
2021-08-19 01:41:23 +02:00
|
|
|
else if(attrName == "readonly" && typeof _target[attrName] != "undefined")
|
2021-08-10 23:02:52 +02:00
|
|
|
{
|
2021-08-13 23:26:18 +02:00
|
|
|
// do NOT overwrite already evaluated readonly attribute
|
2021-08-10 23:02:52 +02:00
|
|
|
}
|
2021-08-13 23:26:18 +02:00
|
|
|
else
|
2021-08-10 23:02:52 +02:00
|
|
|
{
|
2021-08-13 23:26:18 +02:00
|
|
|
let attrs = et2_attribute_registry[_proto.constructor.name] || {};
|
2021-08-19 01:41:23 +02:00
|
|
|
if(mgr != null && typeof attrs[attrName] != "undefined")
|
2021-08-13 23:26:18 +02:00
|
|
|
{
|
2022-01-05 21:14:28 +01:00
|
|
|
let attr = attrs[attrName];
|
2021-08-13 23:26:18 +02:00
|
|
|
|
|
|
|
// If the attribute is marked as boolean, parse the
|
|
|
|
// expression as bool expression.
|
2021-08-19 01:41:23 +02:00
|
|
|
if(attr.type == "boolean")
|
2021-08-10 23:02:52 +02:00
|
|
|
{
|
2021-08-13 23:26:18 +02:00
|
|
|
attrValue = mgr.parseBoolExpression(attrValue);
|
2021-08-10 23:02:52 +02:00
|
|
|
}
|
2021-08-13 23:26:18 +02:00
|
|
|
else
|
2021-08-10 23:02:52 +02:00
|
|
|
{
|
2021-08-13 23:26:18 +02:00
|
|
|
attrValue = mgr.expandName(attrValue);
|
2021-08-10 23:02:52 +02:00
|
|
|
}
|
2021-08-13 23:26:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Set the attribute
|
|
|
|
_target[attrName] = attrValue;
|
2021-08-10 23:02:52 +02:00
|
|
|
}
|
2021-08-13 23:26:18 +02:00
|
|
|
}
|
|
|
|
}
|
2021-08-10 23:02:52 +02:00
|
|
|
|
2021-12-15 00:55:02 +01:00
|
|
|
transformAttributes(attrs)
|
|
|
|
{
|
|
|
|
transformAttributes(this, this.getArrayMgr("content"), attrs);
|
2022-04-20 22:09:01 +02:00
|
|
|
|
|
|
|
// Add in additional modifications
|
2022-06-08 00:39:04 +02:00
|
|
|
if(this.id && this.getArrayMgr("modifications")?.getEntry(this.id))
|
2022-04-20 22:09:01 +02:00
|
|
|
{
|
|
|
|
transformAttributes(this, this.getArrayMgr("content"), this.getArrayMgr("modifications").getEntry(this.id));
|
|
|
|
}
|
2021-12-15 00:55:02 +01:00
|
|
|
}
|
|
|
|
|
2021-08-19 01:41:23 +02:00
|
|
|
iterateOver(_callback : Function, _context, _type)
|
2021-08-13 23:26:18 +02:00
|
|
|
{
|
2023-02-14 18:09:58 +01:00
|
|
|
if(typeof _type === "undefined" || _type === et2_widget || _type === Et2Widget ||
|
2022-08-07 10:10:33 +02:00
|
|
|
typeof _type === 'function' && this instanceof _type ||
|
|
|
|
et2_implements_registry[_type] && et2_implements_registry[_type](this))
|
2021-08-13 23:26:18 +02:00
|
|
|
{
|
|
|
|
_callback.call(_context, this);
|
|
|
|
}
|
2021-08-23 18:21:51 +02:00
|
|
|
|
2021-09-02 21:40:26 +02:00
|
|
|
// Ask children
|
|
|
|
for(let i = 0; i < this._children.length; i++)
|
2021-08-23 18:21:51 +02:00
|
|
|
{
|
2021-09-02 21:40:26 +02:00
|
|
|
this._children[i].iterateOver(_callback, _context, _type);
|
2021-08-23 18:21:51 +02:00
|
|
|
}
|
2021-08-13 23:26:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Needed for legacy compatability.
|
|
|
|
*
|
|
|
|
* @param {Promise[]} promises List of promises from widgets that are not done. Pass an empty array, it will be filled if needed.
|
|
|
|
*/
|
2021-11-10 19:47:07 +01:00
|
|
|
loadingFinished(promises : Promise<any>[])
|
2021-08-13 23:26:18 +02:00
|
|
|
{
|
2021-12-09 18:54:37 +01:00
|
|
|
if(typeof promises === "undefined")
|
|
|
|
{
|
|
|
|
promises = [];
|
|
|
|
}
|
2021-11-10 19:47:07 +01:00
|
|
|
// Note that WebComponents don't do anything here, their lifecycle is different
|
|
|
|
// This is just to support legacy widgets
|
|
|
|
let doLoadingFinished = () =>
|
2021-08-13 23:26:18 +02:00
|
|
|
{
|
2021-11-10 19:47:07 +01:00
|
|
|
/**
|
|
|
|
* This is needed mostly as a bridge between non-WebComponent widgets and
|
|
|
|
* connectedCallback(). It's not really needed if the whole tree is WebComponent.
|
|
|
|
* WebComponents can be added as children immediately after creation, and they handle the
|
|
|
|
* rest themselves with their normal lifecycle (especially connectedCallback(), which is kind
|
|
|
|
* of the equivalent of doLoadingFinished()
|
|
|
|
*/
|
2022-01-05 21:14:28 +01:00
|
|
|
// @ts-ignore this is not an et2_widget, so getDOMNode(this) is bad
|
2021-11-10 21:41:23 +01:00
|
|
|
if(!this._parent_node && this.getParent() instanceof et2_widget && (<et2_DOMWidget>this.getParent()).getDOMNode(this) != this.parentNode)
|
2021-11-10 19:47:07 +01:00
|
|
|
{
|
2022-01-05 21:14:28 +01:00
|
|
|
// @ts-ignore this is not an et2_widget, and Et2Widget is not a Node
|
|
|
|
(<et2_DOMWidget>this.getParent()).getDOMNode(this).append(this);
|
2021-11-10 19:47:07 +01:00
|
|
|
}
|
2021-08-13 23:26:18 +02:00
|
|
|
|
2021-11-10 19:47:07 +01:00
|
|
|
// An empty text node causes problems with legacy widget children
|
|
|
|
// It throws off their insertion indexing, making them get added in the wrong place
|
2022-07-29 16:57:36 +02:00
|
|
|
if(this.childNodes[0]?.nodeType == this.TEXT_NODE && this.childNodes[0].textContent == "")
|
2021-11-10 19:47:07 +01:00
|
|
|
{
|
|
|
|
this.removeChild(this.childNodes[0]);
|
|
|
|
}
|
|
|
|
for(let i = 0; i < this.getChildren().length; i++)
|
|
|
|
{
|
|
|
|
let child = this.getChildren()[i];
|
2021-09-13 19:26:29 +02:00
|
|
|
|
2021-11-10 19:47:07 +01:00
|
|
|
child.loadingFinished(promises);
|
|
|
|
}
|
|
|
|
};
|
2021-12-09 18:54:37 +01:00
|
|
|
doLoadingFinished();
|
2022-03-08 23:11:32 +01:00
|
|
|
|
|
|
|
promises.push(this.getUpdateComplete());
|
2021-08-13 23:26:18 +02:00
|
|
|
}
|
2021-08-10 23:02:52 +02:00
|
|
|
|
2021-08-13 23:26:18 +02:00
|
|
|
getWidgetById(_id)
|
|
|
|
{
|
2021-08-19 01:41:23 +02:00
|
|
|
if(this.id == _id)
|
2021-08-13 23:26:18 +02:00
|
|
|
{
|
|
|
|
return this;
|
|
|
|
}
|
2021-09-02 21:40:26 +02:00
|
|
|
if(this.getChildren().length == 0)
|
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
let check_children = children =>
|
|
|
|
{
|
2022-01-05 21:14:28 +01:00
|
|
|
for(let i = 0; i < children.length; i++)
|
2021-09-02 21:40:26 +02:00
|
|
|
{
|
2022-01-05 21:14:28 +01:00
|
|
|
let elem = children[i].getWidgetById(_id);
|
2021-09-02 21:40:26 +02:00
|
|
|
|
|
|
|
if(elem != null)
|
|
|
|
{
|
|
|
|
return elem;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(this.id && _id.indexOf('[') > -1 && children.length)
|
|
|
|
{
|
2022-01-05 21:14:28 +01:00
|
|
|
let ids = (new et2_arrayMgr()).explodeKey(_id);
|
|
|
|
let widget : Et2WidgetClass = this;
|
|
|
|
for(let i = 0; i < ids.length && widget !== null; i++)
|
2021-09-02 21:40:26 +02:00
|
|
|
{
|
|
|
|
widget = widget.getWidgetById(ids[i]);
|
|
|
|
}
|
|
|
|
return widget;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
return check_children(this.getChildren()) || null;
|
2021-08-13 23:26:18 +02:00
|
|
|
}
|
2021-08-10 23:02:52 +02:00
|
|
|
|
2021-11-10 21:41:23 +01:00
|
|
|
/**
|
|
|
|
* Parent is different than what is specified in the template / hierarchy.
|
|
|
|
* Find it and re-parent there.
|
|
|
|
*
|
|
|
|
* @param {string} parent
|
|
|
|
*/
|
2022-07-21 19:29:43 +02:00
|
|
|
set parentId(parent : string | Element)
|
2021-11-10 21:41:23 +01:00
|
|
|
{
|
2022-09-08 21:14:55 +02:00
|
|
|
this.__parentId = parent;
|
|
|
|
|
|
|
|
this.updateComplete.then(() =>
|
2021-11-10 21:41:23 +01:00
|
|
|
{
|
2022-09-08 21:14:55 +02:00
|
|
|
if(!this.__parentId)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let parent = document.querySelector("#" + this.__parentId) || this.__parentId;
|
|
|
|
if(parent && parent instanceof Element)
|
|
|
|
{
|
|
|
|
parent.append(<Node><unknown>this);
|
|
|
|
this._parent_node = parent;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
get parentId()
|
|
|
|
{
|
|
|
|
return this.__parentId;
|
2021-11-10 21:41:23 +01:00
|
|
|
}
|
|
|
|
|
2021-08-19 01:41:23 +02:00
|
|
|
setParent(new_parent : Et2WidgetClass | et2_widget)
|
2021-08-13 23:26:18 +02:00
|
|
|
{
|
|
|
|
this._parent = new_parent;
|
2021-08-10 23:02:52 +02:00
|
|
|
|
2021-08-19 01:41:23 +02:00
|
|
|
if(this.id)
|
2021-08-13 23:26:18 +02:00
|
|
|
{
|
|
|
|
// Create a namespace for this object
|
2021-08-19 01:41:23 +02:00
|
|
|
if(this._createNamespace())
|
2021-08-13 23:26:18 +02:00
|
|
|
{
|
|
|
|
this.checkCreateNamespace();
|
2021-08-10 23:02:52 +02:00
|
|
|
}
|
2021-08-13 23:26:18 +02:00
|
|
|
}
|
2022-01-05 21:14:28 +01:00
|
|
|
// @ts-ignore
|
2021-09-03 21:08:49 +02:00
|
|
|
this._parent.addChild(this);
|
2021-08-13 23:26:18 +02:00
|
|
|
}
|
2021-08-10 23:02:52 +02:00
|
|
|
|
2021-12-21 18:45:38 +01:00
|
|
|
getParent() : Et2WidgetClass | et2_widget
|
2021-08-13 23:26:18 +02:00
|
|
|
{
|
2021-08-19 01:41:23 +02:00
|
|
|
if(this._parent)
|
2021-08-13 23:26:18 +02:00
|
|
|
{
|
|
|
|
return this._parent;
|
|
|
|
}
|
2021-08-10 23:02:52 +02:00
|
|
|
|
2021-12-21 18:45:38 +01:00
|
|
|
return null;
|
2021-08-13 23:26:18 +02:00
|
|
|
}
|
2021-08-10 23:02:52 +02:00
|
|
|
|
2022-07-22 09:03:54 +02:00
|
|
|
getParentDOMNode() : HTMLElement
|
|
|
|
{
|
|
|
|
return this._parent_node;
|
|
|
|
}
|
|
|
|
|
2021-08-19 01:41:23 +02:00
|
|
|
addChild(child : et2_widget | Et2WidgetClass)
|
2021-08-13 23:26:18 +02:00
|
|
|
{
|
2021-09-03 22:44:52 +02:00
|
|
|
if(this._children.indexOf(child) >= 0)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2021-08-19 01:41:23 +02:00
|
|
|
if(child instanceof et2_widget)
|
2021-08-13 23:26:18 +02:00
|
|
|
{
|
2022-01-05 21:14:28 +01:00
|
|
|
// Type of et2_widget._parent is et2_widget, not Et2Widget. This might cause problems, but they
|
|
|
|
// should be fixed by getting rid of the legacy widget with problems
|
|
|
|
// @ts-ignore
|
2021-08-13 23:26:18 +02:00
|
|
|
child._parent = this;
|
|
|
|
|
|
|
|
// During legacy widget creation, the child's DOM node won't be available yet.
|
|
|
|
this._legacy_children.push(child);
|
2021-08-19 01:41:23 +02:00
|
|
|
let child_node = null;
|
|
|
|
try
|
|
|
|
{
|
2022-01-05 21:14:28 +01:00
|
|
|
//@ts-ignore Technically getDOMNode() is from et2_DOMWidget
|
2021-08-19 01:41:23 +02:00
|
|
|
child_node = typeof child.getDOMNode !== "undefined" ? child.getDOMNode(child) : null;
|
|
|
|
}
|
|
|
|
catch(e)
|
|
|
|
{
|
|
|
|
// Child did not give up its DOM node nicely but errored instead
|
|
|
|
}
|
|
|
|
if(child_node && child_node !== this)
|
2021-08-10 23:02:52 +02:00
|
|
|
{
|
2021-08-13 23:26:18 +02:00
|
|
|
this.append(child_node);
|
2021-08-10 23:02:52 +02:00
|
|
|
}
|
2021-08-13 23:26:18 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
this.append(child);
|
|
|
|
}
|
2021-09-02 21:40:26 +02:00
|
|
|
this._children.push(child);
|
2021-08-13 23:26:18 +02:00
|
|
|
}
|
2021-08-10 23:02:52 +02:00
|
|
|
|
2021-08-13 23:26:18 +02:00
|
|
|
/**
|
2021-09-02 21:40:26 +02:00
|
|
|
* Get child widgets
|
2021-08-13 23:26:18 +02:00
|
|
|
* Use <obj>.children to get web component children
|
|
|
|
* @returns {et2_widget[]}
|
|
|
|
*/
|
|
|
|
getChildren()
|
|
|
|
{
|
2021-09-02 21:40:26 +02:00
|
|
|
return this._children;
|
2021-08-13 23:26:18 +02:00
|
|
|
}
|
2021-08-10 23:02:52 +02:00
|
|
|
|
2021-08-19 01:41:23 +02:00
|
|
|
getType() : string
|
2021-08-13 23:26:18 +02:00
|
|
|
{
|
|
|
|
return this.nodeName;
|
|
|
|
}
|
2021-08-10 23:02:52 +02:00
|
|
|
|
2021-08-19 01:41:23 +02:00
|
|
|
getDOMNode() : HTMLElement
|
2021-08-13 23:26:18 +02:00
|
|
|
{
|
2021-09-15 00:01:22 +02:00
|
|
|
return <HTMLElement><unknown>this;
|
2021-08-13 23:26:18 +02:00
|
|
|
}
|
2021-08-10 23:02:52 +02:00
|
|
|
|
2021-08-19 01:41:23 +02:00
|
|
|
/**
|
|
|
|
* Creates a copy of this widget.
|
|
|
|
*
|
|
|
|
* @param {et2_widget} _parent parent to set for clone, default null
|
|
|
|
*/
|
|
|
|
clone(_parent?) : Et2WidgetClass
|
|
|
|
{
|
|
|
|
// Default _parent to null
|
|
|
|
if(typeof _parent == "undefined")
|
|
|
|
{
|
|
|
|
_parent = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create the copy
|
2022-01-05 21:14:28 +01:00
|
|
|
let copy = <Et2WidgetClass>this.cloneNode();
|
2021-12-16 19:27:28 +01:00
|
|
|
copy.id = this._widget_id;
|
2021-08-19 01:41:23 +02:00
|
|
|
|
2021-09-03 19:20:53 +02:00
|
|
|
if(_parent)
|
2021-08-19 01:41:23 +02:00
|
|
|
{
|
2021-09-03 19:20:53 +02:00
|
|
|
copy.setParent(_parent);
|
2021-08-19 01:41:23 +02:00
|
|
|
}
|
2021-09-03 19:20:53 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
// Copy a reference to the content array manager
|
|
|
|
copy.setArrayMgrs(this.getArrayMgrs());
|
2021-08-19 01:41:23 +02:00
|
|
|
|
2021-09-03 19:20:53 +02:00
|
|
|
// Pass on instance too
|
|
|
|
copy.setInstanceManager(this.getInstanceManager());
|
|
|
|
}
|
|
|
|
|
2021-12-15 19:49:18 +01:00
|
|
|
let widget_class = window.customElements.get(this.localName);
|
|
|
|
let properties = widget_class ? widget_class.properties : [];
|
|
|
|
for(let key in properties)
|
|
|
|
{
|
|
|
|
copy[key] = this[key];
|
|
|
|
}
|
|
|
|
|
2022-03-09 22:23:00 +01:00
|
|
|
// Keep the deferred properties
|
|
|
|
copy._deferred_properties = this._deferred_properties;
|
|
|
|
|
2021-09-03 19:20:53 +02:00
|
|
|
// Create a clone of all child widgets of the given object
|
2022-01-05 21:14:28 +01:00
|
|
|
for(let i = 0; i < this.getChildren().length; i++)
|
2021-09-03 19:20:53 +02:00
|
|
|
{
|
|
|
|
this.getChildren()[i].clone(copy);
|
|
|
|
}
|
2021-08-19 01:41:23 +02:00
|
|
|
|
|
|
|
return copy;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-08-13 23:26:18 +02:00
|
|
|
/**
|
|
|
|
* Sets the array manager for the given part
|
|
|
|
*
|
|
|
|
* @param {string} _part which array mgr to set
|
|
|
|
* @param {object} _mgr
|
|
|
|
*/
|
2021-08-19 01:41:23 +02:00
|
|
|
setArrayMgr(_part : string, _mgr : et2_arrayMgr)
|
2021-08-13 23:26:18 +02:00
|
|
|
{
|
|
|
|
this._mgrs[_part] = _mgr;
|
|
|
|
}
|
2021-08-10 23:02:52 +02:00
|
|
|
|
2021-08-13 23:26:18 +02:00
|
|
|
/**
|
|
|
|
* Returns the array manager object for the given part
|
|
|
|
*
|
|
|
|
* @param {string} managed_array_type name of array mgr to return
|
|
|
|
*/
|
2021-08-19 01:41:23 +02:00
|
|
|
getArrayMgr(managed_array_type : string) : et2_arrayMgr | null
|
2021-08-13 23:26:18 +02:00
|
|
|
{
|
2021-08-19 01:41:23 +02:00
|
|
|
if(this._mgrs && typeof this._mgrs[managed_array_type] != "undefined")
|
2021-08-13 23:26:18 +02:00
|
|
|
{
|
|
|
|
return this._mgrs[managed_array_type];
|
|
|
|
}
|
2021-08-19 01:41:23 +02:00
|
|
|
else if(this.getParent())
|
2021-08-13 23:26:18 +02:00
|
|
|
{
|
|
|
|
return this.getParent().getArrayMgr(managed_array_type);
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
2021-08-10 23:02:52 +02:00
|
|
|
|
2021-08-13 23:26:18 +02:00
|
|
|
/**
|
|
|
|
* Sets all array manager objects - this function can be used to set the
|
|
|
|
* root array managers of the container object.
|
|
|
|
*
|
|
|
|
* @param {object} _mgrs
|
|
|
|
*/
|
|
|
|
setArrayMgrs(_mgrs)
|
|
|
|
{
|
|
|
|
this._mgrs = <et2_arrayMgr[]>et2_cloneObject(_mgrs);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns an associative array containing the top-most array managers.
|
|
|
|
*
|
|
|
|
* @param _mgrs is used internally and should not be supplied.
|
|
|
|
*/
|
2021-08-19 01:41:23 +02:00
|
|
|
getArrayMgrs(_mgrs? : object)
|
2021-08-13 23:26:18 +02:00
|
|
|
{
|
2021-08-19 01:41:23 +02:00
|
|
|
if(typeof _mgrs == "undefined")
|
2021-08-13 23:26:18 +02:00
|
|
|
{
|
|
|
|
_mgrs = {};
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add all managers of this object to the result, if they have not already
|
|
|
|
// been set in the result
|
2022-01-05 21:14:28 +01:00
|
|
|
for(let key in this._mgrs)
|
2021-08-13 23:26:18 +02:00
|
|
|
{
|
2021-08-19 01:41:23 +02:00
|
|
|
if(typeof _mgrs[key] == "undefined")
|
2021-08-10 23:02:52 +02:00
|
|
|
{
|
2021-08-13 23:26:18 +02:00
|
|
|
_mgrs[key] = this._mgrs[key];
|
2021-08-10 23:02:52 +02:00
|
|
|
}
|
2021-08-13 23:26:18 +02:00
|
|
|
}
|
2021-08-10 23:02:52 +02:00
|
|
|
|
2021-08-13 23:26:18 +02:00
|
|
|
// Recursively applies this function to the parent widget
|
2021-08-19 01:41:23 +02:00
|
|
|
if(this._parent)
|
2021-08-13 23:26:18 +02:00
|
|
|
{
|
|
|
|
this._parent.getArrayMgrs(_mgrs);
|
|
|
|
}
|
2021-08-10 23:02:52 +02:00
|
|
|
|
2021-08-13 23:26:18 +02:00
|
|
|
return _mgrs;
|
|
|
|
}
|
2021-08-10 23:02:52 +02:00
|
|
|
|
2021-08-13 23:26:18 +02:00
|
|
|
/**
|
|
|
|
* Checks whether a namespace exists for this element in the content array.
|
|
|
|
* If yes, an own perspective of the content array is created. If not, the
|
|
|
|
* parent content manager is used.
|
|
|
|
*
|
|
|
|
* Constructor attributes are passed in case a child needs to make decisions
|
|
|
|
*/
|
|
|
|
checkCreateNamespace()
|
|
|
|
{
|
|
|
|
// Get the content manager
|
2022-01-05 21:14:28 +01:00
|
|
|
let mgrs = this.getArrayMgrs();
|
2021-08-10 23:02:52 +02:00
|
|
|
|
2022-01-05 21:14:28 +01:00
|
|
|
for(let key in mgrs)
|
2021-08-13 23:26:18 +02:00
|
|
|
{
|
2022-01-05 21:14:28 +01:00
|
|
|
let mgr = mgrs[key];
|
2021-08-10 23:02:52 +02:00
|
|
|
|
2021-08-13 23:26:18 +02:00
|
|
|
// Get the original content manager if we have already created a
|
|
|
|
// perspective for this node
|
2021-08-19 01:41:23 +02:00
|
|
|
if(typeof this._mgrs[key] != "undefined" && mgr.perspectiveData.owner == this)
|
2021-08-10 23:02:52 +02:00
|
|
|
{
|
2022-06-22 23:47:28 +02:00
|
|
|
mgr = mgr.getParentMgr();
|
2021-08-10 23:02:52 +02:00
|
|
|
}
|
|
|
|
|
2021-08-13 23:26:18 +02:00
|
|
|
// Check whether the manager has a namespace for the id of this object
|
2022-01-05 21:14:28 +01:00
|
|
|
let entry = mgr.getEntry(this.id);
|
2021-08-19 01:41:23 +02:00
|
|
|
if(typeof entry === 'object' && entry !== null || this.id)
|
2021-08-10 23:02:52 +02:00
|
|
|
{
|
2021-08-13 23:26:18 +02:00
|
|
|
// The content manager has an own node for this object, so
|
|
|
|
// create an own perspective.
|
|
|
|
this._mgrs[key] = mgr.openPerspective(this, this.id);
|
2021-08-10 23:02:52 +02:00
|
|
|
}
|
2021-08-13 23:26:18 +02:00
|
|
|
else
|
2021-08-10 23:02:52 +02:00
|
|
|
{
|
2021-08-13 23:26:18 +02:00
|
|
|
// The current content manager does not have an own namespace for
|
|
|
|
// this element, so use the content manager of the parent.
|
|
|
|
delete (this._mgrs[key]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-08-10 23:02:52 +02:00
|
|
|
|
2021-09-03 19:20:53 +02:00
|
|
|
/**
|
|
|
|
* Set the instance manager
|
|
|
|
* Normally this is not needed as it's set on the top-level container, and we just return that reference
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
setInstanceManager(manager : etemplate2)
|
|
|
|
{
|
|
|
|
this._inst = manager;
|
|
|
|
}
|
|
|
|
|
2021-08-13 23:26:18 +02:00
|
|
|
/**
|
|
|
|
* Returns the instance manager
|
|
|
|
*
|
|
|
|
* @return {etemplate2}
|
|
|
|
*/
|
|
|
|
getInstanceManager()
|
|
|
|
{
|
2021-08-19 01:41:23 +02:00
|
|
|
if(this._inst != null)
|
2021-08-13 23:26:18 +02:00
|
|
|
{
|
|
|
|
return this._inst;
|
|
|
|
}
|
2021-08-19 01:41:23 +02:00
|
|
|
else if(this.getParent())
|
2021-08-13 23:26:18 +02:00
|
|
|
{
|
2021-12-16 19:27:28 +01:00
|
|
|
return this.getParent().getInstanceManager ? this.getParent().getInstanceManager() : null;
|
2021-08-13 23:26:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
2021-08-10 23:02:52 +02:00
|
|
|
|
2021-08-20 18:23:39 +02:00
|
|
|
/**
|
|
|
|
* Returns the base widget
|
|
|
|
* Usually this is the same as getInstanceManager().widgetContainer
|
|
|
|
*/
|
|
|
|
getRoot() : et2_container
|
|
|
|
{
|
|
|
|
if(this.getParent() != null)
|
|
|
|
{
|
|
|
|
return this.getParent().getRoot();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return <et2_container><unknown>this;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-13 23:26:18 +02:00
|
|
|
/**
|
|
|
|
* Returns the path into the data array. By default, array manager takes care of
|
|
|
|
* this, but some extensions need to override this
|
|
|
|
*/
|
|
|
|
getPath()
|
|
|
|
{
|
2022-01-05 21:14:28 +01:00
|
|
|
let path = this.getArrayMgr("content")?.getPath() ?? [];
|
2021-08-10 23:02:52 +02:00
|
|
|
|
2021-08-13 23:26:18 +02:00
|
|
|
// Prevent namespaced widgets with value from going an extra layer deep
|
2021-12-09 18:54:37 +01:00
|
|
|
if(this.id && this._createNamespace() && path[path.length - 1] == this.id)
|
|
|
|
{
|
|
|
|
path.pop();
|
|
|
|
}
|
2021-08-10 23:02:52 +02:00
|
|
|
|
2021-08-13 23:26:18 +02:00
|
|
|
return path;
|
|
|
|
}
|
2021-08-10 23:02:52 +02:00
|
|
|
|
2021-08-19 01:41:23 +02:00
|
|
|
_createNamespace() : boolean
|
2021-08-13 23:26:18 +02:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2021-08-10 23:02:52 +02:00
|
|
|
|
2021-08-19 01:41:23 +02:00
|
|
|
egw() : IegwAppLocal
|
2021-08-13 23:26:18 +02:00
|
|
|
{
|
2022-01-07 22:13:38 +01:00
|
|
|
if(this.getParent() != null && typeof this.getParent().egw === "function")
|
2021-08-13 23:26:18 +02:00
|
|
|
{
|
|
|
|
return (<et2_widget>this.getParent()).egw();
|
|
|
|
}
|
|
|
|
// Get the window this object belongs to
|
2022-01-05 21:14:28 +01:00
|
|
|
let wnd = null;
|
2021-08-13 23:26:18 +02:00
|
|
|
// @ts-ignore Technically this doesn't have implements(), but it's mixed in
|
2021-08-19 01:41:23 +02:00
|
|
|
if(this.implements(et2_IDOMNode))
|
2021-08-13 23:26:18 +02:00
|
|
|
{
|
2022-01-05 21:14:28 +01:00
|
|
|
let node = (<et2_IDOMNode><unknown>this).getDOMNode();
|
2021-08-19 01:41:23 +02:00
|
|
|
if(node && node.ownerDocument)
|
2021-08-13 23:26:18 +02:00
|
|
|
{
|
|
|
|
wnd = node.ownerDocument.parentNode || node.ownerDocument.defaultView;
|
2021-08-10 23:02:52 +02:00
|
|
|
}
|
2021-08-13 23:26:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// If we're the root object, return the phpgwapi API instance
|
2023-01-24 01:33:22 +01:00
|
|
|
return typeof egw === "function" ? egw('phpgwapi', wnd) : (window['egw'] ? window['egw'] : null);
|
2021-08-13 23:26:18 +02:00
|
|
|
}
|
2022-01-05 21:14:28 +01:00
|
|
|
}
|
2021-08-10 23:02:52 +02:00
|
|
|
|
2021-08-13 23:26:18 +02:00
|
|
|
// Add some more stuff in
|
|
|
|
applyMixins(Et2WidgetClass, [ClassWithInterfaces]);
|
2021-08-10 23:02:52 +02:00
|
|
|
|
2022-06-10 18:25:31 +02:00
|
|
|
return Et2WidgetClass as unknown as Constructor<Et2WidgetClass> & T;
|
2021-08-19 18:54:32 +02:00
|
|
|
}
|
2021-08-27 19:21:40 +02:00
|
|
|
export const Et2Widget = dedupeMixin(Et2WidgetMixin);
|
2021-08-19 18:54:32 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Load a Web Component
|
|
|
|
* @param _nodeName
|
|
|
|
* @param _template_node
|
2022-01-05 21:14:28 +01:00
|
|
|
* @param parent Parent widget
|
2021-08-19 18:54:32 +02:00
|
|
|
*/
|
2022-01-05 21:14:28 +01:00
|
|
|
// @ts-ignore Et2Widget is I guess not the right type
|
2022-05-13 15:32:36 +02:00
|
|
|
export function loadWebComponent(_nodeName : string, _template_node : Element|{[index: string]: any}, parent : Et2Widget|et2_widget|undefined) : HTMLElement
|
2021-08-19 18:54:32 +02:00
|
|
|
{
|
2022-05-03 19:01:42 +02:00
|
|
|
let attrs = {};
|
|
|
|
let load_children = true;
|
|
|
|
|
2022-04-29 17:05:43 +02:00
|
|
|
// support attributes object instead of an Element
|
2022-05-03 19:01:42 +02:00
|
|
|
if(typeof _template_node.getAttribute === 'undefined')
|
|
|
|
{
|
|
|
|
attrs = _template_node;
|
|
|
|
load_children = false;
|
|
|
|
}
|
|
|
|
else
|
2022-04-29 17:05:43 +02:00
|
|
|
{
|
2022-05-03 19:01:42 +02:00
|
|
|
_template_node.getAttributeNames().forEach(attribute =>
|
|
|
|
{
|
|
|
|
attrs[attribute] = _template_node.getAttribute(attribute);
|
|
|
|
});
|
2022-04-29 17:05:43 +02:00
|
|
|
}
|
2022-05-03 19:01:42 +02:00
|
|
|
|
2022-01-11 23:16:50 +01:00
|
|
|
// Try to find the class for the given node
|
2023-02-21 18:31:38 +01:00
|
|
|
let mobile = (typeof egwIsMobile != "undefined" && egwIsMobile());
|
|
|
|
if(mobile && typeof window.customElements.get(_nodeName + "_mobile") != "undefined")
|
|
|
|
{
|
|
|
|
_nodeName += "_mobile";
|
|
|
|
}
|
|
|
|
|
2022-01-11 23:16:50 +01:00
|
|
|
let widget_class = window.customElements.get(_nodeName);
|
|
|
|
if(!widget_class)
|
|
|
|
{
|
|
|
|
// Given node has no registered class. Try some of our special things (remove type, fallback to actual node)
|
2022-05-13 11:59:13 +02:00
|
|
|
let tries = [_nodeName.split('-')[0]];
|
2023-02-21 18:31:38 +01:00
|
|
|
if(_template_node.nodeName)
|
|
|
|
{
|
|
|
|
tries = tries.concat(_template_node.nodeName.toLowerCase());
|
|
|
|
}
|
2022-01-11 23:16:50 +01:00
|
|
|
for(let i = 0; i < tries.length && !window.customElements.get(_nodeName); i++)
|
|
|
|
{
|
|
|
|
_nodeName = tries[i];
|
|
|
|
}
|
|
|
|
widget_class = window.customElements.get(_nodeName);
|
|
|
|
if(!widget_class)
|
|
|
|
{
|
2023-02-21 18:31:38 +01:00
|
|
|
debugger;
|
2022-01-11 23:16:50 +01:00
|
|
|
throw Error("Unknown or unregistered WebComponent '" + _nodeName + "', could not find class. Also checked for " + tries.join(','));
|
|
|
|
}
|
|
|
|
}
|
2022-05-13 15:32:36 +02:00
|
|
|
const readonly = parent?.getArrayMgr("readonlys") ?
|
2022-01-11 23:16:50 +01:00
|
|
|
(<any>parent.getArrayMgr("readonlys")).isReadOnly(
|
2022-05-03 19:01:42 +02:00
|
|
|
attrs["id"], attrs["readonly"],
|
2023-02-14 18:09:58 +01:00
|
|
|
typeof parent?.readonly !== "undefined" ? parent.readonly : parent.options?.readonly || false) : false;
|
2022-01-11 23:16:50 +01:00
|
|
|
if(readonly === true && typeof window.customElements.get(_nodeName + "_ro") != "undefined")
|
|
|
|
{
|
|
|
|
_nodeName += "_ro";
|
|
|
|
}
|
|
|
|
|
2021-08-27 19:21:40 +02:00
|
|
|
// @ts-ignore
|
|
|
|
let widget = <Et2Widget>document.createElement(_nodeName);
|
2021-08-19 18:54:32 +02:00
|
|
|
|
2022-07-19 15:17:16 +02:00
|
|
|
if (parent && typeof widget.setParent === 'function') widget.setParent(parent);
|
2021-08-19 18:54:32 +02:00
|
|
|
|
|
|
|
// Set read-only. Doesn't really matter if it's a ro widget, but otherwise it needs set
|
2022-06-16 21:59:31 +02:00
|
|
|
widget.readonly = readonly;
|
2021-08-19 18:54:32 +02:00
|
|
|
|
2022-08-22 18:36:58 +02:00
|
|
|
delete attrs.readonly;
|
2021-12-15 00:55:02 +01:00
|
|
|
widget.transformAttributes(attrs);
|
|
|
|
|
|
|
|
// Children need to be loaded
|
2022-05-03 19:01:42 +02:00
|
|
|
if(load_children)
|
|
|
|
{
|
|
|
|
widget.loadFromXML(_template_node);
|
|
|
|
}
|
2021-12-15 00:55:02 +01:00
|
|
|
|
|
|
|
return widget;
|
|
|
|
}
|
|
|
|
|
2022-02-24 23:52:45 +01:00
|
|
|
/**
|
|
|
|
* Take attributes from a node in a .xet file and apply those to a WebComponent widget
|
|
|
|
*
|
|
|
|
* Any attributes provided that match a property (or attribute) on the widget will be adjusted according to
|
|
|
|
* the passed arrayManager, coerced into the proper type, and set.
|
|
|
|
* It is here that we find values or set attributes that should come from content.
|
|
|
|
*
|
|
|
|
* @param widget
|
|
|
|
* @param {et2_arrayMgr} mgr
|
|
|
|
* @param attributes
|
|
|
|
*/
|
2021-12-15 00:55:02 +01:00
|
|
|
function transformAttributes(widget, mgr : et2_arrayMgr, attributes)
|
|
|
|
{
|
|
|
|
const widget_class = window.customElements.get(widget.localName);
|
|
|
|
|
2022-07-20 00:20:49 +02:00
|
|
|
|
|
|
|
// Special case attributes
|
2022-08-15 18:35:12 +02:00
|
|
|
if(attributes.attributes)
|
|
|
|
{
|
|
|
|
// Attributes in content? "attributes" is read-only in webComponent
|
|
|
|
let mgr_attributes = mgr.getEntry(attributes.attributes);
|
|
|
|
delete attributes.attributes;
|
|
|
|
if(mgr_attributes)
|
|
|
|
{
|
|
|
|
Object.assign(attributes, ...mgr_attributes);
|
|
|
|
}
|
|
|
|
}
|
2022-07-20 00:20:49 +02:00
|
|
|
if(attributes.width)
|
|
|
|
{
|
|
|
|
widget.style.setProperty("width", attributes.width);
|
|
|
|
widget.style.setProperty("flex", "0 0 auto");
|
|
|
|
delete attributes.width;
|
|
|
|
}
|
|
|
|
|
2021-12-15 00:55:02 +01:00
|
|
|
// Apply any set attributes - widget will do its own coercion
|
|
|
|
for(let attribute in attributes)
|
|
|
|
{
|
|
|
|
let attrValue = attributes[attribute];
|
2021-08-19 18:54:32 +02:00
|
|
|
|
2022-03-05 14:22:45 +01:00
|
|
|
// If there is no attribute set, ignore it. Widget sets its own default.
|
2021-12-09 18:54:37 +01:00
|
|
|
if(typeof attrValue === "undefined")
|
|
|
|
{
|
2022-02-25 18:21:16 +01:00
|
|
|
continue;
|
2021-12-09 18:54:37 +01:00
|
|
|
}
|
2022-02-25 18:21:16 +01:00
|
|
|
|
2022-07-22 11:08:55 +02:00
|
|
|
// preprocessor and transformer can't know if application widget is a web-component or a legacy one
|
|
|
|
// translate attribute names to camelCase (only do it for used underscore, to not require a regexp)
|
|
|
|
if (attribute !== 'select_options' && attribute.indexOf('_') !== -1)
|
|
|
|
{
|
|
|
|
let parts = attribute.split('_');
|
|
|
|
if (attribute === 'parent_node') parts[1] = 'Id';
|
2022-07-26 00:12:09 +02:00
|
|
|
attribute = parts.shift() + parts.map(part => part[0].toUpperCase() + part.substring(1)).join("");
|
2022-07-22 11:08:55 +02:00
|
|
|
}
|
|
|
|
|
2021-08-20 18:23:39 +02:00
|
|
|
const property = widget_class.getPropertyOptions(attribute);
|
|
|
|
|
2021-12-20 23:15:32 +01:00
|
|
|
switch(typeof property === "object" ? property.type : property)
|
2021-08-20 18:23:39 +02:00
|
|
|
{
|
|
|
|
case Boolean:
|
2022-04-20 22:09:01 +02:00
|
|
|
if(typeof attrValue == "boolean")
|
|
|
|
{
|
|
|
|
// Already boolean, nothing needed
|
|
|
|
break;
|
|
|
|
}
|
2021-08-20 18:23:39 +02:00
|
|
|
// If the attribute is marked as boolean, parse the
|
|
|
|
// expression as bool expression.
|
2021-12-15 00:55:02 +01:00
|
|
|
attrValue = mgr ? mgr.parseBoolExpression(attrValue) : attrValue;
|
2022-03-09 22:23:00 +01:00
|
|
|
if(typeof attrValue === "string")
|
|
|
|
{
|
|
|
|
// Parse decided we still needed a string ($row most likely) so we'll defer it until later
|
|
|
|
// Repeating rows & nextmatch will parse it again when doing the row
|
|
|
|
widget.deferredProperties[attribute] = attrValue;
|
|
|
|
// Leave the current value at whatever the default is
|
|
|
|
continue;
|
|
|
|
}
|
2021-08-20 18:23:39 +02:00
|
|
|
break;
|
|
|
|
case Function:
|
2023-01-04 20:00:25 +01:00
|
|
|
if(typeof attrValue == "string" && mgr && mgr.getPerspectiveData().row == null &&
|
2022-08-17 21:23:55 +02:00
|
|
|
(attrValue.indexOf("$row") > -1 || attrValue.indexOf("$row_cont") > -1)
|
|
|
|
)
|
|
|
|
{
|
|
|
|
// Need row context, defer it until later
|
|
|
|
// Repeating rows & nextmatch will parse it again when doing the row
|
|
|
|
widget.deferredProperties[attribute] = attrValue;
|
|
|
|
console.log("Had to defer %s parsing for %o\nCan it be rewritten to avoid $row & $row_cont?", attribute, widget);
|
|
|
|
break;
|
|
|
|
}
|
2021-08-20 18:23:39 +02:00
|
|
|
// We parse it into a function here so we can pass in the widget as context.
|
|
|
|
// Leaving it to the LitElement conversion loses the widget as context
|
|
|
|
if(typeof attrValue !== "function")
|
|
|
|
{
|
|
|
|
attrValue = et2_compileLegacyJS(attrValue, widget, widget);
|
|
|
|
}
|
|
|
|
break;
|
2022-07-14 19:34:37 +02:00
|
|
|
case Object:
|
2022-07-26 19:41:01 +02:00
|
|
|
case Array:
|
2022-08-25 21:25:45 +02:00
|
|
|
// Leave it alone if it's not a string
|
|
|
|
if(typeof attrValue !== "string")
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
// fall through to look in content
|
2021-08-20 18:23:39 +02:00
|
|
|
default:
|
2022-04-20 22:09:01 +02:00
|
|
|
attrValue = mgr ? mgr.expandName("" + attrValue) : attrValue;
|
2022-07-26 15:46:42 +02:00
|
|
|
if(attrValue && typeof attrValue == "string" && widget_class.translate[attribute])
|
2021-09-16 22:56:13 +02:00
|
|
|
{
|
2021-12-09 19:32:31 +01:00
|
|
|
// allow attribute to contain multiple translated sub-strings eg: {Firstname}.{Lastname}
|
|
|
|
if(attrValue.indexOf('{') !== -1)
|
|
|
|
{
|
2022-07-26 15:46:42 +02:00
|
|
|
attrValue = attrValue.replace(/{([^}]+)}/g, (str, p1) =>
|
2021-12-09 19:32:31 +01:00
|
|
|
{
|
2022-07-26 15:46:42 +02:00
|
|
|
return widget.egw().lang(p1);
|
|
|
|
});
|
2021-12-09 19:32:31 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
attrValue = widget.egw().lang(attrValue);
|
|
|
|
}
|
2021-09-16 22:56:13 +02:00
|
|
|
}
|
2022-09-08 19:17:34 +02:00
|
|
|
else if(attrValue && [Object, Array].indexOf(typeof property === "object" ? property.type : property) != -1)
|
|
|
|
{
|
|
|
|
// Value was not supposed to be a string, but was run through here for expandName
|
2022-09-09 16:34:41 +02:00
|
|
|
try
|
|
|
|
{
|
|
|
|
attrValue = JSON.parse(attrValue);
|
|
|
|
}
|
|
|
|
catch(e)
|
|
|
|
{
|
|
|
|
console.info(widget_class.name + "#" + widget.id + " attribute '" + attribute + "' has type " +
|
|
|
|
(typeof property === "object" ? property.type.name : property.name) + " but value %o could not be parsed", attrValue);
|
|
|
|
}
|
2022-09-08 19:17:34 +02:00
|
|
|
}
|
2021-08-20 18:23:39 +02:00
|
|
|
break;
|
|
|
|
}
|
2021-08-19 18:54:32 +02:00
|
|
|
|
2022-06-23 19:00:47 +02:00
|
|
|
// Bind handlers directly, since we can do that now. Event handlers still need to be defined
|
|
|
|
// in properties() as {type: Function}, but this will take care of the binding. This is
|
|
|
|
// separate from internal events.
|
|
|
|
// (handlers can only be bound _after_ the widget is added to the DOM
|
2022-03-02 00:56:58 +01:00
|
|
|
if(attribute.startsWith("on") && typeof attrValue == "function")
|
|
|
|
{
|
2022-06-23 19:00:47 +02:00
|
|
|
//widget.updateComplete.then(() => addEventListener(attribute, attrValue));
|
2022-03-02 00:56:58 +01:00
|
|
|
}
|
2022-06-23 19:00:47 +02:00
|
|
|
|
2022-01-12 19:41:13 +01:00
|
|
|
// Set as attribute or property, as appropriate. Don't set missing attributes.
|
2022-01-11 23:16:50 +01:00
|
|
|
if(widget.getAttributeNames().indexOf(attribute) >= 0 || property.reflect && attrValue)
|
2021-08-19 18:54:32 +02:00
|
|
|
{
|
2021-08-20 18:23:39 +02:00
|
|
|
// Set as attribute (reflected in DOM)
|
2022-06-22 22:25:31 +02:00
|
|
|
widget.setAttribute(attribute, attrValue === true ? "" : attrValue);
|
2021-08-19 18:54:32 +02:00
|
|
|
}
|
2022-06-22 22:25:31 +02:00
|
|
|
else if(attribute === 'options')
|
2022-03-10 13:57:00 +01:00
|
|
|
{
|
2022-06-22 22:25:31 +02:00
|
|
|
console.trace('Ignored setting depricated "options" attribute for widget #' + widget.id, widget);
|
|
|
|
continue;
|
2021-08-19 18:54:32 +02:00
|
|
|
}
|
2022-06-22 22:25:31 +02:00
|
|
|
// Set as property
|
|
|
|
widget[attribute] = attrValue;
|
2021-12-15 00:55:02 +01:00
|
|
|
}
|
2021-08-19 18:54:32 +02:00
|
|
|
|
|
|
|
if(widget_class.getPropertyOptions("value") && widget.set_value)
|
|
|
|
{
|
|
|
|
if(mgr != null)
|
|
|
|
{
|
|
|
|
let val = mgr.getEntry(widget.id, false, true);
|
|
|
|
if(val !== null)
|
|
|
|
{
|
|
|
|
widget.set_value(val);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-12-17 22:47:19 +01:00
|
|
|
}
|
|
|
|
|
2022-01-05 18:21:18 +01:00
|
|
|
/**
|
|
|
|
* Take the name of one of our images, find the full URL (including theme), and wrap it up so you can use it in a
|
|
|
|
* widget's css block.
|
|
|
|
*
|
|
|
|
* @example
|
|
|
|
* import {cssImage} from Et2Widget;
|
|
|
|
* ...
|
|
|
|
* static get styles()
|
|
|
|
* {
|
|
|
|
* return [
|
|
|
|
* ...super.styles,
|
|
|
|
* css`
|
|
|
|
* :host {
|
2022-01-05 18:24:23 +01:00
|
|
|
* background-image: ${cssImage("save")};
|
2022-01-05 18:21:18 +01:00
|
|
|
* }
|
|
|
|
* `];
|
|
|
|
* }
|
2022-01-05 21:14:28 +01:00
|
|
|
* @param image_name Name of the image
|
|
|
|
* @param app_name Optional, image is from an app instead of api
|
2022-01-05 18:21:18 +01:00
|
|
|
* @returns {CSSResult}
|
|
|
|
*/
|
|
|
|
export function cssImage(image_name : string, app_name? : string)
|
|
|
|
{
|
2022-01-07 22:13:38 +01:00
|
|
|
let url = egw?.image(image_name, app_name);
|
2022-01-05 18:21:18 +01:00
|
|
|
if(url)
|
|
|
|
{
|
|
|
|
return css`url(${unsafeCSS(url)})`;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return css``;
|
|
|
|
}
|
|
|
|
}
|