TreeItem.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. var __extends = (this && this.__extends) || function (d, b) {
  2. for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
  3. function __() { this.constructor = d; }
  4. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  5. };
  6. var INSPECTOR;
  7. (function (INSPECTOR) {
  8. var TreeItem = (function (_super) {
  9. __extends(TreeItem, _super);
  10. function TreeItem(tab, obj) {
  11. _super.call(this);
  12. this._children = [];
  13. this._tab = tab;
  14. this._adapter = obj;
  15. this._tools = this._adapter.getTools();
  16. this._build();
  17. }
  18. Object.defineProperty(TreeItem.prototype, "id", {
  19. /** Returns the item ID == its adapter ID */
  20. get: function () {
  21. return this._adapter.id();
  22. },
  23. enumerable: true,
  24. configurable: true
  25. });
  26. /** Add the given item as a child of this one */
  27. TreeItem.prototype.add = function (child) {
  28. this._children.push(child);
  29. this.update();
  30. };
  31. /**
  32. * Function used to compare this item to another tree item.
  33. * Returns the alphabetical sort of the adapter ID
  34. */
  35. TreeItem.prototype.compareTo = function (item) {
  36. var str1 = this.id;
  37. var str2 = item.id;
  38. return str1.localeCompare(str2, [], { numeric: true });
  39. };
  40. /** Returns true if the given obj correspond to the adapter linked to this tree item */
  41. TreeItem.prototype.correspondsTo = function (obj) {
  42. return this._adapter.correspondsTo(obj);
  43. };
  44. /** hide all children of this item */
  45. TreeItem.prototype.fold = function () {
  46. // Do nothing id no children
  47. if (this._children.length > 0) {
  48. for (var _i = 0, _a = this._children; _i < _a.length; _i++) {
  49. var elem = _a[_i];
  50. elem.toHtml().style.display = 'none';
  51. }
  52. this._div.classList.add('folded');
  53. this._div.classList.remove('unfolded');
  54. }
  55. };
  56. /** Show all children of this item */
  57. TreeItem.prototype.unfold = function () {
  58. // Do nothing id no children
  59. if (this._children.length > 0) {
  60. for (var _i = 0, _a = this._children; _i < _a.length; _i++) {
  61. var elem = _a[_i];
  62. elem.toHtml().style.display = 'block';
  63. }
  64. this._div.classList.add('unfolded');
  65. this._div.classList.remove('folded');
  66. }
  67. };
  68. /** Build the HTML of this item */
  69. TreeItem.prototype._build = function () {
  70. this._div.className = 'line';
  71. for (var _i = 0, _a = this._tools; _i < _a.length; _i++) {
  72. var tool = _a[_i];
  73. this._div.appendChild(tool.toHtml());
  74. }
  75. // Id
  76. var text = INSPECTOR.Inspector.DOCUMENT.createElement('span');
  77. text.textContent = this._adapter.id();
  78. this._div.appendChild(text);
  79. // Type
  80. var type = INSPECTOR.Inspector.DOCUMENT.createElement('span');
  81. type.className = 'property-type';
  82. type.textContent = ' - ' + this._adapter.type();
  83. this._div.appendChild(type);
  84. this._lineContent = INSPECTOR.Helpers.CreateDiv('line-content', this._div);
  85. this._addEvent();
  86. };
  87. /**
  88. * Returns one HTML element (.details) containing all details of this primitive
  89. */
  90. TreeItem.prototype.getDetails = function () {
  91. return this._adapter.getProperties();
  92. };
  93. TreeItem.prototype.update = function () {
  94. // Clean division holding all children
  95. INSPECTOR.Helpers.CleanDiv(this._lineContent);
  96. for (var _i = 0, _a = this._children; _i < _a.length; _i++) {
  97. var child = _a[_i];
  98. var elem = child.toHtml();
  99. this._lineContent.appendChild(elem);
  100. }
  101. if (this._children.length > 0) {
  102. // Check if folded or not
  103. if (!this._div.classList.contains('folded') && !this._div.classList.contains('unfolded')) {
  104. this._div.classList.add('folded');
  105. }
  106. }
  107. this.fold();
  108. };
  109. /**
  110. * Add an event listener on the item :
  111. * - one click display details
  112. * - on mouse hover the item is highlighted
  113. */
  114. TreeItem.prototype._addEvent = function () {
  115. var _this = this;
  116. this._div.addEventListener('click', function (e) {
  117. _this._tab.select(_this);
  118. // Fold/unfold the tree
  119. if (_this._isFolded()) {
  120. _this.unfold();
  121. }
  122. else {
  123. _this.fold();
  124. }
  125. e.stopPropagation();
  126. });
  127. // Highlight on mouse over
  128. this._div.addEventListener('mouseover', function (e) {
  129. _this._tab.highlightNode(_this);
  130. e.stopPropagation();
  131. });
  132. // Remove highlight on mouse out
  133. this._div.addEventListener('mouseout', function (e) {
  134. _this._tab.highlightNode();
  135. });
  136. };
  137. /** Highlight or downplay this node */
  138. TreeItem.prototype.highlight = function (b) {
  139. // Remove highlight for all children
  140. if (!b) {
  141. for (var _i = 0, _a = this._children; _i < _a.length; _i++) {
  142. var child = _a[_i];
  143. child._adapter.highlight(b);
  144. }
  145. }
  146. // Highlight this node
  147. this._adapter.highlight(b);
  148. };
  149. /** Returns true if the node is folded, false otherwise */
  150. TreeItem.prototype._isFolded = function () {
  151. return !this._div.classList.contains('unfolded');
  152. };
  153. /** Set this item as active (background lighter) in the tree panel */
  154. TreeItem.prototype.active = function (b) {
  155. this._div.classList.remove('active');
  156. for (var _i = 0, _a = this._children; _i < _a.length; _i++) {
  157. var child = _a[_i];
  158. child.active(false);
  159. }
  160. if (b) {
  161. this._div.classList.add('active');
  162. }
  163. };
  164. return TreeItem;
  165. }(INSPECTOR.BasicElement));
  166. INSPECTOR.TreeItem = TreeItem;
  167. })(INSPECTOR || (INSPECTOR = {}));
  168. //# sourceMappingURL=TreeItem.js.map