This repository has been archived by the owner on Jan 24, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 131
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3d42118
commit d8d4476
Showing
20 changed files
with
194 additions
and
177 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
101 changes: 101 additions & 0 deletions
101
src/frontend/app/store/helpers/entity-relations.tree.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
import { schema } from 'normalizr'; | ||
|
||
import { | ||
createEntityRelationPaginationKey, | ||
EntityInlineParentAction, | ||
EntityTreeRelation, | ||
ValidationResult, | ||
EntityTree, | ||
createEntityRelationKey, | ||
} from './entity-relations.types'; | ||
|
||
const entityTreeCache: { | ||
[entityKey: string]: EntityTree | ||
} = {}; | ||
|
||
function generateCacheKey(entityKey: string, action: EntityInlineParentAction): string { | ||
let includeRelations = action.includeRelations || []; | ||
includeRelations = includeRelations.sort((a, b) => { | ||
return a.localeCompare(b); | ||
}); | ||
return entityKey + '+' + includeRelations.join(','); | ||
} | ||
|
||
export function fetchEntityTree(action: EntityInlineParentAction): EntityTree { | ||
let entity = action.entity; | ||
const isArray = entity['length'] > 0; | ||
entity = isArray ? entity[0] : entity; | ||
const entityKey = entity['key']; | ||
const cacheKey = generateCacheKey(entityKey, action); | ||
let entityTree = entityTreeCache[cacheKey]; | ||
if (!entityTree) { | ||
const rootEntityRelation = new EntityTreeRelation( | ||
entityKey, | ||
entity as schema.Entity, | ||
isArray, | ||
null, | ||
'', | ||
new Array<EntityTreeRelation>() | ||
); | ||
entityTree = { | ||
rootRelation: rootEntityRelation, | ||
requiredParamNames: new Array<string>(), | ||
}; | ||
createEntityTree(entityTree, rootEntityRelation); | ||
entityTreeCache[cacheKey] = entityTree; | ||
// console.log('fetchEntity: Not Found'); | ||
} else { | ||
// console.log('fetchEntity: Found'); | ||
} | ||
// Calc max depth and exclude not needed | ||
entityTree.rootRelation.childRelations = parseEntityTree(entityTree, entityTree.rootRelation, action.includeRelations); | ||
return entityTree; | ||
} | ||
|
||
export function createEntityTree(tree: EntityTree, entityRelation: EntityTreeRelation, schemaObj?, path: string = '') { | ||
const rootEntitySchema = schemaObj || entityRelation.entity['schema']; | ||
Object.keys(rootEntitySchema).forEach(key => { | ||
let value = rootEntitySchema[key]; | ||
const isArray = value['length'] > 0; | ||
value = isArray ? value[0] : value; | ||
|
||
const newPath = path ? path + '.' + key : key; | ||
if (value instanceof schema.Entity) { | ||
const newEntityRelation = new EntityTreeRelation( | ||
value.key, | ||
value, | ||
isArray, | ||
key, | ||
newPath, | ||
new Array<EntityTreeRelation>() | ||
); | ||
entityRelation.childRelations.push(newEntityRelation); | ||
createEntityTree(tree, newEntityRelation, null, ''); | ||
} else if (value instanceof Object) { | ||
createEntityTree(tree, entityRelation, value, newPath); | ||
} | ||
}); | ||
} | ||
|
||
export function parseEntityTree(tree: EntityTree, entityRelation: EntityTreeRelation, includeRelations: string[] = []) | ||
: EntityTreeRelation[] { | ||
const newChildRelations = new Array<EntityTreeRelation>(); | ||
entityRelation.childRelations.forEach((relation, index, array) => { | ||
const parentChildKey = createEntityRelationKey(entityRelation.entityKey, relation.entityKey); | ||
if (includeRelations.indexOf(parentChildKey) >= 0) { | ||
const clone = { ...relation }; | ||
newChildRelations.push(clone); | ||
if (tree.requiredParamNames.indexOf(relation.paramName) < 0) { | ||
tree.requiredParamNames.push(relation.paramName); | ||
} | ||
clone.childRelations = parseEntityTree(tree, relation, includeRelations); | ||
} | ||
}); | ||
entityRelation.childRelations = newChildRelations; | ||
if (entityRelation.childRelations.length) { | ||
tree.maxDepth = tree.maxDepth || 0; | ||
tree.maxDepth++; | ||
} | ||
return newChildRelations; | ||
} | ||
|
Oops, something went wrong.