1 // Copyright (c) 2012 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.
6 * @fileoverview Provides dialog-like behaviors for the tracing UI.
8 cr.define('cr.ui.overlay', function() {
11 * Gets the top, visible overlay. It makes the assumption that if multiple
12 * overlays are visible, the last in the byte order is topmost.
13 * TODO(estade): rely on aria-visibility instead?
14 * @return {HTMLElement} The overlay.
16 function getTopOverlay() {
17 var overlays = document.querySelectorAll('.overlay:not([hidden])');
18 return overlays[overlays.length - 1];
22 * Makes initializations which must hook at the document level.
24 function globalInitialization() {
25 // Close the overlay on escape.
26 document.addEventListener('keydown', function(e) {
27 if (e.keyCode == 27) { // Escape
28 var overlay = getTopOverlay();
32 cr.dispatchSimpleEvent(overlay, 'cancelOverlay');
36 window.addEventListener('resize', setMaxHeightAllPages);
38 setMaxHeightAllPages();
42 * Sets the max-height of all pages in all overlays, based on the window
45 function setMaxHeightAllPages() {
46 var pages = document.querySelectorAll('.overlay .page');
48 var maxHeight = Math.min(0.9 * window.innerHeight, 640) + 'px';
49 for (var i = 0; i < pages.length; i++)
50 pages[i].style.maxHeight = maxHeight;
54 * Adds behavioral hooks for the given overlay.
55 * @param {HTMLElement} overlay The .overlay.
57 function setupOverlay(overlay) {
58 // Close the overlay on clicking any of the pages' close buttons.
59 var closeButtons = overlay.querySelectorAll('.page > .close-button');
60 for (var i = 0; i < closeButtons.length; i++) {
61 closeButtons[i].addEventListener('click', function(e) {
62 cr.dispatchSimpleEvent(overlay, 'cancelOverlay');
66 // Remove the 'pulse' animation any time the overlay is hidden or shown.
67 overlay.__defineSetter__('hidden', function(value) {
68 this.classList.remove('pulse');
70 this.setAttribute('hidden', true);
72 this.removeAttribute('hidden');
74 overlay.__defineGetter__('hidden', function() {
75 return this.hasAttribute('hidden');
78 // Shake when the user clicks away.
79 overlay.addEventListener('click', function(e) {
80 // Only pulse if the overlay was the target of the click.
84 // This may be null while the overlay is closing.
85 var overlayPage = this.querySelector('.page:not([hidden])');
87 overlayPage.classList.add('pulse');
89 overlay.addEventListener('webkitAnimationEnd', function(e) {
90 e.target.classList.remove('pulse');
95 globalInitialization: globalInitialization,
96 setupOverlay: setupOverlay,
100 document.addEventListener('DOMContentLoaded',
101 cr.ui.overlay.globalInitialization);