InspectorView.js 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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. * @return {WebInspector.Searchable}
  96. */
  97. getSearchProvider: function()
  98. {
  99. return this._currentPanel;
  100. },
  101. /**
  102. * @param {WebInspector.Panel} x
  103. */
  104. setCurrentPanel: function(x)
  105. {
  106. if (this._currentPanel === x)
  107. return;
  108. // FIXME: remove search controller.
  109. WebInspector.searchController.cancelSearch();
  110. if (this._currentPanel)
  111. this._currentPanel.detach();
  112. this._currentPanel = x;
  113. if (x) {
  114. x.show();
  115. this.dispatchEventToListeners(WebInspector.InspectorView.Events.PanelSelected);
  116. }
  117. for (var panelName in WebInspector.panels) {
  118. if (WebInspector.panels[panelName] === x) {
  119. WebInspector.settings.lastActivePanel.set(panelName);
  120. this._pushToHistory(panelName);
  121. WebInspector.userMetrics.panelShown(panelName);
  122. }
  123. }
  124. },
  125. /**
  126. * @return {Element}
  127. */
  128. defaultFocusedElement: function()
  129. {
  130. return this._currentPanel ? this._currentPanel.defaultFocusedElement() : null;
  131. },
  132. _keyPress: function(event)
  133. {
  134. // BUG 104250: Windows 7 posts a WM_CHAR message upon the Ctrl+']' keypress.
  135. // Any charCode < 32 is not going to be a valid keypress.
  136. if (event.charCode < 32 && WebInspector.isWin())
  137. return;
  138. clearTimeout(this._keyDownTimer);
  139. delete this._keyDownTimer;
  140. },
  141. _keyDown: function(event)
  142. {
  143. if (!WebInspector.KeyboardShortcut.eventHasCtrlOrMeta(event))
  144. return;
  145. // Ctrl/Cmd + 1-9 should show corresponding panel.
  146. var panelShortcutEnabled = WebInspector.settings.shortcutPanelSwitch.get();
  147. if (panelShortcutEnabled && !event.shiftKey && !event.altKey && event.keyCode > 0x30 && event.keyCode < 0x3A) {
  148. var panelName = this._panelOrder[event.keyCode - 0x31];
  149. if (panelName) {
  150. this.showPanel(panelName);
  151. event.consume(true);
  152. }
  153. return;
  154. }
  155. // BUG85312: On French AZERTY keyboards, AltGr-]/[ combinations (synonymous to Ctrl-Alt-]/[ on Windows) are used to enter ]/[,
  156. // so for a ]/[-related keydown we delay the panel switch using a timer, to see if there is a keypress event following this one.
  157. // If there is, we cancel the timer and do not consider this a panel switch.
  158. if (!WebInspector.isWin() || (!this._openBracketIdentifiers[event.keyIdentifier] && !this._closeBracketIdentifiers[event.keyIdentifier])) {
  159. this._keyDownInternal(event);
  160. return;
  161. }
  162. this._keyDownTimer = setTimeout(this._keyDownInternal.bind(this, event), 0);
  163. },
  164. _keyDownInternal: function(event)
  165. {
  166. if (this._openBracketIdentifiers[event.keyIdentifier]) {
  167. var isRotateLeft = !event.shiftKey && !event.altKey;
  168. if (isRotateLeft) {
  169. var index = this._panelOrder.indexOf(this.currentPanel().name);
  170. index = (index === 0) ? this._panelOrder.length - 1 : index - 1;
  171. this.showPanel(this._panelOrder[index]);
  172. event.consume(true);
  173. return;
  174. }
  175. var isGoBack = event.altKey;
  176. if (isGoBack && this._canGoBackInHistory()) {
  177. this._goBackInHistory();
  178. event.consume(true);
  179. }
  180. return;
  181. }
  182. if (this._closeBracketIdentifiers[event.keyIdentifier]) {
  183. var isRotateRight = !event.shiftKey && !event.altKey;
  184. if (isRotateRight) {
  185. var index = this._panelOrder.indexOf(this.currentPanel().name);
  186. index = (index + 1) % this._panelOrder.length;
  187. this.showPanel(this._panelOrder[index]);
  188. event.consume(true);
  189. return;
  190. }
  191. var isGoForward = event.altKey;
  192. if (isGoForward && this._canGoForwardInHistory()) {
  193. this._goForwardInHistory();
  194. event.consume(true);
  195. }
  196. return;
  197. }
  198. },
  199. _canGoBackInHistory: function()
  200. {
  201. return this._historyIterator > 0;
  202. },
  203. _goBackInHistory: function()
  204. {
  205. this._inHistory = true;
  206. this.setCurrentPanel(WebInspector.panels[this._history[--this._historyIterator]]);
  207. delete this._inHistory;
  208. },
  209. _canGoForwardInHistory: function()
  210. {
  211. return this._historyIterator < this._history.length - 1;
  212. },
  213. _goForwardInHistory: function()
  214. {
  215. this._inHistory = true;
  216. this.setCurrentPanel(WebInspector.panels[this._history[++this._historyIterator]]);
  217. delete this._inHistory;
  218. },
  219. _pushToHistory: function(panelName)
  220. {
  221. if (this._inHistory)
  222. return;
  223. this._history.splice(this._historyIterator + 1, this._history.length - this._historyIterator - 1);
  224. if (!this._history.length || this._history[this._history.length - 1] !== panelName)
  225. this._history.push(panelName);
  226. this._historyIterator = this._history.length - 1;
  227. },
  228. panelsElement: function()
  229. {
  230. return this._panelsElement;
  231. },
  232. /**
  233. * @param {?Element} element
  234. */
  235. setFooterElement: function(element)
  236. {
  237. if (this._currentPanel && this._currentPanel.canSetFooterElement()) {
  238. this._currentPanel.setFooterElement(element);
  239. return;
  240. }
  241. if (element) {
  242. this._footerElementContainer.removeStyleClass("hidden");
  243. this._footerElementContainer.appendChild(element);
  244. this._panelsElement.style.bottom = this._footerElementContainer.offsetHeight + "px";
  245. } else {
  246. this._footerElementContainer.addStyleClass("hidden");
  247. this._footerElementContainer.removeChildren();
  248. this._panelsElement.style.bottom = 0;
  249. }
  250. this.doResize();
  251. },
  252. __proto__: WebInspector.View.prototype
  253. }
  254. /**
  255. * @type {WebInspector.InspectorView}
  256. */
  257. WebInspector.inspectorView = null;