1/*
2 * Copyright (C) 2011 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.View}
34 * @param {!WebInspector.ProfilesPanel} profilesPanel
35 */
36WebInspector.ProfileLauncherView = function(profilesPanel)
37{
38    WebInspector.View.call(this);
39
40    this._panel = profilesPanel;
41
42    this.element.addStyleClass("profile-launcher-view");
43    this.element.addStyleClass("panel-enabler-view");
44
45    this._contentElement = this.element.createChild("div", "profile-launcher-view-content");
46    this._innerContentElement = this._contentElement.createChild("div");
47
48    this._controlButton = this._contentElement.createChild("button", "control-profiling");
49    this._controlButton.addEventListener("click", this._controlButtonClicked.bind(this), false);
50}
51
52WebInspector.ProfileLauncherView.prototype = {
53    /**
54     * @param {WebInspector.ProfileType} profileType
55     */
56    addProfileType: function(profileType)
57    {
58        var descriptionElement = this._innerContentElement.createChild("h1");
59        descriptionElement.textContent = profileType.description;
60        var decorationElement = profileType.decorationElement();
61        if (decorationElement)
62            this._innerContentElement.appendChild(decorationElement);
63        this._isInstantProfile = profileType.isInstantProfile();
64    },
65
66    _controlButtonClicked: function()
67    {
68        this._panel.toggleRecordButton();
69    },
70
71    _updateControls: function()
72    {
73        if (this._isInstantProfile) {
74            this._controlButton.removeStyleClass("running");
75            this._controlButton.textContent = WebInspector.UIString("Take Snapshot");
76        } else if (this._isProfiling) {
77            this._controlButton.addStyleClass("running");
78            this._controlButton.textContent = WebInspector.UIString("Stop");
79        } else {
80            this._controlButton.removeStyleClass("running");
81            this._controlButton.textContent = WebInspector.UIString("Start");
82        }
83    },
84
85    profileStarted: function()
86    {
87        this._isProfiling = true;
88        this._updateControls();
89    },
90
91    profileFinished: function()
92    {
93        this._isProfiling = false;
94        this._updateControls();
95    },
96
97    __proto__: WebInspector.View.prototype
98}
99
100
101/**
102 * @constructor
103 * @extends {WebInspector.ProfileLauncherView}
104 * @param {!WebInspector.ProfilesPanel} profilesPanel
105 */
106WebInspector.MultiProfileLauncherView = function(profilesPanel)
107{
108    WebInspector.ProfileLauncherView.call(this, profilesPanel);
109
110    var header = this._innerContentElement.createChild("h1");
111    header.textContent = WebInspector.UIString("Select profiling type");
112
113    this._profileTypeSelectorForm = this._innerContentElement.createChild("form");
114
115    this._innerContentElement.createChild("div", "flexible-space");
116}
117
118WebInspector.MultiProfileLauncherView.EventTypes = {
119    ProfileTypeSelected: "profile-type-selected"
120}
121
122WebInspector.MultiProfileLauncherView.prototype = {
123    /**
124     * @override
125     * @param {WebInspector.ProfileType} profileType
126     */
127    addProfileType: function(profileType)
128    {
129        var checked = !this._profileTypeSelectorForm.children.length;
130        var labelElement = this._profileTypeSelectorForm.createChild("label");
131        labelElement.textContent = profileType.name;
132        var optionElement = document.createElement("input");
133        labelElement.insertBefore(optionElement, labelElement.firstChild);
134        optionElement.type = "radio";
135        optionElement.name = "profile-type";
136        optionElement.style.hidden = true;
137        if (checked) {
138            optionElement.checked = checked;
139            this.dispatchEventToListeners(WebInspector.MultiProfileLauncherView.EventTypes.ProfileTypeSelected, profileType);
140        }
141        optionElement.addEventListener("change", this._profileTypeChanged.bind(this, profileType), false);
142        var descriptionElement = labelElement.createChild("p");
143        descriptionElement.textContent = profileType.description;
144        var decorationElement = profileType.decorationElement();
145        if (decorationElement)
146            labelElement.appendChild(decorationElement);
147    },
148
149    _controlButtonClicked: function()
150    {
151        this._panel.toggleRecordButton();
152    },
153
154    _updateControls: function()
155    {
156        WebInspector.ProfileLauncherView.prototype._updateControls.call(this);
157        var items = this._profileTypeSelectorForm.elements;
158        for (var i = 0; i < items.length; ++i) {
159            if (items[i].type === "radio")
160                items[i].disabled = this._isProfiling;
161        }
162    },
163
164    /**
165     * @param {WebInspector.ProfileType} profileType
166     */
167    _profileTypeChanged: function(profileType, event)
168    {
169        this.dispatchEventToListeners(WebInspector.MultiProfileLauncherView.EventTypes.ProfileTypeSelected, profileType);
170        this._isInstantProfile = profileType.isInstantProfile();
171        this._updateControls();
172    },
173
174    profileStarted: function()
175    {
176        this._isProfiling = true;
177        this._updateControls();
178    },
179
180    profileFinished: function()
181    {
182        this._isProfiling = false;
183        this._updateControls();
184    },
185
186    __proto__: WebInspector.ProfileLauncherView.prototype
187}
188
189