inspector-component/inspector.js

  1. /**
  2. * CauldronInspector
  3. * Visual inspector of various elements that can be edited in Cauldron
  4. *
  5. * Copyright 2020, 2021 Rolf Bagge, Janus B. Kristensen, CAVI,
  6. * Center for Advanced Visualization and Interaction, Aarhus University
  7. *
  8. * Licensed under the Apache License, Version 2.0 (the "License");
  9. * you may not use this file except in compliance with the License.
  10. * You may obtain a copy of the License at
  11. *
  12. * http://www.apache.org/licenses/LICENSE-2.0
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS,
  15. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. **/
  19. /**
  20. * Visual inspector of various elements that can be edited in Cauldron
  21. * @type type
  22. */
  23. class CauldronInspector {
  24. constructor(){
  25. let self = this;
  26. this.html = document.createElement("div");
  27. this.html.classList.add("cauldron-inspector");
  28. this.fields = document.createElement("div");
  29. this.fields.classList.add("cauldron-inspector-fields");
  30. this.html.appendChild(this.fields);
  31. this.currentSelection = null;
  32. this.currentInspectElements = [];
  33. EventSystem.registerEventCallback("TreeBrowser.Selection", ({detail: {selection: selection}})=>{
  34. self.inspect(selection);
  35. });
  36. }
  37. /**
  38. *
  39. * @param {TreeNode} selection
  40. */
  41. inspect(selection) {
  42. let self = this;
  43. this.currentSelection = selection;
  44. this.html.scrollTo(0, 0);
  45. let inspectorElements = null;
  46. for(let inspector of CauldronInspector.contentBindings) {
  47. inspectorElements = inspector.contentBinding.inspect(selection, this);
  48. if(inspectorElements != null) {
  49. //First inspector to return something wins.
  50. break;
  51. }
  52. }
  53. //Remove old elements
  54. this.currentInspectElements.forEach((inspectElement)=>{
  55. inspectElement.destroy();
  56. });
  57. this.currentInspectElements = [];
  58. if(inspectorElements != null) {
  59. inspectorElements.forEach((element)=>{
  60. self.fields.append(element.html);
  61. self.currentInspectElements.push(element);
  62. });
  63. }
  64. }
  65. reinspect(){
  66. this.inspect(this.currentSelection);
  67. }
  68. /**
  69. * Register a content binding for the CauldronInspector component
  70. * @param contentBinding
  71. * @param {Number} priority
  72. */
  73. static registerContentBinding(contentBinding, priority) {
  74. CauldronInspector.contentBindings.push({
  75. contentBinding: contentBinding,
  76. priority: priority
  77. });
  78. //Sort decorators according to priority
  79. CauldronInspector.contentBindings.sort((i1, i2)=>{
  80. return i2.priority - i1.priority;
  81. });
  82. }
  83. };
  84. CauldronInspector.contentBindings = [];
  85. window.Cauldron.CauldronInspector = CauldronInspector;
  86. class InspectorSegment {
  87. constructor(name, parentList){
  88. this.html = document.createElement("div");
  89. this.html.classList.add("cauldron-inspector-section");
  90. let sectionHeader = document.createElement("div");
  91. sectionHeader.classList.add("cauldron-inspector-header");
  92. sectionHeader.innerText = name;
  93. this.html.appendChild(sectionHeader);
  94. this.parentList = parentList;
  95. }
  96. push(element){
  97. this.parentList.push(element);
  98. }
  99. destroy() {
  100. this.html.remove();
  101. }
  102. }
  103. window.Cauldron.InspectorSegment = InspectorSegment;
  104. class InspectorElement {
  105. constructor() {
  106. this._html = document.createElement("div");
  107. this._html.classList.add("cauldron-inspector-element");
  108. }
  109. setFailing(failing){
  110. if (failing){
  111. if (!this._html.classList.contains("failing-element")) this._html.classList.toggle("failing-element");
  112. } else {
  113. if (this._html.classList.contains("failing-element")) this._html.classList.toggle("failing-element");
  114. }
  115. }
  116. get html() {
  117. return this._html;
  118. }
  119. destroy() {
  120. this.html.remove();
  121. }
  122. }
  123. window.Cauldron.InspectorElement = InspectorElement;
  124. class InspectorHTMLElement extends InspectorElement {
  125. constructor(html) {
  126. super();
  127. this.html.appendChild(html);
  128. }
  129. }
  130. window.Cauldron.InspectorHTMLElement = InspectorHTMLElement;