Skip to content

Commit

Permalink
Allow to mix structures and instances
Browse files Browse the repository at this point in the history
  • Loading branch information
shuart committed Jan 3, 2024
1 parent 2d2fad0 commit 8c42e6c
Show file tree
Hide file tree
Showing 2 changed files with 155 additions and 15 deletions.
120 changes: 119 additions & 1 deletion src/js/modules/common_project_management/structures_management.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,123 @@ var customRepoMethods = function (projectStore,createAggregate) {
return toReturn
}

repo.link = function (sourceId, targetId) {
repo.getAllChildrenOfId = function (sourceId, transformFunction) {
var rootElement = projectStore.get("structures").where("uuid").equals(sourceId)
var root = {
uuid:sourceId ,
element:rootElement || "root is not a structure",
_children:[]
}
var list =[root]
var toReturn = []
var sourceToChildMapping ={}
var childToSourceMapping ={}
var instanceMapping ={}
var structureMapping ={}

var currentHierarchies = projectStore.get("hierarchies").toArray()
var currentStructures = projectStore.get("structures").toArray()
var currentInstances = projectStore.get("instances").toArray()
for (let i = 0; i < currentInstances.length; i++) {
instanceMapping[currentInstances[i].uuid] = currentInstances[i];
}
for (let i = 0; i < currentStructures.length; i++) {
// const structure = currentStructures[i];
// if (childToSourceMapping[structure.uuid] == sourceId) {
// root.children.push(structure)
// }
structureMapping[currentStructures[i].uuid] = currentStructures[i];
}

var recursiveSearch = function (root, currentHierarchies, instanceMapping, structureMapping) {
for (let i = 0; i < currentHierarchies.length; i++) {
const element = currentHierarchies[i];
// sourceToChildMapping[element.from] = element.to
// childToSourceMapping[element.to] = element.from
if (element.from == root.uuid) {
var hasStructure = structureMapping[element.to]
var hasInstance = instanceMapping[element.to]
if (hasStructure) {

var newRoot = {
uuid:hasStructure.uuid,
element:hasStructure,
_isStructure:true,
_children:[]
}
if (transformFunction) {
var childrenReference = newRoot._children //keep original children reference for populating
newRoot =transformFunction(newRoot)
newRoot._children = childrenReference
}
root._children.push(newRoot)
list.push(newRoot)
recursiveSearch(newRoot, currentHierarchies, instanceMapping, structureMapping)
}
if (hasInstance) {
var newRoot = {
uuid:hasInstance.uuid,
element:hasInstance,
_isInstance:true,
// _children:[]
}
if (transformFunction) {
// var childrenReference = newRoot._children //keep original children reference for populating
newRoot =transformFunction(newRoot)
// newRoot._children = childrenReference

}
root._children.push(newRoot)
list.push(newRoot)
// recursiveSearch(newRoot, currentHierarchies, instanceMapping, structureMapping)
}

}

}
}

recursiveSearch(root, currentHierarchies, instanceMapping, structureMapping)


return {root, list}
}

repo.link = function (sourceId, targetId, context) {
var linkType = "STS";
var currentRelationTarget = projectStore.get("structures").where("uuid").equals(targetId)
if (!currentRelationTarget) { //if not a structure
currentRelationTarget = projectStore.get("instances").where("uuid").equals(targetId)
linkType = "STI";
}
var currentRelationSource = projectStore.get("structures").where("uuid").equals(sourceId)
if (!currentRelationSource) {//if not a structure
currentRelationSource = projectStore.get("instances").where("uuid").equals(targetId)
linkType = "ITS";
}
if (currentRelationTarget && currentRelationSource) {
//first remove existing relation
var existingRelations = projectStore.get("hierarchies").where("to").equals(currentRelationTarget.uuid)
if (existingRelations) {
console.log(existingRelations);
for (let i = 0; i < existingRelations.length; i++) {
if (!context) {
projectStore.remove("hierarchies",existingRelations[i].uuid)
}else if (context && existingRelations[i].context == context) {
projectStore.remove("hierarchies",existingRelations[i].uuid)
}

}

}
projectStore.add("hierarchies",{name:`from ${currentRelationSource.name} to ${currentRelationSource.name}`, from:currentRelationSource.uuid, to:currentRelationTarget.uuid, context:context ||undefined, type:"linkType"}) //STS Structure to structure

}else{
console.warn("Missing source or target")
}
}

repo.linkLegacy = function (sourceId, targetId) {
var currentRelationTarget = projectStore.get("structures").where("uuid").equals(targetId)
var currentRelationSource = projectStore.get("structures").where("uuid").equals(sourceId)
if (currentRelationTarget && currentRelationSource) {
Expand All @@ -192,6 +308,8 @@ var customRepoMethods = function (projectStore,createAggregate) {
return repo
}



var createStructuresManagement = function () {
return createRepoManagement(projectManagement.getCurrent().id, 'structures', structureAggregate, customRepoMethods)
}
Expand Down
50 changes: 36 additions & 14 deletions src/js/modules/tools_collections/tools_collections.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,18 @@ var showCollections = function (self) {
}
}

var traverseStructures = function (rootId, structuresRep, listToPush) {
var structuresList = structuresRep.getChildrenOfId(rootId)
for (let i = 0; i < structuresList.length; i++) {
const element = structuresList[i];
var data={uuid:element.uuid, name:element.name,_children:[]}
// if (element.type == undefined) {
data.img = "./img/icons/folder.svg"
// }
traverseStructures(element.uuid, structuresRep,data._children )
listToPush.push(data)
}
}
// var traverseStructures = function (rootId, structuresRep, listToPush) {
// var structuresList = structuresRep.getChildrenOfId(rootId)
// for (let i = 0; i < structuresList.length; i++) {
// const element = structuresList[i];
// var data={uuid:element.uuid, name:element.name,_children:[]}
// // if (element.type == undefined) {
// data.img = "./img/icons/folder.svg"
// // }
// traverseStructures(element.uuid, structuresRep,data._children )
// listToPush.push(data)
// }
// }

var loadSideMenu = function (self) {
self.query(".collection_side_view").innerHTML='' //TODO, update the componenet properly
Expand All @@ -56,7 +56,15 @@ var loadSideMenu = function (self) {
folderComponent.onDropped = function (evt) {
console.log(evt);
var repo = createStructuresManagement()
repo.link(evt.target.data.uuid,evt.dragged.data.uuid )
if (confirm()) {
if (evt.target.isContainer && evt.dragged.isContainer) {
repo.link(evt.target.data.uuid,evt.dragged.data.uuid )
}
if (evt.target.isContainer && (!evt.dragged.isContainer) ) {
repo.link(evt.target.data.uuid,evt.dragged.data.uuid, self.instanceId ) //provide a context to make this relation unique to it
}
}

loadSideMenu(self)
}
folderComponent.list = [
Expand All @@ -70,7 +78,21 @@ var loadSideMenu = function (self) {
var structuresRep = createStructuresManagement()
console.log(structuresRep.getHierarchies())
// var structuresList = structuresRep.getAll()
traverseStructures(self.instanceId,structuresRep, folderComponent.list)
var hierachStruct = structuresRep.getAllChildrenOfId(self.instanceId, function (item) {
var img="./img/icons/folder.svg"
var children = item._children
if (item._isInstance) {
img="./img/icons/file.svg"
children = undefined //clear children if needed
}
return { uuid:item.element.uuid, name:item.element.name, _children:children, img}
})
console.log(hierachStruct);
alert("eejjkjl")
var listOfStruct = hierachStruct.list
var struct = hierachStruct.root
folderComponent.list= folderComponent.list.concat( hierachStruct.root._children )
// traverseStructures(self.instanceId,structuresRep, folderComponent.list)


self.query(".collection_side_view").append(folderComponent)
Expand Down

0 comments on commit 8c42e6c

Please sign in to comment.