PropertyTab.ts 5.3 KB

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