iron-overlay-manager.html 3.02 KB
<!--
@license
Copyright (c) 2015 The Polymer Project Authors. All rights reserved.
This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt
The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt
The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt
Code distributed by Google as part of the polymer project is also
subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt
-->

<link rel="import" href="../polymer/polymer.html">

<script>

  Polymer.IronOverlayManager = (function() {

    var overlays = [];
    var DEFAULT_Z = 10;
    var backdrops = [];

    // track overlays for z-index and focus managemant
    function addOverlay(overlay) {
      var z0 = currentOverlayZ();
      overlays.push(overlay);
      var z1 = currentOverlayZ();
      if (z1 <= z0) {
        applyOverlayZ(overlay, z0);
      }
    }

    function removeOverlay(overlay) {
      var i = overlays.indexOf(overlay);
      if (i >= 0) {
        overlays.splice(i, 1);
        setZ(overlay, '');
      }
    }

    function applyOverlayZ(overlay, aboveZ) {
      setZ(overlay, aboveZ + 2);
    }

    function setZ(element, z) {
      element.style.zIndex = z;
    }

    function currentOverlay() {
      var i = overlays.length - 1;
      while (overlays[i] && !overlays[i].opened) {
        --i;
      }
      return overlays[i];
    }

    function currentOverlayZ() {
      var z;
      var current = currentOverlay();
      if (current) {
        var z1 = window.getComputedStyle(current).zIndex;
        if (!isNaN(z1)) {
          z = Number(z1);
        }
      }
      return z || DEFAULT_Z;
    }

    function focusOverlay() {
      var current = currentOverlay();
      // We have to be careful to focus the next overlay _after_ any current
      // transitions are complete (due to the state being toggled prior to the
      // transition). Otherwise, we risk infinite recursion when a transitioning
      // (closed) overlay becomes the current overlay.
      //
      // NOTE: We make the assumption that any overlay that completes a transition
      // will call into focusOverlay to kick the process back off. Currently:
      // transitionend -> _applyFocus -> focusOverlay.
      if (current && !current.transitioning) {
        current._applyFocus();
      }
    }

    function trackBackdrop(element) {
      // backdrops contains the overlays with a backdrop that are currently
      // visible
      if (element.opened) {
        backdrops.push(element);
      } else {
        var index = backdrops.indexOf(element);
        if (index >= 0) {
          backdrops.splice(index, 1);
        }
      }
    }

    function getBackdrops() {
      return backdrops;
    }

    return {
      addOverlay: addOverlay,
      removeOverlay: removeOverlay,
      currentOverlay: currentOverlay,
      currentOverlayZ: currentOverlayZ,
      focusOverlay: focusOverlay,
      trackBackdrop: trackBackdrop,
      getBackdrops: getBackdrops
    };

  })();

</script>