diff --git a/src/core/css/webcaosdb.css b/src/core/css/webcaosdb.css index 1c8f50be049d308efcac25e41d705b08d96d21e6..2d26538254923437d34b05dde3d02578d54a1a34 100644 --- a/src/core/css/webcaosdb.css +++ b/src/core/css/webcaosdb.css @@ -381,6 +381,10 @@ h5 { margin-right: 0px; } +.caosdb-v-hidden-property { + display: None; +} + .caosdb-v-edit-drag { padding: 5px; } diff --git a/src/core/js/ext_prop_display.js b/src/core/js/ext_prop_display.js index 0b706b0580760806fdade3bf25c6de3a8784b863..a17d3b0740b5ff41b9d351c1fb05173c3c562993 100644 --- a/src/core/js/ext_prop_display.js +++ b/src/core/js/ext_prop_display.js @@ -25,7 +25,7 @@ * @requires log (singleton from loglevel library) * @requires load_config (function from webcaosdb.js) */ -var prop_display = new function ($, logger, load_config) { +var prop_display = new function ($, getEntityName, getPropertyElements, getPropertyName, getUserName, getUserRoles, logger, load_config, query) { /** * Return the property-display config file; `ext_prop_display.json` by @@ -47,10 +47,90 @@ var prop_display = new function ($, logger, load_config) { return conf; } - this.init = async function() { + this._get_entities_in_view = function () { + // Use all entities, both in entity panel and in preview. + return $(".caosdb-entity-panel,.caosdb-entity-preview") + } + + this._hide_properties = function (entities, conf, typesWithChildren) { + + const userName = getUserName(); + const userRoles = getUserRoles(); + for (let typeName of Object.keys(conf)) { + let typeConf = conf[typeName]; + let allNames = typesWithChildren[typeName]; + for (let ent of entities) { + let parents = getParents(ent).map(par => par.name); + + // only hide something if there is a match in at least one parent type + if (parents.some(par => allNames.includes(par))) { + let properties = getPropertyElements(ent); + for (let prop of properties) { + if (this._hide_property(getPropertyName(prop), userName, userRoles, typeConf)) { + console.log(prop); + $(prop).toggleClass("caosdb-v-hidden-property"); + } + else { + $(prop).toggleClass("caosdb-v-show-property"); + } + } + } + } + } + } + + this._hide_property = function (propname, userName, userRoles, conf) { + + // is this property only shown for certain users/groups? + if ((conf.show != undefined) && conf.show.length > 0) { + for (let def of conf.show) { + if (propname.toLowerCase() == def.name.toLowerCase()) { + if (!(def.users.includes(userName)) && !(userRoles.some(role => def.roles.includes(role)))) { + return true + } + } + } + } + + // is this property hidden for certain users/groups? + if ((conf.hide != undefined) && conf.hide.length > 0) { + for (let def of conf.hide) { + if (propname.toLowerCase() == def.name.toLowerCase()) { + console.log(propname); + console.log(`${userName} - ${def.users}`); + console.log(`${userRoles} - ${def.roles}`); + if (def.users.includes(userName) || userRoles.some(role => def.roles.includes(role))) { + return true + } + } + } + } + + return false; + } + + this._getRecordTypes = async function (conf) { + + const parentTypes = Object.keys(conf); + + var typesWithChildren = {}; + + for (let parentName of parentTypes) { + const children = await query(`FIND RECORDTYPE "${parentName}"`); + const names = children.map(ent => getEntityName(ent)); + typesWithChildren[parentName] = names; + } + + return typesWithChildren; + } + + this.init = async function () { console.log("initializing ext_prop_display.js"); const conf = await this.load_config(); + const typesWithChildren = await this._getRecordTypes(conf); + var entities = this._get_entities_in_view(); + this._hide_properties(entities, conf, typesWithChildren) } -}($, log.getLogger("ext_prop_display"), load_config); +}($, getEntityName, getPropertyElements, getPropertyName, getUserName, getUserRoles, log.getLogger("ext_prop_display"), load_config, query); $(document).ready(() => caosdb_modules.register(prop_display));