1// Copyright 2014 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5/**
6 * @constructor
7 * @implements {WebInspector.TargetManager.Observer}
8 */
9WebInspector.TargetsToolbar = function()
10{
11    this.element = document.createElement("div");
12    this.element.className = "status-bar scripts-debug-toolbar targets-toolbar hidden";
13    this._comboBox = new WebInspector.StatusBarComboBox(this._onComboBoxSelectionChange.bind(this));
14    this.element.appendChild(this._comboBox.element);
15
16    /** @type {!Map.<!WebInspector.Target, !Element>} */
17    this._targetToOption = new Map();
18    if (!WebInspector.experimentsSettings.workersInMainWindow.isEnabled())
19        return;
20
21    WebInspector.context.addFlavorChangeListener(WebInspector.Target, this._targetChangedExternally, this);
22    WebInspector.targetManager.observeTargets(this);
23}
24
25WebInspector.TargetsToolbar.prototype = {
26
27    /**
28     * @param {!WebInspector.Target} target
29     */
30    targetAdded: function(target)
31    {
32        var option = this._comboBox.createOption(target.name());
33        option.__target = target;
34        this._targetToOption.put(target, option);
35        if (WebInspector.context.flavor(WebInspector.Target) === target)
36            this._comboBox.select(option);
37
38        this._updateVisibility();
39    },
40
41    /**
42     * @param {!WebInspector.Target} target
43     */
44    targetRemoved: function(target)
45    {
46        var option = this._targetToOption.remove(target);
47        this._comboBox.removeOption(option);
48        this._updateVisibility();
49    },
50
51    _onComboBoxSelectionChange: function()
52    {
53        var selectedOption = this._comboBox.selectedOption();
54        if (!selectedOption)
55            return;
56
57        WebInspector.context.setFlavor(WebInspector.Target, selectedOption.__target);
58    },
59
60    _updateVisibility: function()
61    {
62        var hidden = this._comboBox.size() === 1;
63        this.element.classList.toggle("hidden", hidden);
64    },
65
66    /**
67     * @param {!WebInspector.Event} event
68     */
69    _targetChangedExternally: function(event)
70    {
71        var target = /** @type {?WebInspector.Target} */ (event.data);
72        if (target) {
73            var option = /** @type {!Element} */ (this._targetToOption.get(target));
74            this._comboBox.select(option);
75        }
76    }
77
78}
79