DockController.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /*
  2. * Copyright (C) 2012 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.Object}
  33. */
  34. WebInspector.DockController = function()
  35. {
  36. this._dockToggleButton = new WebInspector.StatusBarButton("", "dock-status-bar-item", 3);
  37. this._dockToggleButtonOption = new WebInspector.StatusBarButton("", "dock-status-bar-item", 3);
  38. this._dockToggleButton.addEventListener("click", this._toggleDockState, this);
  39. this._dockToggleButtonOption.addEventListener("click", this._toggleDockState, this);
  40. this._dockToggleButton.setLongClickOptionsEnabled(this._createDockOptions.bind(this));
  41. this.setDockSide(WebInspector.queryParamsObject["dockSide"] || "bottom");
  42. }
  43. WebInspector.DockController.State = {
  44. DockedToBottom: "bottom",
  45. DockedToRight: "right",
  46. Undocked: "undocked"
  47. }
  48. WebInspector.DockController.Events = {
  49. DockSideChanged: "DockSideChanged"
  50. }
  51. WebInspector.DockController.prototype = {
  52. /**
  53. * @return {Element}
  54. */
  55. get element()
  56. {
  57. return this._dockToggleButton.element;
  58. },
  59. /**
  60. * @return {string}
  61. */
  62. dockSide: function()
  63. {
  64. return this._dockSide;
  65. },
  66. /**
  67. * @param {string} dockSide
  68. */
  69. setDockSide: function(dockSide)
  70. {
  71. if (this._dockSide === dockSide)
  72. return;
  73. if (this._dockSide)
  74. WebInspector.settings.lastDockState.set(this._dockSide);
  75. this._dockSide = dockSide;
  76. if (dockSide === WebInspector.DockController.State.Undocked)
  77. WebInspector.userMetrics.WindowDocked.record();
  78. else
  79. WebInspector.userMetrics.WindowUndocked.record();
  80. this._updateUI();
  81. this.dispatchEventToListeners(WebInspector.DockController.Events.DockSideChanged, this._dockSide);
  82. },
  83. /**
  84. * @param {boolean} unavailable
  85. */
  86. setDockingUnavailable: function(unavailable)
  87. {
  88. this._isDockingUnavailable = unavailable;
  89. this._updateUI();
  90. },
  91. _updateUI: function()
  92. {
  93. var body = document.body;
  94. switch (this._dockSide) {
  95. case WebInspector.DockController.State.DockedToBottom:
  96. body.removeStyleClass("undocked");
  97. body.removeStyleClass("dock-to-right");
  98. body.addStyleClass("dock-to-bottom");
  99. break;
  100. case WebInspector.DockController.State.DockedToRight:
  101. body.removeStyleClass("undocked");
  102. body.addStyleClass("dock-to-right");
  103. body.removeStyleClass("dock-to-bottom");
  104. break;
  105. case WebInspector.DockController.State.Undocked:
  106. body.addStyleClass("undocked");
  107. body.removeStyleClass("dock-to-right");
  108. body.removeStyleClass("dock-to-bottom");
  109. break;
  110. }
  111. if (this._isDockingUnavailable && this._dockSide === WebInspector.DockController.State.Undocked) {
  112. this._dockToggleButton.state = "undock";
  113. this._dockToggleButton.setEnabled(false);
  114. return;
  115. }
  116. this._dockToggleButton.setEnabled(true);
  117. // Choose different last state based on the current one if missing or if is the same.
  118. var sides = [WebInspector.DockController.State.DockedToBottom, WebInspector.DockController.State.Undocked, WebInspector.DockController.State.DockedToRight];
  119. sides.remove(this._dockSide);
  120. var lastState = WebInspector.settings.lastDockState.get();
  121. sides.remove(lastState);
  122. if (sides.length === 2) { // last state was not from the list of potential values
  123. lastState = sides[0];
  124. sides.remove(lastState);
  125. }
  126. this._decorateButtonForTargetState(this._dockToggleButton, lastState);
  127. this._decorateButtonForTargetState(this._dockToggleButtonOption, sides[0]);
  128. },
  129. /**
  130. * @param {WebInspector.StatusBarButton} button
  131. * @param {string} state
  132. */
  133. _decorateButtonForTargetState: function(button, state)
  134. {
  135. switch (state) {
  136. case WebInspector.DockController.State.DockedToBottom:
  137. button.title = WebInspector.UIString("Dock to main window.");
  138. button.state = "bottom";
  139. break;
  140. case WebInspector.DockController.State.DockedToRight:
  141. button.title = WebInspector.UIString("Dock to main window.");
  142. button.state = "right";
  143. break;
  144. case WebInspector.DockController.State.Undocked:
  145. button.title = WebInspector.UIString("Undock into separate window.");
  146. button.state = "undock";
  147. break;
  148. }
  149. },
  150. _createDockOptions: function()
  151. {
  152. return [this._dockToggleButtonOption];
  153. },
  154. /**
  155. * @param {WebInspector.Event} e
  156. */
  157. _toggleDockState: function(e)
  158. {
  159. var action;
  160. switch (e.target.state) {
  161. case "bottom": action = "bottom"; break;
  162. case "right": action = "right"; break;
  163. case "undock": action = "undocked"; break;
  164. }
  165. InspectorFrontendHost.requestSetDockSide(action);
  166. },
  167. __proto__: WebInspector.Object.prototype
  168. }
  169. /**
  170. * @type {?WebInspector.DockController}
  171. */
  172. WebInspector.dockController = null;