integration/cauldron/ConceptTreeGenerator.js

  1. /**
  2. * ConceptDecorator - decorates Concept nodes in TreeBrowser trees
  3. *
  4. * This code is licensed under the MIT License (MIT).
  5. *
  6. * Copyright 2020, 2021, 2022 Rolf Bagge, Janus B. Kristensen, CAVI,
  7. * Center for Advanced Visualization and Interaction, Aarhus University
  8. *
  9. * Permission is hereby granted, free of charge, to any person obtaining a copy
  10. * of this software and associated documentation files (the “Software”), to deal
  11. * in the Software without restriction, including without limitation the rights
  12. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. * copies of the Software, and to permit persons to whom the Software is
  14. * furnished to do so, subject to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be included in
  17. * all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. * THE SOFTWARE.
  26. *
  27. */
  28. /**
  29. *
  30. */
  31. class ConceptTreeGenerator extends TreeGenerator {
  32. constructor(parentNode) {
  33. super();
  34. this.rootNode = new TreeNode({
  35. type: "ConceptRootNode",
  36. context: null
  37. });
  38. TreeGenerator.decorateNode(this.rootNode);
  39. parentNode.addNode(this.rootNode);
  40. }
  41. onAddDatastore(datastore){
  42. let activeElement = document.activeElement;
  43. let self = this;
  44. let datastoreNode = new TreeNode({
  45. type: "DatastoreNode",
  46. lookupKey: datastore.name,
  47. context: datastore
  48. });
  49. TreeGenerator.decorateNode(datastoreNode);
  50. this.rootNode.addNode(datastoreNode);
  51. this.rootNode.unfold();
  52. datastore.registerDestroyCallback(()=>{
  53. self.destroyNode(datastoreNode);
  54. });
  55. // Concepts
  56. datastore.registerConceptAddedCallback((concept)=>{
  57. datastoreNode.unfold();
  58. let conceptNode = new TreeNode({
  59. type: "ConceptNode",
  60. lookupKey: concept.name,
  61. context: concept
  62. });
  63. // Instances
  64. let instanceAddedCallback = datastore.registerConceptInstanceAddedCallback(concept, (uuid)=>{
  65. let node = self.addInstanceNode(datastore, conceptNode, concept, uuid);
  66. let instanceRemovedCallback = datastore.registerConceptInstanceRemovedCallback(concept, (removedUUID)=>{
  67. if (removedUUID===uuid){
  68. self.destroyNode(node);
  69. }
  70. });
  71. node.cleanup = [()=>{
  72. datastore.removeConceptInstanceRemovedCallback(concept, instanceRemovedCallback);
  73. }];
  74. });
  75. TreeGenerator.decorateNode(conceptNode);
  76. datastoreNode.addNode(conceptNode);
  77. });
  78. // STUB: Concepts cannot be removed right now but should be cleaned up here
  79. // datastore.removeConceptInstanceAddedCallback(concept, instanceAddedCallback);
  80. }
  81. destroyNode(node){
  82. node.parentNode.removeNode(node);
  83. // Run cleanup
  84. if (node.cleanup){
  85. for (let entry of node.cleanup){
  86. entry();
  87. }
  88. }
  89. // Destroy children
  90. for (let child of Array.from(node.childNodes)){ // copy to avoid concurrent mods
  91. this.destroyNode(child);
  92. }
  93. }
  94. addInstanceNode(datastore, parentNode, concept, uuid){
  95. let instanceNode = new TreeNode({
  96. type: "ConceptInstanceNode",
  97. lookupKey: uuid,
  98. context: {concept, uuid, datastore}
  99. });
  100. TreeGenerator.decorateNode(instanceNode);
  101. parentNode.addNode(instanceNode);
  102. return instanceNode;
  103. }
  104. }
  105. EventSystem.registerEventCallback("Cauldron.TreeBrowserSpawned", ({detail: {root: rootNode}})=>{
  106. let generator = new ConceptTreeGenerator(rootNode);
  107. EventSystem.triggerEvent("Varv.ConceptTreeGeneratorSpawned", generator);
  108. window.ConceptTreeGenerator.instances.push(generator);
  109. });
  110. window.ConceptTreeGenerator = ConceptTreeGenerator;
  111. window.ConceptTreeGenerator.instances = [];