Home | History | Annotate | Download | only in ui
      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  * @extends {WebInspector.Object}
      8  */
      9 WebInspector.DropDownMenu = function()
     10 {
     11     this.element = document.createElementWithClass("select", "drop-down-menu");
     12     this.element.addEventListener("mousedown", this._onBeforeMouseDown.bind(this), true);
     13     this.element.addEventListener("mousedown", consumeEvent, false);
     14     this.element.addEventListener("change", this._onChange.bind(this), false);
     15 }
     16 
     17 WebInspector.DropDownMenu.Events = {
     18     BeforeShow: "BeforeShow",
     19     ItemSelected: "ItemSelected"
     20 }
     21 
     22 WebInspector.DropDownMenu.prototype = {
     23     _onBeforeMouseDown: function()
     24     {
     25         this.dispatchEventToListeners(WebInspector.DropDownMenu.Events.BeforeShow, null);
     26     },
     27 
     28     _onChange: function()
     29     {
     30         var options = this.element.options;
     31         var selectedOption = options[this.element.selectedIndex];
     32         this.dispatchEventToListeners(WebInspector.DropDownMenu.Events.ItemSelected, selectedOption.id);
     33     },
     34 
     35     /**
     36      * @param {string} id
     37      * @param {string} title
     38      */
     39     addItem: function(id, title)
     40     {
     41         var option = new Option(title);
     42         option.id = id;
     43         this.element.appendChild(option);
     44     },
     45 
     46     /**
     47      * @param {?string} id
     48      */
     49     selectItem: function(id)
     50     {
     51         var children = this.element.children;
     52         for (var i = 0; i < children.length; ++i) {
     53             var child = children[i];
     54             if (child.id === id) {
     55                 this.element.selectedIndex = i;
     56                 return;
     57             }
     58         }
     59         this.element.selectedIndex = -1;
     60     },
     61 
     62     clear: function()
     63     {
     64         this.element.removeChildren();
     65     },
     66 
     67     __proto__: WebInspector.Object.prototype
     68 }
     69