2020-01-21 10:12:39 +01:00
|
|
|
/**
|
|
|
|
* EGroupware eTemplate2 - JS code for implementing inheritance with attributes
|
|
|
|
*
|
|
|
|
* @license http://opensource.org/licenses/gpl-license.php GPL - GNU General Public License
|
|
|
|
* @package etemplate
|
|
|
|
* @subpackage api
|
|
|
|
* @link: https://www.egroupware.org
|
|
|
|
* @author Andreas Stöckel
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*egw:uses
|
|
|
|
et2_core_common;
|
|
|
|
*/
|
|
|
|
|
2021-09-03 11:28:22 +02:00
|
|
|
import {egw} from "../jsapi/egw_global";
|
|
|
|
import {et2_checkType, et2_no_init, et2_validateAttrib} from "./et2_core_common";
|
|
|
|
import {et2_implements_registry} from "./et2_core_interfaces";
|
2020-01-21 10:12:39 +01:00
|
|
|
|
2021-09-03 11:28:22 +02:00
|
|
|
// Needed for mixin
|
|
|
|
export function mix (superclass)
|
|
|
|
{
|
|
|
|
return new MixinBuilder(superclass);
|
|
|
|
}
|
|
|
|
export class MixinBuilder {
|
|
|
|
constructor(superclass) {
|
|
|
|
this.superclass = superclass;
|
|
|
|
}
|
|
|
|
|
|
|
|
with(...mixins) {
|
|
|
|
return mixins.reduce(this.applyMixins, this.superclass);
|
|
|
|
}
|
|
|
|
applyMixins(derivedConstructor: any, baseConstructor: any) {
|
|
|
|
Object.getOwnPropertyNames(baseConstructor.prototype)
|
|
|
|
.forEach(name => {
|
|
|
|
Object.defineProperty(derivedConstructor.prototype,
|
|
|
|
name,
|
|
|
|
Object.
|
|
|
|
getOwnPropertyDescriptor(
|
|
|
|
baseConstructor.prototype,
|
|
|
|
name
|
|
|
|
)
|
|
|
|
);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
copyProperties(target, source) {
|
|
|
|
for (let key of Reflect.ownKeys(source)) {
|
|
|
|
if (key !== "constructor" && key !== "prototype" && key !== "name") {
|
|
|
|
let desc = Object.getOwnPropertyDescriptor(source, key);
|
|
|
|
Object.defineProperty(target, key, desc);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// This one from Typescript docs
|
|
|
|
export function applyMixins(derivedCtor: any, constructors: any[]) {
|
|
|
|
constructors.forEach((baseCtor) => {
|
|
|
|
Object.getOwnPropertyNames(baseCtor.prototype).forEach((name) => {
|
|
|
|
Object.defineProperty(
|
|
|
|
derivedCtor.prototype,
|
|
|
|
name,
|
|
|
|
Object.getOwnPropertyDescriptor(baseCtor.prototype, name) ||
|
|
|
|
Object.create(null)
|
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
Experiments in using mixins to combine et2_widget & LitElement
|
|
|
|
Note that this "works", in that it mixes the code properly.
|
|
|
|
It does not work in that the resulting class does not work with et2's inheritance & class checking stuff
|
|
|
|
|
|
|
|
// This one to make TypeScript happy?
|
|
|
|
interface et2_textbox extends et2_textbox, LitElement {}
|
|
|
|
// This one to make the inheritance magic happen
|
|
|
|
applyMixins(et2_textbox, [et2_textbox,LitElement]);
|
|
|
|
// Make it a real WebComponent
|
|
|
|
customElements.define("et2-textbox",et2_textbox);
|
|
|
|
|
|
|
|
*/
|
2021-06-14 11:47:02 +02:00
|
|
|
export class ClassWithInterfaces
|
|
|
|
{
|
2021-09-03 11:28:22 +02:00
|
|
|
/**
|
|
|
|
* The implements function can be used to check whether the object
|
|
|
|
* implements the given interface.
|
|
|
|
*
|
|
|
|
* As TypeScript can not (yet) check if an objects implements an interface on runtime,
|
|
|
|
* we currently implements with each interface a function called 'implements_'+interfacename
|
|
|
|
* to be able to check here.
|
|
|
|
*
|
|
|
|
* @param _iface name of interface to check
|
|
|
|
*/
|
|
|
|
implements (_iface_name : string)
|
|
|
|
{
|
|
|
|
if (typeof et2_implements_registry[_iface_name] === 'function' &&
|
|
|
|
et2_implements_registry[_iface_name](this))
|
2021-06-14 11:47:02 +02:00
|
|
|
{
|
2021-09-03 11:28:22 +02:00
|
|
|
return true
|
2021-06-14 11:47:02 +02:00
|
|
|
}
|
2021-09-03 11:28:22 +02:00
|
|
|
return false;
|
|
|
|
}
|
2021-06-14 11:47:02 +02:00
|
|
|
|
2021-09-03 11:28:22 +02:00
|
|
|
/**
|
|
|
|
* Check if object is an instance of a class or implements an interface (specified by the interfaces name)
|
|
|
|
*
|
|
|
|
* @param _class_or_interfacename class(-name) or string with name of interface
|
|
|
|
*/
|
|
|
|
instanceOf(_class_or_interfacename: any) : boolean
|
|
|
|
{
|
|
|
|
if (typeof _class_or_interfacename === 'string')
|
2021-06-14 11:47:02 +02:00
|
|
|
{
|
2021-09-03 11:28:22 +02:00
|
|
|
return this.implements(_class_or_interfacename);
|
2021-06-14 11:47:02 +02:00
|
|
|
}
|
2021-09-03 11:28:22 +02:00
|
|
|
return this instanceof _class_or_interfacename;
|
|
|
|
}
|
2021-06-14 11:47:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export class ClassWithAttributes extends ClassWithInterfaces
|
2020-01-21 10:12:39 +01:00
|
|
|
{
|
2021-09-03 11:28:22 +02:00
|
|
|
/**
|
|
|
|
* Object to collect the attributes we operate on
|
|
|
|
*/
|
|
|
|
attributes: object;
|
2020-01-21 10:12:39 +01:00
|
|
|
|
2021-09-03 11:28:22 +02:00
|
|
|
/**
|
|
|
|
* Returns the value of the given attribute. If the property does not
|
|
|
|
* exist, an error message is issued.
|
|
|
|
*
|
|
|
|
* @param {string} _name
|
|
|
|
* @return {*}
|
|
|
|
*/
|
|
|
|
getAttribute(_name)
|
|
|
|
{
|
|
|
|
if (typeof this.attributes[_name] != "undefined" &&
|
|
|
|
!this.attributes[_name].ignore) {
|
|
|
|
if (typeof this["get_" + _name] == "function") {
|
|
|
|
return this["get_" + _name]();
|
|
|
|
} else {
|
|
|
|
return this[_name];
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
egw.debug("error", this, "Attribute '" + _name + "' does not exist!");
|
2020-01-21 10:12:39 +01:00
|
|
|
}
|
2021-09-03 11:28:22 +02:00
|
|
|
}
|
2020-01-21 10:12:39 +01:00
|
|
|
|
2021-09-03 11:28:22 +02:00
|
|
|
/**
|
|
|
|
* The setAttribute function sets the attribute with the given name to
|
|
|
|
* the given value. _override defines, whether this[_name] will be set,
|
|
|
|
* if this key already exists. _override defaults to true. A warning
|
|
|
|
* is issued if the attribute does not exist.
|
|
|
|
*
|
|
|
|
* @param {string} _name
|
|
|
|
* @param {*} _value
|
|
|
|
* @param {boolean} _override
|
|
|
|
*/
|
|
|
|
setAttribute(_name, _value, _override)
|
|
|
|
{
|
|
|
|
if (typeof this.attributes[_name] != "undefined") {
|
|
|
|
if (!this.attributes[_name].ignore) {
|
|
|
|
if (typeof _override == "undefined") {
|
|
|
|
_override = true;
|
|
|
|
}
|
2020-01-21 10:12:39 +01:00
|
|
|
|
2021-09-03 11:28:22 +02:00
|
|
|
var val = et2_checkType(_value, this.attributes[_name].type,
|
|
|
|
_name, this);
|
2020-01-21 10:12:39 +01:00
|
|
|
|
2021-09-03 11:28:22 +02:00
|
|
|
if (typeof this["set_" + _name] == "function") {
|
|
|
|
this["set_" + _name](val);
|
|
|
|
} else if (_override || typeof this[_name] == "undefined") {
|
|
|
|
this[_name] = val;
|
2020-01-21 10:12:39 +01:00
|
|
|
}
|
2021-09-03 11:28:22 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
egw.debug("warn", this, "Attribute '" + _name + "' does not exist!");
|
2020-01-21 10:12:39 +01:00
|
|
|
}
|
2021-09-03 11:28:22 +02:00
|
|
|
}
|
2020-01-21 10:12:39 +01:00
|
|
|
|
2021-09-03 11:28:22 +02:00
|
|
|
/**
|
|
|
|
* generateAttributeSet sanitizes the given associative array of attributes
|
|
|
|
* (by passing each entry to "et2_checkType" and checking for existance of
|
|
|
|
* the attribute) and adds the default values to the associative array.
|
|
|
|
*
|
|
|
|
* @param {object} _attrs is the associative array containing the attributes.
|
|
|
|
*/
|
|
|
|
static generateAttributeSet(widget, _attrs)
|
|
|
|
{
|
|
|
|
// Sanity check and validation
|
|
|
|
for (var key in _attrs) {
|
|
|
|
if (typeof widget[key] != "undefined") {
|
|
|
|
if (!widget[key].ignore) {
|
|
|
|
_attrs[key] = et2_checkType(_attrs[key], widget[key].type,
|
|
|
|
key, this);
|
2020-01-21 10:12:39 +01:00
|
|
|
}
|
2021-09-03 11:28:22 +02:00
|
|
|
} else {
|
|
|
|
// Key does not exist - delete it and issue a warning
|
|
|
|
delete (_attrs[key]);
|
|
|
|
egw.debug("warn", this, "Attribute '" + key +
|
|
|
|
"' does not exist in " + _attrs.type + "!");
|
|
|
|
}
|
|
|
|
}
|
2020-01-21 10:12:39 +01:00
|
|
|
|
2021-09-03 11:28:22 +02:00
|
|
|
// Include default values or already set values for this attribute
|
|
|
|
for (var key in widget) {
|
|
|
|
if (typeof _attrs[key] == "undefined") {
|
|
|
|
var _default = widget[key]["default"];
|
|
|
|
if (_default == et2_no_init) {
|
|
|
|
_default = undefined;
|
2020-01-21 10:12:39 +01:00
|
|
|
}
|
|
|
|
|
2021-09-03 11:28:22 +02:00
|
|
|
_attrs[key] = _default;
|
|
|
|
}
|
2020-01-21 10:12:39 +01:00
|
|
|
}
|
|
|
|
|
2021-09-03 11:28:22 +02:00
|
|
|
return _attrs;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The initAttributes function sets the attributes to their default
|
|
|
|
* values. The attributes are not overwritten, which means, that the
|
|
|
|
* default is only set, if either a setter exists or this[propName] does
|
|
|
|
* not exist yet.
|
|
|
|
*
|
|
|
|
* @param {object} _attrs is the associative array containing the attributes.
|
|
|
|
*/
|
|
|
|
initAttributes(_attrs)
|
|
|
|
{
|
|
|
|
for (var key in _attrs) {
|
|
|
|
if (typeof this.attributes[key] != "undefined" && !this.attributes[key].ignore && !(_attrs[key] == undefined)) {
|
|
|
|
this.setAttribute(key, _attrs[key], false);
|
|
|
|
}
|
2020-01-21 10:12:39 +01:00
|
|
|
}
|
2021-09-03 11:28:22 +02:00
|
|
|
}
|
2020-01-21 10:12:39 +01:00
|
|
|
|
2021-09-03 11:28:22 +02:00
|
|
|
static buildAttributes(class_prototype: object)
|
|
|
|
{
|
|
|
|
let class_tree = [];
|
|
|
|
let attributes = {};
|
|
|
|
let n = 0;
|
|
|
|
do {
|
|
|
|
n++;
|
|
|
|
class_tree.push(class_prototype);
|
|
|
|
class_prototype = Object.getPrototypeOf(class_prototype);
|
|
|
|
} while (class_prototype !== ClassWithAttributes && n < 50);
|
2020-01-21 19:45:21 +01:00
|
|
|
|
2021-09-03 11:28:22 +02:00
|
|
|
for (let i = class_tree.length - 1; i >= 0; i--) {
|
|
|
|
attributes = ClassWithAttributes.extendAttributes(attributes, class_tree[i]._attributes);
|
2020-01-21 19:45:21 +01:00
|
|
|
}
|
2021-09-03 11:28:22 +02:00
|
|
|
return attributes;
|
|
|
|
}
|
2021-06-14 11:47:02 +02:00
|
|
|
|
2021-09-03 11:28:22 +02:00
|
|
|
/**
|
|
|
|
* Extend current _attributes with the one from the parent class
|
|
|
|
*
|
|
|
|
* This gives inheritance from the parent plus the ability to override in the current class.
|
|
|
|
*
|
|
|
|
* @param _attributes
|
|
|
|
* @param _parent
|
|
|
|
*/
|
|
|
|
static extendAttributes(_parent: object, _attributes: object): object
|
|
|
|
{
|
|
|
|
function _copyMerge(_new, _old)
|
2020-01-21 10:12:39 +01:00
|
|
|
{
|
2021-09-03 11:28:22 +02:00
|
|
|
var result = {};
|
2020-01-21 10:12:39 +01:00
|
|
|
|
2021-09-03 11:28:22 +02:00
|
|
|
// Copy the new object
|
|
|
|
if (typeof _new != "undefined") {
|
|
|
|
for (var key in _new) {
|
|
|
|
result[key] = _new[key];
|
|
|
|
}
|
|
|
|
}
|
2021-09-02 18:52:10 +02:00
|
|
|
|
2021-09-03 11:28:22 +02:00
|
|
|
// Merge the old object
|
|
|
|
for (var key in _old) {
|
|
|
|
if (typeof result[key] == "undefined") {
|
|
|
|
result[key] = _old[key];
|
2020-01-21 10:12:39 +01:00
|
|
|
}
|
2021-09-03 11:28:22 +02:00
|
|
|
}
|
2020-01-21 10:12:39 +01:00
|
|
|
|
2021-09-03 11:28:22 +02:00
|
|
|
return result;
|
|
|
|
}
|
2020-01-21 10:12:39 +01:00
|
|
|
|
2021-09-03 11:28:22 +02:00
|
|
|
var attributes = {};
|
2020-01-21 10:12:39 +01:00
|
|
|
|
2021-09-03 11:28:22 +02:00
|
|
|
// Copy the old attributes
|
|
|
|
for (var key in _attributes) {
|
|
|
|
attributes[key] = _copyMerge({}, _attributes[key]);
|
|
|
|
}
|
2020-01-21 10:12:39 +01:00
|
|
|
|
2021-09-03 11:28:22 +02:00
|
|
|
// Add the old attributes to the new ones. If the attributes already
|
|
|
|
// exist, they are merged.
|
|
|
|
for (var key in _parent) {
|
|
|
|
var _old = _parent[key];
|
2020-01-21 10:12:39 +01:00
|
|
|
|
2021-09-03 11:28:22 +02:00
|
|
|
attributes[key] = _copyMerge(attributes[key], _old);
|
|
|
|
}
|
2020-01-21 10:12:39 +01:00
|
|
|
|
2021-09-03 11:28:22 +02:00
|
|
|
// Validate the attributes
|
|
|
|
for (var key in attributes) {
|
|
|
|
et2_validateAttrib(key, attributes[key]);
|
2020-01-21 10:12:39 +01:00
|
|
|
}
|
2021-09-02 18:52:10 +02:00
|
|
|
|
2021-09-03 11:28:22 +02:00
|
|
|
return attributes;
|
|
|
|
}
|
|
|
|
}
|