Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / third_party / WebKit / Source / devtools / front_end / profiler / TargetsComboBoxController.js
blob1942cbd80fcd152da0859284d4bcc68619f73f49
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.
5 /**
6 * @constructor
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 = {
26 /**
27 * @override
28 * @param {!WebInspector.Target} target
30 targetAdded: function(target)
32 if (!target.hasJSContext())
33 return;
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();
44 /**
45 * @override
46 * @param {!WebInspector.Target} target
48 targetRemoved: function(target)
50 if (!target.hasJSContext())
51 return;
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];
60 if (!selectedOption)
61 return;
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);
72 /**
73 * @param {!WebInspector.Event} event
75 _targetChangedExternally: function(event)
77 var target = /** @type {?WebInspector.Target} */ (event.data);
78 if (target) {
79 var option = /** @type {!Element} */ (this._targetToOption.get(target));
80 this._select(option);
84 /**
85 * @param {!Element} option
87 _select: function(option)
89 this._selectElement.selectedIndex = Array.prototype.indexOf.call(/** @type {?} */ (this._selectElement), option);