From e3c5fc6ec35bd2b3a38da58180c41c15912f348d Mon Sep 17 00:00:00 2001 From: florian <f.spreckelsen@inidscale.com> Date: Tue, 29 Nov 2022 14:12:14 +0100 Subject: [PATCH] ENH: Hide properties by default, unhide if appliccable --- src/core/js/ext_prop_display.js | 59 +++++++++++++++++++-------------- src/core/xsl/entity.xsl | 3 +- 2 files changed, 37 insertions(+), 25 deletions(-) diff --git a/src/core/js/ext_prop_display.js b/src/core/js/ext_prop_display.js index a17d3b07..ee5c40ea 100644 --- a/src/core/js/ext_prop_display.js +++ b/src/core/js/ext_prop_display.js @@ -52,29 +52,38 @@ var prop_display = new function ($, getEntityName, getPropertyElements, getPrope return $(".caosdb-entity-panel,.caosdb-entity-preview") } - this._hide_properties = function (entities, conf, typesWithChildren) { + this._unhide_properties = function (entities, conf, allTypes) { 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"); - } + for (let ent of entities) { + let parents = getParents(ent).map(par => par.name); + let properties = getPropertyElements(ent); + if (parents.some(par => allTypes.allTypesOrChildren.includes(par))) { + console.log(parents); + // we know that there is at least one rule for this type (it is + // in `allTypes.allTypesOrChildren`), but we don't know which tp + // apply yet. + for (let typeName of Object.keys(conf)) { + let typeConf = conf[typeName]; + let allNames = allTypes.typesWithChildren[typeName]; + // only hide something if there is a match in at least one parent type + if (parents.some(par => allNames.includes(par))) { + properties.forEach((prop, index) => { + if (this._hide_property(getPropertyName(prop), userName, userRoles, typeConf)) { + console.log(prop); + // Should be hidden by default but better safe than sorry + $(prop).addClass("caosdb-v-hidden-property").removeClass("caosdb-v-show-property"); + } else { + // show this property + $(prop).addClass("caosdb-v-show-property").removeClass("caosdb-v-hidden-property"); + } + }); } } + } else { + // no rules for this RecordType, so show all properties + properties.forEach((prop, index) => $(prop).addClass("caosdb-v-show-property").removeClass("caosdb-v-hidden-property")); } } } @@ -96,9 +105,6 @@ var prop_display = new function ($, getEntityName, getPropertyElements, getPrope 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 } @@ -114,22 +120,27 @@ var prop_display = new function ($, getEntityName, getPropertyElements, getPrope const parentTypes = Object.keys(conf); var typesWithChildren = {}; + var allTypesOrChildren = []; for (let parentName of parentTypes) { const children = await query(`FIND RECORDTYPE "${parentName}"`); const names = children.map(ent => getEntityName(ent)); typesWithChildren[parentName] = names; + allTypesOrChildren = allTypesOrChildren.concat(names); } - return typesWithChildren; + return { + "typesWithChildren": typesWithChildren, + "allTypesOrChildren": allTypesOrChildren + }; } this.init = async function () { console.log("initializing ext_prop_display.js"); const conf = await this.load_config(); - const typesWithChildren = await this._getRecordTypes(conf); + const allTypes = await this._getRecordTypes(conf); var entities = this._get_entities_in_view(); - this._hide_properties(entities, conf, typesWithChildren) + this._unhide_properties(entities, conf, allTypes) } }($, getEntityName, getPropertyElements, getPropertyName, getUserName, getUserRoles, log.getLogger("ext_prop_display"), load_config, query); diff --git a/src/core/xsl/entity.xsl b/src/core/xsl/entity.xsl index 82de9e41..bcb9aacd 100644 --- a/src/core/xsl/entity.xsl +++ b/src/core/xsl/entity.xsl @@ -232,7 +232,8 @@ </xsl:template> <!-- PROPERTIES --> <xsl:template match="Property" mode="entity-body"> - <li class="list-group-item caosdb-v-property-row caosdb-f-entity-property"> + <!-- all properties are now hidden by default --> + <li class="list-group-item caosdb-v-property-row caosdb-f-entity-property caosdb-v-hidden-property"> <xsl:attribute name="id"> <xsl:value-of select="generate-id()"/> </xsl:attribute> -- GitLab