1 // Copyright 2013 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 // A simple popup manager.
8 var activePopup = null;
11 // Set up the buttons to toggle the popup.
12 Array.prototype.forEach.call(document.getElementsByTagName('button'),
14 var popupId = button.getAttribute('data-menu');
17 var popup = document.getElementById(popupId);
19 throw new Error('No element with id "' + popupId + '" for popup');
20 button.addEventListener('click', function(event) {
22 event.stopPropagation();
25 // Make clicking anywhere else or pressing escape on the page hide the popup.
26 document.body.addEventListener('click', function() {
29 document.body.addEventListener('keydown', function(event) {
30 if (event.keyCode == 27)
35 function toggle(popup) {
36 if (hideActive() == popup)
38 popup.style.display = 'block';
42 function hideActive() {
43 if (activePopup == null)
45 activePopup.style.display = ''
46 var wasActive = activePopup;