SidebarPane.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /*
  2. * Copyright (C) 2007 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. *
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of
  14. * its contributors may be used to endorse or promote products derived
  15. * from this software without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
  18. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  19. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  20. * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
  21. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  22. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  23. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  24. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  25. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  26. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27. */
  28. /**
  29. * @constructor
  30. * @extends {WebInspector.View}
  31. */
  32. WebInspector.SidebarPane = function(title)
  33. {
  34. WebInspector.View.call(this);
  35. this.element.className = "sidebar-pane";
  36. this.titleElement = document.createElement("div");
  37. this.titleElement.className = "sidebar-pane-toolbar";
  38. this.bodyElement = this.element.createChild("div", "body");
  39. this._title = title;
  40. this._expandCallback = null;
  41. }
  42. WebInspector.SidebarPane.EventTypes = {
  43. wasShown: "wasShown"
  44. }
  45. WebInspector.SidebarPane.prototype = {
  46. title: function()
  47. {
  48. return this._title;
  49. },
  50. /**
  51. * @param {function()} callback
  52. */
  53. prepareContent: function(callback)
  54. {
  55. if (callback)
  56. callback();
  57. },
  58. expand: function()
  59. {
  60. this.prepareContent(this.onContentReady.bind(this));
  61. },
  62. onContentReady: function()
  63. {
  64. if (this._expandCallback)
  65. this._expandCallback();
  66. else
  67. this._expandPending = true;
  68. },
  69. /**
  70. * @param {function()} callback
  71. */
  72. setExpandCallback: function(callback)
  73. {
  74. this._expandCallback = callback;
  75. if (this._expandPending) {
  76. delete this._expandPending;
  77. this._expandCallback();
  78. }
  79. },
  80. wasShown: function()
  81. {
  82. WebInspector.View.prototype.wasShown.call(this);
  83. this.dispatchEventToListeners(WebInspector.SidebarPane.EventTypes.wasShown);
  84. },
  85. __proto__: WebInspector.View.prototype
  86. }
  87. /**
  88. * @constructor
  89. * @param {Element} container
  90. * @param {WebInspector.SidebarPane} pane
  91. */
  92. WebInspector.SidebarPaneTitle = function(container, pane)
  93. {
  94. this._pane = pane;
  95. this.element = container.createChild("div", "sidebar-pane-title");
  96. this.element.textContent = pane.title();
  97. this.element.tabIndex = 0;
  98. this.element.addEventListener("click", this._toggleExpanded.bind(this), false);
  99. this.element.addEventListener("keydown", this._onTitleKeyDown.bind(this), false);
  100. this.element.appendChild(this._pane.titleElement);
  101. this._pane.setExpandCallback(this._expand.bind(this));
  102. }
  103. WebInspector.SidebarPaneTitle.prototype = {
  104. _expand: function()
  105. {
  106. this.element.addStyleClass("expanded");
  107. this._pane.show(this.element.parentNode, this.element.nextSibling);
  108. },
  109. _collapse: function()
  110. {
  111. this.element.removeStyleClass("expanded");
  112. if (this._pane.element.parentNode == this.element.parentNode)
  113. this._pane.detach();
  114. },
  115. _toggleExpanded: function()
  116. {
  117. if (this.element.hasStyleClass("expanded"))
  118. this._collapse();
  119. else
  120. this._pane.expand();
  121. },
  122. /**
  123. * @param {Event} event
  124. */
  125. _onTitleKeyDown: function(event)
  126. {
  127. if (isEnterKey(event) || event.keyCode === WebInspector.KeyboardShortcut.Keys.Space.code)
  128. this._toggleExpanded();
  129. }
  130. }
  131. /**
  132. * @constructor
  133. * @extends {WebInspector.View}
  134. */
  135. WebInspector.SidebarPaneStack = function()
  136. {
  137. WebInspector.View.call(this);
  138. this.element.className = "sidebar-pane-stack fill";
  139. this.registerRequiredCSS("sidebarPane.css");
  140. }
  141. WebInspector.SidebarPaneStack.prototype = {
  142. /**
  143. * @param {WebInspector.SidebarPane} pane
  144. */
  145. addPane: function(pane)
  146. {
  147. new WebInspector.SidebarPaneTitle(this.element, pane);
  148. },
  149. __proto__: WebInspector.View.prototype
  150. }
  151. /**
  152. * @constructor
  153. * @extends {WebInspector.TabbedPane}
  154. */
  155. WebInspector.SidebarTabbedPane = function()
  156. {
  157. WebInspector.TabbedPane.call(this);
  158. this.element.addStyleClass("sidebar-tabbed-pane");
  159. this.registerRequiredCSS("sidebarPane.css");
  160. }
  161. WebInspector.SidebarTabbedPane.prototype = {
  162. /**
  163. * @param {WebInspector.SidebarPane} pane
  164. */
  165. addPane: function(pane)
  166. {
  167. var title = pane.title();
  168. this.appendTab(title, title, pane);
  169. pane.element.appendChild(pane.titleElement);
  170. pane.setExpandCallback(this.selectTab.bind(this, title));
  171. },
  172. __proto__: WebInspector.TabbedPane.prototype
  173. }