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 */
35WebInspector.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.setLongClickOptionsEnabled(this._createDockOptions.bind(this));
42
43    this.setDockSide(WebInspector.queryParamsObject["can_dock"] ? (WebInspector.queryParamsObject["dockSide"] || "bottom") : "undocked");
44}
45
46WebInspector.DockController.State = {
47    DockedToBottom: "bottom",
48    DockedToRight: "right",
49    Undocked: "undocked"
50}
51
52WebInspector.DockController.Events = {
53    DockSideChanged: "DockSideChanged"
54}
55
56WebInspector.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    _updateUI: function()
94    {
95        var body = document.body;
96        switch (this._dockSide) {
97        case WebInspector.DockController.State.DockedToBottom:
98            body.classList.remove("undocked");
99            body.classList.remove("dock-to-right");
100            body.classList.add("dock-to-bottom");
101            break;
102        case WebInspector.DockController.State.DockedToRight:
103            body.classList.remove("undocked");
104            body.classList.add("dock-to-right");
105            body.classList.remove("dock-to-bottom");
106            break;
107        case WebInspector.DockController.State.Undocked:
108            body.classList.add("undocked");
109            body.classList.remove("dock-to-right");
110            body.classList.remove("dock-to-bottom");
111            break;
112        }
113
114        this._dockToggleButton.setEnabled(true);
115
116        // Choose different last state based on the current one if missing or if is the same.
117        var sides = [WebInspector.DockController.State.DockedToBottom, WebInspector.DockController.State.Undocked, WebInspector.DockController.State.DockedToRight];
118        sides.remove(this._dockSide);
119        var lastState = WebInspector.settings.lastDockState.get();
120
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    /**
131     * @param {!WebInspector.StatusBarButton} button
132     * @param {string} state
133     */
134    _decorateButtonForTargetState: function(button, state)
135    {
136        switch (state) {
137        case WebInspector.DockController.State.DockedToBottom:
138            button.title = WebInspector.UIString("Dock to main window.");
139            button.state = "bottom";
140            break;
141        case WebInspector.DockController.State.DockedToRight:
142            button.title = WebInspector.UIString("Dock to main window.");
143            button.state = "right";
144            break;
145        case WebInspector.DockController.State.Undocked:
146            button.title = WebInspector.UIString("Undock into separate window.");
147            button.state = "undock";
148            break;
149        }
150    },
151
152    _createDockOptions: function()
153    {
154        return [this._dockToggleButtonOption];
155    },
156
157    /**
158     * @param {!WebInspector.Event} e
159     */
160    _toggleDockState: function(e)
161    {
162        var action;
163        switch (e.target.state) {
164        case "bottom": action = "bottom"; break;
165        case "right": action = "right"; break;
166        case "undock": action = "undocked"; break;
167        }
168        InspectorFrontendHost.requestSetDockSide(action);
169    },
170
171    __proto__: WebInspector.Object.prototype
172}
173
174/**
175 * @type {!WebInspector.DockController}
176 */
177WebInspector.dockController;
178