Section.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /*
  2. * Copyright (C) 2007 Apple Inc. All rights reserved.
  3. * Copyright (C) 2009 Google Inc. All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of
  15. * its contributors may be used to endorse or promote products derived
  16. * from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
  19. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  27. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. */
  29. /**
  30. * @constructor
  31. * @param {string|Element} title
  32. * @param {string=} subtitle
  33. */
  34. WebInspector.Section = function(title, subtitle)
  35. {
  36. this.element = document.createElement("div");
  37. this.element.className = "section";
  38. this.element._section = this;
  39. this.headerElement = document.createElement("div");
  40. this.headerElement.className = "header";
  41. this.titleElement = document.createElement("div");
  42. this.titleElement.className = "title";
  43. this.subtitleElement = document.createElement("div");
  44. this.subtitleElement.className = "subtitle";
  45. this.headerElement.appendChild(this.subtitleElement);
  46. this.headerElement.appendChild(this.titleElement);
  47. this.headerElement.addEventListener("click", this.handleClick.bind(this), false);
  48. this.element.appendChild(this.headerElement);
  49. this.title = title;
  50. this.subtitle = subtitle;
  51. this._expanded = false;
  52. }
  53. WebInspector.Section.prototype = {
  54. get title()
  55. {
  56. return this._title;
  57. },
  58. set title(x)
  59. {
  60. if (this._title === x)
  61. return;
  62. this._title = x;
  63. if (x instanceof Node) {
  64. this.titleElement.removeChildren();
  65. this.titleElement.appendChild(x);
  66. } else
  67. this.titleElement.textContent = x;
  68. },
  69. get subtitle()
  70. {
  71. return this._subtitle;
  72. },
  73. set subtitle(x)
  74. {
  75. if (this._subtitle === x)
  76. return;
  77. this._subtitle = x;
  78. this.subtitleElement.textContent = x;
  79. },
  80. get subtitleAsTextForTest()
  81. {
  82. var result = this.subtitleElement.textContent;
  83. var child = this.subtitleElement.querySelector("[data-uncopyable]");
  84. if (child) {
  85. var linkData = child.getAttribute("data-uncopyable");
  86. if (linkData)
  87. result += linkData;
  88. }
  89. return result;
  90. },
  91. get expanded()
  92. {
  93. return this._expanded;
  94. },
  95. set expanded(x)
  96. {
  97. if (x)
  98. this.expand();
  99. else
  100. this.collapse();
  101. },
  102. get populated()
  103. {
  104. return this._populated;
  105. },
  106. set populated(x)
  107. {
  108. this._populated = x;
  109. if (!x && this._expanded) {
  110. this.onpopulate();
  111. this._populated = true;
  112. }
  113. },
  114. onpopulate: function()
  115. {
  116. // Overriden by subclasses.
  117. },
  118. get firstSibling()
  119. {
  120. var parent = this.element.parentElement;
  121. if (!parent)
  122. return null;
  123. var childElement = parent.firstChild;
  124. while (childElement) {
  125. if (childElement._section)
  126. return childElement._section;
  127. childElement = childElement.nextSibling;
  128. }
  129. return null;
  130. },
  131. get lastSibling()
  132. {
  133. var parent = this.element.parentElement;
  134. if (!parent)
  135. return null;
  136. var childElement = parent.lastChild;
  137. while (childElement) {
  138. if (childElement._section)
  139. return childElement._section;
  140. childElement = childElement.previousSibling;
  141. }
  142. return null;
  143. },
  144. get nextSibling()
  145. {
  146. var curElement = this.element;
  147. do {
  148. curElement = curElement.nextSibling;
  149. } while (curElement && !curElement._section);
  150. return curElement ? curElement._section : null;
  151. },
  152. get previousSibling()
  153. {
  154. var curElement = this.element;
  155. do {
  156. curElement = curElement.previousSibling;
  157. } while (curElement && !curElement._section);
  158. return curElement ? curElement._section : null;
  159. },
  160. expand: function()
  161. {
  162. if (this._expanded)
  163. return;
  164. this._expanded = true;
  165. this.element.addStyleClass("expanded");
  166. if (!this._populated) {
  167. this.onpopulate();
  168. this._populated = true;
  169. }
  170. },
  171. collapse: function()
  172. {
  173. if (!this._expanded)
  174. return;
  175. this._expanded = false;
  176. this.element.removeStyleClass("expanded");
  177. },
  178. toggleExpanded: function()
  179. {
  180. this.expanded = !this.expanded;
  181. },
  182. handleClick: function(event)
  183. {
  184. this.toggleExpanded();
  185. event.consume();
  186. }
  187. }