2022-07-05 18:18:12 +02:00
|
|
|
/*
|
|
|
|
* Calendar owner widget
|
|
|
|
*
|
|
|
|
* @license https://opensource.org/licenses/gpl-license.php GPL - GNU General Public License
|
|
|
|
* @package calendar
|
|
|
|
* @subpackage etemplate
|
|
|
|
* @link https://www.egroupware.org
|
|
|
|
* @author Nathan Gray
|
|
|
|
*/
|
|
|
|
|
|
|
|
import {Et2Select} from "../../api/js/etemplate/Et2Select/Et2Select";
|
2024-03-18 22:33:40 +01:00
|
|
|
import {css, html, nothing, TemplateResult} from "lit";
|
2022-07-07 22:15:00 +02:00
|
|
|
import {IsEmail} from "../../api/js/etemplate/Validators/IsEmail";
|
2023-08-01 23:53:16 +02:00
|
|
|
import {SelectOption} from "../../api/js/etemplate/Et2Select/FindSelectOptions";
|
2023-05-11 19:14:17 +02:00
|
|
|
import {Et2StaticSelectMixin} from "../../api/js/etemplate/Et2Select/StaticOptions";
|
2023-10-02 16:04:19 +02:00
|
|
|
import {classMap} from "lit/directives/class-map.js";
|
2022-07-05 18:18:12 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Select widget customised for calendar owner, which can be a user
|
|
|
|
* account or group, or an entry from almost any app, or an email address
|
|
|
|
*
|
|
|
|
*/
|
2023-05-11 19:14:17 +02:00
|
|
|
export class CalendarOwner extends Et2StaticSelectMixin(Et2Select)
|
2022-07-05 18:18:12 +02:00
|
|
|
{
|
|
|
|
|
|
|
|
static get styles()
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
...super.styles,
|
|
|
|
css`
|
|
|
|
/* Larger maximum height before scroll*/
|
|
|
|
.select__tags {
|
|
|
|
max-height: 10em;
|
|
|
|
}
|
2023-11-03 00:29:50 +01:00
|
|
|
|
|
|
|
.title {
|
|
|
|
float: right;
|
|
|
|
}
|
2022-07-05 18:18:12 +02:00
|
|
|
`
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
constructor(...args : any[])
|
|
|
|
{
|
|
|
|
super(...args);
|
|
|
|
this.searchUrl = "calendar_owner_etemplate_widget::ajax_search";
|
|
|
|
this.multiple = true;
|
2022-10-21 18:53:21 +02:00
|
|
|
|
|
|
|
// Take grants into account for search
|
|
|
|
this.searchOptions['checkgrants'] = true;
|
2022-07-05 18:18:12 +02:00
|
|
|
}
|
|
|
|
|
2023-08-01 23:53:16 +02:00
|
|
|
/**
|
|
|
|
* Override parent to show email address in options
|
|
|
|
*
|
|
|
|
* We use this in edit dialog, but the same widget is used in sidemenu where the email is hidden via CSS.
|
|
|
|
* Anything set in "title" will be shown
|
|
|
|
*
|
|
|
|
* @param {SelectOption} option
|
|
|
|
* @returns {TemplateResult}
|
|
|
|
*/
|
|
|
|
_optionTemplate(option : SelectOption) : TemplateResult
|
2023-05-11 19:14:17 +02:00
|
|
|
{
|
2023-10-02 16:04:19 +02:00
|
|
|
// Exclude non-matches when searching
|
|
|
|
// unless they're already selected, in which case removing them removes them from value
|
|
|
|
if(typeof option.isMatch == "boolean" && !option.isMatch && !this.getValueAsArray().includes(option.value))
|
|
|
|
{
|
|
|
|
return html``;
|
|
|
|
}
|
|
|
|
|
2023-09-19 19:59:48 +02:00
|
|
|
const value = (<string>option.value).replaceAll(" ", "___");
|
2023-10-03 00:06:25 +02:00
|
|
|
const classes = option.class ? Object.fromEntries((option.class).split(" ").map(k => [k, true])) : {};
|
2023-08-01 23:53:16 +02:00
|
|
|
return html`
|
2023-09-19 19:59:48 +02:00
|
|
|
<sl-option
|
|
|
|
part="option"
|
2023-12-19 13:22:06 +01:00
|
|
|
exportparts="prefix:tag__prefix, suffix:tag__suffix, image"
|
2023-10-02 16:04:19 +02:00
|
|
|
value="${value}"
|
2023-09-19 19:59:48 +02:00
|
|
|
title="${!option.title || this.noLang ? option.title : this.egw().lang(option.title)}"
|
2023-10-02 16:04:19 +02:00
|
|
|
class=${classMap({
|
|
|
|
"match": option.isMatch,
|
|
|
|
"no-match": !option.isMatch,
|
2023-10-03 00:06:25 +02:00
|
|
|
...classes
|
2023-10-02 16:04:19 +02:00
|
|
|
})}
|
|
|
|
.option=${option}
|
2023-09-19 19:59:48 +02:00
|
|
|
.selected=${this.getValueAsArray().some(v => v == value)}
|
2023-10-02 16:04:19 +02:00
|
|
|
?disabled=${option.disabled}
|
2023-11-03 00:00:34 +01:00
|
|
|
.getTextLabel=${() => {return option.label ?? option.value}}
|
2023-08-01 23:53:16 +02:00
|
|
|
>
|
|
|
|
${this._iconTemplate(option)}
|
|
|
|
${this.noLang ? option.label : this.egw().lang(option.label)}
|
2023-11-03 00:29:50 +01:00
|
|
|
<span class="title">${option.title}</span>
|
2023-09-13 19:55:33 +02:00
|
|
|
</sl-option>`;
|
2023-08-01 23:53:16 +02:00
|
|
|
}
|
2023-05-11 19:14:17 +02:00
|
|
|
|
2022-07-05 18:18:12 +02:00
|
|
|
/**
|
|
|
|
* Override parent to handle our special additional data types (c#,r#,etc.) when they
|
|
|
|
* are not available client side.
|
|
|
|
*
|
|
|
|
* @param {string|string[]} _value array of selected owners, which can be a number,
|
|
|
|
* or a number prefixed with one character indicating the resource type.
|
|
|
|
*/
|
|
|
|
set_value(_value)
|
|
|
|
{
|
|
|
|
super.set_value(_value);
|
|
|
|
|
|
|
|
// If parent didn't find a label, label will be the same as ID so we
|
|
|
|
// can find them that way
|
|
|
|
let missing_labels = [];
|
2022-07-22 21:24:38 +02:00
|
|
|
this.updateComplete.then(() =>
|
2022-07-05 18:18:12 +02:00
|
|
|
{
|
2023-11-01 20:26:23 +01:00
|
|
|
// find that can handle option groups
|
|
|
|
const find = (option) =>
|
|
|
|
{
|
|
|
|
if(Array.isArray(option.value))
|
|
|
|
{
|
2023-11-03 00:00:34 +01:00
|
|
|
return option.value.find(find);
|
2023-11-01 20:26:23 +01:00
|
|
|
}
|
|
|
|
return option.value == this.value[i];
|
|
|
|
}
|
2022-07-22 21:24:38 +02:00
|
|
|
for(var i = 0; i < this.value.length; i++)
|
2022-07-05 18:18:12 +02:00
|
|
|
{
|
2023-11-01 20:26:23 +01:00
|
|
|
if(!this.select_options.find(find))
|
2022-07-22 21:24:38 +02:00
|
|
|
{
|
|
|
|
missing_labels.push(this.value[i]);
|
|
|
|
}
|
2022-07-05 18:18:12 +02:00
|
|
|
}
|
2022-07-22 21:24:38 +02:00
|
|
|
if(Object.keys(missing_labels).length > 0)
|
2022-07-05 18:18:12 +02:00
|
|
|
{
|
2022-07-22 21:24:38 +02:00
|
|
|
// Proper label was not found by parent - ask directly
|
|
|
|
this.egw().json('calendar_owner_etemplate_widget::ajax_owner', [missing_labels], function(data)
|
2022-07-05 18:18:12 +02:00
|
|
|
{
|
2022-07-22 21:24:38 +02:00
|
|
|
for(let owner in data)
|
2022-07-05 18:18:12 +02:00
|
|
|
{
|
2022-07-22 21:24:38 +02:00
|
|
|
if(!owner || typeof owner == "undefined")
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// Put it in the list of options
|
|
|
|
let index = this.select_options.findIndex(o => o.value == owner);
|
2022-12-08 22:26:17 +01:00
|
|
|
let remote_index = this._selected_remote.findIndex(o => o.value == owner);
|
|
|
|
if(remote_index !== -1)
|
2022-07-22 21:24:38 +02:00
|
|
|
{
|
2022-12-08 22:26:17 +01:00
|
|
|
this._selected_remote[remote_index] = data[owner];
|
2022-07-22 21:24:38 +02:00
|
|
|
}
|
2022-12-08 22:26:17 +01:00
|
|
|
else if(index == -1)
|
2022-07-22 21:24:38 +02:00
|
|
|
{
|
2022-12-08 22:26:17 +01:00
|
|
|
this._selected_remote.push(data[owner]);
|
2022-07-22 21:24:38 +02:00
|
|
|
}
|
2022-07-05 18:18:12 +02:00
|
|
|
}
|
2022-07-22 21:24:38 +02:00
|
|
|
this.requestUpdate("select_options");
|
|
|
|
}, this, true, this).sendRequest();
|
|
|
|
}
|
|
|
|
});
|
2022-07-05 18:18:12 +02:00
|
|
|
}
|
2022-07-15 21:31:15 +02:00
|
|
|
|
2023-05-11 18:56:42 +02:00
|
|
|
/**
|
|
|
|
* Check a value for missing options and remove them.
|
|
|
|
*
|
|
|
|
* Override to allow any value, since we won't have all options
|
|
|
|
*
|
|
|
|
* @param {string[]} value
|
|
|
|
* @returns {string[]}
|
|
|
|
*/
|
|
|
|
filterOutMissingOptions(value : string[]) : string[]
|
|
|
|
{
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
2023-01-30 23:26:09 +01:00
|
|
|
/**
|
|
|
|
* Override icon for the select option to use lavatar
|
|
|
|
*
|
|
|
|
* @param option
|
|
|
|
* @protected
|
|
|
|
*/
|
2023-11-01 20:26:23 +01:00
|
|
|
protected _iconTemplate(option : SelectOption)
|
2023-01-30 23:26:09 +01:00
|
|
|
{
|
|
|
|
// Not a user / contact, no icon - use app image
|
|
|
|
if(!option.fname && !option.lname && !option.icon && option.app)
|
|
|
|
{
|
|
|
|
return html`
|
2023-03-28 22:43:50 +02:00
|
|
|
<et2-image src="${option.app}/navbar" style="width: var(--icon-width)"></et2-image>`;
|
2023-01-30 23:26:09 +01:00
|
|
|
}
|
|
|
|
// lavatar uses a size property, not a CSS variable
|
|
|
|
let style = getComputedStyle(this);
|
|
|
|
return html`
|
2023-12-19 13:22:06 +01:00
|
|
|
<et2-lavatar slot="prefix" part="icon" exportparts="image" .size=${style.getPropertyValue("--icon-width")}
|
2023-01-30 23:26:09 +01:00
|
|
|
lname=${option.lname || nothing}
|
|
|
|
fname=${option.fname || nothing}
|
|
|
|
image=${option.icon || nothing}
|
|
|
|
>
|
|
|
|
</et2-lavatar>`;
|
|
|
|
}
|
|
|
|
|
2022-07-15 21:31:15 +02:00
|
|
|
/**
|
|
|
|
* Check if a free entry value is acceptable.
|
|
|
|
* We only check the free entry, since value can be mixed.
|
|
|
|
*
|
|
|
|
* @param text
|
|
|
|
* @returns {boolean}
|
|
|
|
*/
|
|
|
|
public validateFreeEntry(text) : boolean
|
|
|
|
{
|
|
|
|
let validators = [...this.validators, new IsEmail()];
|
|
|
|
let result = validators.filter(v =>
|
|
|
|
v.execute(text, v.param, {node: this}),
|
|
|
|
);
|
|
|
|
return result.length == 0;
|
|
|
|
}
|
2022-07-05 18:18:12 +02:00
|
|
|
}
|
|
|
|
|
2023-01-20 17:41:13 +01:00
|
|
|
if(!customElements.get("et2-calendar-owner"))
|
|
|
|
{
|
|
|
|
customElements.define("et2-calendar-owner", CalendarOwner);
|
|
|
|
}
|