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.
7 * @implements {WebInspector.TargetManager.Observer}
8 * @param {!Element} selectElement
9 * @param {!Element} elementToHide
11 WebInspector.TargetsComboBoxController = function(selectElement, elementToHide)
13 elementToHide.classList.add("hidden");
14 selectElement.addEventListener("change", this._onComboBoxSelectionChange.bind(this), false);
15 this._selectElement = selectElement;
16 this._elementToHide = elementToHide;
17 /** @type {!Map.<!WebInspector.Target, !Element>} */
18 this._targetToOption = new Map();
20 WebInspector.context.addFlavorChangeListener(WebInspector.Target, this._targetChangedExternally, this);
21 WebInspector.targetManager.observeTargets(this);
24 WebInspector.TargetsComboBoxController.prototype = {
27 * @param {!WebInspector.Target} target
29 targetAdded: function(target)
31 var option = this._selectElement.createChild("option");
32 option.text = target.name();
33 option.__target = target;
34 this._targetToOption.put(target, option);
35 if (WebInspector.context.flavor(WebInspector.Target) === target)
36 this._selectElement.selectedIndex = Array.prototype.indexOf.call(/** @type {?} */ (this._selectElement), option);
38 this._updateVisibility();
42 * @param {!WebInspector.Target} target
44 targetRemoved: function(target)
46 var option = this._targetToOption.remove(target);
47 this._selectElement.removeChild(option);
48 this._updateVisibility();
51 _onComboBoxSelectionChange: function()
53 var selectedOption = this._selectElement[this._selectElement.selectedIndex];
57 WebInspector.context.setFlavor(WebInspector.Target, selectedOption.__target);
60 _updateVisibility: function()
62 var hidden = this._selectElement.childElementCount === 1;
63 this._elementToHide.classList.toggle("hidden", hidden);
67 * @param {!WebInspector.Event} event
69 _targetChangedExternally: function(event)
71 var target = /** @type {?WebInspector.Target} */ (event.data);
73 var option = /** @type {!Element} */ (this._targetToOption.get(target));
79 * @param {!Element} option
81 _select: function(option)
83 this._selectElement.selectedIndex = Array.prototype.indexOf.call(/** @type {?} */ (this._selectElement), option);