OLD | NEW |
(Empty) | |
| 1 |
| 2 |
| 3 Polymer.IronOverlayManager = (function() { |
| 4 |
| 5 var overlays = []; |
| 6 var DEFAULT_Z = 10; |
| 7 var backdrops = []; |
| 8 |
| 9 // track overlays for z-index and focus managemant |
| 10 function addOverlay(overlay) { |
| 11 var z0 = currentOverlayZ(); |
| 12 overlays.push(overlay); |
| 13 var z1 = currentOverlayZ(); |
| 14 if (z1 <= z0) { |
| 15 applyOverlayZ(overlay, z0); |
| 16 } |
| 17 } |
| 18 |
| 19 function removeOverlay(overlay) { |
| 20 var i = overlays.indexOf(overlay); |
| 21 if (i >= 0) { |
| 22 overlays.splice(i, 1); |
| 23 setZ(overlay, ''); |
| 24 } |
| 25 } |
| 26 |
| 27 function applyOverlayZ(overlay, aboveZ) { |
| 28 setZ(overlay, aboveZ + 2); |
| 29 } |
| 30 |
| 31 function setZ(element, z) { |
| 32 element.style.zIndex = z; |
| 33 } |
| 34 |
| 35 function currentOverlay() { |
| 36 return overlays[overlays.length-1]; |
| 37 } |
| 38 |
| 39 function currentOverlayZ() { |
| 40 var z; |
| 41 var current = currentOverlay(); |
| 42 if (current) { |
| 43 var z1 = window.getComputedStyle(current).zIndex; |
| 44 if (!isNaN(z1)) { |
| 45 z = Number(z1); |
| 46 } |
| 47 } |
| 48 return z || DEFAULT_Z; |
| 49 } |
| 50 |
| 51 function focusOverlay() { |
| 52 var current = currentOverlay(); |
| 53 // We have to be careful to focus the next overlay _after_ any current |
| 54 // transitions are complete (due to the state being toggled prior to the |
| 55 // transition). Otherwise, we risk infinite recursion when a transitioning |
| 56 // (closed) overlay becomes the current overlay. |
| 57 // |
| 58 // NOTE: We make the assumption that any overlay that completes a transiti
on |
| 59 // will call into focusOverlay to kick the process back off. Currently: |
| 60 // transitionend -> _applyFocus -> focusOverlay. |
| 61 if (current && !current.transitioning) { |
| 62 current._applyFocus(); |
| 63 } |
| 64 } |
| 65 |
| 66 function trackBackdrop(element) { |
| 67 // backdrops contains the overlays with a backdrop that are currently |
| 68 // visible |
| 69 if (element.opened) { |
| 70 backdrops.push(element); |
| 71 } else { |
| 72 var index = backdrops.indexOf(element); |
| 73 if (index >= 0) { |
| 74 backdrops.splice(index, 1); |
| 75 } |
| 76 } |
| 77 } |
| 78 |
| 79 function getBackdrops() { |
| 80 return backdrops; |
| 81 } |
| 82 |
| 83 return { |
| 84 addOverlay: addOverlay, |
| 85 removeOverlay: removeOverlay, |
| 86 currentOverlay: currentOverlay, |
| 87 currentOverlayZ: currentOverlayZ, |
| 88 focusOverlay: focusOverlay, |
| 89 trackBackdrop: trackBackdrop, |
| 90 getBackdrops: getBackdrops |
| 91 }; |
| 92 |
| 93 })(); |
| 94 |
OLD | NEW |