HelpScreen.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /*
  2. * Copyright (C) 2010 Google 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 are
  6. * met:
  7. *
  8. * * Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * * Redistributions in binary form must reproduce the above
  11. * copyright notice, this list of conditions and the following disclaimer
  12. * in the documentation and/or other materials provided with the
  13. * distribution.
  14. * * Neither the name of Google Inc. nor the names of its
  15. * contributors may be used to endorse or promote products derived from
  16. * this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  21. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  22. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  23. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  24. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  25. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  26. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  28. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. */
  30. /**
  31. * @constructor
  32. * @param {string=} title
  33. * @extends {WebInspector.View}
  34. */
  35. WebInspector.HelpScreen = function(title)
  36. {
  37. WebInspector.View.call(this);
  38. this.markAsRoot();
  39. this.registerRequiredCSS("helpScreen.css");
  40. this.element.className = "help-window-outer";
  41. this.element.addEventListener("keydown", this._onKeyDown.bind(this), false);
  42. this.element.tabIndex = 0;
  43. if (title) {
  44. var mainWindow = this.element.createChild("div", "help-window-main");
  45. var captionWindow = mainWindow.createChild("div", "help-window-caption");
  46. captionWindow.appendChild(this._createCloseButton());
  47. this.contentElement = mainWindow.createChild("div", "help-content");
  48. captionWindow.createChild("h1", "help-window-title").textContent = title;
  49. }
  50. }
  51. /**
  52. * @type {WebInspector.HelpScreen}
  53. */
  54. WebInspector.HelpScreen._visibleScreen = null;
  55. /**
  56. * @return {boolean}
  57. */
  58. WebInspector.HelpScreen.isVisible = function()
  59. {
  60. return !!WebInspector.HelpScreen._visibleScreen;
  61. }
  62. WebInspector.HelpScreen.focus = function()
  63. {
  64. WebInspector.HelpScreen._visibleScreen.element.focus();
  65. }
  66. WebInspector.HelpScreen.prototype = {
  67. _createCloseButton: function()
  68. {
  69. var closeButton = document.createElement("div");
  70. closeButton.className = "help-close-button close-button-gray";
  71. closeButton.addEventListener("click", this.hide.bind(this), false);
  72. return closeButton;
  73. },
  74. showModal: function()
  75. {
  76. var visibleHelpScreen = WebInspector.HelpScreen._visibleScreen;
  77. if (visibleHelpScreen === this)
  78. return;
  79. if (visibleHelpScreen)
  80. visibleHelpScreen.hide();
  81. WebInspector.HelpScreen._visibleScreen = this;
  82. this.show(document.body);
  83. this.focus();
  84. },
  85. hide: function()
  86. {
  87. if (!this.isShowing())
  88. return;
  89. WebInspector.HelpScreen._visibleScreen = null;
  90. WebInspector.restoreFocusFromElement(this.element);
  91. this.detach();
  92. },
  93. /**
  94. * @param {number} keyCode
  95. * @return {boolean}
  96. */
  97. isClosingKey: function(keyCode)
  98. {
  99. return [
  100. WebInspector.KeyboardShortcut.Keys.Enter.code,
  101. WebInspector.KeyboardShortcut.Keys.Esc.code,
  102. WebInspector.KeyboardShortcut.Keys.Space.code,
  103. ].indexOf(keyCode) >= 0;
  104. },
  105. _onKeyDown: function(event)
  106. {
  107. if (this.isShowing() && this.isClosingKey(event.keyCode)) {
  108. this.hide();
  109. event.consume();
  110. }
  111. },
  112. __proto__: WebInspector.View.prototype
  113. }
  114. /**
  115. * @constructor
  116. * @param {string=} title
  117. * @param {string=} message
  118. * @extends {WebInspector.HelpScreen}
  119. */
  120. WebInspector.HelpScreenUntilReload = function(title, message)
  121. {
  122. WebInspector.HelpScreen.call(this, title);
  123. var p = this.contentElement.createChild("p");
  124. p.addStyleClass("help-section");
  125. p.textContent = message;
  126. WebInspector.debuggerModel.addEventListener(WebInspector.DebuggerModel.Events.GlobalObjectCleared, this.hide, this);
  127. }
  128. WebInspector.HelpScreenUntilReload.prototype = {
  129. /**
  130. * @override
  131. */
  132. willHide: function()
  133. {
  134. WebInspector.debuggerModel.removeEventListener(WebInspector.DebuggerModel.Events.GlobalObjectCleared, this.hide, this);
  135. WebInspector.HelpScreen.prototype.willHide.call(this);
  136. },
  137. __proto__: WebInspector.HelpScreen.prototype
  138. }