InspectorView.js 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. /*
  2. * Copyright (C) 2011 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. * @extends {WebInspector.View}
  33. */
  34. WebInspector.InspectorView = function()
  35. {
  36. WebInspector.View.call(this);
  37. this.markAsRoot();
  38. this.element.id = "main-panels";
  39. this.element.setAttribute("spellcheck", false);
  40. this._history = [];
  41. this._historyIterator = -1;
  42. document.addEventListener("keydown", this._keyDown.bind(this), false);
  43. document.addEventListener("keypress", this._keyPress.bind(this), false);
  44. this._panelOrder = [];
  45. this._panelDescriptors = {};
  46. // Windows and Mac have two different definitions of '[' and ']', so accept both of each.
  47. this._openBracketIdentifiers = ["U+005B", "U+00DB"].keySet();
  48. this._closeBracketIdentifiers = ["U+005D", "U+00DD"].keySet();
  49. this._footerElementContainer = this.element.createChild("div", "inspector-footer status-bar hidden");
  50. this._panelsElement = this.element.createChild("div", "fill");
  51. }
  52. WebInspector.InspectorView.Events = {
  53. PanelSelected: "PanelSelected"
  54. }
  55. WebInspector.InspectorView.prototype = {
  56. /**
  57. * @param {WebInspector.PanelDescriptor} panelDescriptor
  58. */
  59. addPanel: function(panelDescriptor)
  60. {
  61. this._panelOrder.push(panelDescriptor.name());
  62. this._panelDescriptors[panelDescriptor.name()] = panelDescriptor;
  63. WebInspector.toolbar.addPanel(panelDescriptor);
  64. },
  65. /**
  66. * @param {string} panelName
  67. * @return {?WebInspector.Panel}
  68. */
  69. panel: function(panelName)
  70. {
  71. var panelDescriptor = this._panelDescriptors[panelName];
  72. if (!panelDescriptor && this._panelOrder.length)
  73. panelDescriptor = this._panelDescriptors[this._panelOrder[0]];
  74. return panelDescriptor ? panelDescriptor.panel() : null;
  75. },
  76. /**
  77. * @param {string} panelName
  78. * @return {?WebInspector.Panel}
  79. */
  80. showPanel: function(panelName)
  81. {
  82. var panel = this.panel(panelName);
  83. if (panel)
  84. this.setCurrentPanel(panel);
  85. return panel;
  86. },
  87. /**
  88. * @return {WebInspector.Panel}
  89. */
  90. currentPanel: function()
  91. {
  92. return this._currentPanel;
  93. },
  94. /**
  95. * @param {WebInspector.Panel} x
  96. */
  97. setCurrentPanel: function(x)
  98. {
  99. if (this._currentPanel === x)
  100. return;
  101. if (this._currentPanel)
  102. this._currentPanel.detach();
  103. this._currentPanel = x;
  104. if (x) {
  105. x.show();
  106. this.dispatchEventToListeners(WebInspector.InspectorView.Events.PanelSelected);
  107. // FIXME: remove search controller.
  108. WebInspector.searchController.cancelSearch();
  109. }
  110. for (var panelName in WebInspector.panels) {
  111. if (WebInspector.panels[panelName] === x) {
  112. WebInspector.settings.lastActivePanel.set(panelName);
  113. this._pushToHistory(panelName);
  114. WebInspector.userMetrics.panelShown(panelName);
  115. }
  116. }
  117. },
  118. /**
  119. * @return {Element}
  120. */
  121. defaultFocusedElement: function()
  122. {
  123. return this._currentPanel ? this._currentPanel.defaultFocusedElement() : null;
  124. },
  125. _keyPress: function(event)
  126. {
  127. // BUG 104250: Windows 7 posts a WM_CHAR message upon the Ctrl+']' keypress.
  128. // Any charCode < 32 is not going to be a valid keypress.
  129. if (event.charCode < 32 && WebInspector.isWin())
  130. return;
  131. clearTimeout(this._keyDownTimer);
  132. delete this._keyDownTimer;
  133. },
  134. _keyDown: function(event)
  135. {
  136. if (!WebInspector.KeyboardShortcut.eventHasCtrlOrMeta(event))
  137. return;
  138. // Ctrl/Cmd + 1-9 should show corresponding panel.
  139. if (!event.shiftKey && !event.altKey && event.keyCode > 0x30 && event.keyCode < 0x3A) {
  140. var panelName = this._panelOrder[event.keyCode - 0x31];
  141. if (panelName) {
  142. this.showPanel(panelName);
  143. event.consume(true);
  144. }
  145. return;
  146. }
  147. // BUG85312: On French AZERTY keyboards, AltGr-]/[ combinations (synonymous to Ctrl-Alt-]/[ on Windows) are used to enter ]/[,
  148. // so for a ]/[-related keydown we delay the panel switch using a timer, to see if there is a keypress event following this one.
  149. // If there is, we cancel the timer and do not consider this a panel switch.
  150. if (!WebInspector.isWin() || (!this._openBracketIdentifiers[event.keyIdentifier] && !this._closeBracketIdentifiers[event.keyIdentifier])) {
  151. this._keyDownInternal(event);
  152. return;
  153. }
  154. this._keyDownTimer = setTimeout(this._keyDownInternal.bind(this, event), 0);
  155. },
  156. _keyDownInternal: function(event)
  157. {
  158. if (this._openBracketIdentifiers[event.keyIdentifier]) {
  159. var isRotateLeft = !event.shiftKey && !event.altKey;
  160. if (isRotateLeft) {
  161. var index = this._panelOrder.indexOf(this.currentPanel().name);
  162. index = (index === 0) ? this._panelOrder.length - 1 : index - 1;
  163. this.showPanel(this._panelOrder[index]);
  164. event.consume(true);
  165. return;
  166. }
  167. var isGoBack = event.altKey;
  168. if (isGoBack && this._canGoBackInHistory()) {
  169. this._goBackInHistory();
  170. event.consume(true);
  171. }
  172. return;
  173. }
  174. if (this._closeBracketIdentifiers[event.keyIdentifier]) {
  175. var isRotateRight = !event.shiftKey && !event.altKey;
  176. if (isRotateRight) {
  177. var index = this._panelOrder.indexOf(this.currentPanel().name);
  178. index = (index + 1) % this._panelOrder.length;
  179. this.showPanel(this._panelOrder[index]);
  180. event.consume(true);
  181. return;
  182. }
  183. var isGoForward = event.altKey;
  184. if (isGoForward && this._canGoForwardInHistory()) {
  185. this._goForwardInHistory();
  186. event.consume(true);
  187. }
  188. return;
  189. }
  190. },
  191. _canGoBackInHistory: function()
  192. {
  193. return this._historyIterator > 0;
  194. },
  195. _goBackInHistory: function()
  196. {
  197. this._inHistory = true;
  198. this.setCurrentPanel(WebInspector.panels[this._history[--this._historyIterator]]);
  199. delete this._inHistory;
  200. },
  201. _canGoForwardInHistory: function()
  202. {
  203. return this._historyIterator < this._history.length - 1;
  204. },
  205. _goForwardInHistory: function()
  206. {
  207. this._inHistory = true;
  208. this.setCurrentPanel(WebInspector.panels[this._history[++this._historyIterator]]);
  209. delete this._inHistory;
  210. },
  211. _pushToHistory: function(panelName)
  212. {
  213. if (this._inHistory)
  214. return;
  215. this._history.splice(this._historyIterator + 1, this._history.length - this._historyIterator - 1);
  216. if (!this._history.length || this._history[this._history.length - 1] !== panelName)
  217. this._history.push(panelName);
  218. this._historyIterator = this._history.length - 1;
  219. },
  220. panelsElement: function()
  221. {
  222. return this._panelsElement;
  223. },
  224. /**
  225. * @param {Element?} element
  226. */
  227. setFooterElement: function(element)
  228. {
  229. if (element) {
  230. this._footerElementContainer.removeStyleClass("hidden");
  231. this._footerElementContainer.appendChild(element);
  232. this._panelsElement.style.bottom = this._footerElementContainer.offsetHeight + "px";
  233. } else {
  234. this._footerElementContainer.addStyleClass("hidden");
  235. this._footerElementContainer.removeChildren();
  236. this._panelsElement.style.bottom = 0;
  237. }
  238. this.doResize();
  239. },
  240. /**
  241. * @param {WebInspector.Panel} panel
  242. */
  243. showPanelForAnchorNavigation: function(panel)
  244. {
  245. WebInspector.searchController.disableSearchUntilExplicitAction();
  246. this.setCurrentPanel(panel);
  247. },
  248. __proto__: WebInspector.View.prototype
  249. }
  250. /**
  251. * @type {WebInspector.InspectorView}
  252. */
  253. WebInspector.inspectorView = null;