2022-12-09 21:16:44 +01:00
|
|
|
import {SelectOption} from "./FindSelectOptions";
|
2023-09-13 19:55:33 +02:00
|
|
|
import {LitElement} from "lit";
|
2022-12-09 21:16:44 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* EGroupware eTemplate2 - SelectAccountMixin
|
|
|
|
*
|
|
|
|
* @license http://opensource.org/licenses/gpl-license.php GPL - GNU General Public License
|
|
|
|
* @package api
|
|
|
|
* @link https://www.egroupware.org
|
|
|
|
* @author Nathan Gray
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Common code for editable & read-only select account
|
|
|
|
* Moved out so we don't have copy+paste code in both
|
|
|
|
*/
|
|
|
|
export declare class SelectAccountInterface
|
|
|
|
{
|
|
|
|
set value(new_value : string | string[])
|
|
|
|
|
|
|
|
get value() : string | string[]
|
|
|
|
|
|
|
|
set select_options(new_options : SelectOption[])
|
|
|
|
get select_options() : SelectOption[]
|
|
|
|
}
|
|
|
|
|
|
|
|
type Constructor<T = {}> = new (...args : any[]) => T;
|
|
|
|
export const SelectAccountMixin = <T extends Constructor<LitElement>>(superclass : T) =>
|
|
|
|
{
|
|
|
|
class SelectAccount extends superclass
|
|
|
|
{
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Hold on to accounts we had to fetch from the server
|
|
|
|
* @type {any[]}
|
|
|
|
* @protected
|
|
|
|
*/
|
|
|
|
protected account_options = [];
|
|
|
|
|
2022-12-15 18:22:19 +01:00
|
|
|
constructor()
|
|
|
|
{
|
|
|
|
super();
|
|
|
|
this.account_options = [];
|
|
|
|
}
|
|
|
|
|
2022-12-09 21:16:44 +01:00
|
|
|
/**
|
|
|
|
* If the value has an account that's not already in the list, check with the server.
|
|
|
|
* We probably don't have all the accounts client side. This is similar to freeEntries,
|
|
|
|
* but a little safer because we don't allow just anything.
|
|
|
|
*
|
|
|
|
* @param {any} new_value
|
|
|
|
*/
|
|
|
|
set value(new_value : string | string[])
|
|
|
|
{
|
|
|
|
super.value = new_value;
|
|
|
|
if(!new_value)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
let val = Array.isArray(this.value) ? this.value : [this.value];
|
2023-01-19 17:21:03 +01:00
|
|
|
|
|
|
|
if(this.isConnected)
|
|
|
|
{
|
|
|
|
this._find_options(val)
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// If not already connected, wait until any provided select_options have been found
|
|
|
|
this.updateComplete.then(() =>
|
|
|
|
{
|
|
|
|
this._find_options(val);
|
|
|
|
this.requestUpdate('select_options');
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_find_options(val)
|
|
|
|
{
|
2022-12-09 21:16:44 +01:00
|
|
|
for(let id of val)
|
|
|
|
{
|
2022-12-12 21:10:53 +01:00
|
|
|
// Don't add if it's already there
|
2023-01-19 17:21:03 +01:00
|
|
|
if(this.select_options.findIndex(o => o.value == id) != -1)
|
2022-12-12 21:10:53 +01:00
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2022-12-09 21:16:44 +01:00
|
|
|
let account_name = null;
|
2023-01-25 21:43:35 +01:00
|
|
|
let option = <SelectOption>{value: "" + id, label: id + " ..."};
|
2022-12-09 21:16:44 +01:00
|
|
|
this.account_options.push(option);
|
2023-01-19 17:21:03 +01:00
|
|
|
if(this.value && (account_name = this.egw().link_title('api-accounts', id, false)))
|
2022-12-09 21:16:44 +01:00
|
|
|
{
|
|
|
|
option.label = account_name;
|
|
|
|
}
|
|
|
|
else if(!account_name)
|
|
|
|
{
|
|
|
|
// Not already cached, need to fetch it
|
|
|
|
this.egw().link_title('api-accounts', id, true).then(title =>
|
|
|
|
{
|
2023-06-26 18:49:25 +02:00
|
|
|
option.label = title || '';
|
2022-12-19 20:00:50 +01:00
|
|
|
this.requestUpdate('select_options');
|
2022-12-09 21:16:44 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
get value()
|
|
|
|
{
|
|
|
|
return super.value;
|
|
|
|
}
|
|
|
|
|
|
|
|
get select_options()
|
|
|
|
{
|
2023-10-06 19:37:11 +02:00
|
|
|
return [...new Map([...this.account_options, ...(super.select_options || [])].map(item =>
|
|
|
|
[item.value, item])).values()];
|
2022-12-09 21:16:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
set select_options(value : SelectOption[])
|
|
|
|
{
|
|
|
|
super.select_options = value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return SelectAccount as unknown as Constructor<SelectAccountInterface> & T;
|
|
|
|
}
|