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 = {
28 * @param {!WebInspector.Target} target
30 targetAdded: function(target
)
32 if (!target
.hasJSContext())
34 var option
= this._selectElement
.createChild("option");
35 option
.text
= target
.name();
36 option
.__target
= target
;
37 this._targetToOption
.set(target
, option
);
38 if (WebInspector
.context
.flavor(WebInspector
.Target
) === target
)
39 this._selectElement
.selectedIndex
= Array
.prototype.indexOf
.call(/** @type {?} */ (this._selectElement
), option
);
41 this._updateVisibility();
46 * @param {!WebInspector.Target} target
48 targetRemoved: function(target
)
50 if (!target
.hasJSContext())
52 var option
= this._targetToOption
.remove(target
);
53 this._selectElement
.removeChild(option
);
54 this._updateVisibility();
57 _onComboBoxSelectionChange: function()
59 var selectedOption
= this._selectElement
[this._selectElement
.selectedIndex
];
63 WebInspector
.context
.setFlavor(WebInspector
.Target
, selectedOption
.__target
);
66 _updateVisibility: function()
68 var hidden
= this._selectElement
.childElementCount
=== 1;
69 this._elementToHide
.classList
.toggle("hidden", hidden
);
73 * @param {!WebInspector.Event} event
75 _targetChangedExternally: function(event
)
77 var target
= /** @type {?WebInspector.Target} */ (event
.data
);
79 var option
= /** @type {!Element} */ (this._targetToOption
.get(target
));
85 * @param {!Element} option
87 _select: function(option
)
89 this._selectElement
.selectedIndex
= Array
.prototype.indexOf
.call(/** @type {?} */ (this._selectElement
), option
);