cluedin-widget
Version:
This project contains all the pages needed for browsing entities and searching them. The aim is to replace the CluedIn.Webapp project with this one when all the pages ( including the Admin page ) will be ported to REACT.
144 lines (110 loc) • 4.79 kB
JavaScript
import { createPropertiesFromPromoted } from './generic'
import arrayHelper from '../../array';
var propertyConfiguration = require('./propertyConfiguration');
var validationHelper = require('../../validation');
var collection = require('../../collection');
var socialProperties = require('./propertyConfiguration/social');
var stringHelper = require('../../string');
export function getPropertyToKeep(entity) {
var properties = Object.assign(entity.data.properties, {});
var result = [];
var presentProperties = [];
if (!properties) {
return result;
}
var extractManagedProperties = (value) => {
return function (propertyConf) {
if (validationHelper.is.a.string(propertyConf)) {
if (propertyConf === value) {
presentProperties.push(propertyConf);
}
}
if (validationHelper.is.an.object(propertyConf) && !propertyConf.either) {
if (propertyConf.value === value) {
presentProperties.push(value);
}
}
if (validationHelper.is.an.object(propertyConf) && propertyConf.either) {
propertyConf.either.forEach(function (subPropertyConf) {
if (subPropertyConf.value === value) {
presentProperties.push(value);
}
});
}
};
};
var propertiesConfiguration = propertyConfiguration[entity.data.entityType];
if (!propertiesConfiguration) {
return properties;
}
//loop through important and all, if not present mark them as valid
var allPropertyKeys = Object.keys(properties);
allPropertyKeys.forEach(function (value) {
collection.find(propertiesConfiguration.all || [], extractManagedProperties(value));
collection.find(propertiesConfiguration.fold || [], extractManagedProperties(value));
collection.find(propertiesConfiguration.important || [], extractManagedProperties(value));
});
presentProperties.forEach(function (presentKey) {
delete properties[presentKey];
});
//TODO: remove hack when backend filters things properly
// for now remove offensive stuff
delete properties['attribute-type'];
for (var key in properties) {
if (properties.hasOwnProperty(key)) {
var name = stringHelper.createPropertyDisplayName(key);
var value = properties[key];
result.push({
isHtml: false,
name: name,
value: value,
original: value,
key: stringHelper.formatDisplayName(name).toLowerCase(),
displayName: stringHelper.formatDisplayName(name)
});
}
}
return result;
}
export function extractImportant(entity) {
var properties = entity.data.properties;
if (!properties || properties.length === 0) {
return [];
}
var propertiesConfiguration = propertyConfiguration[entity.data.entityType];
return createPropertiesFromPromoted(properties, propertiesConfiguration ? propertiesConfiguration.important : [], 'important');
}
export function extract(entity) {
var properties = entity.data.properties;
if (!properties || properties.length === 0) {
return [];
}
var importantProperties = extractImportant(entity);
var propertiesConfiguration = propertyConfiguration[entity.data.entityType];
var extendedProperties = arrayHelper.compact(createPropertiesFromPromoted(properties, propertiesConfiguration ? propertiesConfiguration.all : [],'all'));
var foldedProperties = arrayHelper.compact(createPropertiesFromPromoted(properties, propertiesConfiguration && propertiesConfiguration.fold ? propertiesConfiguration.fold : [], 'fold'));
return importantProperties.concat(extendedProperties).concat(foldedProperties);
}
export function extractSocial(properties) {
var result = [];
if (!properties || properties.length === 0) {
return [];
}
for (var social in socialProperties) {
var propertyKey = socialProperties[social];
if (validationHelper.is.a.string(propertyKey)) {
var value = properties[propertyKey];
if (value) {
result.push({ key: social, value: value });
}
} else if (validationHelper.is.an.array(propertyKey)) {
var valueFromArray = collection.find(propertyKey, function (p) {
return properties[p];
});
if (valueFromArray && properties[valueFromArray]) {
result.push({ key: social, value: properties[valueFromArray] });
}
}
}
return result;
}