PropertyTab.ts 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. import { AbstractMesh, Nullable } from "babylonjs";
  2. import { DetailPanel } from "../details/DetailPanel";
  3. import { SearchBar } from "../gui/SearchBar";
  4. import { Helpers } from "../helpers/Helpers";
  5. import { Inspector } from "../Inspector";
  6. import { TreeItem } from "../tree/TreeItem";
  7. import { Tab } from "./Tab";
  8. import { TabBar } from "./TabBar";
  9. import * as Split from "Split";
  10. /**
  11. * A Property tab can creates two panels:
  12. * a tree panel and a detail panel,
  13. * in which properties will be displayed.
  14. * Both panels are separated by a resize bar
  15. */
  16. export abstract class PropertyTab extends Tab {
  17. protected _inspector: Inspector;
  18. /** The panel containing a list of items */
  19. protected _treePanel: HTMLDivElement;
  20. /** The panel containing a list if properties corresponding to an item */
  21. protected _detailsPanel: DetailPanel;
  22. protected _treeItems: Array<TreeItem> = [];
  23. protected _searchBar: SearchBar;
  24. constructor(tabbar: TabBar, name: string, insp: Inspector) {
  25. super(tabbar, name);
  26. this._inspector = insp;
  27. // Build the properties panel : a div that will contains the tree and the detail panel
  28. this._panel = Helpers.CreateDiv('tab-panel') as HTMLDivElement;
  29. this._panel.classList.add('searchable');
  30. // Search bar
  31. this._searchBar = new SearchBar(this);
  32. // Add searchbar
  33. this._panel.appendChild(this._searchBar.toHtml());
  34. // Build the treepanel
  35. this._treePanel = Helpers.CreateDiv('insp-tree', this._panel);
  36. // Build the detail panel
  37. this._detailsPanel = new DetailPanel();
  38. this._panel.appendChild(this._detailsPanel.toHtml());
  39. Split([this._treePanel, this._detailsPanel.toHtml()], {
  40. blockDrag: this._inspector.popupMode,
  41. direction: 'vertical'
  42. });
  43. this.update();
  44. }
  45. /** Overrides dispose */
  46. public dispose() {
  47. this._detailsPanel.dispose();
  48. }
  49. public update(_items?: Array<TreeItem>) {
  50. let items;
  51. if (_items) {
  52. items = _items;
  53. } else {
  54. // Rebuild the tree
  55. this._treeItems = this._getTree();
  56. items = this._treeItems;
  57. }
  58. // Clean the tree
  59. Helpers.CleanDiv(this._treePanel);
  60. // Clean the detail properties
  61. this._detailsPanel.clean();
  62. // Sort items alphabetically
  63. items.sort((item1, item2) => {
  64. return item1.compareTo(item2);
  65. });
  66. // Display items
  67. for (let item of items) {
  68. this._treePanel.appendChild(item.toHtml());
  69. }
  70. }
  71. /** Display the details of the given item */
  72. public displayDetails(item: TreeItem) {
  73. // Remove active state on all items
  74. this.activateNode(item);
  75. // Update the detail panel
  76. this._detailsPanel.details = item.getDetails();
  77. }
  78. /** Select an item in the tree */
  79. public select(item: TreeItem) {
  80. // Active the node
  81. this.activateNode(item);
  82. // Display its details
  83. this.displayDetails(item);
  84. }
  85. /** Set the given item as active in the tree */
  86. public activateNode(item: TreeItem) {
  87. if (this._treeItems) {
  88. for (let node of this._treeItems) {
  89. node.active(false);
  90. }
  91. }
  92. // item.getDiv().scrollIntoView();
  93. item.active(true);
  94. }
  95. /** Returns the treeitem corersponding to the given obj, null if not found */
  96. public getItemFor(_obj: any): Nullable<TreeItem> {
  97. let obj = _obj as AbstractMesh;
  98. // Search recursively
  99. let searchObjectInTree = (object: any, treeItem: TreeItem): Nullable<TreeItem> => {
  100. if (treeItem.correspondsTo(object)) {
  101. return treeItem;
  102. }
  103. else {
  104. if (treeItem.children.length > 0) {
  105. for (let item of treeItem.children) {
  106. let it = searchObjectInTree(obj, item);
  107. if (it) {
  108. return it;
  109. }
  110. }
  111. }
  112. else {
  113. return null;
  114. }
  115. }
  116. return null;
  117. };
  118. for (let item of this._treeItems) {
  119. let it = searchObjectInTree(obj, item);
  120. if (it) {
  121. return it;
  122. }
  123. }
  124. return null;
  125. }
  126. public filter(filter: string) {
  127. let items = [];
  128. for (let item of this._treeItems) {
  129. if (item.id.toLowerCase().indexOf(filter.toLowerCase()) != -1) {
  130. items.push(item);
  131. }
  132. for (let child of item.children) {
  133. if (child.id.toLowerCase().indexOf(filter.toLowerCase()) != -1) {
  134. items.push(item);
  135. }
  136. }
  137. }
  138. this.update(items);
  139. }
  140. /** Builds the tree panel */
  141. protected abstract _getTree(): Array<TreeItem>;
  142. }