1/* 2 * Copyright (C) 2013 Apple Inc. All rights reserved. 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions 6 * are met: 7 * 1. Redistributions of source code must retain the above copyright 8 * notice, this list of conditions and the following disclaimer. 9 * 2. Redistributions in binary form must reproduce the above copyright 10 * notice, this list of conditions and the following disclaimer in the 11 * documentation and/or other materials provided with the distribution. 12 * 13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' 14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS 17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 23 * THE POSSIBILITY OF SUCH DAMAGE. 24 */ 25 26WebInspector.ScopeBar = function(identifier, items, defaultItem) { 27 WebInspector.NavigationItem.call(this, identifier); 28 29 this._element.classList.add(WebInspector.ScopeBar.StyleClassName); 30 31 this._items = items; 32 this._defaultItem = defaultItem; 33 34 this._itemsById = []; 35 this._populate(); 36}; 37 38WebInspector.ScopeBar.StyleClassName = "scope-bar"; 39WebInspector.ScopeBar.Event = { 40 SelectionChanged: "scopebar-selection-did-change" 41}; 42 43WebInspector.ScopeBar.prototype = { 44 constructor: WebInspector.ScopeBar, 45 46 // Public 47 48 get defaultItem() 49 { 50 return this._defaultItem; 51 }, 52 53 item: function(id) 54 { 55 return this._itemsById[id]; 56 }, 57 58 get selectedItems() 59 { 60 return this._items.filter(function(item) { 61 return item.selected; 62 }); 63 }, 64 65 updateLayout: function(expandOnly) 66 { 67 if (expandOnly) 68 return; 69 70 for (var i = 0; i < this._items.length; ++i) { 71 var item = this._items[i]; 72 var isSelected = item.selected; 73 74 if (!isSelected) 75 item.element.classList.add(WebInspector.ScopeBarItem.SelectedStyleClassName); 76 77 var selectedWidth = item.element.offsetWidth; 78 if (selectedWidth) 79 item.element.style.minWidth = selectedWidth + "px"; 80 81 if (!isSelected) 82 item.element.classList.remove(WebInspector.ScopeBarItem.SelectedStyleClassName); 83 } 84 }, 85 86 // Private 87 88 _populate: function() 89 { 90 var item; 91 for (var i = 0; i < this._items.length; ++i) { 92 item = this._items[i]; 93 this._itemsById[item.id] = item; 94 this._element.appendChild(item.element); 95 96 item.addEventListener(WebInspector.ScopeBarItem.Event.SelectionChanged, this._itemSelectionDidChange, this); 97 } 98 99 if (!this.selectedItems.length && this._defaultItem) 100 this._defaultItem.selected = true; 101 }, 102 103 _itemSelectionDidChange: function(event) 104 { 105 var sender = event.target; 106 var item; 107 108 // An exclusive item was selected, unselect everything else. 109 if (sender.isExclusive && sender.selected) { 110 for (var i = 0; i < this._items.length; ++i) { 111 item = this._items[i]; 112 if (item !== sender) 113 item.selected = false; 114 } 115 } else { 116 var replacesCurrentSelection = !event.data.withModifier; 117 for (var i = 0; i < this._items.length; ++i) { 118 item = this._items[i]; 119 if (item.isExclusive && item !== sender && sender.selected) 120 item.selected = false; 121 else if (sender.selected && replacesCurrentSelection && sender !== item) 122 item.selected = false; 123 } 124 } 125 126 // If nothing is selected anymore, select the default item. 127 if (!this.selectedItems.length && this._defaultItem) 128 this._defaultItem.selected = true; 129 130 this.dispatchEventToListeners(WebInspector.ScopeBar.Event.SelectionChanged); 131 } 132}; 133 134WebInspector.ScopeBar.prototype.__proto__ = WebInspector.NavigationItem.prototype; 135