Change egw.accounts() to always return a Promise

This commit is contained in:
nathan 2022-12-15 10:22:19 -07:00
parent fa1cfe3664
commit e756962f86
5 changed files with 75 additions and 49 deletions

View File

@ -13,6 +13,7 @@ import {et2_IInput} from "../et2_core_interfaces";
import {date} from "../lib/date.js";
import {Et2Button} from "./Et2Button";
import {Et2Tabs} from "../Layout/Et2Tabs/Et2Tabs";
import {SelectOption} from "../Et2Select/FindSelectOptions";
/**
* Class which implements the "et2-button-timestamp" tag
@ -73,17 +74,17 @@ export class Et2ButtonTimestamper extends Et2Button
stamp(event: MouseEvent): boolean
{
const now = new Date(new Date().toLocaleString('en-US', {
timeZone: this.timezone || egw.preference('tz')
timeZone: this.timezone || this.egw().preference('tz')
}));
const format = this.format || egw.preference('dateformat') + ' ' + (egw.preference("timeformat") === "12" ? "h:ia" : "H:i")+' ';
const format = this.format || this.egw().preference('dateformat') + ' ' + (this.egw().preference("timeformat") === "12" ? "h:ia" : "H:i") + ' ';
let text = date(format, now);
// Get properly formatted user name
const user = parseInt(egw.user('account_id'));
egw.accounts('accounts', true).then((accounts) =>
const user = '' + parseInt(this.egw().user('account_id'));
this.egw().accounts('accounts').then((accounts) =>
{
const account = accounts.filter(option => option.value == user)[0];
const account = accounts.filter((option : SelectOption) => option.value == user)[0];
text += account.label + ': ';
const widget = this._get_input(this.target);

View File

@ -11,13 +11,14 @@ import {Et2Select} from "./Et2Select";
import {SelectOption} from "./FindSelectOptions";
import {Et2Image} from "../Et2Image/Et2Image";
import {SelectAccountMixin} from "./SelectAccountMixin";
import {Et2StaticSelectMixin} from "./StaticOptions";
export type AccountType = 'accounts'|'groups'|'both'|'owngroups';
/**
* @customElement et2-select-account
*/
export class Et2SelectAccount extends SelectAccountMixin(Et2Select)
export class Et2SelectAccount extends SelectAccountMixin(Et2StaticSelectMixin(Et2Select))
{
static get properties()
{
@ -45,6 +46,32 @@ export class Et2SelectAccount extends SelectAccountMixin(Et2Select)
this.__accountType = 'accounts';
}
connectedCallback()
{
super.connectedCallback();
// Start fetch of select_options
const type = this.egw().preference('account_selection', 'common');
let fetch = [];
// for primary_group we only display owngroups == own memberships, not other groups
if(type === 'primary_group' && this.accountType !== 'accounts')
{
if(this.accountType === 'both')
{
fetch.push(this.egw().accounts('accounts').then(options => {this.static_options = this.static_options.concat(options)}));
}
fetch.push(this.egw().accounts('owngroups').then(options => {this.static_options = this.static_options.concat(options)}));
}
else
{
fetch.push(this.egw().accounts(this.accountType).then(options => {this.static_options = this.static_options.concat(options)}));
}
this.fetchComplete = Promise.all(fetch)
.then(() => this.requestUpdate("select_options"));
}
firstUpdated(changedProperties?)
{
super.firstUpdated(changedProperties);
@ -76,24 +103,7 @@ export class Et2SelectAccount extends SelectAccountMixin(Et2Select)
return [];
}
let select_options : Array<SelectOption> = [...super.select_options] || [];
// for primary_group we only display owngroups == own memberships, not other groups
if (type === 'primary_group' && this.accountType !== 'accounts')
{
if (this.accountType === 'both')
{
select_options = select_options.concat(this.egw().accounts('accounts'));
}
select_options = select_options.concat(this.egw().accounts('owngroups'));
}
else
{
select_options = select_options.concat(this.egw().accounts(this.accountType));
}
// egw.accounts returns value as number, causing the et2-select to not match the option
select_options.forEach(option =>
{
option.value = option.value.toString();
});
return select_options.filter((value, index, self) =>
{
return self.findIndex(v => v.value === value.value) === index;

View File

@ -37,6 +37,12 @@ export const SelectAccountMixin = <T extends Constructor<LitElement>>(superclass
*/
protected account_options = [];
constructor()
{
super();
this.account_options = [];
}
/**
* 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,
@ -86,7 +92,7 @@ export const SelectAccountMixin = <T extends Constructor<LitElement>>(superclass
get select_options()
{
return [...this.account_options, ...super.select_options];
return [...(this.account_options || []), ...super.select_options];
}
set select_options(value : SelectOption[])

View File

@ -549,9 +549,10 @@ declare interface IegwGlobal
* The list is filtered by type, one of 'accounts','groups','both', 'owngroups'
*
* @param {string} type
* @returns {array}
* @returns {Promise}
*/
accounts(type : "accounts"|"groups"|"both"|"owngroups") : object[];
accounts(type : "accounts" | "groups" | "both" | "owngroups") : Promise<{ value : string, label : string, icon? : string }[]>
/**
* Get account-infos for given numerical _account_ids
*
@ -561,7 +562,7 @@ declare interface IegwGlobal
* @param {function} _callback
* @param {object} _context
*/
accountData(_account_ids : number|number[], _field : string, _resolve_groups : boolean,
accountData(_account_ids : number | number[], _field : string, _resolve_groups : boolean,
_callback : Function, _context : object) : void;
/**
* Set account data. This one can be called from the server to pre-fill the cache.

View File

@ -42,6 +42,9 @@ egw.extend('user', egw.MODULE_GLOBAL, function()
let accountData = {};
let resolveGroup = {};
// Hold in-progress request to avoid making more
let request = null;
return {
/**
* Set data of current user
@ -50,7 +53,7 @@ egw.extend('user', egw.MODULE_GLOBAL, function()
* @param {boolean} _need_clone _data need to be cloned, as it is from different window context
* and therefore will be inaccessible in IE, after that window is closed
*/
set_user: function(_data, _need_clone)
set_user: function (_data, _need_clone)
{
userData = _need_clone ? jQuery.extend(true, {}, _data) : _data;
},
@ -88,39 +91,44 @@ egw.extend('user', egw.MODULE_GLOBAL, function()
* Get a list of accounts the user has access to
* The list is filtered by type, one of 'accounts','groups','both', 'owngroups'
*
* Currently the list is queried once synchronous from the server by the first et2_selectAccount.
*
* @param {string} type
* @param {bool} async true: return Promise
* @returns {Array|Promise}
* @returns {Promise<{value:string,label:string,icon?:string}[]>}
*/
accounts: function(type, async)
accounts: function (type)
{
if (typeof type === 'undefined') type = 'accounts';
if (typeof type === 'undefined')
{
type = 'accounts';
}
if(jQuery.isEmptyObject(accountStore))
if (request !== null)
{
return request.then(() =>
{
debugger;
return this.accounts(type)
});
}
if (jQuery.isEmptyObject(accountStore))
{
const cache_it = data =>
{
let types = ["accounts", "groups", "owngroups"];
for(let t of types)
for (let t of types)
{
if(typeof data[t] === "object")
if (typeof data[t] === "object")
{
accountStore[t] = jQuery.extend(true, [], data[t]||[]);
accountStore[t] = jQuery.extend(true, [], data[t] || []);
}
}
}
if (async)
request = egw.request("EGroupware\\Api\\Framework::ajax_user_list", []).then(_data =>
{
return egw.request("EGroupware\\Api\\Framework::ajax_user_list",[]).then(_data =>
{
cache_it(_data);
return this.accounts(type);
});
}
// Synchronous
egw.json("EGroupware\\Api\\Framework::ajax_user_list",[], cache_it, this, false).sendRequest(false);
cache_it(_data);
request = null;
return this.accounts(type);
});
return request;
}
let result = [];
if (type === 'both')
@ -131,7 +139,7 @@ egw.extend('user', egw.MODULE_GLOBAL, function()
{
result = [].concat(accountStore[type]);
}
return async ? Promise.resolve(result) : result;
return Promise.resolve(result);
},
/**