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 = document.createElement("div");
42    this._navigatorSidebarResizeWidgetElement.addStyleClass("scripts-navigator-resizer-widget");
43    this._parentSidebarView.installResizer(this._navigatorSidebarResizeWidgetElement);
44    this._navigatorView.element.appendChild(this._navigatorSidebarResizeWidgetElement);
45
46    this._navigatorShowHideButton = new WebInspector.StatusBarButton(WebInspector.UIString("Hide navigator"), "scripts-navigator-show-hide-button", 3);
47    this._navigatorShowHideButton.state = "pinned";
48    this._navigatorShowHideButton.addEventListener("click", this._toggleNavigator, this);
49    this._editorView.element.appendChild(this._navigatorShowHideButton.element);
50
51    WebInspector.settings.navigatorHidden = WebInspector.settings.createSetting("navigatorHidden", true);
52    if (WebInspector.settings.navigatorHidden.get())
53        this._toggleNavigator();
54}
55
56WebInspector.NavigatorOverlayController.prototype = {
57    wasShown: function()
58    {
59        window.setTimeout(this._maybeShowNavigatorOverlay.bind(this), 0);
60    },
61
62    _maybeShowNavigatorOverlay: function()
63    {
64        if (WebInspector.settings.navigatorHidden.get() && !WebInspector.settings.navigatorWasOnceHidden.get())
65            this.showNavigatorOverlay();
66    },
67
68    _toggleNavigator: function()
69    {
70        if (this._navigatorShowHideButton.state === "overlay")
71            this._pinNavigator();
72        else if (this._navigatorShowHideButton.state === "hidden")
73            this.showNavigatorOverlay();
74        else
75            this._hidePinnedNavigator();
76    },
77
78    _hidePinnedNavigator: function()
79    {
80        this._navigatorShowHideButton.state = "hidden";
81        this._navigatorShowHideButton.title = WebInspector.UIString("Show navigator");
82        this._parentSidebarView.element.appendChild(this._navigatorShowHideButton.element);
83
84        this._editorView.element.addStyleClass("navigator-hidden");
85        this._navigatorSidebarResizeWidgetElement.addStyleClass("hidden");
86
87        this._parentSidebarView.hideSidebarElement();
88        this._navigatorView.detach();
89        this._editorView.focus();
90
91        WebInspector.settings.navigatorWasOnceHidden.set(true);
92        WebInspector.settings.navigatorHidden.set(true);
93    },
94
95    _pinNavigator: function()
96    {
97        this._navigatorShowHideButton.state = "pinned";
98        this._navigatorShowHideButton.title = WebInspector.UIString("Hide navigator");
99
100        this._editorView.element.removeStyleClass("navigator-hidden");
101        this._navigatorSidebarResizeWidgetElement.removeStyleClass("hidden");
102        this._editorView.element.appendChild(this._navigatorShowHideButton.element);
103
104        this._innerHideNavigatorOverlay();
105        this._parentSidebarView.showSidebarElement();
106        this._navigatorView.show(this._parentSidebarView.sidebarElement);
107        this._navigatorView.focus();
108        WebInspector.settings.navigatorHidden.set(false);
109    },
110
111    showNavigatorOverlay: function()
112    {
113        if (this._navigatorShowHideButton.state === "overlay")
114            return;
115
116        this._navigatorShowHideButton.state = "overlay";
117        this._navigatorShowHideButton.title = WebInspector.UIString("Pin navigator");
118
119        this._sidebarOverlay = new WebInspector.SidebarOverlay(this._navigatorView, "scriptsPanelNavigatorOverlayWidth", Preferences.minScriptsSidebarWidth);
120        this._boundKeyDown = this._keyDown.bind(this);
121        this._sidebarOverlay.element.addEventListener("keydown", this._boundKeyDown, false);
122        var navigatorOverlayResizeWidgetElement = document.createElement("div");
123        navigatorOverlayResizeWidgetElement.addStyleClass("scripts-navigator-resizer-widget");
124        this._sidebarOverlay.resizerWidgetElement = navigatorOverlayResizeWidgetElement;
125
126        this._navigatorView.element.appendChild(this._navigatorShowHideButton.element);
127        this._boundContainingElementFocused = this._containingElementFocused.bind(this);
128        this._parentSidebarView.element.addEventListener("mousedown", this._boundContainingElementFocused, false);
129
130        this._sidebarOverlay.show(this._parentSidebarView.element);
131        this._navigatorView.focus();
132    },
133
134    _keyDown: function(event)
135    {
136        if (event.handled)
137            return;
138
139        if (event.keyCode === WebInspector.KeyboardShortcut.Keys.Esc.code) {
140            this.hideNavigatorOverlay();
141            event.consume(true);
142        }
143    },
144
145    hideNavigatorOverlay: function()
146    {
147        if (this._navigatorShowHideButton.state !== "overlay")
148            return;
149
150        this._navigatorShowHideButton.state = "hidden";
151        this._navigatorShowHideButton.title = WebInspector.UIString("Show navigator");
152        this._parentSidebarView.element.appendChild(this._navigatorShowHideButton.element);
153
154        this._innerHideNavigatorOverlay();
155        this._editorView.focus();
156    },
157
158    _innerHideNavigatorOverlay: function()
159    {
160        this._parentSidebarView.element.removeEventListener("mousedown", this._boundContainingElementFocused, false);
161        this._sidebarOverlay.element.removeEventListener("keydown", this._boundKeyDown, false);
162        this._sidebarOverlay.hide();
163    },
164
165    _containingElementFocused: function(event)
166    {
167        if (!event.target.isSelfOrDescendant(this._sidebarOverlay.element))
168            this.hideNavigatorOverlay();
169    },
170
171    isNavigatorPinned: function()
172    {
173        return this._navigatorShowHideButton.state === "pinned";
174    },
175
176    isNavigatorHidden: function()
177    {
178        return this._navigatorShowHideButton.state === "hidden";
179    }
180}
181