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 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above
12 * copyright notice, this list of conditions and the following disclaimer
13 * in the documentation and/or other materials provided with the
14 * distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY GOOGLE INC. AND ITS CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GOOGLE INC.
20 * OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29/**
30 * @constructor
31 * @param {WebInspector.SidebarView} parentSidebarView
32 * @param {WebInspector.View} navigatorView
33 * @param {WebInspector.View} editorView
34 */
35WebInspector.NavigatorOverlayController = function(parentSidebarView, navigatorView, editorView)
36{
37    this._parentSidebarView = parentSidebarView;
38    this._navigatorView = navigatorView;
39    this._editorView = editorView;
40
41    this._navigatorSidebarResizeWidgetElement = this._navigatorView.element.createChild("div", "resizer-widget");
42    this._parentSidebarView.installResizer(this._navigatorSidebarResizeWidgetElement);
43
44    this._navigatorShowHideButton = new WebInspector.StatusBarButton(WebInspector.UIString("Hide navigator"), "left-sidebar-show-hide-button scripts-navigator-show-hide-button", 3);
45    this._navigatorShowHideButton.state = "left";
46    this._navigatorShowHideButton.addEventListener("click", this._toggleNavigator, this);
47    this._editorView.element.appendChild(this._navigatorShowHideButton.element);
48
49    WebInspector.settings.navigatorHidden = WebInspector.settings.createSetting("navigatorHidden", true);
50    if (WebInspector.settings.navigatorHidden.get())
51        this._toggleNavigator();
52}
53
54WebInspector.NavigatorOverlayController.prototype = {
55    wasShown: function()
56    {
57        window.setTimeout(this._maybeShowNavigatorOverlay.bind(this), 0);
58    },
59
60    _maybeShowNavigatorOverlay: function()
61    {
62        if (WebInspector.settings.navigatorHidden.get() && !WebInspector.settings.navigatorWasOnceHidden.get())
63            this.showNavigatorOverlay();
64    },
65
66    _toggleNavigator: function()
67    {
68        if (this._navigatorShowHideButton.state === "overlay")
69            this._pinNavigator();
70        else if (this._navigatorShowHideButton.state === "right")
71            this.showNavigatorOverlay();
72        else
73            this._hidePinnedNavigator();
74    },
75
76    _hidePinnedNavigator: function()
77    {
78        this._navigatorShowHideButton.state = "right";
79        this._navigatorShowHideButton.title = WebInspector.UIString("Show navigator");
80        this._parentSidebarView.element.appendChild(this._navigatorShowHideButton.element);
81
82        this._editorView.element.addStyleClass("navigator-hidden");
83        this._navigatorSidebarResizeWidgetElement.addStyleClass("hidden");
84
85        this._parentSidebarView.hideSidebarElement();
86        this._navigatorView.detach();
87        this._editorView.focus();
88
89        WebInspector.settings.navigatorWasOnceHidden.set(true);
90        WebInspector.settings.navigatorHidden.set(true);
91    },
92
93    _pinNavigator: function()
94    {
95        this._navigatorShowHideButton.state = "left";
96        this._navigatorShowHideButton.title = WebInspector.UIString("Hide navigator");
97
98        this._editorView.element.removeStyleClass("navigator-hidden");
99        this._navigatorSidebarResizeWidgetElement.removeStyleClass("hidden");
100        this._editorView.element.appendChild(this._navigatorShowHideButton.element);
101
102        this._innerHideNavigatorOverlay();
103        this._parentSidebarView.showSidebarElement();
104        this._navigatorView.show(this._parentSidebarView.sidebarElement);
105        this._navigatorView.focus();
106        WebInspector.settings.navigatorHidden.set(false);
107    },
108
109    showNavigatorOverlay: function()
110    {
111        if (this._navigatorShowHideButton.state === "overlay")
112            return;
113
114        this._navigatorShowHideButton.state = "overlay";
115        this._navigatorShowHideButton.title = WebInspector.UIString("Pin navigator");
116
117        this._sidebarOverlay = new WebInspector.SidebarOverlay(this._navigatorView, "scriptsPanelNavigatorOverlayWidth", Preferences.minScriptsSidebarWidth);
118        this._boundKeyDown = this._keyDown.bind(this);
119        this._sidebarOverlay.element.addEventListener("keydown", this._boundKeyDown, false);
120
121        var navigatorOverlayResizeWidgetElement = document.createElement("div");
122        navigatorOverlayResizeWidgetElement.addStyleClass("resizer-widget");
123        this._sidebarOverlay.resizerWidgetElement = navigatorOverlayResizeWidgetElement;
124
125        this._navigatorView.element.appendChild(this._navigatorShowHideButton.element);
126        this._boundContainingElementFocused = this._containingElementFocused.bind(this);
127        this._parentSidebarView.element.addEventListener("mousedown", this._boundContainingElementFocused, false);
128
129        this._sidebarOverlay.show(this._parentSidebarView.element);
130        this._navigatorView.focus();
131    },
132
133    _keyDown: function(event)
134    {
135        if (event.handled)
136            return;
137
138        if (event.keyCode === WebInspector.KeyboardShortcut.Keys.Esc.code) {
139            this.hideNavigatorOverlay();
140            event.consume(true);
141        }
142    },
143
144    hideNavigatorOverlay: function()
145    {
146        if (this._navigatorShowHideButton.state !== "overlay")
147            return;
148
149        this._navigatorShowHideButton.state = "right";
150        this._navigatorShowHideButton.title = WebInspector.UIString("Show navigator");
151        this._parentSidebarView.element.appendChild(this._navigatorShowHideButton.element);
152
153        this._innerHideNavigatorOverlay();
154        this._editorView.focus();
155    },
156
157    _innerHideNavigatorOverlay: function()
158    {
159        this._parentSidebarView.element.removeEventListener("mousedown", this._boundContainingElementFocused, false);
160        this._sidebarOverlay.element.removeEventListener("keydown", this._boundKeyDown, false);
161        this._sidebarOverlay.hide();
162    },
163
164    _containingElementFocused: function(event)
165    {
166        if (!event.target.isSelfOrDescendant(this._sidebarOverlay.element))
167            this.hideNavigatorOverlay();
168    },
169
170    isNavigatorPinned: function()
171    {
172        return this._navigatorShowHideButton.state === "left";
173    },
174
175    isNavigatorHidden: function()
176    {
177        return this._navigatorShowHideButton.state === "right";
178    }
179}
180