1/*
2 * Copyright (C) 2013 Apple 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
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 *    notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 *    notice, this list of conditions and the following disclaimer in the
11 *    documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 * THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26WebInspector.SidebarPanel = function(identifier, displayName, showToolTip, hideToolTip, image, element, role, label) {
27    WebInspector.Object.call(this);
28
29    this._identifier = identifier;
30
31    this._toolbarItem = new WebInspector.ActivateButtonToolbarItem(identifier, showToolTip, hideToolTip, displayName, image, null, "tab");
32    this._toolbarItem.addEventListener(WebInspector.ButtonNavigationItem.Event.Clicked, this.toggle, this);
33    this._toolbarItem.enabled = false;
34
35    this._element = element || document.createElement("div");
36    this._element.classList.add(WebInspector.SidebarPanel.StyleClassName);
37    this._element.classList.add(identifier);
38
39    this._element.setAttribute("role", role || "group");
40    this._element.setAttribute("aria-label", label || displayName);
41
42};
43
44WebInspector.SidebarPanel.StyleClassName = "panel";
45WebInspector.SidebarPanel.SelectedStyleClassName = "selected";
46
47WebInspector.SidebarPanel.prototype = {
48    constructor: WebInspector.SidebarPanel,
49
50    // Public
51
52    get identifier()
53    {
54        return this._identifier;
55    },
56
57    get toolbarItem()
58    {
59        return this._toolbarItem;
60    },
61
62    get element()
63    {
64        return this._element;
65    },
66
67    get visible()
68    {
69        return this.selected && this._parentSidebar && !this._parentSidebar.collapsed;
70    },
71
72    get selected()
73    {
74        return this._element.classList.contains(WebInspector.SidebarPanel.SelectedStyleClassName);
75    },
76
77    set selected(flag)
78    {
79        if (flag)
80            this._element.classList.add(WebInspector.SidebarPanel.SelectedStyleClassName);
81        else
82            this._element.classList.remove(WebInspector.SidebarPanel.SelectedStyleClassName);
83    },
84
85    get parentSidebar()
86    {
87        return this._parentSidebar;
88    },
89
90    show: function()
91    {
92        if (!this._parentSidebar)
93            return;
94
95        this._parentSidebar.collapsed = false;
96        this._parentSidebar.selectedSidebarPanel = this;
97    },
98
99    hide: function()
100    {
101        if (!this._parentSidebar)
102            return;
103
104        this._parentSidebar.collapsed = true;
105        this._parentSidebar.selectedSidebarPanel = null;
106    },
107
108    toggle: function()
109    {
110        if (this.visible)
111            this.hide();
112        else
113            this.show();
114    },
115
116    added: function()
117    {
118        console.assert(this._parentSidebar);
119        this._toolbarItem.enabled = true;
120        this._toolbarItem.activated = this.visible;
121    },
122
123    removed: function()
124    {
125        console.assert(!this._parentSidebar);
126        this._toolbarItem.enabled = false;
127        this._toolbarItem.activated = false;
128    },
129
130    willRemove: function()
131    {
132        // Implemented by subclasses.
133    },
134
135    shown: function()
136    {
137        // Implemented by subclasses.
138    },
139
140    hidden: function()
141    {
142        // Implemented by subclasses.
143    },
144
145    widthDidChange: function()
146    {
147        // Implemented by subclasses.
148    },
149
150    visibilityDidChange: function()
151    {
152        this._toolbarItem.activated = this.visible;
153    }
154};
155
156WebInspector.SidebarPanel.prototype.__proto__ = WebInspector.Object.prototype;
157