2021-12-17 22:47:19 +01:00
|
|
|
/**
|
|
|
|
* EGroupware eTemplate2 - Description WebComponent
|
|
|
|
*
|
|
|
|
* @license http://opensource.org/licenses/gpl-license.php GPL - GNU General Public License
|
|
|
|
* @package api
|
|
|
|
* @link https://www.egroupware.org
|
|
|
|
* @author Nathan Gray
|
|
|
|
*/
|
|
|
|
|
|
|
|
import {Et2Widget} from "../Et2Widget/Et2Widget";
|
2022-04-21 00:23:53 +02:00
|
|
|
import {css, html, LitElement, render} from "@lion/core";
|
2021-12-17 22:47:19 +01:00
|
|
|
import {et2_IDetachedDOM} from "../et2_core_interfaces";
|
|
|
|
import {activateLinks} from "../ActivateLinksDirective";
|
|
|
|
|
|
|
|
export class Et2Description extends Et2Widget(LitElement) implements et2_IDetachedDOM
|
|
|
|
{
|
|
|
|
|
|
|
|
protected _value : string = "";
|
|
|
|
|
|
|
|
static get styles()
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
...super.styles,
|
|
|
|
css`
|
2022-04-21 18:41:30 +02:00
|
|
|
* {
|
|
|
|
white-space: pre-wrap;
|
2021-12-20 23:15:32 +01:00
|
|
|
}
|
|
|
|
:host a {
|
|
|
|
cursor: pointer;
|
|
|
|
color: #26537c;
|
|
|
|
text-decoration: none;
|
2021-12-17 22:47:19 +01:00
|
|
|
}`
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
static get properties()
|
|
|
|
{
|
|
|
|
return {
|
|
|
|
...super.properties,
|
2021-12-20 23:15:32 +01:00
|
|
|
/**
|
|
|
|
* Scan the value, and if there are any links (URL, mailto:) then wrap them in a clickable
|
|
|
|
* <a/> tag
|
|
|
|
*/
|
2022-07-21 17:57:50 +02:00
|
|
|
activeLinks: {
|
2021-12-20 23:15:32 +01:00
|
|
|
type: Boolean,
|
|
|
|
reflect: true
|
|
|
|
},
|
|
|
|
/**
|
|
|
|
* Extra link target
|
|
|
|
* Goes with href. If provided, that's the target for opening the link.
|
|
|
|
*/
|
2022-07-21 17:57:50 +02:00
|
|
|
extraLinkTarget: {
|
2021-12-21 19:16:58 +01:00
|
|
|
type: String,
|
|
|
|
reflect: true
|
2021-12-20 23:15:32 +01:00
|
|
|
},
|
|
|
|
/**
|
|
|
|
* widthxheight, if popup should be used, eg. 640x480
|
|
|
|
*/
|
2022-07-21 17:57:50 +02:00
|
|
|
extraLinkPopup: {
|
2021-12-21 19:16:58 +01:00
|
|
|
type: String,
|
|
|
|
reflect: true
|
2021-12-20 23:15:32 +01:00
|
|
|
},
|
|
|
|
/**
|
|
|
|
* Link URL
|
|
|
|
* If provided, will be clickable and open this URL
|
|
|
|
*/
|
|
|
|
href: {
|
2021-12-21 19:16:58 +01:00
|
|
|
type: String,
|
|
|
|
reflect: true
|
2021-12-20 23:15:32 +01:00
|
|
|
},
|
2021-12-17 22:47:19 +01:00
|
|
|
value: String,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-20 23:15:32 +01:00
|
|
|
constructor()
|
|
|
|
{
|
|
|
|
super();
|
|
|
|
|
|
|
|
// Initialize properties
|
2022-07-21 17:57:50 +02:00
|
|
|
this.activeLinks = false;
|
|
|
|
this.extraLinkPopup = "";
|
|
|
|
this.extraLinkTarget = "_browser";
|
2022-02-03 21:52:14 +01:00
|
|
|
// Don't initialize this to avoid href(unknown) when rendered
|
|
|
|
//this.href = "";
|
2021-12-20 23:15:32 +01:00
|
|
|
this.value = "";
|
2021-12-21 18:52:39 +01:00
|
|
|
|
|
|
|
this._handleClick = this._handleClick.bind(this);
|
2021-12-20 23:15:32 +01:00
|
|
|
}
|
|
|
|
|
2022-04-21 00:23:53 +02:00
|
|
|
connectedCallback()
|
|
|
|
{
|
|
|
|
super.connectedCallback();
|
|
|
|
|
|
|
|
// Put content directly in DOM
|
|
|
|
if(this.value)
|
|
|
|
{
|
|
|
|
render(this._renderContent(), this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-17 22:47:19 +01:00
|
|
|
set_value(value)
|
|
|
|
{
|
|
|
|
this.value = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
get value()
|
|
|
|
{
|
|
|
|
return this._value;
|
|
|
|
}
|
|
|
|
|
|
|
|
set value(_value : string)
|
|
|
|
{
|
|
|
|
let oldValue = this.value;
|
|
|
|
|
|
|
|
if(!_value)
|
|
|
|
{
|
|
|
|
_value = "";
|
|
|
|
}
|
|
|
|
|
|
|
|
// Do we do this here, or in transformAttributes()?
|
2022-07-21 17:57:50 +02:00
|
|
|
if(_value && !this.noLang)
|
2021-12-17 22:47:19 +01:00
|
|
|
{
|
|
|
|
_value = this.egw().lang(_value);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(_value && (_value + "").indexOf('%s') != -1)
|
|
|
|
{
|
|
|
|
_value = _value.replace(/%s/g, _value);
|
|
|
|
}
|
|
|
|
|
|
|
|
this._value = _value;
|
|
|
|
this.requestUpdate('value', oldValue);
|
|
|
|
}
|
|
|
|
|
2022-05-11 21:58:01 +02:00
|
|
|
requestUpdate(attribute, oldValue)
|
|
|
|
{
|
|
|
|
super.requestUpdate(...arguments);
|
|
|
|
// Due to how we do the rendering into the light DOM (not sure it's right) we need this after
|
|
|
|
// value change or it won't actually show up
|
2022-07-21 17:57:50 +02:00
|
|
|
if(["value", "href", "activeLinks"].indexOf(attribute) != -1 && this.parentNode)
|
2022-05-11 21:58:01 +02:00
|
|
|
{
|
|
|
|
this.updateComplete.then(() => render(this._renderContent(), <HTMLElement><unknown>this));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-21 00:23:53 +02:00
|
|
|
_renderContent()
|
2021-12-17 22:47:19 +01:00
|
|
|
{
|
2021-12-20 23:15:32 +01:00
|
|
|
let render = null;
|
2021-12-17 22:47:19 +01:00
|
|
|
|
|
|
|
// Add hover action button (Edit)
|
|
|
|
if(this.hover_action)
|
|
|
|
{
|
|
|
|
// TODO
|
|
|
|
}
|
2021-12-20 23:15:32 +01:00
|
|
|
|
2021-12-17 22:47:19 +01:00
|
|
|
|
|
|
|
// If there's a link, wrap that
|
2022-05-05 21:48:39 +02:00
|
|
|
if(this.href && this.value)
|
2021-12-17 22:47:19 +01:00
|
|
|
{
|
2022-05-05 21:48:39 +02:00
|
|
|
render = this.wrapLink(this.href, this.value);
|
2021-12-17 22:47:19 +01:00
|
|
|
}
|
|
|
|
// If we want to activate links inside, do that
|
2022-07-21 17:57:50 +02:00
|
|
|
else if(this.activeLinks && this.value)
|
2021-12-17 22:47:19 +01:00
|
|
|
{
|
2022-07-21 17:57:50 +02:00
|
|
|
render = this.getActivatedValue(this.value, this.href ? this.extraLinkTarget : '_blank');
|
2021-12-17 22:47:19 +01:00
|
|
|
}
|
|
|
|
// Just do the value
|
|
|
|
else
|
|
|
|
{
|
2022-05-05 21:48:39 +02:00
|
|
|
render = html`${this.value}`;
|
2021-12-20 23:15:32 +01:00
|
|
|
}
|
2022-04-21 00:23:53 +02:00
|
|
|
return render;
|
2021-12-20 23:15:32 +01:00
|
|
|
}
|
|
|
|
|
2022-04-21 00:23:53 +02:00
|
|
|
render()
|
2022-04-13 00:28:36 +02:00
|
|
|
{
|
2022-04-21 18:41:30 +02:00
|
|
|
// Turn off IDE reformatting, or it will add an extra line break into the template
|
|
|
|
// @formatter:off
|
|
|
|
return html`<slot></slot>`;
|
|
|
|
// @formatter:on
|
2022-04-13 00:28:36 +02:00
|
|
|
}
|
|
|
|
|
2022-04-21 00:23:53 +02:00
|
|
|
|
2021-12-20 23:15:32 +01:00
|
|
|
async firstUpdated()
|
|
|
|
{
|
2021-12-21 18:52:39 +01:00
|
|
|
this.removeEventListener('click.extra_link', this._handleClick);
|
2022-07-21 17:57:50 +02:00
|
|
|
if(this.extraLinkPopup || this.mime)
|
2021-12-20 23:15:32 +01:00
|
|
|
{
|
|
|
|
// Add click listener
|
2021-12-21 18:52:39 +01:00
|
|
|
this.addEventListener('click.extra_link', this._handleClick);
|
2021-12-20 23:15:32 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_handleClick(_ev : MouseEvent) : boolean
|
|
|
|
{
|
2022-02-25 11:33:04 +01:00
|
|
|
// call super to get the onclick handling running
|
|
|
|
super._handleClick(_ev);
|
|
|
|
|
2022-07-21 17:57:50 +02:00
|
|
|
if(this.mimeData || this.href)
|
2021-12-20 23:15:32 +01:00
|
|
|
{
|
2022-07-21 17:57:50 +02:00
|
|
|
egw(window).open_link(this.mimeData || this.href, this.extraLinkTarget, this.extraLinkPopup, null, null, this.mime);
|
2021-12-17 22:47:19 +01:00
|
|
|
}
|
2021-12-20 23:15:32 +01:00
|
|
|
_ev.preventDefault();
|
|
|
|
return false;
|
2021-12-17 22:47:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
protected wrapLink(href, value)
|
|
|
|
{
|
|
|
|
if(href.indexOf('/') == -1 && href.split('.').length >= 3 &&
|
|
|
|
!(href.indexOf('mailto:') != -1 || href.indexOf('://') != -1 || href.indexOf('javascript:') != -1)
|
|
|
|
)
|
|
|
|
{
|
|
|
|
href = "/index.php?menuaction=" + href;
|
|
|
|
}
|
|
|
|
if(href.charAt(0) == '/') // link relative to eGW
|
|
|
|
{
|
|
|
|
href = egw.link(href);
|
|
|
|
}
|
|
|
|
return html`<a href="${href}" target="${this.target ?? "_blank"}">${value}</a>`;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected getActivatedValue(value, target)
|
|
|
|
{
|
|
|
|
return html`${activateLinks(value, target)}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
getDetachedAttributes(attrs)
|
|
|
|
{
|
2021-12-20 23:15:32 +01:00
|
|
|
attrs.push("id", "value", "class", "href");
|
2021-12-17 22:47:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
getDetachedNodes() : HTMLElement[]
|
|
|
|
{
|
|
|
|
return [<HTMLElement><unknown>this];
|
|
|
|
}
|
|
|
|
|
|
|
|
setDetachedAttributes(_nodes : HTMLElement[], _values : object, _data? : any) : void
|
|
|
|
{
|
2022-05-03 19:01:42 +02:00
|
|
|
for(let attr in _values)
|
|
|
|
{
|
|
|
|
this[attr] = _values[attr];
|
|
|
|
}
|
2021-12-17 22:47:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
loadFromXML()
|
|
|
|
{
|
|
|
|
// nope
|
|
|
|
}
|
|
|
|
}
|
2022-05-13 11:59:13 +02:00
|
|
|
// @ts-ignore TypeScript is not recognizing that this widget is a LitElement
|
|
|
|
customElements.define("et2-description", Et2Description);
|
2021-12-17 22:47:19 +01:00
|
|
|
|
2022-05-13 11:59:13 +02:00
|
|
|
export class Et2Label extends Et2Description {}
|
2021-12-17 22:47:19 +01:00
|
|
|
// @ts-ignore TypeScript is not recognizing that this widget is a LitElement
|
2022-05-13 11:59:13 +02:00
|
|
|
customElements.define("et2-label", Et2Label);
|