2015-06-19 09:36:51 -07:00
|
|
|
<!--
|
|
|
|
@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>
|
|
|
|
|
2015-11-18 19:35:08 -07:00
|
|
|
Polymer.IronOverlayManager = {
|
2015-06-19 09:36:51 -07:00
|
|
|
|
2015-11-18 19:35:08 -07:00
|
|
|
_overlays: [],
|
|
|
|
|
|
|
|
// iframes have a default z-index of 100, so this default should be at least
|
|
|
|
// that.
|
|
|
|
_minimumZ: 101,
|
|
|
|
|
|
|
|
_backdrops: [],
|
|
|
|
|
|
|
|
_applyOverlayZ: function(overlay, aboveZ) {
|
|
|
|
this._setZ(overlay, aboveZ + 2);
|
|
|
|
},
|
|
|
|
|
|
|
|
_setZ: function(element, z) {
|
|
|
|
element.style.zIndex = z;
|
|
|
|
},
|
2015-06-19 09:36:51 -07:00
|
|
|
|
|
|
|
// track overlays for z-index and focus managemant
|
2015-11-18 19:35:08 -07:00
|
|
|
addOverlay: function(overlay) {
|
|
|
|
var minimumZ = Math.max(this.currentOverlayZ(), this._minimumZ);
|
|
|
|
this._overlays.push(overlay);
|
|
|
|
var newZ = this.currentOverlayZ();
|
|
|
|
if (newZ <= minimumZ) {
|
|
|
|
this._applyOverlayZ(overlay, minimumZ);
|
2015-06-19 09:36:51 -07:00
|
|
|
}
|
2015-11-18 19:35:08 -07:00
|
|
|
},
|
2015-06-19 09:36:51 -07:00
|
|
|
|
2015-11-18 19:35:08 -07:00
|
|
|
removeOverlay: function(overlay) {
|
|
|
|
var i = this._overlays.indexOf(overlay);
|
2015-06-19 09:36:51 -07:00
|
|
|
if (i >= 0) {
|
2015-11-18 19:35:08 -07:00
|
|
|
this._overlays.splice(i, 1);
|
|
|
|
this._setZ(overlay, '');
|
2015-06-19 09:36:51 -07:00
|
|
|
}
|
2015-11-18 19:35:08 -07:00
|
|
|
},
|
2015-06-19 09:36:51 -07:00
|
|
|
|
2015-11-18 19:35:08 -07:00
|
|
|
currentOverlay: function() {
|
|
|
|
var i = this._overlays.length - 1;
|
|
|
|
while (this._overlays[i] && !this._overlays[i].opened) {
|
2015-08-27 21:19:08 -07:00
|
|
|
--i;
|
|
|
|
}
|
2015-11-18 19:35:08 -07:00
|
|
|
return this._overlays[i];
|
|
|
|
},
|
2015-06-19 09:36:51 -07:00
|
|
|
|
2015-11-18 19:35:08 -07:00
|
|
|
currentOverlayZ: function() {
|
|
|
|
var z = this._minimumZ;
|
|
|
|
var current = this.currentOverlay();
|
2015-06-19 09:36:51 -07:00
|
|
|
if (current) {
|
|
|
|
var z1 = window.getComputedStyle(current).zIndex;
|
|
|
|
if (!isNaN(z1)) {
|
|
|
|
z = Number(z1);
|
|
|
|
}
|
|
|
|
}
|
2015-11-18 19:35:08 -07:00
|
|
|
return z;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Ensures that the minimum z-index of new overlays is at least `minimumZ`.
|
|
|
|
* This does not effect the z-index of any existing overlays.
|
|
|
|
*
|
|
|
|
* @param {number} minimumZ
|
|
|
|
*/
|
|
|
|
ensureMinimumZ: function(minimumZ) {
|
|
|
|
this._minimumZ = Math.max(this._minimumZ, minimumZ);
|
|
|
|
},
|
|
|
|
|
|
|
|
focusOverlay: function() {
|
|
|
|
var current = this.currentOverlay();
|
2015-06-19 09:36:51 -07:00
|
|
|
// 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();
|
|
|
|
}
|
2015-11-18 19:35:08 -07:00
|
|
|
},
|
2015-06-19 09:36:51 -07:00
|
|
|
|
2015-11-18 19:35:08 -07:00
|
|
|
trackBackdrop: function(element) {
|
2015-06-19 09:36:51 -07:00
|
|
|
// backdrops contains the overlays with a backdrop that are currently
|
|
|
|
// visible
|
|
|
|
if (element.opened) {
|
2015-11-18 19:35:08 -07:00
|
|
|
this._backdrops.push(element);
|
2015-06-19 09:36:51 -07:00
|
|
|
} else {
|
2015-11-18 19:35:08 -07:00
|
|
|
var index = this._backdrops.indexOf(element);
|
2015-06-19 09:36:51 -07:00
|
|
|
if (index >= 0) {
|
2015-11-18 19:35:08 -07:00
|
|
|
this._backdrops.splice(index, 1);
|
2015-06-19 09:36:51 -07:00
|
|
|
}
|
|
|
|
}
|
2015-11-18 19:35:08 -07:00
|
|
|
},
|
2015-06-19 09:36:51 -07:00
|
|
|
|
2015-11-18 19:35:08 -07:00
|
|
|
getBackdrops: function() {
|
|
|
|
return this._backdrops;
|
2015-06-19 09:36:51 -07:00
|
|
|
}
|
|
|
|
|
2015-11-18 19:35:08 -07:00
|
|
|
};
|
2015-06-19 09:36:51 -07:00
|
|
|
|
|
|
|
</script>
|