Files
raven-client/ayanova/src/api/enums.js
2021-08-05 20:16:27 +00:00

98 lines
2.9 KiB
JavaScript

/* ZZeslint-disable */
export default {
get(enumKey, enumValue) {
enumKey = enumKey.toLowerCase();
if (enumKey != "authorizationroles") {
if (window.$gz.store.state.enums[enumKey] == undefined) {
throw new Error(
"ERROR enums::get -> enumKey " + enumKey + " is missing from store"
);
}
return window.$gz.store.state.enums[enumKey][enumValue];
} else {
let ret = [];
if (enumValue == null || enumValue == 0) {
return "";
}
let availableRoles = this.getSelectionList("AuthorizationRoles");
for (let i = 0; i < availableRoles.length; i++) {
let role = availableRoles[i];
if (!!(enumValue & role.id)) {
ret.push(role.name);
}
}
return ret.join(", ");
}
},
//////////////////////////////////
//
// Used by forms to fetch selection list data
// Sorts alphabetically by default but can be turned off with do not sort
//
getSelectionList(enumKey, noSort) {
enumKey = enumKey.toLowerCase();
let e = window.$gz.store.state.enums[enumKey];
if (!e) {
throw new Error(
"ERROR enums::getSelectionList -> enumKey " +
enumKey +
" is missing from store"
);
}
let ret = [];
//turn it into an array suitable for selection lists
for (const [key, value] of Object.entries(e)) {
ret.push({ id: Number(key), name: value });
}
//sort by name
if (!noSort) {
ret.sort(window.$gz.util.sortByKey("name"));
}
return ret;
},
///////////////////////////////////
//
// Fetches enum list from server
// and puts in store. if necessary
// ACCEPTS an ARRAY or a single STRING KEY
//
async fetchEnumList(enumKey) {
if (!Array.isArray(enumKey)) {
enumKey = [enumKey];
}
for (let i = 0; i < enumKey.length; i++) {
//check if list
//if not then fetch it and store it
let k = enumKey[i].toLowerCase();
//de-lodash
// if (!window.$gz. _.has(window.$gz.store.state.enums, k)) {
//enums is an object this is checking if that object has a key with the name in k
if (!window.$gz.util.has(window.$gz.store.state.enums, k)) {
let that = this;
// eslint-disable-next-line
let dat = await that.fetchEnumKey(k);
//massage the data as necessary
let e = { enumKey: k, items: {} };
for (let i = 0; i < dat.length; i++) {
let o = dat[i];
e.items[o.id] = o.name;
}
//stuff the data into the store
window.$gz.store.commit("setEnum", e);
}
}
},
async fetchEnumKey(enumKey) {
// eslint-disable-next-line
let res = await window.$gz.api.get("enum-list/list/" + enumKey);
//We never expect there to be no data here
if (!res.hasOwnProperty("data")) {
return Promise.reject(res);
}
return res.data;
}
};