Home | History | Annotate | Download | only in components
      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  * @param {!Element} selectElement
      9  * @param {!Element} elementToHide
     10  */
     11 WebInspector.TargetsComboBoxController = function(selectElement, elementToHide)
     12 {
     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();
     19 
     20     WebInspector.context.addFlavorChangeListener(WebInspector.Target, this._targetChangedExternally, this);
     21     WebInspector.targetManager.observeTargets(this);
     22 }
     23 
     24 WebInspector.TargetsComboBoxController.prototype = {
     25 
     26     /**
     27      * @param {!WebInspector.Target} target
     28      */
     29     targetAdded: function(target)
     30     {
     31         var option = this._selectElement.createChild("option");
     32         option.text = target.name();
     33         option.__target = target;
     34         this._targetToOption.set(target, option);
     35         if (WebInspector.context.flavor(WebInspector.Target) === target)
     36             this._selectElement.selectedIndex = Array.prototype.indexOf.call(/** @type {?} */ (this._selectElement), 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._selectElement.removeChild(option);
     48         this._updateVisibility();
     49     },
     50 
     51     _onComboBoxSelectionChange: function()
     52     {
     53         var selectedOption = this._selectElement[this._selectElement.selectedIndex];
     54         if (!selectedOption)
     55             return;
     56 
     57         WebInspector.context.setFlavor(WebInspector.Target, selectedOption.__target);
     58     },
     59 
     60     _updateVisibility: function()
     61     {
     62         var hidden = this._selectElement.childElementCount === 1;
     63         this._elementToHide.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._select(option);
     75         }
     76     },
     77 
     78     /**
     79      * @param {!Element} option
     80      */
     81     _select: function(option)
     82     {
     83         this._selectElement.selectedIndex = Array.prototype.indexOf.call(/** @type {?} */ (this._selectElement), option);
     84     }
     85 
     86 }
     87