2022-05-10 00:06:16 +02:00
|
|
|
/**
|
|
|
|
* EGroupware eTemplate2 - JS Link list object
|
|
|
|
*
|
|
|
|
* @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
|
|
|
|
* @copyright 2022 Nathan Gray
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2024-09-24 19:50:38 +02:00
|
|
|
import {css, html, LitElement, nothing, PropertyValues, render, TemplateResult} from "lit";
|
2023-09-13 19:55:33 +02:00
|
|
|
import {until} from "lit/directives/until.js";
|
2022-05-10 00:06:16 +02:00
|
|
|
import {Et2Widget} from "../Et2Widget/Et2Widget";
|
2024-09-24 11:32:17 +02:00
|
|
|
import {LinkInfo} from "./Et2Link";
|
2022-05-10 00:06:16 +02:00
|
|
|
import {et2_IDetachedDOM} from "../et2_core_interfaces";
|
2024-09-24 11:32:17 +02:00
|
|
|
import {property} from "lit/decorators/property.js";
|
|
|
|
import {customElement} from "lit/decorators/custom-element.js";
|
2024-09-24 19:50:38 +02:00
|
|
|
import {repeat} from "lit/directives/repeat.js";
|
2022-05-10 00:06:16 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Display a list of entries in a comma separated list
|
|
|
|
*
|
|
|
|
* Given an application & entry ID, will query the list of links and display
|
|
|
|
*
|
|
|
|
* @see Et2Link
|
|
|
|
*/
|
|
|
|
|
|
|
|
// @ts-ignore TypeScript says there's something wrong with types
|
2024-09-24 11:32:17 +02:00
|
|
|
@customElement('et2-link-string')
|
2022-05-10 00:06:16 +02:00
|
|
|
export class Et2LinkString extends Et2Widget(LitElement) implements et2_IDetachedDOM
|
|
|
|
{
|
|
|
|
|
|
|
|
static get styles()
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
...super.styles,
|
|
|
|
css`
|
2024-09-24 19:50:38 +02:00
|
|
|
:host {
|
|
|
|
list-style-type: none;
|
|
|
|
display: inline;
|
|
|
|
padding: 0px;
|
|
|
|
}
|
2023-05-18 00:55:05 +02:00
|
|
|
|
2024-10-04 17:42:44 +02:00
|
|
|
et2-link, et2-link::part(base), et2-description {
|
2024-09-24 19:50:38 +02:00
|
|
|
display: inline;
|
|
|
|
}
|
2023-05-18 00:55:05 +02:00
|
|
|
|
2024-10-15 17:06:08 +02:00
|
|
|
et2-link::part(icon) {
|
|
|
|
display: none;
|
|
|
|
}
|
|
|
|
|
2024-09-24 19:50:38 +02:00
|
|
|
et2-link:hover {
|
|
|
|
text-decoration: underline;
|
|
|
|
}
|
2023-05-18 00:55:05 +02:00
|
|
|
|
|
|
|
|
2024-09-24 19:50:38 +02:00
|
|
|
/* CSS for child elements */
|
2023-05-18 00:55:05 +02:00
|
|
|
|
2024-09-24 19:50:38 +02:00
|
|
|
et2-link::part(title):after {
|
|
|
|
content: ", "
|
|
|
|
}
|
2023-05-18 00:55:05 +02:00
|
|
|
|
2024-09-24 19:50:38 +02:00
|
|
|
et2-link:last-child::part(title):after {
|
|
|
|
content: initial;
|
|
|
|
}
|
2022-05-10 00:06:16 +02:00
|
|
|
`
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-09-24 11:32:17 +02:00
|
|
|
/**
|
|
|
|
* Specify the application for all the entries, so you only need to specify the entry ID
|
|
|
|
*/
|
|
|
|
@property({ type: String, reflect: true })
|
|
|
|
application;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Application entry ID
|
|
|
|
*/
|
|
|
|
@property({type: String, reflect: true})
|
|
|
|
entryId;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Application filter
|
|
|
|
* Set to an appname or comma separated list of applications to show only linked entries from those
|
|
|
|
* applications
|
|
|
|
*/
|
|
|
|
@property({type: String})
|
|
|
|
onlyApp;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Type filter
|
|
|
|
* Sub-type key to list only entries of that type
|
|
|
|
*/
|
|
|
|
@property({type: String})
|
|
|
|
linkType;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Show links that are marked as deleted, being held for purge
|
|
|
|
*/
|
|
|
|
@property({type: Boolean})
|
|
|
|
showDeleted = false;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Pass value as an object, will be parsed to set application & entryId
|
|
|
|
*/
|
|
|
|
@property({type: Object})
|
|
|
|
value;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Number of application-links to load (file-links are always fully loaded currently)
|
|
|
|
*
|
|
|
|
* If number is exceeded, a "Load more links ..." button is displayed, which will load the double amount of links each time clicked
|
|
|
|
*/
|
|
|
|
@property({type: Number})
|
|
|
|
limit = 20;
|
2022-05-10 00:06:16 +02:00
|
|
|
|
2024-09-24 19:50:38 +02:00
|
|
|
protected _totalResults : number = 0;
|
|
|
|
protected _link_list : LinkInfo[] = [];
|
|
|
|
protected _loadingPromise : Promise<LinkInfo[]> = Promise.resolve([]);
|
|
|
|
protected _loading = false;
|
2022-05-10 00:06:16 +02:00
|
|
|
|
|
|
|
constructor()
|
|
|
|
{
|
|
|
|
super();
|
|
|
|
}
|
|
|
|
|
2023-06-05 18:43:10 +02:00
|
|
|
async getUpdateComplete()
|
|
|
|
{
|
|
|
|
const result = await super.getUpdateComplete();
|
|
|
|
if(this._loadingPromise)
|
|
|
|
{
|
|
|
|
// Wait for the values to arrive before we say we're done
|
|
|
|
await this._loadingPromise;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
2022-05-10 00:06:16 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the value of the list
|
|
|
|
*
|
|
|
|
* Value can be:
|
|
|
|
* - String: CSV list of entries in either app:ID or just ID if application is set.
|
|
|
|
* - Object: {to_app: <appname>, to_id: <Entry ID>} List of linked entries will be fetched from the server
|
|
|
|
* - Array: {app: <appname>, id: <ID>}[]
|
|
|
|
* @param _value
|
|
|
|
*/
|
|
|
|
public set_value(_value : string | { to_app : string, to_id : string } | LinkInfo[])
|
|
|
|
{
|
|
|
|
this._link_list = [];
|
2024-09-24 19:50:38 +02:00
|
|
|
if(typeof _value["total"] !== "undefined")
|
|
|
|
{
|
|
|
|
this._totalResults = _value["total"];
|
|
|
|
delete _value["total"];
|
|
|
|
}
|
|
|
|
|
2022-05-10 00:06:16 +02:00
|
|
|
if(typeof _value == "object" && !Array.isArray(_value) && !_value.to_app && this.application)
|
|
|
|
{
|
|
|
|
_value.to_app = this.application;
|
|
|
|
}
|
2022-11-01 22:38:58 +01:00
|
|
|
|
|
|
|
// We have app & ID - fetch list
|
2022-11-02 18:59:10 +01:00
|
|
|
if(typeof _value == 'object' && !Array.isArray(_value) && _value.to_app && _value.to_id && (
|
|
|
|
typeof _value.to_id === "string" || typeof _value.to_id == "number"))
|
2022-05-10 00:06:16 +02:00
|
|
|
{
|
|
|
|
this.application = _value.to_app;
|
2022-07-21 17:57:50 +02:00
|
|
|
this.entryId = _value.to_id;
|
2024-09-24 19:50:38 +02:00
|
|
|
|
|
|
|
// Let update complete finish first, if it's not done yet
|
|
|
|
this.updateComplete.then(() =>
|
|
|
|
{
|
|
|
|
this.get_links();
|
|
|
|
})
|
2022-05-10 00:06:16 +02:00
|
|
|
return;
|
|
|
|
}
|
2022-11-01 22:38:58 +01:00
|
|
|
|
|
|
|
// CSV list of IDs for one app
|
2022-05-10 00:06:16 +02:00
|
|
|
if(typeof _value === "string")
|
|
|
|
{
|
|
|
|
let ids = _value.split(",");
|
|
|
|
ids.forEach((id) => (<LinkInfo[]>this._link_list).push(<LinkInfo>{app: this.application, id: id}));
|
|
|
|
}
|
2022-11-01 22:38:58 +01:00
|
|
|
// List of LinkInfo
|
2024-09-24 19:50:38 +02:00
|
|
|
else if(Array.isArray(_value) || typeof _value[0] == "object")
|
2022-05-10 00:06:16 +02:00
|
|
|
{
|
2024-09-24 19:50:38 +02:00
|
|
|
this._link_list = <LinkInfo[]>Object.values(_value);
|
2022-05-10 00:06:16 +02:00
|
|
|
}
|
2022-11-01 22:38:58 +01:00
|
|
|
// List of LinkInfo stuffed into to_id - entry is not yet saved
|
2022-11-02 12:27:49 +01:00
|
|
|
else if(_value.to_id && typeof _value.to_id !== "string")
|
2022-11-01 22:38:58 +01:00
|
|
|
{
|
|
|
|
this.entryId = _value.to_id;
|
|
|
|
Object.keys(_value.to_id).forEach((key) =>
|
|
|
|
{
|
|
|
|
this._link_list.push(<LinkInfo>_value.to_id[key]);
|
|
|
|
});
|
|
|
|
}
|
2024-09-24 19:50:38 +02:00
|
|
|
this.requestUpdate();
|
2022-05-10 00:06:16 +02:00
|
|
|
}
|
|
|
|
|
2023-01-17 22:03:28 +01:00
|
|
|
public updated(changedProperties : PropertyValues)
|
|
|
|
{
|
|
|
|
super.updated(changedProperties);
|
|
|
|
|
|
|
|
if((changedProperties.has("application") || changedProperties.has("entryId") || changedProperties.has("onlyApp") || changedProperties.has("linkType")) &&
|
|
|
|
this.application && this.entryId
|
|
|
|
)
|
|
|
|
{
|
|
|
|
// Something changed, and we have the information needed to get the matching links
|
|
|
|
this.get_links();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-10 00:06:16 +02:00
|
|
|
public render() : TemplateResult
|
|
|
|
{
|
|
|
|
// This shows loading template until loadingPromise resolves, then shows _listTemplate
|
|
|
|
return html`
|
2024-09-24 19:50:38 +02:00
|
|
|
${until(this._loadingPromise?.then(res =>
|
2022-05-10 00:06:16 +02:00
|
|
|
{
|
2024-09-24 19:50:38 +02:00
|
|
|
return this._listTemplate();
|
2022-05-10 00:06:16 +02:00
|
|
|
}),
|
|
|
|
this._loadingTemplate()
|
2024-09-24 19:50:38 +02:00
|
|
|
)}
|
|
|
|
${until(this.moreResultsTemplate(), nothing)}
|
2022-05-10 00:06:16 +02:00
|
|
|
`;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected _listTemplate()
|
|
|
|
{
|
|
|
|
return html`
|
2024-09-24 19:50:38 +02:00
|
|
|
${repeat(this._link_list, l => l.link_id, this._linkTemplate)}
|
|
|
|
`;
|
2022-05-10 00:06:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Render one link
|
|
|
|
*
|
|
|
|
* @param link
|
|
|
|
* @returns {TemplateResult}
|
|
|
|
* @protected
|
|
|
|
*/
|
2022-11-01 22:38:58 +01:00
|
|
|
protected _linkTemplate(link : LinkInfo) : TemplateResult
|
2022-05-10 00:06:16 +02:00
|
|
|
{
|
2022-11-01 22:38:58 +01:00
|
|
|
const id = typeof link.id === "string" ? link.id : link.link_id;
|
2022-05-10 00:06:16 +02:00
|
|
|
return html`
|
2024-09-24 19:50:38 +02:00
|
|
|
<et2-link part="link" class="et2_link"
|
|
|
|
app="${link.app}" entryId="${id}" .value=${link} ._parent=${this}
|
|
|
|
></et2-link>`;
|
2022-05-10 00:06:16 +02:00
|
|
|
}
|
|
|
|
|
2024-09-24 11:32:17 +02:00
|
|
|
/**
|
2024-09-24 19:50:38 +02:00
|
|
|
* Render that we're waiting for data
|
2024-09-24 11:32:17 +02:00
|
|
|
* @returns {TemplateResult}
|
|
|
|
* @protected
|
|
|
|
*/
|
2024-09-24 19:50:38 +02:00
|
|
|
protected _loadingTemplate() : TemplateResult
|
2024-09-24 11:32:17 +02:00
|
|
|
{
|
|
|
|
return html`
|
2024-09-24 19:50:38 +02:00
|
|
|
<div class="search__loading">
|
|
|
|
<sl-spinner></sl-spinner>
|
|
|
|
</div>
|
|
|
|
`;
|
2024-09-24 11:32:17 +02:00
|
|
|
}
|
|
|
|
|
2024-09-24 19:50:38 +02:00
|
|
|
protected async moreResultsTemplate()
|
2022-05-10 00:06:16 +02:00
|
|
|
{
|
2024-09-24 19:50:38 +02:00
|
|
|
if(this._totalResults <= 0 || !this._loadingPromise)
|
|
|
|
{
|
|
|
|
return nothing;
|
|
|
|
}
|
|
|
|
return this._loadingPromise.then(() =>
|
|
|
|
{
|
|
|
|
const moreCount = this._totalResults - this._link_list.length;
|
2024-10-04 17:42:44 +02:00
|
|
|
const more = html`
|
|
|
|
<et2-description statustext="${this.egw().lang("%1 more...", moreCount)}">...
|
|
|
|
</et2-description>`;
|
2024-09-24 19:50:38 +02:00
|
|
|
return html`${moreCount > 0 ? more : nothing}`;
|
|
|
|
});
|
2022-05-10 00:06:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Render a list of links inside the list
|
2022-05-11 00:02:33 +02:00
|
|
|
* These get slotted, rather than put inside the shadow dom
|
|
|
|
*
|
2022-05-10 00:06:16 +02:00
|
|
|
* @param links
|
|
|
|
* @protected
|
|
|
|
*/
|
2022-05-11 00:02:33 +02:00
|
|
|
protected _addLinks(links : LinkInfo[])
|
2022-05-10 00:06:16 +02:00
|
|
|
{
|
2024-09-24 19:50:38 +02:00
|
|
|
return;
|
2022-05-11 22:00:23 +02:00
|
|
|
// Remove anything there right now
|
|
|
|
while(this.lastChild)
|
|
|
|
{
|
|
|
|
this.removeChild(this.lastChild);
|
|
|
|
}
|
|
|
|
|
|
|
|
links.forEach((link) =>
|
|
|
|
{
|
|
|
|
let temp = document.createElement("div");
|
2024-09-24 19:50:38 +02:00
|
|
|
render(this._linkTemplate(link), temp);
|
2022-05-11 22:00:23 +02:00
|
|
|
temp.childNodes.forEach((node) => this.appendChild(node));
|
|
|
|
})
|
|
|
|
|
|
|
|
/*
|
|
|
|
This should work, and it does, but only once.
|
|
|
|
It fails if you try and update then run it again - none of the children get added
|
|
|
|
Something about how lit renders
|
2022-05-11 00:02:33 +02:00
|
|
|
render(html`${repeat(links,
|
|
|
|
(link) => link.app + ":" + link.id,
|
|
|
|
(link) => this._linkTemplate(link))}`,
|
|
|
|
<HTMLElement><unknown>this
|
|
|
|
);
|
2022-05-11 22:00:23 +02:00
|
|
|
|
|
|
|
*/
|
2023-03-03 22:06:11 +01:00
|
|
|
this.dispatchEvent(new Event("change", {bubbles: true}));
|
2022-05-10 00:06:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Starts the request for link list to the server
|
2022-05-11 22:00:23 +02:00
|
|
|
*
|
|
|
|
* Called internally to fetch the list. May be called externally to trigger a refresh if a link is added.
|
2022-05-12 18:54:29 +02:00
|
|
|
*
|
2022-05-10 00:06:16 +02:00
|
|
|
*/
|
2024-09-24 19:50:38 +02:00
|
|
|
public get_links(not_saved_links? : LinkInfo[], offset = 0)
|
2022-05-10 00:06:16 +02:00
|
|
|
{
|
2024-09-24 19:50:38 +02:00
|
|
|
if(this._loading)
|
|
|
|
{
|
|
|
|
// Already waiting
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this._loading = true;
|
|
|
|
|
2022-05-12 18:54:29 +02:00
|
|
|
if(typeof not_saved_links === "undefined")
|
|
|
|
{
|
|
|
|
not_saved_links = [];
|
|
|
|
}
|
2022-05-10 00:06:16 +02:00
|
|
|
let _value = {
|
|
|
|
to_app: this.application,
|
2022-07-21 17:57:50 +02:00
|
|
|
to_id: this.entryId,
|
2023-01-17 22:03:28 +01:00
|
|
|
only_app: this.onlyApp,
|
2024-09-24 11:32:17 +02:00
|
|
|
show_deleted: this.showDeleted,
|
2024-09-24 19:50:38 +02:00
|
|
|
limit: [offset, /* num_rows: */this.limit]
|
2022-05-10 00:06:16 +02:00
|
|
|
};
|
|
|
|
|
2022-05-12 18:08:59 +02:00
|
|
|
this._loadingPromise = <Promise<LinkInfo[]>>(this.egw().jsonq('EGroupware\\Api\\Etemplate\\Widget\\Link::ajax_link_list', [_value]))
|
|
|
|
.then(_value =>
|
|
|
|
{
|
2024-09-24 19:50:38 +02:00
|
|
|
if(typeof _value.total)
|
|
|
|
{
|
|
|
|
this._totalResults = _value.total;
|
|
|
|
delete _value.total;
|
|
|
|
}
|
|
|
|
if(_value)
|
2023-07-25 18:26:22 +02:00
|
|
|
{
|
2024-09-24 19:50:38 +02:00
|
|
|
for(let link of <LinkInfo[]>Object.values(_value))
|
2023-07-25 18:26:22 +02:00
|
|
|
{
|
2024-09-24 19:50:38 +02:00
|
|
|
// Avoid duplicates, files are always sent
|
|
|
|
if(!not_saved_links.some(l => l.app == link.app && l.id == link.id) &&
|
|
|
|
!this._link_list.some(l => l.app == link.app && l.id == link.id))
|
2023-07-25 18:26:22 +02:00
|
|
|
{
|
2024-09-24 19:50:38 +02:00
|
|
|
this._link_list.push(link);
|
2023-07-25 18:26:22 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-09-24 19:50:38 +02:00
|
|
|
this._loading = false;
|
|
|
|
this.requestUpdate();
|
2022-05-12 18:08:59 +02:00
|
|
|
})
|
2022-05-10 00:06:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
getDetachedAttributes(_attrs : string[])
|
|
|
|
{
|
2023-04-25 21:53:16 +02:00
|
|
|
_attrs.push("application", "entryId", "statustext");
|
2022-05-10 00:06:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
getDetachedNodes() : HTMLElement[]
|
|
|
|
{
|
|
|
|
return [<HTMLElement><unknown>this];
|
|
|
|
}
|
|
|
|
|
|
|
|
setDetachedAttributes(_nodes : HTMLElement[], _values : object, _data?)
|
|
|
|
{
|
|
|
|
for(let k in _values)
|
|
|
|
{
|
|
|
|
this[k] = _values[k];
|
2024-10-15 17:06:08 +02:00
|
|
|
this.requestUpdate(k);
|
2022-05-10 00:06:16 +02:00
|
|
|
}
|
|
|
|
}
|
2024-09-24 11:32:17 +02:00
|
|
|
}
|