DetailsSection.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /*
  2. * Copyright (C) 2013 Apple Inc. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. * 1. Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * 2. Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. *
  13. * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
  14. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  15. * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  16. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
  17. * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  18. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  19. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  20. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  21. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  22. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  23. * THE POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. WebInspector.DetailsSection = function(identifier, title, groups, optionsElement, defaultCollapsedSettingValue) {
  26. WebInspector.Object.call(this);
  27. console.assert(identifier);
  28. this._element = document.createElement("div");
  29. this._element.className = WebInspector.DetailsSection.StyleClassName;
  30. this._headerElement = document.createElement("div");
  31. this._headerElement.addEventListener("click", this._headerElementClicked.bind(this));
  32. this._headerElement.className = WebInspector.DetailsSection.HeaderElementStyleClassName;
  33. this._element.appendChild(this._headerElement);
  34. if (optionsElement instanceof HTMLElement) {
  35. this._optionsElement = optionsElement;
  36. this._optionsElement.addEventListener("mousedown", this._optionsElementMouseDown.bind(this));
  37. this._optionsElement.addEventListener("mouseup", this._optionsElementMouseUp.bind(this));
  38. this._headerElement.appendChild(this._optionsElement);
  39. }
  40. this._titleElement = document.createElement("span");
  41. this._headerElement.appendChild(this._titleElement);
  42. this._contentElement = document.createElement("div");
  43. this._contentElement.className = WebInspector.DetailsSection.ContentElementStyleClassName;
  44. this._element.appendChild(this._contentElement);
  45. this._generateDisclosureTrianglesIfNeeded();
  46. this._identifier = identifier;
  47. this.title = title;
  48. this.groups = groups || [new WebInspector.DetailsSectionGroup];
  49. this._collapsedSetting = new WebInspector.Setting(identifier + "-details-section-collapsed", !!defaultCollapsedSettingValue);
  50. this.collapsed = this._collapsedSetting.value;
  51. };
  52. WebInspector.DetailsSection.StyleClassName = "details-section";
  53. WebInspector.DetailsSection.HeaderElementStyleClassName = "header";
  54. WebInspector.DetailsSection.TitleElementStyleClassName = "title";
  55. WebInspector.DetailsSection.ContentElementStyleClassName = "content";
  56. WebInspector.DetailsSection.CollapsedStyleClassName = "collapsed";
  57. WebInspector.DetailsSection.MouseOverOptionsElementStyleClassName = "mouse-over-options-element";
  58. WebInspector.DetailsSection.DisclosureTriangleOpenCanvasIdentifier = "details-section-disclosure-triangle-open";
  59. WebInspector.DetailsSection.DisclosureTriangleClosedCanvasIdentifier = "details-section-disclosure-triangle-closed";
  60. WebInspector.DetailsSection.DisclosureTriangleNormalCanvasIdentifierSuffix = "-normal";
  61. WebInspector.DetailsSection.DisclosureTriangleActiveCanvasIdentifierSuffix = "-active";
  62. WebInspector.DetailsSection.prototype = {
  63. constructor: WebInspector.DetailsSection,
  64. // Public
  65. get element()
  66. {
  67. return this._element;
  68. },
  69. get identifier()
  70. {
  71. return this._identifier;
  72. },
  73. get title()
  74. {
  75. return this._titleElement.textContent;
  76. },
  77. set title(title)
  78. {
  79. this._titleElement.textContent = title;
  80. },
  81. get collapsed()
  82. {
  83. return this._element.classList.contains(WebInspector.DetailsSection.CollapsedStyleClassName);
  84. },
  85. set collapsed(flag)
  86. {
  87. if (flag)
  88. this._element.classList.add(WebInspector.DetailsSection.CollapsedStyleClassName);
  89. else
  90. this._element.classList.remove(WebInspector.DetailsSection.CollapsedStyleClassName);
  91. this._collapsedSetting.value = flag || false;
  92. },
  93. get groups()
  94. {
  95. return this._groups;
  96. },
  97. set groups(groups)
  98. {
  99. this._contentElement.removeChildren();
  100. this._groups = groups || [];
  101. for (var i = 0; i < this._groups.length; ++i)
  102. this._contentElement.appendChild(this._groups[i].element);
  103. },
  104. // Private
  105. _headerElementClicked: function(event)
  106. {
  107. if (event.target.isSelfOrDescendant(this._optionsElement))
  108. return;
  109. this.collapsed = !this.collapsed;
  110. this._element.scrollIntoViewIfNeeded(false);
  111. },
  112. _optionsElementMouseDown: function(event)
  113. {
  114. this._headerElement.classList.add(WebInspector.DetailsSection.MouseOverOptionsElementStyleClassName);
  115. },
  116. _optionsElementMouseUp: function(event)
  117. {
  118. this._headerElement.classList.remove(WebInspector.DetailsSection.MouseOverOptionsElementStyleClassName);
  119. },
  120. _generateDisclosureTrianglesIfNeeded: function()
  121. {
  122. if (WebInspector.DetailsSection._generatedDisclosureTriangles)
  123. return;
  124. // Set this early instead of in _generateDisclosureTriangle because we don't want multiple sections that are
  125. // created at the same time to duplicate the work (even though it would be harmless.)
  126. WebInspector.DetailsSection._generatedDisclosureTriangles = true;
  127. var specifications = {};
  128. specifications[WebInspector.DetailsSection.DisclosureTriangleNormalCanvasIdentifierSuffix] = {
  129. fillColor: [134, 134, 134]
  130. };
  131. specifications[WebInspector.DetailsSection.DisclosureTriangleActiveCanvasIdentifierSuffix] = {
  132. fillColor: [57, 57, 57]
  133. };
  134. generateColoredImagesForCSS("Images/DisclosureTriangleSmallOpen.pdf", specifications, 13, 13, WebInspector.DetailsSection.DisclosureTriangleOpenCanvasIdentifier);
  135. generateColoredImagesForCSS("Images/DisclosureTriangleSmallClosed.pdf", specifications, 13, 13, WebInspector.DetailsSection.DisclosureTriangleClosedCanvasIdentifier);
  136. }
  137. };
  138. WebInspector.DetailsSection.prototype.__proto__ = WebInspector.Object.prototype;