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